query.create.ref.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. var assert = require('assert');
  2. var _ = require('@sailshq/lodash');
  3. var Waterline = require('../../../lib/waterline');
  4. describe('Collection Query ::', function() {
  5. describe('.create()', function() {
  6. describe('with ref values', function() {
  7. it('should maintain object references for `ref` type attributes', function(done) {
  8. var modelDef = {
  9. identity: 'user',
  10. datastore: 'foo',
  11. primaryKey: 'id',
  12. fetchRecordsOnCreate: true,
  13. attributes: {
  14. id: {
  15. type: 'number'
  16. },
  17. blob: {
  18. type: 'ref'
  19. }
  20. }
  21. };
  22. var myBlob = new Buffer([1,2,3,4,5]);
  23. var waterline = new Waterline();
  24. waterline.registerModel(Waterline.Model.extend(_.extend({}, modelDef)));
  25. // Fixture Adapter Def
  26. var adapterDef = {
  27. create: function(con, query, cb) {
  28. assert(query.newRecord.blob === myBlob);
  29. return cb(null, query.newRecord);
  30. }
  31. };
  32. var connections = {
  33. 'foo': {
  34. adapter: 'foobar'
  35. }
  36. };
  37. waterline.initialize({ adapters: { foobar: adapterDef }, datastores: connections }, function(err, orm) {
  38. if (err) {
  39. return done(err);
  40. }
  41. orm.collections.user.create({ blob: myBlob, id: 1 }, done);
  42. });
  43. });//it
  44. });
  45. });
  46. });