Skip to content

Commit

Permalink
fix(update): handle type definitions on update
Browse files Browse the repository at this point in the history
Closes #31
  • Loading branch information
citycide committed Feb 25, 2017
1 parent 1706940 commit eeda08c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
6 changes: 4 additions & 2 deletions src/model.js
Expand Up @@ -97,8 +97,10 @@ export default class Model {
}

update (criteria, data, options) {
let query = this.ctx.knex(this.name).update(data)
query = helpers.buildWhere(query, criteria)
let typedData = types.toDefinition(this, data)
let typedCriteria = types.toDefinition(this, criteria)
let query = this.ctx.knex(this.name).update(typedData)
query = helpers.buildWhere(query, typedCriteria)

return helpers.runQuery(this.ctx, query)
}
Expand Down
4 changes: 4 additions & 0 deletions src/types.js
Expand Up @@ -36,6 +36,10 @@ export function toKnexSchema (model, options) {

// for insertions / updates
export function toDefinition (model, object) {
if (util.isArray(object)) {
return toColumnDefinition(model, object[0], object[2])
}

return util.map(object, (value, column) => {
return toColumnDefinition(model, column, value)
})
Expand Down
24 changes: 19 additions & 5 deletions tests/update.js
Expand Up @@ -10,13 +10,21 @@ const db = new Trilogy(filePath)
test.before(async () => {
await db.model('one', {
first: String,
second: String
second: String,
third: Boolean,
array: Array
})

await db.create('one', {
first: 'fee',
second: 'blah'
})
await Promise.all([
db.create('one', {
first: 'fee',
second: 'blah'
}),
db.create('one', {
third: false,
array: [1, 2, 3]
})
])
})

test.after.always('remove test database file', () => {
Expand All @@ -28,3 +36,9 @@ test('changes the value of an existing key', async t => {
let res = await db.get('one.second', { first: 'fee' })
t.is(res, 'blurg')
})

test('handles model type definitons correctly', async t => {
await db.update('one', { third: false }, { array: [4, 5, 6] })
let res = await db.get('one.array', { third: false })
t.deepEqual(res, [4, 5, 6])
})

0 comments on commit eeda08c

Please sign in to comment.