diff --git a/packages/nql/test/integration/knex/fixtures/base.json b/packages/nql/test/integration/knex/fixtures/base.json index a397185..57eb3c0 100644 --- a/packages/nql/test/integration/knex/fixtures/base.json +++ b/packages/nql/test/integration/knex/fixtures/base.json @@ -5,42 +5,50 @@ "title": "A Whole New World", "featured": false, "image": null, - "status": "published" + "status": "published", + "created_at": "2020-01-05 18:16:49", + "updated_at": "2020-01-10 12:05:08" }, { "id": 2, "title": "The Bare Necessities", "featured": false, "image": "myimage.jpg", - "status": "draft" + "status": "draft", + "created_at": "2020-01-18 08:23:46", + "updated_at": "2020-01-18 12:05:08" }, { "id": 3, "title": "When She Loved Me", "featured": true, "image": null, - "status": "published" + "status": "published", + "created_at": "2022-02-02 10:05:49" }, { "id": 4, "title": "Circle of Life", "featured": true, "image": null, - "status": "published" + "status": "published", + "created_at": "2022-03-01 10:12:23" }, { "id": 5, "title": "Be Our Guest", "featured": true, "image": null, - "status": "published" + "status": "published", + "created_at": "2022-03-02 11:06:49" }, { "id": 6, "title": "He's a Tramp", "featured": true, "image": null, - "status": "published" + "status": "published", + "created_at": "2022-03-02 11:06:50" } ] } diff --git a/packages/nql/test/integration/knex/schema.js b/packages/nql/test/integration/knex/schema.js index 2c8da85..94fc55d 100644 --- a/packages/nql/test/integration/knex/schema.js +++ b/packages/nql/test/integration/knex/schema.js @@ -10,6 +10,7 @@ module.exports.up = function (knex) { table.boolean('featured').defaultsTo(false); table.string('image', 191).nullable(); table.string('status', 191).nullable(); + table.timestamps(true, true); })); }; diff --git a/packages/nql/test/integration/mingo/simple.json b/packages/nql/test/integration/mingo/simple.json index e146d9d..69f9650 100644 --- a/packages/nql/test/integration/mingo/simple.json +++ b/packages/nql/test/integration/mingo/simple.json @@ -8,7 +8,8 @@ "featured": false, "image": null, "status": "published", - "tags": ["photo"] + "tags": ["photo"], + "created_at": "2022-03-02T10:14:23.000Z" }, { "id": 2, @@ -18,7 +19,8 @@ "featured": false, "image": "myimage.jpg", "status": "draft", - "tags": ["video"] + "tags": ["video"], + "created_at": "2022-03-02 10:14:23" }, { "id": 3, diff --git a/packages/nql/test/integration/nql_knex.test.js b/packages/nql/test/integration/nql_knex.test.js index 9dcddf1..d590ac1 100644 --- a/packages/nql/test/integration/nql_knex.test.js +++ b/packages/nql/test/integration/nql_knex.test.js @@ -63,4 +63,50 @@ describe('Integration with Knex', function () { }); }); }); + + describe('Dates', function () { + it('can match based on dates - this is the format that works across MySQL and SQLite3', function () { + const query = nql('created_at:>=\'2022-03-02 11:06:49\''); + + return query + .querySQL(knex('posts')) + .select() + .then((result) => { + result.should.be.an.Array().with.lengthOf(2); + result[0].title.should.eql('Be Our Guest'); + result[1].title.should.eql('He\'s a Tramp'); + }); + }); + + // NOTE: I believe this will work in MySQL but not SQLite3 + it('can match based on dates in iso format ISO', function () { + const query = nql('created_at:>=\'2022-03-02T11:06:49.000Z\''); + + return query + .querySQL(knex('posts')) + .select() + .then((result) => { + result.should.be.an.Array().with.lengthOf(2); + result[0].title.should.eql('Be Our Guest'); + result[1].title.should.eql('He\'s a Tramp'); + }); + }); + + it('can match based on relative dates', function () { + // This test relies on the fact that knex inserts an updated_at of now for all fixtures that are blank + // Only 2 tests have explicit updated_at dates, these should not be returned + const query = nql('updated_at:>now-1d'); + + return query + .querySQL(knex('posts')) + .select() + .then((result) => { + result.should.be.an.Array().with.lengthOf(4); + result[0].title.should.eql('When She Loved Me'); + result[1].title.should.eql('Circle of Life'); + result[2].title.should.eql('Be Our Guest'); + result[3].title.should.eql('He\'s a Tramp'); + }); + }); + }); }); diff --git a/packages/nql/test/integration/nql_mingo.test.js b/packages/nql/test/integration/nql_mingo.test.js index 9a3514d..e62db50 100644 --- a/packages/nql/test/integration/nql_mingo.test.js +++ b/packages/nql/test/integration/nql_mingo.test.js @@ -413,4 +413,46 @@ describe('Integration with Mingo', function () { query.queryJSON(advancedJSON.posts[4]).should.eql(false); }); }); + + describe.only('Dates', function () { + // Dates are a nightmare because SQLite3 and MySQL work differently, and knex doesn't help here + // The date format that goes into a database has to be YYYY-MM-DD HH:mm:ss, but what comes out is normalised to ISO YYYY-MM-DDTHH:mm:ss.000Z + + it('can query JSON by date', function () { + const query = makeQuery('created_at:>=\'2022-03-02 10:14:23\''); + + query.queryJSON({created_at: '2022-03-02T10:14:23.000Z'}).should.eql(true); + query.queryJSON({created_at: '2022-03-02 10:14:23'}).should.eql(true); + query.queryJSON({created_at: '2022-03-02T10:14:24.000Z'}).should.eql(true); + query.queryJSON({created_at: '2022-03-02 10:14:24'}).should.eql(true); + // FAIL query.queryJSON({created_at: '2022-03-02T10:14:22.000Z'}).should.eql(false); + query.queryJSON({created_at: '2022-03-02 10:14:22'}).should.eql(false); + + query.queryJSON({}).should.eql(false); + }); + + it('can query JSON by ISO date', function () { + const query = makeQuery('created_at:>=\'2022-03-02T10:14:23.000Z\''); + + query.queryJSON({created_at: '2022-03-02T10:14:23.000Z'}).should.eql(true); + // FAIL query.queryJSON({created_at: '2022-03-02 10:14:23'}).should.eql(true); + query.queryJSON({created_at: '2022-03-02T10:14:24.000Z'}).should.eql(true); + // FAIL query.queryJSON({created_at: '2022-03-02 10:14:24'}).should.eql(true); + query.queryJSON({created_at: '2022-03-02T10:14:22.000Z'}).should.eql(false); + query.queryJSON({created_at: '2022-03-02 10:14:22'}).should.eql(false); + + query.queryJSON({}).should.eql(false); + }); + + it('can query JSON by rel date', function () { + const query = makeQuery('created_at:>=now-1d'); + + console.log(new Date().toISOString()); + console.log(new Date()); + + query.queryJSON({created_at: new Date().toISOString()}).should.eql(true); + query.queryJSON({created_at: '2022-01-00T00:00:00.000Z'}).should.eql(false); + query.queryJSON({created_at: '2022-01-00 00:00:00'}).should.eql(false); + }); + }); });