sequelize-mock-v5 1.2.4 → 1.3.5
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.5.tgz +0 -0
- package/src/model.js +147 -140
- package/test/model-definition.spec.js +73 -0
- package/test/model.spec.js +145 -145
- package/test/queryinterface.spec.js +171 -172
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.5",
|
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/foyer-inc/sequelize-mock.git"
|
17
|
+
},
|
18
|
+
"keywords": [
|
19
|
+
"sequelize",
|
20
|
+
"mocking",
|
21
|
+
"test",
|
22
|
+
"testing"
|
23
|
+
],
|
24
|
+
"author": "Foyer Inc",
|
25
|
+
"license": "MIT",
|
26
|
+
"bugs": {
|
27
|
+
"url": "https://github.com/foyer-inc/sequelize-mock/issues"
|
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"
|
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,8 +649,8 @@ 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) {
|
652
|
+
fallbackFn: !this.options.autoQueryFallback ? async function () { return } : async function () {
|
653
|
+
return await Promise.all(set.map(async function (val) {
|
647
654
|
return await self.build(val).save();
|
648
655
|
}));
|
649
656
|
},
|
@@ -666,14 +673,14 @@ fakeModel.prototype.destroy = async function (options) {
|
|
666
673
|
return await this.$query({
|
667
674
|
query: "destroy",
|
668
675
|
queryOptions: arguments,
|
669
|
-
fallbackFn: !this.options.autoQueryFallback ? async function() { return }
|
676
|
+
fallbackFn: !this.options.autoQueryFallback ? async function () { return } : async function () {
|
670
677
|
return Promise.resolve(options && typeof options.limit == 'number' ? options.limit : 1);
|
671
678
|
},
|
672
679
|
});
|
673
680
|
}
|
674
681
|
else {
|
675
|
-
|
676
|
-
|
682
|
+
const instanceLevelDestroy = this.__proto__.__proto__.destroy.bind(this);
|
683
|
+
return await instanceLevelDestroy()
|
677
684
|
}
|
678
685
|
|
679
686
|
};
|
@@ -695,40 +702,40 @@ fakeModel.prototype.update = async function (values, options) {
|
|
695
702
|
var self = this;
|
696
703
|
options = options || {};
|
697
704
|
if (this.$query) {
|
698
|
-
|
699
|
-
|
700
|
-
|
701
|
-
|
702
|
-
|
703
|
-
|
704
|
-
|
705
|
-
|
706
|
-
|
707
|
-
|
708
|
-
|
709
|
-
|
710
|
-
|
711
|
-
|
712
|
-
|
713
|
-
|
714
|
-
|
715
|
-
|
705
|
+
return await this.$query({
|
706
|
+
query: "update",
|
707
|
+
queryOptions: arguments,
|
708
|
+
options: options,
|
709
|
+
//if options.returning are empty or false, we are doing the default (returning both)
|
710
|
+
includeAffectedRows: !!!options.returning,
|
711
|
+
fallbackFn: !this.options.autoQueryFallback ? null : async function () {
|
712
|
+
if (!options.returning) {
|
713
|
+
return [1];
|
714
|
+
}
|
715
|
+
return [1, [self.build(values)]];
|
716
|
+
}
|
717
|
+
});
|
718
|
+
}
|
719
|
+
else {
|
720
|
+
const instanceLevelUpdate = this.__proto__.__proto__.update.bind(this, values);
|
721
|
+
return await instanceLevelUpdate()
|
722
|
+
}
|
716
723
|
|
717
724
|
};
|
718
725
|
|
719
726
|
// Noops
|
720
727
|
fakeModel.prototype.addHook =
|
721
|
-
fakeModel.prototype.removeHook = function () {};
|
728
|
+
fakeModel.prototype.removeHook = function () { };
|
722
729
|
|
723
730
|
// Associations
|
724
731
|
fakeModel.prototype.belongsTo = fakeModel.prototype.hasOne = function (item, options) {
|
725
|
-
if(!(item instanceof fakeModel)) {
|
732
|
+
if (!(item instanceof fakeModel)) {
|
726
733
|
return;
|
727
734
|
}
|
728
735
|
|
729
736
|
var isString = typeof item === 'string',
|
730
737
|
name;
|
731
|
-
if(options && options.as) {
|
738
|
+
if (options && options.as) {
|
732
739
|
name = options.as;
|
733
740
|
} else if (isString) {
|
734
741
|
name = item;
|
@@ -736,12 +743,12 @@ fakeModel.prototype.belongsTo = fakeModel.prototype.hasOne = function (item, opt
|
|
736
743
|
name = item.getTableName();
|
737
744
|
}
|
738
745
|
|
739
|
-
var singular = Utils.uppercaseFirst(
|
740
|
-
plural = Utils.uppercaseFirst(
|
746
|
+
var singular = Utils.uppercaseFirst(Utils.singularize(name)),
|
747
|
+
plural = Utils.uppercaseFirst(Utils.pluralize(name)),
|
741
748
|
self = this,
|
742
749
|
noop = function () { return Promise.resolve(self); };
|
743
750
|
|
744
|
-
if(isString) {
|
751
|
+
if (isString) {
|
745
752
|
this.Instance.prototype['get' + singular] = function (opts) { return Promise.resolve(new self.Instance(opts && opts.where ? opts.where : opts)); };
|
746
753
|
} else {
|
747
754
|
this.Instance.prototype['get' + singular] = item.findOne.bind(item);
|
@@ -751,7 +758,7 @@ fakeModel.prototype.belongsTo = fakeModel.prototype.hasOne = function (item, opt
|
|
751
758
|
};
|
752
759
|
|
753
760
|
fakeModel.prototype.belongsToMany = fakeModel.prototype.hasMany = function (item, options) {
|
754
|
-
if(!(item instanceof fakeModel)) {
|
761
|
+
if (!(item instanceof fakeModel)) {
|
755
762
|
return {
|
756
763
|
through: {
|
757
764
|
model: new fakeModel(this.getTableName() + (item && item.name ? item.name : 'Association'), {}, { hasPrimaryKeys: false })
|
@@ -761,10 +768,10 @@ fakeModel.prototype.belongsToMany = fakeModel.prototype.hasMany = function (item
|
|
761
768
|
|
762
769
|
var isString = typeof item === 'string',
|
763
770
|
name, singular, plural;
|
764
|
-
if(options && options.as) {
|
771
|
+
if (options && options.as) {
|
765
772
|
name = options.as;
|
766
|
-
singular = Utils.uppercaseFirst(
|
767
|
-
plural = Utils.uppercaseFirst(
|
773
|
+
singular = Utils.uppercaseFirst(Utils.singularize(name));
|
774
|
+
plural = Utils.uppercaseFirst(name);
|
768
775
|
|
769
776
|
} else {
|
770
777
|
if (isString) {
|
@@ -772,14 +779,14 @@ fakeModel.prototype.belongsToMany = fakeModel.prototype.hasMany = function (item
|
|
772
779
|
} else {
|
773
780
|
name = item.getTableName();
|
774
781
|
}
|
775
|
-
singular = Utils.uppercaseFirst(
|
776
|
-
plural = Utils.uppercaseFirst(
|
782
|
+
singular = Utils.uppercaseFirst(Utils.singularize(name));
|
783
|
+
plural = Utils.uppercaseFirst(Utils.pluralize(name));
|
777
784
|
}
|
778
785
|
|
779
786
|
var self = this,
|
780
787
|
noop = function () { return Promise.resolve(self); };
|
781
788
|
|
782
|
-
if(isString) {
|
789
|
+
if (isString) {
|
783
790
|
this.Instance.prototype['get' + plural] = function (opts) { return Promise.resolve([new self.Instance(opts && opts.where ? opts.where : opts)]); };
|
784
791
|
} else {
|
785
792
|
this.Instance.prototype['get' + plural] = item.findAll.bind(item);
|
@@ -796,7 +803,7 @@ fakeModel.prototype.belongsToMany = fakeModel.prototype.hasMany = function (item
|
|
796
803
|
|
797
804
|
return {
|
798
805
|
through: {
|
799
|
-
model: new fakeModel(
|
806
|
+
model: new fakeModel(this.getTableName() + plural, {}, { hasPrimaryKeys: false })
|
800
807
|
}
|
801
808
|
};
|
802
809
|
};
|
@@ -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
|
+
});
|