Created
December 6, 2018 12:47
-
-
Save kirrg001/b8650a544db01541828d50fbcd1a227d to your computer and use it in GitHub Desktop.
bookshelf previous attrs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var assert = require('assert'); | |
var knex = require('knex')({ | |
client: 'sqlite3', | |
connection: {filename: ':memory:'} | |
}); | |
var bookshelf = require('bookshelf')(knex); | |
var User = bookshelf.Model.extend({ | |
tableName: 'users', | |
initialize: function () { | |
this.on('updated', function (user) { | |
console.log('prev attrs on updated', user.previousAttributes()); | |
}); | |
} | |
}); | |
var Users = bookshelf.Collection.extend({ | |
model: User | |
}); | |
return knex.schema.createTable('users', function (table) { | |
table.increments(); | |
table.text('status').notNullable(); | |
table.text('name').notNullable(); | |
}).then(function () { | |
return knex('users').insert({status: 'active', name: 'ralf'}); | |
}).then(function () { | |
return new User({id: 1}).fetch(); | |
}).then(function (user) { | |
assert.strictEqual(user.get('status'), 'active'); | |
user.set('status', 'active'); | |
return user.save(); | |
}).then(function () { | |
console.log('all done model'); | |
return Users.forge({id: 1}).fetch(); | |
}).then(function (users) { | |
console.log('prev attrs on collection fetch', users.models[0].previousAttributes()); | |
}).catch(function (err) { | |
console.log(err); | |
console.log(err.stack); | |
}).finally(function () { | |
process.exit(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment