首页 > 解决方案 > 从 Feathers nedb 迁移到 Postgres 需要对代码进行哪些更改?

问题描述

我使用了 feathers-nedb 库并在 Node.js 中编写了一个服务器代码。现在我需要转移到 Postgres DB。我已经在 feathers-nedb 中编写了模型和数据插入查询,所以有没有办法我不会弄乱结构,而是连接到 Postgres 并运行代码。

标签: node.jspostgresqlfeathersjs

解决方案


有一种方法。您可以使用 feathers-knex 库。只需将 nedb 模型更改为 feathers-knex 并使用 knex Postgres 连接字符串创建模型。

const dbPath = app.get('nedb');
const Model = new NeDB({
    filename: path.join(dbPath, 'test.db'),
    autoload: true
});

const Model = new knex({
    client: 'pg',
    connection: "postgres://postgres:password@localhost:5432/test",
    searchPath: ['knex', 'public'],
});

这是模型端唯一需要的代码更改。在服务端,使用feathers-knex 代替feathers-nedb。


推荐阅读