Connect Loopback to various Data Sources
Go to file
Raymond Feng 05eae4aa3e Replace all with find to make it consistent 2013-11-18 11:39:07 -08:00
docs Rename association to relation 2013-11-15 20:29:20 -08:00
examples Replace all with find to make it consistent 2013-11-18 11:39:07 -08:00
lib Replace all with find to make it consistent 2013-11-18 11:39:07 -08:00
support Fix executable for neo4j travis 2012-03-10 16:31:04 +04:00
test Rename association to relation 2013-11-15 20:29:20 -08:00
.gitignore Refactor the CRUD operations to DataAccessObject 2013-05-17 08:49:57 -07:00
.gitmodules Added Makefile 2011-11-05 17:54:52 +07:00
.npmignore Ignore npm-debug.log 2013-03-30 21:10:20 +04:00
.travis.yml Add travis 2013-11-08 09:14:25 -08:00
LICENSE Update license file 2013-09-04 14:46:31 -07:00
Makefile Clean up docs 2013-07-22 22:32:05 -07:00
README.md Wrap README.md at 78 characters where possible 2013-11-15 09:55:38 -08:00
docs.json Set up assets to support embedded diagrams 2013-09-11 15:37:10 -07:00
index.js Export Connector class 2013-10-02 16:29:33 -07:00
package.json Bump version and remove blanket 2013-11-15 13:54:29 -08:00

README.md

LoopBack DataSource Juggler

LoopBack DataSource Juggler is an ORM that provides a common set of interfaces for interacting with databases, REST APIs, and other data sources. It was initially forked from JugglingDB.

Overview

LoopBack DataSource Juggler consists of the following components:

  • LoopBack Definition Language
  • DataSource
  • Connector

loopback-datasource-juggler-overview

LoopBack Definition Language

To define model dataSource have single method dataSource.define. It accepts three arguments:

  • 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:

var User = dataSource.define('User', {
    email: String,
    password: String,
    birthDate: Date,
    activated: Boolean
});

var User = dataSource.define('User', {
    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'
});

DataSource

DataSource is a factory for model classes. DataSource connected with specific database or other backend system using connector.

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 different databases.

Creating dataSource

DataSource constructor available on loopback-datasource-juggler module:

var DataSource = require('loopback-datasource-juggler').DataSource;

DataSource constructor accepts two arguments. First argument is connector. It could be a connector name or connector package:

var dataSourceByConnectorName = new DataSource('memory');
var dataSourceByConnectorModule = new DataSource(require('redis'));

Settings

Second argument is optional settings. Settings object format and defaults depends on specific connector, but common fields are:

  • 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

For connector-specific settings refer to connector's readme file.

LoopBack Connectors

Type Package Name
MongoDB loopback-connector-mongodb
Oracle loopback-connector-oracle
MySQL loopback-connector-mysql

LoopBack connectors provide access to backend systems including databases, REST APIs and other services. Connectors are not used directly by application code. We create a DataSource to interact with the connector.

For example,

var DataSource = require('loopback-datasource-juggler').DataSource;
var oracleConnector = require('loopback-connector-oracle');

var ds = new DataSource(oracleConnector, {
    host : '127.0.0.1',
    database : 'XE',
    username : 'strongloop',
    password : 'password',
    debug : true
});

The connector argument passed the DataSource constructor can be one of the following:

  • The connector module from require(connectorName)
  • The full name of the connector module, such as 'loopback-connector-oracle'
  • The short name of the connector module, such as 'oracle', which will be converted to 'loopback-connector-'
  • A local module under ./connectors/ folder

Installation

npm install loopback-datasource-juggler

Also install the appropriated connector, for example for mongodb:

npm install loopback-connector-mongodb

check following list of available connectors