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/lib/SQL.ts DELETED
@@ -1,178 +0,0 @@
1
- import { CHANNELS, type DBEventHandles, type DBNoticeConfig, type DBNotifConfig, type SocketSQLStreamClient, type SocketSQLStreamServer } from "prostgles-types";
2
- import type { DBHandlerClient } from "./prostgles";
3
-
4
- type Args = {
5
- socket: any;
6
- dbo: Partial<DBHandlerClient<void>>;
7
- }
8
- export class SQL {
9
-
10
- notifSubs: {
11
- [key: string]: {
12
- config: DBNotifConfig
13
- listeners: ((notif: any) => void)[]
14
- }
15
- } = {};
16
- removeNotifListener = (listener: any, conf: DBNotifConfig, socket: any) => {
17
- const channelSubs = this.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 this.notifSubs[conf.notifChannel];
23
- }
24
- }
25
- };
26
- addNotifListener = (listener: any, conf: DBNotifConfig, socket: any) => {
27
- const channelSubs = this.notifSubs[conf.notifChannel]
28
- if (!channelSubs) {
29
- this.notifSubs[conf.notifChannel] = {
30
- config: conf,
31
- listeners: [listener]
32
- };
33
- socket.removeAllListeners(conf.socketChannel);
34
- socket.on(conf.socketChannel, (notif: any) => {
35
- if (this.notifSubs[conf.notifChannel]?.listeners.length) {
36
- this.notifSubs[conf.notifChannel]!.listeners.map(l => {
37
- l(notif);
38
- })
39
- } else {
40
- socket.emit(this.notifSubs[conf.notifChannel]?.config.socketUnsubChannel, {});
41
- }
42
- });
43
-
44
- } else {
45
- this.notifSubs[conf.notifChannel]?.listeners.push(listener);
46
- }
47
- };
48
-
49
-
50
- noticeSubs: {
51
- listeners: ((notice: any) => void)[];
52
- config: DBNoticeConfig;
53
- } | undefined;
54
- removeNoticeListener = (listener: any, socket: any) => {
55
- if (this.noticeSubs) {
56
- this.noticeSubs.listeners = this.noticeSubs.listeners.filter(nl => nl !== listener);
57
- if (!this.noticeSubs.listeners.length && this.noticeSubs.config.socketUnsubChannel && socket) {
58
- socket.emit(this.noticeSubs.config.socketUnsubChannel, {});
59
- }
60
- }
61
- }
62
- addNoticeListener = (listener: any, conf: DBNoticeConfig, socket: any) => {
63
- this.noticeSubs ??= {
64
- config: conf,
65
- listeners: []
66
- };
67
-
68
- if (!this.noticeSubs.listeners.length) {
69
- socket.removeAllListeners(conf.socketChannel);
70
- socket.on(conf.socketChannel, (notice: any) => {
71
- if (this.noticeSubs && this.noticeSubs.listeners.length) {
72
- this.noticeSubs.listeners.map(l => {
73
- l(notice);
74
- })
75
- } else {
76
- socket.emit(conf.socketUnsubChannel, {});
77
- }
78
- });
79
- }
80
- this.noticeSubs.listeners.push(listener);
81
- }
82
- setup = async ({ socket, dbo }: Args) => {
83
- const { removeNotifListener, addNotifListener, removeNoticeListener, addNoticeListener } = this;
84
- dbo.sql = function (query, params, options) {
85
- return new Promise((resolve, reject) => {
86
- socket.emit(CHANNELS.SQL, { query, params, options }, (err, res) => {
87
- if (err) reject(err);
88
- else {
89
- if(options?.returnType === "stream"){
90
- const { channel, unsubChannel } = res as SocketSQLStreamServer;
91
- const start: SocketSQLStreamClient["start"] = (listener) => new Promise<Awaited<ReturnType<SocketSQLStreamClient["start"]>>>((resolveStart, rejectStart) => {
92
- socket.on(channel, listener)
93
- socket.emit(channel, {}, (pid: number, err) => {
94
- if(err){
95
- rejectStart(err);
96
- socket.removeAllListeners(channel);
97
- } else {
98
- resolveStart({
99
- pid,
100
- run: (query, params) => {
101
- return new Promise((resolveRun, rejectRun) => {
102
- socket.emit(channel, { query, params }, (data, _err) => {
103
- if(_err){
104
- rejectRun(_err);
105
- } else {
106
- resolveRun(data);
107
- }
108
- });
109
- });
110
- },
111
- stop: (terminate?: boolean) => {
112
- return new Promise((resolveStop, rejectStop) => {
113
- socket.emit(unsubChannel, { terminate }, (data, _err) => {
114
- if(_err){
115
- rejectStop(_err);
116
- } else {
117
- resolveStop(data);
118
- }
119
- });
120
- });
121
- }
122
- });
123
- }
124
- });
125
- });
126
- const streamHandlers = {
127
- channel,
128
- unsubChannel,
129
- start,
130
- } satisfies SocketSQLStreamClient;
131
-
132
- return resolve(streamHandlers as any);
133
- } else if (options &&
134
- (options.returnType === "noticeSubscription") &&
135
- res &&
136
- Object.keys(res).sort().join() === ["socketChannel", "socketUnsubChannel"].sort().join() &&
137
- !Object.values(res).find(v => typeof v !== "string")
138
- ) {
139
- const sockInfo: DBNoticeConfig = res;
140
- const addListener = (listener: (arg: any) => void) => {
141
- addNoticeListener(listener, sockInfo, socket);
142
- return {
143
- ...sockInfo,
144
- removeListener: () => removeNoticeListener(listener, socket)
145
- }
146
- };
147
- const handle: DBEventHandles = {
148
- ...sockInfo,
149
- addListener
150
- };
151
- // @ts-ignore
152
- resolve(handle);
153
- } else if (
154
- (!options || !options.returnType || options.returnType !== "statement") &&
155
- res &&
156
- Object.keys(res).sort().join() === ["socketChannel", "socketUnsubChannel", "notifChannel"].sort().join() &&
157
- !Object.values(res).find(v => typeof v !== "string")
158
- ) {
159
- const sockInfo: DBNotifConfig = res;
160
- const addListener = (listener: (arg: any) => void) => {
161
- addNotifListener(listener, sockInfo, socket)
162
- return {
163
- ...res,
164
- removeListener: () => removeNotifListener(listener, sockInfo, socket)
165
- }
166
- }
167
- const handle: DBEventHandles = { ...res, addListener };
168
- resolve(handle as any);
169
-
170
- } else {
171
- resolve(res);
172
- }
173
- }
174
- });
175
- });
176
- }
177
- }
178
- }