Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
2a30efe
Remove app.modelBuilder()
ritch Jul 1, 2013
643877b
Add initial User model
ritch Jul 1, 2013
c14ef9a
Add default user properties
ritch Jul 2, 2013
2f13c53
Initial users
ritch Jul 2, 2013
42766a2
Add basic email verification
ritch Jul 3, 2013
29d1077
Add schema skeletons for built-in models
Jun 26, 2013
7b8e940
Add normalized properties to Models
ritch Jun 28, 2013
184ecfe
Fix type in docs
ritch Jun 28, 2013
3a15d7d
Fix type in docs
ritch Jun 28, 2013
7f54457
Update README.md
cgole Jun 28, 2013
4aac427
Add more user model docs
ritch Jul 1, 2013
90d990b
Remove app.modelBuilder()
ritch Jul 1, 2013
1abe583
Add initial User model
ritch Jul 1, 2013
5f16585
Add default user properties
ritch Jul 2, 2013
8387a68
Initial users
ritch Jul 2, 2013
fc0777d
Add basic email verification
ritch Jul 3, 2013
e68116b
Merge branch 'users' into 0.9
ritch Jul 12, 2013
a22cf5f
Update docs and add asteroid.memory() sugar api
ritch Jul 12, 2013
78457fb
Updated generated test docs
ritch Jul 12, 2013
9a62c48
Tests README
ritch Jul 12, 2013
a09b527
Create 64 byte session ids
ritch Jul 12, 2013
16617a3
Add user.logout() sugar method and update logout docs
ritch Jul 12, 2013
d9b5dab
Validate uniqueness and format of User email.
ritch Jul 13, 2013
acfaee2
Remove data argument name from user tests
ritch Jul 15, 2013
3f7e4b6
Refactor Model into class. Make createModel() just sugar.
ritch Jul 15, 2013
aa8d1bb
Added bcrypt for password hashing
ritch Jul 15, 2013
49da6f4
Fix login bug.
ritch Jul 16, 2013
fbf0e35
Remove updateAttribute as remote method
ritch Jul 16, 2013
4be6ebc
Fix inheritance bug
ritch Jul 16, 2013
407a50f
Fix model remoting issue.
ritch Jul 16, 2013
2f77311
rename asteroid to loopback
Jul 16, 2013
1059a7b
Add renamed files
Jul 16, 2013
1970f12
Rename jugglingdb to loopback-data
Jul 16, 2013
d128ed4
Remove remote option object
ritch Jul 16, 2013
cba94a2
Add memory docs and test
ritch Jul 16, 2013
253d42a
Cleanup test markdown
ritch Jul 16, 2013
0b5e0d4
More cleanup for test/README.md
ritch Jul 16, 2013
6b1ac1a
Add fields documentation
ritch Jul 17, 2013
a2308cb
Merge 0.9 into master
ritch Jul 17, 2013
7c76671
Manually merge rest adapter
ritch Jul 17, 2013
f7791fc
Manually merge application
ritch Jul 17, 2013
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
329 changes: 236 additions & 93 deletions README.md

Large diffs are not rendered by default.

92 changes: 15 additions & 77 deletions lib/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,6 @@ app.remotes = function () {
}
}

/**
* Expose an object or Class remotely.
*
* @param {String} name The remote namespace (eg. url base)
* @param {Object|Function} obj The object to remote
*/

app.remote = function (name, obj) {
// add the object to the remote exports
this.remotes().exports[name] = obj;

// clear the handlers cache
this._handlers = {};
}

/**
* Remove a route by reference.
*/
Expand All @@ -54,20 +39,11 @@ app.disuse = function (route) {
}
}

/**
* Get ModelBuilder.
*/

app.modelBuilder = function () {
return this._modelBuilder || (this._modelBuilder = new ModelBuilder())
}

/**
* App models.
*/

app._models = [];
app._services = [];

/**
* Expose a model.
Expand All @@ -76,17 +52,10 @@ app._services = [];
*/

app.model = function (Model) {
var remotes = this.remotes();

this._models.push(Model);
Model.shared = true;
Model.app = this;
if(Model._remoteHooks) {
Model._remoteHooks.emit('attached', app);
}

// add to the remote exports
this.remote(Model.pluralModelName, Model);
Model.emit('attached', this);
}

/**
Expand All @@ -98,28 +67,22 @@ app.models = function () {
}

/**
* Expose a service.
*
* @param {String} name
* @param {Service} service
* Get all remote objects.
*/

app.service = function (name, service) {
this._services.push(service);
service.shared = true;

service.app = this;
app.remoteObjects = function () {
var result = {};
var models = this.models();

// add to the remote exports
this.remote(name, service);
}

/**
* Get all exposed services.
*/

app.services = function () {
return this._services;
// add in models
models.forEach(function (ModelCtor) {
// only add shared models
if(ModelCtor.shared && typeof ModelCtor.sharedCtor === 'function') {
result[ModelCtor.pluralModelName] = ModelCtor;
}
});

return result;
}

/**
Expand All @@ -128,29 +91,4 @@ app.services = function () {

app.remotes = function () {
return this._remotes || (this._remotes = RemoteObjects.create());
}

/**
* Get a remotes handler.
*/

app.handler = function (type) {
var handler = this._handlers[type];

if(!handler) {
// get the sl remoting object
var remotes = this.remotes();

// create and save the handler
handler = this._handlers[type] = remotes.handler(type);
}

return handler;
}

/*!
* Handlers
*/

app._handlers = {};

}
140 changes: 48 additions & 92 deletions lib/loopback.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

var express = require('express')
, fs = require('fs')
, ejs = require('ejs')
, EventEmitter = require('events').EventEmitter
, path = require('path')
, proto = require('./application')
Expand Down Expand Up @@ -150,98 +151,7 @@ loopback.createDataSource = function (name, options) {
*/

loopback.createModel = function (name, properties, options) {
assert(typeof name === 'string', 'Cannot create a model without a name');

var mb = new ModelBuilder();
var ModelCtor = mb.define(name, properties, options);

ModelCtor.shared = true;
ModelCtor.sharedCtor = function (data, id, fn) {
if(typeof data === 'function') {
fn = data;
data = null;
id = null;
} else if (typeof id === 'function') {
fn = id;

if(typeof data !== 'object') {
id = data;
data = null;
} else {
id = null;
}
}

if(id && data) {
var model = new ModelCtor(data);
model.id = id;
fn(null, model);
} else if(data) {
fn(null, new ModelCtor(data));
} else if(id) {
ModelCtor.find(id, function (err, model) {
if(err) {
fn(err);
} else if(model) {
fn(null, model);
} else {
err = new Error('could not find a model with id ' + id);
err.statusCode = 404;

fn(err);
}
});
} else {
fn(new Error('must specify an id or data'));
}
};

ModelCtor.sharedCtor.accepts = [
{arg: 'data', type: 'object', http: {source: 'body'}},
{arg: 'id', type: 'any', http: {source: 'url'}}
];

ModelCtor.sharedCtor.http = [
{path: '/'},
{path: '/:id'}
];

// before remote hook
ModelCtor.beforeRemote = function (name, fn) {
var self = this;
if(this.app) {
var remotes = this.app.remotes();
remotes.before(self.pluralModelName + '.' + name, function (ctx, next) {
fn(ctx, ctx.instance, next);
});
} else {
var args = arguments;
this._remoteHooks.once('attached', function () {
self.beforeRemote.apply(ModelCtor, args);
});
}
}

// after remote hook
ModelCtor.afterRemote = function (name, fn) {
var self = this;
if(this.app) {
var remotes = this.app.remotes();
remotes.after(self.pluralModelName + '.' + name, function (ctx, next) {
fn(ctx, ctx.instance, next);
});
} else {
var args = arguments;
this._remoteHooks.once('attached', function () {
self.afterRemote.apply(ModelCtor, args);
});
}
}

// allow hooks to be added before attaching to an app
ModelCtor._remoteHooks = new EventEmitter();

return ModelCtor;
return loopback.Model.extend(name, properties, options);
}

/**
Expand All @@ -260,3 +170,49 @@ loopback.remoteMethod = function (fn, options) {
fn.http = fn.http || {verb: 'get'};
}

/**
* Create a template helper.
*
* var render = loopback.template('foo.ejs');
* var html = render({foo: 'bar'});
*
* @param {String} path Path to the template file.
* @returns {Function}
*/

loopback.template = function (file) {
var templates = this._templates || (this._templates = {});
var str = templates[file] || (templates[file] = fs.readFileSync(file, 'utf8'));
return ejs.compile(str);
}

/**
* Get an in-memory data source. Use one if it already exists.
*
* @param {String} [name] The name of the data source. If not provided, the `'default'` is used.
*/

loopback.memory = function (name) {
name = name || 'default';
var memory = (
this._memoryDataSources
|| (this._memoryDataSources = {})
)[name];

if(!memory) {
memory = this._memoryDataSources[name] = loopback.createDataSource({
connector: loopback.Memory
});
}

return memory;
}

/*
* Built in models / services
*/

loopback.Model = require('./models/model');
loopback.Email = require('./models/email');
loopback.User = require('./models/user');
loopback.Session = require('./models/session');
34 changes: 34 additions & 0 deletions lib/middleware/auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Module dependencies.
*/

var loopback = require('../loopback')
, passport = require('passport');

/**
* Export the middleware.
*/

module.exports = auth;

/**
* Build a temp app for mounting resources.
*/

function auth() {
return function (req, res, next) {
var sub = loopback();

// TODO clean this up
sub._models = req.app._models;
sub._remotes = req.app._remotes;

sub.use(loopback.session({secret: 'change me'}))
sub.use(passport.initialize());
sub.use(passport.session());

sub.handle(req, res, next);
}
}


14 changes: 11 additions & 3 deletions lib/middleware/rest.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,24 @@ module.exports = rest;

function rest() {
return function (req, res, next) {
var handler = req.app.handler('rest');
var app = req.app;
var remotes = app.remotes();

// get all remote objects
var objs = app.remoteObjects();

// export remote objects
remotes.exports = objs;

var handler = remotes.handler('rest');

if(req.url === '/routes') {
res.send(handler.adapter.allRoutes());
} else if(req.url === '/models') {
return res.send(req.app.remotes().toJSON());
return res.send(remotes.toJSON());
} else {
handler(req, res, next);
}
}
}


Loading