Created
March 4, 2018 19:30
-
-
Save ricardograca/bc26ee92baeeee3f792a5d90050608cc to your computer and use it in GitHub Desktop.
MySQL rounding dates with Knex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var assert = require('assert') | |
var knex = require('knex') | |
var mysql = knex({ | |
client: 'mysql', | |
connection: { | |
database: 'bookshelf_test', | |
user: 'root', | |
encoding: 'utf8' | |
} | |
}) | |
var sqlite = knex({ | |
client: 'sqlite3', | |
connection: {filename: ':memory:'}, | |
useNullAsDefault: true | |
}) | |
var db = mysql // Change this to test another client | |
before(function() { | |
return db.schema.dropTableIfExists('cars').then(function() { | |
return db.schema.createTable('cars', function(table) { | |
table.increments() | |
table.string('name', 10) | |
table.timestamps() | |
}) | |
}) | |
}) | |
after(function() { | |
return db.schema.dropTableIfExists('cars').finally(function() { | |
return db.destroy() | |
}) | |
}) | |
describe('hasTimestamps', function() { | |
it('maintains a consistent date value between saving and fetching rows', function() { | |
var createdAt = new Date() | |
var updatedAt = createdAt | |
return db('cars').insert({name: 'car', created_at: createdAt}).then(function() { | |
return db('cars').where({id: 1}) | |
}).then(function(car) { | |
if (db.client.dialect === 'sqlite3') car[0].created_at = new Date(car[0].created_at) | |
assert.equal(car[0].created_at.toJSON(), createdAt.toJSON()) | |
}) | |
}) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "knex_timestamps", | |
"version": "0.0.1", | |
"description": "Test timestamps", | |
"main": "index.js", | |
"scripts": { | |
"test": "mocha index.js" | |
}, | |
"dependencies": { | |
"knex": "0.14.4", | |
"mysql": "*", | |
"mocha": "*", | |
"sqlite3": "*" | |
}, | |
"author": "Ricardo Graça <[email protected]>", | |
"license": "ISC" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment