loopback-connector-mysql/README.md

272 lines
8.5 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-03 21:50:38 +00:00
MySQL connector for [LoopBack Data Source Juggler](http://docs.strongloop.com/loopback-datasource-juggler/).
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-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
## Using the `dataType` field/column option with MySQL
2013-10-03 21:50:38 +00:00
The loopback-datasource-juggler MySQL connector now 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:
- [dataSource.discoverModelDefinitions([username], fn)](https://github.com/strongloop/loopback#datasourcediscovermodeldefinitionsusername-fn)
- [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' }
### Synchronous APIs for discovery
* MySQL.prototype.discoverModelDefinitionsSync = function (options)
* MySQL.prototype.discoverModelPropertiesSync = function (table, options)
* MySQL.prototype.discoverPrimaryKeysSync= function(table, options)
* MySQL.prototype.discoverForeignKeysSync= function(table, options)
* MySQL.prototype.discoverExportedForeignKeysSync= function(table, options)
### Discover/build/try the models
The following example uses `discoverAndBuildModels` to discover, build and try the models:
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
2013-09-24 22:14:23 +00:00
## License
2013-10-03 21:50:38 +00:00
[The MIT license](LICENSE).
2013-09-24 22:14:23 +00:00
The project was initially forked from [mysql-adapter](https://github.com/jugglingdb/mysql-adapter)
which carries the following copyright and permission notices:
2012-12-14 14:01:44 +00:00
2013-03-06 19:10:20 +00:00
2013-10-03 21:50:38 +00:00
Copyright (C) 2012 by Anatoliy Chakkaev
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
2013-03-06 19:10:20 +00:00