-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
268 changed files
with
11,704 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -74,7 +74,7 @@ node_modules/ | |
versions.json | ||
|
||
# Dist | ||
/dist | ||
# /dist | ||
/docs/.vitepress/cache | ||
/docs/.vitepress/dist | ||
/lib | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import type { ClientBase, ClientConfig, QueryArrayConfig, QueryConfig } from 'pg'; | ||
import type { DB, Logger } from './types'; | ||
export interface DBConnection extends DB { | ||
createConnection(): Promise<void>; | ||
column(columnName: string, queryConfig: QueryArrayConfig, values?: any[]): Promise<any[]>; | ||
Check failure on line 5 in dist/db.d.ts
|
||
column(columnName: string, queryConfig: QueryConfig): Promise<any[]>; | ||
column(columnName: string, queryTextOrConfig: string | QueryConfig, values?: any[]): Promise<any[]>; | ||
Check failure on line 7 in dist/db.d.ts
|
||
connected: () => boolean; | ||
addBeforeCloseListener: (listener: any) => number; | ||
close(): Promise<void>; | ||
} | ||
declare function db(connection: ClientBase | string | ClientConfig, logger?: Logger): DBConnection; | ||
export default db; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
"use strict"; | ||
var __create = Object.create; | ||
var __defProp = Object.defineProperty; | ||
var __getOwnPropDesc = Object.getOwnPropertyDescriptor; | ||
var __getOwnPropNames = Object.getOwnPropertyNames; | ||
var __getProtoOf = Object.getPrototypeOf; | ||
var __hasOwnProp = Object.prototype.hasOwnProperty; | ||
var __export = (target, all) => { | ||
for (var name in all) | ||
__defProp(target, name, { get: all[name], enumerable: true }); | ||
}; | ||
var __copyProps = (to, from, except, desc) => { | ||
if (from && typeof from === "object" || typeof from === "function") { | ||
Check failure on line 13 in dist/db.js
|
||
for (let key of __getOwnPropNames(from)) | ||
if (!__hasOwnProp.call(to, key) && key !== except) | ||
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); | ||
} | ||
return to; | ||
}; | ||
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( | ||
// If the importer is in node compatibility mode or this is not an ESM | ||
// file that has been converted to a CommonJS file using a Babel- | ||
// compatible transform (i.e. "__esModule" has not been set), then set | ||
// "default" to the CommonJS "module.exports" for node compatibility. | ||
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, | ||
mod | ||
)); | ||
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); | ||
var db_exports = {}; | ||
__export(db_exports, { | ||
default: () => db_default | ||
}); | ||
module.exports = __toCommonJS(db_exports); | ||
var import_node_util = require("node:util"); | ||
var import_pg = __toESM(require("pg")); | ||
var ConnectionStatus = /* @__PURE__ */ ((ConnectionStatus2) => { | ||
ConnectionStatus2["DISCONNECTED"] = "DISCONNECTED"; | ||
ConnectionStatus2["CONNECTED"] = "CONNECTED"; | ||
ConnectionStatus2["ERROR"] = "ERROR"; | ||
ConnectionStatus2["EXTERNAL"] = "EXTERNAL"; | ||
return ConnectionStatus2; | ||
})(ConnectionStatus || {}); | ||
function db(connection, logger = console) { | ||
const isExternalClient = typeof connection === "object" && "query" in connection && typeof connection.query === "function"; | ||
const client = isExternalClient ? connection : new import_pg.default.Client(connection); | ||
let connectionStatus = isExternalClient ? "EXTERNAL" /* EXTERNAL */ : "DISCONNECTED" /* DISCONNECTED */; | ||
const beforeCloseListeners = []; | ||
const connected = () => connectionStatus === "CONNECTED" /* CONNECTED */ || connectionStatus === "EXTERNAL" /* EXTERNAL */; | ||
const createConnection = () => new Promise((resolve, reject) => { | ||
if (connected()) { | ||
resolve(); | ||
} else if (connectionStatus === "ERROR" /* ERROR */) { | ||
reject( | ||
new Error("Connection already failed, do not try to connect again") | ||
); | ||
} else { | ||
client.connect((err) => { | ||
if (err) { | ||
connectionStatus = "ERROR" /* ERROR */; | ||
logger.error(`could not connect to postgres: ${(0, import_node_util.inspect)(err)}`); | ||
reject(err); | ||
return; | ||
} | ||
connectionStatus = "CONNECTED" /* CONNECTED */; | ||
resolve(); | ||
}); | ||
} | ||
}); | ||
const query = async (queryTextOrConfig, values) => { | ||
await createConnection(); | ||
try { | ||
return await client.query(queryTextOrConfig, values); | ||
} catch (error) { | ||
const { message, position } = error; | ||
const string = typeof queryTextOrConfig === "string" ? queryTextOrConfig : queryTextOrConfig.text; | ||
if (message && position >= 1) { | ||
const endLineWrapIndexOf = string.indexOf("\n", position); | ||
const endLineWrapPos = endLineWrapIndexOf >= 0 ? endLineWrapIndexOf : string.length; | ||
const stringStart = string.slice(0, endLineWrapPos); | ||
const stringEnd = string.slice(endLineWrapPos); | ||
const startLineWrapPos = stringStart.lastIndexOf("\n") + 1; | ||
const padding = " ".repeat(position - startLineWrapPos - 1); | ||
logger.error(`Error executing: | ||
${stringStart} | ||
${padding}^^^^${stringEnd} | ||
${message} | ||
`); | ||
} else { | ||
logger.error(`Error executing: | ||
${string} | ||
${error} | ||
`); | ||
} | ||
throw error; | ||
} | ||
}; | ||
const select = async (queryTextOrConfig, values) => { | ||
const { rows } = await query(queryTextOrConfig, values); | ||
return rows; | ||
}; | ||
const column = async (columnName, queryTextOrConfig, values) => { | ||
const rows = await select(queryTextOrConfig, values); | ||
return rows.map((r) => r[columnName]); | ||
}; | ||
return { | ||
createConnection, | ||
query, | ||
select, | ||
column, | ||
connected, | ||
addBeforeCloseListener: (listener) => beforeCloseListeners.push(listener), | ||
close: async () => { | ||
await beforeCloseListeners.reduce( | ||
(promise, listener) => promise.then(listener).catch((error) => { | ||
logger.error(error.stack || error); | ||
}), | ||
Promise.resolve() | ||
); | ||
if (!isExternalClient) { | ||
connectionStatus = "DISCONNECTED" /* DISCONNECTED */; | ||
client.end(); | ||
} | ||
} | ||
}; | ||
} | ||
var db_default = db; |
Oops, something went wrong.