Skip to content
This repository has been archived by the owner on Mar 6, 2020. It is now read-only.

Date handling for GQL #6

Closed
ErisDS opened this issue Jul 27, 2015 · 4 comments
Closed

Date handling for GQL #6

ErisDS opened this issue Jul 27, 2015 · 4 comments

Comments

@ErisDS
Copy link
Member

ErisDS commented Jul 27, 2015

At the moment, there is little-to-no consideration for date handling in GQL. There are a few mentions of dates in TryGhost/Ghost#5604. In particular the post-processing section under Implementation talks about handling dates after the parser is finished.

Date-like values can realistically appear in different forms:

  • A Number could also be a JavaScript timestamp
  • A Literal could be a valid ISO 8601 date like 2015-07-27T14:54:21
  • A String could contain a valid date in many different forms

It is very difficult to distinguish a date from another value at the lexer level, or to determine whether a String was intended to be a date or a string. The best way we have, therefore, to detect dates, is to assume that any value provided for a date-based property is a date.

That is, if the property name is created_at, updated_at, published_at, last_login or any other known dates and the value is a Number, a Literal or a String, we should try to turn the value into a date using new Date(). This provides us with a well understood set of recognised date formats, and should mean that passing dates from handlebars inside a Ghost theme will work.

E.g. {{#get "posts" filter="published_at: >'{{post.published_at}}'"}} (note the date is wrapped in single-quotes), without any date handling, will create a query in the form:

select * from "posts" where "posts"."published_at" > 'Sat Jun 27 2015 13:02:12 GMT+0100 (BST)'

And SQL doesn't know what to do with that date.

Instead we need to ensure that if we have a date that can be understood by new Date, then SQL is always passed a date in a format it understands.

We also need to ensure this works for all the supported SQL types: SQLite, MySQL and pg.

@schuer
Copy link

schuer commented Dec 13, 2015

Just want to say I would love to see this feature coming :)
The other day I was about to set up an archives page and got stuck at bundling posts by year. Took me a while to find out it’s GQL missing the date features.

(Thx for you good work at Ghost and for spotting issues even before users run across them!)

@laran laran mentioned this issue Mar 26, 2016
@laran
Copy link

laran commented Mar 27, 2016

I implemented this here: laran@279f363

I called them 'transformers'. Basically callbacks which transform each value.

It's an extension of the work in #17. If that PR gets accepted the transformers can be merged in as well.

@kirrg001
Copy link
Contributor

@ErisDS Do you want to keep this issue? Let me know, then i will move it over to NQL 🙂

@ErisDS
Copy link
Member Author

ErisDS commented Dec 13, 2018

We can close this. It turns out dates do basically work and we use them in Ghost.

If we decide to improve this, we can always raise a new issue with a proper spec for what we want to improve.

@ErisDS ErisDS closed this as completed Dec 13, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants