UNPKG

@ultipa-graph/ultipa-node-sdk

Version:

NodeJS SDK for ultipa-server 4.0

258 lines 10.6 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.SchemaExra = void 0; const connection_base_1 = require("./connection.base"); const types_1 = require("../../types"); const utils_1 = require("../../utils"); const lodash_1 = __importDefault(require("lodash")); const { CommandList, SchemaStringWithDefault } = utils_1.UQLMAKER; const JSON_KEYS = ["properties"]; class SchemaExra extends connection_base_1.ConnectionBase { /** * 获得所有schema,或点,边所有schema * @param dbType(delete) * @param requestConfig */ showSchema( // dbType?: ULTIPA.DBType, requestConfig) { return __awaiter(this, void 0, void 0, function* () { // if (dbType === ULTIPA.DBType.DBNODE) { // let nodeRes = await this.showNodeSchema(requestConfig); // return { // ...nodeRes, // data: { // nodeSchema: nodeRes.data // } // }; // } else if (dbType === ULTIPA.DBType.DBEDGE) { // let edgeRes = await this.showEdgeSchema(requestConfig); // return { // ...edgeRes, // data: { // edgeSchema: edgeRes.data // } // }; // } let command = CommandList.listSchema; let uqlMaker = new utils_1.UQLMAKER(command, requestConfig); let res = yield connection_base_1.ConnectionBase.UqlListSimpleAuto(this, uqlMaker, { jsonKeys: JSON_KEYS, }); let nodeSchema, edgeSchema; res.data.forEach((d) => { switch (d.name) { case "_nodeSchema": nodeSchema = d.values; break; case "_edgeSchema": edgeSchema = d.values; break; } }); return Object.assign(Object.assign({}, res), { data: [...nodeSchema, ...edgeSchema] }); }); } /** 获得点或者边的schema */ getSchemaBase(isNode, name, commonReq) { return __awaiter(this, void 0, void 0, function* () { let command = isNode ? CommandList.getNodeSchema : CommandList.getEdgeSchema; let uqlMaker = new utils_1.UQLMAKER(command, commonReq, SchemaStringWithDefault(name)); return connection_base_1.ConnectionBase.UqlGetSimple(this, uqlMaker, { jsonKeys: JSON_KEYS, }); }); } getAllSchema(isNode, commonReq) { return __awaiter(this, void 0, void 0, function* () { let command = isNode ? CommandList.getNodeSchema : CommandList.getEdgeSchema; let uqlMaker = new utils_1.UQLMAKER(command, commonReq); return connection_base_1.ConnectionBase.UqlListSimple(this, uqlMaker, { jsonKeys: JSON_KEYS, }); }); } /** * 获取某个schema * @param name * @param dbType * @param commonReq */ getSchema(schemaName, dbType, requestConfig) { return __awaiter(this, void 0, void 0, function* () { return this.getSchemaBase(dbType === types_1.ULTIPA.DBType.DBNODE, schemaName, requestConfig); }); } /** 获得点的schema */ getNodeSchema(schemaName, requestConfig) { return __awaiter(this, void 0, void 0, function* () { return this.getSchema(schemaName, types_1.ULTIPA.DBType.DBNODE, requestConfig); }); } /** 获得点的所有schema */ showNodeSchema(requestConfig) { return __awaiter(this, void 0, void 0, function* () { return this.getAllSchema(true, requestConfig); }); } /** 获得边的schema */ getEdgeSchema(schemaName, requestConfig) { return __awaiter(this, void 0, void 0, function* () { return this.getSchemaBase(false, schemaName, requestConfig); }); } /** 获得边的所有schema */ showEdgeSchema(requestConfig) { return __awaiter(this, void 0, void 0, function* () { return this.getAllSchema(false, requestConfig); }); } /** 创建点或者边的schema */ createSchemaBase(req, commonReq) { return __awaiter(this, void 0, void 0, function* () { let command = req.dbType === types_1.ULTIPA.DBType.DBNODE ? CommandList.createNodeSchema : CommandList.createEdgeSchema; let commandParams = [utils_1.UQLMAKER.ForceString(req.name), req.desc || ""]; let uqlMaker = new utils_1.UQLMAKER(command, commonReq, commandParams); return connection_base_1.ConnectionBase.UqlUpdateSimple(this, uqlMaker); }); } hasSchema(req, commonReq) { return __awaiter(this, void 0, void 0, function* () { let res = yield this.getSchema(req.name, req.dbType, commonReq); return Object.assign(Object.assign({}, res), { data: !lodash_1.default.isEmpty(res.data) }); }); } /** * 如果不存在schema,创建schema * @param req * @param commonReq */ createSchemaIfNotExist(schema, requestConfig) { return __awaiter(this, void 0, void 0, function* () { let isSchemaExist = yield this.hasSchema(schema, requestConfig); if (isSchemaExist.data) { return { status: utils_1.FormatResponse.successStatus(), statistics: utils_1.FormatResponse.statisticsSum(), data: true, }; } let res = yield this.createSchemaBase(schema, requestConfig); return Object.assign(Object.assign({}, res), { data: false }); }); } /** 删除点或者边的schema */ // async dropSchema( // isNode: boolean, // req: RequestType.DeleteSchema, // commonReq?: RequestType.RequestConfig // ) dropSchema(schema, requestConfig) { return __awaiter(this, void 0, void 0, function* () { let command = (schema.dbType === types_1.ULTIPA.DBType.DBNODE) ? CommandList.dropNodeSchema : CommandList.dropEdgeSchema; let commandParams = [SchemaStringWithDefault(schema.name)]; let uqlMaker = new utils_1.UQLMAKER(command, requestConfig, commandParams); return connection_base_1.ConnectionBase.UqlUpdateSimple(this, uqlMaker); }); } /** 创建点的schema */ createNodeSchema(req, commonReq) { return __awaiter(this, void 0, void 0, function* () { return this.createSchemaBase(Object.assign(Object.assign({}, req), { dbType: types_1.ULTIPA.DBType.DBNODE }), commonReq); }); } /** 删除点的schema */ // async dropNodeSchema( // req: RequestType.DeleteSchema, // commonReq?: RequestType.RequestConfig // ) { // return this.dropSchema(true, req, commonReq); // } /** 创建边的schema */ createEdgeSchema(req, commonReq) { return __awaiter(this, void 0, void 0, function* () { return this.createSchemaBase(Object.assign(Object.assign({}, req), { dbType: types_1.ULTIPA.DBType.DBEDGE }), commonReq); }); } /** 删除边的schema */ // async dropEdgeSchema( // req: RequestType.DeleteSchema, // commonReq?: RequestType.RequestConfig // ) { // return this.dropSchema(false, req, commonReq); // } /** 更新点或者边的schema */ alterSchema(schema, newSchema, requestConfig) { return __awaiter(this, void 0, void 0, function* () { // req: RequestType.UpdateSchema, let req = { name: schema.name, dbType: schema.dbType, newName: newSchema.name, newDesc: newSchema.desc, }; let command = req.dbType === types_1.ULTIPA.DBType.DBNODE ? CommandList.updateNodeSchema : CommandList.updateEdgeSchema; let commandParams = [SchemaStringWithDefault(req.name)]; let uqlMaker = new utils_1.UQLMAKER(command, requestConfig, commandParams); let set = {}; if (req.newName) { set.name = req.newName; } if ((0, utils_1.isNotNullString)(req.newDesc)) { set.description = req.newDesc; } uqlMaker.addParam("set", set); return connection_base_1.ConnectionBase.UqlUpdateSimple(this, uqlMaker); }); } /** 更新点的schema */ // async alterNodeSchema( // req: RequestType.UpdateSchema, // commonReq?: RequestType.RequestConfig // ) { // return this.alterSchema({ ...req, dbType: ULTIPA.DBType.DBNODE }, commonReq); // } alterNodeSchema(schema, newSchema, requestConfig) { return __awaiter(this, void 0, void 0, function* () { return this.alterSchema(schema, newSchema, requestConfig); }); } /** 更新边的schema */ // async alterEdgeSchema( // req: RequestType.UpdateSchema, // commonReq?: RequestType.RequestConfig // ) { // return this.alterSchema({ ...req, dbType: ULTIPA.DBType.DBEDGE }, commonReq); // } alterEdgeSchema(schema, newSchema, requestConfig) { return __awaiter(this, void 0, void 0, function* () { return this.alterSchema(schema, newSchema, requestConfig); }); } } exports.SchemaExra = SchemaExra; //# sourceMappingURL=schema.extra.js.map