@event-driven-io/dumbo 0.1.0

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/index.cjs ADDED
@@ -0,0 +1,2 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }var _pg = require('pg'); var _pg2 = _interopRequireDefault(_pg);var r=new Map,u= exports.getPool =t=>{let e=typeof t=="string"?t:t.connectionString,s=typeof t=="string"?{connectionString:e}:t;return _nullishCoalesce(r.get(e), () => (r.set(e,new _pg2.default.Pool(s)).get(e)))},n= exports.endPool =async t=>{let e=r.get(t);e&&(await e.end(),r.delete(t))},m= exports.endAllPools =()=>Promise.all([...r.keys()].map(t=>n(t)));var f=(t,e)=>{let s=u({connectionString:t,database:e});return{connect:()=>s.connect(),close:()=>n(t)}};var l=async(t,e)=>{let s=await t.connect();try{return await e(s)}finally{s.release()}},p= exports.executeInTransaction =async(t,e)=>l(t,async s=>{try{await s.query("BEGIN");let{success:o,result:a}=await e(s);return o?await s.query("COMMIT"):await s.query("ROLLBACK"),a}catch(o){throw await s.query("ROLLBACK"),o}}),c= exports.executeSQL =async(t,e)=>"totalCount"in t?l(t,s=>s.query(e)):t.query(e),S= exports.executeSQLInTransaction =async(t,e)=>(console.log(e),p(t,async s=>({success:!0,result:await s.query(e)}))),T= exports.executeSQLBatchInTransaction =async(t,...e)=>p(t,async s=>{for(let o of e)await s.query(o);return{success:!0,result:void 0}}),E= exports.firstOrNull =async t=>{let e=await t;return e.rows.length>0?_nullishCoalesce(e.rows[0], () => (null)):null},M= exports.first =async t=>{let e=await t;if(e.rows.length===0)throw new Error("Query didn't return any result");return e.rows[0]},k= exports.singleOrNull =async t=>{let e=await t;if(e.rows.length>1)throw new Error("Query had more than one result");return e.rows.length>0?_nullishCoalesce(e.rows[0], () => (null)):null},R= exports.single =async t=>{let e=await t;if(e.rows.length===0)throw new Error("Query didn't return any result");if(e.rows.length>1)throw new Error("Query had more than one result");return e.rows[0]},I= exports.mapRows =async(t,e)=>(await t).rows.map(e),g= exports.toCamelCase =t=>t.replace(/_([a-z])/g,e=>_nullishCoalesce(_optionalChain([e, 'access', _ => _[1], 'optionalAccess', _2 => _2.toUpperCase, 'call', _3 => _3()]), () => (""))),b= exports.mapToCamelCase =t=>{let e={};for(let s in t)Object.prototype.hasOwnProperty.call(t,s)&&(e[g(s)]=t[s]);return e},v= exports.exists =async(t,e)=>(await R(c(t,e))).exists===!0;var _pgformat = require('pg-format'); var _pgformat2 = _interopRequireDefault(_pgformat);var N=(t,...e)=>_pgformat2.default.call(void 0, t,...e);exports.endAllPools = m; exports.endPool = n; exports.execute = l; exports.executeInTransaction = p; exports.executeSQL = c; exports.executeSQLBatchInTransaction = T; exports.executeSQLInTransaction = S; exports.exists = v; exports.first = M; exports.firstOrNull = E; exports.getPool = u; exports.mapRows = I; exports.mapToCamelCase = b; exports.postgresClient = f; exports.single = R; exports.singleOrNull = k; exports.sql = N; exports.toCamelCase = g;
2
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/connections/client.ts","../src/connections/pool.ts","../src/execute/index.ts","../src/sql/index.ts"],"names":["pg","pools","getPool","connectionStringOrOptions","connectionString","poolOptions","endPool","pool","endAllPools","postgresClient","database","execute","handle","client","executeInTransaction","success","result","e","executeSQL","poolOrClient","sql","executeSQLInTransaction","executeSQLBatchInTransaction","sqls","firstOrNull","getResult","first","singleOrNull","single","mapRows","map","toCamelCase","snakeStr","g","mapToCamelCase","obj","newObj","key","exists","format","sqlQuery","params"],"mappings":"AAAA,MAAe,KCAf,OAAOA,MAAQ,KAEf,IAAMC,EAA8B,IAAI,IAE3BC,EACXC,GACY,CACZ,IAAMC,EACJ,OAAOD,GAA8B,SACjCA,EACAA,EAA0B,iBAE1BE,EACJ,OAAOF,GAA8B,SACjC,CAAE,iBAAAC,CAAiB,EACnBD,EAGN,OACEF,EAAM,IAAIG,CAAgB,GAC1BH,EAAM,IAAIG,EAAkB,IAAIJ,EAAG,KAAKK,CAAW,CAAC,EAAE,IAAID,CAAgB,CAE9E,EAEaE,EAAU,MAAOF,GAA4C,CACxE,IAAMG,EAAON,EAAM,IAAIG,CAAgB,EACnCG,IACF,MAAMA,EAAK,IAAI,EACfN,EAAM,OAAOG,CAAgB,EAEjC,EAEaI,EAAc,IACzB,QAAQ,IACN,CAAC,GAAGP,EAAM,KAAK,CAAC,EAAE,IAAKG,GAAqBE,EAAQF,CAAgB,CAAC,CACvE,ED3BK,IAAMK,EAAiB,CAC5BL,EACAM,IACmB,CACnB,IAAMH,EAAOL,EAAQ,CAAE,iBAAAE,EAAkB,SAAAM,CAAS,CAAC,EAEnD,MAAO,CACL,QAAS,IAAMH,EAAK,QAAQ,EAC5B,MAAO,IAAMD,EAAQF,CAAgB,CACvC,CACF,EEfO,IAAMO,EAAU,MACrBJ,EACAK,IACG,CACH,IAAMC,EAAS,MAAMN,EAAK,QAAQ,EAClC,GAAI,CACF,OAAO,MAAMK,EAAOC,CAAM,CAC5B,QAAE,CACAA,EAAO,QAAQ,CACjB,CACF,EAEaC,EAAuB,MAClCP,EACAK,IAIAD,EAAQJ,EAAM,MAAOM,GAAW,CAC9B,GAAI,CACF,MAAMA,EAAO,MAAM,OAAO,EAE1B,GAAM,CAAE,QAAAE,EAAS,OAAAC,CAAO,EAAI,MAAMJ,EAAOC,CAAM,EAE/C,OAAIE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S,EAAG,MAAM,IAAI,MAAM,gCAAgC,EAE5E,OAAOA,EAAO,KAAK,CAAC,CACtB,EAEaa,EAAU,MAIrBJ,EACAK,KAEe,MAAML,GAEP,KAAK,IAAIK,CAAG,EAGfC,EAAeC,GAC1BA,EAAS,QAAQ,YAAcC,GAAMA,EAAE,CAAC,GAAG,YAAY,GAAK,EAAE,EAEnDC,EACXC,GACM,CACN,IAAMC,EAAkC,CAAC,EACzC,QAAWC,KAAOF,EACZ,OAAO,UAAU,eAAe,KAAKA,EAAKE,CAAG,IAC/CD,EAAOL,EAAYM,CAAG,CAAC,EAAIF,EAAIE,CAAG,GAGtC,OAAOD,CACT,EAIaE,EAAS,MAAO/B,EAAea,KAC3B,MAAMQ,EAAOV,EAAiCX,EAAMa,CAAG,CAAC,GAEzD,SAAW,GC5J3B,OAAOmB,MAAY,YAIZ,IAAMnB,EAAM,CAACoB,KAAqBC,IAChCF,EAAOC,EAAU,GAAGC,CAAM","sourcesContent":["import pg from 'pg';\nimport { endPool, getPool } from './pool';\n\nexport interface PostgresClient {\n connect(): Promise<pg.PoolClient>;\n close(): Promise<void>;\n}\n\nexport const postgresClient = (\n connectionString: string,\n database?: string,\n): PostgresClient => {\n const pool = getPool({ connectionString, database });\n\n return {\n connect: () => pool.connect(),\n close: () => endPool(connectionString),\n };\n};\n","import pg from 'pg';\n\nconst pools: Map<string, pg.Pool> = new Map();\n\nexport const getPool = (\n connectionStringOrOptions: string | pg.PoolConfig,\n): pg.Pool => {\n const connectionString =\n typeof connectionStringOrOptions === 'string'\n ? connectionStringOrOptions\n : connectionStringOrOptions.connectionString!;\n\n const poolOptions =\n typeof connectionStringOrOptions === 'string'\n ? { connectionString }\n : connectionStringOrOptions;\n\n //TODO: this should include database name resolution for key\n return (\n pools.get(connectionString) ??\n pools.set(connectionString, new pg.Pool(poolOptions)).get(connectionString)!\n );\n};\n\nexport const endPool = async (connectionString: string): Promise<void> => {\n const pool = pools.get(connectionString);\n if (pool) {\n await pool.end();\n pools.delete(connectionString);\n }\n};\n\nexport const endAllPools = () =>\n Promise.all(\n [...pools.keys()].map((connectionString) => endPool(connectionString)),\n );\n","import type pg from 'pg';\nimport type { SQL } from '../sql';\n\nexport const execute = async <Result = void>(\n pool: pg.Pool,\n handle: (client: pg.PoolClient) => Promise<Result>,\n) => {\n const client = await pool.connect();\n try {\n return await handle(client);\n } finally {\n client.release();\n }\n};\n\nexport const executeInTransaction = async <Result = void>(\n pool: pg.Pool,\n handle: (\n client: pg.PoolClient,\n ) => Promise<{ success: boolean; result: Result }>,\n): Promise<Result> =>\n execute(pool, async (client) => {\n try {\n await client.query('BEGIN');\n\n const { success, result } = await handle(client);\n\n if (success) await client.query('COMMIT');\n else await client.query('ROLLBACK');\n\n return result;\n } catch (e) {\n await client.query('ROLLBACK');\n throw e;\n }\n });\n\nexport const executeSQL = async <\n Result extends pg.QueryResultRow = pg.QueryResultRow,\n>(\n poolOrClient: pg.Pool | pg.PoolClient,\n sql: SQL,\n): Promise<pg.QueryResult<Result>> =>\n 'totalCount' in poolOrClient\n ? execute(poolOrClient, (client) => client.query<Result>(sql))\n : poolOrClient.query<Result>(sql);\n\nexport const executeSQLInTransaction = async <\n Result extends pg.QueryResultRow = pg.QueryResultRow,\n>(\n pool: pg.Pool,\n sql: SQL,\n) => {\n console.log(sql);\n return executeInTransaction(pool, async (client) => ({\n success: true,\n result: await client.query<Result>(sql),\n }));\n};\n\nexport const executeSQLBatchInTransaction = async <\n Result extends pg.QueryResultRow = pg.QueryResultRow,\n>(\n pool: pg.Pool,\n ...sqls: SQL[]\n) =>\n executeInTransaction(pool, async (client) => {\n for (const sql of sqls) {\n await client.query<Result>(sql);\n }\n\n return { success: true, result: undefined };\n });\n\nexport const firstOrNull = async <\n Result extends pg.QueryResultRow = pg.QueryResultRow,\n>(\n getResult: Promise<pg.QueryResult<Result>>,\n): Promise<Result | null> => {\n const result = await getResult;\n\n return result.rows.length > 0 ? result.rows[0] ?? null : null;\n};\n\nexport const first = async <\n Result extends pg.QueryResultRow = pg.QueryResultRow,\n>(\n getResult: Promise<pg.QueryResult<Result>>,\n): Promise<Result> => {\n const result = await getResult;\n\n if (result.rows.length === 0)\n throw new Error(\"Query didn't return any result\");\n\n return result.rows[0]!;\n};\n\nexport const singleOrNull = async <\n Result extends pg.QueryResultRow = pg.QueryResultRow,\n>(\n getResult: Promise<pg.QueryResult<Result>>,\n): Promise<Result | null> => {\n const result = await getResult;\n\n if (result.rows.length > 1) throw new Error('Query had more than one result');\n\n return result.rows.length > 0 ? result.rows[0] ?? null : null;\n};\n\nexport const single = async <\n Result extends pg.QueryResultRow = pg.QueryResultRow,\n>(\n getResult: Promise<pg.QueryResult<Result>>,\n): Promise<Result> => {\n const result = await getResult;\n\n if (result.rows.length === 0)\n throw new Error(\"Query didn't return any result\");\n\n if (result.rows.length > 1) throw new Error('Query had more than one result');\n\n return result.rows[0]!;\n};\n\nexport const mapRows = async <\n Result extends pg.QueryResultRow = pg.QueryResultRow,\n Mapped = unknown,\n>(\n getResult: Promise<pg.QueryResult<Result>>,\n map: (row: Result) => Mapped,\n): Promise<Mapped[]> => {\n const result = await getResult;\n\n return result.rows.map(map);\n};\n\nexport const toCamelCase = (snakeStr: string): string =>\n snakeStr.replace(/_([a-z])/g, (g) => g[1]?.toUpperCase() ?? '');\n\nexport const mapToCamelCase = <T extends Record<string, unknown>>(\n obj: T,\n): T => {\n const newObj: Record<string, unknown> = {};\n for (const key in obj) {\n if (Object.prototype.hasOwnProperty.call(obj, key)) {\n newObj[toCamelCase(key)] = obj[key];\n }\n }\n return newObj as T;\n};\n\nexport type ExistsSQLQueryResult = { exists: boolean };\n\nexport const exists = async (pool: pg.Pool, sql: SQL): Promise<boolean> => {\n const result = await single(executeSQL<ExistsSQLQueryResult>(pool, sql));\n\n return result.exists === true;\n};\n","import format from 'pg-format';\n\nexport type SQL = string & { __brand: 'sql' };\n\nexport const sql = (sqlQuery: string, ...params: unknown[]): SQL => {\n return format(sqlQuery, ...params) as SQL;\n};\n"]}
@@ -0,0 +1,38 @@
1
+ import pg from 'pg';
2
+
3
+ interface PostgresClient {
4
+ connect(): Promise<pg.PoolClient>;
5
+ close(): Promise<void>;
6
+ }
7
+ declare const postgresClient: (connectionString: string, database?: string) => PostgresClient;
8
+
9
+ declare const getPool: (connectionStringOrOptions: string | pg.PoolConfig) => pg.Pool;
10
+ declare const endPool: (connectionString: string) => Promise<void>;
11
+ declare const endAllPools: () => Promise<void[]>;
12
+
13
+ type SQL = string & {
14
+ __brand: 'sql';
15
+ };
16
+ declare const sql: (sqlQuery: string, ...params: unknown[]) => SQL;
17
+
18
+ declare const execute: <Result = void>(pool: pg.Pool, handle: (client: pg.PoolClient) => Promise<Result>) => Promise<Result>;
19
+ declare const executeInTransaction: <Result = void>(pool: pg.Pool, handle: (client: pg.PoolClient) => Promise<{
20
+ success: boolean;
21
+ result: Result;
22
+ }>) => Promise<Result>;
23
+ declare const executeSQL: <Result extends pg.QueryResultRow = pg.QueryResultRow>(poolOrClient: pg.Pool | pg.PoolClient, sql: SQL) => Promise<pg.QueryResult<Result>>;
24
+ declare const executeSQLInTransaction: <Result extends pg.QueryResultRow = pg.QueryResultRow>(pool: pg.Pool, sql: SQL) => Promise<pg.QueryResult<Result>>;
25
+ declare const executeSQLBatchInTransaction: <Result extends pg.QueryResultRow = pg.QueryResultRow>(pool: pg.Pool, ...sqls: SQL[]) => Promise<undefined>;
26
+ declare const firstOrNull: <Result extends pg.QueryResultRow = pg.QueryResultRow>(getResult: Promise<pg.QueryResult<Result>>) => Promise<Result | null>;
27
+ declare const first: <Result extends pg.QueryResultRow = pg.QueryResultRow>(getResult: Promise<pg.QueryResult<Result>>) => Promise<Result>;
28
+ declare const singleOrNull: <Result extends pg.QueryResultRow = pg.QueryResultRow>(getResult: Promise<pg.QueryResult<Result>>) => Promise<Result | null>;
29
+ declare const single: <Result extends pg.QueryResultRow = pg.QueryResultRow>(getResult: Promise<pg.QueryResult<Result>>) => Promise<Result>;
30
+ declare const mapRows: <Result extends pg.QueryResultRow = pg.QueryResultRow, Mapped = unknown>(getResult: Promise<pg.QueryResult<Result>>, map: (row: Result) => Mapped) => Promise<Mapped[]>;
31
+ declare const toCamelCase: (snakeStr: string) => string;
32
+ declare const mapToCamelCase: <T extends Record<string, unknown>>(obj: T) => T;
33
+ type ExistsSQLQueryResult = {
34
+ exists: boolean;
35
+ };
36
+ declare const exists: (pool: pg.Pool, sql: SQL) => Promise<boolean>;
37
+
38
+ export { type ExistsSQLQueryResult, type PostgresClient, type SQL, endAllPools, endPool, execute, executeInTransaction, executeSQL, executeSQLBatchInTransaction, executeSQLInTransaction, exists, first, firstOrNull, getPool, mapRows, mapToCamelCase, postgresClient, single, singleOrNull, sql, toCamelCase };
@@ -0,0 +1,38 @@
1
+ import pg from 'pg';
2
+
3
+ interface PostgresClient {
4
+ connect(): Promise<pg.PoolClient>;
5
+ close(): Promise<void>;
6
+ }
7
+ declare const postgresClient: (connectionString: string, database?: string) => PostgresClient;
8
+
9
+ declare const getPool: (connectionStringOrOptions: string | pg.PoolConfig) => pg.Pool;
10
+ declare const endPool: (connectionString: string) => Promise<void>;
11
+ declare const endAllPools: () => Promise<void[]>;
12
+
13
+ type SQL = string & {
14
+ __brand: 'sql';
15
+ };
16
+ declare const sql: (sqlQuery: string, ...params: unknown[]) => SQL;
17
+
18
+ declare const execute: <Result = void>(pool: pg.Pool, handle: (client: pg.PoolClient) => Promise<Result>) => Promise<Result>;
19
+ declare const executeInTransaction: <Result = void>(pool: pg.Pool, handle: (client: pg.PoolClient) => Promise<{
20
+ success: boolean;
21
+ result: Result;
22
+ }>) => Promise<Result>;
23
+ declare const executeSQL: <Result extends pg.QueryResultRow = pg.QueryResultRow>(poolOrClient: pg.Pool | pg.PoolClient, sql: SQL) => Promise<pg.QueryResult<Result>>;
24
+ declare const executeSQLInTransaction: <Result extends pg.QueryResultRow = pg.QueryResultRow>(pool: pg.Pool, sql: SQL) => Promise<pg.QueryResult<Result>>;
25
+ declare const executeSQLBatchInTransaction: <Result extends pg.QueryResultRow = pg.QueryResultRow>(pool: pg.Pool, ...sqls: SQL[]) => Promise<undefined>;
26
+ declare const firstOrNull: <Result extends pg.QueryResultRow = pg.QueryResultRow>(getResult: Promise<pg.QueryResult<Result>>) => Promise<Result | null>;
27
+ declare const first: <Result extends pg.QueryResultRow = pg.QueryResultRow>(getResult: Promise<pg.QueryResult<Result>>) => Promise<Result>;
28
+ declare const singleOrNull: <Result extends pg.QueryResultRow = pg.QueryResultRow>(getResult: Promise<pg.QueryResult<Result>>) => Promise<Result | null>;
29
+ declare const single: <Result extends pg.QueryResultRow = pg.QueryResultRow>(getResult: Promise<pg.QueryResult<Result>>) => Promise<Result>;
30
+ declare const mapRows: <Result extends pg.QueryResultRow = pg.QueryResultRow, Mapped = unknown>(getResult: Promise<pg.QueryResult<Result>>, map: (row: Result) => Mapped) => Promise<Mapped[]>;
31
+ declare const toCamelCase: (snakeStr: string) => string;
32
+ declare const mapToCamelCase: <T extends Record<string, unknown>>(obj: T) => T;
33
+ type ExistsSQLQueryResult = {
34
+ exists: boolean;
35
+ };
36
+ declare const exists: (pool: pg.Pool, sql: SQL) => Promise<boolean>;
37
+
38
+ export { type ExistsSQLQueryResult, type PostgresClient, type SQL, endAllPools, endPool, execute, executeInTransaction, executeSQL, executeSQLBatchInTransaction, executeSQLInTransaction, exists, first, firstOrNull, getPool, mapRows, mapToCamelCase, postgresClient, single, singleOrNull, sql, toCamelCase };
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ import"pg";import i from"pg";var r=new Map,u=t=>{let e=typeof t=="string"?t:t.connectionString,s=typeof t=="string"?{connectionString:e}:t;return r.get(e)??r.set(e,new i.Pool(s)).get(e)},n=async t=>{let e=r.get(t);e&&(await e.end(),r.delete(t))},m=()=>Promise.all([...r.keys()].map(t=>n(t)));var f=(t,e)=>{let s=u({connectionString:t,database:e});return{connect:()=>s.connect(),close:()=>n(t)}};var l=async(t,e)=>{let s=await t.connect();try{return await e(s)}finally{s.release()}},p=async(t,e)=>l(t,async s=>{try{await s.query("BEGIN");let{success:o,result:a}=await e(s);return o?await s.query("COMMIT"):await s.query("ROLLBACK"),a}catch(o){throw await s.query("ROLLBACK"),o}}),c=async(t,e)=>"totalCount"in t?l(t,s=>s.query(e)):t.query(e),S=async(t,e)=>(console.log(e),p(t,async s=>({success:!0,result:await s.query(e)}))),T=async(t,...e)=>p(t,async s=>{for(let o of e)await s.query(o);return{success:!0,result:void 0}}),E=async t=>{let e=await t;return e.rows.length>0?e.rows[0]??null:null},M=async t=>{let e=await t;if(e.rows.length===0)throw new Error("Query didn't return any result");return e.rows[0]},k=async t=>{let e=await t;if(e.rows.length>1)throw new Error("Query had more than one result");return e.rows.length>0?e.rows[0]??null:null},R=async t=>{let e=await t;if(e.rows.length===0)throw new Error("Query didn't return any result");if(e.rows.length>1)throw new Error("Query had more than one result");return e.rows[0]},I=async(t,e)=>(await t).rows.map(e),g=t=>t.replace(/_([a-z])/g,e=>e[1]?.toUpperCase()??""),b=t=>{let e={};for(let s in t)Object.prototype.hasOwnProperty.call(t,s)&&(e[g(s)]=t[s]);return e},v=async(t,e)=>(await R(c(t,e))).exists===!0;import y from"pg-format";var N=(t,...e)=>y(t,...e);export{m as endAllPools,n as endPool,l as execute,p as executeInTransaction,c as executeSQL,T as executeSQLBatchInTransaction,S as executeSQLInTransaction,v as exists,M as first,E as firstOrNull,u as getPool,I as mapRows,b as mapToCamelCase,f as postgresClient,R as single,k as singleOrNull,N as sql,g as toCamelCase};
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/connections/client.ts","../src/connections/pool.ts","../src/execute/index.ts","../src/sql/index.ts"],"sourcesContent":["import pg from 'pg';\nimport { endPool, getPool } from './pool';\n\nexport interface PostgresClient {\n connect(): Promise<pg.PoolClient>;\n close(): Promise<void>;\n}\n\nexport const postgresClient = (\n connectionString: string,\n database?: string,\n): PostgresClient => {\n const pool = getPool({ connectionString, database });\n\n return {\n connect: () => pool.connect(),\n close: () => endPool(connectionString),\n };\n};\n","import pg from 'pg';\n\nconst pools: Map<string, pg.Pool> = new Map();\n\nexport const getPool = (\n connectionStringOrOptions: string | pg.PoolConfig,\n): pg.Pool => {\n const connectionString =\n typeof connectionStringOrOptions === 'string'\n ? connectionStringOrOptions\n : connectionStringOrOptions.connectionString!;\n\n const poolOptions =\n typeof connectionStringOrOptions === 'string'\n ? { connectionString }\n : connectionStringOrOptions;\n\n //TODO: this should include database name resolution for key\n return (\n pools.get(connectionString) ??\n pools.set(connectionString, new pg.Pool(poolOptions)).get(connectionString)!\n );\n};\n\nexport const endPool = async (connectionString: string): Promise<void> => {\n const pool = pools.get(connectionString);\n if (pool) {\n await pool.end();\n pools.delete(connectionString);\n }\n};\n\nexport const endAllPools = () =>\n Promise.all(\n [...pools.keys()].map((connectionString) => endPool(connectionString)),\n );\n","import type pg from 'pg';\nimport type { SQL } from '../sql';\n\nexport const execute = async <Result = void>(\n pool: pg.Pool,\n handle: (client: pg.PoolClient) => Promise<Result>,\n) => {\n const client = await pool.connect();\n try {\n return await handle(client);\n } finally {\n client.release();\n }\n};\n\nexport const executeInTransaction = async <Result = void>(\n pool: pg.Pool,\n handle: (\n client: pg.PoolClient,\n ) => Promise<{ success: boolean; result: Result }>,\n): Promise<Result> =>\n execute(pool, async (client) => {\n try {\n await client.query('BEGIN');\n\n const { success, result } = await handle(client);\n\n if (success) await client.query('COMMIT');\n else await client.query('ROLLBACK');\n\n return result;\n } catch (e) {\n await client.query('ROLLBACK');\n throw e;\n }\n });\n\nexport const executeSQL = async <\n Result extends pg.QueryResultRow = pg.QueryResultRow,\n>(\n poolOrClient: pg.Pool | pg.PoolClient,\n sql: SQL,\n): Promise<pg.QueryResult<Result>> =>\n 'totalCount' in poolOrClient\n ? execute(poolOrClient, (client) => client.query<Result>(sql))\n : poolOrClient.query<Result>(sql);\n\nexport const executeSQLInTransaction = async <\n Result extends pg.QueryResultRow = pg.QueryResultRow,\n>(\n pool: pg.Pool,\n sql: SQL,\n) => {\n console.log(sql);\n return executeInTransaction(pool, async (client) => ({\n success: true,\n result: await client.query<Result>(sql),\n }));\n};\n\nexport const executeSQLBatchInTransaction = async <\n Result extends pg.QueryResultRow = pg.QueryResultRow,\n>(\n pool: pg.Pool,\n ...sqls: SQL[]\n) =>\n executeInTransaction(pool, async (client) => {\n for (const sql of sqls) {\n await client.query<Result>(sql);\n }\n\n return { success: true, result: undefined };\n });\n\nexport const firstOrNull = async <\n Result extends pg.QueryResultRow = pg.QueryResultRow,\n>(\n getResult: Promise<pg.QueryResult<Result>>,\n): Promise<Result | null> => {\n const result = await getResult;\n\n return result.rows.length > 0 ? result.rows[0] ?? null : null;\n};\n\nexport const first = async <\n Result extends pg.QueryResultRow = pg.QueryResultRow,\n>(\n getResult: Promise<pg.QueryResult<Result>>,\n): Promise<Result> => {\n const result = await getResult;\n\n if (result.rows.length === 0)\n throw new Error(\"Query didn't return any result\");\n\n return result.rows[0]!;\n};\n\nexport const singleOrNull = async <\n Result extends pg.QueryResultRow = pg.QueryResultRow,\n>(\n getResult: Promise<pg.QueryResult<Result>>,\n): Promise<Result | null> => {\n const result = await getResult;\n\n if (result.rows.length > 1) throw new Error('Query had more than one result');\n\n return result.rows.length > 0 ? result.rows[0] ?? null : null;\n};\n\nexport const single = async <\n Result extends pg.QueryResultRow = pg.QueryResultRow,\n>(\n getResult: Promise<pg.QueryResult<Result>>,\n): Promise<Result> => {\n const result = await getResult;\n\n if (result.rows.length === 0)\n throw new Error(\"Query didn't return any result\");\n\n if (result.rows.length > 1) throw new Error('Query had more than one result');\n\n return result.rows[0]!;\n};\n\nexport const mapRows = async <\n Result extends pg.QueryResultRow = pg.QueryResultRow,\n Mapped = unknown,\n>(\n getResult: Promise<pg.QueryResult<Result>>,\n map: (row: Result) => Mapped,\n): Promise<Mapped[]> => {\n const result = await getResult;\n\n return result.rows.map(map);\n};\n\nexport const toCamelCase = (snakeStr: string): string =>\n snakeStr.replace(/_([a-z])/g, (g) => g[1]?.toUpperCase() ?? '');\n\nexport const mapToCamelCase = <T extends Record<string, unknown>>(\n obj: T,\n): T => {\n const newObj: Record<string, unknown> = {};\n for (const key in obj) {\n if (Object.prototype.hasOwnProperty.call(obj, key)) {\n newObj[toCamelCase(key)] = obj[key];\n }\n }\n return newObj as T;\n};\n\nexport type ExistsSQLQueryResult = { exists: boolean };\n\nexport const exists = async (pool: pg.Pool, sql: SQL): Promise<boolean> => {\n const result = await single(executeSQL<ExistsSQLQueryResult>(pool, sql));\n\n return result.exists === true;\n};\n","import format from 'pg-format';\n\nexport type SQL = string & { __brand: 'sql' };\n\nexport const sql = (sqlQuery: string, ...params: unknown[]): SQL => {\n return format(sqlQuery, ...params) as SQL;\n};\n"],"mappings":"AAAA,MAAe,KCAf,OAAOA,MAAQ,KAEf,IAAMC,EAA8B,IAAI,IAE3BC,EACXC,GACY,CACZ,IAAMC,EACJ,OAAOD,GAA8B,SACjCA,EACAA,EAA0B,iBAE1BE,EACJ,OAAOF,GAA8B,SACjC,CAAE,iBAAAC,CAAiB,EACnBD,EAGN,OACEF,EAAM,IAAIG,CAAgB,GAC1BH,EAAM,IAAIG,EAAkB,IAAIJ,EAAG,KAAKK,CAAW,CAAC,EAAE,IAAID,CAAgB,CAE9E,EAEaE,EAAU,MAAOF,GAA4C,CACxE,IAAMG,EAAON,EAAM,IAAIG,CAAgB,EACnCG,IACF,MAAMA,EAAK,IAAI,EACfN,EAAM,OAAOG,CAAgB,EAEjC,EAEaI,EAAc,IACzB,QAAQ,IACN,CAAC,GAAGP,EAAM,KAAK,CAAC,EAAE,IAAKG,GAAqBE,EAAQF,CAAgB,CAAC,CACvE,ED3BK,IAAMK,EAAiB,CAC5BC,EACAC,IACmB,CACnB,IAAMC,EAAOC,EAAQ,CAAE,iBAAAH,EAAkB,SAAAC,CAAS,CAAC,EAEnD,MAAO,CACL,QAAS,IAAMC,EAAK,QAAQ,EAC5B,MAAO,IAAME,EAAQJ,CAAgB,CACvC,CACF,EEfO,IAAMK,E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AAe,MAG1BF,GAC2B,CAC3B,IAAMT,EAAS,MAAMS,EAErB,GAAIT,EAAO,KAAK,OAAS,EAAG,MAAM,IAAI,MAAM,gCAAgC,EAE5E,OAAOA,EAAO,KAAK,OAAS,EAAIA,EAAO,KAAK,CAAC,GAAK,KAAO,IAC3D,EAEaY,EAAS,MAGpBH,GACoB,CACpB,IAAMT,EAAS,MAAMS,EAErB,GAAIT,EAAO,KAAK,SAAW,EACzB,MAAM,IAAI,MAAM,gCAAgC,EAElD,GAAIA,EAAO,KAAK,OAAS,EAAG,MAAM,IAAI,MAAM,gCAAgC,EAE5E,OAAOA,EAAO,KAAK,CAAC,CACtB,EAEaa,EAAU,MAIrBJ,EACAK,KAEe,MAAML,GAEP,KAAK,IAAIK,CAAG,EAGfC,EAAeC,GAC1BA,EAAS,QAAQ,YAAcC,GAAMA,EAAE,CAAC,GAAG,YAAY,GAAK,EAAE,EAEnDC,EACXC,GACM,CACN,IAAMC,EAAkC,CAAC,EACzC,QAAWC,KAAOF,EACZ,OAAO,UAAU,eAAe,KAAKA,EAAKE,CAAG,IAC/CD,EAAOL,EAAYM,CAAG,CAAC,EAAIF,EAAIE,CAAG,GAGtC,OAAOD,CACT,EAIaE,EAAS,MAAO3B,EAAeS,KAC3B,MAAMQ,EAAOV,EAAiCP,EAAMS,CAAG,CAAC,GAEzD,SAAW,GC5J3B,OAAOmB,MAAY,YAIZ,IAAMC,EAAM,CAACC,KAAqBC,IAChCH,EAAOE,EAAU,GAAGC,CAAM","names":["pg","pools","getPool","connectionStringOrOptions","connectionString","poolOptions","endPool","pool","endAllPools","postgresClient","connectionString","database","pool","getPool","endPool","execute","pool","handle","client","executeInTransaction","success","result","e","executeSQL","poolOrClient","sql","executeSQLInTransaction","executeSQLBatchInTransaction","sqls","firstOrNull","getResult","first","singleOrNull","single","mapRows","map","toCamelCase","snakeStr","g","mapToCamelCase","obj","newObj","key","exists","format","sql","sqlQuery","params"]}
package/package.json ADDED
@@ -0,0 +1,60 @@
1
+ {
2
+ "name": "@event-driven-io/dumbo",
3
+ "version": "0.1.0",
4
+ "description": "Dumbo - tools for dealing with PostgreSQL",
5
+ "type": "module",
6
+ "scripts": {
7
+ "build": "tsup",
8
+ "build:ts": "tsc",
9
+ "build:ts:watch": "tsc --watch",
10
+ "test": "run-s test:unit test:int test:e2e",
11
+ "test:unit": "glob -c \"node --import tsx --test\" **/*.unit.spec.ts",
12
+ "test:int": "glob -c \"node --import tsx --test\" **/*.int.spec.ts",
13
+ "test:e2e": "glob -c \"node --import tsx --test\" **/*.e2e.spec.ts",
14
+ "test:watch": "node --import tsx --test --watch",
15
+ "test:unit:watch": "glob -c \"node --import tsx --test --watch\" **/*.unit.spec.ts",
16
+ "test:int:watch": "glob -c \"node --import tsx --test --watch\" **/*.int.spec.ts",
17
+ "test:e2e:watch": "glob -c \"node --import tsx --test --watch\" **/*.e2e.spec.ts"
18
+ },
19
+ "repository": {
20
+ "type": "git",
21
+ "url": "git+https://github.com/event-driven-io/Pongo.git"
22
+ },
23
+ "keywords": [
24
+ "Event Sourcing"
25
+ ],
26
+ "author": "Oskar Dudycz",
27
+ "bugs": {
28
+ "url": "https://github.com/event-driven-io/Pongo/issues"
29
+ },
30
+ "homepage": "https://event-driven-io.github.io/Pongo/",
31
+ "exports": {
32
+ ".": {
33
+ "import": {
34
+ "types": "./dist/index.d.ts",
35
+ "default": "./dist/index.js"
36
+ },
37
+ "require": {
38
+ "types": "./dist/index.d.cts",
39
+ "default": "./dist/index.cjs"
40
+ }
41
+ }
42
+ },
43
+ "main": "./dist/index.cjs",
44
+ "module": "./dist/index.js",
45
+ "types": "./dist/index.d.ts",
46
+ "files": [
47
+ "dist"
48
+ ],
49
+ "peerDependencies": {
50
+ "@types/uuid": "^9.0.8",
51
+ "@types/pg": "^8.11.6",
52
+ "@types/pg-format": "^1.0.5",
53
+ "pg": "^8.12.0",
54
+ "pg-format": "^1.0.4",
55
+ "uuid": "^9.0.1"
56
+ },
57
+ "devDependencies": {
58
+ "@types/node": "20.11.30"
59
+ }
60
+ }