node.js - 如何构建一个对象并将其存储为 postgres 上的 hstore 类型
问题描述
我正在尝试在 nodejs 上构建一个对象,然后将其作为 hstore 数据类型存储到 postgres 数据库中。例如在java中你会做类似的事情:
private mapObject = new Map<string, string>();
mapObject(firstString) = secondString;
然后你把它推送到数据库。
我将如何在 nodejs 中创建这样的对象,不确定是否需要 javascript hashmap 对象或者我该怎么做?我正在使用 nodejs 和 pg 库连接到 postgres 数据库
解决方案
也许我会使用sequelizejs(Node.js v4 及更高版本的基于承诺的 ORM)来回答。
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
文档中的代码。
推荐阅读
- google-apps-script - 如何获取活动用户并放置在特定单元格中?
- python - 在这种情况下,“'tuple' 对象没有属性 'first_name' 是什么意思?
- excel - 如何显示用户表单,然后隐藏,然后再次显示而不再次运行用户表单激活代码
- android - 在 Android 9 上打开时停止工作,如何解决?
- c# - 您如何以编程方式在自定义 WPF 面板实现中创建新控件?
- java - 我可以使用 TestInfo 接口从 @BeforeEach 中排除单个测试吗?
- html - 如何在加载时停止 FOUT(无样式文本的闪烁)
- android - Kotlin:在折线图 Mpandroidchart 的 x 轴上设置自定义标签
- google-chrome - 哪些政策会阻止加载未打包的 Chrome 扩展程序?
- angular - 尝试实现 Spinner 时出现异常