2016-05-03 22:50:21 +00:00
|
|
|
// Copyright IBM Corp. 2014,2016. All Rights Reserved.
|
|
|
|
// Node module: loopback
|
|
|
|
// This file is licensed under the MIT License.
|
|
|
|
// License text available at https://opensource.org/licenses/MIT
|
|
|
|
|
2016-11-15 21:46:23 +00:00
|
|
|
'use strict';
|
2017-02-24 14:11:06 +00:00
|
|
|
|
|
|
|
const assert = require('assert');
|
|
|
|
|
2015-04-01 21:50:36 +00:00
|
|
|
module.exports = function(registry) {
|
2014-10-10 09:53:22 +00:00
|
|
|
// NOTE(bajtos) we must use static require() due to browserify limitations
|
|
|
|
|
2016-08-08 15:24:17 +00:00
|
|
|
registry.KeyValueModel = createModel(
|
|
|
|
require('../common/models/key-value-model.json'),
|
|
|
|
require('../common/models/key-value-model.js'));
|
|
|
|
|
2015-04-01 21:50:36 +00:00
|
|
|
registry.Email = createModel(
|
2014-10-10 11:49:21 +00:00
|
|
|
require('../common/models/email.json'),
|
|
|
|
require('../common/models/email.js'));
|
2014-10-10 09:53:22 +00:00
|
|
|
|
2015-04-01 21:50:36 +00:00
|
|
|
registry.Application = createModel(
|
2014-10-13 06:41:31 +00:00
|
|
|
require('../common/models/application.json'),
|
|
|
|
require('../common/models/application.js'));
|
|
|
|
|
2015-04-01 21:50:36 +00:00
|
|
|
registry.AccessToken = createModel(
|
2014-10-13 08:23:35 +00:00
|
|
|
require('../common/models/access-token.json'),
|
|
|
|
require('../common/models/access-token.js'));
|
|
|
|
|
2015-05-29 23:44:18 +00:00
|
|
|
registry.User = createModel(
|
2015-03-13 15:50:30 +00:00
|
|
|
require('../common/models/user.json'),
|
|
|
|
require('../common/models/user.js'));
|
|
|
|
|
2015-04-01 21:50:36 +00:00
|
|
|
registry.RoleMapping = createModel(
|
2014-10-13 09:20:04 +00:00
|
|
|
require('../common/models/role-mapping.json'),
|
|
|
|
require('../common/models/role-mapping.js'));
|
|
|
|
|
2015-04-01 21:50:36 +00:00
|
|
|
registry.Role = createModel(
|
2014-10-13 09:31:27 +00:00
|
|
|
require('../common/models/role.json'),
|
|
|
|
require('../common/models/role.js'));
|
2014-10-13 08:55:08 +00:00
|
|
|
|
2015-04-01 21:50:36 +00:00
|
|
|
registry.ACL = createModel(
|
2014-10-13 08:55:08 +00:00
|
|
|
require('../common/models/acl.json'),
|
|
|
|
require('../common/models/acl.js'));
|
2014-10-13 08:46:55 +00:00
|
|
|
|
2015-04-01 21:50:36 +00:00
|
|
|
registry.Scope = createModel(
|
2014-10-13 08:46:55 +00:00
|
|
|
require('../common/models/scope.json'),
|
|
|
|
require('../common/models/scope.js'));
|
2014-10-10 09:53:22 +00:00
|
|
|
|
2015-04-01 21:50:36 +00:00
|
|
|
registry.Change = createModel(
|
2014-10-13 09:58:14 +00:00
|
|
|
require('../common/models/change.json'),
|
|
|
|
require('../common/models/change.js'));
|
2014-10-13 09:45:06 +00:00
|
|
|
|
2015-04-01 21:50:36 +00:00
|
|
|
registry.Checkpoint = createModel(
|
2014-10-13 09:45:06 +00:00
|
|
|
require('../common/models/checkpoint.json'),
|
|
|
|
require('../common/models/checkpoint.js'));
|
2014-10-09 14:51:12 +00:00
|
|
|
|
2014-10-10 09:53:22 +00:00
|
|
|
function createModel(definitionJson, customizeFn) {
|
2017-02-24 14:11:06 +00:00
|
|
|
// Clone the JSON definition to allow applications
|
|
|
|
// to modify model settings while not affecting
|
|
|
|
// settings of new models created in the local registry
|
|
|
|
// of another app.
|
|
|
|
// This is needed because require() always returns the same
|
|
|
|
// object instance it loaded during the first call.
|
|
|
|
definitionJson = cloneDeepJson(definitionJson);
|
|
|
|
|
2015-04-01 21:50:36 +00:00
|
|
|
var Model = registry.createModel(definitionJson);
|
2014-10-10 09:53:22 +00:00
|
|
|
customizeFn(Model);
|
|
|
|
return Model;
|
|
|
|
}
|
2014-10-09 14:51:12 +00:00
|
|
|
};
|
2017-02-24 14:11:06 +00:00
|
|
|
|
|
|
|
// Because we are cloning objects created by JSON.parse,
|
|
|
|
// the cloning algorithm can stay much simpler than a general-purpose
|
|
|
|
// "cloneDeep" e.g. from lodash.
|
|
|
|
function cloneDeepJson(obj) {
|
|
|
|
const result = Array.isArray(obj) ? [] : {};
|
|
|
|
assert.equal(Object.getPrototypeOf(result), Object.getPrototypeOf(obj));
|
|
|
|
for (const key in obj) {
|
|
|
|
const value = obj[key];
|
|
|
|
if (typeof value === 'object') {
|
|
|
|
result[key] = cloneDeepJson(value);
|
|
|
|
} else {
|
|
|
|
result[key] = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|