yekonga-server
Version:
Yekonga Server
128 lines (109 loc) • 4.7 kB
JavaScript
// @ts-nocheck
/*global Yekonga, serverLibrary */
const H = Yekonga.Helper;
// @ts-ignore
const systemCollections = require('./database.json');
class SystemDataController {
constructor(schema) {
if (!Array.isArray(schema)) schema = [];
let duplicate = [];
let reservedForSystem = [];
this.schema = [];
Yekonga.SystemCollections = [];
Yekonga.SystemCollectionNames = [];
for (const row of systemCollections) {
reservedForSystem.push(row._id.collection);
Yekonga.SystemCollectionNames.push(row._id.collection);
this.schema.push(row);
}
for (const row of schema) {
if (!Yekonga.SystemCollectionNames.includes(row._id.collection)) {
Yekonga.SystemCollectionNames.push(row._id.collection);
this.schema.push(row);
} else if (reservedForSystem.includes(row._id.collection)) {
let collection = this.getCollection(row._id.collection);
for (const key in row) {
if (!collection[key]) {
collection[key] = row[key];
} else if (collection[key].options && row[key].options) {
if (Array.isArray(collection[key].options)) {
if (Array.isArray(row[key].options)) {
for (const option of row[key].options) {
if (!collection[key].options.includes(option)) {
collection[key].options.push(option);
}
}
} else {
for (const option in row[key].options) {
if (!collection[key].options.includes(option)) {
collection[key].options.push(option);
}
}
}
} else {
if (Array.isArray(row[key].options)) {
for (const option of row[key].options) {
if (!collection[key].options[option]) {
collection[key].options[option] = option;
}
}
} else {
for (const option in row[key].options) {
if (!collection[key].options[option]) {
collection[key].options[option] = option;
}
}
}
}
}
}
for (let i = 0; i < this.schema.length; i++) {
if (this.schema[i]._id.collection == collection._id.collection) {
this.schema[i] = collection;
break;
}
}
} else {
duplicate.push(row._id.collection);
}
}
if (duplicate.length) {
throw new Error(`Duplicate collection/table <${duplicate.join(',')}>`);
}
}
getCollection(collect) {
for (const row of systemCollections) {
if (row._id.collection == collect) return Yekonga.Helper.copyJson(row);
}
}
getFields(collect) {
let list = [];
let collection = this.getCollection(collect);
for (const key in collection) {
list.push(key);
}
return list;
}
setDataByProfileId() {
const queryByProfileId = {};
}
setWhereByProfileId(model, parentKey = "") {
const targetCollection = model._id.collection.startsWith('_') ?
model._id.collection.substr(1) :
model._id.collection;
const targetSecondaryKey = H.getVariable(`${H.toSingular(H.getUnderscore(targetCollection))}_id`);
for (const row of this.schema) {
const collection = row._id.collection.startsWith('_') ?
row._id.collection.substr(1) :
row._id.collection;
const secondaryKey = H.getVariable(`${H.toSingular(H.getUnderscore(collection))}_id`);
if (row['profileId']) {
} else {
}
}
}
}
module.exports = SystemDataController;
// Object.defineProperty(Yekonga.Model, "User", {
// get: function() { return Yekonga.Model['__users__']; }
// });