11

Bridging JavaScript and Sequelize

 3 years ago
source link: https://blog.feathersjs.com/bridging-javascript-and-sequelize-17e118825314
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Responses (2)

Hi eddy may I suggest you to change your JavaScript logo ? I mean it for you as this is the Java logo, readers like me may think that for you JavaScript and Java are the same thing 😅
And I am sure this is not your case. The more standard one is the yellow squared one, or the shield like logo :)

You have 2 free member-only stories left this month.

Bridging JavaScript and Sequelize

Image for post
Image for post

Sequelize does not support some JavaScript data types. We present one way to use “natural” JavaScript types with Sequelize.

Sequelize does not natively support JavaScript’s Boolean, Date, Object and Array data types. This makes it awkward to write database agnostic code compatible with NoSQL databases. It also makes it awkward to write “natural” JavaScript.

There is no avoiding the translation of some JavaScript data types to a format natural for Sequelize. The question is what’s the most convenient way to do it.

The sequelizeConvert hook

Feathers hooks are middleware which run before or after a database service call, and they are the obvious place to do the translation.

The sequelizeConvert hook, recently added to Feathers common hooks library, automates the conversion. You can call it with

const { sequelizeConvert } = require('feathers-hooks-common');const convert = {
isVerified: 'boolean',
verifyExpires: 'date',
verifyChanges: 'json',
passwordHistory: 'json',
};app.service('users').hooks({
before: { all: [ sequelizeConvert(convert) ] },
after: { all: [ sequelizeConvert(convert) ] },
});app.service('users').create({
isVerified: false, verifyExpires: Date.now() + 60 * 60 * 24 * 2,
verifyChanges: { email: '[email protected]' }, passwordHistory: [],
});

The hook converts:

  • JavaScript Boolean true, false to Sequelize DataTypes.INTEGER 1 and 0.
  • Date types, e.g. Date.now(), to DataTypes.DATE. Dates with a null value are persisted as nullby Sequelize, and returned to JavaScript as null.
  • Object and Array types to DataTypes.STRING using JSON.stringify and JSON.parse.

The hook’s signature is sequelizeConvert(convert, ignores, conversions) where:

  • convert is an object as shown above. The prop names are the field names in the record. The valid values are boolean, date and json.
  • ignores is an optional array of field names to not convert. Having a separate ignore which overrides convert is convenient in some circumstances.
  • conversion is an optional object containing the conversion functions. It may be used to override the default used for each JavaScript type. The default functions are:
constdefaultConversions = {
boolean: {
sql: boolean => boolean ? 1 : 0,
js: numb => !!numb,
},
date: {
sql: dateNow => dateNow,
js: sqlDate => new Date(sqlDate).valueOf() || null,
},
json: {
sql: obj => JSON.stringify(obj),
js: str => JSON.parse(str),
}
};

The conversions occur automatically once you define hooks, and you no longer need to pay attention to them.

In conclusion

This article is part of a series of articles on using Sequelize in Feathers. Subscribe to The Feathers Flightpath publication to be informed of the coming articles.

As always, feel free to join Feathers Slack to join the discussion or just lurk around.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK