loopback/README.md

1133 lines
32 KiB
Markdown
Raw Normal View History

2013-07-16 17:49:25 +00:00
# loopback
2013-06-25 20:52:14 +00:00
v0.9.0
2013-04-09 16:02:36 +00:00
## Install
2013-07-16 17:49:25 +00:00
slnode install loopback -g
2013-04-26 18:44:19 +00:00
2013-06-06 20:50:46 +00:00
## Server API
2013-04-26 18:44:19 +00:00
2013-06-05 14:38:04 +00:00
- [App](#app)
2013-06-06 20:50:46 +00:00
- [Model](#model)
- [DataSource](#data-source)
- [Connectors](#connectors)
2013-07-16 17:49:25 +00:00
- [Loopback Types](#loopback-types)
2013-07-17 21:08:14 +00:00
- [GeoPoint](#geo-point)
2013-06-06 20:50:46 +00:00
- [REST Router](#rest-router)
- [Bundled Models](#bundled-models)
- [User](#user-model)
- [Session](#session-model)
- [Email](#email-model)
2013-04-26 18:44:19 +00:00
2013-06-06 20:50:46 +00:00
## Client API
2013-04-26 18:44:19 +00:00
2013-06-05 14:38:04 +00:00
_TODO_
### App
2013-07-17 21:52:21 +00:00
Create A Loopbackapplication.
2013-06-05 14:38:04 +00:00
2013-07-16 17:49:25 +00:00
var loopback = require('loopback');
var app = loopback();
2013-06-05 14:38:04 +00:00
app.get('/', function(req, res){
res.send('hello world');
});
app.listen(3000);
**Notes:**
- extends [express](http://expressjs.com/api.html#express)
- see [express docs](http://expressjs.com/api.html) for details
- supports [express / connect middleware](http://expressjs.com/api.html#middleware)
#### app.model(Model)
Expose a `Model` to remote clients.
// create a testing data source
2013-07-16 17:49:25 +00:00
var memory = loopback.memory();
2013-06-11 16:01:44 +00:00
var Color = memory.createModel('color', {name: String});
Color.attachTo(memory);
2013-06-05 14:38:04 +00:00
app.model(Color);
2013-07-16 17:49:25 +00:00
app.use(loopback.rest());
2013-06-05 14:38:04 +00:00
**Note:** this will expose all [shared methods](#shared-methods) on the model.
#### app.models()
Get the app's exposed models.
var models = app.models();
models.forEach(function (Model) {
2013-06-11 16:01:44 +00:00
console.log(Model.modelName); // color
2013-06-05 14:38:04 +00:00
});
### Model
2013-07-17 21:52:21 +00:00
A Loopback `Model` is a vanilla JavaScript class constructor with an attached set of properties and options. A `Model` instance is created by passing a data object containing properties to the `Model` constructor. A `Model` constructor will clean the object passed to it and only set the values matching the properties you define.
2013-06-06 20:30:52 +00:00
2013-06-11 16:01:44 +00:00
// valid color
2013-07-16 17:49:25 +00:00
var Color = loopback.createModel('color', {name: String});
2013-06-06 20:30:52 +00:00
var red = new Color({name: 'red'});
2013-06-11 16:01:44 +00:00
console.log(red.name); // red
// invalid color
var foo = new Color({bar: 'bat baz'});
console.log(foo.bar); // undefined
2013-06-05 14:38:04 +00:00
**Properties**
A model defines a list of property names, types and other validation metadata. A [DataSource](#data-source) uses this definition to validate a `Model` during operations such as `save()`.
2013-06-11 16:01:44 +00:00
**Options**
2013-06-05 14:38:04 +00:00
2013-06-11 16:01:44 +00:00
Some [DataSources](#data-source) may support additional `Model` options.
2013-06-05 14:38:04 +00:00
2013-07-17 21:52:21 +00:00
Define A Loopbackmodel.
2013-06-05 14:38:04 +00:00
2013-07-16 17:49:25 +00:00
var User = loopback.createModel('user', {
2013-06-05 14:38:04 +00:00
first: String,
last: String,
age: Number
});
2013-06-06 20:30:52 +00:00
### Validation (expiremental)
#### Model.validatesFormatOf(property, options)
Require a model to include a property that matches the given format.
User.validatesFormat('name', {with: /\w+/});
2013-06-06 20:30:52 +00:00
#### Model.validatesPresenceOf(properties...)
Require a model to include a property to be considered valid.
User.validatesPresenceOf('first', 'last', 'age');
#### Model.validatesLengthOf(property, options)
Require a property length to be within a specified range.
User.validatesLengthOf('password', {min: 5, message: {min: 'Password is too short'}});
#### Model.validatesInclusionOf(property, options)
Require a value for `property` to be in the specified array.
User.validatesInclusionOf('gender', {in: ['male', 'female']});
#### Model.validatesExclusionOf(property, options)
Require a value for `property` to not exist in the specified array.
User.validatesExclusionOf('domain', {in: ['www', 'billing', 'admin']});
#### Model.validatesNumericalityOf(property, options)
Require a value for `property` to be a specific type of `Number`.
User.validatesNumericalityOf('age', {int: true});
#### Model.validatesUniquenessOf(property, options)
Ensure the value for `property` is unique.
User.validatesUniquenessOf('email', {message: 'email is not unique'});
**Note:** not available for all [connectors](#connectors).
#### myModel.isValid()
Validate the model instance.
user.isValid(function (valid) {
if (!valid) {
user.errors // hash of errors {attr: [errmessage, errmessage, ...], attr: ...}
}
});
2013-06-28 01:26:44 +00:00
#### Model.properties
2013-07-16 17:49:25 +00:00
An object containing a normalized set of properties supplied to `loopback.createModel(name, properties)`.
2013-06-28 01:26:44 +00:00
Example:
var props = {
a: String,
b: {type: 'Number'},
c: {type: 'String', min: 10, max: 100},
d: Date,
2013-07-16 17:49:25 +00:00
e: loopback.GeoPoint
2013-06-28 01:26:44 +00:00
};
2013-07-16 17:49:25 +00:00
var MyModel = loopback.createModel('foo', props);
2013-06-28 01:26:44 +00:00
console.log(MyModel.properties);
Outputs:
{
"a": {type: String},
"b": {type: Number},
"c": {
"type": String,
"min": 10,
"max": 100
},
"d": {type: Date},
"e": {type: GeoPoint},
"id": {
"id": 1
}
}
2013-06-05 14:38:04 +00:00
#### Model.attachTo(dataSource)
Attach a model to a [DataSource](#data-source). Attaching a [DataSource](#data-source) updates the model with additional methods and behaviors.
2013-07-16 17:49:25 +00:00
var oracle = loopback.createDataSource({
connector: require('loopback-connector-oracle'),
2013-06-05 14:38:04 +00:00
host: '111.22.333.44',
database: 'MYDB',
username: 'username',
password: 'password'
});
User.attachTo(oracle);
2013-06-06 20:50:46 +00:00
**Note:** until a model is attached to a data source it will **not** have any **attached methods**.
2013-06-05 14:38:04 +00:00
#### CRUD and Query Mixins
2013-07-16 20:39:03 +00:00
Mixins are added by attaching a vanilla model to a [data source](#data-source) with a [connector](#connectors). Each [connector](#connectors) enables its own set of operations that are mixed into a `Model` as methods. To see available methods for a data source call `dataSource.operations()`.
Log the available methods for a memory data source.
var ops = loopback
.createDataSource({connector: loopback.Memory})
.operations();
console.log(Object.keys(ops));
Outputs:
[ 'create',
'updateOrCreate',
'upsert',
'findOrCreate',
'exists',
'findById',
'find',
'all',
'findOne',
'destroyAll',
'deleteAll',
'count',
'include',
'relationNameFor',
'hasMany',
'belongsTo',
'hasAndBelongsToMany',
'save',
'isNewRecord',
'destroy',
'delete',
'updateAttribute',
'updateAttributes',
'reload' ]
Here is the definition of the `count()` operation.
{
accepts: [ { arg: 'where', type: 'object' } ],
http: { verb: 'get', path: '/count' },
remoteEnabled: true,
name: 'count'
}
2013-06-06 00:44:46 +00:00
#### Static Methods
2013-06-06 00:44:46 +00:00
2013-07-16 20:39:03 +00:00
**Note:** These are the default mixin methods for a `Model` attached to a data source. See the specific connector for additional API documentation.
2013-06-21 21:59:30 +00:00
##### Model.create(data, [callback])
2013-06-06 00:44:46 +00:00
2013-06-21 21:59:30 +00:00
Create an instance of Model with given data and save to the attached data source. Callback is optional.
2013-06-06 00:44:46 +00:00
User.create({first: 'Joe', last: 'Bob'}, function(err, user) {
console.log(user instanceof User); // true
});
2013-06-21 21:59:30 +00:00
**Note:** You must include a callback and use the created model provided in the callback if your code depends on your model being saved or having an `id`.
2013-06-06 00:44:46 +00:00
##### Model.count([query], callback)
2013-06-06 00:44:46 +00:00
Query count of Model instances in data source. Optional query param allows to count filtered set of Model instances.
2013-06-06 00:44:46 +00:00
User.count({approved: true}, function(err, count) {
console.log(count); // 2081
2013-06-06 00:44:46 +00:00
});
##### Model.find(filter, callback)
2013-06-06 00:44:46 +00:00
Find all instances of Model, matched by query. Fields used for filter and sort should be declared with `{index: true}` in model definition.
**filter**
- **where** `Object` { key: val, key2: {gt: 'val2'}}
- **include** `String`, `Object` or `Array`.
- **order** `String`
- **limit** `Number`
- **skip** `Number`
2013-07-17 19:18:17 +00:00
- **fields** `Object|Array|String`
- `['foo']` or `'foo'` - include only the foo property
- `['foo', 'bar']` - include the foo and bar properties
- `{foo: true}` - include only foo
- `{bat: false}` - include all properties, exclude bat
Find the second page of 10 users over age 21 in descending order exluding the password property.
User.find({
where: {
age: {gt: 21}},
order: 'age DESC',
limit: 10,
skip: 10,
fields: {password: false}
},
console.log
);
2013-06-06 20:50:46 +00:00
**Note:** See the specific connector's [docs](#connectors) for more info.
2013-06-06 00:44:46 +00:00
##### Model.destroyAll(callback)
2013-06-06 00:44:46 +00:00
Delete all Model instances from data source. **Note:** destroyAll method does not perform destroy hooks.
2013-06-06 00:44:46 +00:00
##### Model.findById(id, callback)
Find instance by id.
User.findById(23, function(err, user) {
console.info(user.id); // 23
2013-06-06 00:44:46 +00:00
});
2013-06-21 00:31:23 +00:00
##### Model.findOne(where, callback)
2013-06-21 00:31:23 +00:00
Find a single instance that matches the given where expression.
2013-06-21 00:31:23 +00:00
User.findOne({id: 23}, function(err, user) {
console.info(user.id); // 23
});
##### Model.upsert(data, callback)
Update when record with id=data.id found, insert otherwise. **Note:** no setters, validations or hooks applied when using upsert.
##### Custom Static Methods
2013-06-05 14:38:04 +00:00
Define a static model method.
User.login = function (username, password, fn) {
var passwordHash = hashPassword(password);
this.findOne({username: username}, function (err, user) {
var failErr = new Error('login failed');
if(err) {
fn(err);
} else if(!user) {
fn(failErr);
} else if(user.password === passwordHash) {
MySessionModel.create({userId: user.id}, function (err, session) {
fn(null, session.id);
});
} else {
fn(failErr);
}
});
}
2013-06-20 21:17:38 +00:00
Setup the static model method to be exposed to clients as a [remote method](#remote-method).
2013-06-05 19:13:55 +00:00
2013-07-16 17:49:25 +00:00
loopback.remoteMethod(
2013-06-06 00:11:21 +00:00
User.login,
{
accepts: [
{arg: 'username', type: 'string', required: true},
{arg: 'password', type: 'string', required: true}
],
returns: {arg: 'sessionId', type: 'any'},
http: {path: '/sign-in'}
}
);
2013-06-05 14:38:04 +00:00
#### Instance Methods
2013-07-16 20:39:03 +00:00
**Note:** These are the default mixin methods for a `Model` attached to a data source. See the specific connector for additional API documentation.
##### model.save([options], [callback])
Save an instance of a Model to the attached data source.
var joe = new User({first: 'Joe', last: 'Bob'});
joe.save(function(err, user) {
if(user.errors) {
console.log(user.errors);
} else {
console.log(user.id);
}
});
##### model.updateAttributes(data, [callback])
Save specified attributes to the attached data source.
user.updateAttributes({
first: 'updatedFirst',
name: 'updatedLast'
}, fn);
##### model.destroy([callback])
Remove a model from the attached data source.
model.destroy(function(err) {
// model instance destroyed
});
##### Custom Instance Methods
2013-06-06 00:11:21 +00:00
Define an instance method.
2013-06-05 14:38:04 +00:00
User.prototype.logout = function (fn) {
MySessionModel.destroyAll({userId: this.id}, fn);
}
2013-06-06 00:11:21 +00:00
Define a remote model instance method.
2013-06-05 14:38:04 +00:00
2013-07-16 17:49:25 +00:00
loopback.remoteMethod(User.prototype.logout);
2013-06-05 19:13:55 +00:00
2013-06-05 19:17:29 +00:00
#### Remote Methods
2013-06-05 19:13:55 +00:00
2013-06-20 21:17:38 +00:00
Both instance and static methods can be exposed to clients. A remote method must accept a callback with the conventional `fn(err, result, ...)` signature.
2013-06-05 19:13:55 +00:00
2013-07-16 17:49:25 +00:00
##### loopback.remoteMethod(fn, [options]);
2013-06-05 19:13:55 +00:00
2013-06-05 19:17:29 +00:00
Expose a remote method.
2013-06-05 19:13:55 +00:00
Product.stats = function(fn) {
2013-07-16 20:39:03 +00:00
var calc = require('./stats');
Product.find(function(err, products) {
var productStats = calc(products);
fn(null, productStats);
});
2013-06-05 19:13:55 +00:00
}
2013-07-16 17:49:25 +00:00
loopback.remoteMethod(
2013-06-06 00:16:00 +00:00
Product.stats,
{
2013-07-16 20:39:03 +00:00
returns: {arg: 'stats', type: 'object'},
2013-06-06 00:16:00 +00:00
http: {path: '/info', verb: 'get'}
}
);
2013-06-05 19:13:55 +00:00
**Options**
2013-06-05 19:17:29 +00:00
- **accepts** - (optional) an arguments description specifying the remote method's arguments. A
- **returns** - (optional) an arguments description specifying the remote methods callback arguments.
2013-06-05 19:13:55 +00:00
- **http** - (advanced / optional, object) http routing info
- **http.path** - the path relative to the model the method will be exposed at. May be a path fragment (eg. '/:myArg') which will be populated by an arg of the same name in the accepts description. For example the stats method above will be at the whole path `/products/stats`.
2013-06-05 19:13:55 +00:00
- **http.verb** - (get, post, put, del, all) - the route verb the method will be available from.
**Argument Description**
An arguments description defines either a single argument as an object or an ordered set of arguments as an array.
2013-06-06 00:11:21 +00:00
// examples
{arg: 'myArg', type: 'number'}
[
{arg: 'arg1', type: 'number', required: true},
{arg: 'arg2', type: 'array'}
]
2013-06-05 19:13:55 +00:00
**Types**
2013-07-16 17:49:25 +00:00
Each argument may define any of the [loopback types](#loopback-types).
2013-06-05 19:13:55 +00:00
**Notes:**
- The callback is an assumed argument and does not need to be specified in the accepts array.
- The err argument is also assumed and does not need to be specified in the returns array.
2013-06-05 14:38:04 +00:00
#### Remote Hooks
2013-06-05 19:13:55 +00:00
Run a function before or after a remote method is called by a client.
2013-06-05 14:38:04 +00:00
2013-06-12 22:44:38 +00:00
// *.save === prototype.save
User.beforeRemote('*.save', function(ctx, user, next) {
if(ctx.user) {
2013-06-05 14:38:04 +00:00
next();
} else {
next(new Error('must be logged in to update'))
}
});
2013-06-12 22:44:38 +00:00
User.afterRemote('*.save', function(ctx, user, next) {
2013-06-11 16:01:44 +00:00
console.log('user has been saved', user);
next();
});
2013-06-12 22:44:38 +00:00
Remote hooks also support wildcards. Run a function before any remote method is called.
// ** will match both prototype.* and *.*
User.beforeRemote('**', function(ctx, user, next) {
console.log(ctx.methodString, 'was invoked remotely'); // users.prototype.save was invoked remotely
next();
});
Other wildcard examples
// run before any static method eg. User.find
2013-06-12 22:44:38 +00:00
User.beforeRemote('*', ...);
// run before any instance method eg. User.prototype.save
User.beforeRemote('prototype.*', ...);
2013-06-13 04:30:20 +00:00
// prevent password hashes from being sent to clients
User.afterRemote('**', function (ctx, user, next) {
if(ctx.result) {
if(Array.isArray(ctx.result)) {
ctx.result.forEach(function (result) {
result.password = undefined;
});
} else {
ctx.result.password = undefined;
}
}
next();
});
2013-06-05 19:13:55 +00:00
#### Context
2013-06-12 22:44:38 +00:00
Remote hooks are provided with a Context `ctx` object which contains transport specific data (eg. for http: `req` and `res`). The `ctx` object also has a set of consistent apis across transports.
2013-06-05 19:13:55 +00:00
2013-06-12 22:44:38 +00:00
##### ctx.user
2013-06-05 19:13:55 +00:00
2013-06-12 22:44:38 +00:00
A `Model` representing the user calling the method remotely. **Note:** this is undefined if the remote method is not invoked by a logged in user.
2013-06-05 19:13:55 +00:00
2013-06-13 04:30:20 +00:00
##### ctx.result
During `afterRemote` hooks, `ctx.result` will contain the data about to be sent to a client. Modify this object to transform data before it is sent.
2013-06-05 19:13:55 +00:00
##### Rest
2013-07-16 17:49:25 +00:00
When [loopback.rest](#loopbackrest) is used the following `ctx` properties are available.
2013-06-05 19:13:55 +00:00
###### ctx.req
The express ServerRequest object. [See full documentation](http://expressjs.com/api.html#req).
###### ctx.res
The express ServerResponse object. [See full documentation](http://expressjs.com/api.html#res).
Access the raw `req` object for the remote method call.
#### Relationships
##### Model.hasMany(Model)
Define a "one to many" relationship.
// by referencing model
Book.hasMany(Chapter);
// specify the name
Book.hasMany('chapters', {model: Chapter});
Query and create the related models.
Book.create(function(err, book) {
2013-06-20 21:17:38 +00:00
// create a chapter instance
// ready to be saved in the data source
var chapter = book.chapters.build({name: 'Chapter 1'});
2013-06-05 19:13:55 +00:00
2013-06-20 21:17:38 +00:00
// save the new chapter
chapter.save();
2013-06-05 19:13:55 +00:00
2013-06-20 21:17:38 +00:00
// you can also call the Chapter.create method with
// the `chapters` property which will build a chapter
// instance and save the it in the data source
book.chapters.create({name: 'Chapter 2'}, function(err, savedChapter) {
// this callback is optional
});
2013-06-05 19:13:55 +00:00
2013-06-20 21:17:38 +00:00
// query chapters for the book using the
2013-06-05 19:13:55 +00:00
book.chapters(function(err, chapters) {
2013-06-20 21:17:38 +00:00
// all chapters with bookId = book.id
console.log(chapters);
2013-06-05 19:13:55 +00:00
});
book.chapters({where: {name: 'test'}, function(err, chapters) {
// all chapters with bookId = book.id and name = 'test'
2013-06-20 21:17:38 +00:00
console.log(chapters);
2013-06-05 19:13:55 +00:00
});
});
2013-06-05 14:38:04 +00:00
#### Shared Methods
2013-07-16 17:49:25 +00:00
Any static or instance method can be decorated as `shared`. These methods are exposed over the provided transport (eg. [loopback.rest](#rest)).
2013-06-05 14:38:04 +00:00
### Data Source
2013-07-17 21:52:21 +00:00
A Loopback `DataSource` provides [Models](#model) with the ability to manipulate data. Attaching a `DataSource` to a `Model` adds [instance methods](#instance-methods) and [static methods](#static-methods) to the `Model`. The added methods may be [remote methods](#remote-methods).
2013-06-05 14:38:04 +00:00
Define a data source for persisting models.
2013-07-16 17:49:25 +00:00
var oracle = loopback.createDataSource({
connector: 'oracle',
2013-06-05 14:38:04 +00:00
host: '111.22.333.44',
database: 'MYDB',
username: 'username',
password: 'password'
2013-04-26 18:44:19 +00:00
});
2013-06-11 16:01:44 +00:00
#### dataSource.createModel(name, properties, options)
2013-04-26 18:44:19 +00:00
2013-06-05 14:38:04 +00:00
Define a model and attach it to a `DataSource`.
2013-04-26 18:44:19 +00:00
2013-06-05 14:38:04 +00:00
var Color = oracle.createModel('color', {name: String});
2013-04-26 18:44:19 +00:00
2013-06-20 21:17:38 +00:00
#### dataSource.discoverModelDefinitions([username], fn)
2013-06-05 19:13:55 +00:00
2013-06-20 21:17:38 +00:00
Discover a set of model definitions (table or collection names) based on tables or collections in a data source.
2013-06-20 17:17:55 +00:00
2013-06-20 21:17:38 +00:00
oracle.discoverModelDefinitions(function (err, models) {
2013-06-20 17:17:55 +00:00
models.forEach(function (def) {
// def.name ~ the model name
oracle.discoverSchema(null, def.name, function (err, schema) {
console.log(schema);
});
});
2013-06-05 19:13:55 +00:00
});
2013-06-05 14:38:04 +00:00
2013-06-20 17:17:55 +00:00
#### dataSource.discoverSchema([owner], name, fn)
2013-06-05 19:13:55 +00:00
2013-06-20 21:17:38 +00:00
Discover the schema of a specific table or collection.
2013-06-11 16:01:44 +00:00
2013-06-20 21:17:38 +00:00
**Example schema from oracle connector:**
2013-06-11 16:01:44 +00:00
2013-06-20 21:17:38 +00:00
{
"name": "Product",
"options": {
"idInjection": false,
"oracle": {
"schema": "BLACKPOOL",
"table": "PRODUCT"
}
},
"properties": {
"id": {
"type": "String",
"required": true,
"length": 20,
"id": 1,
"oracle": {
"columnName": "ID",
"dataType": "VARCHAR2",
"dataLength": 20,
"nullable": "N"
}
},
"name": {
"type": "String",
"required": false,
"length": 64,
"oracle": {
"columnName": "NAME",
"dataType": "VARCHAR2",
"dataLength": 64,
"nullable": "Y"
}
},
"audibleRange": {
"type": "Number",
"required": false,
"length": 22,
"oracle": {
"columnName": "AUDIBLE_RANGE",
"dataType": "NUMBER",
"dataLength": 22,
"nullable": "Y"
}
},
"effectiveRange": {
"type": "Number",
"required": false,
"length": 22,
"oracle": {
"columnName": "EFFECTIVE_RANGE",
"dataType": "NUMBER",
"dataLength": 22,
"nullable": "Y"
}
},
"rounds": {
"type": "Number",
"required": false,
"length": 22,
"oracle": {
"columnName": "ROUNDS",
"dataType": "NUMBER",
"dataLength": 22,
"nullable": "Y"
}
},
"extras": {
"type": "String",
"required": false,
"length": 64,
"oracle": {
"columnName": "EXTRAS",
"dataType": "VARCHAR2",
"dataLength": 64,
"nullable": "Y"
}
},
"fireModes": {
"type": "String",
"required": false,
"length": 64,
"oracle": {
"columnName": "FIRE_MODES",
"dataType": "VARCHAR2",
"dataLength": 64,
"nullable": "Y"
}
}
}
2013-06-11 16:01:44 +00:00
}
2013-06-12 22:44:38 +00:00
#### dataSource.enableRemote(operation)
2013-06-12 22:44:38 +00:00
Enable remote access to a data source operation. Each [connector](#connector) has its own set of set remotely enabled and disabled operations. You can always list these by calling `dataSource.operations()`.
2013-06-12 22:44:38 +00:00
#### dataSource.disableRemote(operation)
2013-06-12 22:44:38 +00:00
Disable remote access to a data source operation. Each [connector](#connector) has its own set of set enabled and disabled operations. You can always list these by calling `dataSource.operations()`.
// all rest data source operations are
// disabled by default
2013-07-16 17:49:25 +00:00
var oracle = loopback.createDataSource({
connector: require('loopback-connector-oracle'),
host: '...',
...
});
// or only disable it as a remote method
oracle.disableRemote('destroyAll');
**Notes:**
- disabled operations will not be added to attached models
- disabling the remoting for a method only affects client access (it will still be available from server models)
- data sources must enable / disable operations before attaching or creating models
#### dataSource.operations()
List the enabled and disabled operations.
console.log(oracle.operations());
Output:
{
find: {
2013-06-11 16:01:44 +00:00
remoteEnabled: true,
accepts: [...],
returns: [...]
enabled: true
},
2013-06-11 16:01:44 +00:00
save: {
remoteEnabled: true,
prototype: true,
accepts: [...],
returns: [...],
enabled: true
},
...
}
2013-06-06 20:30:52 +00:00
#### Connectors
Create a data source with a specific connector. See **available connectors** for specific connector documentation.
2013-06-06 00:11:21 +00:00
2013-07-16 17:49:25 +00:00
var memory = loopback.createDataSource({
connector: loopback.Memory
2013-06-06 00:11:21 +00:00
});
**Available Connectors**
2013-06-06 00:11:21 +00:00
2013-07-16 20:39:03 +00:00
- [In Memory](#memory-connector)
- [REST](http://github.com/strongloop/loopback-connector-rest)
- [Oracle](http://github.com/strongloop/loopback-connector-oracle)
- [MongoDB](http://github.com/strongloop/loopback-connector-mongodb)
- TODO - [MySQL](http://github.com/strongloop/loopback-connector-mysql)
- TODO - [SQLite3](http://github.com/strongloop/loopback-connector-sqlite)
- TODO - [Postgres](http://github.com/strongloop/loopback-connector-postgres)
- TODO - [Redis](http://github.com/strongloop/loopback-connector-redis)
- TODO - [CouchDB](http://github.com/strongloop/loopback-connector-couch)
- TODO - [Firebird](http://github.com/strongloop/loopback-connector-firebird)
**Installing Connectors**
Include the connector in your package.json dependencies and run `npm install`.
{
"dependencies": {
2013-07-16 17:49:25 +00:00
"loopback-connector-oracle": "latest"
}
}
2013-06-06 00:11:21 +00:00
2013-07-16 20:39:03 +00:00
##### Memory Connector
The built-in memory connector allows you to test your application without connecting to an actual persistent data source, such as a database. Although the memory connector is very well tested it is not recommended to be used in production. Creating a data source using the memory connector is very simple.
// use the built in memory function
// to create a memory data source
var memory = loopback.memory();
// or create it using the standard
// data source creation api
var memory = loopback.createDataSource({
connector: loopback.Memory
});
// create a model using the
// memory data source
var properties = {
name: String,
price: Number
};
var Product = memory.createModel('product', properties);
Product.create([
{name: 'apple', price: 0.79},
{name: 'pear', price: 1.29},
{name: 'orange', price: 0.59},
], count);
function count() {
Product.count(console.log); // 3
}
###### Operations
**CRUD / Query**
The memory connector supports all the standard [query and crud operations](#crud-and-query-mixins) to allow you to test your models against an in memory data source.
**GeoPoint Filtering**
The memory connector also supports geo-filtering when using the `find()` operation with an attached model. See [GeoPoint](#geopoint) for more information on geo-filtering.
2013-06-24 23:30:09 +00:00
### GeoPoint
Use the `GeoPoint` class.
2013-07-16 17:49:25 +00:00
var GeoPoint = require('loopback').GeoPoint;
2013-06-05 14:38:04 +00:00
2013-06-05 19:13:55 +00:00
Embed a latitude / longitude point in a [Model](#model).
2013-07-16 17:49:25 +00:00
var CoffeeShop = loopback.createModel('coffee-shop', {
2013-06-05 19:13:55 +00:00
location: 'GeoPoint'
});
2013-07-16 17:49:25 +00:00
Loopback Model's with a GeoPoint property and an attached DataSource may be queried using geo spatial filters and sorting.
2013-06-05 19:13:55 +00:00
Find the 3 nearest coffee shops.
2013-06-24 22:22:25 +00:00
CoffeeShop.attachTo(oracle);
2013-06-24 22:32:22 +00:00
var here = new GeoPoint({lat: 10.32424, lng: 5.84978});
CoffeeShop.find({where: {location: {near: here}}, limit:3}, function(err, nearbyShops) {
2013-06-05 19:13:55 +00:00
console.info(nearbyShops); // [CoffeeShop, ...]
});
2013-06-05 14:38:04 +00:00
2013-06-05 19:13:55 +00:00
#### geoPoint.distanceTo(geoPoint, options)
2013-06-05 14:38:04 +00:00
2013-06-05 19:13:55 +00:00
Get the distance to another `GeoPoint`.
2013-06-05 14:38:04 +00:00
2013-06-24 22:32:22 +00:00
var here = new GeoPoint({lat: 10, lng: 10});
var there = new GeoPoint({lat: 5, lng: 5});
2013-06-05 19:13:55 +00:00
console.log(here.distanceTo(there, {type: 'miles'})); // 438
#### GeoPoint.distanceBetween(a, b, options)
2013-06-05 14:38:04 +00:00
2013-06-05 19:13:55 +00:00
Get the distance between two points.
2013-06-05 14:38:04 +00:00
2013-06-05 19:13:55 +00:00
GeoPoint.distanceBetween(here, there, {type: 'miles'}) // 438
2013-06-05 14:38:04 +00:00
2013-06-05 19:13:55 +00:00
#### Distance Types
2013-06-05 14:38:04 +00:00
2013-06-24 23:30:09 +00:00
**Note:** all distance methods use `miles` by default.
2013-06-07 16:49:33 +00:00
- `miles`
- `radians`
- `kilometers`
2013-06-24 23:30:09 +00:00
- `meters`
- `miles`
- `feet`
- `degrees`
2013-06-05 14:38:04 +00:00
2013-06-05 19:13:55 +00:00
#### geoPoint.lat
2013-06-05 14:38:04 +00:00
2013-06-05 19:13:55 +00:00
The latitude point in degrees. Range: -90 to 90.
2013-04-09 16:02:36 +00:00
2013-06-24 22:32:22 +00:00
#### geoPoint.lng
2013-04-09 16:02:36 +00:00
2013-06-05 19:13:55 +00:00
The longitude point in degrees. Range: -180 to 180.
2013-05-20 21:31:04 +00:00
2013-07-16 17:49:25 +00:00
### Loopback Types
2013-05-20 21:31:04 +00:00
2013-07-16 17:49:25 +00:00
Various APIs in Loopback accept type descriptions (eg. [remote methods](#remote-methods), [loopback.createModel()](#model)). The following is a list of supported types.
2013-05-20 21:31:04 +00:00
2013-06-05 19:13:55 +00:00
- `null` - JSON null
- `Boolean` - JSON boolean
- `Number` - JSON number
- `String` - JSON string
- `Object` - JSON object
- `Array` - JSON array
- `Date` - a JavaScript date object
- `Buffer` - a node.js Buffer object
2013-07-17 21:52:21 +00:00
- [GeoPoint](#geopoint) - A Loopback GeoPoint object.
2013-06-26 03:29:58 +00:00
## Bundled Models
2013-07-01 00:38:53 +00:00
2013-07-16 17:49:25 +00:00
The Loopback library is unopinioned in the way you define your app's data and logic. Loopback also bundles useful pre-built models for common use cases.
2013-07-01 00:38:53 +00:00
- User - register and authenticate users of your app locally or against 3rd party services.
- Email - send emails to your app users using smtp or 3rd party services.
2013-07-01 18:46:41 +00:00
2013-07-16 17:49:25 +00:00
Defining a model with `loopback.createModel()` is really just extending the base `loopback.Model` type using `loopback.Model.extend()`. The bundled models extend from the base `loopback.Model` allowing you to extend them arbitrarily.
2013-07-01 00:38:53 +00:00
2013-06-26 03:29:58 +00:00
### User Model
2013-07-01 00:38:53 +00:00
Register and authenticate users of your app locally or against 3rd party services.
#### Create a User Model
2013-07-16 17:49:25 +00:00
Extend a vanilla Loopback model using the built in User model.
2013-06-26 03:29:58 +00:00
// create a data source
2013-07-16 17:49:25 +00:00
var memory = loopback.memory();
2013-06-26 03:29:58 +00:00
// define a User model
2013-07-16 17:49:25 +00:00
var User = loopback.User.extend('user');
2013-07-01 00:38:53 +00:00
2013-06-26 03:29:58 +00:00
// attach to the memory connector
User.attachTo(memory);
// also attach the session model to a data source
User.session.attachTo(memory);
2013-07-01 00:38:53 +00:00
// expose over the app's api
app.model(User);
2013-07-16 17:49:25 +00:00
**Note:** By default the `loopback.User` model uses the `loopback.Session` model to persist sessions. You can change this by setting the `session` property.
**Note:** You must attach both the `User` and `User.session` model's to a data source!
// define a custom session model
2013-07-16 17:49:25 +00:00
var MySession = loopback.Session.extend('my-session');
// define a custom User model
2013-07-16 17:49:25 +00:00
var User = loopback.User.extend('user');
// use the custom session model
User.session = MySession;
// attaching to
2013-07-01 00:38:53 +00:00
#### User Creation
Create a user like any other model.
// username and password are not required
User.create({email: 'foo@bar.com', password: 'bar'}, function(err, user) {
console.log(user);
});
#### Authentication Strategies (Using Passport.js)
Setup an authentication strategy.
[See all available providers from passport.js](http://passportjs.org/guide/providers/).
// first add your model to your app
app.model(User);
// by default your User model has a local strategy similar to below
// customize your own
// disable the default local strategy
User.useLocalStrategy(false);
// create a custom strategy
var LocalStrategy = require('passport-local').Strategy;
2013-07-01 23:50:03 +00:00
passport.use(new LocalStrategy(function(username, password, done) {
2013-07-01 00:38:53 +00:00
User.findOne({ username: username }, function(err, user) {
if (err) { return done(err); }
if (!user) { return done(null, false, { message: 'Unknown user ' + username }); }
user.comparePassword(password, function(err, isMatch) {
if (err) return done(err);
if(isMatch) {
return done(null, user);
} else {
return done(null, false, { message: 'Invalid password' });
}
});
});
}));
#### Login a User
2013-07-03 05:37:31 +00:00
Create a session for a user.
2013-07-01 00:38:53 +00:00
User.login({username: 'foo', password: 'bar'}, function(err, session) {
console.log(session);
});
**REST**
You must provide a username and password over rest. To ensure these values are encrypted, include these as part of the body and make sure you are serving your app over https (through a proxy or using the https node server).
POST
/users/login
...
{
"email": "foo@bar.com",
"password": "bar"
}
...
200 OK
{
"sid": "1234abcdefg",
"uid": "123"
}
#### Logout a User
**NODE**
// login a user and logout
User.login({"email": "foo@bar.com", "password": "bar"}, function(err, session) {
User.logout(session.id, function(err) {
// user logged out
});
});
// logout a user (server side only)
User.findOne({email: 'foo@bar.com'}, function(err, user) {
user.logout();
});
**REST**
2013-07-01 00:38:53 +00:00
2013-07-03 05:37:31 +00:00
**Note:** When calling this method remotely, the first argument will be populated with the current user's id. If the caller is not logged in the method will fail with an error status code `401`.
2013-07-01 00:38:53 +00:00
#### Verify Email Addresses
2013-07-03 05:37:31 +00:00
Require a user to verify their email address before being able to login. This will send an email to the user containing a link to verify their address. Once the user follows the link they will be redirected to `/` and be able to login normally.
User.requireEmailVerfication = true;
User.afterRemote('create', function(ctx, user, next) {
var options = {
type: 'email',
to: user.email,
from: 'noreply@myapp.com',
subject: 'Thanks for Registering at FooBar',
text: 'Please verify your email address!'
template: 'verify.ejs',
redirect: '/'
};
user.verify(options, next);
});
2013-07-01 00:38:53 +00:00
#### Send Reset Password Email
Send an email to the user's supplied email address containing a link to reset their password.
2013-07-03 05:37:31 +00:00
User.reset(email, function(err) {
console.log('email sent');
2013-06-26 03:29:58 +00:00
});
2013-07-01 00:38:53 +00:00
#### Remote Password Reset
2013-07-16 17:49:25 +00:00
The password reset email will send users to a page rendered by loopback with fields required to reset the user's password. You may customize this template by defining a `resetTemplate` setting.
2013-07-03 05:37:31 +00:00
User.settings.resetTemplate = 'reset.ejs';
#### Remote Password Reset Confirmation
Confirm the password reset.
User.confirmReset(token, function(err) {
console.log(err || 'your password was reset');
});
2013-06-26 03:29:58 +00:00
### Session Model
2013-07-16 17:49:25 +00:00
Identify users by creating sessions when they connect to your loopback app. By default the `loopback.User` model uses the `loopback.Session` model to persist sessions. You can change this by setting the `session` property.
// define a custom session model
2013-07-16 17:49:25 +00:00
var MySession = loopback.Session.extend('my-session');
// define a custom User model
2013-07-16 17:49:25 +00:00
var User = loopback.User.extend('user');
// use the custom session model
User.session = MySession;
// attach both Session and User to a data source
2013-07-16 17:49:25 +00:00
User.attachTo(loopback.memory());
MySession.attachTo(loopback.memory());
2013-06-26 03:29:58 +00:00
### Email Model
2013-07-16 17:49:25 +00:00
Send emails from your loopback app.
2013-06-26 03:29:58 +00:00
2013-06-06 20:50:46 +00:00
### REST Router
2013-06-06 00:11:21 +00:00
2013-07-16 17:49:25 +00:00
Expose models over rest using the `loopback.rest` router.
2013-06-06 00:11:21 +00:00
2013-07-16 17:49:25 +00:00
app.use(loopback.rest());
2013-06-06 00:11:21 +00:00
2013-06-06 20:50:46 +00:00
**REST Documentation**
View generated REST documentation by visiting: [http://localhost:3000/_docs](http://localhost:3000/_docs).
2013-06-06 00:11:21 +00:00
### SocketIO Middleware **Not Available**
2013-07-16 17:49:25 +00:00
**Coming Soon** - Expose models over socket.io using the `loopback.sio()` middleware.
2013-06-06 00:11:21 +00:00
2013-07-16 17:49:25 +00:00
app.use(loopback.sio);
2013-06-06 00:11:21 +00:00