loopback-datasource-juggler/docs/data-source.md

128 lines
3.8 KiB
Markdown
Raw Normal View History

2013-07-23 22:19:35 +00:00
# loopback-data-datasource
Everything about datasource, data types and model definition.
2013-03-24 00:43:47 +00:00
## DESCRIPTION
2013-07-23 22:19:35 +00:00
DataSource is a factory for model classes. DataSource connected with specific database or other
backend system using connector.
2013-03-24 00:43:47 +00:00
2013-07-23 22:19:35 +00:00
All model classes within single datasource shares same connector type and one database
connection. But it's possible to use more than one datasource to connect with
2013-03-24 00:43:47 +00:00
different databases.
## EVENTS
2013-07-23 22:19:35 +00:00
Instances of DataSource are event emitters, events supported by default:
2013-03-24 00:43:47 +00:00
* `.on('connected', function() {})`:
Fired when db connection established. Params: none.
* `.on('log', function(msg, duration) {})`:
2013-07-23 22:19:35 +00:00
Fired when connector logged line. Params: String message, Number duration
2013-03-24 00:43:47 +00:00
## USAGE
2013-07-23 22:19:35 +00:00
### Creating dataSource
2013-03-24 00:43:47 +00:00
2013-07-23 22:19:35 +00:00
`DataSource` constructor available on `loopback-data` module:
2013-03-24 00:43:47 +00:00
2013-07-23 22:19:35 +00:00
var DataSource = require('loopback-data').DataSource;
2013-03-24 00:43:47 +00:00
2013-07-23 22:19:35 +00:00
DataSource constructor accepts two arguments. First argument is connector. It could be
connector name or connector package:
2013-03-24 00:43:47 +00:00
2013-07-23 22:19:35 +00:00
var dataSourceByConnectorName = new DataSource('memory');
var dataSourceByConnectorModule = new DataSource(require('redis'));
2013-03-24 00:43:47 +00:00
### Settings
Second argument is optional settings. Settings object format and defaults
2013-07-23 22:19:35 +00:00
depends on specific connector, but common fields are:
2013-03-24 00:43:47 +00:00
* `host`:
Database host
* `port`:
Database port
* `username`:
Username to connect to database
* `password`:
Password to connect to database
* `database`:
Database name
* `debug`:
Turn on verbose mode to debug db queries and lifecycle
2013-07-23 22:19:35 +00:00
For connector-specific settings refer to connector's readme file.
2013-03-24 00:43:47 +00:00
### Connecting to database
2013-07-23 22:19:35 +00:00
DataSource connecting to database automatically. Once connection established dataSource
2013-03-24 00:43:47 +00:00
object emit 'connected' event, and set `connected` flag to true, but it is not
necessary to wait for 'connected' event because all queries cached and executed
2013-07-23 22:19:35 +00:00
when dataSource emit 'connected' event.
2013-03-24 00:43:47 +00:00
2013-07-23 22:19:35 +00:00
To disconnect from database server call `dataSource.disconnect` method. This call
forwarded to connector if connector have ability to connect/disconnect.
2013-03-24 00:43:47 +00:00
### Model definition
2013-07-23 22:19:35 +00:00
To define model dataSource have single method `dataSource.define`. It accepts three
2013-03-24 00:43:47 +00:00
argumets:
* **model name**:
String name in camel-case with first upper-case letter. This name will be used
later to access model.
* **properties**:
Object with property type definitions. Key is property name, value is type
definition. Type definition can be function representing type of property
(String, Number, Date, Boolean), or object with {type: String|Number|...,
index: true|false} format.
* **settings**:
Object with model-wide settings such as `tableName` or so.
Examples of model definition:
2013-07-23 22:19:35 +00:00
var User = dataSource.define('User', {
2013-03-24 00:43:47 +00:00
email: String,
password: String,
birthDate: Date,
activated: Boolean
});
2013-07-23 22:19:35 +00:00
var User = dataSource.define('User', {
2013-03-24 00:43:47 +00:00
email: { type: String, limit: 150, index: true },
password: { type: String, limit: 50 },
birthDate: Date,
registrationDate: {
type: Date,
default: function () { return new Date }
},
activated: { type: Boolean, default: false }
}, {
tableName: 'users'
});
### DB structure syncronization
2013-07-23 22:19:35 +00:00
DataSource instance have two methods for updating db structure: automigrate and
2013-03-24 00:43:47 +00:00
autoupdate.
The `automigrate` method drop table (if exists) and create it again,
`autoupdate` method generates ALTER TABLE query. Both method accepts callback
called when migration/update done.
To check if any db changes required use `isActual` method. It accepts single
`callback` argument, which receive boolean value depending on db state: false if
2013-07-23 22:19:35 +00:00
db structure outdated, true when dataSource and db is in sync:
2013-03-24 00:43:47 +00:00
2013-07-23 22:19:35 +00:00
dataSource.isActual(function(err, actual) {
2013-03-24 00:43:47 +00:00
if (!actual) {
2013-07-23 22:19:35 +00:00
dataSource.autoupdate();
2013-03-24 00:43:47 +00:00
}
});
## SEE ALSO
2013-07-23 22:19:35 +00:00
loopback-data-model
loopback-data-connector