strategy.alter.schemaless.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. var assert = require('assert');
  2. var _ = require('@sailshq/lodash');
  3. var Waterline = require('../../lib/waterline');
  4. var MigrateHelper = require('../support/migrate.helper');
  5. describe.skip('Alter Mode Recovery with schemaless data', function () {
  6. var record;
  7. // Create an adapter and set some existing data
  8. before(function (done) {
  9. // Hold data to be used in tests
  10. var persistentData = [{
  11. id: 1,
  12. name: 'batman',
  13. car: 'batmobile',
  14. age: 50
  15. }];
  16. var adapter = {
  17. registerDatastore: function (connection, collections, cb) {
  18. cb(null, null);
  19. },
  20. define: function (connectionName, collectionName, definition, cb) {
  21. this.describe(connectionName, collectionName, function (err, schema) {
  22. cb(null, schema);
  23. });
  24. },
  25. describe: function (connectionName, collectionName, cb, connection) {
  26. var schema = {
  27. name: { type: 'string' },
  28. age: { type: 'number' }
  29. };
  30. cb(null, (persistentData.length === 1) ? schema : undefined);
  31. },
  32. find: function (connectionName, collectionName, options, cb, connection) {
  33. if(!options.select && !options.where) {
  34. return cb(null, persistentData);
  35. }
  36. var results;
  37. if(!options.where) {
  38. results = persistentData;
  39. }
  40. else {
  41. results = _.filter(persistentData, options.where);
  42. }
  43. // Psuedo support for select (needed to act like a real adapter)
  44. if(options.select) {
  45. // Force ID in query
  46. options.select.push('id');
  47. results = _.map(results, function(result) {
  48. return _.pick(result, options.select);
  49. });
  50. }
  51. cb(null, results);
  52. },
  53. create: function (connectionName, collectionName, data, cb, connection) {
  54. persistentData.push(data);
  55. cb(null, data);
  56. },
  57. drop: function (connectionName, collectionName, relations, cb, connection) {
  58. persistentData = [];
  59. cb(null);
  60. }
  61. };
  62. var waterline = new Waterline();
  63. // Build up a model to test
  64. var PersonModel = {
  65. identity: 'Person',
  66. tableName: 'person_table',
  67. datastore: 'test_alter',
  68. migrate: 'alter',
  69. adapter: 'fake',
  70. schema: false,
  71. attributes: {
  72. name: 'string',
  73. age: 'integer',
  74. id: 'integer'
  75. }
  76. };
  77. // Create a connection using the stub adapter we created above
  78. var connections = {
  79. 'test_alter': {
  80. adapter: 'fake'
  81. }
  82. };
  83. // Build up the named adapters object
  84. var adapters = {fake: adapter};
  85. // Build the collections and find the record
  86. var PersonCollection = Waterline.Model.extend(PersonModel);
  87. waterline.registerModel(PersonCollection);
  88. waterline.initialize({adapters: adapters, datastores: connections}, function (err, data) {
  89. if (err) return done(err);
  90. MigrateHelper(data, function(err) {
  91. data.collections.person.findOne({id: 1}, function (err, found) {
  92. if (err) return done(err);
  93. record = found;
  94. done();
  95. });
  96. });
  97. });
  98. });
  99. it('should keep data already in the data store', function() {
  100. assert(record);
  101. });
  102. it('should include the attributes in the schema', function() {
  103. assert.equal(record.id, 1);
  104. assert.equal(record.name, 'batman');
  105. assert.equal(record.age, 50);
  106. });
  107. it.skip('should include the attributes NOT in the schema', function() {
  108. assert.equal(record.car, 'batmobile');
  109. });
  110. });