Compare commits

...

2 Commits

Author SHA1 Message Date
AJ ONeal
c825e7af06 allow changing id 2017-03-16 20:20:19 -04:00
AJ ONeal
4a2dde22c8 better error msg 2017-01-14 17:56:11 -05:00
2 changed files with 15 additions and 12 deletions

View File

@ -81,10 +81,10 @@ API
It's kinda CRUDdy... but don't let that scare you.
* `upsert(id, data)` - creates or updates based on existence in DB (use this)
* `upsert(id, data[, oldId])` - creates or updates based on existence in DB (use this)
* modifies `createdAt` and or `updatedAt`
* `create(id, obj)` - same as above, but fails if the object exists
* `save(data)` - (just don't use this, please) creates or updates based on presence of ID
* `save(data[, oldId])` - (just don't use this, please) creates or updates based on presence of ID
* `destroy(id)` - mark a record as `deletedAt` from DB
* `get(id)` - grab one by id
* `find(attrs, opts)` - grab many by indexable attributes

View File

@ -97,7 +97,8 @@ function wrap(db, dir, dbsMap) {
db.all(sql, earr, function (err, results) {
if (err) {
console.error("[Error] add column '" + tablename + "'");
console.error(err.stack);
console.error(sql);
console.error(err.stack || new Error('stack').stack);
cb(err);
return;
}
@ -302,13 +303,13 @@ function wrap(db, dir, dbsMap) {
}).then(simpleParse);
};
DB.upsert = function (id, data) {
DB.upsert = function (id, data, oldId) {
if (!data) {
data = id;
id = data[idnameCased];
}
return DB.set(id, data).then(function (result) {
return DB.set(oldId || id, data, oldId).then(function (result) {
var success = result.changes >= 1;
if (success) {
@ -319,8 +320,8 @@ function wrap(db, dir, dbsMap) {
});
};
DB.save = function (data) {
if (!data[idnameCased]) {
DB.save = function (data, oldId) {
if (!data[idnameCased] && !oldId) {
// NOTE saving the id both in the object and the id for now
var UUID = require('node-uuid');
data[idnameCased] = UUID.v4();
@ -330,7 +331,7 @@ function wrap(db, dir, dbsMap) {
});
}
return DB.set(data[idnameCased], data).then(function (result) {
return DB.set(oldId || data[idnameCased], data, oldId).then(function (result) {
var success = result.changes >= 1;
if (success) {
@ -395,7 +396,7 @@ function wrap(db, dir, dbsMap) {
};
// pull indices from object
function strainUpdate(id, data/*, vals*/, cb) {
function strainUpdate(id, data/*, vals*/, cb, oldId) {
var fieldable = [];
var json;
var sql;
@ -440,7 +441,9 @@ function wrap(db, dir, dbsMap) {
delete data[camelCase(col.name)];
});
delete data[idnameCased];
if (!oldId) {
delete data[idnameCased];
}
if (!fieldable.length || Object.keys(data).length) {
json = JSON.stringify(data);
@ -466,7 +469,7 @@ function wrap(db, dir, dbsMap) {
return sql;
}
DB.set = function (id, obj) {
DB.set = function (id, obj, oldId) {
obj.updatedAt = Date.now();
var json = JSON.stringify(obj);
@ -484,7 +487,7 @@ function wrap(db, dir, dbsMap) {
//var vals = [];
// removes known fields from data
data.updated_at = Date.now();
var sql = strainUpdate(id, data/*, vals*/, sqlTpl);
var sql = strainUpdate(id, data/*, vals*/, sqlTpl, oldId);
//console.log('[debug] DB.set() sql:', sql);
db.run(sql, /*vals*/[], function (err) {