不要怂,就是干,撸起袖子干!

Commit 3ff27d10 by Felix Becker Committed by Jan Aagaard Meier

ES6 refactor: associations (#6050)

* Make BelongsTo association an ES6 class

* ES6 refactor of belongs-to.js

* Make BelongsToMany an ES6 class

* ES6 refactor of BelongsToMany

* Make HasMany an ES6 class

* ES6 refactor of HasMany

* Make HasOne an ES6 class

* ES6 refactor of HasOne

* ES6 refactor of association helpers

* ES6 refactor of associations/index.js

* ES6 refactor of association mixin
1 parent 5d7b26c2
'use strict'; 'use strict';
var Utils = require('./../utils'); const Utils = require('./../utils');
function checkNamingCollision (association) { function checkNamingCollision (association) {
if (association.source.rawAttributes.hasOwnProperty(association.as)) { if (association.source.rawAttributes.hasOwnProperty(association.as)) {
...@@ -11,6 +11,7 @@ function checkNamingCollision (association) { ...@@ -11,6 +11,7 @@ function checkNamingCollision (association) {
); );
} }
} }
exports.checkNamingCollision = checkNamingCollision;
function addForeignKeyConstraints (newAttribute, source, target, options, key) { function addForeignKeyConstraints (newAttribute, source, target, options, key) {
// FK constraints are opt-in: users must either set `foreignKeyConstraints` // FK constraints are opt-in: users must either set `foreignKeyConstraints`
...@@ -19,9 +20,9 @@ function addForeignKeyConstraints (newAttribute, source, target, options, key) { ...@@ -19,9 +20,9 @@ function addForeignKeyConstraints (newAttribute, source, target, options, key) {
if (options.foreignKeyConstraint || options.onDelete || options.onUpdate) { if (options.foreignKeyConstraint || options.onDelete || options.onUpdate) {
// Find primary keys: composite keys not supported with this approach // Find primary keys: composite keys not supported with this approach
var primaryKeys = Utils._.chain(source.rawAttributes).keys() const primaryKeys = Utils._.chain(source.rawAttributes).keys()
.filter(function($key) { return source.rawAttributes[$key].primaryKey; }) .filter($key => source.rawAttributes[$key].primaryKey)
.map(function($key) { return source.rawAttributes[$key].field || $key; }).value(); .map($key => source.rawAttributes[$key].field || $key).value();
if (primaryKeys.length === 1) { if (primaryKeys.length === 1) {
if (!!source.$schema) { if (!!source.$schema) {
...@@ -42,8 +43,4 @@ function addForeignKeyConstraints (newAttribute, source, target, options, key) { ...@@ -42,8 +43,4 @@ function addForeignKeyConstraints (newAttribute, source, target, options, key) {
} }
} }
} }
exports.addForeignKeyConstraints = addForeignKeyConstraints;
module.exports = {
checkNamingCollision: checkNamingCollision,
addForeignKeyConstraints: addForeignKeyConstraints
};
'use strict'; 'use strict';
var Association = require('./base'); const Association = require('./base');
Association.BelongsTo = require('./belongs-to'); Association.BelongsTo = require('./belongs-to');
Association.HasOne = require('./has-one'); Association.HasOne = require('./has-one');
Association.HasMany = require('./has-many'); Association.HasMany = require('./has-many');
Association.BelongsToMany = require('./belongs-to-many'); Association.BelongsToMany = require('./belongs-to-many');
module.exports = Association; module.exports = Association;
module.exports.default = Association;
module.exports.Association = Association;
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!