Comparing version 0.2.3 to 0.3.0
{ | ||
"name": "dbeasy", | ||
"version": "0.2.3", | ||
"version": "0.3.0", | ||
"description": "Promise-based wrapper for postgresql driver that makes easy what should be easy while protecting your foot.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
38
store.js
@@ -55,2 +55,9 @@ 'use strict'; | ||
var defaultFields = { | ||
id: 'bigint NOT NULL', | ||
created: 'timestamp without time zone DEFAULT now() NOT NULL', | ||
updated: 'timestamp without time zone DEFAULT now() NOT NULL', | ||
}; | ||
var neverUpdated = ['id', 'created']; | ||
var db = client.connect(options); | ||
@@ -148,2 +155,5 @@ db.prepareDir(path.join(__dirname, 'sql')); | ||
.then(function(cols) { | ||
cols = _.reject(cols, function(col) { | ||
return _.contains(neverUpdated, col.columnName); | ||
}); | ||
var inputData = {}; | ||
@@ -219,2 +229,16 @@ inputData[BAG_COL] = {}; | ||
function addUpdateContext(onInsertContext, whereProps) { | ||
return onInsertContext.then(function(ctx) { | ||
ctx.templateVars.whereColBinds = {}; | ||
var nextBindVar = _.keys(ctx.templateVars.bindVars).length + 1; | ||
_.each(whereProps, function(val, name) { | ||
ctx.inputData[name] = val; | ||
ctx.templateVars.bindVars[nextBindVar] = name; | ||
ctx.templateVars.whereColBinds[nextBindVar] = name; | ||
nextBindVar++; | ||
}); | ||
return ctx; | ||
}); | ||
} | ||
function addNamespace(namespace) { | ||
@@ -345,8 +369,3 @@ var schema = _str.underscored(namespace); | ||
spec.fields = addDefaultFields(spec.fields, { | ||
id: 'bigint NOT NULL', | ||
creatorId: 'bigint NOT NULL', | ||
created: 'timestamp without time zone DEFAULT now() NOT NULL', | ||
updated: 'timestamp without time zone DEFAULT now() NOT NULL', | ||
}); | ||
spec.fields = addDefaultFields(spec.fields, defaultFields); | ||
@@ -366,3 +385,3 @@ onSpecs = onSpecs | ||
return (data.id | ||
? ss.update(name, data) | ||
? ss.update(name, _.pick(data, 'id'), data) | ||
: ss.insert(name, data) | ||
@@ -387,6 +406,7 @@ ); | ||
ss.update = function(name, data) { | ||
ss.update = function(name, whereProps, data) { | ||
data = _.omit(data, _.keys(defaultFields)); | ||
return onSpecs | ||
.then(function() { | ||
return getInsertContext(name, data); | ||
return addUpdateContext(getInsertContext(name, data), whereProps); | ||
}) | ||
@@ -393,0 +413,0 @@ .then(function(ctx) { |
@@ -12,2 +12,3 @@ "use strict"; | ||
util = require('./util'); | ||
Promise.longStackTraces(); | ||
@@ -35,3 +36,3 @@ suite('Store', function() { | ||
.then(function() { | ||
return store.upsert('foo.fooBar', {creator: {id: '9'}}); | ||
return store.upsert('foo.fooBar', {}); | ||
}) | ||
@@ -46,3 +47,2 @@ .then(function() { | ||
expect(getterResult).to.have.property('id', '1'); | ||
expect(getterResult.creator).to.eql({id: '9'}); | ||
expect(getterResult.created).to.be.a('Date'); | ||
@@ -76,3 +76,3 @@ expect(getterResult.updated).to.be.a('Date'); | ||
fooBar.creator.id = '11'; | ||
return store.update('foo.fooBar', fooBar); | ||
return store.update('foo.fooBar', _.pick(fooBar, 'id'), fooBar); | ||
}) | ||
@@ -233,3 +233,2 @@ .then(function(fooBar) { | ||
return store.upsert('bigBiz.emp', { | ||
creatorId: '3', | ||
firstName: 'Mel', | ||
@@ -239,3 +238,2 @@ deptId: '1', | ||
.then(function(result) { | ||
expect(result.creator).to.have.property('id', '3'); | ||
expect(result.dept).to.have.property('id', '1'); | ||
@@ -261,8 +259,8 @@ }); | ||
store.addSpec('bigBiz.emp', {}); | ||
return store.upsert('bigBiz.emp', {creatorId: '99'}) | ||
return store.upsert('bigBiz.emp', {}) | ||
.then(function() { | ||
return store.findOne('bigBiz.emp', {creatorId: '99'}); | ||
return store.findOne('bigBiz.emp', {id: '1'}); | ||
}) | ||
.then(function(result) { | ||
expect(result.creator).to.have.property('id', '99'); | ||
expect(result).to.have.property('id', '1'); | ||
}); | ||
@@ -269,0 +267,0 @@ }); |
Sorry, the diff of this file is not supported yet
60072
1444