UNPKG

@ultipa-graph/ultipa-driver

Version:

NodeJS SDK for ultipa-server 5.2

253 lines 10 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SchemaExra = void 0; const types_extra_1 = require("../../types/types.extra"); const types_1 = require("../../types/types"); const utils_1 = require("../../utils"); const property_extra_1 = require("./property.extra"); const { CommandList, SchemaStringWithDefault } = utils_1.UQLMAKER; const JSON_KEYS = ["properties"]; const SCHEMA_PROPERTY_DATA_FORMAT = (schema, dbType) => { }; const FORMAT_SCHEMAS = (schemas, dbType, graphCount) => { let lastSchemas = []; schemas.forEach((schema) => { let count = graphCount.filter(i => i.schema === schema.name); let resSchema = new types_1.Schema(); resSchema.dbType = dbType; resSchema.id = schema?.id; resSchema.name = schema.name; resSchema.description = schema.description || ""; resSchema.total = count.reduce((acc, cur) => acc + parseInt(cur.count), 0) + ""; let stats = count.map(i => { let stats = new types_1.SchemaStat(); stats.dbType = dbType; stats.schema = i.schema; stats.fromSchema = i.from_schema; stats.toSchema = i.to_schema; stats.count = i.count; return stats; }); resSchema.stats = stats; resSchema.properties = schema.properties; lastSchemas.push(resSchema); }); return lastSchemas; }; class SchemaExra extends property_extra_1.PropertyExtra { async showSchemaBase(dbType, schemaName, config) { let command = CommandList.listSchema; if (dbType === types_1.DBType.DBNODE) { command = CommandList.listNodeSchema; } if (dbType === types_1.DBType.DBEDGE) { command = CommandList.listEdgeSchema; } let uqlMaker = new utils_1.UQLMAKER(command, config, schemaName?.trim()?.length > 0 ? utils_1.UQLMAKER.SchemaStringWithDefault(schemaName) : undefined); let res = await this.uql(uqlMaker.toString(), config); let result = []; let graphCount = res.items[types_extra_1.ResponseTableName.GRAPH_COUNT]?.asTable().toKV(); if (dbType === types_1.DBType.DBNODE || dbType === undefined) { let nodeSchema = res.items[types_extra_1.ResponseTableName.NODE_SCHEMA]?.asSchemas() || []; result.push(...FORMAT_SCHEMAS(nodeSchema, types_1.DBType.DBNODE, graphCount)); } if (dbType === types_1.DBType.DBEDGE || dbType === undefined) { let edgeSchema = res.items[types_extra_1.ResponseTableName.EDGE_SCHEMA]?.asSchemas() || []; result.push(...FORMAT_SCHEMAS(edgeSchema, types_1.DBType.DBEDGE, graphCount)); } return result; } /** * Retrieves all nodes and edge schemas from the current graphset. * @param config */ async showSchema(config) { return this.showSchemaBase(undefined, undefined, config); } /** * Get one schema info * @param isNode * @param name * @param config * @returns */ async getSchemaBase(dbType, schemaName, config) { let res = await this.showSchemaBase(dbType, schemaName, config); return res.find(s => s.name === schemaName && s.dbType === dbType); } /** * Retrieves a node or edge schema from the current graphset. * @param name * @param dbType * @param config */ async getSchema(schemaName, dbType, config) { return this.getSchemaBase(dbType, schemaName, config); } /** *Retrieves a node schema from the current graphset. * @param schemaName * @param config * @returns Response<Schema> */ async getNodeSchema(schemaName, config) { return this.getSchemaBase(types_1.DBType.DBNODE, schemaName, config); } /** * Retrieves all node schemas from the current graphset. * @param config * @returns Response<Schema[]> */ async showNodeSchema(config) { let res = await this.showSchemaBase(types_1.DBType.DBNODE, undefined, config); return res.filter(s => s.dbType === types_1.DBType.DBNODE); } /** * Retrieves an edge schema from the current graphset. * @param config * @returns */ async getEdgeSchema(schemaName, config) { return this.getSchemaBase(types_1.DBType.DBEDGE, schemaName, config); } /** * Retrieves all node schemas from the current graphset. * @param config * @returns Response<Schema[]> */ async showEdgeSchema(config) { let res = await this.showSchemaBase(types_1.DBType.DBEDGE, undefined, config); return res.filter(s => s.dbType === types_1.DBType.DBEDGE); } async createSchemaBase(req, config) { let command = req.dbType === types_1.DBType.DBNODE ? CommandList.createNodeSchema : CommandList.createEdgeSchema; let commandParams = [utils_1.UQLMAKER.ForceString(req.name), req.description || ""]; let uqlMaker = new utils_1.UQLMAKER(command, config, commandParams); return this.uql(uqlMaker.toString(), config); } /** * Creates a new schema in the current graphset. * @param schema * @param isCreateProperties * @param config * @returns */ async createSchema(schema, isCreateProperties = false, config) { let command = schema.dbType === types_1.DBType.DBNODE ? CommandList.createNodeSchema : CommandList.createEdgeSchema; let commandParams = [utils_1.UQLMAKER.ForceString(schema.name), schema.description || ""]; let uqlMaker = new utils_1.UQLMAKER(command, config, commandParams); let response = await this.uql(uqlMaker.toString(), config); if (isCreateProperties) { let dbType = schema.dbType; schema.properties?.forEach(async (prop) => { prop.schema = schema.name; let res = await this.createProperty(dbType, prop, config); response.statistics = utils_1.FormatResponse.statisticsSum(response.statistics, res.statistics); }); } return response; } async hasSchema(req, config) { let res = await this.getSchema(req.name, req.dbType, config); return res ? true : false; } /** * Creates a new schema in the current graphset, handling cases where the given schema name already exists by ignoring the error. * @param schema * @param config */ async createSchemaIfNotExist(schema, isCreateProperties = false, config) { let isSchemaExist = await this.hasSchema(schema, config); if (isSchemaExist) { return { exist: true, response: { status: utils_1.FormatResponse.successStatus(), statistics: utils_1.FormatResponse.statisticsSum() }, }; } let res = await this.createSchema(schema, isCreateProperties, config); return { exist: false, response: res }; } /** * Drops one schema from the current graphset by its name. */ async dropSchema(schema, config) { let command = (schema.dbType === types_1.DBType.DBNODE) ? CommandList.dropNodeSchema : CommandList.dropEdgeSchema; let commandParams = [SchemaStringWithDefault(schema.name)]; let uqlMaker = new utils_1.UQLMAKER(command, config, commandParams); return this.uql(uqlMaker.toString(), config); } /** * Creates a new node schema in the current graphset. * @param req * @param config * @returns */ async createNodeSchema(req, config) { return this.createSchemaBase({ ...req, dbType: types_1.DBType.DBNODE }, config); } /** * Creates a new edge schema in the current graphset. * @param req * @param config * @returns */ async createEdgeSchema(req, config) { return this.createSchemaBase({ ...req, dbType: types_1.DBType.DBEDGE }, config); } /** * Alters the name and description of one existing schema in the current graphset by its name. * @param schema * @param newSchema * @param config * @returns */ async alterSchema(originalSchema, newSchema, config) { let command = originalSchema.dbType === types_1.DBType.DBNODE ? CommandList.updateNodeSchema : CommandList.updateEdgeSchema; let commandParams = [SchemaStringWithDefault(originalSchema.name)]; let uqlMaker = new utils_1.UQLMAKER(command, config, commandParams); let set = {}; if (newSchema.name) { set.name = newSchema.name; } if ((0, utils_1.isNotNullString)(newSchema.description)) { set.description = newSchema.description; } uqlMaker.addParam("set", set); return this.uql(uqlMaker.toString(), config); } /** * Alters the name and description of one existing node schema in the current graphset by its name. * @param schema * @param newSchema * @param config * @returns */ async alterNodeSchema(originalSchema, newSchema, config) { return this.alterSchema(originalSchema, newSchema, config); } /** * Alters the name and description of one existing edge schema in the current graphset by its name. * @param schema * @param newSchema * @param config * @returns */ async alterEdgeSchema(originalSchema, newSchema, config) { return this.alterSchema(originalSchema, newSchema, config); } } exports.SchemaExra = SchemaExra; //# sourceMappingURL=schema.extra.js.map