afterCreate.create.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. var assert = require('assert');
  2. var Waterline = require('../../../lib/waterline');
  3. describe('After Create Lifecycle Callback ::', function() {
  4. describe('Create ::', function() {
  5. var person;
  6. before(function(done) {
  7. var waterline = new Waterline();
  8. var Model = Waterline.Model.extend({
  9. identity: 'user',
  10. datastore: 'foo',
  11. primaryKey: 'id',
  12. fetchRecordsOnCreate: true,
  13. attributes: {
  14. id: {
  15. type: 'number'
  16. },
  17. name: {
  18. type: 'string'
  19. }
  20. },
  21. afterCreate: function(values, cb) {
  22. values.name = values.name + ' updated';
  23. return cb();
  24. }
  25. });
  26. waterline.registerModel(Model);
  27. // Fixture Adapter Def
  28. var adapterDef = { create: function(con, query, cb) { return cb(null, query.newRecord); }};
  29. var connections = {
  30. 'foo': {
  31. adapter: 'foobar'
  32. }
  33. };
  34. waterline.initialize({ adapters: { foobar: adapterDef }, datastores: connections }, function(err, orm) {
  35. if (err) {
  36. return done(err);
  37. }
  38. person = orm.collections.user;
  39. return done();
  40. });
  41. });
  42. it('should run afterCreate and mutate values', function(done) {
  43. person.create({ name: 'test', id: 1 }, function(err, user) {
  44. if (err) {
  45. return done(err);
  46. }
  47. assert.equal(user.name, 'test updated');
  48. return done();
  49. });
  50. });
  51. });
  52. });