npm install bookshelf-django --save
yarn add bookshelf-django
Load models to registry
require('bookshelf-django')(bookshelf);
var AuthUser = bookshelf.model('Django.Auth.User');
or load model/collection
var AuthUser = require('bookshelf-django')(bookshelf).Auth.User;
Django session framework model
Django session framework collection
Django site model
Django site collection
Django flat page model
Returns string representation of a permission.
const FlatPage = bookshelf.model('Django.FlatPage')
FlatPage.forge({id: 1})
.fetch()
.then(function(flat_page) {
console.log(flat_page.toString())
})
Django flat pages collection
Django redirect model
Returns string representation of a redirect.
const Redirect = bookshelf.model('Django.Redirect')
Redirect.forge({id: 1})
.fetch()
.then(function(redirect) {
console.log(redirect.toString())
})
Django redirects collection
Django content type model
Django content type collection
Permission model
Returns string representation of a permission.
const AuthPermission = bookshelf.model('Django.Auth.Permission')
AuthPermission.forge({id: 1})
.fetch({withRelated: 'contentType'})
.then(function(permission) {
console.log(permission.toString())
})
Returns Promise
with string representation of a permission.
const AuthPermissions = bookshelf.collection('Django.Auth.Permissions')
AuthPermission.forge({id: 1})
.fetch({withRelated: 'contentType'})
.then(function(permission) {
return permission.toStringAsync()
}).then(function(permission) {
console.log(permission)
})
Parameters
-
permissionModel
Django.Auth.Permission
Django.Auth.Permission
object
Stringify a Django.Auth.Permission
object.
const AuthPermission = bookshelf.model('Django.Auth.Permission')
AuthPermission.forge({id: 1})
.fetch()
.then(AuthPermission.stringify)
.then(function(permission) {
console.log(permission)
})
Permission collection
Groups of permissions model
Collection of permissions of group permissions.
const AuthPermission = bookshelf.model('Django.Auth.Permission')
const AuthGroup = bookshelf.model('Django.Auth.Group')
AuthGroup.forge()
.fetch({withRelated: ['permissions', 'permissions.contentType']})
.then(function(permissions) {
return permissions.getPermissions()
}).then(function(permissions) {
console.log(permissions.map(AuthPermission.stringify))
})
Groups of permissions collection
Collection of unique permissions of group permissions.
const AuthPermission = bookshelf.model('Django.Auth.Permission')
const AuthGroups = bookshelf.collection('Django.Auth.Groups')
AuthGroups.all()
.fetch({withRelated: ['permissions', 'permissions.contentType']})
.then(function(permissions) {
return permissions.getPermissions()
}).then(function(permissions) {
console.log(permissions.map(AuthPermission.stringify))
})
Django user collection
Collection of unique permissions of group permissions and permissions.
const AuthPermission = bookshelf.model('Django.Auth.Permission')
const AuthUser = bookshelf.model('Django.Auth.User')
AuthUser.forge({id: 1})
.fetch()
.then(function(user) {
return user.getPermissions()
}).then(function(permissions) {
console.log(permissions.map(AuthPermission.stringify))
})
Django user collection
In beforeAll
hook runs knex migrations in order
- Django
- Your project
All migrations must have own migration table (tableName
property).
before(() => {
knex.migrate.latest({directory: 'node_modules/bookshelf-django/migrations/', tableName: 'knex_migrations_django'})
}).then(() => {
knex.migrate.latest({directory: 'migrations/', tableName: 'knex_migrations_my_project'})
})
In afterAll
hook runs knex migrations in reverse order
- Your project
- Django
after(() => {
knex.migrate.rollback({directory: 'migrations/', tableName: 'knex_migrations_my_project'})
}).then(() => {
knex.migrate.rollback({directory: 'node_modules/bookshelf-django/migrations/', tableName: 'knex_migrations_django'})
})