Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

orderBy with dates #25

Open
mikew opened this issue Dec 6, 2016 · 1 comment
Open

orderBy with dates #25

mikew opened this issue Dec 6, 2016 · 1 comment

Comments

@mikew
Copy link

mikew commented Dec 6, 2016

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?

@jmdobry
Copy link
Member

jmdobry commented Dec 12, 2016

Storing as an integer could work, but I think you'd want them stored as Rethink's native Time type. How does it work for you if you construct your dates with r#time() instead of new Date()?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants