From 67539a143b4bc22beb05e25e64c61ad214f7cda5 Mon Sep 17 00:00:00 2001 From: Daniel Lockyer Date: Wed, 16 Jun 2021 14:16:21 +0100 Subject: [PATCH] Extracted Bookshelf data manipulation code into plugins no issue - this commit extracts all code relating to manipulating/fixing data from the Base model into its own plugin --- core/server/models/base/bookshelf.js | 2 + core/server/models/base/data-manipulation.js | 87 ++++++++++++++++++++ core/server/models/base/index.js | 76 ----------------- 3 files changed, 89 insertions(+), 76 deletions(-) create mode 100644 core/server/models/base/data-manipulation.js diff --git a/core/server/models/base/bookshelf.js b/core/server/models/base/bookshelf.js index 8e6ba95eac..9556d5b550 100644 --- a/core/server/models/base/bookshelf.js +++ b/core/server/models/base/bookshelf.js @@ -60,6 +60,8 @@ ghostBookshelf.plugin(require('./filtered-collection')); ghostBookshelf.plugin(require('./user-type')); +ghostBookshelf.plugin(require('./data-manipulation')); + // Manages nested updates (relationships) ghostBookshelf.plugin('bookshelf-relations', { allowedOptions: ['context', 'importing', 'migrating'], diff --git a/core/server/models/base/data-manipulation.js b/core/server/models/base/data-manipulation.js new file mode 100644 index 0000000000..14f27e2ee7 --- /dev/null +++ b/core/server/models/base/data-manipulation.js @@ -0,0 +1,87 @@ +const _ = require('lodash'); +const moment = require('moment'); + +const schema = require('../../data/schema'); + +/** + * @param {import('bookshelf')} Bookshelf + */ +module.exports = function (Bookshelf) { + Bookshelf.Model = Bookshelf.Model.extend({ + getNullableStringProperties() { + const table = schema.tables[this.tableName]; + return Object.keys(table).filter(column => table[column].nullable); + }, + + setEmptyValuesToNull: function setEmptyValuesToNull() { + const nullableStringProps = this.getNullableStringProperties(); + return nullableStringProps.forEach((prop) => { + if (this.get(prop) === '') { + this.set(prop, null); + } + }); + }, + + /** + * before we insert dates into the database, we have to normalize + * date format is now in each db the same + */ + fixDatesWhenSave: function fixDates(attrs) { + const self = this; + + _.each(attrs, function each(value, key) { + if (value !== null + && Object.prototype.hasOwnProperty.call(schema.tables[self.tableName], key) + && schema.tables[self.tableName][key].type === 'dateTime') { + attrs[key] = moment(value).format('YYYY-MM-DD HH:mm:ss'); + } + }); + + return attrs; + }, + + /** + * all supported databases (sqlite, mysql) return different values + * + * sqlite: + * - knex returns a UTC String (2018-04-12 20:50:35) + * mysql: + * - knex wraps the UTC value into a local JS Date + */ + fixDatesWhenFetch: function fixDates(attrs) { + const self = this; + let dateMoment; + + _.each(attrs, function each(value, key) { + if (value !== null + && Object.prototype.hasOwnProperty.call(schema.tables[self.tableName], key) + && schema.tables[self.tableName][key].type === 'dateTime') { + dateMoment = moment(value); + + // CASE: You are somehow able to store e.g. 0000-00-00 00:00:00 + // Protect the code base and return the current date time. + if (dateMoment.isValid()) { + attrs[key] = dateMoment.startOf('seconds').toDate(); + } else { + attrs[key] = moment().startOf('seconds').toDate(); + } + } + }); + + return attrs; + }, + + // Convert integers to real booleans + fixBools: function fixBools(attrs) { + const self = this; + _.each(attrs, function each(value, key) { + if (Object.prototype.hasOwnProperty.call(schema.tables[self.tableName], key) + && schema.tables[self.tableName][key].type === 'bool') { + attrs[key] = value ? true : false; + } + }); + + return attrs; + } + }); +}; diff --git a/core/server/models/base/index.js b/core/server/models/base/index.js index 8a2dec5c0d..8e02c68856 100644 --- a/core/server/models/base/index.js +++ b/core/server/models/base/index.js @@ -77,82 +77,6 @@ ghostBookshelf.Model = ghostBookshelf.Model.extend({ return parentSync; }, - /** - * before we insert dates into the database, we have to normalize - * date format is now in each db the same - */ - fixDatesWhenSave: function fixDates(attrs) { - const self = this; - - _.each(attrs, function each(value, key) { - if (value !== null - && Object.prototype.hasOwnProperty.call(schema.tables[self.tableName], key) - && schema.tables[self.tableName][key].type === 'dateTime') { - attrs[key] = moment(value).format('YYYY-MM-DD HH:mm:ss'); - } - }); - - return attrs; - }, - - /** - * all supported databases (sqlite, mysql) return different values - * - * sqlite: - * - knex returns a UTC String (2018-04-12 20:50:35) - * mysql: - * - knex wraps the UTC value into a local JS Date - */ - fixDatesWhenFetch: function fixDates(attrs) { - const self = this; - let dateMoment; - - _.each(attrs, function each(value, key) { - if (value !== null - && Object.prototype.hasOwnProperty.call(schema.tables[self.tableName], key) - && schema.tables[self.tableName][key].type === 'dateTime') { - dateMoment = moment(value); - - // CASE: You are somehow able to store e.g. 0000-00-00 00:00:00 - // Protect the code base and return the current date time. - if (dateMoment.isValid()) { - attrs[key] = dateMoment.startOf('seconds').toDate(); - } else { - attrs[key] = moment().startOf('seconds').toDate(); - } - } - }); - - return attrs; - }, - - // Convert integers to real booleans - fixBools: function fixBools(attrs) { - const self = this; - _.each(attrs, function each(value, key) { - if (Object.prototype.hasOwnProperty.call(schema.tables[self.tableName], key) - && schema.tables[self.tableName][key].type === 'bool') { - attrs[key] = value ? true : false; - } - }); - - return attrs; - }, - - getNullableStringProperties() { - const table = schema.tables[this.tableName]; - return Object.keys(table).filter(column => table[column].nullable); - }, - - setEmptyValuesToNull: function setEmptyValuesToNull() { - const nullableStringProps = this.getNullableStringProperties(); - return nullableStringProps.forEach((prop) => { - if (this.get(prop) === '') { - this.set(prop, null); - } - }); - }, - // format date before writing to DB, bools work format: function format(attrs) { return this.fixDatesWhenSave(attrs);