rxdb
Version:
A local-first realtime NoSQL Database for JavaScript applications - https://rxdb.info/
87 lines (83 loc) • 3.19 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.createNewRxDocument = createNewRxDocument;
exports.getDocumentOrmPrototype = getDocumentOrmPrototype;
exports.getDocumentPrototype = getDocumentPrototype;
exports.getRxDocumentConstructor = getRxDocumentConstructor;
var _rxDocument = require("./rx-document.js");
var _hooks = require("./hooks.js");
var _overwritable = require("./overwritable.js");
var _index = require("./plugins/utils/index.js");
/**
* For the ORM capabilities,
* we have to merge the document prototype
* with the ORM functions and the data
* We do this iterating over the properties and
* adding them to a new object.
* In the future we should do this by chaining the __proto__ objects
*/
var constructorForCollection = new WeakMap();
function getDocumentPrototype(rxCollection) {
var schemaProto = rxCollection.schema.getDocumentPrototype();
var ormProto = getDocumentOrmPrototype(rxCollection);
var baseProto = _rxDocument.basePrototype;
var proto = {};
[schemaProto, ormProto, baseProto].forEach(obj => {
var props = Object.getOwnPropertyNames(obj);
props.forEach(key => {
var desc = Object.getOwnPropertyDescriptor(obj, key);
/**
* When enumerable is true, it will show on console dir(instance)
* To not pollute the output, only getters and methods are enumerable
*/
var enumerable = true;
if (key.startsWith('_') || key.endsWith('_') || key.startsWith('$') || key.endsWith('$')) enumerable = false;
if (typeof desc.value === 'function') {
// when getting a function, we automatically do a .bind(this)
Object.defineProperty(proto, key, {
get() {
return desc.value.bind(this);
},
enumerable,
configurable: false
});
} else {
desc.enumerable = enumerable;
desc.configurable = false;
if (desc.writable) desc.writable = false;
Object.defineProperty(proto, key, desc);
}
});
});
return proto;
}
function getRxDocumentConstructor(rxCollection) {
return (0, _index.getFromMapOrCreate)(constructorForCollection, rxCollection, () => (0, _rxDocument.createRxDocumentConstructor)(getDocumentPrototype(rxCollection)));
}
/**
* Create a RxDocument-instance from the jsonData
* and the prototype merge.
* You should never call this method directly,
* instead you should get the document from collection._docCache.getCachedRxDocument().
*/
function createNewRxDocument(rxCollection, documentConstructor, docData) {
var doc = (0, _rxDocument.createWithConstructor)(documentConstructor, rxCollection, _overwritable.overwritable.deepFreezeWhenDevMode(docData));
rxCollection._runHooksSync('post', 'create', docData, doc);
(0, _hooks.runPluginHooks)('postCreateRxDocument', doc);
return doc;
}
/**
* returns the prototype-object
* that contains the orm-methods,
* used in the proto-merge
*/
function getDocumentOrmPrototype(rxCollection) {
var proto = {};
Object.entries(rxCollection.methods).forEach(([k, v]) => {
proto[k] = v;
});
return proto;
}
//# sourceMappingURL=rx-document-prototype-merge.js.map
;