UNPKG

@deep-foundation/deeplinks

Version:

[![npm](https://img.shields.io/npm/v/@deep-foundation/deeplinks.svg)](https://www.npmjs.com/package/@deep-foundation/deeplinks) [![Gitpod](https://img.shields.io/badge/Gitpod-ready--to--code-blue?logo=gitpod)](https://gitpod.io/#https://github.com/deep-fo

209 lines 9.44 kB
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 __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; import { generateApolloClient } from '@deep-foundation/hasura/client.js'; import { DeepClient } from './client.js'; import _ from 'lodash'; import { serializeError } from 'serialize-error'; const apolloClient = generateApolloClient({ path: `${process.env.DEEPLINKS_HASURA_PATH}/v1/graphql`, ssl: !!+process.env.DEEPLINKS_HASURA_SSL, secret: process.env.DEEPLINKS_HASURA_SECRET, }); const deep = new DeepClient({ apolloClient }); export const insertHandler = (handleOperationTypeId, typeId, code, forceOwnerId, supportsId) => __awaiter(void 0, void 0, void 0, function* () { const syncTextFileTypeId = deep.idLocal('@deep-foundation/core', 'SyncTextFile'); const handlerJSFile = (yield deep.insert({ type_id: syncTextFileTypeId, }, { name: 'INSERT_HANDLER_JS_FILE' })).data[0]; const handlerJSFileValue = (yield deep.insert({ link_id: handlerJSFile === null || handlerJSFile === void 0 ? void 0 : handlerJSFile.id, value: code }, { table: 'strings' })).data[0]; const handlerTypeId = deep.idLocal('@deep-foundation/core', 'Handler'); const handler = (yield deep.insert({ from_id: supportsId || deep.idLocal('@deep-foundation/core', 'dockerSupportsJs'), type_id: handlerTypeId, to_id: handlerJSFile === null || handlerJSFile === void 0 ? void 0 : handlerJSFile.id, }, { name: 'INSERT_HANDLER' })).data[0]; const containTypeId = deep.idLocal('@deep-foundation/core', 'Contain'); const ownerId = forceOwnerId || (yield deep.id('deep', 'admin')); const ownerContainHandler = (yield deep.insert({ from_id: ownerId, type_id: containTypeId, to_id: handler === null || handler === void 0 ? void 0 : handler.id, }, { name: 'INSERT_ADMIN_CONTAIN_HANDLER' })).data[0]; const handleOperation = (yield deep.insert({ from_id: typeId, type_id: handleOperationTypeId, to_id: handler === null || handler === void 0 ? void 0 : handler.id, }, { name: 'INSERT_INSERT_HANDLER' })).data[0]; return { handlerId: handler === null || handler === void 0 ? void 0 : handler.id, handleOperationId: handleOperation === null || handleOperation === void 0 ? void 0 : handleOperation.id, handlerJSFileId: handlerJSFile === null || handlerJSFile === void 0 ? void 0 : handlerJSFile.id, handlerJSFileValueId: handlerJSFileValue === null || handlerJSFileValue === void 0 ? void 0 : handlerJSFileValue.id, ownerContainHandlerId: ownerContainHandler === null || ownerContainHandler === void 0 ? void 0 : ownerContainHandler.id, }; }); export function insertSelector() { return __awaiter(this, void 0, void 0, function* () { const typeTypeId = deep.idLocal('@deep-foundation/core', 'Type'); const { data: [{ id: ty0 }] } = yield deep.insert({ type_id: typeTypeId, }); const { data: [{ id: ty1 }] } = yield deep.insert({ type_id: typeTypeId, from_id: ty0, to_id: ty0, }); const treeIncludeDownTypeId = deep.idLocal('@deep-foundation/core', 'TreeIncludeDown'); const { data: [{ id: tr1, out: treeIncludes }] } = yield deep.insert({ type_id: deep.idLocal('@deep-foundation/core', 'Tree'), out: { data: [ { type_id: treeIncludeDownTypeId, to_id: ty1, }, { type_id: treeIncludeDownTypeId, to_id: ty0, }, ] } }, { returning: ` id out(where: { to_id: { _in: [${ty0}, ${ty1}] }, type_id: { _eq: ${treeIncludeDownTypeId} } }) { id } `, }); const { data: [{ id: id0 }] } = yield deep.insert({ type_id: ty0, }); const selectorIncludeTypeId = deep.idLocal('@deep-foundation/core', 'SelectorInclude'); const selectorTreeTypeId = deep.idLocal('@deep-foundation/core', 'SelectorTree'); const { data: [{ id: s1, out: selectorData }] } = yield deep.insert({ type_id: deep.idLocal('@deep-foundation/core', 'Selector'), out: { data: [ { type_id: selectorIncludeTypeId, to_id: id0, out: { data: { type_id: selectorTreeTypeId, to_id: tr1 } }, }, ] } }, { returning: ` id out(where: { to_id: { _eq: ${id0} }, type_id: { _eq: ${selectorIncludeTypeId} } }) { id out(where: { to_id: { _eq: ${tr1} }, type_id: { _eq: ${selectorTreeTypeId} } }) { id } } `, }); return { nodeTypeId: ty0, linkTypeId: ty1, treeId: tr1, treeIncludesIds: treeIncludes.map(({ id }) => id), selectorId: s1, selectorIncludeId: selectorData[0].id, selectorTreeId: selectorData[0].out[0].id, rootId: id0, }; }); } ; export function insertSelectorItems({ selectorId, nodeTypeId, linkTypeId, treeId, rootId }) { return __awaiter(this, void 0, void 0, function* () { const { data: [{ id: linkId1, to: { id: nodeId1 } }] } = yield deep.insert({ type_id: linkTypeId, from_id: rootId, to: { data: { type_id: nodeTypeId, } } }, { returning: 'id to { id }' }); const { data: [{ id: linkId2, to: { id: nodeId2 } }] } = yield deep.insert({ from_id: nodeId1, type_id: linkTypeId, to: { data: { type_id: nodeTypeId, } } }, { returning: 'id to { id }' }); return [ { linkId: linkId1, nodeId: nodeId1 }, { linkId: linkId2, nodeId: nodeId2 }, ]; }); } ; export function insertSelectorItem({ selectorId, nodeTypeId, linkTypeId, treeId, rootId }) { return __awaiter(this, void 0, void 0, function* () { const { data: [{ id: linkId }] } = yield deep.insert({ type_id: linkTypeId, from_id: rootId, to: { data: { type_id: nodeTypeId, } } }, { returning: 'id to { id }' }); return { linkId }; }); } ; export const deleteHandler = (handler) => __awaiter(void 0, void 0, void 0, function* () { const { handlerJSFileValueId, scheduleValueId } = handler, ids = __rest(handler, ["handlerJSFileValueId", "scheduleValueId"]); const result = { links: [], strings: [] }; const _ids = []; const compact = { id: { _in: _ids.concat(_.compact(Object.values(ids))) } }; result.links.push(yield deep.delete(compact)); return result; }); export const deleteSelector = (selector) => __awaiter(void 0, void 0, void 0, function* () { const { treeIncludesIds } = selector, withoutTreeIncluds = __rest(selector, ["treeIncludesIds"]); const ids = (_.concat(treeIncludesIds, Object.values(withoutTreeIncluds))); const _ids = []; const compact = { id: { _in: _ids.concat(_.compact(ids)) } }; yield deep.delete(compact); }); export function deleteId(id, options = { table: 'links' }) { return __awaiter(this, void 0, void 0, function* () { yield deleteIds([id], options); }); } export function deleteIds(ids, options = { table: 'links' }) { return __awaiter(this, void 0, void 0, function* () { const idsFiltered = ids === null || ids === void 0 ? void 0 : ids.filter(linkId => typeof linkId === 'number'); if ((idsFiltered === null || idsFiltered === void 0 ? void 0 : idsFiltered.length) > 0) { try { return yield deep.delete(idsFiltered, options); } catch (e) { const serializedError = serializeError(e); console.error(`Error deleting ids: ${idsFiltered.join(', ')}`, JSON.stringify(serializedError, null, 2)); } } else { return { data: [] }; } }); } //# sourceMappingURL=handlers.js.map