Skip to content

Commit

Permalink
Removed lodash from data-manipulation and sanitize model plugins
Browse files Browse the repository at this point in the history
- only some small micro-optimizations but we can remove lodash from a
  sanitize plugin method (needs an empty object fallback because Object.keys is
  less forgiving than _.keys and there are some tests which don't fully
  initialize the model)
- also converts the data-manipulation plugins into for-loops because
  there are more native than using lodash, and it means we can remove
  lodash from this file too
- lodash is a bit of a perf burn on extreme hotpaths, so it's good to
  clean it up where it's not needed
- adds a bit of JSDoc to help with editor autocomplete
  • Loading branch information
daniellockyer committed Jan 30, 2025
1 parent 6d7c711 commit 29ee00f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
35 changes: 21 additions & 14 deletions ghost/core/core/server/models/base/plugins/data-manipulation.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const _ = require('lodash');
const moment = require('moment');

const schema = require('../../../data/schema');
Expand All @@ -25,18 +24,18 @@ module.exports = function (Bookshelf) {
/**
* before we insert dates into the database, we have to normalize
* date format is now in each db the same
*
* @param {object} attrs - attributes to convert
* @returns {object} attrs - converted attributes
*/
fixDatesWhenSave: function fixDatesWhenSave(attrs) {
const self = this;
const tableDef = schema.tables[this.tableName];

_.each(attrs, function each(value, key) {
if (value !== null
&& Object.prototype.hasOwnProperty.call(schema.tables, self.tableName)
&& 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');
for (const key in attrs) {
if (attrs[key] && tableDef?.[key]?.type === 'dateTime') {
attrs[key] = moment(attrs[key]).format('YYYY-MM-DD HH:mm:ss');
}
});
}

return attrs;
},
Expand All @@ -48,11 +47,14 @@ module.exports = function (Bookshelf) {
* - knex returns a UTC String (2018-04-12 20:50:35)
* mysql:
* - knex wraps the UTC value into a local JS Date
*
* @param {object} attrs - attributes to convert
* @returns {object} attrs - converted attributes
*/
fixDatesWhenFetch: function fixDatesWhenFetch(attrs) {
const tableDef = schema.tables[this.tableName];

Object.keys(attrs).forEach((key) => {
for (const key in attrs) {
if (attrs[key] && tableDef?.[key]?.type === 'dateTime') {
const dateMoment = moment(attrs[key]);

Expand All @@ -64,20 +66,25 @@ module.exports = function (Bookshelf) {
attrs[key] = moment().startOf('seconds').toDate();
}
}
});
}

return attrs;
},

// Convert integers to real booleans
/**
* Convert integers to real booleans
*
* @param {object} attrs - attributes to convert
* @returns {object} attrs - converted attributes
*/
fixBools: function fixBools(attrs) {
const tableDef = schema.tables[this.tableName];

Object.keys(attrs).forEach((key) => {
for (const key in attrs) {
if (tableDef?.[key]?.type === 'boolean') {
attrs[key] = !!attrs[key];
}
});
}

return attrs;
}
Expand Down
2 changes: 1 addition & 1 deletion ghost/core/core/server/models/base/plugins/sanitize.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module.exports = function (Bookshelf) {
Bookshelf.Model = Bookshelf.Model.extend({
// Ghost option handling - get permitted attributes from server/data/schema.js, where the DB schema is defined
permittedAttributes: function permittedAttributes() {
return _.keys(schema.tables[this.tableName])
return Object.keys(schema.tables[this.tableName] || {})
.filter(key => key.indexOf('@@') === -1);
}
}, {
Expand Down

0 comments on commit 29ee00f

Please sign in to comment.