首页 > 解决方案 > 如何构建一个对象并将其存储为 postgres 上的 hstore 类型

问题描述

我正在尝试在 nodejs 上构建一个对象,然后将其作为 hstore 数据类型存储到 postgres 数据库中。例如在java中你会做类似的事情:

private mapObject = new Map<string, string>();
mapObject(firstString) = secondString;

然后你把它推送到数据库。

我将如何在 nodejs 中创建这样的对象,不确定是否需要 javascript hashmap 对象或者我该怎么做?我正在使用 nodejs 和 pg 库连接到 postgres 数据库

标签: node.jspostgresqlhashmappg

解决方案


也许我会使用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文档中的代码。


推荐阅读