Skip to content

Commit

Permalink
feat(create): return the created object
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
citycide committed Feb 3, 2017
1 parent e707d64 commit e551bbd
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
36 changes: 32 additions & 4 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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()
}
}
Expand All @@ -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()
}
3 changes: 2 additions & 1 deletion src/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}) {
Expand Down Expand Up @@ -91,7 +93,6 @@ export default class Model {
.then(existing => {
if (existing) return existing
return this.create({ ...criteria, ...creation })
.then(() => this.findOne(criteria))
})
}

Expand Down

0 comments on commit e551bbd

Please sign in to comment.