如何使用 PostgreSQL 保存用户

How to save users using PostgreSQL

我跟进了 LoopBack JWT tutorial,但它没有提到如何在真实数据源(例如 RDBM 数据源)中存储凭据

为此,我只是用 connector: 'postgresql' 创建了一个 datasource,但它不起作用,因为我总是收到此错误消息!

Request POST /signup failed with status code 500. error: relation "public.user" does not exist
    at Parser.parseErrorMessage (/home/laptop/LoopBack/multitenancy/node_modules/pg-protocol/src/parser.ts:357:11)
    at Parser.handlePacket (/home/laptop/LoopBack/multitenancy/node_modules/pg-protocol/src/parser.ts:186:21)
    at Parser.parse (/home/laptop/LoopBack/multitenancy/node_modules/pg-protocol/src/parser.ts:101:30)
    at Socket.<anonymous> (/home/laptop/LoopBack/multitenancy/node_modules/pg-protocol/src/index.ts:7:48)
    at Socket.emit (events.js:315:20)
    at addChunk (internal/streams/readable.js:309:12)
    at readableAddChunk (internal/streams/readable.js:284:9)
    at Socket.Readable.push (internal/streams/readable.js:223:10)
    at TCP.onStreamRead (internal/stream_base_commons.js:188:23)

您可以找到完整的源代码here

BTW,当我运行npm run migrate时,我希望生成Usertable,因为user.controller.ts包含 User class 的扩展,但它没有!

能否指导我如何在 RDBM 数据源中存储凭据。

为了历史记录;此问题已由 Diana Lau 解决如下:

  1. 创建 User & UserCredentials 以集成 JWT
$ lb4 model User
? Please select the model base class Entity (A persisted model with an ID)
? Allow additional (free-form) properties? No
Model User will be created in src/models/user.model.ts

Let's add a property to User
Enter an empty property name when done

? Enter the property name:
   create src/models/user.model.ts
   update src/models/index.ts

Model User was/were created in src/models
$ lb4 model UserCredentials
? Please select the model base class Entity (A persisted model with an ID)
? Allow additional (free-form) properties? No
Model UserCredentials will be created in src/models/user-credentials.model.ts

Let's add a property to UserCredentials
Enter an empty property name when done

? Enter the property name:
   create src/models/user-credentials.model.ts
   update src/models/index.ts

Model UserCredentials was/were created in src/models
  1. models/user.model.tsmodels/user-credentials.model.ts 替换为 user.model.ts & user-credentials.model.ts

  2. UserUserCredentials

    创建存储库
$ lb4 repository
? Please select the datasource PostgresqlDatasource
? Select the model(s) you want to generate a repository for UserCredentials, User
? Please select the repository base class DefaultCrudRepository (Juggler bridge)
? Please enter the name of the ID property for UserCredentials: id
? Please enter the name of the ID property for User: id
   create src/repositories/user-credentials.repository.ts
   create src/repositories/user.repository.ts
   update src/repositories/index.ts
   update src/repositories/index.ts

Repositories UserCredentialsRepository, UserRepository was/were created in src/repositories
  1. 运行 npm run build

  2. 修改 datasources/postgresql.datasource.ts 以满足正确的 PostgreSQL 配置,然后根据您的配置创建一个新的 PostgreSQL 数据库。

  3. 运行 npm run migrate

  4. 删除以下文件:

  • models/user.model.ts
  • models/user-credentials.model.ts
  • repositories/user.repository.ts
  • repositories/user-credentials.repository.ts
  1. 通过删除以下行来修改 models/index.ts
export * from './user.model';
export * from './user-credentials.model';
  1. 通过删除以下行修改 repositories/index.ts
export * from './user-credentials.repository';
export * from './user.repository';

注意:我在 this issue.

中建议了一个新功能请求,以便更好地集成 JWT