prostgles-client 4.0.172 → 4.0.173

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,19 @@
1
+ import { type ClientSchema, type TableSchemaForClient } from "prostgles-types";
2
+ import { type InitOptions, type DBHandlerClient } from "./prostgles";
3
+ import type { SyncedTable } from "./SyncedTable/SyncedTable";
4
+ import type { getSyncHandler } from "./syncHandler";
5
+ import type { getSubscriptionHandler } from "./subscriptionHandler";
6
+ type Args = {
7
+ schema: TableSchemaForClient;
8
+ onDebug: InitOptions["onDebug"];
9
+ socket: InitOptions["socket"];
10
+ joinTables: ClientSchema["joinTables"];
11
+ syncedTable: typeof SyncedTable | undefined;
12
+ syncHandler: ReturnType<typeof getSyncHandler>;
13
+ subscriptionHandler: ReturnType<typeof getSubscriptionHandler>;
14
+ };
15
+ export declare const getDBO: ({ schema, onDebug, syncedTable, syncHandler, subscriptionHandler, socket, joinTables }: Args) => {
16
+ dbo: Partial<DBHandlerClient>;
17
+ };
18
+ export {};
19
+ //# sourceMappingURL=getDBO.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getDBO.d.ts","sourceRoot":"","sources":["../lib/getDBO.ts"],"names":[],"mappings":"AAAA,OAAO,EAA4B,KAAK,YAAY,EAA+B,KAAK,oBAAoB,EAAE,MAAM,iBAAiB,CAAC;AACtI,OAAO,EAAE,KAAK,WAAW,EAA6C,KAAK,eAAe,EAAmC,MAAM,aAAa,CAAC;AACjJ,OAAO,KAAK,EAAQ,WAAW,EAAwB,MAAM,2BAA2B,CAAC;AACzF,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AACpD,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,uBAAuB,CAAC;AAGpE,KAAK,IAAI,GAAG;IACV,MAAM,EAAE,oBAAoB,CAAC;IAC7B,OAAO,EAAE,WAAW,CAAC,SAAS,CAAC,CAAC;IAChC,MAAM,EAAE,WAAW,CAAC,QAAQ,CAAC,CAAC;IAC9B,UAAU,EAAE,YAAY,CAAC,YAAY,CAAC,CAAC;IACvC,WAAW,EAAE,OAAO,WAAW,GAAG,SAAS,CAAC;IAC5C,WAAW,EAAE,UAAU,CAAC,OAAO,cAAc,CAAC,CAAC;IAC/C,mBAAmB,EAAE,UAAU,CAAC,OAAO,sBAAsB,CAAC,CAAC;CAChE,CAAA;AAID,eAAO,MAAM,MAAM,2FAA4F,IAAI;;CAoIlH,CAAA"}
package/dist/getDBO.js ADDED
@@ -0,0 +1,131 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getDBO = void 0;
4
+ const prostgles_types_1 = require("prostgles-types");
5
+ const prostgles_1 = require("./prostgles");
6
+ const preffix = prostgles_types_1.CHANNELS._preffix;
7
+ const getDBO = ({ schema, onDebug, syncedTable, syncHandler, subscriptionHandler, socket, joinTables }) => {
8
+ const dbo = JSON.parse(JSON.stringify(schema));
9
+ /* Building DBO object */
10
+ const checkSubscriptionArgs = (basicFilter, options, onChange, onError) => {
11
+ 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") {
12
+ throw "Expecting: ( basicFilter<object>, options<object>, onChange<function> , onError?<function>) but got something else";
13
+ }
14
+ };
15
+ const sub_commands = ["subscribe", "subscribeOne"];
16
+ (0, prostgles_types_1.getKeys)(dbo).forEach(tableName => {
17
+ const all_commands = Object.keys(dbo[tableName]);
18
+ const dboTable = dbo[tableName];
19
+ all_commands
20
+ .sort((a, b) => sub_commands.includes(a) - sub_commands.includes(b))
21
+ .forEach(command => {
22
+ if (command === "sync") {
23
+ dboTable._syncInfo = { ...dboTable[command] };
24
+ if (syncedTable) {
25
+ dboTable.getSync = async (filter, params = {}) => {
26
+ await (onDebug === null || onDebug === void 0 ? void 0 : onDebug({ type: "table", command: "getSync", tableName, data: { filter, params } }));
27
+ return syncedTable.create({
28
+ name: tableName,
29
+ onDebug: onDebug,
30
+ filter,
31
+ db: dbo,
32
+ ...params
33
+ });
34
+ };
35
+ const upsertSyncTable = async (basicFilter = {}, options = {}, onError) => {
36
+ const syncName = `${tableName}.${JSON.stringify(basicFilter)}.${JSON.stringify((0, prostgles_types_1.omitKeys)(options, ["handlesOnData"]))}`;
37
+ if (!syncHandler.syncedTables[syncName]) {
38
+ syncHandler.syncedTables[syncName] = await syncedTable.create({
39
+ ...options,
40
+ onDebug: onDebug,
41
+ name: tableName,
42
+ filter: basicFilter,
43
+ db: dbo,
44
+ onError
45
+ });
46
+ }
47
+ return syncHandler.syncedTables[syncName];
48
+ };
49
+ const sync = async (basicFilter, options = { handlesOnData: true, select: "*" }, onChange, onError) => {
50
+ await (onDebug === null || onDebug === void 0 ? void 0 : onDebug({ type: "table", command: "sync", tableName, data: { basicFilter, options } }));
51
+ checkSubscriptionArgs(basicFilter, options, onChange, onError);
52
+ const s = await upsertSyncTable(basicFilter, options, onError);
53
+ return await s.sync(onChange, options.handlesOnData);
54
+ };
55
+ const syncOne = async (basicFilter, options = { handlesOnData: true }, onChange, onError) => {
56
+ await (onDebug === null || onDebug === void 0 ? void 0 : onDebug({ type: "table", command: "syncOne", tableName, data: { basicFilter, options } }));
57
+ checkSubscriptionArgs(basicFilter, options, onChange, onError);
58
+ const s = await upsertSyncTable(basicFilter, options, onError);
59
+ return await s.syncOne(basicFilter, onChange, options.handlesOnData);
60
+ };
61
+ dboTable.sync = sync;
62
+ dboTable.syncOne = syncOne;
63
+ // eslint-disable-next-line react-hooks/rules-of-hooks
64
+ dboTable.useSync = (basicFilter, options) => (0, prostgles_1.useSync)(sync, basicFilter, options);
65
+ // eslint-disable-next-line react-hooks/rules-of-hooks
66
+ dboTable.useSyncOne = (basicFilter, options) => (0, prostgles_1.useSync)(syncOne, basicFilter, options);
67
+ }
68
+ dboTable._sync = async function (param1, param2, syncHandles) {
69
+ await (onDebug === null || onDebug === void 0 ? void 0 : onDebug({ type: "table", command: "_sync", tableName, data: { param1, param2, syncHandles } }));
70
+ return syncHandler.addSync({ tableName, command, param1, param2 }, syncHandles);
71
+ };
72
+ }
73
+ else if (sub_commands.includes(command)) {
74
+ const subFunc = async function (param1 = {}, param2 = {}, onChange, onError) {
75
+ await (onDebug === null || onDebug === void 0 ? void 0 : onDebug({ type: "table", command: command, tableName, data: { param1, param2, onChange, onError } }));
76
+ checkSubscriptionArgs(param1, param2, onChange, onError);
77
+ return subscriptionHandler.addSub(dbo, { tableName, command, param1, param2 }, onChange, onError);
78
+ };
79
+ dboTable[command] = subFunc;
80
+ const SUBONE = "subscribeOne";
81
+ /**
82
+ * React hooks
83
+ */
84
+ const handlerName = command === "subscribe" ? "useSubscribe" : command === "subscribeOne" ? "useSubscribeOne" : undefined;
85
+ if (handlerName) {
86
+ // eslint-disable-next-line react-hooks/rules-of-hooks
87
+ dboTable[handlerName] = (filter, options) => (0, prostgles_1.useSubscribe)(subFunc, command === SUBONE, filter, options);
88
+ }
89
+ if (command === SUBONE || !sub_commands.includes(SUBONE)) {
90
+ dboTable[SUBONE] = async function (param1, param2, onChange, onError) {
91
+ await (onDebug === null || onDebug === void 0 ? void 0 : onDebug({ type: "table", command: "getSync", tableName, data: { param1, param2, onChange, onError } }));
92
+ checkSubscriptionArgs(param1, param2, onChange, onError);
93
+ const onChangeOne = (rows) => { onChange(rows[0]); };
94
+ return subscriptionHandler.addSub(dbo, { tableName, command, param1, param2 }, onChangeOne, onError);
95
+ };
96
+ }
97
+ }
98
+ else {
99
+ const method = async function (param1, param2, param3) {
100
+ await (onDebug === null || onDebug === void 0 ? void 0 : onDebug({ type: "table", command: command, tableName, data: { param1, param2, param3 } }));
101
+ return new Promise((resolve, reject) => {
102
+ socket.emit(preffix, { tableName, command, param1, param2, param3 },
103
+ /* Get col definition and re-cast data types?! */
104
+ (err, res) => {
105
+ if (err)
106
+ reject(err);
107
+ else
108
+ resolve(res);
109
+ });
110
+ });
111
+ };
112
+ dboTable[command] = method;
113
+ const methodName = command === "findOne" ? "useFindOne" : command === "find" ? "useFind" : command === "count" ? "useCount" : command === "size" ? "useSize" : undefined;
114
+ if (methodName) {
115
+ // eslint-disable-next-line react-hooks/rules-of-hooks
116
+ dboTable[methodName] = (param1, param2, param3) => (0, prostgles_1.useFetch)(method, [param1, param2, param3]);
117
+ }
118
+ if (["find", "findOne"].includes(command)) {
119
+ dboTable.getJoinedTables = function () {
120
+ return joinTables
121
+ .filter(tb => Array.isArray(tb) && tb.includes(tableName))
122
+ .flat()
123
+ .filter(t => t !== tableName);
124
+ };
125
+ }
126
+ }
127
+ });
128
+ });
129
+ return { dbo };
130
+ };
131
+ exports.getDBO = getDBO;
@@ -0,0 +1,6 @@
1
+ import type { ClientSchema, MethodHandler } from "prostgles-types";
2
+ import type { InitOptions } from "./prostgles";
3
+ export declare const getMethods: ({ onDebug, methods, socket }: Pick<InitOptions, "onDebug" | "socket"> & Pick<ClientSchema, "methods">) => {
4
+ methodsObj: MethodHandler;
5
+ };
6
+ //# sourceMappingURL=getMethods.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getMethods.d.ts","sourceRoot":"","sources":["../lib/getMethods.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAEnE,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAE/C,eAAO,MAAM,UAAU,iCAAkC,KAAK,WAAW,EAAE,SAAS,GAAG,QAAQ,CAAC,GAAG,KAAK,YAAY,EAAE,SAAS,CAAC;;CAyB/H,CAAA"}
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getMethods = void 0;
4
+ const prostgles_types_1 = require("prostgles-types");
5
+ const getMethods = ({ onDebug, methods, socket }) => {
6
+ let methodsObj = {};
7
+ const _methods = JSON.parse(JSON.stringify(methods));
8
+ _methods.map(method => {
9
+ /** New method def */
10
+ const isBasic = typeof method === "string";
11
+ const methodName = isBasic ? method : method.name;
12
+ const onRun = async function (...params) {
13
+ await (onDebug === null || onDebug === void 0 ? void 0 : onDebug({ type: "method", command: methodName, data: { params } }));
14
+ return new Promise((resolve, reject) => {
15
+ socket.emit(prostgles_types_1.CHANNELS.METHOD, { method: methodName, params }, (err, res) => {
16
+ if (err)
17
+ reject(err);
18
+ else
19
+ resolve(res);
20
+ });
21
+ });
22
+ };
23
+ methodsObj[methodName] = isBasic ? onRun : {
24
+ ...method,
25
+ run: onRun
26
+ };
27
+ });
28
+ methodsObj = Object.freeze(methodsObj);
29
+ return { methodsObj };
30
+ };
31
+ exports.getMethods = getMethods;
@@ -0,0 +1,6 @@
1
+ import { type SQLHandler } from "prostgles-types";
2
+ import type { InitOptions } from "./prostgles";
3
+ export declare const getSqlHandler: ({ socket }: Pick<InitOptions, "socket">) => {
4
+ sql: SQLHandler;
5
+ };
6
+ //# sourceMappingURL=getSqlHandler.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getSqlHandler.d.ts","sourceRoot":"","sources":["../lib/getSqlHandler.ts"],"names":[],"mappings":"AAAA,OAAO,EAAY,KAAK,UAAU,EAAwH,MAAM,iBAAiB,CAAC;AAClL,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAE/C,eAAO,MAAM,aAAa,eAAgB,KAAK,WAAW,EAAE,QAAQ,CAAC;;CA0KpE,CAAA"}
@@ -0,0 +1,167 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getSqlHandler = void 0;
4
+ const prostgles_types_1 = require("prostgles-types");
5
+ const getSqlHandler = ({ socket }) => {
6
+ let noticeSubs;
7
+ const notifSubs = {};
8
+ const removeNotifListener = (listener, conf, socket) => {
9
+ const channelSubs = notifSubs[conf.notifChannel];
10
+ if (channelSubs) {
11
+ channelSubs.listeners = channelSubs.listeners.filter(nl => nl !== listener);
12
+ if (!channelSubs.listeners.length && channelSubs.config.socketUnsubChannel && socket) {
13
+ socket.emit(channelSubs.config.socketUnsubChannel, {});
14
+ delete notifSubs[conf.notifChannel];
15
+ }
16
+ }
17
+ };
18
+ const addNotifListener = (listener, conf, socket) => {
19
+ var _a;
20
+ const channelSubs = notifSubs[conf.notifChannel];
21
+ if (!channelSubs) {
22
+ notifSubs[conf.notifChannel] = {
23
+ config: conf,
24
+ listeners: [listener]
25
+ };
26
+ socket.removeAllListeners(conf.socketChannel);
27
+ socket.on(conf.socketChannel, (notif) => {
28
+ var _a, _b;
29
+ if ((_a = notifSubs[conf.notifChannel]) === null || _a === void 0 ? void 0 : _a.listeners.length) {
30
+ notifSubs[conf.notifChannel].listeners.map(l => {
31
+ l(notif);
32
+ });
33
+ }
34
+ else {
35
+ socket.emit((_b = notifSubs[conf.notifChannel]) === null || _b === void 0 ? void 0 : _b.config.socketUnsubChannel, {});
36
+ }
37
+ });
38
+ }
39
+ else {
40
+ (_a = notifSubs[conf.notifChannel]) === null || _a === void 0 ? void 0 : _a.listeners.push(listener);
41
+ }
42
+ };
43
+ const removeNoticeListener = (listener, socket) => {
44
+ if (noticeSubs) {
45
+ noticeSubs.listeners = noticeSubs.listeners.filter(nl => nl !== listener);
46
+ if (!noticeSubs.listeners.length && noticeSubs.config.socketUnsubChannel && socket) {
47
+ socket.emit(noticeSubs.config.socketUnsubChannel, {});
48
+ }
49
+ }
50
+ };
51
+ const addNoticeListener = (listener, conf, socket) => {
52
+ noticeSubs !== null && noticeSubs !== void 0 ? noticeSubs : (noticeSubs = {
53
+ config: conf,
54
+ listeners: []
55
+ });
56
+ if (!noticeSubs.listeners.length) {
57
+ socket.removeAllListeners(conf.socketChannel);
58
+ socket.on(conf.socketChannel, (notice) => {
59
+ if (noticeSubs && noticeSubs.listeners.length) {
60
+ noticeSubs.listeners.map(l => {
61
+ l(notice);
62
+ });
63
+ }
64
+ else {
65
+ socket.emit(conf.socketUnsubChannel, {});
66
+ }
67
+ });
68
+ }
69
+ noticeSubs.listeners.push(listener);
70
+ };
71
+ const sql = function (query, params, options) {
72
+ return new Promise((resolve, reject) => {
73
+ socket.emit(prostgles_types_1.CHANNELS.SQL, { query, params, options }, (err, res) => {
74
+ if (err) {
75
+ return reject(err);
76
+ }
77
+ if ((options === null || options === void 0 ? void 0 : options.returnType) === "stream") {
78
+ const { channel, unsubChannel } = res;
79
+ const start = (listener) => new Promise((resolveStart, rejectStart) => {
80
+ socket.on(channel, listener);
81
+ socket.emit(channel, {}, (pid, err) => {
82
+ if (err) {
83
+ rejectStart(err);
84
+ socket.removeAllListeners(channel);
85
+ }
86
+ else {
87
+ resolveStart({
88
+ pid,
89
+ run: (query, params) => {
90
+ return new Promise((resolveRun, rejectRun) => {
91
+ socket.emit(channel, { query, params }, (data, _err) => {
92
+ if (_err) {
93
+ rejectRun(_err);
94
+ }
95
+ else {
96
+ resolveRun(data);
97
+ }
98
+ });
99
+ });
100
+ },
101
+ stop: (terminate) => {
102
+ return new Promise((resolveStop, rejectStop) => {
103
+ socket.emit(unsubChannel, { terminate }, (data, _err) => {
104
+ if (_err) {
105
+ rejectStop(_err);
106
+ }
107
+ else {
108
+ resolveStop(data);
109
+ }
110
+ });
111
+ });
112
+ }
113
+ });
114
+ }
115
+ });
116
+ });
117
+ const streamHandlers = {
118
+ channel,
119
+ unsubChannel,
120
+ start,
121
+ };
122
+ return resolve(streamHandlers);
123
+ }
124
+ else if (options &&
125
+ (options.returnType === "noticeSubscription") &&
126
+ res &&
127
+ Object.keys(res).sort().join() === ["socketChannel", "socketUnsubChannel"].sort().join() &&
128
+ !Object.values(res).find(v => typeof v !== "string")) {
129
+ const sockInfo = res;
130
+ const addListener = (listener) => {
131
+ addNoticeListener(listener, sockInfo, socket);
132
+ return {
133
+ ...sockInfo,
134
+ removeListener: () => removeNoticeListener(listener, socket)
135
+ };
136
+ };
137
+ const handle = {
138
+ ...sockInfo,
139
+ addListener
140
+ };
141
+ // @ts-ignore
142
+ resolve(handle);
143
+ }
144
+ else if ((!options || !options.returnType || options.returnType !== "statement") &&
145
+ res &&
146
+ Object.keys(res).sort().join() === ["socketChannel", "socketUnsubChannel", "notifChannel"].sort().join() &&
147
+ !Object.values(res).find(v => typeof v !== "string")) {
148
+ const sockInfo = res;
149
+ const addListener = (listener) => {
150
+ addNotifListener(listener, sockInfo, socket);
151
+ return {
152
+ ...res,
153
+ removeListener: () => removeNotifListener(listener, sockInfo, socket)
154
+ };
155
+ };
156
+ const handle = { ...res, addListener };
157
+ resolve(handle);
158
+ }
159
+ else {
160
+ resolve(res);
161
+ }
162
+ });
163
+ });
164
+ };
165
+ return { sql };
166
+ };
167
+ exports.getSqlHandler = getSqlHandler;