Comparing version 0.4.0 to 0.5.0
{ | ||
"name": "dbeasy", | ||
"version": "0.4.0", | ||
"version": "0.5.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", |
32
store.js
@@ -8,3 +8,4 @@ 'use strict'; | ||
SYS_COL_PREFIX = '__', | ||
BAG_COL = SYS_COL_PREFIX + 'bag'; | ||
BAG_COL = SYS_COL_PREFIX + 'bag', | ||
assert = require('assert'); | ||
@@ -148,3 +149,4 @@ exports.BAG_COL = BAG_COL; | ||
function getInsertContext(specName, data) { | ||
function getWriteContext(specName, data, opts) { | ||
assert(opts.partial !== undefined); | ||
var tableName = _str.underscored(specName); | ||
@@ -198,3 +200,3 @@ // Do not save derived values. | ||
var fieldName = _str.camelize(colName); | ||
if (inputData[fieldName] === undefined) { | ||
if (opts.partial && inputData[fieldName] === undefined) { | ||
return; | ||
@@ -233,3 +235,3 @@ } | ||
function addUpdateContext(onInsertContext, whereProps) { | ||
function addWhereContext(onInsertContext, whereProps) { | ||
return onInsertContext.then(function(ctx) { | ||
@@ -386,13 +388,6 @@ ctx.templateVars.whereColBinds = {}; | ||
ss.upsert = function(name, data) { | ||
return (data.id | ||
? ss.update(name, _.pick(data, 'id'), data) | ||
: ss.insert(name, data) | ||
); | ||
}; | ||
ss.insert = function(name, data) { | ||
return onSpecs | ||
.then(function() { | ||
return getInsertContext(name, data); | ||
return getWriteContext(name, data, {partial: false}); | ||
}) | ||
@@ -409,7 +404,16 @@ .then(function(ctx) { | ||
ss.update = function(name, whereProps, data) { | ||
ss.replace = function(name, whereProps, data) { | ||
return ss.update(name, whereProps, data, {partial: false}); | ||
}; | ||
ss.update = function(name, whereProps, data, opts) { | ||
opts = _.defaults(opts || {}, { | ||
partial: true | ||
}); | ||
data = _.omit(data, _.keys(defaultFields)); | ||
return onSpecs | ||
.then(function() { | ||
return addUpdateContext(getInsertContext(name, data), whereProps); | ||
return addWhereContext( | ||
getWriteContext(name, data, opts), | ||
whereProps); | ||
}) | ||
@@ -416,0 +420,0 @@ .then(function(ctx) { |
@@ -25,3 +25,3 @@ "use strict"; | ||
.then(function() { | ||
store = util.createStore({poolSize: 3, debug: true}); | ||
store = util.createStore({poolSize: 3}); | ||
return store.dropNamespace('bigBiz') | ||
@@ -42,3 +42,3 @@ .catch(_.noop); | ||
.then(function() { | ||
return store.upsert('foo.fooBar', {}); | ||
return store.insert('foo.fooBar', {}); | ||
}) | ||
@@ -74,13 +74,2 @@ .then(function() { | ||
expect(queryResults[0].__deleted).to.be.a('Date'); | ||
}) | ||
.then(function() { | ||
return store.insert('foo.fooBar', {creator: {id: '15'}}); | ||
}) | ||
.then(function(fooBar) { | ||
expect(fooBar).to.have.property('id'); | ||
fooBar.creator.id = '11'; | ||
return store.update('foo.fooBar', _.pick(fooBar, 'id'), fooBar); | ||
}) | ||
.then(function(fooBar) { | ||
expect(fooBar.creator).to.have.property('id', '11'); | ||
}); | ||
@@ -96,3 +85,3 @@ }); | ||
}); | ||
return store.upsert('bigBiz.emp', { | ||
return store.insert('bigBiz.emp', { | ||
creator: {id: '3'}, | ||
@@ -107,8 +96,9 @@ firstName: 'Mel', | ||
.then(function() { | ||
return store.upsert('bigBiz.emp', { | ||
id: '1', | ||
creator: {id: '3'}, | ||
firstName: 'Melly', | ||
interests: {favoriteSandwich: 'Falafel'}, | ||
}); | ||
return store.replace('bigBiz.emp', | ||
{ id: '1' }, | ||
{ | ||
creator: {id: '3'}, | ||
firstName: 'Melly', | ||
interests: {favoriteSandwich: 'Falafel'}, | ||
}); | ||
}) | ||
@@ -121,2 +111,13 @@ .then(function(result) { | ||
expect(result).to.not.have.property('dept'); | ||
}) | ||
.then(function() { | ||
return store.update('bigBiz.emp', | ||
{ id: '1' }, | ||
{ | ||
deptId: '4', | ||
}); | ||
}) | ||
.then(function(result) { | ||
expect(result).to.have.property('firstName', 'Melly'); | ||
expect(result.dept).to.have.property('id', '4'); | ||
}); | ||
@@ -138,3 +139,3 @@ | ||
}); | ||
return store.upsert('bigBiz.emp', { | ||
return store.insert('bigBiz.emp', { | ||
creator: {id: '3'}, | ||
@@ -165,3 +166,3 @@ dept: {id: '2'} | ||
}); | ||
return store.upsert('bigBiz.emp', { | ||
return store.insert('bigBiz.emp', { | ||
dept: {id: '2'} | ||
@@ -199,3 +200,3 @@ }).then(function(emp) { | ||
}); | ||
return store.upsert('bigBiz.emp', { | ||
return store.insert('bigBiz.emp', { | ||
creator: {id: 1}, | ||
@@ -226,3 +227,3 @@ firstName: 'Joe' | ||
}); | ||
return store.upsert({ | ||
return store.insert({ | ||
creator: {id: '3'}, | ||
@@ -244,3 +245,3 @@ firstName: 'Mel', | ||
}); | ||
return store.upsert('bigBiz.emp', { | ||
return store.insert('bigBiz.emp', { | ||
firstName: 'Mel', | ||
@@ -261,3 +262,3 @@ deptId: '1', | ||
}); | ||
return store.upsert('bigBiz.emp', {}) | ||
return store.insert('bigBiz.emp', {}) | ||
.then(function(result) { | ||
@@ -271,3 +272,3 @@ expect(result).to.not.have.property('id'); | ||
store.addSpec('bigBiz.emp', {}); | ||
return store.upsert('bigBiz.emp', {}) | ||
return store.insert('bigBiz.emp', {}) | ||
.then(function() { | ||
@@ -274,0 +275,0 @@ return store.findOne('bigBiz.emp', {id: '1'}); |
60363
1456