sequelize
Advanced tools
Comparing version 1.0.0 to 1.0.1
@@ -85,2 +85,3 @@ # v0.1.0 # | ||
# v1.0.1 # | ||
- TODO: Add Model.count(callback), which returns the number of elements saved in the database | ||
- Added Model.count(callback), which returns the number of elements saved in the database | ||
- Fixed self associations |
{ | ||
"name": "sequelizejs.com", | ||
"description": "The official sequelize webite.", | ||
"version": "0.4.3", | ||
"version": "1.0.0", | ||
"homepage": "http://sequelizejs.com", | ||
@@ -22,4 +22,6 @@ "repository": { | ||
"less": ">=1.0.41", | ||
"connect": ">=1.4.0", | ||
"express": ">=2.3.2", | ||
"express-view-helpers": ">=0.0.2" | ||
} | ||
} |
@@ -8,2 +8,10 @@ var Utils = require("./../utils") | ||
this.options = options | ||
this.isSelfAssociation = (this.source.tableName == this.target.tableName) | ||
if(this.isSelfAssociation && !this.options.foreignKey && !!this.options.as) | ||
this.options.foreignKey = Utils._.underscoredIf(Utils.singularize(this.options.as) + "Id", this.source.options.underscored) | ||
this.associationAccessor = this.isSelfAssociation | ||
? Utils.combineTableNames(this.target.tableName, this.options.as || this.target.tableName) | ||
: this.target.tableName | ||
} | ||
@@ -10,0 +18,0 @@ |
@@ -39,3 +39,3 @@ var Utils = require('./../utils') | ||
var chainer = new Utils.QueryChainer | ||
var foreignIdentifier = self.definition.target.associations[self.definition.source.tableName].identifier | ||
var foreignIdentifier = self.definition.target.associations[self.definition.associationAccessor].identifier | ||
var obsoleteAssociations = oldAssociations.filter(function(obj) { return !obj.equalsOneOf(newAssociations) }) | ||
@@ -76,6 +76,7 @@ | ||
.on('success', function() { | ||
var chainer = new Utils.QueryChainer | ||
var foreignIdentifier = self.definition.target.associations[self.definition.source.tableName].identifier | ||
var unassociatedObjects = newAssociations.filter(function(obj) { return !obj.equalsOneOf(oldAssociations) }) | ||
var chainer = new Utils.QueryChainer | ||
, association = self.definition.target.associations[self.definition.associationAccessor] | ||
, foreignIdentifier = association.isSelfAssociation ? association.foreignIdentifier : association.identifier | ||
, unassociatedObjects = newAssociations.filter(function(obj) { return !obj.equalsOneOf(oldAssociations) }) | ||
unassociatedObjects.forEach(function(unassociatedObject) { | ||
@@ -85,11 +86,11 @@ var attributes = {} | ||
attributes[foreignIdentifier] = unassociatedObject.id | ||
chainer.add(self.definition.connectorModel.create(attributes)) | ||
}) | ||
chainer | ||
.run() | ||
.on('success', function() { emitter.emit('success', null) }) | ||
.on('success', function() { emitter.emit('success', newAssociations) }) | ||
.on('failure', function(err) { emitter.emit('failure', err) }) | ||
}) | ||
} |
@@ -31,5 +31,5 @@ var Utils = require('./../utils') | ||
chainer | ||
.run() | ||
.on('success', function() { emitter.emit('success', null) }) | ||
.on('failure', function() { emitter.emit('failure', null) }) | ||
.run() | ||
.on('success', function() { emitter.emit('success', newAssociations) }) | ||
.on('failure', function(err) { emitter.emit('failure', err) }) | ||
} |
@@ -11,2 +11,8 @@ var Utils = require("./../utils") | ||
this.options = options | ||
this.isSelfAssociation = (this.source.tableName == this.target.tableName) | ||
this.associationAccessor = this.combinedName = Utils.combineTableNames( | ||
this.source.tableName, | ||
this.isSelfAssociation ? (this.options.as || this.target.tableName) : this.target.tableName | ||
) | ||
@@ -26,8 +32,15 @@ var as = (this.options.as || Utils.pluralize(this.target.tableName)) | ||
HasMany.prototype.injectAttributes = function() { | ||
var multiAssociation = this.target.associations.hasOwnProperty(this.associationAccessor) | ||
this.identifier = this.options.foreignKey || Utils._.underscoredIf(Utils.singularize(this.source.tableName) + "Id", this.options.underscored) | ||
if (this.target.associations.hasOwnProperty(this.source.tableName)) { | ||
// is there already a single sided association between the source and the target? | ||
// or is the association on the model itself? | ||
if (this.isSelfAssociation || multiAssociation) { | ||
// remove the obsolete association identifier from the source | ||
this.foreignIdentifier = this.target.associations[this.source.tableName].identifier | ||
delete this.source.attributes[this.foreignIdentifier] | ||
if(this.isSelfAssociation) { | ||
this.foreignIdentifier = (this.options.as || this.target.tableName) + 'Id' | ||
} else { | ||
this.foreignIdentifier = this.target.associations[this.associationAccessor].identifier | ||
delete this.source.attributes[this.foreignIdentifier] | ||
} | ||
@@ -38,7 +51,6 @@ // define a new model, which connects the models | ||
combinedTableAttributes[this.foreignIdentifier] = {type:DataTypes.INTEGER, primaryKey: true} | ||
this.connectorModel = this.source.modelManager.sequelize.define(this.combinedName, combinedTableAttributes) | ||
if(!this.isSelfAssociation) this.target.associations[this.associationAccessor].connectorModel = this.connectorModel | ||
this.connectorModel = | ||
this.target.associations[this.source.tableName].connectorModel = | ||
this.source.modelManager.sequelize.define(Utils.combineTableNames(this.source.tableName, this.target.tableName), combinedTableAttributes) | ||
this.connectorModel.sync() | ||
@@ -56,3 +68,3 @@ } else { | ||
var self = this | ||
obj[this.accessors.get] = function() { | ||
@@ -92,4 +104,4 @@ var Class = self.connectorModel ? HasManyMultiLinked : HasManySingleLinked | ||
instance[self.accessors.set](currentAssociatedObjects) | ||
.on('success', function() { customEventEmitter.emit('success', null) }) | ||
.on('failure', function() { customEventEmitter.emit('failure', null) }) | ||
.on('success', function(instances) { customEventEmitter.emit('success', instances) }) | ||
.on('failure', function(err) { customEventEmitter.emit('failure', err) }) | ||
}) | ||
@@ -96,0 +108,0 @@ }) |
@@ -8,2 +8,11 @@ var Utils = require("./../utils") | ||
this.options = options | ||
this.isSelfAssociation = (this.source.tableName == this.target.tableName) | ||
if(this.isSelfAssociation && !this.options.foreignKey && !!this.options.as) | ||
this.options.foreignKey = Utils._.underscoredIf(Utils.singularize(this.options.as) + "Id", this.options.underscored) | ||
this.associationAccessor = this.isSelfAssociation | ||
? Utils.combineTableNames(this.target.tableName, this.options.as || this.target.tableName) | ||
: this.target.tableName | ||
this.accessors = { | ||
@@ -53,3 +62,3 @@ get: Utils._.camelize('get_' + (this.options.as || Utils.singularize(this.target.tableName))), | ||
associatedObject.save() | ||
.on('success', function() { customEventEmitter.emit('success', '') }) | ||
.on('success', function() { customEventEmitter.emit('success', associatedObject) }) | ||
.on('failure', function(err) { customEventEmitter.emit('failure', err) }) | ||
@@ -56,0 +65,0 @@ }) |
@@ -12,3 +12,3 @@ var Utils = require("./../utils") | ||
var association = new HasOne(this, associatedModel, Utils._.extend((options||{}), this.options)) | ||
this.associations[associatedModel.tableName] = association.injectAttributes() | ||
this.associations[association.associationAccessor] = association.injectAttributes() | ||
}, | ||
@@ -19,3 +19,3 @@ belongsTo: function(associatedModel, options) { | ||
var association = new BelongsTo(this, associatedModel, Utils._.extend((options||{}), this.options)) | ||
this.associations[associatedModel.tableName] = association.injectAttributes() | ||
this.associations[association.associationAccessor] = association.injectAttributes() | ||
}, | ||
@@ -26,9 +26,8 @@ hasMany: function(associatedModel, options) { | ||
var association = new HasMany(this, associatedModel, Utils._.extend((options||{}), this.options)) | ||
this.associations[associatedModel.tableName] = association.injectAttributes() | ||
this.associations[association.associationAccessor] = association.injectAttributes() | ||
} | ||
}, | ||
instanceMethods: { | ||
} | ||
} | ||
@@ -62,7 +62,12 @@ var Utils = require("./utils") | ||
.on('success', function() { self.emit('success', self) }) | ||
.on('failure', function() { self.emit('failure', self) }) | ||
.on('failure', function(err) { self.emit('failure', err) }) | ||
} | ||
if(options.force) this.drop().on('success', function() { doQuery() }) | ||
else doQuery() | ||
if(options.force) { | ||
this.drop() | ||
.on('success', function() { doQuery() }) | ||
.on('failure', function(err) { self.emit('failure', err) }) | ||
} else { | ||
doQuery() | ||
} | ||
@@ -80,2 +85,13 @@ return this | ||
ModelDefinition.prototype.count = function(options) { | ||
var self = this | ||
var emitter = new Utils.CustomEventEmitter(function() { | ||
self.query(QueryGenerator.countQuery(self.tableName, options), self, {plain: true}).on('success', function(obj) { | ||
emitter.emit('success', obj['count(*)']) | ||
}) | ||
}) | ||
return emitter.run() | ||
} | ||
ModelDefinition.prototype.findAll = function(options) { | ||
@@ -129,3 +145,2 @@ return this.query(QueryGenerator.selectQuery(this.tableName, options)) | ||
Utils._.each(this.options.instanceMethods || {}, function(fct, name) { instance[name] = fct }) | ||
Utils._.each(this.associations, function(association, associationName) { | ||
@@ -132,0 +147,0 @@ association.injectGetter(instance) |
@@ -26,4 +26,11 @@ var Utils = require("./utils") | ||
emitter | ||
.on('success', function(){ self.finishedEmits++; self.finish() }) | ||
.on('failure', function(err){ self.finishedEmits++; self.fails.push(err); self.finish() }) | ||
.on('success', function(){ | ||
self.finishedEmits++ | ||
self.finish() | ||
}) | ||
.on('failure', function(err){ | ||
self.finishedEmits++ | ||
self.fails.push(err) | ||
self.finish() | ||
}) | ||
} | ||
@@ -30,0 +37,0 @@ QueryChainer.prototype.finish = function(result) { |
@@ -78,2 +78,6 @@ var Utils = require("./utils") | ||
countQuery: function(tableName, options) { | ||
return QueryGenerator.selectQuery(tableName, options).replace("*", "count(*)") | ||
}, | ||
/* | ||
@@ -80,0 +84,0 @@ Returns an insert into command. Parameters: table name + hash of attribute-value-pairs. |
@@ -55,3 +55,3 @@ var Utils = require("./utils") | ||
.on('success', function() { eventEmitter.emit('success', null) }) | ||
.on('failure', function() { eventEmitter.emit('failure', null) }) | ||
.on('failure', function(err) { eventEmitter.emit('failure', err) }) | ||
}) | ||
@@ -58,0 +58,0 @@ return eventEmitter.run() |
{ | ||
"name": "sequelize", | ||
"description": "MySQL ORM for Node.JS", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"author": "Sascha Depold <sascha@depold.com>", | ||
@@ -6,0 +6,0 @@ "contributors": [ |
module.exports = { | ||
username: 'root', | ||
password: null, | ||
database: 'sequelize_test', | ||
host: '127.0.0.1' | ||
username: 'root' | ||
, password: null | ||
, database: 'sequelize_test' | ||
, host: '127.0.0.1' | ||
, rand: function() { | ||
return parseInt(Math.random() * 99999999999999) | ||
} | ||
} |
@@ -12,3 +12,3 @@ var assert = require("assert") | ||
var num = parseInt(Math.random() * 9999999999999) | ||
var num = config.rand() | ||
, User = sequelize.define('User' + num, { name: Sequelize.STRING }) | ||
@@ -15,0 +15,0 @@ , Task = sequelize.define('Task' + num, { name: Sequelize.STRING }) |
@@ -7,3 +7,3 @@ var assert = require("assert") | ||
var initUsers = function(num, callback) { | ||
var User = sequelize.define('User' + parseInt(Math.random() * 99999999), { name: Sequelize.STRING, bio: Sequelize.TEXT }) | ||
var User = sequelize.define('User' + config.rand(), { name: Sequelize.STRING, bio: Sequelize.TEXT }) | ||
, users = [] | ||
@@ -28,3 +28,3 @@ | ||
'build should fill the object with default values': function() { | ||
var Task = sequelize.define('Task' + parseInt(Math.random() * 99999999), { | ||
var Task = sequelize.define('Task' + config.rand(), { | ||
title: {type: Sequelize.STRING, defaultValue: 'a task!'}, | ||
@@ -31,0 +31,0 @@ foo: {type: Sequelize.INTEGER, defaultValue: 2}, |
@@ -7,3 +7,3 @@ var assert = require("assert") | ||
var initUsers = function(num, callback) { | ||
return sequelize.define('User' + parseInt(Math.random() * 99999999), { name: Sequelize.STRING, bio: Sequelize.TEXT }) | ||
return sequelize.define('User' + config.rand(), { name: Sequelize.STRING, bio: Sequelize.TEXT }) | ||
} | ||
@@ -13,3 +13,3 @@ | ||
'do not allow duplicated records with unique:true': function(exit) { | ||
var User = sequelize.define('User' + parseInt(Math.random() * 99999999), { | ||
var User = sequelize.define('User' + config.rand(), { | ||
username: {type: Sequelize.STRING, unique: true} | ||
@@ -27,3 +27,3 @@ }) | ||
'it should raise an error if created object breaks definition constraints': function(exit) { | ||
var User = sequelize.define('User' + parseInt(Math.random() * 99999999), { | ||
var User = sequelize.define('User' + config.rand(), { | ||
username: {type: Sequelize.STRING, unique: true}, | ||
@@ -30,0 +30,0 @@ smth: {type: Sequelize.STRING, allowNull: false} |
@@ -8,3 +8,3 @@ var assert = require("assert") | ||
'destroy should delete a saved record from the database': function(exit) { | ||
var User = sequelize.define('User' + parseInt(Math.random() * 99999999), { name: Sequelize.STRING, bio: Sequelize.TEXT }) | ||
var User = sequelize.define('User' + config.rand(), { name: Sequelize.STRING, bio: Sequelize.TEXT }) | ||
User.sync({force: true}).on('success', function() { | ||
@@ -25,3 +25,3 @@ User.create({name: 'hallo', bio: 'welt'}).on('success', function(u) { | ||
'destroy should mark the record as deleted if paranoid is activated': function(exit) { | ||
var User = sequelize.define('User' + parseInt(Math.random() * 99999999), { name: Sequelize.STRING, bio: Sequelize.TEXT }, {paranoid:true}) | ||
var User = sequelize.define('User' + config.rand(), { name: Sequelize.STRING, bio: Sequelize.TEXT }, {paranoid:true}) | ||
User.sync({force: true}).on('success', function() { | ||
@@ -28,0 +28,0 @@ User.create({name: 'asd', bio: 'asd'}).on('success', function(u) { |
@@ -8,3 +8,3 @@ var assert = require("assert") | ||
'it should correctly determine equal objects': function(exit) { | ||
var User = sequelize.define('User' + parseInt(Math.random() * 99999999), { name: Sequelize.STRING, bio: Sequelize.TEXT }) | ||
var User = sequelize.define('User' + config.rand(), { name: Sequelize.STRING, bio: Sequelize.TEXT }) | ||
@@ -19,3 +19,3 @@ User.sync({force: true}).on('success', function() { | ||
'it should correctly work with different primary keys': function(exit) { | ||
var User = sequelize.define('User' + parseInt(Math.random() * 99999999), { | ||
var User = sequelize.define('User' + config.rand(), { | ||
foo: {type: Sequelize.STRING, primaryKey: true}, | ||
@@ -34,3 +34,3 @@ bar: {type: Sequelize.STRING, primaryKey: true}, | ||
'equalsOneOf should work': function(exit) { | ||
var User = sequelize.define('User' + parseInt(Math.random() * 99999999), { | ||
var User = sequelize.define('User' + config.rand(), { | ||
foo: {type: Sequelize.STRING, primaryKey: true}, | ||
@@ -37,0 +37,0 @@ bar: {type: Sequelize.STRING, primaryKey: true}, |
@@ -7,3 +7,3 @@ var assert = require("assert") | ||
var initUsers = function(num, callback) { | ||
var User = sequelize.define('User' + parseInt(Math.random() * 99999999), { name: Sequelize.STRING, bio: Sequelize.TEXT }) | ||
var User = sequelize.define('User' + config.rand(), { name: Sequelize.STRING, bio: Sequelize.TEXT }) | ||
var createUser = function() { | ||
@@ -65,3 +65,3 @@ User.create({name: 'user' + num, bio: 'foobar'}).on('success', function(user){ | ||
'find should find records by primaryKeys': function(exit) { | ||
var User = sequelize.define('User' + parseInt(Math.random() * 999999999), { | ||
var User = sequelize.define('User' + config.rand(), { | ||
identifier: {type: Sequelize.STRING, primaryKey: true}, | ||
@@ -68,0 +68,0 @@ name: Sequelize.STRING |
@@ -8,3 +8,3 @@ var assert = require("assert") | ||
'it should correctly add the foreign id - monodirectional': function() { | ||
var num = parseInt(Math.random() * 99999999) | ||
var num = config.rand() | ||
var User = sequelize.define('User' + num, { username: Sequelize.STRING }) | ||
@@ -17,3 +17,3 @@ var Task = sequelize.define('Task' + num, { title: Sequelize.STRING }) | ||
'it should correctly add the foreign ids - bidirectional': function(exit) { | ||
var num = parseInt(Math.random() * 99999999) | ||
var num = config.rand() | ||
var User = sequelize.define('User' + num, { username: Sequelize.STRING }) | ||
@@ -24,3 +24,3 @@ var Task = sequelize.define('Task' + num, { title: Sequelize.STRING }) | ||
User.hasMany(Task) | ||
assert.isUndefined(Task.attributes['User'+num+'Id']) | ||
@@ -38,3 +38,3 @@ assert.isUndefined(User.attributes['User'+num+'Id']) | ||
'it should correctly add the foreign id with underscore - monodirectional': function() { | ||
var num = parseInt(Math.random() * 99999999) | ||
var num = config.rand() | ||
var User = sequelize.define('User' + num, { username: Sequelize.STRING }) | ||
@@ -47,3 +47,3 @@ var Task = sequelize.define('Task' + num, { title: Sequelize.STRING}, {underscored: true}) | ||
'it should correctly add the foreign id with underscore - bidirectional': function() { | ||
var num = parseInt(Math.random() * 99999999) | ||
var num = config.rand() | ||
var User = sequelize.define('User' + num, { username: Sequelize.STRING }, {underscored: true}) | ||
@@ -66,3 +66,3 @@ var Task = sequelize.define('Task' + num, { title: Sequelize.STRING }) | ||
'it should correctly add the foreign id when defining the foreignkey as option - monodirectional': function() { | ||
var num = parseInt(Math.random() * 99999999) | ||
var num = config.rand() | ||
var User = sequelize.define('User' + num, { username: Sequelize.STRING }, {underscored: true}) | ||
@@ -75,3 +75,3 @@ var Task = sequelize.define('Task' + num, { title: Sequelize.STRING }) | ||
'it should correctly add the foreign id when defining the foreignkey as option - bidirectional': function() { | ||
var num = parseInt(Math.random() * 99999999) | ||
var num = config.rand() | ||
var User = sequelize.define('User' + num, { username: Sequelize.STRING }, {underscored: true}) | ||
@@ -91,3 +91,3 @@ var Task = sequelize.define('Task' + num, { title: Sequelize.STRING }) | ||
'it should define getter and setter - monodirectional': function() { | ||
var num = parseInt(Math.random() * 99999999) | ||
var num = config.rand() | ||
var User = sequelize.define('User' + num, { username: Sequelize.STRING }) | ||
@@ -103,3 +103,3 @@ var Task = sequelize.define('Task' + num, { title: Sequelize.STRING }) | ||
'it should define getter and setter - bidirectional': function() { | ||
var num = parseInt(Math.random() * 99999999) | ||
var num = config.rand() | ||
var User = sequelize.define('User' + num, { username: Sequelize.STRING }) | ||
@@ -120,3 +120,3 @@ var Task = sequelize.define('Task' + num, { title: Sequelize.STRING }) | ||
'it should define getter and setter according to as option - monodirectional': function() { | ||
var num = parseInt(Math.random() * 99999999) | ||
var num = config.rand() | ||
var User = sequelize.define('User' + num, { username: Sequelize.STRING }) | ||
@@ -132,3 +132,3 @@ var Task = sequelize.define('Task' + num, { title: Sequelize.STRING }) | ||
'it should define getter and setter according to as option - bidirectional': function() { | ||
var num = parseInt(Math.random() * 99999999) | ||
var num = config.rand() | ||
var User = sequelize.define('User' + num, { username: Sequelize.STRING }) | ||
@@ -149,4 +149,4 @@ var Task = sequelize.define('Task' + num, { title: Sequelize.STRING }) | ||
'it should set and get the correct objects - monodirectional': function(exit) { | ||
var User = sequelize.define('User' + parseInt(Math.random() * 99999999), { username: Sequelize.STRING }) | ||
var Task = sequelize.define('Task' + parseInt(Math.random() * 99999999), { title: Sequelize.STRING }) | ||
var User = sequelize.define('User' + config.rand(), { username: Sequelize.STRING }) | ||
var Task = sequelize.define('Task' + config.rand(), { title: Sequelize.STRING }) | ||
@@ -177,4 +177,4 @@ User.hasMany(Task, {as: 'Tasks'}) | ||
'it should set and get the correct objects - bidirectional': function(exit) { | ||
var User = sequelize.define('User' + parseInt(Math.random() * 99999999), { username: Sequelize.STRING }) | ||
var Task = sequelize.define('Task' + parseInt(Math.random() * 99999999), { title: Sequelize.STRING }) | ||
var User = sequelize.define('User' + config.rand(), { username: Sequelize.STRING }) | ||
var Task = sequelize.define('Task' + config.rand(), { title: Sequelize.STRING }) | ||
@@ -212,3 +212,44 @@ User.hasMany(Task, {as: 'Tasks'}) | ||
}) | ||
}, | ||
'it should correctly build the connector model names': function(exit){ | ||
var num = config.rand() | ||
, Person = sequelize.define('Person' + num, { name: Sequelize.STRING }) | ||
Person.hasMany(Person, {as: 'Children'}) | ||
Person.hasMany(Person, {as: 'Friends'}) | ||
Person.hasMany(Person, {as: 'CoWorkers'}) | ||
Person.sync({force: true}).on('success', function() { | ||
var modelNames = sequelize.modelManager.models.map(function(model) { return model.tableName }) | ||
, expectation = ["Person" + num + "s", "ChildrenPerson" + num + "s", "CoWorkersPerson" + num + "s", "FriendsPerson" + num + "s"] | ||
expectation.forEach(function(ex) { | ||
assert.eql(modelNames.indexOf(ex) > -1, true) | ||
}) | ||
exit(function(){}) | ||
}) | ||
}, | ||
'it should correctly get and set the connected models': function(exit) { | ||
var num = config.rand() | ||
, Person = sequelize.define('Person' + num, { name: Sequelize.STRING }) | ||
Person.hasMany(Person, {as: 'Children'}) | ||
Person.hasMany(Person, {as: 'Friends'}) | ||
Person.hasMany(Person, {as: 'CoWorkers'}) | ||
Person.sync({force: true}).on('success', function() { | ||
Person.create({name: 'foobar'}).on('success', function(person) { | ||
Person.create({name: 'friend'}).on('success', function(friend) { | ||
person.setFriends([friend]).on('success', function() { | ||
person.getFriends().on('success', function(friends) { | ||
assert.eql(friends.length, 1) | ||
assert.eql(friends[0].name, 'friend') | ||
exit(function(){}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
} | ||
} |
@@ -8,5 +8,5 @@ var assert = require("assert") | ||
'it should correctly add the foreign id': function() { | ||
var num = parseInt(Math.random() * 99999999) | ||
var num = config.rand() | ||
var User = sequelize.define('User' + num, { username: Sequelize.STRING }) | ||
var Task = sequelize.define('Task' + parseInt(Math.random() * 99999999), { title: Sequelize.STRING }) | ||
var Task = sequelize.define('Task' + num, { title: Sequelize.STRING }) | ||
@@ -17,5 +17,5 @@ User.hasOne(Task) | ||
'it should correctly add the foreign id with underscore': function() { | ||
var num = parseInt(Math.random() * 99999999) | ||
var num = config.rand() | ||
var User = sequelize.define('User' + num, { username: Sequelize.STRING }, {underscored: true}) | ||
var Task = sequelize.define('Task' + parseInt(Math.random() * 99999999), { title: Sequelize.STRING }) | ||
var Task = sequelize.define('Task' + num, { title: Sequelize.STRING }) | ||
@@ -26,3 +26,3 @@ User.hasOne(Task) | ||
'it should correctly add the foreign id when defining the foreignkey as option': function() { | ||
var num = parseInt(Math.random() * 99999999) | ||
var num = config.rand() | ||
var User = sequelize.define('User' + num, { username: Sequelize.STRING }, {underscored: true}) | ||
@@ -35,3 +35,3 @@ var Task = sequelize.define('Task' + num, { title: Sequelize.STRING }) | ||
'it should define getter and setter': function() { | ||
var num = parseInt(Math.random() * 99999999) | ||
var num = config.rand() | ||
var User = sequelize.define('User' + num, { username: Sequelize.STRING }) | ||
@@ -47,3 +47,3 @@ var Task = sequelize.define('Task' + num, { title: Sequelize.STRING }) | ||
'it should define getter and setter according to as option': function() { | ||
var num = parseInt(Math.random() * 99999999) | ||
var num = config.rand() | ||
var User = sequelize.define('User' + num, { username: Sequelize.STRING }) | ||
@@ -59,4 +59,4 @@ var Task = sequelize.define('Task' + num, { title: Sequelize.STRING }) | ||
'it should set and get the correct objects': function(exit) { | ||
var User = sequelize.define('User' + parseInt(Math.random() * 99999999), { username: Sequelize.STRING }) | ||
var Task = sequelize.define('Task' + parseInt(Math.random() * 99999999), { title: Sequelize.STRING }) | ||
var User = sequelize.define('User' + config.rand(), { username: Sequelize.STRING }) | ||
var Task = sequelize.define('Task' + config.rand(), { title: Sequelize.STRING }) | ||
@@ -81,4 +81,4 @@ User.hasOne(Task, {as: 'Task'}) | ||
'it should correctly unset the obsolete objects': function(exit) { | ||
var User = sequelize.define('User' + parseInt(Math.random() * 99999999), { username: Sequelize.STRING }) | ||
var Task = sequelize.define('Task' + parseInt(Math.random() * 99999999), { title: Sequelize.STRING }) | ||
var User = sequelize.define('User' + config.rand(), { username: Sequelize.STRING }) | ||
var Task = sequelize.define('Task' + config.rand(), { title: Sequelize.STRING }) | ||
@@ -108,3 +108,23 @@ User.hasOne(Task, {as: 'Task'}) | ||
}) | ||
}, | ||
'it should correctly associate with itself': function(exit) { | ||
var Person = sequelize.define('Person' + config.rand(), { name: Sequelize.STRING }) | ||
Person.hasOne(Person, {as: 'Mother', foreignKey: 'MotherId'}) | ||
Person.hasOne(Person, {as: 'Father', foreignKey: 'FatherId'}) | ||
Person.sync({force: true}).on('success', function() { | ||
var p = Person.build() | ||
assert.isDefined(p.setFather) | ||
assert.isDefined(p.setMother) | ||
exit(function(){}) | ||
}) | ||
}, | ||
'it should automatically set the foreignKey if it is a self association': function() { | ||
var num = config.rand() | ||
var Person = sequelize.define('Person' + num, { name: Sequelize.STRING }) | ||
Person.hasOne(Person, {as: 'Mother'}) | ||
assert.eql(Person.associations["MotherPerson"+num+"s"].options.foreignKey, 'MotherId') | ||
} | ||
} |
@@ -7,3 +7,3 @@ var assert = require("assert") | ||
var initUsers = function(num, callback) { | ||
var User = sequelize.define('User' + parseInt(Math.random() * 99999999), { name: Sequelize.STRING, bio: Sequelize.TEXT }) | ||
var User = sequelize.define('User' + config.rand(), { name: Sequelize.STRING, bio: Sequelize.TEXT }) | ||
, users = [] | ||
@@ -10,0 +10,0 @@ |
@@ -8,3 +8,3 @@ var assert = require("assert") | ||
'save should add a record to the database': function(exit) { | ||
var User = sequelize.define('User' + parseInt(Math.random() * 99999999), { name: Sequelize.STRING, bio: Sequelize.TEXT }) | ||
var User = sequelize.define('User' + config.rand(), { name: Sequelize.STRING, bio: Sequelize.TEXT }) | ||
@@ -26,3 +26,3 @@ User.sync({force: true}).on('success', function() { | ||
'save should update the timestamp updated_at': function(exit) { | ||
var User = sequelize.define('User' + parseInt(Math.random() * 99999999), { name: Sequelize.STRING, bio: Sequelize.TEXT }) | ||
var User = sequelize.define('User' + config.rand(), { name: Sequelize.STRING, bio: Sequelize.TEXT }) | ||
User.sync({force: true}).on('success', function() { | ||
@@ -29,0 +29,0 @@ var now = Date.now() |
@@ -5,3 +5,3 @@ var assert = require("assert") | ||
, sequelize = new Sequelize(config.database, config.username, config.password, {logging: false}) | ||
, User = sequelize.define('User' + parseInt(Math.random() * 9999999999999), { name: Sequelize.STRING, bio: Sequelize.TEXT }) | ||
, User = sequelize.define('User' + config.rand(), { name: Sequelize.STRING, bio: Sequelize.TEXT }) | ||
@@ -32,3 +32,3 @@ module.exports = { | ||
'it should not set primary keys or timestamps': function(exit) { | ||
User = sequelize.define('User' + parseInt(Math.random() * 9999999999999), { | ||
User = sequelize.define('User' + config.rand(), { | ||
name: Sequelize.STRING, bio: Sequelize.TEXT, identifier: {type: Sequelize.STRING, primaryKey: true} | ||
@@ -35,0 +35,0 @@ }) |
@@ -7,3 +7,3 @@ var assert = require("assert") | ||
var initUsers = function(num, callback) { | ||
var User = sequelize.define('User' + parseInt(Math.random() * 99999999), { name: Sequelize.STRING, bio: Sequelize.TEXT }, {timestamps:false}) | ||
var User = sequelize.define('User' + config.rand(), { name: Sequelize.STRING, bio: Sequelize.TEXT }, {timestamps:false}) | ||
, users = [] | ||
@@ -10,0 +10,0 @@ |
var assert = require("assert") | ||
, config = require("./../config") | ||
, Sequelize = require("./../../index") | ||
@@ -13,3 +14,3 @@ , sequelize = new Sequelize('database', 'username', 'password') | ||
'it should handle extended attributes correctly - unique': function() { | ||
var User = sequelize.define('User' + parseInt(Math.random() * 999999999), { | ||
var User = sequelize.define('User' + config.rand(), { | ||
username: {type: Sequelize.STRING, unique: true} | ||
@@ -20,3 +21,3 @@ }, { timestamps: false }) | ||
'it should handle extended attributes correctly - default': function() { | ||
var User = sequelize.define('User' + parseInt(Math.random() * 999999999), { | ||
var User = sequelize.define('User' + config.rand(), { | ||
username: {type: Sequelize.STRING, defaultValue: 'foo'} | ||
@@ -27,3 +28,3 @@ }, { timestamps: false }) | ||
'it should handle extended attributes correctly - null': function() { | ||
var User = sequelize.define('User' + parseInt(Math.random() * 999999999), { | ||
var User = sequelize.define('User' + config.rand(), { | ||
username: {type: Sequelize.STRING, allowNull: false} | ||
@@ -34,3 +35,3 @@ }, { timestamps: false }) | ||
'it should handle extended attributes correctly - primary key': function() { | ||
var User = sequelize.define('User' + parseInt(Math.random() * 999999999), { | ||
var User = sequelize.define('User' + config.rand(), { | ||
username: {type: Sequelize.STRING, primaryKey: true} | ||
@@ -41,3 +42,3 @@ }, { timestamps: false }) | ||
'primaryKeys should be correctly determined': function() { | ||
var User = sequelize.define('User' + parseInt(Math.random() * 999999999), { | ||
var User = sequelize.define('User' + config.rand(), { | ||
foo: {type: Sequelize.STRING, primaryKey: true}, | ||
@@ -49,4 +50,4 @@ bar: Sequelize.STRING | ||
'it should add updatedAt and createdAt if timestamps is undefined or true': function() { | ||
var User1 = sequelize.define('User' + parseInt(Math.random() * 999999999), {}) | ||
var User2 = sequelize.define('User' + parseInt(Math.random() * 999999999), {}, { timestamps: true }) | ||
var User1 = sequelize.define('User' + config.rand(), {}) | ||
var User2 = sequelize.define('User' + config.rand(), {}, { timestamps: true }) | ||
@@ -57,7 +58,7 @@ assert.eql(User1.attributes, {id:"INT NOT NULL auto_increment PRIMARY KEY", updatedAt:"DATETIME NOT NULL", createdAt:"DATETIME NOT NULL"}) | ||
'it should add deletedAt if paranoid is true': function() { | ||
var User = sequelize.define('User' + parseInt(Math.random() * 999999999), {}, { paranoid: true }) | ||
var User = sequelize.define('User' + config.rand(), {}, { paranoid: true }) | ||
assert.eql(User.attributes, {id:"INT NOT NULL auto_increment PRIMARY KEY", deletedAt:"DATETIME", updatedAt:"DATETIME NOT NULL", createdAt:"DATETIME NOT NULL"}) | ||
}, | ||
'timestamp columns should be underscored if underscored is passed': function() { | ||
var User = sequelize.define('User' + parseInt(Math.random() * 999999999), {}, { paranoid: true, underscored: true }) | ||
var User = sequelize.define('User' + config.rand(), {}, { paranoid: true, underscored: true }) | ||
assert.eql(User.attributes, {id:"INT NOT NULL auto_increment PRIMARY KEY", deleted_at:"DATETIME", updated_at:"DATETIME NOT NULL", created_at:"DATETIME NOT NULL"}) | ||
@@ -64,0 +65,0 @@ }, |
@@ -8,6 +8,6 @@ var assert = require("assert") | ||
'it should sync all models - so instances can be created and saved to the database without failures': function(exit) { | ||
var Project = sequelize.define('project' + parseInt(Math.random() * 9999999999999999999), { | ||
var Project = sequelize.define('project' + config.rand(), { | ||
title: Sequelize.STRING | ||
}) | ||
var Task = sequelize.define('task' + parseInt(Math.random() * 99999999999999999), { | ||
var Task = sequelize.define('task' + config.rand(), { | ||
title: Sequelize.STRING | ||
@@ -14,0 +14,0 @@ }) |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
374782
105
2757
20