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.
- package/dist/getDBO.d.ts +19 -0
- package/dist/getDBO.d.ts.map +1 -0
- package/dist/getDBO.js +131 -0
- package/dist/getMethods.d.ts +6 -0
- package/dist/getMethods.d.ts.map +1 -0
- package/dist/getMethods.js +31 -0
- package/dist/getSqlHandler.d.ts +6 -0
- package/dist/getSqlHandler.d.ts.map +1 -0
- package/dist/getSqlHandler.js +167 -0
- package/dist/index.js +1 -1
- package/dist/index.no-sync.js +1 -1
- package/dist/prostgles.d.ts +7 -2
- package/dist/prostgles.d.ts.map +1 -1
- package/dist/prostgles.js +21 -150
- package/lib/getDBO.ts +152 -0
- package/lib/getMethods.ts +30 -0
- package/lib/getSqlHandler.ts +174 -0
- package/lib/prostgles.ts +35 -169
- package/package.json +1 -1
- package/tests/package-lock.json +1 -1
- package/dist/SQL.d.ts +0 -25
- package/dist/SQL.d.ts.map +0 -1
- package/dist/SQL.js +0 -172
- package/lib/SQL.ts +0 -178
package/dist/prostgles.d.ts
CHANGED
|
@@ -4,9 +4,9 @@ import { type AuthHandler } from "./Auth";
|
|
|
4
4
|
import type { Sync, SyncDataItem, SyncOne, SyncOneOptions, SyncOptions, SyncedTable } from "./SyncedTable/SyncedTable";
|
|
5
5
|
export declare const hasWnd: boolean;
|
|
6
6
|
export declare const debug: any;
|
|
7
|
-
export { MethodHandler, SQLResult, asName };
|
|
8
7
|
export * from "./react-hooks";
|
|
9
8
|
export * from "./useProstglesClient";
|
|
9
|
+
export { MethodHandler, SQLResult, asName };
|
|
10
10
|
export type ViewHandlerClient<T extends AnyObject = AnyObject, S extends DBSchema | void = void> = ViewHandler<T, S> & {
|
|
11
11
|
getJoinedTables: () => string[];
|
|
12
12
|
_syncInfo?: any;
|
|
@@ -75,7 +75,7 @@ export type DBHandlerClient<Schema = void> = (Schema extends DBSchema ? {
|
|
|
75
75
|
sql?: SQLHandler;
|
|
76
76
|
} & DbJoinMaker;
|
|
77
77
|
type OnReadyArgs = {
|
|
78
|
-
dbo: DBHandlerClient
|
|
78
|
+
dbo: DBHandlerClient | any;
|
|
79
79
|
methods: MethodHandler | undefined;
|
|
80
80
|
tableSchema: DBSchemaTable[] | undefined;
|
|
81
81
|
auth: AuthHandler;
|
|
@@ -108,12 +108,17 @@ type DebugEvent = {
|
|
|
108
108
|
} | SyncDebugEvent | {
|
|
109
109
|
type: "schemaChanged";
|
|
110
110
|
data: ClientSchema;
|
|
111
|
+
state: "connected" | "disconnected" | "reconnected" | undefined;
|
|
111
112
|
} | {
|
|
112
113
|
type: "onReady";
|
|
113
114
|
data: OnReadyArgs;
|
|
114
115
|
} | {
|
|
115
116
|
type: "onReady.notMounted";
|
|
116
117
|
data: OnReadyArgs;
|
|
118
|
+
} | {
|
|
119
|
+
type: "onReady.call";
|
|
120
|
+
data: OnReadyArgs;
|
|
121
|
+
state: "connected" | "disconnected" | "reconnected" | undefined;
|
|
117
122
|
};
|
|
118
123
|
export type InitOptions<DBSchema = void> = {
|
|
119
124
|
socket: any;
|
package/dist/prostgles.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prostgles.d.ts","sourceRoot":"","sources":["../lib/prostgles.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EACV,SAAS,EACT,YAAY,EACZ,iBAAiB,EACjB,QAAQ,EACR,aAAa,EACb,WAAW,EACX,cAAc,EACd,UAAU,EACV,mBAAmB,EACnB,aAAa,EACb,UAAU,EACV,SAAS,EACT,YAAY,EACZ,eAAe,EACf,YAAY,EACZ,WAAW,EACZ,MAAM,iBAAiB,CAAC;AAEzB,OAAO,EAEL,MAAM,
|
|
1
|
+
{"version":3,"file":"prostgles.d.ts","sourceRoot":"","sources":["../lib/prostgles.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EACV,SAAS,EACT,YAAY,EACZ,iBAAiB,EACjB,QAAQ,EACR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AC,EAAE,CAAC,MAAM,EAAE,GAAG,EAAE,KAAK,CAAC,EAAE,GAAG,KAAK,GAAG,CAAC;IAChD,YAAY,CAAC,EAAE,MAAM,GAAG,CAAC;IAEzB,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,UAAU,KAAK,GAAG,CAAC;CACtC,CAAA;AAED,MAAM,MAAM,WAAW,GAAG,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,GAAG,CAAC;AAElD,MAAM,MAAM,UAAU,GAAG;IACvB,SAAS,EAAE,MAAM,CAAC;IAClB,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,SAAS,CAAC;IACnB,MAAM,CAAC,EAAE,SAAS,CAAC;CACpB,CAAC;AAEF,MAAM,MAAM,eAAe,GAAG;IAAE,IAAI,EAAE,MAAM,EAAE,CAAC;IAAC,QAAQ,EAAE,OAAO,CAAA;CAAE,CAAA;AAEnE,MAAM,MAAM,QAAQ,GAAG;IACrB,SAAS,EAAE,MAAM,EAAE,CAAC;IACpB,YAAY,EAAE,MAAM,CAAC;IACrB,WAAW,EAAE,MAAM,CAAA;CACpB,CAAA;AAOD,wBAAgB,SAAS,CAAC,QAAQ,EAAE,QAAQ,EAAE,WAAW,CAAC,QAAQ,CAAC,EAAE,WAAW,EAAE,OAAO,WAAW,GAAG,SAAS,oBA2H/G"}
|
package/dist/prostgles.js
CHANGED
|
@@ -22,8 +22,10 @@ exports.prostgles = exports.asName = exports.debug = exports.hasWnd = void 0;
|
|
|
22
22
|
const prostgles_types_1 = require("prostgles-types");
|
|
23
23
|
Object.defineProperty(exports, "asName", { enumerable: true, get: function () { return prostgles_types_1.asName; } });
|
|
24
24
|
const Auth_1 = require("./Auth");
|
|
25
|
+
const getDBO_1 = require("./getDBO");
|
|
26
|
+
const getMethods_1 = require("./getMethods");
|
|
27
|
+
const getSqlHandler_1 = require("./getSqlHandler");
|
|
25
28
|
const react_hooks_1 = require("./react-hooks");
|
|
26
|
-
const SQL_1 = require("./SQL");
|
|
27
29
|
const subscriptionHandler_1 = require("./subscriptionHandler");
|
|
28
30
|
const syncHandler_1 = require("./syncHandler");
|
|
29
31
|
const DEBUG_KEY = "DEBUG_SYNCEDTABLE";
|
|
@@ -49,11 +51,10 @@ function prostgles(initOpts, syncedTable) {
|
|
|
49
51
|
if (cb)
|
|
50
52
|
socket.on(prostgles_types_1.CHANNELS.SCHEMA_CHANGED, cb);
|
|
51
53
|
}
|
|
52
|
-
const preffix = prostgles_types_1.CHANNELS._preffix;
|
|
53
54
|
const subscriptionHandler = (0, subscriptionHandler_1.getSubscriptionHandler)(initOpts);
|
|
54
55
|
const syncHandler = (0, syncHandler_1.getSyncHandler)(initOpts);
|
|
55
56
|
let state;
|
|
56
|
-
const
|
|
57
|
+
const sqlHandler = (0, getSqlHandler_1.getSqlHandler)({ socket });
|
|
57
58
|
return new Promise((resolve, reject) => {
|
|
58
59
|
socket.removeAllListeners(prostgles_types_1.CHANNELS.CONNECTION);
|
|
59
60
|
socket.on(prostgles_types_1.CHANNELS.CONNECTION, error => {
|
|
@@ -76,18 +77,20 @@ function prostgles(initOpts, syncedTable) {
|
|
|
76
77
|
}
|
|
77
78
|
/* Schema = published schema */
|
|
78
79
|
socket.on(prostgles_types_1.CHANNELS.SCHEMA, async (args) => {
|
|
79
|
-
await (onDebug === null || onDebug === void 0 ? void 0 : onDebug({ type: "schemaChanged", data: args }));
|
|
80
|
+
await (onDebug === null || onDebug === void 0 ? void 0 : onDebug({ type: "schemaChanged", data: args, state }));
|
|
80
81
|
const { joinTables = [], ...clientSchema } = args;
|
|
81
82
|
const { schema, methods, tableSchema, auth: authConfig, rawSQL, err } = clientSchema;
|
|
82
83
|
/** Only destroy existing syncs if schema changed */
|
|
83
84
|
const schemaDidNotChange = (schemaAge === null || schemaAge === void 0 ? void 0 : schemaAge.clientSchema) && (0, react_hooks_1.isEqual)(schemaAge.clientSchema, clientSchema);
|
|
84
85
|
if (!schemaDidNotChange) {
|
|
85
|
-
await syncHandler.destroySyncs();
|
|
86
|
+
await syncHandler.destroySyncs().catch(error => console.error("Error while destroying syncs", error));
|
|
87
|
+
}
|
|
88
|
+
if (err) {
|
|
89
|
+
console.error("Error on schema change:", err);
|
|
86
90
|
}
|
|
87
91
|
if ((state === "connected" || state === "reconnected") && onReconnect) {
|
|
88
92
|
onReconnect(socket, err);
|
|
89
93
|
if (err) {
|
|
90
|
-
console.error(err);
|
|
91
94
|
return;
|
|
92
95
|
}
|
|
93
96
|
schemaAge = { origin: "onReconnect", date: new Date(), clientSchema };
|
|
@@ -101,154 +104,20 @@ function prostgles(initOpts, syncedTable) {
|
|
|
101
104
|
}
|
|
102
105
|
const isReconnect = state === "reconnected";
|
|
103
106
|
state = "connected";
|
|
104
|
-
const dbo = JSON.parse(JSON.stringify(schema));
|
|
105
|
-
const _methods = JSON.parse(JSON.stringify(methods));
|
|
106
|
-
let methodsObj = {};
|
|
107
107
|
const auth = (0, Auth_1.setupAuth)({ authData: authConfig, socket, onReload });
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
reject(err);
|
|
118
|
-
else
|
|
119
|
-
resolve(res);
|
|
120
|
-
});
|
|
121
|
-
});
|
|
122
|
-
};
|
|
123
|
-
methodsObj[methodName] = isBasic ? onRun : {
|
|
124
|
-
...method,
|
|
125
|
-
run: onRun
|
|
126
|
-
};
|
|
108
|
+
const { methodsObj } = (0, getMethods_1.getMethods)({ onDebug, methods, socket });
|
|
109
|
+
const { dbo } = (0, getDBO_1.getDBO)({
|
|
110
|
+
schema,
|
|
111
|
+
onDebug,
|
|
112
|
+
syncedTable,
|
|
113
|
+
syncHandler,
|
|
114
|
+
subscriptionHandler,
|
|
115
|
+
socket,
|
|
116
|
+
joinTables,
|
|
127
117
|
});
|
|
128
|
-
methodsObj = Object.freeze(methodsObj);
|
|
129
118
|
if (rawSQL) {
|
|
130
|
-
sql
|
|
119
|
+
dbo.sql = sqlHandler.sql;
|
|
131
120
|
}
|
|
132
|
-
/* Building DBO object */
|
|
133
|
-
const checkSubscriptionArgs = (basicFilter, options, onChange, onError) => {
|
|
134
|
-
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") {
|
|
135
|
-
throw "Expecting: ( basicFilter<object>, options<object>, onChange<function> , onError?<function>) but got something else";
|
|
136
|
-
}
|
|
137
|
-
};
|
|
138
|
-
const sub_commands = ["subscribe", "subscribeOne"];
|
|
139
|
-
(0, prostgles_types_1.getKeys)(dbo).forEach(tableName => {
|
|
140
|
-
const all_commands = Object.keys(dbo[tableName]);
|
|
141
|
-
const dboTable = dbo[tableName];
|
|
142
|
-
all_commands
|
|
143
|
-
.sort((a, b) => sub_commands.includes(a) - sub_commands.includes(b))
|
|
144
|
-
.forEach(command => {
|
|
145
|
-
if (command === "sync") {
|
|
146
|
-
dboTable._syncInfo = { ...dboTable[command] };
|
|
147
|
-
if (syncedTable) {
|
|
148
|
-
dboTable.getSync = async (filter, params = {}) => {
|
|
149
|
-
await (onDebug === null || onDebug === void 0 ? void 0 : onDebug({ type: "table", command: "getSync", tableName, data: { filter, params } }));
|
|
150
|
-
return syncedTable.create({
|
|
151
|
-
name: tableName,
|
|
152
|
-
onDebug: onDebug,
|
|
153
|
-
filter,
|
|
154
|
-
db: dbo,
|
|
155
|
-
...params
|
|
156
|
-
});
|
|
157
|
-
};
|
|
158
|
-
const upsertSyncTable = async (basicFilter = {}, options = {}, onError) => {
|
|
159
|
-
const syncName = `${tableName}.${JSON.stringify(basicFilter)}.${JSON.stringify((0, prostgles_types_1.omitKeys)(options, ["handlesOnData"]))}`;
|
|
160
|
-
if (!syncHandler.syncedTables[syncName]) {
|
|
161
|
-
syncHandler.syncedTables[syncName] = await syncedTable.create({
|
|
162
|
-
...options,
|
|
163
|
-
onDebug: onDebug,
|
|
164
|
-
name: tableName,
|
|
165
|
-
filter: basicFilter,
|
|
166
|
-
db: dbo,
|
|
167
|
-
onError
|
|
168
|
-
});
|
|
169
|
-
}
|
|
170
|
-
return syncHandler.syncedTables[syncName];
|
|
171
|
-
};
|
|
172
|
-
const sync = async (basicFilter, options = { handlesOnData: true, select: "*" }, onChange, onError) => {
|
|
173
|
-
await (onDebug === null || onDebug === void 0 ? void 0 : onDebug({ type: "table", command: "sync", tableName, data: { basicFilter, options } }));
|
|
174
|
-
checkSubscriptionArgs(basicFilter, options, onChange, onError);
|
|
175
|
-
const s = await upsertSyncTable(basicFilter, options, onError);
|
|
176
|
-
return await s.sync(onChange, options.handlesOnData);
|
|
177
|
-
};
|
|
178
|
-
const syncOne = async (basicFilter, options = { handlesOnData: true }, onChange, onError) => {
|
|
179
|
-
await (onDebug === null || onDebug === void 0 ? void 0 : onDebug({ type: "table", command: "syncOne", tableName, data: { basicFilter, options } }));
|
|
180
|
-
checkSubscriptionArgs(basicFilter, options, onChange, onError);
|
|
181
|
-
const s = await upsertSyncTable(basicFilter, options, onError);
|
|
182
|
-
return await s.syncOne(basicFilter, onChange, options.handlesOnData);
|
|
183
|
-
};
|
|
184
|
-
dboTable.sync = sync;
|
|
185
|
-
dboTable.syncOne = syncOne;
|
|
186
|
-
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
187
|
-
dboTable.useSync = (basicFilter, options) => (0, react_hooks_1.useSync)(sync, basicFilter, options);
|
|
188
|
-
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
189
|
-
dboTable.useSyncOne = (basicFilter, options) => (0, react_hooks_1.useSync)(syncOne, basicFilter, options);
|
|
190
|
-
}
|
|
191
|
-
dboTable._sync = async function (param1, param2, syncHandles) {
|
|
192
|
-
await (onDebug === null || onDebug === void 0 ? void 0 : onDebug({ type: "table", command: "_sync", tableName, data: { param1, param2, syncHandles } }));
|
|
193
|
-
return syncHandler.addSync({ tableName, command, param1, param2 }, syncHandles);
|
|
194
|
-
};
|
|
195
|
-
}
|
|
196
|
-
else if (sub_commands.includes(command)) {
|
|
197
|
-
const subFunc = async function (param1 = {}, param2 = {}, onChange, onError) {
|
|
198
|
-
await (onDebug === null || onDebug === void 0 ? void 0 : onDebug({ type: "table", command: command, tableName, data: { param1, param2, onChange, onError } }));
|
|
199
|
-
checkSubscriptionArgs(param1, param2, onChange, onError);
|
|
200
|
-
return subscriptionHandler.addSub(dbo, { tableName, command, param1, param2 }, onChange, onError);
|
|
201
|
-
};
|
|
202
|
-
dboTable[command] = subFunc;
|
|
203
|
-
const SUBONE = "subscribeOne";
|
|
204
|
-
/**
|
|
205
|
-
* React hooks
|
|
206
|
-
*/
|
|
207
|
-
const handlerName = command === "subscribe" ? "useSubscribe" : command === "subscribeOne" ? "useSubscribeOne" : undefined;
|
|
208
|
-
if (handlerName) {
|
|
209
|
-
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
210
|
-
dboTable[handlerName] = (filter, options) => (0, react_hooks_1.useSubscribe)(subFunc, command === SUBONE, filter, options);
|
|
211
|
-
}
|
|
212
|
-
if (command === SUBONE || !sub_commands.includes(SUBONE)) {
|
|
213
|
-
dboTable[SUBONE] = async function (param1, param2, onChange, onError) {
|
|
214
|
-
await (onDebug === null || onDebug === void 0 ? void 0 : onDebug({ type: "table", command: "getSync", tableName, data: { param1, param2, onChange, onError } }));
|
|
215
|
-
checkSubscriptionArgs(param1, param2, onChange, onError);
|
|
216
|
-
const onChangeOne = (rows) => { onChange(rows[0]); };
|
|
217
|
-
return subscriptionHandler.addSub(dbo, { tableName, command, param1, param2 }, onChangeOne, onError);
|
|
218
|
-
};
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
else {
|
|
222
|
-
const method = async function (param1, param2, param3) {
|
|
223
|
-
await (onDebug === null || onDebug === void 0 ? void 0 : onDebug({ type: "table", command: command, tableName, data: { param1, param2, param3 } }));
|
|
224
|
-
return new Promise((resolve, reject) => {
|
|
225
|
-
socket.emit(preffix, { tableName, command, param1, param2, param3 },
|
|
226
|
-
/* Get col definition and re-cast data types?! */
|
|
227
|
-
(err, res) => {
|
|
228
|
-
if (err)
|
|
229
|
-
reject(err);
|
|
230
|
-
else
|
|
231
|
-
resolve(res);
|
|
232
|
-
});
|
|
233
|
-
});
|
|
234
|
-
};
|
|
235
|
-
dboTable[command] = method;
|
|
236
|
-
const methodName = command === "findOne" ? "useFindOne" : command === "find" ? "useFind" : command === "count" ? "useCount" : command === "size" ? "useSize" : undefined;
|
|
237
|
-
if (methodName) {
|
|
238
|
-
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
239
|
-
dboTable[methodName] = (param1, param2, param3) => (0, react_hooks_1.useFetch)(method, [param1, param2, param3]);
|
|
240
|
-
}
|
|
241
|
-
if (["find", "findOne"].includes(command)) {
|
|
242
|
-
dboTable.getJoinedTables = function () {
|
|
243
|
-
return joinTables
|
|
244
|
-
.filter(tb => Array.isArray(tb) && tb.includes(tableName))
|
|
245
|
-
.flat()
|
|
246
|
-
.filter(t => t !== tableName);
|
|
247
|
-
};
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
});
|
|
251
|
-
});
|
|
252
121
|
await subscriptionHandler.reAttachAll();
|
|
253
122
|
await syncHandler.reAttachAll();
|
|
254
123
|
joinTables.flat().map(table => {
|
|
@@ -266,6 +135,8 @@ function prostgles(initOpts, syncedTable) {
|
|
|
266
135
|
});
|
|
267
136
|
(async () => {
|
|
268
137
|
try {
|
|
138
|
+
const onReadyArgs = { dbo, methods: methodsObj, tableSchema, auth, isReconnect };
|
|
139
|
+
await (onDebug === null || onDebug === void 0 ? void 0 : onDebug({ type: "onReady.call", data: onReadyArgs, state }));
|
|
269
140
|
await onReady(dbo, methodsObj, tableSchema, auth, isReconnect);
|
|
270
141
|
}
|
|
271
142
|
catch (err) {
|
package/lib/getDBO.ts
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { type AnyObject, CHANNELS, type ClientSchema, getKeys, isObject, omitKeys, type TableSchemaForClient } from "prostgles-types";
|
|
2
|
+
import { type InitOptions, type TableHandlerClient, type AnyFunction, type DBHandlerClient, useSync, useSubscribe, useFetch } from "./prostgles";
|
|
3
|
+
import type { Sync, SyncedTable, SyncOne, SyncOptions } from "./SyncedTable/SyncedTable";
|
|
4
|
+
import type { getSyncHandler } from "./syncHandler";
|
|
5
|
+
import type { getSubscriptionHandler } from "./subscriptionHandler";
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
type Args = {
|
|
9
|
+
schema: TableSchemaForClient;
|
|
10
|
+
onDebug: InitOptions["onDebug"];
|
|
11
|
+
socket: InitOptions["socket"];
|
|
12
|
+
joinTables: ClientSchema["joinTables"];
|
|
13
|
+
syncedTable: typeof SyncedTable | undefined;
|
|
14
|
+
syncHandler: ReturnType<typeof getSyncHandler>;
|
|
15
|
+
subscriptionHandler: ReturnType<typeof getSubscriptionHandler>;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const preffix = CHANNELS._preffix;
|
|
19
|
+
|
|
20
|
+
export const getDBO = ({ schema, onDebug, syncedTable, syncHandler, subscriptionHandler, socket, joinTables }: Args) => {
|
|
21
|
+
|
|
22
|
+
const dbo: Partial<DBHandlerClient> = JSON.parse(JSON.stringify(schema));
|
|
23
|
+
|
|
24
|
+
/* Building DBO object */
|
|
25
|
+
const checkSubscriptionArgs = (basicFilter: AnyObject | undefined, options: AnyObject | undefined, onChange: AnyFunction, onError?: AnyFunction) => {
|
|
26
|
+
if (basicFilter !== undefined && !isObject(basicFilter) || options !== undefined && !isObject(options) || !(typeof onChange === "function") || onError !== undefined && typeof onError !== "function") {
|
|
27
|
+
throw "Expecting: ( basicFilter<object>, options<object>, onChange<function> , onError?<function>) but got something else";
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
const sub_commands = ["subscribe", "subscribeOne"] as const;
|
|
31
|
+
getKeys(dbo).forEach(tableName => {
|
|
32
|
+
const all_commands = Object.keys(dbo[tableName]!);
|
|
33
|
+
|
|
34
|
+
const dboTable = dbo[tableName] as TableHandlerClient;
|
|
35
|
+
all_commands
|
|
36
|
+
.sort((a, b) => <never>sub_commands.includes(a as any) - <never>sub_commands.includes(b as any))
|
|
37
|
+
.forEach(command => {
|
|
38
|
+
|
|
39
|
+
if (command === "sync") {
|
|
40
|
+
dboTable._syncInfo = { ...dboTable[command] };
|
|
41
|
+
if (syncedTable) {
|
|
42
|
+
dboTable.getSync = async (filter, params = {}) => {
|
|
43
|
+
await onDebug?.({ type: "table", command: "getSync", tableName, data: { filter, params } });
|
|
44
|
+
return syncedTable.create({
|
|
45
|
+
name: tableName,
|
|
46
|
+
onDebug: onDebug as any,
|
|
47
|
+
filter,
|
|
48
|
+
db: dbo,
|
|
49
|
+
...params
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
const upsertSyncTable = async (basicFilter = {}, options: SyncOptions = {}, onError) => {
|
|
53
|
+
const syncName = `${tableName}.${JSON.stringify(basicFilter)}.${JSON.stringify(omitKeys(options, ["handlesOnData"]))}`
|
|
54
|
+
if (!syncHandler.syncedTables[syncName]) {
|
|
55
|
+
syncHandler.syncedTables[syncName] = await syncedTable.create({
|
|
56
|
+
...options,
|
|
57
|
+
onDebug: onDebug as any,
|
|
58
|
+
name: tableName,
|
|
59
|
+
filter: basicFilter,
|
|
60
|
+
db: dbo,
|
|
61
|
+
onError
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
return syncHandler.syncedTables[syncName]
|
|
65
|
+
}
|
|
66
|
+
const sync: Sync<AnyObject> = async (basicFilter, options = { handlesOnData: true, select: "*" }, onChange, onError) => {
|
|
67
|
+
await onDebug?.({ type: "table", command: "sync", tableName, data: { basicFilter, options } });
|
|
68
|
+
checkSubscriptionArgs(basicFilter, options, onChange, onError);
|
|
69
|
+
const s = await upsertSyncTable(basicFilter, options, onError);
|
|
70
|
+
return await s.sync(onChange, options.handlesOnData);
|
|
71
|
+
}
|
|
72
|
+
const syncOne: SyncOne<AnyObject> = async (basicFilter, options = { handlesOnData: true }, onChange, onError) => {
|
|
73
|
+
await onDebug?.({ type: "table", command: "syncOne", tableName, data: { basicFilter, options } });
|
|
74
|
+
checkSubscriptionArgs(basicFilter, options, onChange, onError);
|
|
75
|
+
const s = await upsertSyncTable(basicFilter, options, onError);
|
|
76
|
+
return await s.syncOne(basicFilter, onChange, options.handlesOnData);
|
|
77
|
+
}
|
|
78
|
+
dboTable.sync = sync;
|
|
79
|
+
dboTable.syncOne = syncOne;
|
|
80
|
+
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
81
|
+
dboTable.useSync = (basicFilter, options) => useSync(sync, basicFilter, options) as any;
|
|
82
|
+
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
83
|
+
dboTable.useSyncOne = (basicFilter, options) => useSync(syncOne, basicFilter, options) as any;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
dboTable._sync = async function (param1, param2, syncHandles) {
|
|
87
|
+
await onDebug?.({ type: "table", command: "_sync", tableName, data: { param1, param2, syncHandles } });
|
|
88
|
+
return syncHandler.addSync({ tableName, command, param1, param2 }, syncHandles);
|
|
89
|
+
}
|
|
90
|
+
} else if (sub_commands.includes(command as any)) {
|
|
91
|
+
const subFunc = async function (param1 = {}, param2 = {}, onChange, onError) {
|
|
92
|
+
await onDebug?.({ type: "table", command: command as typeof sub_commands[number], tableName, data: { param1, param2, onChange, onError } });
|
|
93
|
+
checkSubscriptionArgs(param1, param2, onChange, onError);
|
|
94
|
+
return subscriptionHandler.addSub(dbo, { tableName, command, param1, param2 }, onChange, onError);
|
|
95
|
+
};
|
|
96
|
+
dboTable[command] = subFunc;
|
|
97
|
+
const SUBONE = "subscribeOne";
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* React hooks
|
|
101
|
+
*/
|
|
102
|
+
const handlerName = command === "subscribe" ? "useSubscribe" : command === "subscribeOne"? "useSubscribeOne" : undefined;
|
|
103
|
+
if(handlerName){
|
|
104
|
+
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
105
|
+
dboTable[handlerName] = (filter, options) => useSubscribe(subFunc, command === SUBONE, filter, options)
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
if (command === SUBONE || !sub_commands.includes(SUBONE)) {
|
|
109
|
+
dboTable[SUBONE] = async function (param1, param2, onChange, onError) {
|
|
110
|
+
await onDebug?.({ type: "table", command: "getSync", tableName, data: { param1, param2, onChange, onError } });
|
|
111
|
+
checkSubscriptionArgs(param1, param2, onChange, onError);
|
|
112
|
+
|
|
113
|
+
const onChangeOne = (rows) => { onChange(rows[0]) };
|
|
114
|
+
return subscriptionHandler.addSub(dbo, { tableName, command, param1, param2 }, onChangeOne, onError);
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
} else {
|
|
118
|
+
const method = async function (param1, param2, param3) {
|
|
119
|
+
await onDebug?.({ type: "table", command: command as any, tableName, data: { param1, param2, param3 } });
|
|
120
|
+
return new Promise((resolve, reject) => {
|
|
121
|
+
socket.emit(preffix,
|
|
122
|
+
{ tableName, command, param1, param2, param3 },
|
|
123
|
+
|
|
124
|
+
/* Get col definition and re-cast data types?! */
|
|
125
|
+
(err, res) => {
|
|
126
|
+
if (err) reject(err);
|
|
127
|
+
else resolve(res);
|
|
128
|
+
}
|
|
129
|
+
);
|
|
130
|
+
})
|
|
131
|
+
}
|
|
132
|
+
dboTable[command] = method;
|
|
133
|
+
|
|
134
|
+
const methodName = command === "findOne" ? "useFindOne" : command === "find" ? "useFind" : command === "count" ? "useCount" : command === "size" ? "useSize" : undefined;
|
|
135
|
+
if(methodName){
|
|
136
|
+
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
137
|
+
dboTable[methodName] = (param1, param2, param3?) => useFetch(method, [param1, param2, param3]);
|
|
138
|
+
}
|
|
139
|
+
if (["find", "findOne"].includes(command)) {
|
|
140
|
+
dboTable.getJoinedTables = function () {
|
|
141
|
+
return joinTables
|
|
142
|
+
.filter(tb => Array.isArray(tb) && tb.includes(tableName))
|
|
143
|
+
.flat()
|
|
144
|
+
.filter(t => t !== tableName);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
})
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
return { dbo };
|
|
152
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { ClientSchema, MethodHandler } from "prostgles-types";
|
|
2
|
+
import { CHANNELS } from "prostgles-types";
|
|
3
|
+
import type { InitOptions } from "./prostgles";
|
|
4
|
+
|
|
5
|
+
export const getMethods = ({ onDebug, methods, socket }: Pick<InitOptions, "onDebug" | "socket"> & Pick<ClientSchema, "methods">) => {
|
|
6
|
+
|
|
7
|
+
let methodsObj: MethodHandler = {};
|
|
8
|
+
const _methods: typeof methods = JSON.parse(JSON.stringify(methods));
|
|
9
|
+
_methods.map(method => {
|
|
10
|
+
/** New method def */
|
|
11
|
+
const isBasic = typeof method === "string";
|
|
12
|
+
const methodName = isBasic ? method : method.name;
|
|
13
|
+
const onRun = async function (...params) {
|
|
14
|
+
await onDebug?.({ type: "method", command: methodName, data: { params } });
|
|
15
|
+
return new Promise((resolve, reject) => {
|
|
16
|
+
socket.emit(CHANNELS.METHOD, { method: methodName, params }, (err, res) => {
|
|
17
|
+
if (err) reject(err);
|
|
18
|
+
else resolve(res);
|
|
19
|
+
});
|
|
20
|
+
})
|
|
21
|
+
}
|
|
22
|
+
methodsObj[methodName] = isBasic ? onRun : {
|
|
23
|
+
...method,
|
|
24
|
+
run: onRun
|
|
25
|
+
};
|
|
26
|
+
});
|
|
27
|
+
methodsObj = Object.freeze(methodsObj);
|
|
28
|
+
|
|
29
|
+
return { methodsObj };
|
|
30
|
+
}
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import { CHANNELS, type SQLHandler, type DBEventHandles, type DBNoticeConfig, type DBNotifConfig, type SocketSQLStreamClient, type SocketSQLStreamServer } from "prostgles-types";
|
|
2
|
+
import type { InitOptions } from "./prostgles";
|
|
3
|
+
|
|
4
|
+
export const getSqlHandler = ({ socket }: Pick<InitOptions, "socket">) => {
|
|
5
|
+
|
|
6
|
+
let noticeSubs: {
|
|
7
|
+
listeners: ((notice: any) => void)[];
|
|
8
|
+
config: DBNoticeConfig;
|
|
9
|
+
} | undefined;
|
|
10
|
+
const notifSubs: {
|
|
11
|
+
[key: string]: {
|
|
12
|
+
config: DBNotifConfig
|
|
13
|
+
listeners: ((notif: any) => void)[]
|
|
14
|
+
}
|
|
15
|
+
} = {};
|
|
16
|
+
const removeNotifListener = (listener: any, conf: DBNotifConfig, socket: any) => {
|
|
17
|
+
const channelSubs = notifSubs[conf.notifChannel]
|
|
18
|
+
if (channelSubs) {
|
|
19
|
+
channelSubs.listeners = channelSubs.listeners.filter(nl => nl !== listener);
|
|
20
|
+
if (!channelSubs.listeners.length && channelSubs.config.socketUnsubChannel && socket) {
|
|
21
|
+
socket.emit(channelSubs.config.socketUnsubChannel, {});
|
|
22
|
+
delete notifSubs[conf.notifChannel];
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
const addNotifListener = (listener: any, conf: DBNotifConfig, socket: any) => {
|
|
27
|
+
const channelSubs = notifSubs[conf.notifChannel]
|
|
28
|
+
if (!channelSubs) {
|
|
29
|
+
notifSubs[conf.notifChannel] = {
|
|
30
|
+
config: conf,
|
|
31
|
+
listeners: [listener]
|
|
32
|
+
};
|
|
33
|
+
socket.removeAllListeners(conf.socketChannel);
|
|
34
|
+
socket.on(conf.socketChannel, (notif: any) => {
|
|
35
|
+
if (notifSubs[conf.notifChannel]?.listeners.length) {
|
|
36
|
+
notifSubs[conf.notifChannel]!.listeners.map(l => {
|
|
37
|
+
l(notif);
|
|
38
|
+
})
|
|
39
|
+
} else {
|
|
40
|
+
socket.emit(notifSubs[conf.notifChannel]?.config.socketUnsubChannel, {});
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
} else {
|
|
45
|
+
notifSubs[conf.notifChannel]?.listeners.push(listener);
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
const removeNoticeListener = (listener: any, socket: any) => {
|
|
51
|
+
if (noticeSubs) {
|
|
52
|
+
noticeSubs.listeners = noticeSubs.listeners.filter(nl => nl !== listener);
|
|
53
|
+
if (!noticeSubs.listeners.length && noticeSubs.config.socketUnsubChannel && socket) {
|
|
54
|
+
socket.emit(noticeSubs.config.socketUnsubChannel, {});
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
const addNoticeListener = (listener: any, conf: DBNoticeConfig, socket: any) => {
|
|
59
|
+
noticeSubs ??= {
|
|
60
|
+
config: conf,
|
|
61
|
+
listeners: []
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
if (!noticeSubs.listeners.length) {
|
|
65
|
+
socket.removeAllListeners(conf.socketChannel);
|
|
66
|
+
socket.on(conf.socketChannel, (notice: any) => {
|
|
67
|
+
if (noticeSubs && noticeSubs.listeners.length) {
|
|
68
|
+
noticeSubs.listeners.map(l => {
|
|
69
|
+
l(notice);
|
|
70
|
+
})
|
|
71
|
+
} else {
|
|
72
|
+
socket.emit(conf.socketUnsubChannel, {});
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
noticeSubs.listeners.push(listener);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const sql: SQLHandler = function (query, params, options) {
|
|
80
|
+
return new Promise((resolve, reject) => {
|
|
81
|
+
socket.emit(CHANNELS.SQL, { query, params, options }, (err, res) => {
|
|
82
|
+
if (err) {
|
|
83
|
+
return reject(err);
|
|
84
|
+
}
|
|
85
|
+
if(options?.returnType === "stream"){
|
|
86
|
+
const { channel, unsubChannel } = res as SocketSQLStreamServer;
|
|
87
|
+
const start: SocketSQLStreamClient["start"] = (listener) => new Promise<Awaited<ReturnType<SocketSQLStreamClient["start"]>>>((resolveStart, rejectStart) => {
|
|
88
|
+
socket.on(channel, listener)
|
|
89
|
+
socket.emit(channel, {}, (pid: number, err) => {
|
|
90
|
+
if(err){
|
|
91
|
+
rejectStart(err);
|
|
92
|
+
socket.removeAllListeners(channel);
|
|
93
|
+
} else {
|
|
94
|
+
resolveStart({
|
|
95
|
+
pid,
|
|
96
|
+
run: (query, params) => {
|
|
97
|
+
return new Promise((resolveRun, rejectRun) => {
|
|
98
|
+
socket.emit(channel, { query, params }, (data, _err) => {
|
|
99
|
+
if(_err){
|
|
100
|
+
rejectRun(_err);
|
|
101
|
+
} else {
|
|
102
|
+
resolveRun(data);
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
},
|
|
107
|
+
stop: (terminate?: boolean) => {
|
|
108
|
+
return new Promise((resolveStop, rejectStop) => {
|
|
109
|
+
socket.emit(unsubChannel, { terminate }, (data, _err) => {
|
|
110
|
+
if(_err){
|
|
111
|
+
rejectStop(_err);
|
|
112
|
+
} else {
|
|
113
|
+
resolveStop(data);
|
|
114
|
+
}
|
|
115
|
+
});
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
const streamHandlers = {
|
|
123
|
+
channel,
|
|
124
|
+
unsubChannel,
|
|
125
|
+
start,
|
|
126
|
+
} satisfies SocketSQLStreamClient;
|
|
127
|
+
|
|
128
|
+
return resolve(streamHandlers as any);
|
|
129
|
+
} else if (options &&
|
|
130
|
+
(options.returnType === "noticeSubscription") &&
|
|
131
|
+
res &&
|
|
132
|
+
Object.keys(res).sort().join() === ["socketChannel", "socketUnsubChannel"].sort().join() &&
|
|
133
|
+
!Object.values(res).find(v => typeof v !== "string")
|
|
134
|
+
) {
|
|
135
|
+
const sockInfo: DBNoticeConfig = res;
|
|
136
|
+
const addListener = (listener: (arg: any) => void) => {
|
|
137
|
+
addNoticeListener(listener, sockInfo, socket);
|
|
138
|
+
return {
|
|
139
|
+
...sockInfo,
|
|
140
|
+
removeListener: () => removeNoticeListener(listener, socket)
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
const handle: DBEventHandles = {
|
|
144
|
+
...sockInfo,
|
|
145
|
+
addListener
|
|
146
|
+
};
|
|
147
|
+
// @ts-ignore
|
|
148
|
+
resolve(handle);
|
|
149
|
+
} else if (
|
|
150
|
+
(!options || !options.returnType || options.returnType !== "statement") &&
|
|
151
|
+
res &&
|
|
152
|
+
Object.keys(res).sort().join() === ["socketChannel", "socketUnsubChannel", "notifChannel"].sort().join() &&
|
|
153
|
+
!Object.values(res).find(v => typeof v !== "string")
|
|
154
|
+
) {
|
|
155
|
+
const sockInfo: DBNotifConfig = res;
|
|
156
|
+
const addListener = (listener: (arg: any) => void) => {
|
|
157
|
+
addNotifListener(listener, sockInfo, socket)
|
|
158
|
+
return {
|
|
159
|
+
...res,
|
|
160
|
+
removeListener: () => removeNotifListener(listener, sockInfo, socket)
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
const handle: DBEventHandles = { ...res, addListener };
|
|
164
|
+
resolve(handle as any);
|
|
165
|
+
|
|
166
|
+
} else {
|
|
167
|
+
resolve(res);
|
|
168
|
+
}
|
|
169
|
+
});
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
return { sql };
|
|
174
|
+
}
|