db-conn-pgsql
Version:
Database Connecton Postgres SQL
53 lines • 1.55 kB
JavaScript
import { PgSqlConnection } from "./PgSqlConnection.js";
import * as URL from 'url';
import pg from "pg";
import { PgSqlDataSource } from "./PgSqlDataSource.js";
import { PgSqlDialect } from "./dialect/PgSqlDialect.js";
export class PgSqlDriver {
oDialect = new PgSqlDialect();
getDialect() {
return this.oDialect;
}
getPool(url, props) {
const config = this.getConfig(url, props);
const ds = new PgSqlDataSource(config);
return ds;
}
acceptsURL(url) {
const rt = url.startsWith('postgres://');
return rt;
}
async connect(url, props) {
//const pool = new Pool(config);
//const rt = new PgSqlConnection(pool);
const config = this.getConfig(url, props);
const client = new pg.Client(config);
await client.connect();
const rt = new PgSqlConnection(client);
return rt;
}
getConfig(url, props) {
const config = {
port: 5432,
user: 'postgres'
};
const u = URL.parse(url, false);
const [user, password] = u.auth.split(":");
if (user) {
config.user = user;
}
if (password) {
config.password = password;
}
config.host = u.hostname;
if (u.port) {
config.port = parseInt(u.port);
}
if (u.path) {
const database = u.path.substring(1);
config.database = database;
}
return config;
}
}
//# sourceMappingURL=PgSqlDriver.js.map