Skip to content

Commit

Permalink
fix: ensure timestamps/schema options are handled properly (#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
haltcase committed Aug 8, 2021
1 parent 7dce97d commit a30b29c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/schema-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,17 @@ export function toKnexSchema <D extends types.ReturnDict> (
for (const [name, descriptor] of Object.entries(model.schema)) {
// these timestamp fields are handled as part of the model options
// processing below, ignore them here so we don't duplicate the fields
if (options.timestamps && (name === 'created_at' || name === 'updated_at')) return
if (options.timestamps && (name === 'created_at' || name === 'updated_at')) {
continue
}

// each column's value is either its type or a descriptor
const type = getDataType(descriptor)
const partial = table[toKnexMethod(type)](name)

if (isFunction(descriptor) || !isObject(descriptor)) return
if (isFunction(descriptor) || !isObject(descriptor)) {
continue
}

const props = types.ColumnDescriptor.check(descriptor)

Expand Down
25 changes: 25 additions & 0 deletions tests/issues/110.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import test from 'ava'
import { connect } from '../../src'

const db = connect(':memory:')

test.before(async () => {
const tests = await db.model('tests', {
id: 'increments',
item: String
}, {
timestamps: true
})

await tests.create({
item: 'test'
})
})

test.after.always(() => db.close())

test('timestamp fields are available on created objects', async t => {
const found = await db.findOne('tests', { item: 'test' })
t.true(found.created_at instanceof Date)
t.true(found.updated_at instanceof Date)
})

0 comments on commit a30b29c

Please sign in to comment.