This commit is contained in:
Morten Olsen
2025-07-28 22:27:40 +02:00
parent b35782a7d8
commit 48f1bde404
25 changed files with 3341 additions and 235 deletions

View File

@@ -1,7 +1,9 @@
import knex, { type Knex } from "knex";
import type { PostgresDatabase, PostgresRole } from "./postgres.types.ts";
import { Services } from "../../utils/service.ts";
import { ConfigService } from "../config/config.ts";
import knex, { type Knex } from 'knex';
import { Services } from '../../utils/service.ts';
import { ConfigService } from '../config/config.ts';
import type { PostgresDatabase, PostgresRole } from './postgres.types.ts';
class PostgresService {
#db: Knex;
@@ -21,39 +23,24 @@ class PostgresService {
}
public upsertRole = async (role: PostgresRole) => {
const existingRole = await this.#db.raw(
'SELECT 1 FROM pg_roles WHERE rolname = ?',
[role.name]
);
const existingRole = await this.#db.raw('SELECT 1 FROM pg_roles WHERE rolname = ?', [role.name]);
if (existingRole.rows.length === 0) {
await this.#db.raw(
`CREATE ROLE ${role.name} WITH LOGIN PASSWORD '${role.password}'`,
);
await this.#db.raw(`CREATE ROLE ${role.name} WITH LOGIN PASSWORD '${role.password}'`);
} else {
await this.#db.raw(
`ALTER ROLE ${role.name} WITH PASSWORD '${role.password}'`,
);
await this.#db.raw(`ALTER ROLE ${role.name} WITH PASSWORD '${role.password}'`);
}
}
};
public upsertDatabase = async (database: PostgresDatabase) => {
const existingDatabase = await this.#db.raw(
'SELECT * FROM pg_database WHERE datname = ?',
[database.name]
);
const existingDatabase = await this.#db.raw('SELECT * FROM pg_database WHERE datname = ?', [database.name]);
if (existingDatabase.rows.length === 0) {
await this.#db.raw(
`CREATE DATABASE ${database.name} OWNER ${database.owner}`,
);
await this.#db.raw(`CREATE DATABASE ${database.name} OWNER ${database.owner}`);
} else {
await this.#db.raw(
`ALTER DATABASE ${database.name} OWNER TO ${database.owner}`,
);
await this.#db.raw(`ALTER DATABASE ${database.name} OWNER TO ${database.owner}`);
}
}
};
}
export { PostgresService };
export { PostgresService };

View File

@@ -8,4 +8,4 @@ type PostgresDatabase = {
owner: string;
};
export type { PostgresRole, PostgresDatabase };
export type { PostgresRole, PostgresDatabase };