Comparing version 3.2.0 to 3.3.0
47
index.js
@@ -96,2 +96,6 @@ var crypto = require("crypto"); | ||
if (!obj.hasIdentity()) { | ||
throw new Error(obj._meta.table + ' entity must have ' + obj._meta.id + ' to be updated'); | ||
} | ||
if (obj._meta.compoundKey) { | ||
@@ -103,6 +107,2 @@ keys.push.apply(keys, obj._meta.id); | ||
} else { | ||
if (obj.identity() === undefined) { | ||
throw new Error('entity must have ' + obj._meta.id + ' to be updated'); | ||
} | ||
keys.push(obj._meta.id); | ||
@@ -191,3 +191,5 @@ whereClause = obj._meta.id + ' = @' + obj._meta.id; | ||
var self = this; | ||
var force = options && options.hasOwnProperty('force')? options.force: false; | ||
var forceUpdate = options && options.hasOwnProperty('update')? options.update: false; | ||
var forceInsert = options && options.hasOwnProperty('insert')? options.insert: false; | ||
var force = options && options.hasOwnProperty('force')? options.force: forceInsert || forceUpdate; | ||
@@ -208,3 +210,3 @@ var waitForOneToManys; | ||
if (self.changed() || force) { | ||
var writePromise = self.saved() ? update(self) : insert(self); | ||
var writePromise = self.saved() || forceUpdate ? update(self) : insert(self); | ||
@@ -235,5 +237,3 @@ return writePromise.then(function () { | ||
if (waitForOneToManys) { | ||
return self._saving.then(function () { | ||
return Promise.all(oneToManyPromises); | ||
}); | ||
return Promise.all(oneToManyPromises.concat([self._saving])) | ||
} else { | ||
@@ -249,9 +249,22 @@ return self._saving; | ||
identity: function () { | ||
if (this.hasIdentity()) { | ||
if (this._meta.compoundKey) { | ||
var self = this; | ||
return this._meta.id.map(function (id) { | ||
return self[id]; | ||
}); | ||
} else { | ||
return this[this._meta.id]; | ||
} | ||
} | ||
}, | ||
hasIdentity: function () { | ||
if (this._meta.compoundKey) { | ||
var self = this; | ||
return this._meta.id.map(function (id) { | ||
return self[id]; | ||
return this._meta.id.every(function (id) { | ||
return self.hasOwnProperty(id) && !!self[id] | ||
}); | ||
} else { | ||
return this[this._meta.id]; | ||
return this.hasOwnProperty(this._meta.id) && !!this[this._meta.id] | ||
} | ||
@@ -296,11 +309,15 @@ }, | ||
insert: function () { | ||
return insert(this) | ||
return this.save({insert: true}) | ||
}, | ||
update: function () { | ||
return update(this) | ||
return this.save({update: true}) | ||
}, | ||
upsert: function () { | ||
return update(this) | ||
if (this.hasIdentity()) { | ||
return this.update() | ||
} else { | ||
return this.insert() | ||
} | ||
} | ||
@@ -307,0 +324,0 @@ }; |
{ | ||
"name": "sworm", | ||
"version": "3.2.0", | ||
"version": "3.3.0", | ||
"description": "a lightweight write-only ORM for MSSQL, MySQL, PostgreSQL, Oracle, Sqlite 3", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -42,2 +42,3 @@ var fs = require("fs-promise"); | ||
var person; | ||
var personWeirdId | ||
var address; | ||
@@ -98,2 +99,10 @@ var personAddress; | ||
}); | ||
personWeirdId = db.model({ | ||
table: "people_weird_id", | ||
id: "weird_id", | ||
foreignKeyFor: function(x) { | ||
return x + "_weird_id"; | ||
} | ||
}); | ||
}); | ||
@@ -180,6 +189,2 @@ }); | ||
it("can insert emtpy rows", function() { | ||
var personWeirdId = db.model({ | ||
table: "people_weird_id", | ||
id: "weird_id" | ||
}); | ||
var p = personWeirdId({}); | ||
@@ -468,6 +473,2 @@ return p.save().then(function() { | ||
it("can insert with weird_id", function() { | ||
var personWeirdId = db.model({ | ||
table: "people_weird_id", | ||
id: "weird_id" | ||
}); | ||
var p = personWeirdId({ | ||
@@ -855,10 +856,2 @@ name: "bob" | ||
it("can save a many to one relationship with a custom foreign key", function() { | ||
var personWeirdId = db.model({ | ||
table: "people_weird_id", | ||
id: "weird_id", | ||
foreignKeyFor: function(x) { | ||
return x + "_weird_id"; | ||
} | ||
}); | ||
var bob = personWeirdId({ | ||
@@ -1149,2 +1142,17 @@ name: "bob", | ||
}); | ||
it('saves one to manies first', function () { | ||
var bob = person({name: 'bob', address: address({address: 'lolo st'})}) | ||
return bob.insert().then(function () { | ||
return db.query('select * from addresses') | ||
}).then(function (rows) { | ||
expect(rows).to.eql([ | ||
{ | ||
address: 'lolo st', | ||
id: bob.address_id | ||
} | ||
]) | ||
}) | ||
}); | ||
}); | ||
@@ -1169,2 +1177,21 @@ | ||
it('saves one to manies first', function () { | ||
var bob = person({name: 'bob'}) | ||
return bob.save().then(function () { | ||
var bob2 = person({name: 'bob2', id: bob.id, address: address({address: 'lolo st'})}) | ||
return bob2.update().then(function () { | ||
return db.query('select * from addresses') | ||
}).then(function (rows) { | ||
expect(rows).to.eql([ | ||
{ | ||
address: 'lolo st', | ||
id: bob2.address_id | ||
} | ||
]) | ||
}) | ||
}) | ||
}); | ||
it('cannot update without id set', function () { | ||
@@ -1176,9 +1203,7 @@ var bob = person({name: 'bob'}) | ||
return expect(function () { | ||
bob2.update() | ||
}).to.throw('entity must have id to be updated') | ||
return expect(bob2.update()).to.eventually.be.rejectedWith('entity must have id to be updated') | ||
}) | ||
}); | ||
it('cannot update without id set', function () { | ||
it('throws if no entity is found to update', function () { | ||
var bob = person({name: 'bob'}) | ||
@@ -1196,9 +1221,100 @@ | ||
describe('upsert', function () { | ||
var bob | ||
beforeEach(function () { | ||
bob = person({name: 'bob'}) | ||
return bob.save() | ||
}) | ||
it('inserts when there is no id', function () { | ||
var bob = person({name: 'bob'}) | ||
var bob2 = person({name: 'bob2'}) | ||
return bob.save().then(function () { | ||
return bob2.upsert().then(function () { | ||
return db.query('select * from people') | ||
}).then(function (rows) { | ||
expect(rows.map(function (r) { return r.name })).to.eql([ | ||
'bob', | ||
'bob2' | ||
]) | ||
}) | ||
}); | ||
it('updates when there is an id', function () { | ||
var bob2 = person({name: 'bob2', id: bob.id}) | ||
return bob2.upsert().then(function () { | ||
return db.query('select * from people') | ||
}).then(function (rows) { | ||
expect(rows.map(function (r) { return r.name })).to.eql([ | ||
'bob2' | ||
]) | ||
}) | ||
}); | ||
}); | ||
describe('identity', function () { | ||
describe('non-compound keys', function () { | ||
context('with ids', function () { | ||
it('returns identity', function () { | ||
var bob = personWeirdId({name: 'bob', weird_id: 5}) | ||
expect(bob.identity()).to.eql(5) | ||
}) | ||
it('has identity', function () { | ||
var bob = personWeirdId({name: 'bob', weird_id: 5}) | ||
expect(bob.hasIdentity()).to.be.true | ||
}) | ||
}) | ||
context('without ids', function () { | ||
it('returns undefined', function () { | ||
var bob = personWeirdId({name: 'bob'}) | ||
expect(bob.identity()).to.eql(undefined) | ||
}) | ||
it('does not have identity', function () { | ||
var bob = personWeirdId({name: 'bob'}) | ||
expect(bob.hasIdentity()).to.be.false | ||
}) | ||
}) | ||
}) | ||
describe('compound keys', function () { | ||
context('with ids', function () { | ||
it('returns identity', function () { | ||
var bobAddress = personAddress({address_id: 1, person_id: 2}) | ||
expect(bobAddress.identity()).to.eql([1, 2]) | ||
}) | ||
it('has identity', function () { | ||
var bobAddress = personAddress({address_id: 1, person_id: 2}) | ||
expect(bobAddress.hasIdentity()).to.be.true | ||
}) | ||
}) | ||
context('without ids', function () { | ||
it('returns undefined', function () { | ||
var bobAddress = personAddress({}) | ||
expect(bobAddress.identity()).to.eql(undefined) | ||
}) | ||
it('does not have identity', function () { | ||
var bobAddress = personAddress({}) | ||
expect(bobAddress.hasIdentity()).to.be.false | ||
}) | ||
}) | ||
context('with half ids', function () { | ||
it('returns undefined', function () { | ||
var bobAddress = personAddress({address_id: 1}) | ||
expect(bobAddress.identity()).to.eql(undefined) | ||
}) | ||
it('does not have identity', function () { | ||
var bobAddress = personAddress({}) | ||
expect(bobAddress.hasIdentity()).to.be.false | ||
}) | ||
}) | ||
}) | ||
}) | ||
}); | ||
@@ -1205,0 +1321,0 @@ |
@@ -1,2 +0,1 @@ | ||
var dockerHostname = require('./dockerHostname'); | ||
var describeDatabase = require('./describeDatabase'); | ||
@@ -10,3 +9,3 @@ var sworm = require('..'); | ||
? { user: "travis", password: "", database: name } | ||
: { host: dockerHostname, user: "root", password: "password", database: name } | ||
: { host: 'localhost', user: "root", password: "password", database: name } | ||
}; | ||
@@ -13,0 +12,0 @@ } |
if (!process.env.TRAVIS) { | ||
var dockerHostname = require('./dockerHostname'); | ||
var describeDatabase = require('./describeDatabase'); | ||
@@ -107,3 +106,3 @@ var sworm = require('..'); | ||
driver: 'oracle', | ||
url: addUrlParams('oracle://system:oracle@' + dockerHostname + ':1521/XE', options) | ||
url: addUrlParams('oracle://system:oracle@localhost:1521/XE', options) | ||
}; | ||
@@ -118,3 +117,3 @@ } | ||
password: "oracle", | ||
connectString: dockerHostname + ':1521/XE' | ||
connectString: 'localhost:1521/XE' | ||
}, options) | ||
@@ -121,0 +120,0 @@ }; |
@@ -1,2 +0,1 @@ | ||
var dockerHostname = require('./dockerHostname'); | ||
var describeDatabase = require('./describeDatabase'); | ||
@@ -11,3 +10,3 @@ var sworm = require('..'); | ||
name = name || ''; | ||
var url = process.env.TRAVIS? 'postgres://postgres@localhost/' + name: 'postgres://postgres:password@' + dockerHostname + '/' + name; | ||
var url = process.env.TRAVIS? 'postgres://postgres@localhost/' + name: 'postgres://postgres:password@localhost/' + name; | ||
@@ -30,3 +29,3 @@ if (extras) { | ||
: { | ||
host: dockerHostname, | ||
host: 'localhost', | ||
user: 'postgres', | ||
@@ -33,0 +32,0 @@ password: 'password', |
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
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
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
125064
2948
7
28