我应该如何将LoopBack Framework应用程序连接到预先存在/数据填充的MySQL数据库/数据源?

时间:2023-01-13 22:01:42

This question pertains specifically to LoopBack Node.js Framework from StrongLoop (owned by / backed by IBM). From what I know so far this should be a two step process.

这个问题特别适用于StrongLoop的LoopBack Node.js框架(由IBM拥有/支持)。据我所知,到目前为止,这应该是一个两步过程。

  1. Write a script to discover / create Models from the existing MySQL Schema. Here are the official docs regarding that: https://loopback.io/doc/en/lb3/Discovering-models-from-relational-databases.html
  2. 编写脚本以从现有MySQL模式中发现/创建模型。以下是有关该文件的官方文档:https://loopback.io/doc/en/lb3/Discovering-models-from-relational-databases.html

I think I successfully handled the discovery / model creation piece using the following NPM package: https://www.npmjs.com/package/loopback-discovery (GitHub repo for project can be found here: https://github.com/akera-io/loopback-discovery)

我想我使用以下NPM软件包成功处理了发现/模型创建部分:https://www.npmjs.com/package/loopback-discovery(项目的GitHub repo可以在这里找到:https://github.com/ akera-IO /环回发现)

  1. Use AutoUpdate (via script in loopback-project/server/boot directory) to check to make sure the MySQL DB fits with the Model definition that was created by the Discovery / Creation module. Here is a * topic on the difference between LoopBack's AutoMigrate and AutoUpdate functions: loopback automigrate vs autoupdate
  2. 使用AutoUpdate(通过loopback-project / server / boot目录中的脚本)检查以确保MySQL DB符合Discovery / Creation模块创建的Model定义。这是关于LoopBack的AutoMigrate和AutoUpdate函数之间差异的*主题:loopback automigrate vs autoupdate

From there I think I should be able to hit my API and access the data in the MySQL datasource, but alas...

从那里我想我应该能够访问我的API并访问MySQL数据源中的数据,但唉......

When I go through the above steps, I successfully get ALL of the models from my selected table in the loopback-project/common/models directory (my models happen to be posts from a blog).

当我完成上述步骤时,我成功地从loopback-project / common / models目录中的所选表中获取了所有模型(我的模型碰巧是来自博客的帖子)。

In my loopback-project/server/model-config.json all of the tables / models from my MySQL database exist and I then set my "posts" model to public in order to expose it via API.

在我的loopback-project / server / model-config.json中,存在我MySQL数据库中的所有表/模型,然后我将“posts”模型设置为public,以便通过API公开它。

Since the NPM Module discussed above uses LoopBack and the LoopBack datasources I know that I can connect to my DB and my loopback-project/server/datasource.js file properly configures loopback to talk to my server / mysql instance.

由于上面讨论的NPM模块使用LoopBack和LoopBack数据源,我知道我可以连接到我的数据库,我的loopback-project / server / datasource.js文件正确配置环回以与我的服务器/ mysql实例通信。

When I launch my loopback instance using node . I can hit http://0.0.0.0/explore and I have the proper CRUD options for my 'Posts' model exactly as I would expect.

当我使用node启动我的loopback实例时。我可以点击http://0.0.0.0/explore,我的'Posts'模型有正确的CRUD选项,正如我所期望的那样。

HOWEVER.

然而。

When I attempt to query my DB via GET Posts Api Endpoint I receive the following error that my table does not exist (even though it clearly does in MySQL since my blog is using it AND the previous NPM package was able to read from it:

当我尝试通过GET Posts Api Endpoint查询我的数据库时,我收到以下错误:我的表不存在(即使它在MySQL中显然也是如此,因为我的博客正在使用它并且之前的NPM包能够从中读取:

{
  "error": {
    "statusCode": 500,
    "name": "Error",
    "message": "ER_NO_SUCH_TABLE: Table 'my_mysqlDB.Posts' doesn't exist",
    "code": "ER_NO_SUCH_TABLE",
    "errno": 1146,
    "sqlState": "42S02",
    "index": 0,
    "stack": "Error: ER_NO_SUCH_TABLE: Table 'my_mysqlDB.Posts' doesn't exist\n    at Query.Sequence._packetToError (/app/AvailableTripsLBcode/node_modules/mysql/lib/protocol/sequences/Sequence.js:52:14)\n    at Query.ErrorPacket (/app/AvailableTripsLBcode/node_modules/mysql/lib/protocol/sequences/Query.js:77:18)\n    at Protocol._parsePacket (/app/AvailableTripsLBcode/node_modules/mysql/lib/protocol/Protocol.js:280:23)\n    at Parser.write (/app/AvailableTripsLBcode/node_modules/mysql/lib/protocol/Parser.js:75:12)\n    at Protocol.write (/app/AvailableTripsLBcode/node_modules/mysql/lib/protocol/Protocol.js:39:16)\n    at Socket.<anonymous> (/app/AvailableTripsLBcode/node_modules/mysql/lib/Connection.js:103:28)\n    at emitOne (events.js:96:13)\n    at Socket.emit (events.js:188:7)\n    at readableAddChunk (_stream_readable.js:176:18)\n    at Socket.Readable.push (_stream_readable.js:134:10)\n    at TCP.onread (net.js:551:20)\n    --------------------\n    at Protocol._enqueue (/app/AvailableTripsLBcode/node_modules/mysql/lib/protocol/Protocol.js:141:48)\n    at PoolConnection.query (/app/AvailableTripsLBcode/node_modules/mysql/lib/Connection.js:208:25)\n    at runQuery (/app/AvailableTripsLBcode/node_modules/loopback-connector-mysql/lib/mysql.js:186:16)\n    at executeWithConnection (/app/AvailableTripsLBcode/node_modules/loopback-connector-mysql/lib/mysql.js:228:7)\n    at Ping.onOperationComplete [as _callback] (/app/AvailableTripsLBcode/node_modules/mysql/lib/Pool.js:110:5)\n    at Ping.Sequence.end (/app/AvailableTripsLBcode/node_modules/mysql/lib/protocol/sequences/Sequence.js:86:24)\n    at Ping.Sequence.OkPacket (/app/AvailableTripsLBcode/node_modules/mysql/lib/protocol/sequences/Sequence.js:95:8)\n    at Protocol._parsePacket (/app/AvailableTripsLBcode/node_modules/mysql/lib/protocol/Protocol.js:280:23)\n    at Parser.write (/app/AvailableTripsLBcode/node_modules/mysql/lib/protocol/Parser.js:75:12)\n    at Protocol.write (/app/AvailableTripsLBcode/node_modules/mysql/lib/protocol/Protocol.js:39:16)\n    at Socket.<anonymous> (/app/AvailableTripsLBcode/node_modules/mysql/lib/Connection.js:103:28)\n    at emitOne (events.js:96:13)\n    at Socket.emit (events.js:188:7)\n    at readableAddChunk (_stream_readable.js:176:18)\n    at Socket.Readable.push (_stream_readable.js:134:10)\n    at TCP.onread (net.js:551:20)"
  }
}

I only care about ONE table on my DB (posts) and I only need to read not / write. I am not sure if I need models for all the other models to be in common/models directory, or if I can have ONLY my Posts model which relates to my 'posts' table on the datasource, to that end I have tried both ways with no luck.

我只关心我的数据库(帖子)上的一个表,我只需要读不写/写。我不确定我是否需要所有其他模型的模型在common / models目录中,或者如果我只能拥有与数据源上的'posts'表相关的Posts模型,为此我尝试了两种方式没有运气。

I feel like there is something I missing about how LoopBack handles schema creation based on Models but I haven't had any luck solving it as of yet.

我觉得有些东西我错过了关于LoopBack如何处理基于模型的模式创建,但我还没有任何运气解决它。

Some other notes / details.

其他一些说明/细节。

  1. Re-Creating via AutoMigrate is not an option since it drops the table and re-creates thus losing all data (which is the only reason I need to connect in the first place).
  2. 通过AutoMigrate重新创建不是一个选项,因为它会丢弃表并重新创建,从而丢失所有数据(这是我首先需要连接的唯一原因)。

1 个解决方案

#1


2  

if your model is named "Post", then loopback will be looking for Post table in mysql.

如果你的模型被命名为“Post”,那么loopback将在mysql中查找Post表。

    "name": "Post",
  "options": {
    "idInjection": false,
    "mysql": {
      "schema": "LOOPBACK",
      "table": "post"
    }
  }

add this in the post.json file and it will work

在post.json文件中添加它,它将工作

#1


2  

if your model is named "Post", then loopback will be looking for Post table in mysql.

如果你的模型被命名为“Post”,那么loopback将在mysql中查找Post表。

    "name": "Post",
  "options": {
    "idInjection": false,
    "mysql": {
      "schema": "LOOPBACK",
      "table": "post"
    }
  }

add this in the post.json file and it will work

在post.json文件中添加它,它将工作