sequelize-mock-v5 1.2.4 → 1.3.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +51 -51
- package/sequelize-mock-v5-1.3.4.tgz +0 -0
- package/src/model.js +150 -142
- package/test/model-definition.spec.js +73 -0
- package/test/model.spec.js +174 -145
package/package.json
CHANGED
@@ -1,54 +1,54 @@
|
|
1
1
|
{
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
2
|
+
"name": "sequelize-mock-v5",
|
3
|
+
"version": "1.3.4",
|
4
|
+
"description": "A simple mock interface specifically for testing code relying on Sequelize models",
|
5
|
+
"main": "src/index.js",
|
6
|
+
"scripts": {
|
7
|
+
"doc-gen": "node scripts/doc-gen.js",
|
8
|
+
"test": "nyc --reporter=text --reporter=html mocha",
|
9
|
+
"test-report": "nyc report --reporter=html",
|
10
|
+
"coverage": "nyc report --reporter=text-lcov | coveralls",
|
11
|
+
"preversion": "npm test",
|
12
|
+
"version": "node scripts/version.js && git add changelog.md"
|
13
|
+
},
|
14
|
+
"repository": {
|
15
|
+
"type": "git",
|
16
|
+
"url": "git+https://github.com/KozyOps/sequelize-mock.git"
|
17
|
+
},
|
18
|
+
"keywords": [
|
19
|
+
"sequelize",
|
20
|
+
"mocking",
|
21
|
+
"test",
|
22
|
+
"testing"
|
23
|
+
],
|
24
|
+
"author": "Kozy Operations",
|
25
|
+
"license": "MIT",
|
26
|
+
"bugs": {
|
27
|
+
"url": "https://github.com/KozyOps/sequelize-mock/issues"
|
28
|
+
},
|
29
|
+
"homepage": "https://github.com/KozyOps/sequelize-mock#readme",
|
30
|
+
"dependencies": {
|
31
|
+
"inflection": "^1.10.0",
|
32
|
+
"lodash": "^4.17.20"
|
33
|
+
},
|
34
|
+
"devDependencies": {
|
35
|
+
"coveralls": "^3.1.0",
|
36
|
+
"dox": "^0.9.0",
|
37
|
+
"glob": "^7.1.1",
|
38
|
+
"istanbul": "^0.4.5",
|
39
|
+
"mocha": "^8.1.3",
|
40
|
+
"moment": "^2.17.1",
|
41
|
+
"nyc": "^15.1.0",
|
42
|
+
"proxyquire": "^1.7.10",
|
43
|
+
"should": "^11.1.1"
|
44
|
+
},
|
45
|
+
"nyc": {
|
46
|
+
"exclude": [
|
47
|
+
"src/index.js"
|
23
48
|
],
|
24
|
-
"
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
"homepage": "https://github.com/foyer-inc/sequelize-mock#readme",
|
30
|
-
"dependencies": {
|
31
|
-
"inflection": "^1.10.0",
|
32
|
-
"lodash": "^4.17.20"
|
33
|
-
},
|
34
|
-
"devDependencies": {
|
35
|
-
"coveralls": "^3.1.0",
|
36
|
-
"dox": "^0.9.0",
|
37
|
-
"glob": "^7.1.1",
|
38
|
-
"istanbul": "^0.4.5",
|
39
|
-
"mocha": "^8.1.3",
|
40
|
-
"moment": "^2.17.1",
|
41
|
-
"nyc": "^15.1.0",
|
42
|
-
"proxyquire": "^1.7.10",
|
43
|
-
"should": "^11.1.1"
|
44
|
-
},
|
45
|
-
"nyc": {
|
46
|
-
"exclude": [
|
47
|
-
"src/index.js"
|
48
|
-
],
|
49
|
-
"include": [
|
50
|
-
"src/**/*.js"
|
51
|
-
],
|
52
|
-
"all": true
|
53
|
-
}
|
49
|
+
"include": [
|
50
|
+
"src/**/*.js"
|
51
|
+
],
|
52
|
+
"all": true
|
53
|
+
}
|
54
54
|
}
|
Binary file
|
package/src/model.js
CHANGED
@@ -28,8 +28,8 @@ var _ = require('lodash'),
|
|
28
28
|
* @param {Object} [opts.instanceMethods] Map of function names and the functions to be run. These functions will be added to any instances of this Model type
|
29
29
|
* @param {Object} [opts.sequelize] Sequelize instance that this is tied to
|
30
30
|
**/
|
31
|
-
function fakeModel
|
32
|
-
if(typeof name === 'object') {
|
31
|
+
function fakeModel(name, defaults, opts) {
|
32
|
+
if (typeof name === 'object') {
|
33
33
|
defaults = name;
|
34
34
|
name = '';
|
35
35
|
}
|
@@ -70,6 +70,13 @@ function fakeModel (name, defaults, opts) {
|
|
70
70
|
this.name = name;
|
71
71
|
this._defaults = defaults || {};
|
72
72
|
|
73
|
+
/**
|
74
|
+
* Model definition containing the attributes/fields defined for this model
|
75
|
+
*
|
76
|
+
* @member {Object}
|
77
|
+
**/
|
78
|
+
this.modelDefinition = defaults || {};
|
79
|
+
|
73
80
|
this.tableName = this.options.tableName || (this.options.freezeTableName ? name : Utils.pluralize(name));
|
74
81
|
|
75
82
|
/**
|
@@ -77,7 +84,7 @@ function fakeModel (name, defaults, opts) {
|
|
77
84
|
*
|
78
85
|
* @member {Object}
|
79
86
|
**/
|
80
|
-
this.Instance = function() {
|
87
|
+
this.Instance = function () {
|
81
88
|
Instance.apply(this, arguments);
|
82
89
|
};
|
83
90
|
|
@@ -88,7 +95,7 @@ function fakeModel (name, defaults, opts) {
|
|
88
95
|
}
|
89
96
|
});
|
90
97
|
|
91
|
-
if(this.options.instanceMethods) {
|
98
|
+
if (this.options.instanceMethods) {
|
92
99
|
_.each(this.options.instanceMethods, function (fn, name) {
|
93
100
|
self.Instance.prototype[name] = fn;
|
94
101
|
});
|
@@ -101,7 +108,7 @@ function fakeModel (name, defaults, opts) {
|
|
101
108
|
createdDefault: this.options.createdDefault,
|
102
109
|
fallbackFn: !this.options.autoQueryFallback ? this.build.bind(this) : null,
|
103
110
|
};
|
104
|
-
if(this.options.sequelize) {
|
111
|
+
if (this.options.sequelize) {
|
105
112
|
qiOptions.parent = this.options.sequelize.getQueryInterface();
|
106
113
|
}
|
107
114
|
/**
|
@@ -251,15 +258,15 @@ fakeModel.prototype.getTableName = function () {
|
|
251
258
|
* @return {Model} Self
|
252
259
|
**/
|
253
260
|
fakeModel.prototype.unscoped =
|
254
|
-
/**
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
fakeModel.prototype.scope = function () {
|
261
|
-
|
262
|
-
};
|
261
|
+
/**
|
262
|
+
* No-op that returns the current object
|
263
|
+
*
|
264
|
+
* @instance
|
265
|
+
* @return {Model} Self
|
266
|
+
**/
|
267
|
+
fakeModel.prototype.scope = function () {
|
268
|
+
return this;
|
269
|
+
};
|
263
270
|
|
264
271
|
/**
|
265
272
|
* No-op that returns a void.
|
@@ -267,7 +274,7 @@ fakeModel.prototype.scope = function () {
|
|
267
274
|
* @instance
|
268
275
|
* @return {undefined}
|
269
276
|
**/
|
270
|
-
fakeModel.prototype.addScope = function () {};
|
277
|
+
fakeModel.prototype.addScope = function () { };
|
271
278
|
|
272
279
|
/**
|
273
280
|
* Executes a mock query to find all of the instances with any provided options. Without
|
@@ -295,14 +302,14 @@ fakeModel.prototype.addScope = function () {};
|
|
295
302
|
* @param {Object} [options.where] Values that any automatically created Instances should have
|
296
303
|
* @return {Promise<Instance[]>} result returned by the mock query
|
297
304
|
**/
|
298
|
-
fakeModel.prototype.findAll =
|
305
|
+
fakeModel.prototype.findAll = async function (options) {
|
299
306
|
var self = this;
|
300
307
|
|
301
308
|
return await this.$query({
|
302
309
|
query: "findAll",
|
303
310
|
queryOptions: arguments,
|
304
|
-
fallbackFn: !this.options.autoQueryFallback ? async function() { return } : async function () {
|
305
|
-
return [
|
311
|
+
fallbackFn: !this.options.autoQueryFallback ? async function () { return } : async function () {
|
312
|
+
return [self.build(options ? options.where : {})];
|
306
313
|
},
|
307
314
|
});
|
308
315
|
};
|
@@ -338,21 +345,21 @@ fakeModel.prototype.findAll = async function (options) {
|
|
338
345
|
* @return {Promise<Object>} result returned by the mock query
|
339
346
|
**/
|
340
347
|
fakeModel.prototype.findAndCount =
|
341
|
-
fakeModel.prototype.findAndCountAll =
|
342
|
-
|
348
|
+
fakeModel.prototype.findAndCountAll = async function (options) {
|
349
|
+
var self = this;
|
343
350
|
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
};
|
351
|
+
return await this.$query({
|
352
|
+
query: "findAndCountAll",
|
353
|
+
queryOptions: arguments,
|
354
|
+
fallbackFn: !this.options.autoQueryFallback ? async function () { return } : async function () {
|
355
|
+
let result = [self.build(options ? options.where : {})]
|
356
|
+
return {
|
357
|
+
count: result.length,
|
358
|
+
rows: result
|
359
|
+
};
|
360
|
+
},
|
361
|
+
});
|
362
|
+
};
|
356
363
|
|
357
364
|
/**
|
358
365
|
* Executes a mock query to count all of the instances with any provided options. Without any other configuration, the default behavior when no queueud query result
|
@@ -379,14 +386,14 @@ fakeModel.prototype.findAndCountAll = async function (options) {
|
|
379
386
|
* @param {Object} [options.where] Values that any automatically created Instances should have
|
380
387
|
* @return {Promise<Object>} result returned by the mock query
|
381
388
|
**/
|
382
|
-
|
389
|
+
fakeModel.prototype.count = async function (options) {
|
383
390
|
var self = this;
|
384
391
|
|
385
392
|
return await this.$query({
|
386
393
|
query: "count",
|
387
394
|
queryOptions: arguments,
|
388
|
-
fallbackFn: !this.options.autoQueryFallback ? async function() { return }
|
389
|
-
let result =
|
395
|
+
fallbackFn: !this.options.autoQueryFallback ? async function () { return } : async function () {
|
396
|
+
let result = [self.build(options ? options.where : {})]
|
390
397
|
return result.length
|
391
398
|
}
|
392
399
|
});
|
@@ -405,17 +412,17 @@ fakeModel.prototype.findAndCountAll = async function (options) {
|
|
405
412
|
* @return {Promise<Instance>} Promise that resolves with an instance with the given ID
|
406
413
|
**/
|
407
414
|
fakeModel.prototype.findById =
|
408
|
-
fakeModel.prototype.findByPk = async function (id) {
|
409
|
-
|
415
|
+
fakeModel.prototype.findByPk = async function (id) {
|
416
|
+
var self = this;
|
410
417
|
|
411
|
-
|
412
|
-
|
413
|
-
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
|
418
|
-
};
|
418
|
+
return await this.$query({
|
419
|
+
query: "findById",
|
420
|
+
queryOptions: arguments,
|
421
|
+
fallbackFn: !this.options.autoQueryFallback ? async function () { return } : async function () {
|
422
|
+
return self.build({ id: id });
|
423
|
+
},
|
424
|
+
});
|
425
|
+
};
|
419
426
|
|
420
427
|
/**
|
421
428
|
* Executes a mock query to find an instance with the given infomation. Without any other
|
@@ -442,17 +449,17 @@ fakeModel.prototype.findByPk = async function (id) {
|
|
442
449
|
* @return {Promise<Instance>} Promise that resolves with an instance with the given properties
|
443
450
|
**/
|
444
451
|
fakeModel.prototype.find =
|
445
|
-
fakeModel.prototype.findOne = async function (obj) {
|
446
|
-
|
452
|
+
fakeModel.prototype.findOne = async function (obj) {
|
453
|
+
var self = this;
|
447
454
|
|
448
|
-
|
449
|
-
|
450
|
-
|
451
|
-
|
452
|
-
|
453
|
-
|
454
|
-
|
455
|
-
};
|
455
|
+
return await this.$query({
|
456
|
+
query: "findOne",
|
457
|
+
queryOptions: arguments,
|
458
|
+
fallbackFn: !this.options.autoQueryFallback ? async function () { return } : async function () {
|
459
|
+
return self.build(obj ? obj.where : {});
|
460
|
+
},
|
461
|
+
});
|
462
|
+
};
|
456
463
|
|
457
464
|
/**
|
458
465
|
* Executes a mock query to find the max value of a field. Without any other
|
@@ -465,38 +472,38 @@ fakeModel.prototype.findOne = async function (obj) {
|
|
465
472
|
* @return {Any} the default value for the given field
|
466
473
|
**/
|
467
474
|
fakeModel.prototype.max =
|
468
|
-
/**
|
469
|
-
|
470
|
-
|
471
|
-
|
472
|
-
|
473
|
-
|
474
|
-
|
475
|
-
|
476
|
-
|
477
|
-
|
478
|
-
fakeModel.prototype.min =
|
479
|
-
/**
|
480
|
-
|
481
|
-
|
482
|
-
|
483
|
-
|
484
|
-
|
485
|
-
|
486
|
-
|
487
|
-
|
488
|
-
|
489
|
-
fakeModel.prototype.sum = async function (field) {
|
490
|
-
|
475
|
+
/**
|
476
|
+
* Executes a mock query to find the min value of a field. Without any other
|
477
|
+
* configuration, the default behavior when no queueud query result is present
|
478
|
+
* is to return the default value for the given field
|
479
|
+
*
|
480
|
+
* @instance
|
481
|
+
* @method
|
482
|
+
* @param {String} field Name of the field to return for
|
483
|
+
* @return {Any} the default value for the given field
|
484
|
+
**/
|
485
|
+
fakeModel.prototype.min =
|
486
|
+
/**
|
487
|
+
* Executes a mock query to find the sum value of a field. Without any other
|
488
|
+
* configuration, the default behavior when no queueud query result is present
|
489
|
+
* is to return the default value for the given field
|
490
|
+
*
|
491
|
+
* @instance
|
492
|
+
* @method
|
493
|
+
* @param {String} field Name of the field to return for
|
494
|
+
* @return {Any} the default value for the given field
|
495
|
+
**/
|
496
|
+
fakeModel.prototype.sum = async function (field) {
|
497
|
+
var self = this;
|
491
498
|
|
492
|
-
|
493
|
-
|
494
|
-
|
495
|
-
|
496
|
-
|
497
|
-
|
498
|
-
|
499
|
-
};
|
499
|
+
return await this.$query({
|
500
|
+
query: "sum",
|
501
|
+
queryOptions: arguments,
|
502
|
+
fallbackFn: !this.options.autoQueryFallback ? async function () { return } : async function () {
|
503
|
+
return self._defaults[field];
|
504
|
+
},
|
505
|
+
});
|
506
|
+
};
|
500
507
|
|
501
508
|
/**
|
502
509
|
* Builds a new Instance with the given properties
|
@@ -517,7 +524,7 @@ fakeModel.prototype.build = function (values, options) {
|
|
517
524
|
deletedAt: this.options.deletedAt,
|
518
525
|
});
|
519
526
|
|
520
|
-
if(typeof options.isNewRecord != 'boolean') {
|
527
|
+
if (typeof options.isNewRecord != 'boolean') {
|
521
528
|
options.isNewRecord = true;
|
522
529
|
}
|
523
530
|
|
@@ -583,22 +590,22 @@ fakeModel.prototype.findOrCreate = async function (obj) {
|
|
583
590
|
* @return {Promise<Boolean>} Promise that resolves with a boolean meant to indicate if something was inserted
|
584
591
|
**/
|
585
592
|
fakeModel.prototype.insertOrUpdate =
|
586
|
-
fakeModel.prototype.upsert = async function (values) {
|
587
|
-
|
593
|
+
fakeModel.prototype.upsert = async function (values) {
|
594
|
+
var self = this;
|
588
595
|
|
589
|
-
|
590
|
-
|
591
|
-
|
592
|
-
|
593
|
-
|
594
|
-
|
595
|
-
|
596
|
-
|
597
|
-
|
598
|
-
|
599
|
-
|
600
|
-
|
601
|
-
}
|
596
|
+
return await this.$query({
|
597
|
+
query: "upsert",
|
598
|
+
queryOptions: arguments,
|
599
|
+
fallbackFn: !this.options.autoQueryFallback ? async function () { return } : async function () {
|
600
|
+
if (Object.keys().length === 0) {
|
601
|
+
return self.options.createdDefault;
|
602
|
+
}
|
603
|
+
else {
|
604
|
+
return await self.build(values).save();
|
605
|
+
}
|
606
|
+
},
|
607
|
+
});
|
608
|
+
}
|
602
609
|
|
603
610
|
/**
|
604
611
|
* @instance
|
@@ -615,7 +622,7 @@ fakeModel.prototype.upsert = async function (values, returning) {
|
|
615
622
|
query: "upsert",
|
616
623
|
queryOptions: arguments,
|
617
624
|
//if there is nothing queued, return the input object with a created value equal to the createdDefault
|
618
|
-
fallbackFn: !this.options.autoQueryFallback ? async function() { return }
|
625
|
+
fallbackFn: !this.options.autoQueryFallback ? async function () { return } : async function (values) {
|
619
626
|
if (typeof values === 'undefined') {
|
620
627
|
return self.options.createdDefault;
|
621
628
|
}
|
@@ -642,9 +649,10 @@ fakeModel.prototype.bulkCreate = async function (set, options) {
|
|
642
649
|
return await this.$query({
|
643
650
|
query: "bulkCreate",
|
644
651
|
queryOptions: arguments,
|
645
|
-
fallbackFn: !this.options.autoQueryFallback ? async function() { return }
|
646
|
-
return await Promise.all(set.map(async function(val) {
|
647
|
-
|
652
|
+
fallbackFn: !this.options.autoQueryFallback ? async function () { return } : async function () {
|
653
|
+
return await Promise.all(set.map(async function (val) {
|
654
|
+
let res = await self.create(val);
|
655
|
+
return await res.fallbackFn();
|
648
656
|
}));
|
649
657
|
},
|
650
658
|
});
|
@@ -666,14 +674,19 @@ fakeModel.prototype.destroy = async function (options) {
|
|
666
674
|
return await this.$query({
|
667
675
|
query: "destroy",
|
668
676
|
queryOptions: arguments,
|
669
|
-
fallbackFn: !this.options.autoQueryFallback ? async function() { return }
|
677
|
+
fallbackFn: !this.options.autoQueryFallback ? async function () { return } : async function () {
|
670
678
|
return Promise.resolve(options && typeof options.limit == 'number' ? options.limit : 1);
|
671
679
|
},
|
672
680
|
});
|
673
681
|
}
|
674
682
|
else {
|
675
|
-
|
676
|
-
|
683
|
+
return await this.__proto__.Model.$query({
|
684
|
+
query: "destroy",
|
685
|
+
queryOptions: arguments,
|
686
|
+
fallbackFn: !this.options.autoQueryFallback ? async function () { return } : async function () {
|
687
|
+
return Promise.resolve(options && typeof options.limit == 'number' ? options.limit : 1);
|
688
|
+
},
|
689
|
+
});
|
677
690
|
}
|
678
691
|
|
679
692
|
};
|
@@ -694,41 +707,36 @@ fakeModel.prototype.destroy = async function (options) {
|
|
694
707
|
fakeModel.prototype.update = async function (values, options) {
|
695
708
|
var self = this;
|
696
709
|
options = options || {};
|
697
|
-
if (this.$query) {
|
698
|
-
return await this.$query({
|
699
|
-
query: "update",
|
700
|
-
queryOptions: arguments,
|
701
|
-
options: options,
|
702
|
-
//if options.returning are empty or false, we are doing the default (returning both)
|
703
|
-
includeAffectedRows: !!!options.returning,
|
704
|
-
fallbackFn: !this.options.autoQueryFallback ? null : async function() {
|
705
|
-
if(!options.returning) {
|
706
|
-
return [1];
|
707
|
-
}
|
708
|
-
return [ 1, [self.build(values)] ];
|
709
|
-
}
|
710
|
-
});
|
711
|
-
}
|
712
|
-
else {
|
713
|
-
const instanceLevelUpdate = this.__proto__.__proto__.update.bind(this, values);
|
714
|
-
return await instanceLevelUpdate()
|
715
|
-
}
|
716
710
|
|
711
|
+
|
712
|
+
return await this.$query({
|
713
|
+
query: "update",
|
714
|
+
queryOptions: arguments,
|
715
|
+
options: options,
|
716
|
+
//if options.returning are empty or false, we are doing the default (returning both)
|
717
|
+
includeAffectedRows: !!!options.returning,
|
718
|
+
fallbackFn: !this.options.autoQueryFallback ? null : async function () {
|
719
|
+
if (!options.returning) {
|
720
|
+
return [1];
|
721
|
+
}
|
722
|
+
return [1, [self.build(values)]];
|
723
|
+
}
|
724
|
+
});
|
717
725
|
};
|
718
726
|
|
719
727
|
// Noops
|
720
728
|
fakeModel.prototype.addHook =
|
721
|
-
fakeModel.prototype.removeHook = function () {};
|
729
|
+
fakeModel.prototype.removeHook = function () { };
|
722
730
|
|
723
731
|
// Associations
|
724
732
|
fakeModel.prototype.belongsTo = fakeModel.prototype.hasOne = function (item, options) {
|
725
|
-
if(!(item instanceof fakeModel)) {
|
733
|
+
if (!(item instanceof fakeModel)) {
|
726
734
|
return;
|
727
735
|
}
|
728
736
|
|
729
737
|
var isString = typeof item === 'string',
|
730
738
|
name;
|
731
|
-
if(options && options.as) {
|
739
|
+
if (options && options.as) {
|
732
740
|
name = options.as;
|
733
741
|
} else if (isString) {
|
734
742
|
name = item;
|
@@ -736,12 +744,12 @@ fakeModel.prototype.belongsTo = fakeModel.prototype.hasOne = function (item, opt
|
|
736
744
|
name = item.getTableName();
|
737
745
|
}
|
738
746
|
|
739
|
-
var singular = Utils.uppercaseFirst(
|
740
|
-
plural = Utils.uppercaseFirst(
|
747
|
+
var singular = Utils.uppercaseFirst(Utils.singularize(name)),
|
748
|
+
plural = Utils.uppercaseFirst(Utils.pluralize(name)),
|
741
749
|
self = this,
|
742
750
|
noop = function () { return Promise.resolve(self); };
|
743
751
|
|
744
|
-
if(isString) {
|
752
|
+
if (isString) {
|
745
753
|
this.Instance.prototype['get' + singular] = function (opts) { return Promise.resolve(new self.Instance(opts && opts.where ? opts.where : opts)); };
|
746
754
|
} else {
|
747
755
|
this.Instance.prototype['get' + singular] = item.findOne.bind(item);
|
@@ -751,7 +759,7 @@ fakeModel.prototype.belongsTo = fakeModel.prototype.hasOne = function (item, opt
|
|
751
759
|
};
|
752
760
|
|
753
761
|
fakeModel.prototype.belongsToMany = fakeModel.prototype.hasMany = function (item, options) {
|
754
|
-
if(!(item instanceof fakeModel)) {
|
762
|
+
if (!(item instanceof fakeModel)) {
|
755
763
|
return {
|
756
764
|
through: {
|
757
765
|
model: new fakeModel(this.getTableName() + (item && item.name ? item.name : 'Association'), {}, { hasPrimaryKeys: false })
|
@@ -761,10 +769,10 @@ fakeModel.prototype.belongsToMany = fakeModel.prototype.hasMany = function (item
|
|
761
769
|
|
762
770
|
var isString = typeof item === 'string',
|
763
771
|
name, singular, plural;
|
764
|
-
if(options && options.as) {
|
772
|
+
if (options && options.as) {
|
765
773
|
name = options.as;
|
766
|
-
singular = Utils.uppercaseFirst(
|
767
|
-
plural = Utils.uppercaseFirst(
|
774
|
+
singular = Utils.uppercaseFirst(Utils.singularize(name));
|
775
|
+
plural = Utils.uppercaseFirst(name);
|
768
776
|
|
769
777
|
} else {
|
770
778
|
if (isString) {
|
@@ -772,14 +780,14 @@ fakeModel.prototype.belongsToMany = fakeModel.prototype.hasMany = function (item
|
|
772
780
|
} else {
|
773
781
|
name = item.getTableName();
|
774
782
|
}
|
775
|
-
singular = Utils.uppercaseFirst(
|
776
|
-
plural = Utils.uppercaseFirst(
|
783
|
+
singular = Utils.uppercaseFirst(Utils.singularize(name));
|
784
|
+
plural = Utils.uppercaseFirst(Utils.pluralize(name));
|
777
785
|
}
|
778
786
|
|
779
787
|
var self = this,
|
780
788
|
noop = function () { return Promise.resolve(self); };
|
781
789
|
|
782
|
-
if(isString) {
|
790
|
+
if (isString) {
|
783
791
|
this.Instance.prototype['get' + plural] = function (opts) { return Promise.resolve([new self.Instance(opts && opts.where ? opts.where : opts)]); };
|
784
792
|
} else {
|
785
793
|
this.Instance.prototype['get' + plural] = item.findAll.bind(item);
|
@@ -796,7 +804,7 @@ fakeModel.prototype.belongsToMany = fakeModel.prototype.hasMany = function (item
|
|
796
804
|
|
797
805
|
return {
|
798
806
|
through: {
|
799
|
-
model: new fakeModel(
|
807
|
+
model: new fakeModel(this.getTableName() + plural, {}, { hasPrimaryKeys: false })
|
800
808
|
}
|
801
809
|
};
|
802
810
|
};
|
@@ -0,0 +1,73 @@
|
|
1
|
+
'use strict';
|
2
|
+
|
3
|
+
var should = require('should');
|
4
|
+
var Sequelize = require('../src/index.js');
|
5
|
+
|
6
|
+
describe('Model Definition Integration', function () {
|
7
|
+
|
8
|
+
describe('modelDefinition property', function () {
|
9
|
+
it('should be available on models defined with sequelize.define()', function () {
|
10
|
+
var sequelize = new Sequelize();
|
11
|
+
var modelDef = {
|
12
|
+
'name': 'STRING',
|
13
|
+
'email': 'STRING',
|
14
|
+
'age': 'INTEGER',
|
15
|
+
};
|
16
|
+
|
17
|
+
var User = sequelize.define('User', modelDef);
|
18
|
+
|
19
|
+
User.should.have.property('modelDefinition').which.is.an.Object();
|
20
|
+
User.modelDefinition.should.equal(modelDef);
|
21
|
+
User.modelDefinition.should.have.property('name').which.is.exactly('STRING');
|
22
|
+
User.modelDefinition.should.have.property('email').which.is.exactly('STRING');
|
23
|
+
User.modelDefinition.should.have.property('age').which.is.exactly('INTEGER');
|
24
|
+
});
|
25
|
+
|
26
|
+
it('should be accessible through model instances', function () {
|
27
|
+
var sequelize = new Sequelize();
|
28
|
+
var modelDef = {
|
29
|
+
'title': 'STRING',
|
30
|
+
'content': 'TEXT',
|
31
|
+
'published': 'BOOLEAN',
|
32
|
+
};
|
33
|
+
|
34
|
+
var Post = sequelize.define('Post', modelDef);
|
35
|
+
var post = Post.build();
|
36
|
+
|
37
|
+
post.Model.should.have.property('modelDefinition').which.is.an.Object();
|
38
|
+
post.Model.modelDefinition.should.equal(modelDef);
|
39
|
+
post.Model.modelDefinition.should.have.property('title').which.is.exactly('STRING');
|
40
|
+
post.Model.modelDefinition.should.have.property('content').which.is.exactly('TEXT');
|
41
|
+
post.Model.modelDefinition.should.have.property('published').which.is.exactly('BOOLEAN');
|
42
|
+
});
|
43
|
+
|
44
|
+
it('should work with Sequelize DataTypes', function () {
|
45
|
+
var sequelize = new Sequelize();
|
46
|
+
var modelDef = {
|
47
|
+
'name': sequelize.STRING,
|
48
|
+
'age': sequelize.INTEGER,
|
49
|
+
'active': sequelize.BOOLEAN,
|
50
|
+
};
|
51
|
+
|
52
|
+
var User = sequelize.define('User', modelDef);
|
53
|
+
|
54
|
+
User.should.have.property('modelDefinition').which.is.an.Object();
|
55
|
+
User.modelDefinition.should.equal(modelDef);
|
56
|
+
User.modelDefinition.should.have.property('name').which.is.exactly(sequelize.STRING);
|
57
|
+
User.modelDefinition.should.have.property('age').which.is.exactly(sequelize.INTEGER);
|
58
|
+
User.modelDefinition.should.have.property('active').which.is.exactly(sequelize.BOOLEAN);
|
59
|
+
});
|
60
|
+
|
61
|
+
it('should handle empty model definitions', function () {
|
62
|
+
var sequelize = new Sequelize();
|
63
|
+
var modelDef = {};
|
64
|
+
|
65
|
+
var EmptyModel = sequelize.define('EmptyModel', modelDef);
|
66
|
+
|
67
|
+
EmptyModel.should.have.property('modelDefinition').which.is.an.Object();
|
68
|
+
EmptyModel.modelDefinition.should.equal(modelDef);
|
69
|
+
Object.keys(EmptyModel.modelDefinition).should.have.length(0);
|
70
|
+
});
|
71
|
+
});
|
72
|
+
|
73
|
+
});
|