bbm

bookshelf-base-model

Base model for Bookshelf ORM with some goodies

Showing:

Popularity

Downloads/wk

1

GitHub Stars

0

Maintenance

Last Commit

6yrs ago

Contributors

1

Package

Dependencies

1

Size (min+gzip)

29.7KB

License

MIT

Type Definitions

Tree-Shakeable

No?

Categories

Readme

bookshelf-base-model

Base model for bookshelf.

Install

npm install bookshelf-base-model --save

Usage

const knex = require('knex')({...})
const bookshelf = require('bookshelf')(knex)
const BaseModel = require('bookshelf-base-model')(bookshelf)

const User = BaseModel.extend({
  tableName: 'user',

  fields: {
    username: {
      validate: ['required']
    },
    email: {
      validate: ['required', 'email']
    },
    other: {}
  }
})

Features

Permitted fields

Allow only attributes from fields hash to go into database.

let user = User.forge({
  username: 'some-user-name',
  email: 'user@name.com',
  nonofield: 'invalid field'
})
user
  .save()
  .then(user => {
    // user saved without error
    // user.get('nonofield') === undefined
  })

Validation

Add validation to your fields using awesome Checkit module. It supports both sync and async validation. List of all available validators can be found here.

let user = User.forge({ email: 'invalid@email' })
user
  .save()
  .catch(User.ValidationError, function (err) {
    // handle validation error
  })
  .catch(function (err) {
    // handle db/other error
  })

Test

npm test

Licence

MIT

Rate & Review

Great Documentation0
Easy to Use0
Performant0
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Slow0
Buggy0
Abandoned0
Unwelcoming Community0
100