环回 + 用户模型迁移到 Postgres 数据库

loopback + user model migrate into postgres database

本文关键字:Postgres 数据库 迁移 模型 用户 环回      更新时间:2023-09-26

我是环回的新手,

我尝试使用用户模型,如注册,登录。我关注了这个网址 - https://github.com/strongloop/loopback-example-user-management

它工作得很好,但它在没有 postgres 数据库的情况下工作。

我添加了代码datasoruces.json。

{
    "db": {
        "name": "db",
        "connector": "memory"
    },
    "mydata": {
        "postgres": {
            "host": "localhost",
            "port": "5432",
            "database": "myapps_login",
            "username": "postgres",
            "password": "password",
            "name": "postgres",
            "debug": true,
            "connector": "postgresql"
        }
    }

如何使用数据库访问用户模型?

有两个问题。首先是你没有正确定义数据源。不应具有嵌套的postgres对象:

"mydata": {
    "name": "mydata",
    "host": "localhost",
    "port": "5432",
    "database": "myapps_login",
    "username": "postgres",
    "password": "password",
    "debug": true,
    "connector": "postgresql"
}

接下来,您需要将User模型附加到新数据源。编辑server/model-config.json文件:

{
  // ...
  "User": {
    "dataSource": "mydata",
    "public": true
  },
  // ...
}

最后,确保您已经安装了 postgres 连接器依赖项(从命令行,在项目目录中):

npm install --save loopback-connector-postgresql