prostgles-client 4.0.171 → 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/dist/useProstglesClient.js +1 -1
- 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/lib/useProstglesClient.ts +1 -1
- 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/lib/prostgles.ts
CHANGED
|
@@ -26,18 +26,16 @@ import type {
|
|
|
26
26
|
import {
|
|
27
27
|
CHANNELS,
|
|
28
28
|
asName,
|
|
29
|
-
getJoinHandlers
|
|
30
|
-
getKeys,
|
|
31
|
-
isObject,
|
|
32
|
-
omitKeys,
|
|
29
|
+
getJoinHandlers
|
|
33
30
|
} from "prostgles-types";
|
|
34
31
|
|
|
35
32
|
import { type AuthHandler, setupAuth } from "./Auth";
|
|
36
|
-
import {
|
|
37
|
-
import {
|
|
38
|
-
import {
|
|
33
|
+
import { getDBO } from "./getDBO";
|
|
34
|
+
import { getMethods } from "./getMethods";
|
|
35
|
+
import { getSqlHandler } from "./getSqlHandler";
|
|
36
|
+
import { isEqual } from "./react-hooks";
|
|
39
37
|
import { getSubscriptionHandler } from "./subscriptionHandler";
|
|
40
|
-
import type {
|
|
38
|
+
import type { Sync, SyncDataItem, SyncOne, SyncOneOptions, SyncOptions, SyncedTable } from "./SyncedTable/SyncedTable";
|
|
41
39
|
import { getSyncHandler } from "./syncHandler";
|
|
42
40
|
|
|
43
41
|
const DEBUG_KEY = "DEBUG_SYNCEDTABLE";
|
|
@@ -48,9 +46,9 @@ export const debug: any = function (...args: any[]) {
|
|
|
48
46
|
}
|
|
49
47
|
};
|
|
50
48
|
|
|
51
|
-
export { MethodHandler, SQLResult, asName };
|
|
52
49
|
export * from "./react-hooks";
|
|
53
50
|
export * from "./useProstglesClient";
|
|
51
|
+
export { MethodHandler, SQLResult, asName };
|
|
54
52
|
|
|
55
53
|
|
|
56
54
|
export type ViewHandlerClient<T extends AnyObject = AnyObject, S extends DBSchema | void = void> = ViewHandler<T, S> & {
|
|
@@ -124,7 +122,7 @@ export type DBHandlerClient<Schema = void> = (Schema extends DBSchema ? {
|
|
|
124
122
|
} & DbJoinMaker;
|
|
125
123
|
|
|
126
124
|
type OnReadyArgs = {
|
|
127
|
-
dbo: DBHandlerClient
|
|
125
|
+
dbo: DBHandlerClient | any;
|
|
128
126
|
methods: MethodHandler | undefined;
|
|
129
127
|
tableSchema: DBSchemaTable[] | undefined;
|
|
130
128
|
auth: AuthHandler;
|
|
@@ -163,6 +161,7 @@ type DebugEvent =
|
|
|
163
161
|
| {
|
|
164
162
|
type: "schemaChanged";
|
|
165
163
|
data: ClientSchema;
|
|
164
|
+
state: "connected" | "disconnected" | "reconnected" | undefined;
|
|
166
165
|
}
|
|
167
166
|
| {
|
|
168
167
|
type: "onReady";
|
|
@@ -172,6 +171,11 @@ type DebugEvent =
|
|
|
172
171
|
type: "onReady.notMounted";
|
|
173
172
|
data: OnReadyArgs;
|
|
174
173
|
}
|
|
174
|
+
| {
|
|
175
|
+
type: "onReady.call";
|
|
176
|
+
data: OnReadyArgs;
|
|
177
|
+
state: "connected" | "disconnected" | "reconnected" | undefined;
|
|
178
|
+
}
|
|
175
179
|
;
|
|
176
180
|
|
|
177
181
|
export type InitOptions<DBSchema = void> = {
|
|
@@ -219,6 +223,7 @@ type CurrentClientSchema = {
|
|
|
219
223
|
date: Date;
|
|
220
224
|
clientSchema: Omit<ClientSchema, "joinTables">;
|
|
221
225
|
};
|
|
226
|
+
|
|
222
227
|
export function prostgles<DBSchema>(initOpts: InitOptions<DBSchema>, syncedTable: typeof SyncedTable | undefined) {
|
|
223
228
|
const { socket, onReady, onDisconnect, onReconnect, onSchemaChange = true, onReload, onDebug } = initOpts;
|
|
224
229
|
let schemaAge: CurrentClientSchema | undefined;
|
|
@@ -232,13 +237,11 @@ export function prostgles<DBSchema>(initOpts: InitOptions<DBSchema>, syncedTable
|
|
|
232
237
|
if (cb) socket.on(CHANNELS.SCHEMA_CHANGED, cb)
|
|
233
238
|
}
|
|
234
239
|
|
|
235
|
-
const preffix = CHANNELS._preffix;
|
|
236
|
-
|
|
237
240
|
const subscriptionHandler = getSubscriptionHandler(initOpts);
|
|
238
241
|
const syncHandler = getSyncHandler(initOpts);
|
|
239
242
|
|
|
240
243
|
let state: undefined | "connected" | "disconnected" | "reconnected";
|
|
241
|
-
const
|
|
244
|
+
const sqlHandler = getSqlHandler({ socket });
|
|
242
245
|
|
|
243
246
|
|
|
244
247
|
return new Promise((resolve, reject) => {
|
|
@@ -266,20 +269,22 @@ export function prostgles<DBSchema>(initOpts: InitOptions<DBSchema>, syncedTable
|
|
|
266
269
|
|
|
267
270
|
/* Schema = published schema */
|
|
268
271
|
socket.on(CHANNELS.SCHEMA, async (args: ClientSchema) => {
|
|
269
|
-
await onDebug?.({ type: "schemaChanged", data: args });
|
|
272
|
+
await onDebug?.({ type: "schemaChanged", data: args, state });
|
|
270
273
|
const { joinTables = [], ...clientSchema } = args;
|
|
271
274
|
const { schema, methods, tableSchema, auth: authConfig, rawSQL, err } = clientSchema;
|
|
272
275
|
|
|
273
276
|
/** Only destroy existing syncs if schema changed */
|
|
274
277
|
const schemaDidNotChange = schemaAge?.clientSchema && isEqual(schemaAge.clientSchema, clientSchema)
|
|
275
278
|
if(!schemaDidNotChange){
|
|
276
|
-
await syncHandler.destroySyncs();
|
|
279
|
+
await syncHandler.destroySyncs().catch(error => console.error("Error while destroying syncs", error));
|
|
277
280
|
}
|
|
278
281
|
|
|
282
|
+
if (err) {
|
|
283
|
+
console.error("Error on schema change:", err)
|
|
284
|
+
}
|
|
279
285
|
if ((state === "connected" || state === "reconnected") && onReconnect) {
|
|
280
286
|
onReconnect(socket, err);
|
|
281
287
|
if (err) {
|
|
282
|
-
console.error(err)
|
|
283
288
|
return;
|
|
284
289
|
}
|
|
285
290
|
schemaAge = { origin: "onReconnect", date: new Date(), clientSchema };
|
|
@@ -295,163 +300,22 @@ export function prostgles<DBSchema>(initOpts: InitOptions<DBSchema>, syncedTable
|
|
|
295
300
|
const isReconnect = state === "reconnected";
|
|
296
301
|
state = "connected";
|
|
297
302
|
|
|
298
|
-
const dbo: Partial<DBHandlerClient> = JSON.parse(JSON.stringify(schema));
|
|
299
|
-
const _methods: typeof methods = JSON.parse(JSON.stringify(methods));
|
|
300
|
-
let methodsObj: MethodHandler = {};
|
|
301
|
-
|
|
302
303
|
const auth = setupAuth({ authData: authConfig, socket, onReload });
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
else resolve(res);
|
|
314
|
-
});
|
|
315
|
-
})
|
|
316
|
-
}
|
|
317
|
-
methodsObj[methodName] = isBasic ? onRun : {
|
|
318
|
-
...method,
|
|
319
|
-
run: onRun
|
|
320
|
-
};
|
|
304
|
+
const { methodsObj } = getMethods({ onDebug, methods, socket });
|
|
305
|
+
|
|
306
|
+
const { dbo } = getDBO({
|
|
307
|
+
schema,
|
|
308
|
+
onDebug,
|
|
309
|
+
syncedTable,
|
|
310
|
+
syncHandler,
|
|
311
|
+
subscriptionHandler,
|
|
312
|
+
socket,
|
|
313
|
+
joinTables,
|
|
321
314
|
});
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
if (rawSQL) {
|
|
325
|
-
sql.setup({ dbo, socket });
|
|
315
|
+
if(rawSQL){
|
|
316
|
+
dbo.sql = sqlHandler.sql;
|
|
326
317
|
}
|
|
327
318
|
|
|
328
|
-
/* Building DBO object */
|
|
329
|
-
const checkSubscriptionArgs = (basicFilter: AnyObject | undefined, options: AnyObject | undefined, onChange: AnyFunction, onError?: AnyFunction) => {
|
|
330
|
-
if (basicFilter !== undefined && !isObject(basicFilter) || options !== undefined && !isObject(options) || !(typeof onChange === "function") || onError !== undefined && typeof onError !== "function") {
|
|
331
|
-
throw "Expecting: ( basicFilter<object>, options<object>, onChange<function> , onError?<function>) but got something else";
|
|
332
|
-
}
|
|
333
|
-
}
|
|
334
|
-
const sub_commands = ["subscribe", "subscribeOne"] as const;
|
|
335
|
-
getKeys(dbo).forEach(tableName => {
|
|
336
|
-
const all_commands = Object.keys(dbo[tableName]!);
|
|
337
|
-
|
|
338
|
-
const dboTable = dbo[tableName] as TableHandlerClient;
|
|
339
|
-
all_commands
|
|
340
|
-
.sort((a, b) => <never>sub_commands.includes(a as any) - <never>sub_commands.includes(b as any))
|
|
341
|
-
.forEach(command => {
|
|
342
|
-
|
|
343
|
-
if (command === "sync") {
|
|
344
|
-
dboTable._syncInfo = { ...dboTable[command] };
|
|
345
|
-
if (syncedTable) {
|
|
346
|
-
dboTable.getSync = async (filter, params = {}) => {
|
|
347
|
-
await onDebug?.({ type: "table", command: "getSync", tableName, data: { filter, params } });
|
|
348
|
-
return syncedTable.create({
|
|
349
|
-
name: tableName,
|
|
350
|
-
onDebug: onDebug as any,
|
|
351
|
-
filter,
|
|
352
|
-
db: dbo,
|
|
353
|
-
...params
|
|
354
|
-
});
|
|
355
|
-
}
|
|
356
|
-
const upsertSyncTable = async (basicFilter = {}, options: SyncOptions = {}, onError) => {
|
|
357
|
-
const syncName = `${tableName}.${JSON.stringify(basicFilter)}.${JSON.stringify(omitKeys(options, ["handlesOnData"]))}`
|
|
358
|
-
if (!syncHandler.syncedTables[syncName]) {
|
|
359
|
-
syncHandler.syncedTables[syncName] = await syncedTable.create({
|
|
360
|
-
...options,
|
|
361
|
-
onDebug: onDebug as any,
|
|
362
|
-
name: tableName,
|
|
363
|
-
filter: basicFilter,
|
|
364
|
-
db: dbo,
|
|
365
|
-
onError
|
|
366
|
-
});
|
|
367
|
-
}
|
|
368
|
-
return syncHandler.syncedTables[syncName]
|
|
369
|
-
}
|
|
370
|
-
const sync: Sync<AnyObject> = async (basicFilter, options = { handlesOnData: true, select: "*" }, onChange, onError) => {
|
|
371
|
-
await onDebug?.({ type: "table", command: "sync", tableName, data: { basicFilter, options } });
|
|
372
|
-
checkSubscriptionArgs(basicFilter, options, onChange, onError);
|
|
373
|
-
const s = await upsertSyncTable(basicFilter, options, onError);
|
|
374
|
-
return await s.sync(onChange, options.handlesOnData);
|
|
375
|
-
}
|
|
376
|
-
const syncOne: SyncOne<AnyObject> = async (basicFilter, options = { handlesOnData: true }, onChange, onError) => {
|
|
377
|
-
await onDebug?.({ type: "table", command: "syncOne", tableName, data: { basicFilter, options } });
|
|
378
|
-
checkSubscriptionArgs(basicFilter, options, onChange, onError);
|
|
379
|
-
const s = await upsertSyncTable(basicFilter, options, onError);
|
|
380
|
-
return await s.syncOne(basicFilter, onChange, options.handlesOnData);
|
|
381
|
-
}
|
|
382
|
-
dboTable.sync = sync;
|
|
383
|
-
dboTable.syncOne = syncOne;
|
|
384
|
-
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
385
|
-
dboTable.useSync = (basicFilter, options) => useSync(sync, basicFilter, options) as any;
|
|
386
|
-
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
387
|
-
dboTable.useSyncOne = (basicFilter, options) => useSync(syncOne, basicFilter, options) as any;
|
|
388
|
-
}
|
|
389
|
-
|
|
390
|
-
dboTable._sync = async function (param1, param2, syncHandles) {
|
|
391
|
-
await onDebug?.({ type: "table", command: "_sync", tableName, data: { param1, param2, syncHandles } });
|
|
392
|
-
return syncHandler.addSync({ tableName, command, param1, param2 }, syncHandles);
|
|
393
|
-
}
|
|
394
|
-
} else if (sub_commands.includes(command as any)) {
|
|
395
|
-
const subFunc = async function (param1 = {}, param2 = {}, onChange, onError) {
|
|
396
|
-
await onDebug?.({ type: "table", command: command as typeof sub_commands[number], tableName, data: { param1, param2, onChange, onError } });
|
|
397
|
-
checkSubscriptionArgs(param1, param2, onChange, onError);
|
|
398
|
-
return subscriptionHandler.addSub(dbo, { tableName, command, param1, param2 }, onChange, onError);
|
|
399
|
-
};
|
|
400
|
-
dboTable[command] = subFunc;
|
|
401
|
-
const SUBONE = "subscribeOne";
|
|
402
|
-
|
|
403
|
-
/**
|
|
404
|
-
* React hooks
|
|
405
|
-
*/
|
|
406
|
-
const handlerName = command === "subscribe" ? "useSubscribe" : command === "subscribeOne"? "useSubscribeOne" : undefined;
|
|
407
|
-
if(handlerName){
|
|
408
|
-
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
409
|
-
dboTable[handlerName] = (filter, options) => useSubscribe(subFunc, command === SUBONE, filter, options)
|
|
410
|
-
}
|
|
411
|
-
|
|
412
|
-
if (command === SUBONE || !sub_commands.includes(SUBONE)) {
|
|
413
|
-
dboTable[SUBONE] = async function (param1, param2, onChange, onError) {
|
|
414
|
-
await onDebug?.({ type: "table", command: "getSync", tableName, data: { param1, param2, onChange, onError } });
|
|
415
|
-
checkSubscriptionArgs(param1, param2, onChange, onError);
|
|
416
|
-
|
|
417
|
-
const onChangeOne = (rows) => { onChange(rows[0]) };
|
|
418
|
-
return subscriptionHandler.addSub(dbo, { tableName, command, param1, param2 }, onChangeOne, onError);
|
|
419
|
-
};
|
|
420
|
-
}
|
|
421
|
-
} else {
|
|
422
|
-
const method = async function (param1, param2, param3) {
|
|
423
|
-
await onDebug?.({ type: "table", command: command as any, tableName, data: { param1, param2, param3 } });
|
|
424
|
-
return new Promise((resolve, reject) => {
|
|
425
|
-
socket.emit(preffix,
|
|
426
|
-
{ tableName, command, param1, param2, param3 },
|
|
427
|
-
|
|
428
|
-
/* Get col definition and re-cast data types?! */
|
|
429
|
-
(err, res) => {
|
|
430
|
-
if (err) reject(err);
|
|
431
|
-
else resolve(res);
|
|
432
|
-
}
|
|
433
|
-
);
|
|
434
|
-
})
|
|
435
|
-
}
|
|
436
|
-
dboTable[command] = method;
|
|
437
|
-
|
|
438
|
-
const methodName = command === "findOne" ? "useFindOne" : command === "find" ? "useFind" : command === "count" ? "useCount" : command === "size" ? "useSize" : undefined;
|
|
439
|
-
if(methodName){
|
|
440
|
-
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
441
|
-
dboTable[methodName] = (param1, param2, param3?) => useFetch(method, [param1, param2, param3]);
|
|
442
|
-
}
|
|
443
|
-
if (["find", "findOne"].includes(command)) {
|
|
444
|
-
dboTable.getJoinedTables = function () {
|
|
445
|
-
return joinTables
|
|
446
|
-
.filter(tb => Array.isArray(tb) && tb.includes(tableName))
|
|
447
|
-
.flat()
|
|
448
|
-
.filter(t => t !== tableName);
|
|
449
|
-
}
|
|
450
|
-
}
|
|
451
|
-
}
|
|
452
|
-
})
|
|
453
|
-
});
|
|
454
|
-
|
|
455
319
|
await subscriptionHandler.reAttachAll();
|
|
456
320
|
await syncHandler.reAttachAll();
|
|
457
321
|
|
|
@@ -470,6 +334,8 @@ export function prostgles<DBSchema>(initOpts: InitOptions<DBSchema>, syncedTable
|
|
|
470
334
|
|
|
471
335
|
(async () => {
|
|
472
336
|
try {
|
|
337
|
+
const onReadyArgs = { dbo, methods: methodsObj, tableSchema, auth, isReconnect };
|
|
338
|
+
await onDebug?.({ type: "onReady.call", data: onReadyArgs, state });
|
|
473
339
|
await onReady(dbo as DBHandlerClient<DBSchema>, methodsObj, tableSchema, auth, isReconnect);
|
|
474
340
|
} catch (err) {
|
|
475
341
|
console.error("Prostgles: Error within onReady: \n", err);
|
|
@@ -72,7 +72,7 @@ export const useProstglesClient = <DBSchema>({ skip, socketOptions, ...initOpts
|
|
|
72
72
|
initOpts.onDebug?.({ type: "onReady.notMounted", data: onReadyArgs as any });
|
|
73
73
|
return;
|
|
74
74
|
}
|
|
75
|
-
initOpts.onDebug?.({ type: "onReady", data:
|
|
75
|
+
initOpts.onDebug?.({ type: "onReady", data: onReadyArgs as any });
|
|
76
76
|
setOnReadyArgs({ ...onReadyArgs, isLoading: false });
|
|
77
77
|
}
|
|
78
78
|
}, SyncedTable)
|
package/package.json
CHANGED
package/tests/package-lock.json
CHANGED
package/dist/SQL.d.ts
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import { type DBNoticeConfig, type DBNotifConfig } from "prostgles-types";
|
|
2
|
-
import type { DBHandlerClient } from "./prostgles";
|
|
3
|
-
type Args = {
|
|
4
|
-
socket: any;
|
|
5
|
-
dbo: Partial<DBHandlerClient<void>>;
|
|
6
|
-
};
|
|
7
|
-
export declare class SQL {
|
|
8
|
-
notifSubs: {
|
|
9
|
-
[key: string]: {
|
|
10
|
-
config: DBNotifConfig;
|
|
11
|
-
listeners: ((notif: any) => void)[];
|
|
12
|
-
};
|
|
13
|
-
};
|
|
14
|
-
removeNotifListener: (listener: any, conf: DBNotifConfig, socket: any) => void;
|
|
15
|
-
addNotifListener: (listener: any, conf: DBNotifConfig, socket: any) => void;
|
|
16
|
-
noticeSubs: {
|
|
17
|
-
listeners: ((notice: any) => void)[];
|
|
18
|
-
config: DBNoticeConfig;
|
|
19
|
-
} | undefined;
|
|
20
|
-
removeNoticeListener: (listener: any, socket: any) => void;
|
|
21
|
-
addNoticeListener: (listener: any, conf: DBNoticeConfig, socket: any) => void;
|
|
22
|
-
setup: ({ socket, dbo }: Args) => Promise<void>;
|
|
23
|
-
}
|
|
24
|
-
export {};
|
|
25
|
-
//# sourceMappingURL=SQL.d.ts.map
|
package/dist/SQL.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"SQL.d.ts","sourceRoot":"","sources":["../lib/SQL.ts"],"names":[],"mappings":"AAAA,OAAO,EAAiC,KAAK,cAAc,EAAE,KAAK,aAAa,EAA0D,MAAM,iBAAiB,CAAC;AACjK,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAEnD,KAAK,IAAI,GAAG;IACV,MAAM,EAAE,GAAG,CAAC;IACZ,GAAG,EAAE,OAAO,CAAC,eAAe,CAAC,IAAI,CAAC,CAAC,CAAC;CACrC,CAAA;AACD,qBAAa,GAAG;IAEd,SAAS,EAAE;QACT,CAAC,GAAG,EAAE,MAAM,GAAG;YACb,MAAM,EAAE,aAAa,CAAA;YACrB,SAAS,EAAE,CAAC,CAAC,KAAK,EAAE,GAAG,KAAK,IAAI,CAAC,EAAE,CAAA;SACpC,CAAA;KACF,CAAM;IACP,mBAAmB,aAAc,GAAG,QAAQ,aAAa,UAAU,GAAG,UASpE;IACF,gBAAgB,aAAc,GAAG,QAAQ,aAAa,UAAU,GAAG,UAqBjE;IAGF,UAAU,EAAE;QACV,SAAS,EAAE,CAAC,CAAC,MAAM,EAAE,GAAG,KAAK,IAAI,CAAC,EAAE,CAAC;QACrC,MAAM,EAAE,cAAc,CAAC;KACxB,GAAG,SAAS,CAAC;IACd,oBAAoB,aAAc,GAAG,UAAU,GAAG,UAOjD;IACD,iBAAiB,aAAc,GAAG,QAAQ,cAAc,UAAU,GAAG,UAmBpE;IACD,KAAK,oBAA2B,IAAI,mBA+FnC;CACF"}
|
package/dist/SQL.js
DELETED
|
@@ -1,172 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.SQL = void 0;
|
|
4
|
-
const prostgles_types_1 = require("prostgles-types");
|
|
5
|
-
class SQL {
|
|
6
|
-
constructor() {
|
|
7
|
-
this.notifSubs = {};
|
|
8
|
-
this.removeNotifListener = (listener, conf, socket) => {
|
|
9
|
-
const channelSubs = this.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 this.notifSubs[conf.notifChannel];
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
};
|
|
18
|
-
this.addNotifListener = (listener, conf, socket) => {
|
|
19
|
-
var _a;
|
|
20
|
-
const channelSubs = this.notifSubs[conf.notifChannel];
|
|
21
|
-
if (!channelSubs) {
|
|
22
|
-
this.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 = this.notifSubs[conf.notifChannel]) === null || _a === void 0 ? void 0 : _a.listeners.length) {
|
|
30
|
-
this.notifSubs[conf.notifChannel].listeners.map(l => {
|
|
31
|
-
l(notif);
|
|
32
|
-
});
|
|
33
|
-
}
|
|
34
|
-
else {
|
|
35
|
-
socket.emit((_b = this.notifSubs[conf.notifChannel]) === null || _b === void 0 ? void 0 : _b.config.socketUnsubChannel, {});
|
|
36
|
-
}
|
|
37
|
-
});
|
|
38
|
-
}
|
|
39
|
-
else {
|
|
40
|
-
(_a = this.notifSubs[conf.notifChannel]) === null || _a === void 0 ? void 0 : _a.listeners.push(listener);
|
|
41
|
-
}
|
|
42
|
-
};
|
|
43
|
-
this.removeNoticeListener = (listener, socket) => {
|
|
44
|
-
if (this.noticeSubs) {
|
|
45
|
-
this.noticeSubs.listeners = this.noticeSubs.listeners.filter(nl => nl !== listener);
|
|
46
|
-
if (!this.noticeSubs.listeners.length && this.noticeSubs.config.socketUnsubChannel && socket) {
|
|
47
|
-
socket.emit(this.noticeSubs.config.socketUnsubChannel, {});
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
};
|
|
51
|
-
this.addNoticeListener = (listener, conf, socket) => {
|
|
52
|
-
var _a;
|
|
53
|
-
(_a = this.noticeSubs) !== null && _a !== void 0 ? _a : (this.noticeSubs = {
|
|
54
|
-
config: conf,
|
|
55
|
-
listeners: []
|
|
56
|
-
});
|
|
57
|
-
if (!this.noticeSubs.listeners.length) {
|
|
58
|
-
socket.removeAllListeners(conf.socketChannel);
|
|
59
|
-
socket.on(conf.socketChannel, (notice) => {
|
|
60
|
-
if (this.noticeSubs && this.noticeSubs.listeners.length) {
|
|
61
|
-
this.noticeSubs.listeners.map(l => {
|
|
62
|
-
l(notice);
|
|
63
|
-
});
|
|
64
|
-
}
|
|
65
|
-
else {
|
|
66
|
-
socket.emit(conf.socketUnsubChannel, {});
|
|
67
|
-
}
|
|
68
|
-
});
|
|
69
|
-
}
|
|
70
|
-
this.noticeSubs.listeners.push(listener);
|
|
71
|
-
};
|
|
72
|
-
this.setup = async ({ socket, dbo }) => {
|
|
73
|
-
const { removeNotifListener, addNotifListener, removeNoticeListener, addNoticeListener } = this;
|
|
74
|
-
dbo.sql = function (query, params, options) {
|
|
75
|
-
return new Promise((resolve, reject) => {
|
|
76
|
-
socket.emit(prostgles_types_1.CHANNELS.SQL, { query, params, options }, (err, res) => {
|
|
77
|
-
if (err)
|
|
78
|
-
reject(err);
|
|
79
|
-
else {
|
|
80
|
-
if ((options === null || options === void 0 ? void 0 : options.returnType) === "stream") {
|
|
81
|
-
const { channel, unsubChannel } = res;
|
|
82
|
-
const start = (listener) => new Promise((resolveStart, rejectStart) => {
|
|
83
|
-
socket.on(channel, listener);
|
|
84
|
-
socket.emit(channel, {}, (pid, err) => {
|
|
85
|
-
if (err) {
|
|
86
|
-
rejectStart(err);
|
|
87
|
-
socket.removeAllListeners(channel);
|
|
88
|
-
}
|
|
89
|
-
else {
|
|
90
|
-
resolveStart({
|
|
91
|
-
pid,
|
|
92
|
-
run: (query, params) => {
|
|
93
|
-
return new Promise((resolveRun, rejectRun) => {
|
|
94
|
-
socket.emit(channel, { query, params }, (data, _err) => {
|
|
95
|
-
if (_err) {
|
|
96
|
-
rejectRun(_err);
|
|
97
|
-
}
|
|
98
|
-
else {
|
|
99
|
-
resolveRun(data);
|
|
100
|
-
}
|
|
101
|
-
});
|
|
102
|
-
});
|
|
103
|
-
},
|
|
104
|
-
stop: (terminate) => {
|
|
105
|
-
return new Promise((resolveStop, rejectStop) => {
|
|
106
|
-
socket.emit(unsubChannel, { terminate }, (data, _err) => {
|
|
107
|
-
if (_err) {
|
|
108
|
-
rejectStop(_err);
|
|
109
|
-
}
|
|
110
|
-
else {
|
|
111
|
-
resolveStop(data);
|
|
112
|
-
}
|
|
113
|
-
});
|
|
114
|
-
});
|
|
115
|
-
}
|
|
116
|
-
});
|
|
117
|
-
}
|
|
118
|
-
});
|
|
119
|
-
});
|
|
120
|
-
const streamHandlers = {
|
|
121
|
-
channel,
|
|
122
|
-
unsubChannel,
|
|
123
|
-
start,
|
|
124
|
-
};
|
|
125
|
-
return resolve(streamHandlers);
|
|
126
|
-
}
|
|
127
|
-
else if (options &&
|
|
128
|
-
(options.returnType === "noticeSubscription") &&
|
|
129
|
-
res &&
|
|
130
|
-
Object.keys(res).sort().join() === ["socketChannel", "socketUnsubChannel"].sort().join() &&
|
|
131
|
-
!Object.values(res).find(v => typeof v !== "string")) {
|
|
132
|
-
const sockInfo = res;
|
|
133
|
-
const addListener = (listener) => {
|
|
134
|
-
addNoticeListener(listener, sockInfo, socket);
|
|
135
|
-
return {
|
|
136
|
-
...sockInfo,
|
|
137
|
-
removeListener: () => removeNoticeListener(listener, socket)
|
|
138
|
-
};
|
|
139
|
-
};
|
|
140
|
-
const handle = {
|
|
141
|
-
...sockInfo,
|
|
142
|
-
addListener
|
|
143
|
-
};
|
|
144
|
-
// @ts-ignore
|
|
145
|
-
resolve(handle);
|
|
146
|
-
}
|
|
147
|
-
else if ((!options || !options.returnType || options.returnType !== "statement") &&
|
|
148
|
-
res &&
|
|
149
|
-
Object.keys(res).sort().join() === ["socketChannel", "socketUnsubChannel", "notifChannel"].sort().join() &&
|
|
150
|
-
!Object.values(res).find(v => typeof v !== "string")) {
|
|
151
|
-
const sockInfo = res;
|
|
152
|
-
const addListener = (listener) => {
|
|
153
|
-
addNotifListener(listener, sockInfo, socket);
|
|
154
|
-
return {
|
|
155
|
-
...res,
|
|
156
|
-
removeListener: () => removeNotifListener(listener, sockInfo, socket)
|
|
157
|
-
};
|
|
158
|
-
};
|
|
159
|
-
const handle = { ...res, addListener };
|
|
160
|
-
resolve(handle);
|
|
161
|
-
}
|
|
162
|
-
else {
|
|
163
|
-
resolve(res);
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
});
|
|
167
|
-
});
|
|
168
|
-
};
|
|
169
|
-
};
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
exports.SQL = SQL;
|