prostgles-client 4.0.172 → 4.0.174
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/getDbHandler.d.ts +19 -0
- package/dist/getDbHandler.d.ts.map +1 -0
- package/dist/getDbHandler.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/{subscriptionHandler.d.ts → getSubscriptionHandler.d.ts} +1 -1
- package/dist/getSubscriptionHandler.d.ts.map +1 -0
- package/dist/{syncHandler.d.ts → getSyncHandler.d.ts} +1 -1
- package/dist/getSyncHandler.d.ts.map +1 -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 +28 -156
- package/lib/getDbHandler.ts +152 -0
- package/lib/getMethods.ts +30 -0
- package/lib/getSqlHandler.ts +174 -0
- package/lib/prostgles.ts +41 -174
- 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/dist/subscriptionHandler.d.ts.map +0 -1
- package/dist/syncHandler.d.ts.map +0 -1
- package/lib/SQL.ts +0 -178
- /package/dist/{subscriptionHandler.js → getSubscriptionHandler.js} +0 -0
- /package/dist/{syncHandler.js → getSyncHandler.js} +0 -0
- /package/lib/{subscriptionHandler.ts → getSubscriptionHandler.ts} +0 -0
- /package/lib/{syncHandler.ts → getSyncHandler.ts} +0 -0
package/lib/prostgles.ts
CHANGED
|
@@ -26,19 +26,17 @@ 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 {
|
|
39
|
-
import {
|
|
40
|
-
import
|
|
41
|
-
import {
|
|
33
|
+
import { getDBO } from "./getDbHandler";
|
|
34
|
+
import { getMethods } from "./getMethods";
|
|
35
|
+
import { getSqlHandler } from "./getSqlHandler";
|
|
36
|
+
import { isEqual } from "./react-hooks";
|
|
37
|
+
import { getSubscriptionHandler } from "./getSubscriptionHandler";
|
|
38
|
+
import type { Sync, SyncDataItem, SyncOne, SyncOneOptions, SyncOptions, SyncedTable } from "./SyncedTable/SyncedTable";
|
|
39
|
+
import { getSyncHandler } from "./getSyncHandler";
|
|
42
40
|
|
|
43
41
|
const DEBUG_KEY = "DEBUG_SYNCEDTABLE";
|
|
44
42
|
export const hasWnd = typeof window !== "undefined";
|
|
@@ -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,18 +237,16 @@ 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);
|
|
242
|
+
const sqlHandler = getSqlHandler(initOpts);
|
|
239
243
|
|
|
240
244
|
let state: undefined | "connected" | "disconnected" | "reconnected";
|
|
241
|
-
const sql = new SQL();
|
|
242
245
|
|
|
243
246
|
|
|
244
247
|
return new Promise((resolve, reject) => {
|
|
245
248
|
|
|
246
|
-
socket.removeAllListeners(CHANNELS.CONNECTION)
|
|
249
|
+
socket.removeAllListeners(CHANNELS.CONNECTION);
|
|
247
250
|
socket.on(CHANNELS.CONNECTION, error => {
|
|
248
251
|
reject(error);
|
|
249
252
|
return "ok"
|
|
@@ -266,20 +269,23 @@ 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
|
-
|
|
279
|
+
syncHandler.destroySyncs()
|
|
280
|
+
.catch(error => console.error("Error while destroying syncs", error));
|
|
277
281
|
}
|
|
278
282
|
|
|
283
|
+
if (err) {
|
|
284
|
+
console.error("Error on schema change:", err)
|
|
285
|
+
}
|
|
279
286
|
if ((state === "connected" || state === "reconnected") && onReconnect) {
|
|
280
287
|
onReconnect(socket, err);
|
|
281
288
|
if (err) {
|
|
282
|
-
console.error(err)
|
|
283
289
|
return;
|
|
284
290
|
}
|
|
285
291
|
schemaAge = { origin: "onReconnect", date: new Date(), clientSchema };
|
|
@@ -295,165 +301,24 @@ export function prostgles<DBSchema>(initOpts: InitOptions<DBSchema>, syncedTable
|
|
|
295
301
|
const isReconnect = state === "reconnected";
|
|
296
302
|
state = "connected";
|
|
297
303
|
|
|
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
304
|
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
|
-
};
|
|
305
|
+
const { methodsObj } = getMethods({ onDebug, methods, socket });
|
|
306
|
+
|
|
307
|
+
const { dbo } = getDBO({
|
|
308
|
+
schema,
|
|
309
|
+
onDebug,
|
|
310
|
+
syncedTable,
|
|
311
|
+
syncHandler,
|
|
312
|
+
subscriptionHandler,
|
|
313
|
+
socket,
|
|
314
|
+
joinTables,
|
|
321
315
|
});
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
if (rawSQL) {
|
|
325
|
-
sql.setup({ dbo, socket });
|
|
316
|
+
if(rawSQL){
|
|
317
|
+
dbo.sql = sqlHandler.sql;
|
|
326
318
|
}
|
|
327
319
|
|
|
328
|
-
|
|
329
|
-
|
|
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
|
-
await subscriptionHandler.reAttachAll();
|
|
456
|
-
await syncHandler.reAttachAll();
|
|
320
|
+
subscriptionHandler.reAttachAll();
|
|
321
|
+
syncHandler.reAttachAll();
|
|
457
322
|
|
|
458
323
|
joinTables.flat().map(table => {
|
|
459
324
|
dbo.innerJoin ??= {};
|
|
@@ -470,6 +335,8 @@ export function prostgles<DBSchema>(initOpts: InitOptions<DBSchema>, syncedTable
|
|
|
470
335
|
|
|
471
336
|
(async () => {
|
|
472
337
|
try {
|
|
338
|
+
const onReadyArgs = { dbo, methods: methodsObj, tableSchema, auth, isReconnect };
|
|
339
|
+
await onDebug?.({ type: "onReady.call", data: onReadyArgs, state });
|
|
473
340
|
await onReady(dbo as DBHandlerClient<DBSchema>, methodsObj, tableSchema, auth, isReconnect);
|
|
474
341
|
} catch (err) {
|
|
475
342
|
console.error("Prostgles: Error within onReady: \n", err);
|
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;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"subscriptionHandler.d.ts","sourceRoot":"","sources":["../lib/subscriptionHandler.ts"],"names":[],"mappings":"AAAA,OAAO,EAA+C,KAAK,oBAAoB,EAAE,KAAK,mBAAmB,EAAqB,MAAM,iBAAiB,CAAC;AACtJ,OAAO,EAAkB,KAAK,WAAW,EAAE,KAAK,UAAU,EAAE,KAAK,WAAW,EAAE,MAAM,aAAa,CAAC;AAGlG,KAAK,YAAY,GAAG,UAAU,GAAG;IAC/B,QAAQ,EAAE,GAAG,CAAC;IACd,MAAM,EAAE,WAAW,CAAC;IACpB,QAAQ,EAAE,WAAW,EAAE,CAAC;IACxB,aAAa,EAAE,CAAC,WAAW,GAAG,SAAS,CAAC,EAAE,CAAC;IAC3C,YAAY,EAAE,MAAM,CAAC;IACrB,QAAQ,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;CAC/B,CAAC;AAEF,KAAK,aAAa,GAAG;IACnB,CAAC,GAAG,EAAE,MAAM,GAAG,YAAY,CAAA;CAC5B,CAAC;AAIF,eAAO,MAAM,sBAAsB,aAAc,KAAK,WAAW,EAAE,QAAQ,GAAG,SAAS,CAAC;kBAiE3D,GAAG,UAAU,UAAU,YAAY,WAAW,aAAa,WAAW,KAAG,QAAQ,mBAAmB,CAAC;;2DAlBlE,UAAU,KAAG,QAAQ,oBAAoB,CAAC;gCA7BrE,MAAM,gBAAgB,MAAM,WAAW,WAAW,KAAG,QAAQ,IAAI,CAAC;;CA6KtG,CAAA"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"syncHandler.d.ts","sourceRoot":"","sources":["../lib/syncHandler.ts"],"names":[],"mappings":"AAAA,OAAO,EAAsB,KAAK,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AAC7E,OAAO,KAAK,EAAe,UAAU,EAAE,WAAW,EAAY,MAAM,aAAa,CAAC;AAgBlF,eAAO,MAAM,cAAc,wBAAyB,KAAK,WAAW,EAAE,QAAQ,GAAG,SAAS,CAAC;;;sBAsD1D,UAAU,YAAY,iBAAiB,KAAG,QAAQ,GAAG,CAAC;;CAiItF,CAAA"}
|