/* Where is defined the DB connection.
 *
 * We got the following collections (cf. schema.js):
 *  - User
 *  - Client
 *  - Grant
 *
 */

require.paths.unshift(__dirname + "/../vendors/rest-mongo/src")

var events = require('events')

  , rest_mongo = require("rest-mongo/core")
  , mongo_backend = require("rest-mongo/mongo_backend")
  , config = require('./lib/config_loader').get_config()
  , schema = require('./schema').schema
  , server_schema = require('./server_schema').schema
  ;


var backend = mongo_backend.get_backend(config.db);
exports.RFactory = rest_mongo.getRFactory(schema, backend, {
  additional_schema: server_schema
});
// The RFactory to serve public data:
exports.RFactoryPublic = rest_mongo.getRFactory(schema, backend);


// Ensure indexes are created:
backend.db.createIndex('User', 'email', true, function(){}); // email is unique


// To ensure DB concistency:
var emitter = exports.emitter = new events.EventEmitter();

// When removing a client, remove its authorizations
emitter.on('DELETE:Client', function(ids) {
  var R = RFactory();
  R.Authorization.remove({query: {
    client: {'$in': ids.map(function(id){return {id: id}})}
  }});
});


/* Where is defined the DB connection.
 *
 * We got the following collections (cf. schema.js):
 *  - User
 *  - Client
 *  - Grant
 *
 */

require.paths.unshift(__dirname + "/../vendors/rest-mongo/src")

var events = require('events')

  , rest_mongo = require("rest-mongo/core")
  , mongo_backend = require("rest-mongo/mongo_backend")
  , config = require('./lib/config_loader').get_config()
  , schema = require('./schema').schema
  , server_schema = require('./server_schema').schema
  ;


var backend = mongo_backend.get_backend(config.db);
exports.RFactory = rest_mongo.getRFactory(schema, backend, {
  additional_schema: server_schema
});
// The RFactory to serve public data:
exports.RFactoryPublic = rest_mongo.getRFactory(schema, backend);


// Ensure indexes are created:
backend.db.createIndex('User', 'email', true, function(){}); // email is unique


// To ensure DB concistency:
var emitter = exports.emitter = new events.EventEmitter();

// When removing a client, remove its authorizations
emitter.on('DELETE:Client', function(ids) {
  var R = exports.RFactory();
  R.Authorization.remove({query: {
    client: {'$in': ids.map(function(id){return {id: id}})}
  }});
});


Fix delete client: authorizations should be removed.
Fix delete client: authorizations should be removed.

Signed-off-by: François de Metz <4073ce8ed0c8af431a3bf625d935cc2c9542ae9c@af83.com>
Signed-off-by: Virtuo <ff019a5748a52b5641624af88a54a2f0e46a9fb5@ruyssen.fr>
