loopback-connector-mysql/README.md

370 lines
10 KiB
Markdown
Raw Normal View History

2013-10-03 21:50:38 +00:00
## loopback-connector-mysql
2012-12-14 14:01:44 +00:00
2013-10-14 22:19:04 +00:00
`loopback-connector-mysql` is the MySQL connector module for [loopback-datasource-juggler](http://docs.strongloop.com/loopback-datasource-juggler/).
2012-12-14 14:01:44 +00:00
## Installation
````sh
npm install loopback-connector-mysql --save
````
2012-12-14 14:01:44 +00:00
## Usage
To use it you need `loopback-datasource-juggler`.
2012-12-14 14:01:44 +00:00
1. Setup dependencies in `package.json`:
```json
{
...
"dependencies": {
"loopback-datasource-juggler": "latest",
2013-07-22 00:27:54 +00:00
"loopback-connector-mysql": "latest"
2012-12-14 14:01:44 +00:00
},
...
}
```
2. Use:
```javascript
2013-09-24 22:14:23 +00:00
var DataSource = require('loopback-datasource-juggler').DataSource;
var dataSource = new DataSource('mysql', {
2013-10-14 22:19:04 +00:00
host: 'localhost',
port: 3306,
2013-10-03 21:50:38 +00:00
database: 'mydb',
username: 'myuser',
password: 'mypass'
2012-12-14 14:01:44 +00:00
});
```
2013-10-03 21:50:38 +00:00
You can optionally pass a few additional parameters supported by [`node-mysql`](https://github.com/felixge/node-mysql),
2013-09-24 22:14:23 +00:00
most particularly `password` and `collation`. `Collation` currently defaults
to `utf8_general_ci`. The `collation` value will also be used to derive the
connection charset.
2012-12-14 14:01:44 +00:00
2013-10-14 22:19:04 +00:00
## Data type mappings
`loopback-connector-mysql` uses the following rules to map between JSON types and MySQL data types.
### JSON to MySQL types
- String/JSON: VARCHAR
- Text: TEXT
- Number: INT
- Date: DATETIME
- BOOLEAN: TINYINT(1)
- Point/GeoPoint: POINT
- Enum: ENUM
### MySQL to JSON types
- CHAR: String
- CHAR(1): Boolean
- VARCHAR/TINYTEXT/MEDIUMTEXT/LONGTEXT/TEXT/ENUM/SET: String
- TINYBLOB/MEDIUMBLOB/LONGBLOB/BLOB/BINARY/VARBINARY/BIT: Binary
- TINYINT/SMALLINT/INT/MEDIUMINT/YEAR/FLOAT/DOUBLE/NUMERIC/DECIMAL: Number
- DATE/TIMESTAMP/DATETIME: Date
## Using the `dataType` field/column option with MySQL
2013-10-14 22:19:04 +00:00
`loopback-connector-mysql` allows mapping of LoopBack model properties to MYSQL columns using the 'mysql' property of the
property definition. For example,
"locationId":{
"type":"String",
"required":true,
"length":20,
"mysql":
{
"columnName":"LOCATION_ID",
"dataType":"VARCHAR2",
"dataLength":20,
"nullable":"N"
}
}
`loopback-connector-mysql` also supports using the `dataType` column/property attribute to specify what MySQL column
type is used for many loopback-datasource-juggler types.
The following type-dataType combinations are supported:
2013-10-03 21:50:38 +00:00
- Number
- integer
- tinyint
- smallint
- mediumint
- int
- bigint
Use the `limit` option to alter the display width.
Example:
`{ count : { type: Number, dataType: 'smallInt' }}`
2013-10-03 21:50:38 +00:00
- floating point types
- float
- double
Use the `precision` and `scale` options to specify custom precision. Default is (16,8).
Example:
`{ average : { type: Number, dataType: 'float', precision: 20, scale: 4 }}`
2013-10-03 21:50:38 +00:00
- fixed-point exact value types
- decimal
- numeric
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:
`{ stdDev : { type: Number, dataType: 'decimal', precision: 12, scale: 8 }}`
2013-10-03 21:50:38 +00:00
- String / DataSource.Text / DataSource.JSON
- varchar
- char
- text
- mediumtext
- tinytext
- longtext
Example:
`{ userName : { type: String, dataType: 'char', limit: 24 }}`
Example:
`{ biography : { type: String, dataType: 'longtext' }}`
2013-10-03 21:50:38 +00:00
- Date
- datetime
- timestamp
Example:
`{ startTime : { type: Date, dataType: 'timestamp' }}`
2012-12-14 14:01:44 +00:00
2013-10-03 21:50:38 +00:00
* Enum
2013-06-19 15:41:51 +00:00
Enums are special.
2013-06-18 18:32:42 +00:00
Create an Enum using Enum factory:
```javascript
2013-09-24 22:14:23 +00:00
var MOOD = dataSource.EnumFactory('glad', 'sad', 'mad');
2013-06-18 18:32:42 +00:00
MOOD.SAD; // 'sad'
MOOD(2); // 'sad'
MOOD('SAD'); // 'sad'
MOOD('sad'); // 'sad'
```
2013-10-03 21:50:38 +00:00
- `{ mood: { type: MOOD }}`
- `{ choice: { type: dataSource.EnumFactory('yes', 'no', 'maybe'), null: false }}`
## Discovering Models
MySQL data sources allow you to discover model definition information from existing mysql databases. See the following APIs:
2013-10-14 22:19:04 +00:00
- [dataSource.discoverModelDefinitions([owner], fn)](https://github.com/strongloop/loopback#datasourcediscovermodeldefinitionsusername-fn)
2013-10-03 21:50:38 +00:00
- [dataSource.discoverSchema([owner], name, fn)](https://github.com/strongloop/loopback#datasourcediscoverschemaowner-name-fn)
### Asynchronous APIs for discovery
* MySQL.prototype.discoverModelDefinitions = function (options, cb)
- options:
- all: {Boolean} To include tables/views from all schemas/owners
- owner/schema: {String} The schema/owner name
- views: {Boolean} To include views
- cb:
- Get a list of table/view names, for example:
{type: 'table', name: 'INVENTORY', owner: 'STRONGLOOP' }
{type: 'table', name: 'LOCATION', owner: 'STRONGLOOP' }
{type: 'view', name: 'INVENTORY_VIEW', owner: 'STRONGLOOP' }
* MySQL.prototype.discoverModelProperties = function (table, options, cb)
- table: {String} The name of a table or view
- options:
- owner/schema: {String} The schema/owner name
- cb:
- Get a list of model property definitions, for example:
{ owner: 'STRONGLOOP',
tableName: 'PRODUCT',
columnName: 'ID',
dataType: 'VARCHAR2',
dataLength: 20,
nullable: 'N',
type: 'String' }
{ owner: 'STRONGLOOP',
tableName: 'PRODUCT',
columnName: 'NAME',
dataType: 'VARCHAR2',
dataLength: 64,
nullable: 'Y',
type: 'String' }
* MySQL.prototype.discoverPrimaryKeys= function(table, options, cb)
- table: {String} The name of a table or view
- options:
- owner/schema: {String} The schema/owner name
- cb:
- Get a list of primary key definitions, for example:
{ owner: 'STRONGLOOP',
tableName: 'INVENTORY',
columnName: 'PRODUCT_ID',
keySeq: 1,
pkName: 'ID_PK' }
{ owner: 'STRONGLOOP',
tableName: 'INVENTORY',
columnName: 'LOCATION_ID',
keySeq: 2,
pkName: 'ID_PK' }
* MySQL.prototype.discoverForeignKeys= function(table, options, cb)
- table: {String} The name of a table or view
- options:
- owner/schema: {String} The schema/owner name
- cb:
- Get a list of foreign key definitions, for example:
{ fkOwner: 'STRONGLOOP',
fkName: 'PRODUCT_FK',
fkTableName: 'INVENTORY',
fkColumnName: 'PRODUCT_ID',
keySeq: 1,
pkOwner: 'STRONGLOOP',
pkName: 'PRODUCT_PK',
pkTableName: 'PRODUCT',
pkColumnName: 'ID' }
2013-06-19 15:41:51 +00:00
2013-10-03 21:50:38 +00:00
* MySQL.prototype.discoverExportedForeignKeys= function(table, options, cb)
- table: {String} The name of a table or view
- options:
- owner/schema: {String} The schema/owner name
- cb:
- Get a list of foreign key definitions that reference the primary key of the given table, for example:
{ fkName: 'PRODUCT_FK',
fkOwner: 'STRONGLOOP',
fkTableName: 'INVENTORY',
fkColumnName: 'PRODUCT_ID',
keySeq: 1,
pkName: 'PRODUCT_PK',
pkOwner: 'STRONGLOOP',
pkTableName: 'PRODUCT',
pkColumnName: 'ID' }
### Discover/build/try the models
2013-10-14 22:19:04 +00:00
#### Build a LDL schema by discovery
Data sources backed by the MySQL connector can discover LDL models from the database using the `discoverSchema` API. For
example,
dataSource.discoverSchema('INVENTORY', {owner: 'STRONGLOOP'}, function (err, schema) {
...
}
Here is the sample result. Please note there are 'mysql' properties in addition to the regular LDL model options and
properties. The 'mysql' objects contain the MySQL specific mappings.
{
"name":"Inventory",
"options":{
"idInjection":false,
"mysql":{
"schema":"STRONGLOOP",
"table":"INVENTORY"
}
},
"properties":{
"productId":{
"type":"String",
"required":false,
"length":60,
"precision":null,
"scale":null,
"id":1,
"mysql":{
"columnName":"PRODUCT_ID",
"dataType":"varchar",
"dataLength":60,
"dataPrecision":null,
"dataScale":null,
"nullable":"NO"
}
},
"locationId":{
"type":"String",
"required":false,
"length":60,
"precision":null,
"scale":null,
"id":2,
"mysql":{
"columnName":"LOCATION_ID",
"dataType":"varchar",
"dataLength":60,
"dataPrecision":null,
"dataScale":null,
"nullable":"NO"
}
},
"available":{
"type":"Number",
"required":false,
"length":null,
"precision":10,
"scale":0,
"mysql":{
"columnName":"AVAILABLE",
"dataType":"int",
"dataLength":null,
"dataPrecision":10,
"dataScale":0,
"nullable":"YES"
}
},
"total":{
"type":"Number",
"required":false,
"length":null,
"precision":10,
"scale":0,
"mysql":{
"columnName":"TOTAL",
"dataType":"int",
"dataLength":null,
"dataPrecision":10,
"dataScale":0,
"nullable":"YES"
}
}
}
}
We can also discover and build model classes in one shot. The following example uses `discoverAndBuildModels` to discover,
build and try the models:
2013-10-03 21:50:38 +00:00
dataSource.discoverAndBuildModels('INVENTORY', { owner: 'STRONGLOOP', visited: {}, associations: true},
function (err, models) {
// Show records from the models
for(var m in models) {
models[m].all(show);
};
// Find one record for inventory
models.Inventory.findOne({}, function(err, inv) {
console.log("\nInventory: ", inv);
// Follow the foreign key to navigate to the product
inv.product(function(err, prod) {
console.log("\nProduct: ", prod);
console.log("\n ------------- ");
});
});
}
2013-06-18 18:32:42 +00:00