From e551bbd7a3abc5d459047f4abf349c9fb69c54d6 Mon Sep 17 00:00:00 2001 From: citycide Date: Thu, 2 Feb 2017 23:26:21 -0600 Subject: [PATCH] feat(create): return the created object Change the return value of `create()` to be the newly created object, or the existing one if a unique violation was encountered. This makes more sense given that in general the number modifed would always have been 0 or 1 so it might as well give you the object if 1, or nothing if 0. BREAKING CHANGE: The return value of `create()` is no longer the number of created objects. Instead, `create()` returns the created object. --- src/helpers.js | 36 ++++++++++++++++++++++++++++++++---- src/model.js | 3 ++- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/src/helpers.js b/src/helpers.js index 6fe1864..742f46b 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -67,8 +67,10 @@ export function isValidWhere (where) { } export function runQuery (instance, query, needResponse) { + let asString = query.toString() + let action = getQueryAction(asString) if (util.isFunction(instance.verbose)) { - instance.verbose(query.toString()) + instance.verbose(asString) } if (instance.isNative) { @@ -83,14 +85,14 @@ export function runQuery (instance, query, needResponse) { let response if (needResponse) { - response = parseResponse(db.exec(query.toString())) + response = parseResponse(db.exec(asString)) if (query._sequence && query._sequence[0].method === 'hasTable') { response = !!response.length } } else { - db.run(query.toString()) + db.run(asString) - if (util.isOneOf(['insert', 'update', 'delete'], query._method)) { + if (util.isOneOf(['insert', 'update', 'delete'], action)) { response = db.getRowsModified() } } @@ -100,3 +102,29 @@ export function runQuery (instance, query, needResponse) { return response }) } + +export function findLastObject (model, object) { + let key = '' + let hasIncrements = false + util.each(model.schema, (props, name) => { + if (props === 'increments' || props.type === 'increments') { + key = name + hasIncrements = true + } else if (props.primary) { + key = name + } + }) + + if (!key && !hasIncrements) return + + let query = hasIncrements + ? model.ctx.knex('sqlite_sequence').first('seq').where({ name: model.name }) + : model.ctx.knex(model.name).first().where({ [key]: object[key] }) + + return runQuery(model.ctx, query, true) + .then(res => hasIncrements ? model.findOne({ [key]: res.seq }) : res) +} + +function getQueryAction (str) { + return str.split(' ', 1)[0].toLowerCase() +} diff --git a/src/model.js b/src/model.js index 5d066d8..2395185 100644 --- a/src/model.js +++ b/src/model.js @@ -21,6 +21,8 @@ export default class Model { ) return helpers.runQuery(this.ctx, query) + .then(() => helpers.findLastObject(this, object)) + .then(res => res || this.findOne(object)) } find (column, criteria, options = {}) { @@ -91,7 +93,6 @@ export default class Model { .then(existing => { if (existing) return existing return this.create({ ...criteria, ...creation }) - .then(() => this.findOne(criteria)) }) }