Skip to content

orderBy with dates #25

@mikew

Description

@mikew

Whenever I use orderBy with a date the results are never consistent.

With this code:

export const SurveyError = defineMapper({
  name: 'SurveyError',
})

function populate () {
  const fullDay = 24 * 60 * 60 * 1000
  const now = new Date()

  return Promise.all([
    SurveyError.create({ happenedAt: new Date(now - fullDay * 0) }),
    SurveyError.create({ happenedAt: new Date(now - fullDay * 1) }),
    SurveyError.create({ happenedAt: new Date(now - fullDay * 2) }),
    SurveyError.create({ happenedAt: new Date(now - fullDay * 3) }),
  ])
}

function logDates () {
  return SurveyError
    .findAll({ orderBy: [ [ 'happenedAt', 'asc' ] ] })
    .then(items => {
      console.log(items.map(x => x.happenedAt))
    })
}

populate()
  .then(logDates)
  .catch(console.error)

You can run it multiple times and get different results:

// Run 1
[ 2016-12-04T20:56:12.288Z,
  2016-12-05T20:56:12.288Z,
  2016-12-06T20:56:12.288Z,
  2016-12-03T20:56:12.288Z ]

// Run 2
[ 2016-12-04T20:58:21.900Z,
  2016-12-05T20:58:21.900Z,
  2016-12-06T20:58:21.900Z,
  2016-12-03T20:58:21.900Z ]

// Run 3
[ 2016-12-06T20:58:37.565Z,
  2016-12-05T20:58:37.565Z,
  2016-12-03T20:58:37.565Z,
  2016-12-04T20:58:37.565Z ]

Should the dates be stored as an integer?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions