You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// models/objstore.js'use strict';module.exports=(sequelize,DataTypes)=>{classObjStoreextendsModel{/** * Helper method for defining associations. * This method is not a part of Sequelize lifecycle. * The `models/index` file will call this method automatically. */staticassociate(models){// define association here}}ObjStore.init({id: {type: DataTypes.STRING,primaryKey: true,unique: true},attrs: {type: DataTypes.TEXT,get: function(){varstr=this.getDataValue('attrs')||'{}';returnJSON.parse(str);},set: function(val){this.setDataValue('attrs',JSON.stringify(val));}}},{
sequelize,modelName: 'ObjStore',timestamps: false});returnObjStore;};
// lib/store.js'use strict';vardb=require('./models');module.exports={set: asyncfunction(id,attrs){var[record,_]=awaitdb.ObjStore.findOrCreate({where: { id }});record.attrs=attrs;returnrecord.save();},get: asyncfunction(id){returndb.ObjStore.findOne({where: { id }}).then(function(record){if(!record){returnfalse;}returnrecord.get({plain: true}).attrs;});}};
The text was updated successfully, but these errors were encountered:
@coolaj86 Can you take a look at this? The flaw I see is that this may not update the attrs if a single attribute changes. Maybe I should make use of Object.assign. Even if that situation doesn't come up, it might be good to future proof it. Let me know if you have any feedback.
I'm documenting a way to use Sequelize for
store
.The text was updated successfully, but these errors were encountered: