Skip to content

Latest commit

 

History

History

plugin-with-options

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

Objection.js example plugin with options

This project serves as the best practices example of an objection.js plugin that takes options.

The plugin adds a session method for QueryBuilder and extends a model so that it sets modifiedAt, modifiedBy, createdAt and createdBy properties automatically based on the given session.

This example is exactly the same as the plugin example but this one accepts options. The only difference is that the main module is a factory method that accepts options and returns a mixin.

Usage example:

const Model = require('objection').Model;

const Session = require('path/to/this/example')({
  setCreatedBy: false,
  setModifiedBy: false
});

class Person extends Session(Model) {
  static get tableName() {
    return 'Person';
  }
}

module.exports = Person;
// expressjs route.
router.post('/persons', (req, res) => {
  return (
    Person.query()
      // The following method was added by our plugin.
      .session(req.session)
      .insert(req.body)
      .then(person => {
        // Our plugin set the following property.
        console.log(person.createdAt);
        // This wasn't set because of the `setCreatedBy: false` option.
        console.log(person.createdBy); // -->  undefined

        res.send(person);
      })
  );
});