2016-10-31 21:02:14 +00:00
# loopback-connector-mysql
2012-12-14 14:01:44 +00:00
2020-03-16 14:09:17 +00:00
[MySQL ](https://www.mysql.com/ ) is a popular open-source relational database management system (RDBMS). The `loopback-connector-mysql` module provides the MySQL connector module for the LoopBack framework.
2014-01-10 00:40:11 +00:00
2017-01-12 00:06:55 +00:00
< div class = "gh-only" > See also < a href = "http://loopback.io/doc/en/lb3/MySQL-connector.html" > LoopBack MySQL Connector< / a > in LoopBack documentation.
< br / > < br / >
< b > NOTE< / b > : The MySQL connector requires MySQL 5.0+.
2017-01-11 00:13:45 +00:00
< / div >
2012-12-14 14:01:44 +00:00
2013-10-28 22:12:20 +00:00
## Installation
2017-01-11 00:13:45 +00:00
In your application root directory, enter this command to install the connector:
```sh
2013-10-28 22:12:20 +00:00
npm install loopback-connector-mysql --save
2017-01-11 00:13:45 +00:00
```
This installs the module from npm and adds it as a dependency to the application's `package.json` file.
If you create a MySQL data source using the data source generator as described below, you don't have to do this, since the generator will run `npm install` for you.
## Creating a MySQL data source
Use the [Data source generator ](http://loopback.io/doc/en/lb3/Data-source-generator.html ) to add a MySQL data source to your application.
The generator will prompt for the database server hostname, port, and other settings
2020-03-16 14:09:17 +00:00
required to connect to a MySQL database. It will also run the `npm install` command above for you.
2017-01-11 00:13:45 +00:00
The entry in the application's `/server/datasources.json` will look like this:
```javascript
"mydb": {
"name": "mydb",
"connector": "mysql",
"host": "myserver",
"port": 3306,
"database": "mydb",
"password": "mypassword",
"user": "admin"
}
```
Edit `datasources.json` to add any other additional properties that you require.
### Properties
< table >
< thead >
< tr >
< th width = "150" > Property< / th >
< th width = "80" > Type< / th >
< th > Description< / th >
< / tr >
< / thead >
< tbody >
< tr >
< td > collation< / td >
< td > String< / td >
< td > Determines the charset for the connection. Default is utf8_general_ci.< / td >
< / tr >
< tr >
< td > connector< / td >
< td > String< / td >
< td > Connector name, either “loopback-connector-mysql” or “mysql”.< / td >
< / tr >
< tr >
< td > connectionLimit< / td >
< td > Number< / td >
< td > The maximum number of connections to create at once. Default is 10.< / td >
< / tr >
< tr >
< td > database< / td >
< td > String< / td >
< td > Database name< / td >
< / tr >
< tr >
< td > debug< / td >
< td > Boolean< / td >
< td > If true, turn on verbose mode to debug database queries and lifecycle.< / td >
< / tr >
< tr >
< td > host< / td >
< td > String< / td >
< td > Database host name< / td >
< / tr >
< tr >
< td > password< / td >
< td > String< / td >
< td > Password to connect to database< / td >
< / tr >
< tr >
< td > port< / td >
< td > Number< / td >
< td > Database TCP port< / td >
< / tr >
< tr >
< td > socketPath< / td >
< td > String< / td >
< td > The path to a unix domain socket to connect to. When used host and port are ignored.< / td >
< / tr >
< tr >
< td > supportBigNumbers< / td >
< td > Boolean< / td >
< td > Enable this option to deal with big numbers (BIGINT and DECIMAL columns) in the database. Default is false.< / td >
< / tr >
< tr >
2017-04-24 20:19:33 +00:00
< td > timeZone< / td >
2017-01-11 00:13:45 +00:00
< td > String< / td >
< td > The timezone used to store local dates. Default is ‘ local’ .< / td >
< / tr >
< tr >
< td > url< / td >
< td > String< / td >
< td > Connection URL of form < code > mysql://user:password@host/db< / code > . Overrides other connection settings.< / td >
< / tr >
< tr >
< td > username< / td >
< td > String< / td >
< td > Username to connect to database< / td >
< / tr >
< / tbody >
< / table >
**NOTE**: In addition to these properties, you can use additional parameters supported by [`node-mysql` ](https://github.com/felixge/node-mysql ).
## Type mappings
2013-10-28 22:12:20 +00:00
2017-01-11 00:13:45 +00:00
See [LoopBack types ](http://loopback.io/doc/en/lb3/LoopBack-types.html ) for details on LoopBack's data types.
2012-12-14 14:01:44 +00:00
2017-01-11 00:13:45 +00:00
### LoopBack to MySQL types
2012-12-14 14:01:44 +00:00
2017-01-11 00:13:45 +00:00
< table >
< thead >
< tr >
2020-03-19 16:41:21 +00:00
< th width = "450" > LoopBack Type< / th >
< th width = "450" > MySQL Type< / th >
2017-01-11 00:13:45 +00:00
< / tr >
< / thead >
< tbody >
< tr >
< td > String/JSON< / td >
< td > VARCHAR< / td >
< / tr >
< tr >
< td > Text< / td >
< td > TEXT< / td >
< / tr >
< tr >
< td > Number< / td >
< td > INT< / td >
< / tr >
< tr >
< td > Date< / td >
< td > DATETIME< / td >
< / tr >
< tr >
< td > Boolean< / td >
< td > TINYINT(1)< / td >
< / tr >
< tr >
< td > < a href = "http://apidocs.strongloop.com/loopback-datasource-juggler/#geopoint" class = "external-link" > GeoPoint< / a > object< / td >
< td > POINT< / td >
< / tr >
< tr >
< td > Custom Enum type< br > (See < a href = "#enum" > Enum< / a > below)< / td >
< td > ENUM< / td >
< / tr >
< / tbody >
< / table >
2012-12-14 14:01:44 +00:00
2017-01-11 00:13:45 +00:00
### MySQL to LoopBack types
< table >
2020-03-19 16:41:21 +00:00
< thead >
2017-01-11 00:13:45 +00:00
< tr >
2020-03-19 16:41:21 +00:00
< th width = "450" > MySQL Type< / th >
< th width = "450" > LoopBack Type< / th >
2017-01-11 00:13:45 +00:00
< / tr >
2020-03-19 16:41:21 +00:00
< / thead >
< tbody >
2017-01-11 00:13:45 +00:00
< tr >
< td > CHAR< / td >
< td > String< / td >
< / tr >
< tr >
2017-07-26 13:51:09 +00:00
< td > BIT(1)< br > CHAR(1)< br > TINYINT(1)< / td >
2017-01-11 00:13:45 +00:00
< td > Boolean< / td >
< / tr >
< tr >
< td > VARCHAR< br > TINYTEXT< br > MEDIUMTEXT< br > LONGTEXT< br > TEXT< br > ENUM< br > SET< / td >
< td > String< / td >
< / tr >
< tr >
< td > TINYBLOB< br > MEDIUMBLOB< br > LONGBLOB< br > BLOB< br > BINARY< br > VARBINARY< br > BIT< / td >
< td > Node.js < a href = "http://nodejs.org/api/buffer.html" > Buffer object< / a > < / td >
< / tr >
< tr >
< td > TINYINT< br > SMALLINT< br > INT< br > MEDIUMINT< br > YEAR< br > FLOAT< br > DOUBLE< br > NUMERIC< br > DECIMAL< / td >
< td >
< p > Number< br > For FLOAT and DOUBLE, see < a href = "#floating-point-types" > Floating-point types< / a > . < / p >
< p > For NUMERIC and DECIMAL, see < a href = "MySQL-connector.html" > Fixed-point exact value types< / a > < / p >
< / td >
< / tr >
< tr >
< td > DATE< br > TIMESTAMP< br > DATETIME< / td >
< td > Date< / td >
< / tr >
< / tbody >
< / table >
2020-03-16 14:09:17 +00:00
_NOTE_ as of v3.0.0 of MySQL Connector, the following flags were introduced:
2017-07-26 13:51:09 +00:00
2020-03-16 14:09:17 +00:00
- `treatCHAR1AsString`
2017-07-26 13:51:09 +00:00
default `false` - treats CHAR(1) as a String instead of a Boolean
2020-03-16 14:09:17 +00:00
- `treatBIT1AsBit`
2017-07-26 13:51:09 +00:00
default `true` - treats BIT(1) as a Boolean instead of a Binary
2020-03-16 14:09:17 +00:00
- `treatTINYINT1AsTinyInt`
2017-07-26 13:51:09 +00:00
default `true` - treats TINYINT(1) as a Boolean instead of a Number
2017-01-11 00:13:45 +00:00
## Using the datatype field/column option with MySQL
Use the `mysql` model property to specify additional MySQL-specific properties for a LoopBack model.
For example:
{% include code-caption.html content="/common/models/model.json" %}
2020-03-16 14:09:17 +00:00
2017-01-11 00:13:45 +00:00
```javascript
"locationId":{
"type":"String",
"required":true,
"length":20,
"mysql":
2012-12-14 14:01:44 +00:00
{
2017-01-11 00:13:45 +00:00
"columnName":"LOCATION_ID",
2017-03-10 11:50:59 +00:00
"dataType":"VARCHAR",
2017-01-11 00:13:45 +00:00
"dataLength":20,
"nullable":"N"
2012-12-14 14:01:44 +00:00
}
2017-01-11 00:13:45 +00:00
}
```
You can also use the dataType column/property attribute to specify what MySQL column type to use for many loopback-datasource-juggler types.
The following type-dataType combinations are supported:
2020-03-16 14:09:17 +00:00
- Number
- integer
- tinyint
- smallint
- mediumint
- int
- bigint
2017-01-11 00:13:45 +00:00
Use the `limit` option to alter the display width. Example:
```javascript
{ userName : {
type: String,
dataType: 'char',
limit: 24
}
}
```
2017-08-09 00:40:40 +00:00
### Default Clause/Constant
2020-03-16 14:09:17 +00:00
2017-08-09 00:40:40 +00:00
Use the `default` property to have MySQL handle setting column `DEFAULT` value.
2020-03-16 14:09:17 +00:00
2017-08-09 00:40:40 +00:00
```javascript
"status": {
"type": "string",
"mysql": {
"default": "pending"
}
},
"number": {
"type": "number",
"mysql": {
"default": 256
}
}
```
2020-03-16 14:09:17 +00:00
2017-08-09 00:40:40 +00:00
For the date or timestamp types use `CURRENT_TIMESTAMP` or `now` :
2020-03-16 14:09:17 +00:00
2017-08-09 00:40:40 +00:00
```javascript
"last_modified": {
"type": "date",
"mysql": {
"default":"CURRENT_TIMESTAMP"
}
}
```
2020-03-16 14:09:17 +00:00
2017-08-09 00:40:40 +00:00
**NOTE**: The following column types do **NOT** supported [MySQL Default Values ](https://dev.mysql.com/doc/refman/5.7/en/data-type-defaults.html ):
2020-03-16 14:09:17 +00:00
2017-08-09 00:40:40 +00:00
- BLOB
- TEXT
- GEOMETRY
- JSON
2017-01-11 00:13:45 +00:00
### Floating-point types
For Float and Double data types, use the `precision` and `scale` options to specify custom precision. Default is (16,8). For example:
```javascript
{ average :
{ type: Number,
dataType: 'float',
precision: 20,
scale: 4
}
}
```
### Fixed-point exact value types
For Decimal and Numeric types, use the `precision` and `scale` options to specify custom precision. Default is (9,2).
These aren't likely to function as true fixed-point.
Example:
```javascript
{ stdDev :
2020-03-16 14:09:17 +00:00
{ type: Number,
2017-01-11 00:13:45 +00:00
dataType: 'decimal',
precision: 12,
scale: 8
}
}
```
### Other types
Convert String / DataSource.Text / DataSource.JSON to the following MySQL types:
2020-03-16 14:09:17 +00:00
- varchar
- char
- text
- mediumtext
- tinytext
- longtext
2017-01-11 00:13:45 +00:00
2020-03-16 14:09:17 +00:00
Example:
2017-01-11 00:13:45 +00:00
```javascript
{ userName :
{ type: String,
dataType: 'char',
limit: 24
}
}
```
2020-03-16 14:09:17 +00:00
Example:
2017-01-11 00:13:45 +00:00
```javascript
{ biography :
{ type: String,
dataType: 'longtext'
}
}
```
2017-04-24 20:19:33 +00:00
Convert JSON Date types to datetime or timestamp
2020-03-16 14:09:17 +00:00
Example:
2017-04-24 20:19:33 +00:00
```javascript
{ startTime :
{ type: Date,
dataType: 'timestamp'
}
}
```
2017-01-11 00:13:45 +00:00
### Enum
Enums are special. Create an Enum using Enum factory:
```javascript
var MOOD = dataSource.EnumFactory('glad', 'sad', 'mad');
MOOD.SAD; // 'sad'
MOOD(2); // 'sad'
MOOD('SAD'); // 'sad'
MOOD('sad'); // 'sad'
{ mood: { type: MOOD }}
{ choice: { type: dataSource.EnumFactory('yes', 'no', 'maybe'), null: false }}
```
## Discovery and auto-migration
### Model discovery
The MySQL connector supports _model discovery_ that enables you to create LoopBack models
2020-03-16 14:09:17 +00:00
based on an existing database schema using the unified [database discovery API ](http://apidocs.strongloop.com/loopback-datasource-juggler/#datasource-prototype-discoverandbuildmodels ). For more information on discovery, see [Discovering models from relational databases ](https://loopback.io/doc/en/lb3/Discovering-models-from-relational-databases.html ).
2017-01-11 00:13:45 +00:00
2017-05-04 14:48:46 +00:00
### Auto-migration
2017-01-11 00:13:45 +00:00
The MySQL connector also supports _auto-migration_ that enables you to create a database schema
from LoopBack models using the [LoopBack automigrate method ](http://apidocs.strongloop.com/loopback-datasource-juggler/#datasource-prototype-automigrate ).
For more information on auto-migration, see [Creating a database schema from models ](https://loopback.io/doc/en/lb3/Creating-a-database-schema-from-models.html ) for more information.
2017-06-15 17:32:08 +00:00
#### Auto-migrate/Auto-update models with foreign keys
MySQL handles the foreign key integrity of the related models upon auto-migrate or auto-update operation. It first deletes any related models before calling delete on the models with the relationship.
Example:
**model-definiton.json**
2020-03-16 14:09:17 +00:00
2017-06-15 17:32:08 +00:00
```json
{
"name": "Book",
"base": "PersistedModel",
2017-06-20 16:01:55 +00:00
"idInjection": false,
2017-06-15 17:32:08 +00:00
"properties": {
2017-06-20 16:01:55 +00:00
"bId": {
"type": "number",
"id": true,
"required": true
},
2017-06-15 17:32:08 +00:00
"name": {
"type": "string"
},
2017-06-20 16:01:55 +00:00
"isbn": {
"type": "string"
}
2017-06-15 17:32:08 +00:00
},
"validations": [],
"relations": {
"author": {
"type": "belongsTo",
"model": "Author",
2017-06-20 16:01:55 +00:00
"foreignKey": "authorId"
2017-06-15 17:32:08 +00:00
}
},
"acls": [],
"methods": {},
"foreignKeys": {
"authorId": {
"name": "authorId",
"foreignKey": "authorId",
2017-06-20 16:01:55 +00:00
"entityKey": "aId",
2018-12-04 14:19:44 +00:00
"entity": "Author",
"onUpdate": "restrict",
"onDelete": "restrict"
2017-06-15 17:32:08 +00:00
}
}
}
```
```json
{
"name": "Author",
"base": "PersistedModel",
2017-06-20 16:01:55 +00:00
"idInjection": false,
2017-06-15 17:32:08 +00:00
"properties": {
2017-06-20 16:01:55 +00:00
"aId": {
"type": "number",
"id": true,
"required": true
},
2017-06-15 17:32:08 +00:00
"name": {
"type": "string"
2017-06-20 16:01:55 +00:00
},
"dob": {
2017-06-15 17:32:08 +00:00
"type": "date"
}
},
"validations": [],
2017-06-20 16:01:55 +00:00
"relations": {},
2017-06-15 17:32:08 +00:00
"acls": [],
2017-06-20 16:01:55 +00:00
"methods": {}
2017-06-15 17:32:08 +00:00
}
```
**boot-script.js**
2020-03-16 14:09:17 +00:00
2017-06-15 17:32:08 +00:00
```js
module.exports = function(app) {
var mysqlDs = app.dataSources.mysqlDS;
var Book = app.models.Book;
var Author = app.models.Author;
2017-06-20 16:01:55 +00:00
// first autoupdate the `Author` model to avoid foreign key constraint failure
mysqlDs.autoupdate('Author', function(err) {
2017-06-15 17:32:08 +00:00
if (err) throw err;
2017-06-20 16:01:55 +00:00
console.log('\nAutoupdated table `Author` .');
2017-06-15 17:32:08 +00:00
2017-06-20 16:01:55 +00:00
mysqlDs.autoupdate('Book', function(err) {
if (err) throw err;
console.log('\nAutoupdated table `Book` .');
// at this point the database table `Book` should have one foreign key `authorId` integrated
});
2017-06-15 17:32:08 +00:00
});
};
```
2020-03-16 14:09:17 +00:00
2017-06-16 16:27:48 +00:00
#### Breaking Changes with GeoPoint since 5.x
2020-03-16 14:09:17 +00:00
2017-06-16 16:27:48 +00:00
Prior to `loopback-connector-mysql@5.x` , MySQL connector was saving and loading GeoPoint properties from the MySQL database in reverse.
MySQL expects values to be POINT(X, Y) or POINT(lng, lat), but the connector was saving them in the opposite order(i.e. POINT(lat,lng)).
If you have an application with a model that has a GeoPoint property using previous versions of this connector, you can migrate your models
using the following programmatic approach:
**NOTE** Please back up the database tables that have your application data before performing any of the steps.
2020-03-16 14:09:17 +00:00
2017-06-16 16:27:48 +00:00
1. Create a boot script under `server/boot/` directory with the following:
2020-03-16 14:09:17 +00:00
2017-06-16 16:27:48 +00:00
```js
'use strict';
module.exports = function(app) {
function findAndUpdate() {
var teashop = app.models.teashop;
//find all instances of the model we'd like to migrate
teashop.find({}, function(err, teashops) {
teashops.forEach(function(teashopInstance) {
//what we fetch back from the db is wrong, so need to revert it here
2020-03-16 14:09:17 +00:00
var newLocation = {
lng: teashopInstance.location.lat,
lat: teashopInstance.location.lng
};
2017-06-16 16:27:48 +00:00
//only update the GeoPoint property for the model
2020-03-16 14:09:17 +00:00
teashopInstance.updateAttribute('location', newLocation, function(
err,
inst
) {
if (err) console.log('update attribute failed', err);
else console.log('updateAttribute successful');
2017-06-16 16:27:48 +00:00
});
});
});
}
findAndUpdate();
};
```
2020-03-16 14:09:17 +00:00
2017-06-16 16:27:48 +00:00
2. Run the boot script by simply running your application or `node .`
2017-06-15 17:32:08 +00:00
2017-06-16 16:27:48 +00:00
For the above example, the model definition is as follows:
2020-03-16 14:09:17 +00:00
2017-06-16 16:27:48 +00:00
```json
{
"name": "teashop",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"name": {
"type": "string",
"default": "storename"
},
"location": {
"type": "geopoint"
}
},
"validations": [],
"relations": {},
"acls": [],
"methods": {}
}
```
2017-01-11 00:13:45 +00:00
## Running tests
2015-03-15 21:41:46 +00:00
2017-05-12 14:13:15 +00:00
### Own instance
2020-03-16 14:09:17 +00:00
2017-05-12 14:13:15 +00:00
If you have a local or remote MySQL instance and would like to use that to run the test suite, use the following command:
2020-03-16 14:09:17 +00:00
2017-05-12 14:13:15 +00:00
- Linux
2020-03-16 14:09:17 +00:00
2017-05-12 14:13:15 +00:00
```bash
2017-05-15 17:38:30 +00:00
MYSQL_HOST=< HOST > MYSQL_PORT=< PORT > MYSQL_USER=< USER > MYSQL_PASSWORD=< PASSWORD > MYSQL_DATABASE=< DATABASE > CI=true npm test
2017-05-12 14:13:15 +00:00
```
2020-03-16 14:09:17 +00:00
2017-05-12 14:13:15 +00:00
- Windows
2020-03-16 14:09:17 +00:00
2017-05-12 14:13:15 +00:00
```bash
2017-05-15 17:38:30 +00:00
SET MYSQL_HOST=< HOST > SET MYSQL_PORT=< PORT > SET MYSQL_USER=< USER > SET MYSQL_PASSWORD=< PASSWORD > SET MYSQL_DATABASE=< DATABASE > SET CI=true npm test
2017-05-12 14:13:15 +00:00
```
2015-03-15 21:41:46 +00:00
2017-05-12 14:13:15 +00:00
### Docker
2020-03-16 14:09:17 +00:00
2017-05-12 14:13:15 +00:00
If you do not have a local MySQL instance, you can also run the test suite with very minimal requirements.
2020-03-16 14:09:17 +00:00
2017-05-12 14:13:15 +00:00
- Assuming you have [Docker ](https://docs.docker.com/engine/installation/ ) installed, run the following script which would spawn a MySQL instance on your local:
2020-03-16 14:09:17 +00:00
2017-05-12 14:13:15 +00:00
```bash
2017-05-15 17:38:30 +00:00
source setup.sh < HOST > < PORT > < USER > < PASSWORD > < DATABASE >
2017-05-12 14:13:15 +00:00
```
2020-03-16 14:09:17 +00:00
2017-05-15 17:38:30 +00:00
where `<HOST>` , `<PORT>` , `<USER>` , `<PASSWORD>` and `<DATABASE>` are optional parameters. The default values are `localhost` , `3306` , `root` , `pass` and `testdb` respectively.
2020-03-16 14:09:17 +00:00
2017-05-12 14:13:15 +00:00
- Run the test:
2020-03-16 14:09:17 +00:00
2017-05-12 14:13:15 +00:00
```bash
npm test
```