简体   繁体   English

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

[英]How should I connect a LoopBack Framework app to a Pre-Existing / Data Filled MySQL Database / Data Source?

This question pertains specifically to LoopBack Node.js Framework from StrongLoop (owned by / backed by IBM). 此问题专门与StrongLoop(IBM拥有/支持)的LoopBack Node.js框架有关。 From what I know so far this should be a two step process. 据我所知,这应该是一个两步过程。

  1. Write a script to discover / create Models from the existing MySQL Schema. 编写脚本以从现有的MySQL模式中发现/创建模型。 Here are the official docs regarding that: https://loopback.io/doc/en/lb3/Discovering-models-from-relational-databases.html 这是与此有关的官方文档: 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存储库: https : //github.com/ akera-io / loopback-discovery

  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. 使用自动更新(通过loopback-project / server / boot目录中的脚本)检查以确保MySQL DB符合由“发现/创建”模块创建的模型定义。 Here is a StackOverflow topic on the difference between LoopBack's AutoMigrate and AutoUpdate functions: loopback automigrate vs autoupdate 这是关于LoopBack的AutoMigrate和AutoUpdate函数之间的区别的StackOverflow主题: Loopback automigrate与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数据源中的数据,但是a ...

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文件正确配置了Loopback以与服务器/ mysql实例进行通信。

When I launch my loopback instance using node . 当我使用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. 我可以按http://0.0.0.0/explore进行操作,并且与我期望的完全一样,我的“帖子”模型具有正确的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如何处理基于Models的架构创建方面缺少一些东西,但是到目前为止我还没有运气解决它。

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). 通过AutoMigrate重新创建不是一种选择,因为它会删除表并重新创建,从而丢失所有数据(这是我首先需要连接的唯一原因)。

if your model is named "Post" , then loopback will be looking for Post table in mysql . 如果您的模型名为"Post" ,则回送将在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文件中,它将起作用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM