UNPKG

prostgles-client

Version:

Reactive client for Postgres

183 lines (182 loc) 9.93 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getDBO = void 0; const prostgles_types_1 = require("prostgles-types"); const prostgles_1 = require("./prostgles"); const SyncedTable_1 = require("./SyncedTable/SyncedTable"); const preffix = prostgles_types_1.CHANNELS._preffix; const getDBO = ({ schema, tableSchema, onDebug, syncedTable, syncHandler, subscriptionHandler, socket, }) => { /* Building DBO 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 dbo = {}; const schemaClone = (0, SyncedTable_1.quickClone)(schema); (0, prostgles_types_1.getObjectEntries)(schemaClone).forEach(([tableName, methods]) => { const allowedCommands = (0, prostgles_types_1.getKeys)(methods); dbo[tableName] = {}; const dboTable = dbo[tableName]; allowedCommands .sort((a, b) => subscribeCommands.includes(a) - subscribeCommands.includes(b)) .forEach((command) => { if (command === "sync") { dboTable._syncInfo = { ...methods[command] }; 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: dbo, ...params, }); }; const upsertSyncTable = async (basicFilter = {}, options = {}, onError) => { const syncName = `${tableName}.${JSON.stringify(basicFilter)}.${JSON.stringify((0, prostgles_types_1.omitKeys)(options, ["handlesOnData"]))}`; if (!syncHandler.syncedTables[syncName]) { syncHandler.syncedTables[syncName] = await syncedTable.create({ ...options, onDebug: onDebug, name: tableName, filter: basicFilter, db: dbo, onError, }); } return syncHandler.syncedTables[syncName]; }; 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, prostgles_1.useSync)(sync, basicFilter, options, hookOptions); dboTable.useSyncOne = (basicFilter, options, hookOptions) => // eslint-disable-next-line react-hooks/rules-of-hooks (0, prostgles_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(dbo, { 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, prostgles_1.useSubscribe)(subFunc, command === SUBONE, filter, options, hookOptions); } if (command === SUBONE || !subscribeCommands.includes(SUBONE)) { dboTable[SUBONE] = async function (param1, param2, onChange, onError) { await (onDebug === null || onDebug === void 0 ? void 0 : onDebug({ type: "table", command: "getSync", tableName, data: { param1, param2, onChange, onError }, })); checkSubscriptionArgs(param1, param2, onChange, onError); const onChangeOne = (rows) => { onChange(rows[0]); }; return subscriptionHandler.addSub(dbo, { tableName, command, param1, param2 }, onChangeOne, onError); }; } } 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 methodName = command === "findOne" ? "useFindOne" : command === "find" ? "useFind" : command === "count" ? "useCount" : command === "size" ? "useSize" : undefined; if (methodName) { dboTable[methodName] = (param1, param2, hookOptions) => // eslint-disable-next-line react-hooks/rules-of-hooks (0, prostgles_1.useFetch)(method, [param1, param2], hookOptions); } } }); }); return { dbo }; }; exports.getDBO = getDBO;