metaData.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. var assert = require('assert');
  2. var _ = require('@sailshq/lodash');
  3. var SchemaBuilder = require('../lib/waterline-schema');
  4. describe('Meta Extended Data :: ', function() {
  5. describe('On Models', function() {
  6. var schema;
  7. before(function() {
  8. var fixtures = [
  9. {
  10. identity: 'user',
  11. primaryKey: 'id',
  12. meta: {
  13. schemaName: 'foo'
  14. },
  15. attributes: {
  16. id: {
  17. type: 'number'
  18. },
  19. name: {
  20. type: 'string'
  21. }
  22. }
  23. },
  24. {
  25. identity: 'car',
  26. primaryKey: 'id',
  27. attributes: {
  28. id: {
  29. type: 'number'
  30. },
  31. age: {
  32. type: 'number'
  33. }
  34. }
  35. }
  36. ];
  37. var collections = _.map(fixtures, function(obj) {
  38. var collection = function() {};
  39. collection.prototype = obj;
  40. return collection;
  41. });
  42. // Build the schema
  43. schema = SchemaBuilder(collections);
  44. });
  45. it('should keep the meta key on the user collection', function() {
  46. assert(schema.user.meta);
  47. assert.equal(schema.user.meta.schemaName, 'foo');
  48. });
  49. it('should add an empty meta object to the car collection', function() {
  50. assert(schema.car.meta);
  51. assert.equal(_.keys(schema.car.meta).length, 0);
  52. });
  53. });
  54. describe('On generated join tables', function() {
  55. var schema;
  56. before(function() {
  57. var fixtures = [
  58. {
  59. identity: 'user',
  60. primaryKey: 'id',
  61. attributes: {
  62. id: {
  63. type: 'number'
  64. },
  65. cars: {
  66. collection: 'car',
  67. via: 'drivers'
  68. }
  69. }
  70. },
  71. {
  72. identity: 'car',
  73. primaryKey: 'id',
  74. meta: {
  75. schemaName: 'foo'
  76. },
  77. attributes: {
  78. id: {
  79. type: 'number'
  80. },
  81. drivers: {
  82. collection: 'user',
  83. via: 'cars',
  84. dominant: true
  85. }
  86. }
  87. }
  88. ];
  89. var collections = _.map(fixtures, function(obj) {
  90. var collection = function() {};
  91. collection.prototype = obj;
  92. return collection;
  93. });
  94. // Build the schema
  95. schema = SchemaBuilder(collections);
  96. });
  97. it('should add the meta data to the join table', function() {
  98. assert(schema.car_drivers__user_cars);
  99. assert(schema.car_drivers__user_cars.meta);
  100. assert.equal(schema.car_drivers__user_cars.meta.schemaName, 'foo');
  101. });
  102. });
  103. });