UNPKG

@devbro/sql-generator

Version:
1 lines 3.98 kB
{"version":3,"sources":["../../../src/databases/postgresql/PostgresqlConnection.ts"],"sourcesContent":["import { Connection as ConnectionAbs } from '../../Connection';\nimport { Connection, PoolClient, PoolConfig } from 'pg';\nimport { Pool } from 'pg';\nimport { CompiledSql } from '../../types';\nimport { Query } from '../../Query';\nimport { PostgresqlQueryGrammar } from './PostgresqlQueryGrammar';\nimport { Schema } from '../../Schema';\nimport { PostgresqlSchemaGrammar } from './PostgresqlSchemaGrammar';\nimport Cursor from 'pg-cursor';\n\nexport class PostgresqlConnection extends ConnectionAbs {\n connection: PoolClient | undefined;\n static pool: Pool;\n\n static defaults: PoolConfig = {\n port: 5432,\n ssl: false,\n max: 20,\n idleTimeoutMillis: 1, // wait X milli seconds before closing an idle/released connection\n connectionTimeoutMillis: 30000, // wait up to 30 seconds to obtain a new connection\n maxUses: 7500,\n };\n\n constructor(params: PoolConfig) {\n super();\n if (!PostgresqlConnection.pool) {\n PostgresqlConnection.pool = new Pool({ ...PostgresqlConnection.defaults, ...params });\n }\n }\n async connect(): Promise<boolean> {\n this.connection = await PostgresqlConnection.pool.connect();\n return true;\n }\n async runQuery(sql: CompiledSql) {\n const result = await this.connection?.query(sql.sql, sql.bindings);\n return result?.rows;\n }\n\n async runCursor(sql: CompiledSql): Promise<any> {\n return this.connection?.query(new Cursor(sql.sql, sql.bindings));\n }\n\n async disconnect(): Promise<boolean> {\n await this.connection?.release();\n return true;\n }\n\n getQuery(): Query {\n return new Query(this, new PostgresqlQueryGrammar());\n }\n\n getSchema(): Schema {\n return new Schema(this, new PostgresqlSchemaGrammar());\n }\n\n async beginTransaction(): Promise<void> {\n if (!this.connection) {\n throw new Error('No active connection to begin a transaction.');\n }\n await this.connection.query('BEGIN');\n }\n\n async commit(): Promise<void> {\n if (!this.connection) {\n throw new Error('No active connection to commit a transaction.');\n }\n await this.connection.query('COMMIT');\n }\n\n async rollback(): Promise<void> {\n if (!this.connection) {\n throw new Error('No active connection to rollback a transaction.');\n }\n await this.connection.query('ROLLBACK');\n }\n\n static async destroy(): Promise<void> {\n PostgresqlConnection.pool.end();\n return;\n }\n}\n"],"mappings":"AAAA,SAAS,cAAc,qBAAqB;AAE5C,SAAS,YAAY;AAErB,SAAS,aAAa;AACtB,SAAS,8BAA8B;AACvC,SAAS,cAAc;AACvB,SAAS,+BAA+B;AACxC,OAAO,YAAY;AAEZ,MAAM,6BAA6B,cAAc;AAAA,EACtD;AAAA,EACA,OAAO;AAAA,EAEP,OAAO,WAAuB;AAAA,IAC5B,MAAM;AAAA,IACN,KAAK;AAAA,IACL,KAAK;AAAA,IACL,mBAAmB;AAAA;AAAA,IACnB,yBAAyB;AAAA;AAAA,IACzB,SAAS;AAAA,EACX;AAAA,EAEA,YAAY,QAAoB;AAC9B,UAAM;AACN,QAAI,CAAC,qBAAqB,MAAM;AAC9B,2BAAqB,OAAO,IAAI,KAAK,EAAE,GAAG,qBAAqB,UAAU,GAAG,OAAO,CAAC;AAAA,IACtF;AAAA,EACF;AAAA,EACA,MAAM,UAA4B;AAChC,SAAK,aAAa,MAAM,qBAAqB,KAAK,QAAQ;AAC1D,WAAO;AAAA,EACT;AAAA,EACA,MAAM,SAAS,KAAkB;AAC/B,UAAM,SAAS,MAAM,KAAK,YAAY,MAAM,IAAI,KAAK,IAAI,QAAQ;AACjE,WAAO,QAAQ;AAAA,EACjB;AAAA,EAEA,MAAM,UAAU,KAAgC;AAC9C,WAAO,KAAK,YAAY,MAAM,IAAI,OAAO,IAAI,KAAK,IAAI,QAAQ,CAAC;AAAA,EACjE;AAAA,EAEA,MAAM,aAA+B;AACnC,UAAM,KAAK,YAAY,QAAQ;AAC/B,WAAO;AAAA,EACT;AAAA,EAEA,WAAkB;AAChB,WAAO,IAAI,MAAM,MAAM,IAAI,uBAAuB,CAAC;AAAA,EACrD;AAAA,EAEA,YAAoB;AAClB,WAAO,IAAI,OAAO,MAAM,IAAI,wBAAwB,CAAC;AAAA,EACvD;AAAA,EAEA,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