prostgles-client
Version:
Reactive client for Postgres
168 lines (167 loc) • 7.63 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getSqlHandler = void 0;
const prostgles_types_1 = require("prostgles-types");
const getSqlHandler = ({ socket }) => {
let noticeSubs;
const notifSubs = {};
const removeNotifListener = (listener, conf, socket) => {
const channelSubs = notifSubs[conf.notifChannel];
if (channelSubs) {
channelSubs.listeners = channelSubs.listeners.filter(nl => nl !== listener);
if (!channelSubs.listeners.length && channelSubs.config.socketUnsubChannel && socket) {
socket.emit(channelSubs.config.socketUnsubChannel, {});
delete notifSubs[conf.notifChannel];
}
}
};
const addNotifListener = (listener, conf, socket) => {
var _a;
const channelSubs = notifSubs[conf.notifChannel];
if (!channelSubs) {
notifSubs[conf.notifChannel] = {
config: conf,
listeners: [listener]
};
socket.removeAllListeners(conf.socketChannel);
socket.on(conf.socketChannel, (notif) => {
var _a, _b;
if ((_a = notifSubs[conf.notifChannel]) === null || _a === void 0 ? void 0 : _a.listeners.length) {
notifSubs[conf.notifChannel].listeners.map(l => {
l(notif);
});
}
else {
socket.emit((_b = notifSubs[conf.notifChannel]) === null || _b === void 0 ? void 0 : _b.config.socketUnsubChannel, {});
}
});
}
else {
(_a = notifSubs[conf.notifChannel]) === null || _a === void 0 ? void 0 : _a.listeners.push(listener);
}
};
const removeNoticeListener = (listener, socket) => {
if (noticeSubs) {
noticeSubs.listeners = noticeSubs.listeners.filter(nl => nl !== listener);
if (!noticeSubs.listeners.length && noticeSubs.config.socketUnsubChannel && socket) {
socket.emit(noticeSubs.config.socketUnsubChannel, {});
}
}
};
const addNoticeListener = (listener, conf, socket) => {
noticeSubs !== null && noticeSubs !== void 0 ? noticeSubs : (noticeSubs = {
config: conf,
listeners: []
});
if (!noticeSubs.listeners.length) {
socket.removeAllListeners(conf.socketChannel);
socket.on(conf.socketChannel, (notice) => {
if (noticeSubs && noticeSubs.listeners.length) {
noticeSubs.listeners.map(l => {
l(notice);
});
}
else {
socket.emit(conf.socketUnsubChannel, {});
}
});
}
noticeSubs.listeners.push(listener);
};
const sql = function (query, params, options) {
return new Promise((resolve, reject) => {
socket.emit(prostgles_types_1.CHANNELS.SQL, { query, params, options }, (err, res) => {
if (err) {
return reject(err);
}
if ((options === null || options === void 0 ? void 0 : options.returnType) === "stream") {
const { channel, unsubChannel } = res;
const start = (listener) => new Promise((resolveStart, rejectStart) => {
socket.on(channel, listener);
socket.emit(channel, {}, (pid, err) => {
if (err) {
rejectStart(err);
socket.removeAllListeners(channel);
}
else {
resolveStart({
pid,
run: (query, params) => {
return new Promise((resolveRun, rejectRun) => {
socket.emit(channel, { query, params }, (data, _err) => {
if (_err) {
rejectRun(_err);
}
else {
resolveRun(data);
}
});
});
},
stop: (terminate) => {
return new Promise((resolveStop, rejectStop) => {
socket.emit(unsubChannel, { terminate }, (data, _err) => {
if (_err) {
rejectStop(_err);
}
else {
resolveStop(data);
}
});
});
}
});
}
});
});
const streamHandlers = {
channel,
unsubChannel,
start,
};
return resolve(streamHandlers);
}
else if (options &&
(options.returnType === "noticeSubscription") &&
res &&
Object.keys(res).sort().join() === ["socketChannel", "socketUnsubChannel"].sort().join() &&
!Object.values(res).find(v => typeof v !== "string")) {
const sockInfo = res;
const addListener = (listener) => {
addNoticeListener(listener, sockInfo, socket);
return {
...sockInfo,
removeListener: () => removeNoticeListener(listener, socket)
};
};
const handle = {
...sockInfo,
addListener
};
// @ts-ignore
resolve(handle);
}
else if ((!options || !options.returnType || options.returnType !== "statement") &&
res &&
Object.keys(res).sort().join() === ["socketChannel", "socketUnsubChannel", "notifChannel"].sort().join() &&
!Object.values(res).find(v => typeof v !== "string")) {
const sockInfo = res;
const addListener = (listener) => {
addNotifListener(listener, sockInfo, socket);
return {
...res,
removeListener: () => removeNotifListener(listener, sockInfo, socket)
};
};
const handle = { ...res, addListener };
resolve(handle);
}
else {
resolve(res);
}
});
});
};
return { sql };
};
exports.getSqlHandler = getSqlHandler;