如何构建一个对象并将其作为hstore类型存储在postgres上
我正在尝试在nodejs上构建一个对象,然后将其作为hstore数据类型存储到postgres数据库中。例如在java中你会做类似的事情:
private mapObject = new Map<string, string>();
mapObject(firstString) = secondString;
然后你把它推到数据库。
我如何在nodejs中创建这样的对象,不确定是否需要javascript hashmap对象或者我该怎么做?我使用nodejs和pg库连接到postgres数据库
回答如下:也许我会回答使用sequelizejs(基于promise的ORM for Node.js v4及更高版本)。
const User = sequelize.define('user', {
firstName: {
type: Sequelize.STRING
},
lastName: {
type: Sequelize.STRING
}
});
// force: true will drop the table if it already exists
User.sync({force: true}).then(() => {
// Table created using object format
return User.create({
firstName: 'John',
lastName: 'Hancock'
});
});
//optional to use anything you want
这是来自sequelizejs
文档的代码。