1.0.42 • Published 3 years ago

bookshelf-dump v1.0.42

Weekly downloads
1
License
MIT
Repository
-
Last release
3 years ago

Bookshelf Dump

Build

This bookshelf plugin allows you to drop a basic object into a .dump() function that will then check if the columns exist and set() the data for you. The main use case for this is dumping request.body objects into the function to then be applied to your model without running the risk of setting an attribute that doesn't exist.

Installation with NPM

npm i bookshelf-dump --save

Example usage

Bookshelf.js

const knex = require('knex')(require('./knexfile'));
const bookshelf = require('bookshelf')(knex);

bookshelf.plugin('bookshelf-dump');
module.exports = bookshelf;

Model.js

const bookshelf = require('../bookshelf');

const Event = bookshelf.Model.extend({
  tableName: 'table_name',
  hasTimestamps: true,
});

module.exports = bookshelf.model('Event', Event);

Controller.js

const Model = require('./model');

// req.body = { name: "New Name", description: "Updated" }
const update = async (req, res) => {
  try {
    // model.attributes = { name: "Name", description: "Original" }
    let model = await Model.where('name', req.params.name).fetch();
    model.dump(req.body);

    model = await model.save(); // model.attribute = { name: "New Name", description: "Updated" }

    return res.json(model);
  } catch(e) {
    return res.json({
      errors: e
    });
  }
}

Testing

Testing of the plugin is seamless as it uses the mocha.js testing framework with the chai.js TDD and BDD assertion library.

To test the plugin simply run npm test

1.0.42

3 years ago

1.0.41

4 years ago

1.0.40

4 years ago

1.0.39

4 years ago

1.0.38

4 years ago

1.0.37

4 years ago

1.0.36

4 years ago

1.0.35

4 years ago

1.0.34

4 years ago

1.0.2

5 years ago

1.0.1

6 years ago

1.0.0

6 years ago