ts-postgres
Version:
PostgreSQL client in TypeScript
208 lines • 7.17 kB
JavaScript
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
if (kind === "m") throw new TypeError("Private method is not writable");
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _ResultRowImpl_names, _ResultRowImpl_lookup;
function makeRecord(names, data) {
const result = {};
names.forEach((key, j) => (result[key] = data[j]));
return result;
}
class ResultRowImpl extends Array {
constructor() {
super(...arguments);
_ResultRowImpl_names.set(this, void 0);
_ResultRowImpl_lookup.set(this, void 0);
}
set(names, lookup, values) {
__classPrivateFieldSet(this, _ResultRowImpl_names, names, "f");
__classPrivateFieldSet(this, _ResultRowImpl_lookup, lookup, "f");
this.push(...values);
}
/**
* Return value for the provided column name.
*/
get(name) {
const i = __classPrivateFieldGet(this, _ResultRowImpl_lookup, "f")?.get(name);
if (i === undefined)
throw new Error(`Invalid column name: ${String(name)}`);
return this[i];
}
/**
* Return an object mapping column names to values.
*/
reify() {
if (__classPrivateFieldGet(this, _ResultRowImpl_names, "f") === undefined)
throw new Error('Column names not available');
return makeRecord(__classPrivateFieldGet(this, _ResultRowImpl_names, "f"), this);
}
}
_ResultRowImpl_names = new WeakMap(), _ResultRowImpl_lookup = new WeakMap();
/**
* The awaited query result.
*
* Iterating over the result yields objects of the generic type parameter.
*/
export class Result {
constructor(names, rows, status) {
this.names = names;
this.rows = rows;
this.status = status;
}
[Symbol.iterator]() {
let i = 0;
const rows = this.rows;
const length = rows.length;
const names = this.names;
const shift = () => {
const data = rows[i++];
return makeRecord(names, data);
};
return {
next: () => {
if (i === length)
return { done: true, value: undefined };
return { done: false, value: shift() };
},
};
}
}
/**
* The query result iterator.
*
* Iterating asynchronously yields objects of the generic type parameter.
*/
class ResultIteratorImpl extends Promise {
constructor(names, data, executor) {
super((resolve, reject) => {
executor((status) => {
const names = this.names || [];
const data = this.data || [];
const lookup = new Map();
let i = 0;
for (const name of names) {
lookup.set(name, i);
i++;
}
resolve(new Result(names, data.map((values) => {
const row = new ResultRowImpl();
row.set(names, lookup, values);
return row;
}), status));
}, reject);
});
this.names = names;
this.data = data;
this.subscribers = [];
this.done = false;
}
/**
* Return the first item (if any) from the query results.
*/
async first() {
for await (const row of this) {
return row;
}
}
/**
* Return the first item from the query results, or throw an error.
*/
async one() {
for await (const row of this) {
return row;
}
throw new Error('Query returned an empty result');
}
notify(done, status) {
if (done)
this.done = true;
for (const subscriber of this.subscribers)
subscriber(done, status);
this.subscribers.length = 0;
}
[Symbol.asyncIterator]() {
let i = 0;
//const container = this.container;
const shift = () => {
const names = this.names;
const values = this.data[i];
i++;
if (names === null) {
throw new Error('Column name mapping missing.');
}
return makeRecord(names, values);
};
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
let error = null;
this.catch((reason) => {
error = new Error(reason);
});
return {
next: async () => {
if (error) {
throw error;
}
if (this.data.length <= i) {
if (this.done) {
return { done: true, value: undefined };
}
if (await new Promise((resolve, reject) => {
this.subscribers.push((done, status) => {
if (typeof status !== 'undefined') {
reject(status);
}
else {
resolve(done);
}
});
})) {
return { done: true, value: undefined };
}
}
return { value: shift(), done: false };
},
};
}
}
ResultIteratorImpl.prototype.constructor = Promise;
export function makeResult(transform) {
let dataHandler = null;
const names = [];
const rows = [];
const p = new ResultIteratorImpl(names, rows, (resolve, reject) => {
dataHandler = (row) => {
if (row === null || typeof row === 'string') {
resolve(row);
p.notify(true);
}
else if (Array.isArray(row)) {
rows.push(row);
p.notify(false);
}
else {
reject(row);
p.notify(true, row);
}
};
});
const nameHandler = (ns) => {
names.length = 0;
if (transform) {
ns = ns.map(transform);
}
names.push(...ns);
};
return {
iterator: p,
dataHandler: dataHandler,
nameHandler: nameHandler,
};
}
//# sourceMappingURL=result.js.map