From 2dbad3fcdeace9656cf919fa29ebd7112badbc22 Mon Sep 17 00:00:00 2001 From: Alexandre Bodin Date: Tue, 3 Dec 2019 09:50:43 +0100 Subject: [PATCH 01/11] Init date type splitting --- .../lib/buildDatabaseSchema.js | 9 +++-- .../lib/mount-models.js | 33 ++++++++++++++----- .../strapi-connector-bookshelf/package.json | 2 +- packages/strapi-generate-model/lib/before.js | 4 ++- packages/strapi/bin/strapi.js | 1 + yarn.lock | 7 +++- 6 files changed, 41 insertions(+), 15 deletions(-) diff --git a/packages/strapi-connector-bookshelf/lib/buildDatabaseSchema.js b/packages/strapi-connector-bookshelf/lib/buildDatabaseSchema.js index 793a8f2a7f..afdb743de8 100644 --- a/packages/strapi-connector-bookshelf/lib/buildDatabaseSchema.js +++ b/packages/strapi-connector-bookshelf/lib/buildDatabaseSchema.js @@ -412,20 +412,23 @@ const getType = ({ definition, attribute, name, tableExists = false }) => { return 'decimal(10,2)'; // TODO: split time types as they should be different case 'date': + return 'date'; case 'time': - case 'datetime': + return 'time'; + case 'datetime': { if (client === 'pg') { return 'timestamp with time zone'; } - return 'timestamp'; - case 'timestamp': + } + case 'timestamp': { if (client === 'pg') { return 'timestamp with time zone'; } else if (client === 'sqlite3' && tableExists) { return 'timestamp DEFAULT NULL'; } return 'timestamp DEFAULT CURRENT_TIMESTAMP'; + } case 'timestampUpdate': switch (client) { case 'pg': diff --git a/packages/strapi-connector-bookshelf/lib/mount-models.js b/packages/strapi-connector-bookshelf/lib/mount-models.js index f56ebe7fb4..aff2e96ec1 100644 --- a/packages/strapi-connector-bookshelf/lib/mount-models.js +++ b/packages/strapi-connector-bookshelf/lib/mount-models.js @@ -611,10 +611,8 @@ module.exports = ({ models, target, plugin = false }, ctx) => { : Promise.resolve(); }); - //eslint-disable-next-line - this.on('saving', (instance, attrs, options) => { + this.on('saving', instance => { instance.attributes = mapper(instance.attributes); - attrs = mapper(attrs); return _.isFunction(target[model.toLowerCase()]['beforeSave']) ? target[model.toLowerCase()]['beforeSave'] @@ -664,7 +662,10 @@ module.exports = ({ models, target, plugin = false }, ctx) => { } if (attr.type === 'date' && definition.client === 'sqlite3') { - attributes[key] = dateFns.parse(attributes[key]); + const cast = dateFns.parseISO(attributes[key]); + attributes[key] = dateFns.isValid(cast) + ? dateFns.format(cast, 'yyyy-MM-dd') + : null; } if ( @@ -756,13 +757,27 @@ const castValueFromType = (type, value /* definition */) => { switch (type) { case 'json': return JSON.stringify(value); - // TODO: handle real date format 1970-01-01 - // TODO: handle real time format 12:00:00 - case 'time': + case 'time': { + try { + dateFns.parse(value, 'HH:mm:ss', new Date()); + } catch (error) { + throw new Error(`Invalid time format, expected a time HH:mm:ss`); + } + + return value; + } + case 'date': { + try { + dateFns.parse(value, 'yyyy-MM-dd', new Date()); + } catch (error) { + throw new Error(`Invalid date format, expected a date YYYY-MM-DD`); + } + + return value; + } case 'timestamp': - case 'date': case 'datetime': { - const date = dateFns.parse(value); + const date = dateFns.parseISO(value); if (dateFns.isValid(date)) return date; date.setTime(value); diff --git a/packages/strapi-connector-bookshelf/package.json b/packages/strapi-connector-bookshelf/package.json index 7276174bac..665256b64c 100644 --- a/packages/strapi-connector-bookshelf/package.json +++ b/packages/strapi-connector-bookshelf/package.json @@ -17,7 +17,7 @@ "main": "./lib", "dependencies": { "bookshelf": "^1.0.1", - "date-fns": "^1.30.1", + "date-fns": "^2.8.1", "inquirer": "^6.3.1", "lodash": "^4.17.11", "pluralize": "^7.0.0", diff --git a/packages/strapi-generate-model/lib/before.js b/packages/strapi-generate-model/lib/before.js index ab7e215ed8..e24d0c1c5c 100644 --- a/packages/strapi-generate-model/lib/before.js +++ b/packages/strapi-generate-model/lib/before.js @@ -138,6 +138,7 @@ module.exports = (scope, cb) => { // Get default connection try { scope.connection = + scope.args.connection || JSON.parse( fs.readFileSync( path.resolve( @@ -148,7 +149,8 @@ module.exports = (scope, cb) => { 'database.json' ) ) - ).defaultConnection || ''; + ).defaultConnection || + ''; } catch (err) { return cb.invalid(err); } diff --git a/packages/strapi/bin/strapi.js b/packages/strapi/bin/strapi.js index 7db7e17bf9..e106b1180b 100755 --- a/packages/strapi/bin/strapi.js +++ b/packages/strapi/bin/strapi.js @@ -144,6 +144,7 @@ program .option('-a, --api ', 'API name to generate a sub API') .option('-p, --plugin ', 'plugin name') .option('-t, --tpl