@oldendev/plugins
Advanced tools
Comparing version
@@ -11,13 +11,16 @@ 'use strict'; | ||
for (const name of names) { | ||
const entity = model.entities.get(name); | ||
returinings[name] = Object.keys(entity.fields); | ||
} | ||
for (const [entity, methods] of Object.entries(entities)) { | ||
const channel = channels.get(entity); | ||
const schema = model.entities.get(entity); | ||
const keys = returinings[entity] = Object.keys(schema.fields); | ||
const manys = []; | ||
const ones = []; | ||
keys.forEach((key) => { | ||
const { one, many } = schema.fields[key]; | ||
if (one) ones.push({ [key]: one }); | ||
if (many) manys.push({ [key]: many }); | ||
}); | ||
if (methods.includes('schema')) { | ||
iface['schema' + entity] = (context) => async () => | ||
model.entities.get(entity).toJSON(); | ||
iface['schema' + entity] = (context) => async () => schema.toJSON(); | ||
} | ||
@@ -58,2 +61,12 @@ | ||
const result = await db.pg.row(entity, ...conditions); | ||
if (ones.length > 0) { | ||
for (const one of ones) { | ||
for (const [field, table] of Object.entries(one)) { | ||
const { toLowerCamel } = metarhia.metautil; | ||
const tableIdName = toLowerCamel(table) + 'Id'; | ||
const id = result[field + 'Id']; | ||
result[field] = await db.pg.row(table, { [tableIdName]: id }); | ||
} | ||
} | ||
} | ||
return result; | ||
@@ -68,2 +81,14 @@ }; | ||
const result = await db.pg.select(entity, fields, ...conditions); | ||
for (const row of result) { | ||
if (ones.length > 0) { | ||
for (const one of ones) { | ||
for (const [field, table] of Object.entries(one)) { | ||
const { toLowerCamel } = metarhia.metautil; | ||
const tableIdName = toLowerCamel(table) + 'Id'; | ||
const id = row[field + 'Id']; | ||
row[field] = await db.pg.row(table, { [tableIdName]: id }); | ||
} | ||
} | ||
} | ||
} | ||
return result; | ||
@@ -70,0 +95,0 @@ }; |
{ | ||
"name": "@oldendev/plugins", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"description": "CRUD plugin for Metarhia impress, metasql", | ||
@@ -5,0 +5,0 @@ "main": "plugins.js", |
9846
13.07%152
20.63%