Socket
Socket
Sign inDemoInstall

bookshelf-model

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bookshelf-model

Save your models in a central location so that you can refer them easily instead of having to require it every time


Version published
Weekly downloads
1
Maintainers
1
Weekly downloads
 
Created
Source

bookshelf-model

Save your models in a central location so that you can refer them easily instead of having to require it every time and helps deal with the challenges of circular module dependencies in Node.

Usage:

You must define your models as this:

var models = null;

module.exports = function(bookshelf) {
  var Administrator = bookshelf.Model.extend({
    tableName: 'administrators',
    user: function() {
      return this.belongsTo(models.User);
    }
  },
  {
    // if your model has one or more relations, you should define this function
    // db contains all the models that have been found
    associate: function(db) {
      models = db;
    }
  });

  return Administrator;
}

In your main app.js file simply call the helper (the first parameter is the bookshelf object and the second one is the directory where your models are):

//...

var knex = require('knex')(dbConfig.development),
    bookshelf = require('bookshelf')(knex),
    bookshelfModel = require('bookshelf-model');
    
var models = bookshelfModel(bookshelf, __dirname + '/models');

// if you are using express you should do this
app.locals.models = models;

//...

The model's name is taken from the file's name, if your file is named user.js the model's name will be User. To separate words of a file's name you must use sneake_case and the model's name wil be in PascalCase (E.g: police_officer -> PoliceOfficer)

Feedback:

Pull requests, feature ideas and bug reports are welcome

License:

MIT

Keywords

FAQs

Package last updated on 11 Sep 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc