UNPKG

prostgles-client

Version:

Reactive client for Postgres

191 lines (190 loc) 10.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getDB = void 0; const prostgles_types_1 = require("prostgles-types"); const useFetch_1 = require("./hooks/useFetch"); const useSubscribe_1 = require("./hooks/useSubscribe"); const useSync_1 = require("./hooks/useSync"); const SyncedTable_1 = require("./SyncedTable/SyncedTable"); const preffix = prostgles_types_1.CHANNELS._preffix; const getDB = ({ tableSchema, onDebug, syncedTable, syncHandler, subscriptionHandler, socket, }) => { var _a; /* Building DB object */ const checkSubscriptionArgs = (basicFilter, options, onChange, onError) => { if ((basicFilter !== undefined && !(0, prostgles_types_1.isObject)(basicFilter)) || (options !== undefined && !(0, prostgles_types_1.isObject)(options)) || !(typeof onChange === "function") || (onError !== undefined && typeof onError !== "function")) { throw "Expecting: ( basicFilter<object>, options<object>, onChange<function> , onError?<function>) but got something else"; } }; const subscribeCommands = ["subscribe", "subscribeOne"]; const db = {}; const schemaClone = (_a = (0, SyncedTable_1.quickClone)(tableSchema)) !== null && _a !== void 0 ? _a : []; schemaClone.forEach(({ name: tableName, publishInfo }) => { const allowedCommands = (0, prostgles_types_1.getAllowedTableMethods)({ publishInfo }); db[tableName] = {}; const dboTable = db[tableName]; allowedCommands .sort((a, b) => Number((0, prostgles_types_1.includes)(subscribeCommands, a)) - Number((0, prostgles_types_1.includes)(subscribeCommands, b))) .forEach((command) => { var _a; if (command === "sync") { const syncConfig = (_a = publishInfo.select) === null || _a === void 0 ? void 0 : _a.syncConfig; if (!syncConfig) { throw `Table ${tableName} does not have syncConfig in publishInfo.select`; } dboTable._syncInfo = { ...syncConfig }; if (syncedTable) { dboTable.getSync = async (filter, params = {}) => { await (onDebug === null || onDebug === void 0 ? void 0 : onDebug({ type: "table", command: "getSync", tableName, data: { filter, params }, })); return syncedTable.create({ name: tableName, onDebug: onDebug, filter, db: db, ...params, }); }; const upsertSyncTable = async (basicFilter = {}, options = {}, onError) => { var _a; const syncName = `${tableName}.${JSON.stringify(basicFilter)}.${JSON.stringify((0, prostgles_types_1.omitKeys)(options, ["handlesOnData"]))}`; const syncedTableHandler = (_a = syncHandler.syncedTables[syncName]) !== null && _a !== void 0 ? _a : (await syncedTable.create({ ...options, onDebug: onDebug, name: tableName, filter: basicFilter, db: db, onError, })); syncHandler.syncedTables[syncName] = syncedTableHandler; return syncedTableHandler; }; const sync = (async (basicFilter, options = { handlesOnData: true, select: "*" }, onChange, onError) => { await (onDebug === null || onDebug === void 0 ? void 0 : onDebug({ type: "table", command: "sync", tableName, data: { basicFilter, options }, })); checkSubscriptionArgs(basicFilter, options, onChange, onError); const s = await upsertSyncTable(basicFilter, options, onError); return await s.sync(onChange, options.handlesOnData); }); const syncOne = (async (basicFilter, options = { handlesOnData: true }, onChange, onError) => { await (onDebug === null || onDebug === void 0 ? void 0 : onDebug({ type: "table", command: "syncOne", tableName, data: { basicFilter, options }, })); checkSubscriptionArgs(basicFilter, options, onChange, onError); const s = await upsertSyncTable(basicFilter, options, onError); return await s.syncOne(basicFilter, onChange, options.handlesOnData); }); dboTable.sync = sync; dboTable.syncOne = syncOne; dboTable.useSync = (basicFilter, options, hookOptions) => // eslint-disable-next-line react-hooks/rules-of-hooks (0, useSync_1.useSync)(sync, basicFilter, options, hookOptions); dboTable.useSyncOne = (basicFilter, options, hookOptions) => // eslint-disable-next-line react-hooks/rules-of-hooks (0, useSync_1.useSync)(syncOne, basicFilter, options, hookOptions); } dboTable._sync = async function (param1, param2, syncHandles) { await (onDebug === null || onDebug === void 0 ? void 0 : onDebug({ type: "table", command: "_sync", tableName, data: { param1, param2, syncHandles }, })); return syncHandler.addSync({ tableName, command, param1, param2 }, syncHandles); }; } else if (subscribeCommands.includes(command)) { const subFunc = async function (param1 = {}, param2 = {}, onChange, onError) { await (onDebug === null || onDebug === void 0 ? void 0 : onDebug({ type: "table", command: command, tableName, data: { param1, param2, onChange, onError }, })); checkSubscriptionArgs(param1, param2, onChange, onError); return subscriptionHandler.addSub(db, { tableName, command, param1, param2 }, onChange, onError); }; dboTable[command] = subFunc; const SUBONE = "subscribeOne"; /** * React hooks */ const handlerName = command === "subscribe" ? "useSubscribe" : command === "subscribeOne" ? "useSubscribeOne" : undefined; if (handlerName) { dboTable[handlerName] = (filter, options, hookOptions) => // eslint-disable-next-line react-hooks/rules-of-hooks (0, useSubscribe_1.useSubscribe)(subFunc, command === SUBONE, filter, options, hookOptions); } if (command === SUBONE || !subscribeCommands.includes(SUBONE)) { dboTable[SUBONE] = async function (param1, param2, onChange) { await (onDebug === null || onDebug === void 0 ? void 0 : onDebug({ type: "table", command: "getSync", tableName, data: { param1, param2, onChange }, })); checkSubscriptionArgs(param1, param2, onChange); const onChangeOne = (rows) => { onChange(rows[0]); }; return subscriptionHandler.addSub(db, { tableName, command, param1, param2 }, onChangeOne); }; } } else { const method = async function (param1, param2, param3) { var _a; if (command === "getColumns" && !param1 && !param2 && !param3) { const columns = (_a = tableSchema === null || tableSchema === void 0 ? void 0 : tableSchema.find((t) => t.name === tableName)) === null || _a === void 0 ? void 0 : _a.columns; if (columns) return columns; } await (onDebug === null || onDebug === void 0 ? void 0 : onDebug({ type: "table", command: command, tableName, data: { param1, param2, param3 }, })); return new Promise((resolve, reject) => { socket.emit(preffix, { tableName, command, param1, param2, param3 }, /* Get col definition and re-cast data types?! */ (err, res) => { if (err) reject(err); else resolve(res); }); }); }; dboTable[command] = method; const reactHookName = command === "findOne" ? "useFindOne" : command === "find" ? "useFind" : command === "count" ? "useCount" : command === "size" ? "useSize" : undefined; if (reactHookName) { dboTable[reactHookName] = (param1, param2, hookOptions) => // eslint-disable-next-line react-hooks/rules-of-hooks (0, useFetch_1.useFetch)(method, [param1, param2], hookOptions); } } }); }); return { db: db }; }; exports.getDB = getDB;