query.autoupdatedat.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. var assert = require('assert');
  2. var _ = require('@sailshq/lodash');
  3. var Waterline = require('../../../lib/waterline');
  4. describe('Collection Query ::', function() {
  5. describe('.update()', function() {
  6. describe('with autoUpdatedAt', function() {
  7. var modelDef = {
  8. identity: 'user',
  9. datastore: 'foo',
  10. primaryKey: 'id',
  11. fetchRecordsOnCreate: true,
  12. attributes: {
  13. id: {
  14. type: 'number'
  15. },
  16. stringdate: {
  17. type: 'string',
  18. autoUpdatedAt: true
  19. },
  20. numberdate: {
  21. type: 'number',
  22. autoUpdatedAt: true
  23. },
  24. refdate: {
  25. type: 'ref',
  26. autoUpdatedAt: true
  27. },
  28. }
  29. };
  30. it('should use correct types for autoUpdatedAt fields based on the attribute `type`', function(done) {
  31. var waterline = new Waterline();
  32. waterline.registerModel(Waterline.Model.extend(_.extend({}, modelDef)));
  33. // Fixture Adapter Def
  34. var adapterDef = { update: function(con, query, cb) { query.valuesToSet.id = 1; return cb(null, [query.valuesToSet]); }};
  35. var connections = {
  36. 'foo': {
  37. adapter: 'foobar'
  38. }
  39. };
  40. waterline.initialize({ adapters: { foobar: adapterDef }, datastores: connections }, function(err, orm) {
  41. if (err) {
  42. return done(err);
  43. }
  44. orm.collections.user.update({ id: 1 }, {}, function(err, records) {
  45. assert.equal(typeof records[0].numberdate, 'number');
  46. assert.equal(typeof records[0].stringdate, 'string');
  47. assert.equal(typeof records[0].refdate, 'object');
  48. return done();
  49. }, { fetch: true });
  50. });
  51. });
  52. });
  53. });
  54. });