replace python django backend with nodejs backend

This commit is contained in:
wea_ondara
2024-05-27 18:59:58 +02:00
parent ebd0748894
commit 8b60d023e8
123 changed files with 15193 additions and 88 deletions

59
backend/src/knex.ts Normal file
View File

@@ -0,0 +1,59 @@
import knex, {type Knex} from 'knex';
import {Model} from 'objection';
import type {ConfigType} from './config/confighelper';
import {type Migration, Migrations} from './migrations/_migrations';
export async function initDb(config: ConfigType, enableForeignKeys: boolean): Promise<Knex> {
const dbConf = (config.database as any)[config.database.use];
if (!dbConf) {
throw new Error('No database config found with name "' + config.database.use + '"!');
}
if (dbConf.client === 'sqlite3') {
if (!dbConf.pool) {
dbConf.pool = {};
}
if (enableForeignKeys) {
dbConf.pool.afterCreate = (conn: any, cb: any) => {
conn.run('PRAGMA foreign_keys = ON', cb);
};
}
}
const knx = knex(dbConf);
Model.knex(knx);
return knx;
}
export async function migrateDb(knex: Knex): Promise<boolean> {
try {
await knex.migrate.latest({
migrationSource: new ViteMigrationSource(Migrations),
});
return true;
} catch (e) {
console.log(e);
return false;
}
}
class ViteMigrationSource implements Knex.MigrationSource<Migration> {
private readonly migrations: Migration[];
constructor(migrations: Migration[]) {
this.migrations = migrations;
}
getMigrations(loadExtensions: string[]): Promise<Migration[]> {
return Promise.resolve(this.migrations);
}
getMigrationName(migration: Migration): string {
return migration.name;
}
getMigration(migration: Migration): Promise<Knex.Migration> {
return Promise.resolve(migration.migration);
}
}