nodecommons-esm-database-postgres 0.0.2

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.
@@ -0,0 +1,3 @@
1
+ import { commonsPostgresServiceToUrl, CommonsPostgresService } from './services/commons-postgres.service.mjs';
2
+ import { ICommonsPostgresCredentials, isICommonsPostgresCredentials } from './interfaces/icommons-postgres-credentials.mjs';
3
+ export { commonsPostgresServiceToUrl, CommonsPostgresService, ICommonsPostgresCredentials, isICommonsPostgresCredentials };
package/dist/index.mjs ADDED
@@ -0,0 +1,4 @@
1
+ import { commonsPostgresServiceToUrl, CommonsPostgresService } from './services/commons-postgres.service.mjs';
2
+ import { isICommonsPostgresCredentials } from './interfaces/icommons-postgres-credentials.mjs';
3
+ export { commonsPostgresServiceToUrl, CommonsPostgresService, isICommonsPostgresCredentials };
4
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.mjs","sourceRoot":"","sources":["../src/index.mts"],"names":[],"mappings":"AAAA,OAAO,EAAE,2BAA2B,EAAE,sBAAsB,EAAE,MAAM,yCAAyC,CAAC;AAC9G,OAAO,EAA+B,6BAA6B,EAAE,MAAM,gDAAgD,CAAC;AAC5H,OAAO,EACN,2BAA2B,EAC3B,sBAAsB,EAEtB,6BAA6B,EAC7B,CAAC"}
@@ -0,0 +1,6 @@
1
+ import { ICommonsCredentials } from 'nodecommons-esm-database';
2
+ export interface ICommonsPostgresCredentials extends ICommonsCredentials {
3
+ user: string;
4
+ password: string;
5
+ }
6
+ export declare function isICommonsPostgresCredentials(test: any): test is ICommonsPostgresCredentials;
@@ -0,0 +1,12 @@
1
+ import { commonsTypeHasPropertyString } from 'tscommons-esm-core';
2
+ import { isICommonsCredentials } from 'nodecommons-esm-database';
3
+ export function isICommonsPostgresCredentials(test) {
4
+ if (!isICommonsCredentials(test))
5
+ return false;
6
+ if (!commonsTypeHasPropertyString(test, 'user'))
7
+ return false;
8
+ if (!commonsTypeHasPropertyString(test, 'password'))
9
+ return false;
10
+ return true;
11
+ }
12
+ //# sourceMappingURL=icommons-postgres-credentials.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"icommons-postgres-credentials.mjs","sourceRoot":"","sources":["../../src/interfaces/icommons-postgres-credentials.mts"],"names":[],"mappings":"AAAA,OAAO,EAAE,4BAA4B,EAAE,MAAM,oBAAoB,CAAC;AAElE,OAAO,EAAuB,qBAAqB,EAAE,MAAM,0BAA0B,CAAC;AAOtF,MAAM,UAAU,6BAA6B,CAAC,IAAS;IACtD,IAAI,CAAC,qBAAqB,CAAC,IAAI,CAAC;QAAE,OAAO,KAAK,CAAC;IAE/C,IAAI,CAAC,4BAA4B,CAAC,IAAI,EAAE,MAAM,CAAC;QAAE,OAAO,KAAK,CAAC;IAC9D,IAAI,CAAC,4BAA4B,CAAC,IAAI,EAAE,UAAU,CAAC;QAAE,OAAO,KAAK,CAAC;IAElE,OAAO,IAAI,CAAC;AACb,CAAC"}
@@ -0,0 +1,15 @@
1
+ import { TPropertyObject } from 'tscommons-esm-core';
2
+ import { CommonsSqlDatabaseService } from 'nodecommons-esm-database';
3
+ import { ICommonsPostgresCredentials } from '../interfaces/icommons-postgres-credentials.mjs';
4
+ export declare function commonsPostgresServiceToUrl(credentials: ICommonsPostgresCredentials): string;
5
+ export declare class CommonsPostgresService extends CommonsSqlDatabaseService<ICommonsPostgresCredentials> {
6
+ private database?;
7
+ constructor(credentials: ICommonsPostgresCredentials);
8
+ connect(): Promise<void>;
9
+ doesTableExist(name: string): Promise<boolean>;
10
+ internalSelect(sql: string, params?: TPropertyObject): Promise<TPropertyObject[]>;
11
+ internalNone(sql: string, params?: TPropertyObject): Promise<void>;
12
+ protected internalTransactionBegin(): Promise<void>;
13
+ protected internalTransactionCommit(): Promise<void>;
14
+ protected internalTransactionRollback(): Promise<void>;
15
+ }
@@ -0,0 +1,86 @@
1
+ import pgPromise from 'pg-promise';
2
+ import { ECommonsDatabaseEngine } from 'nodecommons-esm-database';
3
+ import { CommonsSqlDatabaseService } from 'nodecommons-esm-database';
4
+ export function commonsPostgresServiceToUrl(credentials) {
5
+ return `postgres://${credentials.user}:${credentials.password}@${credentials.host || 'localhost'}:${credentials.port || 5432}/${credentials.name}`;
6
+ }
7
+ function rewriteSql(sql, params) {
8
+ for (const field of Object.keys(params)) {
9
+ const regex = new RegExp(`:${field}(\\W|$)`, 'g');
10
+ sql = sql.replace(regex, '${' + field + '}$1');
11
+ }
12
+ return sql;
13
+ }
14
+ export class CommonsPostgresService extends CommonsSqlDatabaseService {
15
+ database;
16
+ constructor(credentials) {
17
+ super(ECommonsDatabaseEngine.POSTGRES, credentials);
18
+ }
19
+ async connect() {
20
+ const pgp = pgPromise({
21
+ // Initialization Options
22
+ });
23
+ const cn = commonsPostgresServiceToUrl(this.credentials);
24
+ this.database = pgp(cn); // eslint-disable-line @typescript-eslint/no-unsafe-assignment
25
+ }
26
+ async doesTableExist(name) {
27
+ return (await this.internalSelect(`
28
+ SELECT 1
29
+ FROM information_schema.tables
30
+ WHERE table_schema = 'public'
31
+ AND table_name = ${this.quote(name)}
32
+ `)).length > 0;
33
+ }
34
+ async internalSelect(sql, params) {
35
+ if (!this.database)
36
+ throw new Error('No connection to database');
37
+ try {
38
+ let rows = [];
39
+ if (params) {
40
+ sql = rewriteSql(sql, params);
41
+ rows = await this.database.manyOrNone(sql, params);
42
+ }
43
+ else {
44
+ rows = await this.database.manyOrNone(sql);
45
+ }
46
+ return rows;
47
+ }
48
+ catch (e) {
49
+ console.error(e);
50
+ throw e;
51
+ }
52
+ }
53
+ async internalNone(sql, params) {
54
+ if (!this.database)
55
+ throw new Error('No connection to database');
56
+ try {
57
+ if (params) {
58
+ sql = rewriteSql(sql, params);
59
+ await this.database.none(sql, params);
60
+ }
61
+ else {
62
+ await this.database.none(sql);
63
+ }
64
+ }
65
+ catch (e) {
66
+ console.error(e);
67
+ throw e;
68
+ }
69
+ }
70
+ async internalTransactionBegin() {
71
+ if (!this.database)
72
+ throw new Error('No connection to database');
73
+ // not implemented yet
74
+ }
75
+ async internalTransactionCommit() {
76
+ if (!this.database)
77
+ throw new Error('No connection to database');
78
+ // not implemented yet
79
+ }
80
+ async internalTransactionRollback() {
81
+ if (!this.database)
82
+ throw new Error('No connection to database');
83
+ // not implemented yet
84
+ }
85
+ }
86
+ //# sourceMappingURL=commons-postgres.service.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"commons-postgres.service.mjs","sourceRoot":"","sources":["../../src/services/commons-postgres.service.mts"],"names":[],"mappings":"AAAA,OAAO,SAAS,MAAM,YAAY,CAAC;AAKnC,OAAO,EAAE,sBAAsB,EAAE,MAAM,0BAA0B,CAAC;AAClE,OAAO,EAAE,yBAAyB,EAAE,MAAM,0BAA0B,CAAC;AAIrE,MAAM,UAAU,2BAA2B,CAAC,WAAwC;IACnF,OAAO,cAAc,WAAW,CAAC,IAAI,IAAI,WAAW,CAAC,QAAQ,IAAI,WAAW,CAAC,IAAI,IAAI,WAAW,IAAI,WAAW,CAAC,IAAI,IAAI,IAAI,IAAI,WAAW,CAAC,IAAI,EAAE,CAAC;AACpJ,CAAC;AAED,SAAS,UAAU,CAAC,GAAW,EAAE,MAAuB;IACvD,KAAK,MAAM,KAAK,IAAI,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC;QACzC,MAAM,KAAK,GAAW,IAAI,MAAM,CAAC,IAAI,KAAK,SAAS,EAAE,GAAG,CAAC,CAAC;QAC1D,GAAG,GAAG,GAAG,CAAC,OAAO,CAAC,KAAK,EAAE,IAAI,GAAG,KAAK,GAAG,KAAK,CAAC,CAAC;IAChD,CAAC;IAED,OAAO,GAAG,CAAC;AACZ,CAAC;AAED,MAAM,OAAO,sBAAuB,SAAQ,yBAAsD;IACzF,QAAQ,CAAkB;IAElC,YAAY,WAAwC;QACnD,KAAK,CACH,sBAAsB,CAAC,QAAQ,EAC/B,WAAW,CACZ,CAAC;IACH,CAAC;IAEM,KAAK,CAAC,O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CAAC;YACJ,IAAI,MAAM,EAAE,CAAC;gBACZ,GAAG,GAAG,UAAU,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC;gBAE9B,MAAM,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC;YACvC,CAAC;iBAAM,CAAC;gBACP,MAAM,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;YAC/B,CAAC;QACF,CAAC;QAAC,OAAO,CAAC,EAAE,CAAC;YACZ,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;YACjB,MAAM,CAAC,CAAC;QACT,CAAC;IACF,CAAC;IAES,KAAK,CAAC,wBAAwB;QACvC,IAAI,CAAC,IAAI,CAAC,QAAQ;YAAE,MAAM,IAAI,KAAK,CAAC,2BAA2B,CAAC,CAAC;QAEjE,sBAAsB;IACvB,CAAC;IAES,KAAK,CAAC,yBAAyB;QACxC,IAAI,CAAC,IAAI,CAAC,QAAQ;YAAE,MAAM,IAAI,KAAK,CAAC,2BAA2B,CAAC,CAAC;QAEjE,sBAAsB;IACvB,CAAC;IAES,KAAK,CAAC,2BAA2B;QAC1C,IAAI,CAAC,IAAI,CAAC,QAAQ;YAAE,MAAM,IAAI,KAAK,CAAC,2BAA2B,CAAC,CAAC;QAEjE,sBAAsB;IACvB,CAAC;CACD"}
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "nodecommons-esm-database-postgres",
3
+ "version": "0.0.2",
4
+ "description": "",
5
+ "scripts": {
6
+ "tsc": "./node_modules/typescript/bin/tsc",
7
+ "preprepare": "rm -rf ./dist; php ~/Dev/etim.php src/ && npm run tsc",
8
+ "publish-major": "rm -rf dist; npm run tsc && npx eslint . && npm run preprepare && npm version major && npm install && npm publish && git add . && git commit -m 'publish'",
9
+ "publish-minor": "rm -rf dist; npm run tsc && npx eslint . && npm run preprepare && npm version minor && npm install && npm publish && git add . && git commit -m 'publish'",
10
+ "publish-patch": "rm -rf dist; npm run tsc && npx eslint . && npm run preprepare && npm version patch && npm install && npm publish && git add . && git commit -m 'publish'"
11
+ },
12
+ "main": "dist/index.mjs",
13
+ "types": "dist/index.d.mjs",
14
+ "type": "module",
15
+ "author": "Pete Morris",
16
+ "license": "ISC",
17
+ "devDependencies": {
18
+ "@stylistic/eslint-plugin-ts": "^2.10.1",
19
+ "@types/node": "^22.9.0",
20
+ "eslint-plugin-import": "^2.31.0",
21
+ "eslint-plugin-prefer-arrow-functions": "^3.4.1",
22
+ "typescript": "^5.6.3",
23
+ "typescript-eslint": "^8.14.0"
24
+ },
25
+ "files": [
26
+ "dist/**/*"
27
+ ],
28
+ "dependencies": {
29
+ "nodecommons-esm-database": "^0.0.2",
30
+ "pg-promise": "^11.10.2",
31
+ "tscommons-esm-core": "^0.0.2"
32
+ }
33
+ }