realm-js/lib/objects.js

45 lines
1022 B
JavaScript
Raw Normal View History

2015-10-28 17:37:17 +00:00
/* Copyright 2015 Realm Inc - All Rights Reserved
* Proprietary and Confidential
*/
'use strict';
const constants = require('./constants');
const util = require('./util');
const {keys} = constants;
const registeredConstructors = {};
module.exports = {
create,
registerConstructors,
};
function create(realmId, info) {
let schema = info.schema;
2015-10-06 07:52:15 +00:00
let constructor = (registeredConstructors[realmId] || {})[schema.name];
let object = constructor ? Object.create(constructor.prototype) : {};
let props = {};
object[keys.realm] = realmId;
object[keys.id] = info.id;
object[keys.type] = info.type;
schema.properties.forEach((prop) => {
let name = prop.name;
props[name] = {
get: util.getterForProperty(name),
set: util.setterForProperty(name),
};
});
Object.defineProperties(object, props);
return object;
}
2015-10-06 07:52:15 +00:00
function registerConstructors(realmId, constructors) {
registeredConstructors[realmId] = constructors;
}