@synotech/utils
Version:
a collection of utilities for internal use
63 lines (55 loc) • 1.17 kB
text/typescript
import { Knex as KnexInterface } from 'knex';
interface KnexConfig {
client: 'pg';
connection: {
user?: string;
host?: string;
database?: string;
password?: string;
port?: number;
ssl?: any
};
}
class PostgreSQL {
private knex: KnexInterface;
constructor(config: KnexConfig) {
this.knex = require('knex')(config);
}
async updateTable(
objectId: string,
tableName: string,
data: Record<string, any>
): Promise<boolean> {
try {
const result = await this.knex(tableName)
.where({ objectId })
.update(data);
console.log(`Updated ${result} row(s)`);
return true;
} catch (error) {
throw error;
}
}
async runAnalyzeOnTable(): Promise<void> {
try {
await this.knex.raw('ANALYZE');
} catch (error) {
throw error;
}
}
}
const config: KnexConfig = {
client: 'pg',
connection: {
user: process.env.PG_DB_USER,
host: process.env.PG_DB_HOST,
database: process.env.PG_DB_NAME,
password: process.env.PG_DB_PASSWORD,
port: parseInt(process.env.PG_DB_PORT ?? '5432', 10),
ssl: {
rejectUnauthorized: false,
},
},
};
const postgres = new PostgreSQL(config);
export { postgres, PostgreSQL };