references.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. var assert = require('assert');
  2. var _ = require('@sailshq/lodash');
  3. var SchemaBuilder = require('../lib/waterline-schema/schema');
  4. var ForeignKeyMapper = require('../lib/waterline-schema/foreignKeys');
  5. var JoinTableMapper = require('../lib/waterline-schema/joinTables');
  6. var ReferenceMapper = require('../lib/waterline-schema/references');
  7. describe('Reference Mapper :: ', function() {
  8. describe('With automatic column name', function() {
  9. var schema;
  10. before(function() {
  11. var fixtures = [
  12. {
  13. identity: 'foo',
  14. primaryKey: 'id',
  15. attributes: {
  16. id: {
  17. type: 'number'
  18. },
  19. name: {
  20. type: 'string'
  21. },
  22. bars: {
  23. collection: 'bar',
  24. via: 'foo'
  25. }
  26. }
  27. },
  28. {
  29. identity: 'bar',
  30. primaryKey: 'id',
  31. attributes: {
  32. id: {
  33. type: 'number'
  34. },
  35. foo: {
  36. model: 'foo',
  37. columnName: 'foo_id'
  38. }
  39. }
  40. }
  41. ];
  42. var collections = _.map(fixtures, function(obj) {
  43. var collection = function() {};
  44. collection.prototype = obj;
  45. return collection;
  46. });
  47. // Build the schema
  48. schema = SchemaBuilder(collections);
  49. ForeignKeyMapper(schema);
  50. JoinTableMapper(schema);
  51. ReferenceMapper(schema);
  52. });
  53. /**
  54. * Test that a reference to bar gets built for the foo table:
  55. *
  56. * attributes: {
  57. * bars: {
  58. * collection: 'foo'
  59. * references: 'bar',
  60. * on: 'bar_id'
  61. * }
  62. * }
  63. */
  64. it('should add a reference to the bar table', function() {
  65. assert(schema.foo.schema.bars);
  66. assert.equal(schema.foo.schema.bars.collection, 'bar');
  67. assert.equal(schema.foo.schema.bars.references, 'bar');
  68. assert.equal(schema.foo.schema.bars.on, 'foo_id');
  69. });
  70. });
  71. });