将LoopBack应用程序连接到现有的MySQL数据库 [英] Connect LoopBack app to existing MySQL database

查看:181
本文介绍了将LoopBack应用程序连接到现有的MySQL数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个本地MySQL数据库mydb.它包含一个名为person的表,其中包含字段id(主键,整数自动递增)和name(文本).该表包含我不想删除的记录.

I have a local MySQL database mydb. It contains a table called person, with fields id (primary key, integer auto-increment) and name (text). The table contains records that I do not want to delete.

我想要一个LoopBack应用程序,该应用程序通过数据源myds连接到mydb.该应用程序应具有模型Person(扩展了PersistedModel). Person模型上的CRUD操作应反映在mydbpeople表上.

I want a LoopBack app that connects to mydb through a data source myds. The app should have a model Person (which extends PersistedModel). CRUD operations on the Person model should be reflected on the people table of mydb.

如何构建应用程序?

推荐答案

使用应用程序生成器生成一个空的应用程序.

Generate an empty application with the application generator.

>lb
? What's the name of your application? myapp 
? Enter name of the directory to contain the project: myapp
? Which version of LoopBack would you like to use? 3.x (current)
? What kind of application do you have in mind? empty-server

添加新的数据源:

/* /server/datasources.json */
  "myds": {
    "host": "localhost",
    "port": 3306,
    "url": "",
    "database": "mydb",
    "password": "password",
    "name": "myds",
    "user": "root",
    "connector": "mysql"
  }

安装loopback-connector-mysql:

Install loopback-connector-mysql:

> npm install loopback-connector-mysql --save

创建模型

/* /common/models/Person.json */
{
  "name": "Person",
  "plural": "People",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "id": {
      "type": "number",
      "required": true
    },
    "name": {
      "type": "string"
    }
  },
  "validations": [],
  "relations": {},
  "acls": [],
  "methods": {}
}

然后将模型附加到数据源.

Then attach the model to the data source.

/* /server/model-config.json */
{
  // ...
  "Person": {
    "dataSource": "myds",
    "public": true
  }
  //...
}

该应用程序已完成.要运行该应用,

The app is complete. To run the app,

> node .

然后在浏览器中导航到localhost:3000/explorer.单击Person模型,然后单击路由GET /People,然后单击尝试一下!"该应用程序应从数据库返回person表中的所有行.其他CRUD操作也应正常运行.

Then navigate to localhost:3000/explorer in a browser. Click on the Person model, then click on the route GET /People, then click "try it out!" The app should return all rows in the person table from the database. The other CRUD operations should also function properly.

请注意,命名非常重要.当我们将名为Person的模型连接到数据源myds时,该数据源本身也连接到数据库mydb,LoopBack将在mydb中查找一个名为Person的表(这可能与表名中的区分大小写进行交互)您的MySQL安装),其确切名称为Person的属性.

Note that the naming is very important. When we connect a model named Person and to the datasource myds, which itself connects to the database mydb, LoopBack looks for a table called Person in mydb (this may interact with case-sensitivty of table names in your MySQL installation), with exactly the properties of Person as its field names.

这篇关于将LoopBack应用程序连接到现有的MySQL数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆