36 lines
1.3 KiB
JavaScript
36 lines
1.3 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.traverseProjection = exports.makeProjection = void 0;
|
|
const Entity_1 = require("../types/Entity");
|
|
const relation_1 = require("../store/relation");
|
|
function makeProjection(entity, schema) {
|
|
const { attributes } = schema[entity];
|
|
const attrs = Object.keys(attributes);
|
|
attrs.push(Entity_1.PrimaryKeyAttribute, Entity_1.CreateAtAttribute, Entity_1.UpdateAtAttribute, Entity_1.DeleteAtAttribute);
|
|
const projection = {};
|
|
attrs.forEach((attr) => Object.assign(projection, {
|
|
[attr]: 1,
|
|
}));
|
|
return projection;
|
|
}
|
|
exports.makeProjection = makeProjection;
|
|
function traverseProjection(entity, schema, projection, callback) {
|
|
const access = (entity2, proj) => {
|
|
callback(entity2, proj);
|
|
for (const attr in proj) {
|
|
const rel = (0, relation_1.judgeRelation)(schema, entity2, attr);
|
|
if (rel === 2) {
|
|
access(attr, proj[attr]);
|
|
}
|
|
else if (typeof rel === 'string') {
|
|
access(rel, proj[attr]);
|
|
}
|
|
else if (rel instanceof Array) {
|
|
access(rel[0], proj[attr].data);
|
|
}
|
|
}
|
|
};
|
|
access(entity, projection);
|
|
}
|
|
exports.traverseProjection = traverseProjection;
|