Skip to content

Commit

Permalink
fix: store dates as ISO formatted strings
Browse files Browse the repository at this point in the history
BREAKING CHANGE: date formatting has changed to improve reliability and predictability.
  • Loading branch information
haltcase committed Jun 2, 2019
1 parent 0044ed5 commit d2a7cda
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
20 changes: 16 additions & 4 deletions src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ export default class Model <

const order = options.random ? 'random' : options.order
let query = this.ctx.knex(this.name).select()
query = helpers.buildWhere(query, criteria)
query = helpers.buildWhere(query, this.cast.toDefinition(
criteria || {},
{ raw: true, ...options }
))

if (order) query = helpers.buildOrder(query, order)
if (options.limit) query = query.limit(options.limit)
Expand Down Expand Up @@ -114,7 +117,10 @@ export default class Model <

const order = options.random ? 'random' : options.order
let query = this.ctx.knex(this.name).first()
query = helpers.buildWhere(query, criteria)
query = helpers.buildWhere(query, this.cast.toDefinition(
criteria || {},
{ raw: true, ...options }
))

if (order) query = helpers.buildOrder(query, order)
if (options.skip) query = query.offset(options.skip)
Expand Down Expand Up @@ -373,7 +379,10 @@ async function baseCount <D extends types.ReturnDict> (
const val = `${column} as count`
const builder = model.ctx.knex(model.name)
let query = options.distinct ? builder.countDistinct(val) : builder.count(val)
query = helpers.buildWhere(query, criteria)
query = helpers.buildWhere(query, model.cast.toDefinition(
criteria || {},
{ raw: true, ...options }
))

if (options.group) query = query.groupBy(toArray(options.group))

Expand All @@ -393,7 +402,10 @@ async function baseMinMax <D extends types.ReturnDict> (

const val = `${column} as ${method}`
let query = model.ctx.knex(model.name)[method](val)
query = helpers.buildWhere(query, criteria)
query = helpers.buildWhere(query, model.cast.toDefinition(
criteria || {},
{ raw: true, ...options }
))

if (options.group) query = query.groupBy(toArray(options.group))

Expand Down
2 changes: 1 addition & 1 deletion src/schema-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ export function toInputType (type: string, value: any): types.StorageType | neve
case 'increments':
return Number(value)
case 'date':
return new Date(value)
return (value as Date).toISOString()
default:
return invariant(false, `invalid type on insert to database: ${type}`)
}
Expand Down
6 changes: 4 additions & 2 deletions tests/find-or-create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,16 @@ test('creates missing objects or returns an existing one', async t => {
}

const fresh = await games.findOrCreate(first)

t.is(await games.count({ genre: 'FPS' }), 1)

const existing1 = await games.findOrCreate({ name: 'Overwatch' })
const existing2 = await games.findOrCreate(first)

t.deepEqual(fresh, existing1)
t.deepEqual(fresh, existing2)
t.is(await games.count({ genre: 'FPS' }), 1)

t.is(fresh!.last_played, existing1!.last_played)
t.is(fresh!.last_played, existing2!.last_played)
t.is(fresh!.last_played.toISOString(), existing1!.last_played.toISOString())
t.is(fresh!.last_played.toISOString(), existing2!.last_played.toISOString())
})

0 comments on commit d2a7cda

Please sign in to comment.