Something went wrong. Try again.
A self-hosted web platform for applications & services with design based on Google's Material 3 Expressive. (Work In Progress)
self-hosted typescript
Something went wrong. Try again.
2.2 kB · 69 lines
TypeScript
at commit 3b00a86a
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970import postgres from "postgres";import type { Instance } from "../index.ts";import System from "../system.ts";
export default class DatabaseSystem extends System { databaseConnections: { [connectionId: string]: postgres.Sql };
constructor(instance: Instance) { super("database", instance);
this.databaseConnections = {}; }
// private sqlite(connectionId: string) { // if (this.databaseConnections[connectionId]) return this.databaseConnections[connectionId];
// const conPath = `sqlite://${path.join(this.instance.sys.filesystem.FS_ROOT, connectionId)}.sqlite`;
// const con = new SQL({ // adapter: "sqlite", // readwrite: true, // create: true, // filename: conPath, // });
// this.databaseConnections[connectionId] = con;
// return con; // }
/** * Create or get an already active postgresql database connection * @returns postgres.Sql<{}> */ postgres() { if (this.databaseConnections.postgres) return this.databaseConnections.postgres;
this.log.info( `Connecting to database @ '${this.log.emphasis(this.instance.sys.configuration.databases.postgres.host)}' named '${this.log.emphasis(this.instance.sys.configuration.databases.postgres.database)}'`, );
const con = postgres({ db: this.instance.sys.configuration.databases.postgres.database, hostname: this.instance.sys.configuration.databases.postgres.host, port: this.instance.sys.configuration.databases.postgres.port, user: this.instance.sys.configuration.databases.postgres.user, password: this.instance.sys.configuration.databases.postgres.password,
onnotice: (message) => { if (message.severity === "NOTICE") return;
console.log(message); }, });
this.databaseConnections.postgres = con;
this.log.info( `Established connection to database @ '${this.log.emphasis(this.instance.sys.configuration.databases.postgres.host)}' named '${this.log.emphasis(this.instance.sys.configuration.databases.postgres.database)}'`, );
return con; }
override async startup(): Promise<boolean> { this.log.info("Starting up..."); return true; }}