2022-10-24 01:13:21 +00:00
|
|
|
import { Cache } from '@nocobase/cache';
|
2022-12-18 01:20:56 +00:00
|
|
|
import { Repository, Transactionable } from '@nocobase/database';
|
2022-01-19 02:09:30 +00:00
|
|
|
import { uid } from '@nocobase/utils';
|
2022-02-11 10:13:14 +00:00
|
|
|
import lodash from 'lodash';
|
2022-01-19 02:09:30 +00:00
|
|
|
import { Transaction } from 'sequelize';
|
2022-02-11 10:13:14 +00:00
|
|
|
import { ChildOptions, SchemaNode, TargetPosition } from './dao/ui_schema_node_dao';
|
2022-01-19 02:09:30 +00:00
|
|
|
|
2022-10-24 01:13:21 +00:00
|
|
|
export interface GetJsonSchemaOptions {
|
2022-01-19 02:09:30 +00:00
|
|
|
includeAsyncNode?: boolean;
|
2022-10-24 01:13:21 +00:00
|
|
|
readFromCache?: boolean;
|
|
|
|
transaction?: Transaction;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface GetPropertiesOptions {
|
|
|
|
readFromCache?: boolean;
|
2022-01-19 02:09:30 +00:00
|
|
|
transaction?: Transaction;
|
|
|
|
}
|
|
|
|
|
2022-02-18 04:29:03 +00:00
|
|
|
type BreakRemoveOnType = {
|
|
|
|
[key: string]: any;
|
|
|
|
};
|
|
|
|
|
2022-05-22 00:48:19 +00:00
|
|
|
export interface removeParentOptions extends Transactionable {
|
2022-02-18 04:29:03 +00:00
|
|
|
removeParentsIfNoChildren?: boolean;
|
|
|
|
breakRemoveOn?: BreakRemoveOnType;
|
|
|
|
}
|
|
|
|
|
2022-03-20 12:04:07 +00:00
|
|
|
interface InsertAdjacentOptions extends removeParentOptions {
|
|
|
|
wrap?: any;
|
|
|
|
}
|
2022-02-18 04:29:03 +00:00
|
|
|
|
2022-01-19 02:09:30 +00:00
|
|
|
const nodeKeys = ['properties', 'definitions', 'patternProperties', 'additionalProperties', 'items'];
|
|
|
|
|
2022-03-16 10:45:20 +00:00
|
|
|
function transaction(transactionAbleArgPosition?: number) {
|
|
|
|
return (target: any, propertyKey: string, descriptor: PropertyDescriptor) => {
|
|
|
|
const originalMethod = descriptor.value;
|
|
|
|
|
|
|
|
descriptor.value = async function (...args) {
|
|
|
|
if (!lodash.isNumber(transactionAbleArgPosition)) {
|
|
|
|
transactionAbleArgPosition = originalMethod.length - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
let transaction = lodash.get(args, [transactionAbleArgPosition, 'transaction']);
|
|
|
|
let handleTransaction = false;
|
|
|
|
if (!transaction) {
|
|
|
|
transaction = await this.database.sequelize.transaction();
|
|
|
|
handleTransaction = true;
|
|
|
|
|
|
|
|
lodash.set(args, transactionAbleArgPosition, {
|
|
|
|
...lodash.get(args, transactionAbleArgPosition, {}),
|
|
|
|
transaction,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (handleTransaction) {
|
|
|
|
try {
|
|
|
|
const results = await originalMethod.apply(this, args);
|
|
|
|
await transaction.commit();
|
|
|
|
return results;
|
|
|
|
} catch (e) {
|
|
|
|
await transaction.rollback();
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return await originalMethod.apply(this, args);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return descriptor;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-02-11 10:13:14 +00:00
|
|
|
export class UiSchemaRepository extends Repository {
|
2022-10-24 01:13:21 +00:00
|
|
|
cache: Cache;
|
|
|
|
|
|
|
|
// if you need to handle cache in repo method, so you must set cache first
|
|
|
|
setCache(cache: Cache) {
|
|
|
|
this.cache = cache;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* clear cache with xUid which in uiSchemaTreePath's Path
|
|
|
|
* @param {string} xUid
|
|
|
|
* @param {Transaction} transaction
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
|
|
|
async clearXUidPathCache(xUid: string, transaction: Transaction) {
|
|
|
|
if (!this.cache || !xUid) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// find all xUid node's parent nodes
|
|
|
|
const uiSchemaNodes = await this.database.getRepository('uiSchemaTreePath').find({
|
|
|
|
filter: {
|
|
|
|
descendant: xUid,
|
|
|
|
},
|
|
|
|
transaction: transaction,
|
|
|
|
});
|
|
|
|
for (const uiSchemaNode of uiSchemaNodes) {
|
|
|
|
await this.cache.del(`p_${uiSchemaNode['ancestor']}`);
|
|
|
|
await this.cache.del(`s_${uiSchemaNode['ancestor']}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-20 12:01:07 +00:00
|
|
|
tableNameAdapter(tableName) {
|
2022-02-15 16:22:47 +00:00
|
|
|
if (this.database.sequelize.getDialect() === 'postgres') {
|
2022-02-20 12:01:07 +00:00
|
|
|
return `"${tableName}"`;
|
2022-02-15 16:22:47 +00:00
|
|
|
}
|
2022-02-20 12:01:07 +00:00
|
|
|
return tableName;
|
|
|
|
}
|
|
|
|
|
|
|
|
get uiSchemasTableName() {
|
|
|
|
return this.tableNameAdapter(this.model.tableName);
|
2022-02-15 16:22:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get uiSchemaTreePathTableName() {
|
2022-02-18 04:29:03 +00:00
|
|
|
const model = this.database.getCollection('uiSchemaTreePath').model;
|
2022-02-20 12:01:07 +00:00
|
|
|
return this.tableNameAdapter(model.tableName);
|
2022-02-15 16:22:47 +00:00
|
|
|
}
|
|
|
|
|
2022-02-19 17:23:04 +00:00
|
|
|
sqlAdapter(sql: string) {
|
|
|
|
if (this.database.sequelize.getDialect() === 'mysql') {
|
|
|
|
return lodash.replace(sql, /"/g, '`');
|
|
|
|
}
|
|
|
|
|
|
|
|
return sql;
|
|
|
|
}
|
|
|
|
|
2022-01-19 02:09:30 +00:00
|
|
|
static schemaToSingleNodes(schema: any, carry: SchemaNode[] = [], childOptions: ChildOptions = null): SchemaNode[] {
|
|
|
|
const node = lodash.cloneDeep(
|
|
|
|
lodash.isString(schema)
|
|
|
|
? {
|
|
|
|
'x-uid': schema,
|
|
|
|
}
|
|
|
|
: schema,
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!lodash.get(node, 'name')) {
|
|
|
|
node.name = uid();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!lodash.get(node, 'x-uid')) {
|
|
|
|
node['x-uid'] = uid();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (childOptions) {
|
|
|
|
node.childOptions = childOptions;
|
|
|
|
}
|
|
|
|
|
|
|
|
carry.push(node);
|
|
|
|
|
|
|
|
for (const nodeKey of nodeKeys) {
|
|
|
|
const nodeProperty = lodash.get(node, nodeKey);
|
2022-03-20 12:04:07 +00:00
|
|
|
const childNodeChildOptions = {
|
|
|
|
parentUid: node['x-uid'],
|
|
|
|
parentPath: [node['x-uid'], ...lodash.get(childOptions, 'parentPath', [])],
|
|
|
|
type: nodeKey,
|
|
|
|
};
|
2022-01-19 02:09:30 +00:00
|
|
|
|
|
|
|
// array items
|
|
|
|
if (nodeKey === 'items' && nodeProperty) {
|
|
|
|
const handleItems = lodash.isArray(nodeProperty) ? nodeProperty : [nodeProperty];
|
2022-03-20 12:04:07 +00:00
|
|
|
for (const [i, item] of handleItems.entries()) {
|
|
|
|
carry = this.schemaToSingleNodes(item, carry, { ...childNodeChildOptions, sort: i + 1 });
|
2022-01-19 02:09:30 +00:00
|
|
|
}
|
|
|
|
} else if (lodash.isPlainObject(nodeProperty)) {
|
|
|
|
const subNodeNames = lodash.keys(lodash.get(node, nodeKey));
|
|
|
|
|
|
|
|
delete node[nodeKey];
|
|
|
|
|
2022-03-20 12:04:07 +00:00
|
|
|
for (const [i, subNodeName] of subNodeNames.entries()) {
|
2022-01-19 02:09:30 +00:00
|
|
|
const subSchema = {
|
|
|
|
name: subNodeName,
|
|
|
|
...lodash.get(nodeProperty, subNodeName),
|
|
|
|
};
|
|
|
|
|
2022-03-20 12:04:07 +00:00
|
|
|
carry = this.schemaToSingleNodes(subSchema, carry, { ...childNodeChildOptions, sort: i + 1 });
|
2022-01-19 02:09:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return carry;
|
|
|
|
}
|
|
|
|
|
2022-10-24 01:13:21 +00:00
|
|
|
async getProperties(uid: string, options: GetPropertiesOptions = {}) {
|
|
|
|
if (options?.readFromCache && this.cache) {
|
|
|
|
return this.cache.wrap(`p_${uid}`, () => {
|
|
|
|
return this.doGetProperties(uid, options);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return this.doGetProperties(uid, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
private async doGetProperties(uid: string, options: GetPropertiesOptions = {}) {
|
2022-03-21 06:51:23 +00:00
|
|
|
const { transaction } = options;
|
|
|
|
|
2022-01-19 02:09:30 +00:00
|
|
|
const db = this.database;
|
|
|
|
|
|
|
|
const rawSql = `
|
2022-02-19 17:23:04 +00:00
|
|
|
SELECT "SchemaTable"."x-uid" as "x-uid", "SchemaTable"."name" as "name", "SchemaTable"."schema" as "schema",
|
2022-01-19 02:09:30 +00:00
|
|
|
TreePath.depth as depth,
|
2022-02-11 04:55:30 +00:00
|
|
|
NodeInfo.type as type, NodeInfo.async as async, ParentPath.ancestor as parent, ParentPath.sort as sort
|
2022-02-15 16:22:47 +00:00
|
|
|
FROM ${this.uiSchemaTreePathTableName} as TreePath
|
2022-02-19 17:23:04 +00:00
|
|
|
LEFT JOIN ${this.uiSchemasTableName} as "SchemaTable" ON "SchemaTable"."x-uid" = TreePath.descendant
|
|
|
|
LEFT JOIN ${this.uiSchemaTreePathTableName} as NodeInfo ON NodeInfo.descendant = "SchemaTable"."x-uid" and NodeInfo.descendant = NodeInfo.ancestor and NodeInfo.depth = 0
|
|
|
|
LEFT JOIN ${this.uiSchemaTreePathTableName} as ParentPath ON (ParentPath.descendant = "SchemaTable"."x-uid" AND ParentPath.depth = 1)
|
2022-01-19 02:09:30 +00:00
|
|
|
WHERE TreePath.ancestor = :ancestor AND (NodeInfo.async = false or TreePath.depth = 1)`;
|
|
|
|
|
2022-02-19 17:23:04 +00:00
|
|
|
const nodes = await db.sequelize.query(this.sqlAdapter(rawSql), {
|
2022-01-19 02:09:30 +00:00
|
|
|
replacements: {
|
|
|
|
ancestor: uid,
|
|
|
|
},
|
2022-03-21 06:51:23 +00:00
|
|
|
transaction,
|
2022-01-19 02:09:30 +00:00
|
|
|
});
|
|
|
|
|
2022-02-13 02:36:25 +00:00
|
|
|
if (nodes[0].length == 0) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-01-19 02:09:30 +00:00
|
|
|
const schema = this.nodesToSchema(nodes[0], uid);
|
|
|
|
return lodash.pick(schema, ['type', 'properties']);
|
|
|
|
}
|
|
|
|
|
2022-10-24 01:13:21 +00:00
|
|
|
private async doGetJsonSchema(uid: string, options?: GetJsonSchemaOptions) {
|
2022-01-19 02:09:30 +00:00
|
|
|
const db = this.database;
|
|
|
|
|
2022-02-15 16:22:47 +00:00
|
|
|
const treeTable = this.uiSchemaTreePathTableName;
|
2022-01-19 02:09:30 +00:00
|
|
|
|
|
|
|
const rawSql = `
|
2022-02-19 17:23:04 +00:00
|
|
|
SELECT "SchemaTable"."x-uid" as "x-uid", "SchemaTable"."name" as name, "SchemaTable"."schema" as "schema" ,
|
2022-01-19 02:09:30 +00:00
|
|
|
TreePath.depth as depth,
|
|
|
|
NodeInfo.type as type, NodeInfo.async as async, ParentPath.ancestor as parent, ParentPath.sort as sort
|
|
|
|
FROM ${treeTable} as TreePath
|
2022-02-19 17:23:04 +00:00
|
|
|
LEFT JOIN ${this.uiSchemasTableName} as "SchemaTable" ON "SchemaTable"."x-uid" = TreePath.descendant
|
|
|
|
LEFT JOIN ${treeTable} as NodeInfo ON NodeInfo.descendant = "SchemaTable"."x-uid" and NodeInfo.descendant = NodeInfo.ancestor and NodeInfo.depth = 0
|
|
|
|
LEFT JOIN ${treeTable} as ParentPath ON (ParentPath.descendant = "SchemaTable"."x-uid" AND ParentPath.depth = 1)
|
2022-01-19 02:09:30 +00:00
|
|
|
WHERE TreePath.ancestor = :ancestor ${options?.includeAsyncNode ? '' : 'AND (NodeInfo.async != true )'}
|
|
|
|
`;
|
|
|
|
|
2022-02-19 17:23:04 +00:00
|
|
|
const nodes = await db.sequelize.query(this.sqlAdapter(rawSql), {
|
2022-01-19 02:09:30 +00:00
|
|
|
replacements: {
|
|
|
|
ancestor: uid,
|
|
|
|
},
|
|
|
|
transaction: options?.transaction,
|
|
|
|
});
|
|
|
|
|
2022-02-13 02:36:25 +00:00
|
|
|
if (nodes[0].length == 0) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-10-24 01:13:21 +00:00
|
|
|
return this.nodesToSchema(nodes[0], uid);
|
|
|
|
}
|
|
|
|
|
|
|
|
async getJsonSchema(uid: string, options?: GetJsonSchemaOptions): Promise<any> {
|
|
|
|
if (options?.readFromCache && this.cache) {
|
|
|
|
return this.cache.wrap(`s_${uid}`, () => {
|
|
|
|
return this.doGetJsonSchema(uid, options);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return this.doGetJsonSchema(uid, options);
|
2022-01-19 02:09:30 +00:00
|
|
|
}
|
|
|
|
|
2022-02-20 12:01:07 +00:00
|
|
|
private ignoreSchemaProperties(schemaProperties) {
|
|
|
|
return lodash.omit(schemaProperties, nodeKeys);
|
|
|
|
}
|
|
|
|
|
2022-01-19 02:09:30 +00:00
|
|
|
nodesToSchema(nodes, rootUid) {
|
|
|
|
const nodeAttributeSanitize = (node) => {
|
|
|
|
const schema = {
|
2022-02-20 12:01:07 +00:00
|
|
|
...this.ignoreSchemaProperties(lodash.isPlainObject(node.schema) ? node.schema : JSON.parse(node.schema)),
|
2022-01-19 02:09:30 +00:00
|
|
|
...lodash.pick(node, [...nodeKeys, 'name']),
|
2022-02-19 17:23:04 +00:00
|
|
|
['x-uid']: node['x-uid'],
|
2022-01-19 02:09:30 +00:00
|
|
|
['x-async']: !!node.async,
|
|
|
|
};
|
|
|
|
|
2022-02-13 01:38:41 +00:00
|
|
|
if (lodash.isNumber(node.sort)) {
|
|
|
|
schema['x-index'] = node.sort;
|
|
|
|
}
|
|
|
|
|
2022-01-19 02:09:30 +00:00
|
|
|
return schema;
|
|
|
|
};
|
|
|
|
|
|
|
|
const buildTree = (rootNode) => {
|
2022-02-19 17:23:04 +00:00
|
|
|
const children = nodes.filter((node) => node.parent == rootNode['x-uid']);
|
2022-01-19 02:09:30 +00:00
|
|
|
|
|
|
|
if (children.length > 0) {
|
|
|
|
const childrenGroupByType = lodash.groupBy(children, 'type');
|
|
|
|
|
|
|
|
for (const childType of Object.keys(childrenGroupByType)) {
|
|
|
|
const properties = childrenGroupByType[childType]
|
|
|
|
.map((child) => buildTree(child))
|
|
|
|
.sort((a, b) => a['x-index'] - b['x-index']) as any;
|
|
|
|
|
|
|
|
rootNode[childType] =
|
|
|
|
childType == 'items'
|
|
|
|
? properties.length == 1
|
|
|
|
? properties[0]
|
|
|
|
: properties
|
|
|
|
: properties.reduce((carry, item) => {
|
|
|
|
carry[item.name] = item;
|
|
|
|
delete item['name'];
|
|
|
|
return carry;
|
|
|
|
}, {});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nodeAttributeSanitize(rootNode);
|
|
|
|
};
|
|
|
|
|
2022-02-19 17:23:04 +00:00
|
|
|
return buildTree(nodes.find((node) => node['x-uid'] == rootUid));
|
2022-01-19 02:09:30 +00:00
|
|
|
}
|
|
|
|
|
2022-03-16 10:45:20 +00:00
|
|
|
@transaction()
|
2022-05-22 00:48:19 +00:00
|
|
|
async clearAncestor(uid: string, options?: Transactionable) {
|
2022-10-24 01:13:21 +00:00
|
|
|
await this.clearXUidPathCache(uid, options?.transaction);
|
2022-03-16 10:45:20 +00:00
|
|
|
const db = this.database;
|
|
|
|
const treeTable = this.uiSchemaTreePathTableName;
|
|
|
|
|
|
|
|
await db.sequelize.query(
|
|
|
|
`DELETE
|
|
|
|
FROM ${treeTable}
|
|
|
|
WHERE descendant IN
|
|
|
|
(SELECT descendant FROM (SELECT descendant FROM ${treeTable} WHERE ancestor = :uid) as descendantTable)
|
|
|
|
AND ancestor IN (SELECT ancestor
|
|
|
|
FROM (SELECT ancestor FROM ${treeTable} WHERE descendant = :uid AND ancestor != descendant) as ancestorTable)
|
|
|
|
`,
|
|
|
|
{
|
|
|
|
type: 'DELETE',
|
|
|
|
replacements: {
|
|
|
|
uid,
|
|
|
|
},
|
|
|
|
transaction: options.transaction,
|
|
|
|
},
|
|
|
|
);
|
2022-01-19 02:09:30 +00:00
|
|
|
}
|
|
|
|
|
2022-03-16 10:45:20 +00:00
|
|
|
@transaction()
|
2022-01-19 02:09:30 +00:00
|
|
|
async patch(newSchema: any, options?) {
|
2022-03-16 10:45:20 +00:00
|
|
|
const { transaction } = options;
|
2022-01-19 02:09:30 +00:00
|
|
|
const rootUid = newSchema['x-uid'];
|
2022-10-24 01:13:21 +00:00
|
|
|
await this.clearXUidPathCache(rootUid, transaction);
|
2022-12-18 01:20:56 +00:00
|
|
|
if (!newSchema['properties']) {
|
|
|
|
const s = await this.model.findByPk(rootUid, { transaction });
|
2023-01-09 07:41:08 +00:00
|
|
|
s.set('schema', { ...s.toJSON(), ...newSchema });
|
2022-12-18 01:20:56 +00:00
|
|
|
// console.log(s.toJSON());
|
|
|
|
await s.save({ transaction, hooks: false });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const oldTree = await this.getJsonSchema(rootUid, { transaction });
|
2022-01-19 02:09:30 +00:00
|
|
|
const traverSchemaTree = async (schema, path = []) => {
|
|
|
|
const node = schema;
|
|
|
|
const oldNode = path.length == 0 ? oldTree : lodash.get(oldTree, path);
|
|
|
|
const oldNodeUid = oldNode['x-uid'];
|
|
|
|
|
|
|
|
await this.updateNode(oldNodeUid, node, transaction);
|
|
|
|
|
|
|
|
const properties = node.properties;
|
|
|
|
if (lodash.isPlainObject(properties)) {
|
|
|
|
for (const name of Object.keys(properties)) {
|
|
|
|
await traverSchemaTree(properties[name], [...path, 'properties', name]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-03-16 10:45:20 +00:00
|
|
|
await traverSchemaTree(newSchema);
|
2022-01-19 02:09:30 +00:00
|
|
|
}
|
|
|
|
|
2022-08-20 10:04:14 +00:00
|
|
|
@transaction()
|
|
|
|
async batchPatch(schemas: any[], options?) {
|
|
|
|
const { transaction } = options;
|
|
|
|
for (const schema of schemas) {
|
|
|
|
await this.patch(schema, { ...options, transaction });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-24 01:13:21 +00:00
|
|
|
protected async updateNode(uid: string, schema: any, transaction?: Transaction) {
|
2022-01-19 02:09:30 +00:00
|
|
|
const nodeModel = await this.findOne({
|
|
|
|
filter: {
|
2022-02-19 17:23:04 +00:00
|
|
|
'x-uid': uid,
|
2022-01-19 02:09:30 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
await nodeModel.update(
|
|
|
|
{
|
|
|
|
schema: {
|
|
|
|
...(nodeModel.get('schema') as any),
|
|
|
|
...lodash.omit(schema, ['x-async', 'name', 'x-uid', 'properties']),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
hooks: false,
|
|
|
|
transaction,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-02-18 04:29:03 +00:00
|
|
|
protected async childrenCount(uid, transaction) {
|
2022-02-10 12:28:27 +00:00
|
|
|
const db = this.database;
|
|
|
|
|
2022-02-18 04:29:03 +00:00
|
|
|
const countResult = await db.sequelize.query(
|
|
|
|
`SELECT COUNT(*) as count FROM ${this.uiSchemaTreePathTableName} where ancestor = :ancestor and depth = 1`,
|
|
|
|
{
|
|
|
|
replacements: {
|
|
|
|
ancestor: uid,
|
|
|
|
},
|
|
|
|
type: 'SELECT',
|
|
|
|
transaction,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
return parseInt(countResult[0]['count']);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected async isLeafNode(uid, transaction) {
|
|
|
|
const childrenCount = await this.childrenCount(uid, transaction);
|
|
|
|
return childrenCount === 0;
|
|
|
|
}
|
|
|
|
|
2022-10-24 01:13:21 +00:00
|
|
|
protected async findParentUid(uid, transaction?) {
|
2022-02-18 04:29:03 +00:00
|
|
|
const parent = await this.database.getRepository('uiSchemaTreePath').findOne({
|
2022-02-10 12:28:27 +00:00
|
|
|
filter: {
|
|
|
|
descendant: uid,
|
|
|
|
depth: 1,
|
|
|
|
},
|
2022-02-15 14:32:02 +00:00
|
|
|
transaction,
|
2022-02-10 12:28:27 +00:00
|
|
|
});
|
|
|
|
|
2022-02-18 04:29:03 +00:00
|
|
|
return parent ? (parent.get('ancestor') as string) : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected async findNodeSchemaWithParent(uid, transaction) {
|
|
|
|
const schema = await this.database.getRepository('uiSchemas').findOne({
|
|
|
|
filter: {
|
2022-02-19 17:23:04 +00:00
|
|
|
'x-uid': uid,
|
2022-02-18 04:29:03 +00:00
|
|
|
},
|
|
|
|
transaction,
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
parentUid: await this.findParentUid(uid, transaction),
|
|
|
|
schema,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
protected async isSingleChild(uid, transaction) {
|
|
|
|
const db = this.database;
|
|
|
|
|
|
|
|
const parent = await this.findParentUid(uid, transaction);
|
|
|
|
|
2022-02-10 13:06:22 +00:00
|
|
|
if (!parent) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-02-18 04:29:03 +00:00
|
|
|
const parentChildrenCount = await this.childrenCount(parent, transaction);
|
2022-02-10 12:28:27 +00:00
|
|
|
|
|
|
|
if (parentChildrenCount == 1) {
|
2022-02-15 16:22:47 +00:00
|
|
|
const schema = await db.getRepository('uiSchemas').findOne({
|
2022-02-10 13:06:22 +00:00
|
|
|
filter: {
|
2022-02-19 17:23:04 +00:00
|
|
|
'x-uid': parent,
|
2022-02-10 13:06:22 +00:00
|
|
|
},
|
2022-02-15 14:32:02 +00:00
|
|
|
transaction,
|
2022-02-10 13:06:22 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return schema;
|
2022-02-10 12:28:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-05-22 00:48:19 +00:00
|
|
|
async removeEmptyParents(options: Transactionable & { uid: string; breakRemoveOn?: BreakRemoveOnType }) {
|
2022-02-18 04:29:03 +00:00
|
|
|
const { transaction, uid, breakRemoveOn } = options;
|
2022-02-10 12:28:27 +00:00
|
|
|
|
2022-10-24 01:13:21 +00:00
|
|
|
await this.clearXUidPathCache(uid, transaction);
|
|
|
|
|
2022-02-10 12:28:27 +00:00
|
|
|
const removeParent = async (nodeUid: string) => {
|
2022-02-10 13:06:22 +00:00
|
|
|
const parent = await this.isSingleChild(nodeUid, transaction);
|
|
|
|
|
2022-02-18 04:29:03 +00:00
|
|
|
if (parent && !this.breakOnMatched(parent, breakRemoveOn)) {
|
2022-02-19 17:23:04 +00:00
|
|
|
await removeParent(parent.get('x-uid') as string);
|
2022-02-10 12:28:27 +00:00
|
|
|
} else {
|
|
|
|
await this.remove(nodeUid, { transaction });
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
await removeParent(uid);
|
|
|
|
}
|
|
|
|
|
2022-02-18 04:29:03 +00:00
|
|
|
private breakOnMatched(schemaInstance, breakRemoveOn: BreakRemoveOnType): boolean {
|
|
|
|
if (!breakRemoveOn) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const key of Object.keys(breakRemoveOn)) {
|
|
|
|
const instanceValue = schemaInstance.get(key);
|
|
|
|
const breakRemoveOnValue = breakRemoveOn[key];
|
|
|
|
if (instanceValue !== breakRemoveOnValue) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-05-22 00:48:19 +00:00
|
|
|
async recursivelyRemoveIfNoChildren(options: Transactionable & { uid: string; breakRemoveOn?: BreakRemoveOnType }) {
|
2022-02-18 04:29:03 +00:00
|
|
|
const { uid, transaction, breakRemoveOn } = options;
|
|
|
|
|
2022-10-24 01:13:21 +00:00
|
|
|
await this.clearXUidPathCache(uid, transaction);
|
|
|
|
|
2022-02-18 04:29:03 +00:00
|
|
|
const removeLeafNode = async (nodeUid: string) => {
|
|
|
|
const isLeafNode = await this.isLeafNode(nodeUid, transaction);
|
|
|
|
|
|
|
|
if (isLeafNode) {
|
|
|
|
const { parentUid, schema } = await this.findNodeSchemaWithParent(nodeUid, transaction);
|
|
|
|
|
|
|
|
if (this.breakOnMatched(schema, breakRemoveOn)) {
|
|
|
|
// break at here
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
// remove current node
|
|
|
|
await this.remove(nodeUid, {
|
|
|
|
transaction,
|
|
|
|
});
|
|
|
|
// continue remove
|
|
|
|
await removeLeafNode(parentUid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
await removeLeafNode(uid);
|
|
|
|
}
|
|
|
|
|
2022-03-16 10:45:20 +00:00
|
|
|
@transaction()
|
2022-05-22 00:48:19 +00:00
|
|
|
async remove(uid: string, options?: Transactionable & removeParentOptions) {
|
2022-03-16 10:45:20 +00:00
|
|
|
let { transaction } = options;
|
2022-01-19 02:09:30 +00:00
|
|
|
|
2022-10-24 01:13:21 +00:00
|
|
|
await this.clearXUidPathCache(uid, transaction);
|
2022-03-16 10:45:20 +00:00
|
|
|
if (options?.removeParentsIfNoChildren) {
|
|
|
|
await this.removeEmptyParents({ transaction, uid, breakRemoveOn: options.breakRemoveOn });
|
|
|
|
return;
|
2022-01-19 02:09:30 +00:00
|
|
|
}
|
|
|
|
|
2022-03-16 10:45:20 +00:00
|
|
|
await this.database.sequelize.query(
|
|
|
|
this.sqlAdapter(`DELETE FROM ${this.uiSchemasTableName} WHERE "x-uid" IN (
|
2022-02-20 12:01:07 +00:00
|
|
|
SELECT descendant FROM ${this.uiSchemaTreePathTableName} WHERE ancestor = :uid
|
|
|
|
)`),
|
2022-03-16 10:45:20 +00:00
|
|
|
{
|
|
|
|
replacements: {
|
|
|
|
uid,
|
2022-01-19 02:09:30 +00:00
|
|
|
},
|
2022-03-16 10:45:20 +00:00
|
|
|
transaction,
|
|
|
|
},
|
|
|
|
);
|
2022-02-20 12:01:07 +00:00
|
|
|
|
2022-03-16 10:45:20 +00:00
|
|
|
await this.database.sequelize.query(
|
|
|
|
` DELETE FROM ${this.uiSchemaTreePathTableName}
|
2022-01-19 02:09:30 +00:00
|
|
|
WHERE descendant IN (
|
|
|
|
select descendant FROM
|
|
|
|
(SELECT descendant
|
2022-02-20 12:01:07 +00:00
|
|
|
FROM ${this.uiSchemaTreePathTableName}
|
2022-01-19 02:09:30 +00:00
|
|
|
WHERE ancestor = :uid)as descendantTable) `,
|
2022-03-16 10:45:20 +00:00
|
|
|
{
|
|
|
|
replacements: {
|
|
|
|
uid,
|
2022-01-19 02:09:30 +00:00
|
|
|
},
|
2022-03-16 10:45:20 +00:00
|
|
|
transaction,
|
|
|
|
},
|
|
|
|
);
|
2022-01-19 02:09:30 +00:00
|
|
|
}
|
|
|
|
|
2022-03-16 10:45:20 +00:00
|
|
|
@transaction()
|
2022-10-24 01:13:21 +00:00
|
|
|
protected async insertBeside(
|
|
|
|
targetUid: string,
|
|
|
|
schema: any,
|
|
|
|
side: 'before' | 'after',
|
|
|
|
options?: InsertAdjacentOptions,
|
|
|
|
) {
|
2022-03-17 07:54:27 +00:00
|
|
|
const { transaction } = options;
|
|
|
|
const targetParent = await this.findParentUid(targetUid, transaction);
|
2022-01-19 02:09:30 +00:00
|
|
|
|
|
|
|
const db = this.database;
|
2022-02-18 04:29:03 +00:00
|
|
|
|
2022-02-15 16:22:47 +00:00
|
|
|
const treeTable = this.uiSchemaTreePathTableName;
|
2022-02-18 04:29:03 +00:00
|
|
|
|
2022-01-19 02:09:30 +00:00
|
|
|
const typeQuery = await db.sequelize.query(`SELECT type from ${treeTable} WHERE ancestor = :uid AND depth = 0;`, {
|
|
|
|
type: 'SELECT',
|
|
|
|
replacements: {
|
|
|
|
uid: targetUid,
|
|
|
|
},
|
2022-03-17 07:54:27 +00:00
|
|
|
transaction,
|
2022-01-19 02:09:30 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const nodes = UiSchemaRepository.schemaToSingleNodes(schema);
|
|
|
|
|
|
|
|
const rootNode = nodes[0];
|
|
|
|
|
|
|
|
rootNode.childOptions = {
|
2022-02-18 04:29:03 +00:00
|
|
|
parentUid: targetParent,
|
2022-01-19 02:09:30 +00:00
|
|
|
type: typeQuery[0]['type'],
|
|
|
|
position: {
|
|
|
|
type: side,
|
|
|
|
target: targetUid,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2022-02-18 04:29:03 +00:00
|
|
|
const insertedNodes = await this.insertNodes(nodes, options);
|
2022-03-17 07:54:27 +00:00
|
|
|
return await this.getJsonSchema(insertedNodes[0].get('x-uid'), {
|
|
|
|
transaction,
|
|
|
|
});
|
2022-01-19 02:09:30 +00:00
|
|
|
}
|
|
|
|
|
2022-03-16 10:45:20 +00:00
|
|
|
@transaction()
|
2022-10-24 01:13:21 +00:00
|
|
|
protected async insertInner(
|
|
|
|
targetUid: string,
|
|
|
|
schema: any,
|
|
|
|
position: 'first' | 'last',
|
|
|
|
options?: InsertAdjacentOptions,
|
|
|
|
) {
|
2022-03-17 07:54:27 +00:00
|
|
|
const { transaction } = options;
|
|
|
|
|
2022-01-19 02:09:30 +00:00
|
|
|
const nodes = UiSchemaRepository.schemaToSingleNodes(schema);
|
|
|
|
const rootNode = nodes[0];
|
2022-02-18 04:29:03 +00:00
|
|
|
|
2022-01-19 02:09:30 +00:00
|
|
|
rootNode.childOptions = {
|
|
|
|
parentUid: targetUid,
|
|
|
|
type: lodash.get(schema, 'x-node-type', 'properties'),
|
|
|
|
position,
|
|
|
|
};
|
|
|
|
|
2022-02-18 04:29:03 +00:00
|
|
|
const insertedNodes = await this.insertNodes(nodes, options);
|
2022-03-20 12:04:07 +00:00
|
|
|
|
2022-03-17 07:54:27 +00:00
|
|
|
return await this.getJsonSchema(insertedNodes[0].get('x-uid'), {
|
|
|
|
transaction,
|
|
|
|
});
|
2022-01-19 02:09:30 +00:00
|
|
|
}
|
|
|
|
|
2022-05-22 00:48:19 +00:00
|
|
|
private async schemaExists(schema: any, options?: Transactionable): Promise<boolean> {
|
2022-03-20 12:04:07 +00:00
|
|
|
if (lodash.isObject(schema) && !schema['x-uid']) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { transaction } = options;
|
|
|
|
const result = await this.database.sequelize.query(
|
|
|
|
this.sqlAdapter(`select "x-uid" from ${this.uiSchemasTableName} where "x-uid" = :uid`),
|
|
|
|
{
|
|
|
|
type: 'SELECT',
|
|
|
|
replacements: {
|
|
|
|
uid: lodash.isString(schema) ? schema : schema['x-uid'],
|
|
|
|
},
|
|
|
|
transaction,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
return result.length > 0;
|
|
|
|
}
|
|
|
|
|
2022-03-16 10:45:20 +00:00
|
|
|
@transaction()
|
2022-02-18 04:29:03 +00:00
|
|
|
async insertAdjacent(
|
|
|
|
position: 'beforeBegin' | 'afterBegin' | 'beforeEnd' | 'afterEnd',
|
|
|
|
target: string,
|
|
|
|
schema: any,
|
|
|
|
options?: InsertAdjacentOptions,
|
|
|
|
) {
|
2022-03-20 12:04:07 +00:00
|
|
|
const { transaction } = options;
|
|
|
|
|
2022-10-24 01:13:21 +00:00
|
|
|
// if schema is existed then clear origin path schema cache
|
|
|
|
await this.clearXUidPathCache(schema['x-uid'], transaction);
|
|
|
|
|
2022-03-20 12:04:07 +00:00
|
|
|
if (options.wrap) {
|
|
|
|
// insert wrap schema using insertNewSchema
|
|
|
|
const wrapSchemaNodes = await this.insertNewSchema(options.wrap, {
|
|
|
|
transaction,
|
|
|
|
returnNode: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
const lastWrapNode = wrapSchemaNodes[wrapSchemaNodes.length - 1];
|
|
|
|
|
|
|
|
// insert schema into wrap schema
|
|
|
|
await this.insertAdjacent('afterBegin', lastWrapNode['x-uid'], schema, lodash.omit(options, 'wrap'));
|
|
|
|
|
|
|
|
schema = wrapSchemaNodes[0]['x-uid'];
|
|
|
|
|
|
|
|
options.removeParentsIfNoChildren = false;
|
|
|
|
} else {
|
|
|
|
const schemaExists = await this.schemaExists(schema, { transaction });
|
2022-04-11 02:09:25 +00:00
|
|
|
|
2022-03-20 12:04:07 +00:00
|
|
|
if (schemaExists) {
|
|
|
|
schema = lodash.isString(schema) ? schema : schema['x-uid'];
|
|
|
|
} else {
|
|
|
|
const insertedSchema = await this.insertNewSchema(schema, {
|
|
|
|
transaction,
|
|
|
|
returnNode: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
schema = insertedSchema[0]['x-uid'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-24 01:13:21 +00:00
|
|
|
const result = await this[`insert${lodash.upperFirst(position)}`](target, schema, options);
|
|
|
|
// clear target schema path cache
|
|
|
|
await this.clearXUidPathCache(result['x-uid'], transaction);
|
|
|
|
return result;
|
2022-01-19 02:09:30 +00:00
|
|
|
}
|
|
|
|
|
2022-03-16 10:45:20 +00:00
|
|
|
@transaction()
|
2022-03-20 12:04:07 +00:00
|
|
|
protected async insertAfterBegin(targetUid: string, schema: any, options?: InsertAdjacentOptions) {
|
2022-02-18 04:29:03 +00:00
|
|
|
return await this.insertInner(targetUid, schema, 'first', options);
|
2022-01-19 02:09:30 +00:00
|
|
|
}
|
|
|
|
|
2022-03-16 10:45:20 +00:00
|
|
|
@transaction()
|
2022-03-20 12:04:07 +00:00
|
|
|
protected async insertBeforeEnd(targetUid: string, schema: any, options?: InsertAdjacentOptions) {
|
2022-02-18 04:29:03 +00:00
|
|
|
return await this.insertInner(targetUid, schema, 'last', options);
|
2022-01-19 02:09:30 +00:00
|
|
|
}
|
|
|
|
|
2022-03-16 10:45:20 +00:00
|
|
|
@transaction()
|
2022-03-20 12:04:07 +00:00
|
|
|
protected async insertBeforeBegin(targetUid: string, schema: any, options?: InsertAdjacentOptions) {
|
2022-02-18 04:29:03 +00:00
|
|
|
return await this.insertBeside(targetUid, schema, 'before', options);
|
2022-01-19 02:09:30 +00:00
|
|
|
}
|
|
|
|
|
2022-03-16 10:45:20 +00:00
|
|
|
@transaction()
|
2022-03-20 12:04:07 +00:00
|
|
|
protected async insertAfterEnd(targetUid: string, schema: any, options?: InsertAdjacentOptions) {
|
2022-02-18 04:29:03 +00:00
|
|
|
return await this.insertBeside(targetUid, schema, 'after', options);
|
2022-01-19 02:09:30 +00:00
|
|
|
}
|
|
|
|
|
2022-03-16 10:45:20 +00:00
|
|
|
@transaction()
|
2022-10-24 01:13:21 +00:00
|
|
|
protected async insertNodes(nodes: SchemaNode[], options?: Transactionable) {
|
2022-03-16 10:45:20 +00:00
|
|
|
const { transaction } = options;
|
2022-01-19 02:09:30 +00:00
|
|
|
|
|
|
|
const insertedNodes = [];
|
|
|
|
|
2022-03-16 10:45:20 +00:00
|
|
|
for (const node of nodes) {
|
|
|
|
insertedNodes.push(
|
|
|
|
await this.insertSingleNode(node, {
|
|
|
|
...options,
|
|
|
|
transaction,
|
|
|
|
}),
|
|
|
|
);
|
2022-01-19 02:09:30 +00:00
|
|
|
}
|
2022-03-16 10:45:20 +00:00
|
|
|
|
|
|
|
return insertedNodes;
|
2022-01-19 02:09:30 +00:00
|
|
|
}
|
|
|
|
|
2022-03-16 10:45:20 +00:00
|
|
|
@transaction()
|
2022-05-22 00:48:19 +00:00
|
|
|
async insert(schema: any, options?: Transactionable) {
|
2022-01-19 02:09:30 +00:00
|
|
|
const nodes = UiSchemaRepository.schemaToSingleNodes(schema);
|
|
|
|
const insertedNodes = await this.insertNodes(nodes, options);
|
2022-10-24 01:13:21 +00:00
|
|
|
const result = await this.getJsonSchema(insertedNodes[0].get('x-uid'), {
|
2022-01-19 02:09:30 +00:00
|
|
|
transaction: options?.transaction,
|
|
|
|
});
|
2022-10-24 01:13:21 +00:00
|
|
|
await this.clearXUidPathCache(result['x-uid'], options?.transaction);
|
|
|
|
return result;
|
2022-01-19 02:09:30 +00:00
|
|
|
}
|
|
|
|
|
2022-03-20 12:04:07 +00:00
|
|
|
@transaction()
|
|
|
|
async insertNewSchema(
|
|
|
|
schema: any,
|
2022-05-22 00:48:19 +00:00
|
|
|
options?: Transactionable & {
|
2022-03-20 12:04:07 +00:00
|
|
|
returnNode?: boolean;
|
|
|
|
},
|
|
|
|
) {
|
|
|
|
const { transaction } = options;
|
|
|
|
|
|
|
|
const nodes = UiSchemaRepository.schemaToSingleNodes(schema);
|
|
|
|
// insert schema fist
|
|
|
|
await this.database.sequelize.query(
|
|
|
|
this.sqlAdapter(
|
|
|
|
`INSERT INTO ${this.uiSchemasTableName} ("x-uid", "name", "schema") VALUES ${nodes
|
|
|
|
.map((n) => '(?)')
|
|
|
|
.join(',')};`,
|
|
|
|
),
|
|
|
|
{
|
|
|
|
replacements: lodash.cloneDeep(nodes).map((node) => {
|
|
|
|
const { uid, name } = this.prepareSingleNodeForInsert(node);
|
|
|
|
return [uid, name, JSON.stringify(node)];
|
|
|
|
}),
|
|
|
|
type: 'insert',
|
|
|
|
transaction,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
const treePathData: Array<any> = lodash.cloneDeep(nodes).reduce((carry, item) => {
|
|
|
|
const { uid, childOptions, async } = this.prepareSingleNodeForInsert(item);
|
|
|
|
|
|
|
|
return [
|
|
|
|
...carry,
|
|
|
|
// self reference
|
|
|
|
[uid, uid, 0, childOptions?.type || null, async, null],
|
|
|
|
// parent references
|
|
|
|
...lodash.get(childOptions, 'parentPath', []).map((parentUid, index) => {
|
|
|
|
return [parentUid, uid, index + 1, null, null, childOptions.sort];
|
|
|
|
}),
|
|
|
|
];
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
// insert tree path
|
|
|
|
await this.database.sequelize.query(
|
|
|
|
this.sqlAdapter(
|
|
|
|
`INSERT INTO ${
|
|
|
|
this.uiSchemaTreePathTableName
|
|
|
|
} (ancestor, descendant, depth, type, async, sort) VALUES ${treePathData.map((item) => '(?)').join(',')}`,
|
|
|
|
),
|
|
|
|
{
|
|
|
|
replacements: treePathData,
|
|
|
|
type: 'insert',
|
|
|
|
transaction,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2022-04-11 02:09:25 +00:00
|
|
|
const rootNode = nodes[0];
|
|
|
|
if (rootNode['x-server-hooks']) {
|
|
|
|
const rootModel = await this.findOne({ filter: { 'x-uid': rootNode['x-uid'] }, transaction });
|
|
|
|
await this.database.emitAsync(`${this.collection.name}.afterCreateWithAssociations`, rootModel, options);
|
|
|
|
}
|
|
|
|
|
2022-03-20 12:04:07 +00:00
|
|
|
if (options?.returnNode) {
|
|
|
|
return nodes;
|
|
|
|
}
|
|
|
|
|
2022-10-24 01:13:21 +00:00
|
|
|
const result = await this.getJsonSchema(nodes[0]['x-uid'], {
|
2022-03-20 12:04:07 +00:00
|
|
|
transaction,
|
|
|
|
});
|
2022-10-24 01:13:21 +00:00
|
|
|
await this.clearXUidPathCache(result['x-uid'], transaction);
|
|
|
|
return result;
|
2022-03-20 12:04:07 +00:00
|
|
|
}
|
|
|
|
|
2022-02-08 08:36:48 +00:00
|
|
|
private async insertSchemaRecord(name, uid, schema, transaction) {
|
2022-02-09 12:24:25 +00:00
|
|
|
const serverHooks = schema['x-server-hooks'] || [];
|
|
|
|
|
2022-02-08 08:36:48 +00:00
|
|
|
const node = await this.create({
|
|
|
|
values: {
|
|
|
|
name,
|
2022-02-15 14:32:02 +00:00
|
|
|
['x-uid']: uid,
|
2022-02-08 08:36:48 +00:00
|
|
|
schema,
|
2022-02-09 12:24:25 +00:00
|
|
|
serverHooks,
|
2022-02-08 08:36:48 +00:00
|
|
|
},
|
|
|
|
transaction,
|
2022-02-10 14:53:26 +00:00
|
|
|
context: {
|
|
|
|
disableInsertHook: true,
|
|
|
|
},
|
2022-02-08 08:36:48 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2022-03-20 12:04:07 +00:00
|
|
|
private prepareSingleNodeForInsert(schema: SchemaNode) {
|
2022-01-19 02:09:30 +00:00
|
|
|
const uid = schema['x-uid'];
|
|
|
|
const name = schema['name'];
|
|
|
|
const async = lodash.get(schema, 'x-async', false);
|
|
|
|
const childOptions = schema['childOptions'];
|
|
|
|
|
|
|
|
delete schema['x-uid'];
|
|
|
|
delete schema['x-async'];
|
|
|
|
delete schema['name'];
|
|
|
|
delete schema['childOptions'];
|
|
|
|
|
2022-03-20 12:04:07 +00:00
|
|
|
return { uid, name, async, childOptions };
|
|
|
|
}
|
|
|
|
|
2022-05-22 00:48:19 +00:00
|
|
|
async insertSingleNode(schema: SchemaNode, options: Transactionable & removeParentOptions) {
|
2022-03-20 12:04:07 +00:00
|
|
|
const { transaction } = options;
|
|
|
|
|
|
|
|
const db = this.database;
|
|
|
|
|
|
|
|
const { uid, name, async, childOptions } = this.prepareSingleNodeForInsert(schema);
|
2022-01-19 02:09:30 +00:00
|
|
|
let savedNode;
|
|
|
|
|
|
|
|
// check node exists or not
|
|
|
|
const existsNode = await this.findOne({
|
|
|
|
filter: {
|
2022-02-19 17:23:04 +00:00
|
|
|
'x-uid': uid,
|
2022-01-19 02:09:30 +00:00
|
|
|
},
|
|
|
|
transaction,
|
|
|
|
});
|
|
|
|
|
2022-02-15 16:22:47 +00:00
|
|
|
const treeTable = this.uiSchemaTreePathTableName;
|
2022-01-19 02:09:30 +00:00
|
|
|
|
|
|
|
if (existsNode) {
|
|
|
|
savedNode = existsNode;
|
|
|
|
} else {
|
2022-02-08 08:36:48 +00:00
|
|
|
savedNode = await this.insertSchemaRecord(name, uid, schema, transaction);
|
2022-01-19 02:09:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (childOptions) {
|
2022-02-18 04:29:03 +00:00
|
|
|
const oldParentUid = await this.findParentUid(uid, transaction);
|
2022-01-19 02:09:30 +00:00
|
|
|
const parentUid = childOptions.parentUid;
|
|
|
|
|
2022-03-20 12:04:07 +00:00
|
|
|
const childrenCount = await this.childrenCount(uid, transaction);
|
2022-01-19 02:09:30 +00:00
|
|
|
|
2022-03-20 12:04:07 +00:00
|
|
|
const isTree = childrenCount > 0;
|
2022-01-19 02:09:30 +00:00
|
|
|
|
|
|
|
// if node is a tree root move tree to new path
|
|
|
|
if (isTree) {
|
2022-03-16 10:45:20 +00:00
|
|
|
await this.clearAncestor(uid, { transaction });
|
2022-01-19 02:09:30 +00:00
|
|
|
|
2022-02-18 04:29:03 +00:00
|
|
|
// insert new tree path
|
2022-01-19 02:09:30 +00:00
|
|
|
await db.sequelize.query(
|
|
|
|
`INSERT INTO ${treeTable} (ancestor, descendant, depth)
|
|
|
|
SELECT supertree.ancestor, subtree.descendant, supertree.depth + subtree.depth + 1
|
|
|
|
FROM ${treeTable} AS supertree
|
|
|
|
CROSS JOIN ${treeTable} AS subtree
|
|
|
|
WHERE supertree.descendant = :parentUid
|
|
|
|
AND subtree.ancestor = :uid;`,
|
|
|
|
{
|
|
|
|
type: 'INSERT',
|
|
|
|
replacements: {
|
|
|
|
uid,
|
|
|
|
parentUid,
|
|
|
|
},
|
|
|
|
transaction,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-03-20 12:04:07 +00:00
|
|
|
// update type
|
|
|
|
await db.sequelize.query(
|
|
|
|
`UPDATE ${treeTable} SET type = :type WHERE depth = 0 AND ancestor = :uid AND descendant = :uid`,
|
|
|
|
{
|
|
|
|
type: 'update',
|
|
|
|
transaction,
|
|
|
|
replacements: {
|
|
|
|
type: childOptions.type,
|
|
|
|
uid,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2022-01-19 02:09:30 +00:00
|
|
|
if (!isTree) {
|
|
|
|
if (existsNode) {
|
|
|
|
// remove old path
|
|
|
|
await db.sequelize.query(`DELETE FROM ${treeTable} WHERE descendant = :uid AND ancestor != descendant`, {
|
|
|
|
type: 'DELETE',
|
|
|
|
replacements: {
|
|
|
|
uid,
|
|
|
|
},
|
|
|
|
transaction,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// insert tree path
|
|
|
|
await db.sequelize.query(
|
|
|
|
`INSERT INTO ${treeTable} (ancestor, descendant, depth)
|
|
|
|
SELECT t.ancestor, :modelKey, depth + 1 FROM ${treeTable} AS t WHERE t.descendant = :modelParentKey `,
|
|
|
|
{
|
|
|
|
type: 'INSERT',
|
|
|
|
transaction,
|
|
|
|
replacements: {
|
2022-02-19 17:23:04 +00:00
|
|
|
modelKey: savedNode.get('x-uid'),
|
2022-01-19 02:09:30 +00:00
|
|
|
modelParentKey: parentUid,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!existsNode) {
|
|
|
|
// insert type && async
|
|
|
|
await db.sequelize.query(
|
|
|
|
`INSERT INTO ${treeTable}(ancestor, descendant, depth, type, async) VALUES (:modelKey, :modelKey, 0, :type, :async )`,
|
|
|
|
{
|
|
|
|
type: 'INSERT',
|
|
|
|
replacements: {
|
2022-02-19 17:23:04 +00:00
|
|
|
modelKey: savedNode.get('x-uid'),
|
2022-01-19 02:09:30 +00:00
|
|
|
type: childOptions.type,
|
|
|
|
async,
|
|
|
|
},
|
|
|
|
transaction,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const nodePosition = childOptions.position || 'last';
|
|
|
|
|
|
|
|
let sort;
|
|
|
|
|
|
|
|
// insert at first
|
|
|
|
if (nodePosition === 'first') {
|
|
|
|
sort = 1;
|
2022-02-15 14:32:02 +00:00
|
|
|
|
|
|
|
let updateSql = `UPDATE ${treeTable} as TreeTable
|
2022-01-19 02:09:30 +00:00
|
|
|
SET sort = TreeTable.sort + 1
|
|
|
|
FROM ${treeTable} as NodeInfo
|
|
|
|
WHERE NodeInfo.descendant = TreeTable.descendant and NodeInfo.depth = 0
|
2022-02-15 14:32:02 +00:00
|
|
|
AND TreeTable.depth = 1 AND TreeTable.ancestor = :ancestor and NodeInfo.type = :type`;
|
|
|
|
|
|
|
|
// Compatible with mysql
|
|
|
|
if (this.database.sequelize.getDialect() === 'mysql') {
|
2022-04-11 02:09:25 +00:00
|
|
|
updateSql = `UPDATE ${treeTable} as TreeTable
|
2022-02-15 14:32:02 +00:00
|
|
|
JOIN ${treeTable} as NodeInfo ON (NodeInfo.descendant = TreeTable.descendant and NodeInfo.depth = 0)
|
|
|
|
SET TreeTable.sort = TreeTable.sort + 1
|
|
|
|
WHERE TreeTable.depth = 1 AND TreeTable.ancestor = :ancestor and NodeInfo.type = :type`;
|
|
|
|
}
|
|
|
|
|
|
|
|
// move all child last index
|
|
|
|
await db.sequelize.query(updateSql, {
|
|
|
|
replacements: {
|
|
|
|
ancestor: childOptions.parentUid,
|
|
|
|
type: childOptions.type,
|
2022-01-19 02:09:30 +00:00
|
|
|
},
|
2022-02-15 14:32:02 +00:00
|
|
|
transaction,
|
|
|
|
});
|
2022-01-19 02:09:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (nodePosition === 'last') {
|
|
|
|
const maxSort = await db.sequelize.query(
|
|
|
|
`SELECT ${
|
|
|
|
this.database.sequelize.getDialect() === 'postgres' ? 'coalesce' : 'ifnull'
|
|
|
|
}(MAX(TreeTable.sort), 0) as maxsort FROM ${treeTable} as TreeTable
|
|
|
|
LEFT JOIN ${treeTable} as NodeInfo
|
|
|
|
ON NodeInfo.descendant = TreeTable.descendant and NodeInfo.depth = 0
|
|
|
|
WHERE TreeTable.depth = 1 AND TreeTable.ancestor = :ancestor and NodeInfo.type = :type`,
|
|
|
|
{
|
|
|
|
type: 'SELECT',
|
|
|
|
replacements: {
|
|
|
|
ancestor: childOptions.parentUid,
|
|
|
|
type: childOptions.type,
|
|
|
|
},
|
|
|
|
transaction,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
sort = parseInt(maxSort[0]['maxsort']) + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lodash.isPlainObject(nodePosition)) {
|
|
|
|
const targetPosition = nodePosition as TargetPosition;
|
|
|
|
const target = targetPosition.target;
|
|
|
|
|
|
|
|
const targetSort = await db.sequelize.query(
|
|
|
|
`SELECT TreeTable.sort as sort FROM ${treeTable} as TreeTable
|
|
|
|
LEFT JOIN ${treeTable} as NodeInfo
|
|
|
|
ON NodeInfo.descendant = TreeTable.descendant and NodeInfo.depth = 0 WHERE TreeTable.depth = 1 AND TreeTable.ancestor = :ancestor AND TreeTable.descendant = :descendant and NodeInfo.type = :type`,
|
|
|
|
{
|
|
|
|
type: 'SELECT',
|
|
|
|
replacements: {
|
|
|
|
ancestor: childOptions.parentUid,
|
|
|
|
descendant: target,
|
|
|
|
type: childOptions.type,
|
|
|
|
},
|
|
|
|
transaction,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
sort = targetSort[0].sort;
|
|
|
|
|
|
|
|
if (targetPosition.type == 'after') {
|
|
|
|
sort += 1;
|
|
|
|
}
|
|
|
|
|
2022-02-15 14:32:02 +00:00
|
|
|
let updateSql = `UPDATE ${treeTable} as TreeTable
|
|
|
|
SET sort = TreeTable.sort + 1
|
|
|
|
FROM ${treeTable} as NodeInfo
|
|
|
|
WHERE NodeInfo.descendant = TreeTable.descendant
|
|
|
|
and NodeInfo.depth = 0
|
|
|
|
AND TreeTable.depth = 1
|
|
|
|
AND TreeTable.ancestor = :ancestor
|
|
|
|
and TreeTable.sort >= :sort
|
|
|
|
and NodeInfo.type = :type`;
|
|
|
|
|
|
|
|
if (this.database.sequelize.getDialect() === 'mysql') {
|
|
|
|
updateSql = `UPDATE ${treeTable} as TreeTable
|
|
|
|
JOIN ${treeTable} as NodeInfo ON (NodeInfo.descendant = TreeTable.descendant and NodeInfo.depth = 0)
|
|
|
|
SET TreeTable.sort = TreeTable.sort + 1
|
|
|
|
WHERE TreeTable.depth = 1 AND TreeTable.ancestor = :ancestor and TreeTable.sort >= :sort and NodeInfo.type = :type`;
|
|
|
|
}
|
|
|
|
|
|
|
|
await db.sequelize.query(updateSql, {
|
|
|
|
replacements: {
|
|
|
|
ancestor: childOptions.parentUid,
|
|
|
|
sort,
|
|
|
|
type: childOptions.type,
|
2022-01-19 02:09:30 +00:00
|
|
|
},
|
2022-02-15 14:32:02 +00:00
|
|
|
transaction,
|
|
|
|
});
|
2022-01-19 02:09:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// update order
|
|
|
|
const updateSql = `UPDATE ${treeTable} SET sort = :sort WHERE depth = 1 AND ancestor = :ancestor AND descendant = :descendant`;
|
|
|
|
await db.sequelize.query(updateSql, {
|
|
|
|
type: 'UPDATE',
|
|
|
|
replacements: {
|
|
|
|
ancestor: childOptions.parentUid,
|
|
|
|
sort,
|
|
|
|
descendant: uid,
|
|
|
|
},
|
|
|
|
transaction,
|
|
|
|
});
|
2022-02-18 04:29:03 +00:00
|
|
|
|
|
|
|
// move node to new parent
|
|
|
|
if (oldParentUid !== null && oldParentUid !== parentUid) {
|
|
|
|
await this.database.emitAsync('uiSchemaMove', savedNode, {
|
|
|
|
transaction,
|
|
|
|
oldParentUid,
|
|
|
|
parentUid,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (options.removeParentsIfNoChildren) {
|
|
|
|
await this.recursivelyRemoveIfNoChildren({
|
|
|
|
transaction,
|
|
|
|
uid: oldParentUid,
|
|
|
|
breakRemoveOn: options.breakRemoveOn,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2022-01-19 02:09:30 +00:00
|
|
|
} else {
|
|
|
|
// insert root node path
|
|
|
|
await db.sequelize.query(
|
|
|
|
`INSERT INTO ${treeTable}(ancestor, descendant, depth, async) VALUES (:modelKey, :modelKey, 0, :async )`,
|
|
|
|
{
|
|
|
|
type: 'INSERT',
|
|
|
|
replacements: {
|
2022-02-19 17:23:04 +00:00
|
|
|
modelKey: savedNode.get('x-uid'),
|
2022-01-19 02:09:30 +00:00
|
|
|
async,
|
|
|
|
},
|
|
|
|
transaction,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
2022-10-24 01:13:21 +00:00
|
|
|
await this.clearXUidPathCache(uid, transaction);
|
2022-01-19 02:09:30 +00:00
|
|
|
return savedNode;
|
|
|
|
}
|
|
|
|
}
|
2022-02-11 10:13:14 +00:00
|
|
|
|
|
|
|
export default UiSchemaRepository;
|