Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions lib/associations/base.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
var Association = function () {};
'use strict';

module.exports = Association;
var Association = function() {};

// Normalize input - may be array or single obj, instance or primary key - convert it to an array of built objects
Association.prototype.toInstanceArray = function (objs) {
if (!Array.isArray(objs)) {
objs = [objs];
}
return objs.map(function(obj) {
if (!(obj instanceof this.target.Instance)) {
var tmpInstance = {};
tmpInstance[this.target.primaryKeyAttribute] = obj;
return this.target.build(tmpInstance, {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really considering introducing babel-node so we could write

return this.target.build({
  [this.target.primaryKeyAttribute]: obj
}, {
 ...
});

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+10!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not really sure if this is such a good idea. Many developers won't have a clue what that stands for and you prevent other's from working on sequelize. Every JS developer should know the old syntax but only a few might know about the newest features. So is it really worth it?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say there's likely a good overlap between people contributing to oss node.js projects and people interested in ES6 syntax.

And if it improves the productivity of those who contribute most, i'd say it's likely worth it.
thoughts @overlookmotel @mbroadst?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This stuff is starting to become commonplace among newer JS projects, and hopefully even more so with newer Node & IO.js versions.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In any case it's low on the priority list @BridgeAR :)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have experience with node-babel in particular, but ES6 is definitely coming and I'm using ES6/7 explicitly in my frontend projects. I'd +1 this 😄

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've opened an issue for this: #3951

isNewRecord: false
});
}
return obj;
}, this);
};
module.exports = Association;
357 changes: 125 additions & 232 deletions lib/associations/belongs-to-many.js

Large diffs are not rendered by default.

15 changes: 8 additions & 7 deletions lib/associations/belongs-to.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var Utils = require('./../utils')
, Helpers = require('./helpers')
, _ = require('lodash')
, Transaction = require('../transaction')
, Association = require('./base')
, util = require('util');
Expand All @@ -18,7 +19,7 @@ var BelongsTo = function(source, target, options) {
this.isSelfAssociation = (this.source === this.target);
this.as = this.options.as;

if (Utils._.isObject(this.options.foreignKey)) {
if (_.isObject(this.options.foreignKey)) {
this.foreignKeyAttribute = this.options.foreignKey;
this.foreignKey = this.foreignKeyAttribute.name || this.foreignKeyAttribute.fieldName;
} else {
Expand All @@ -37,18 +38,18 @@ var BelongsTo = function(source, target, options) {
}

if (!this.options.foreignKey) {
this.options.foreignKey = Utils._.camelizeIf(
this.options.foreignKey = _.camelizeIf(
[
Utils._.underscoredIf(this.as, this.source.options.underscored),
_.underscoredIf(this.as, this.source.options.underscored),
this.target.primaryKeyAttribute
].join('_'),
!this.source.options.underscored
);
}

this.identifier = this.foreignKey || Utils._.camelizeIf(
this.identifier = this.foreignKey || _.camelizeIf(
[
Utils._.underscoredIf(this.options.name.singular, this.target.options.underscored),
_.underscoredIf(this.options.name.singular, this.target.options.underscored),
this.target.primaryKeyAttribute
].join('_'),
!this.target.options.underscored
Expand All @@ -74,7 +75,7 @@ util.inherits(BelongsTo, Association);
BelongsTo.prototype.injectAttributes = function() {
var newAttributes = {};

newAttributes[this.identifier] = Utils._.defaults(this.foreignKeyAttribute, { type: this.options.keyType || this.target.rawAttributes[this.targetIdentifier].type });
newAttributes[this.identifier] = _.defaults(this.foreignKeyAttribute, { type: this.options.keyType || this.target.rawAttributes[this.targetIdentifier].type });
if (this.options.constraints !== false) {
this.options.onDelete = this.options.onDelete || 'SET NULL';
this.options.onUpdate = this.options.onUpdate || 'CASCADE';
Expand Down Expand Up @@ -141,7 +142,7 @@ BelongsTo.prototype.injectSetter = function(instancePrototype) {

if (options.save === false) return;

options = Utils._.extend({
options = _.extend({
fields: [association.identifier],
allowNull: [association.identifier],
association: true
Expand Down
135 changes: 0 additions & 135 deletions lib/associations/has-many-single-linked.js

This file was deleted.

Loading