ft_transcendence/src/@shared/src/database/index.ts
Maieul BOYER 5a905a1239 feat(oauth2/db): reworked oauth2 database footprint
- Removed `auth` table and merged its information inside the `user`
  table
- Changed around some field names in the database
- Changed Create*User functions to not be using overload but different
  functions
2025-11-18 15:30:55 +01:00

36 lines
1,020 B
TypeScript

import fp from 'fastify-plugin';
import { FastifyInstance, FastifyPluginAsync } from 'fastify';
import { isNullish } from '@shared/utils';
import { Database as DbImpl } from './mixin/_base';
import { IUserDb, UserImpl } from './mixin/user';
Object.assign(DbImpl.prototype, UserImpl);
export interface Database extends DbImpl, IUserDb { }
// When using .decorate you have to specify added properties for Typescript
declare module 'fastify' {
export interface FastifyInstance {
db: Database;
}
}
let dbAdded = false;
export const useDatabase = fp<FastifyPluginAsync>(async function(
f: FastifyInstance,
_options: object) {
void _options;
if (dbAdded) { return; }
dbAdded = true;
const path = process.env.DATABASE_DIR;
if (isNullish(path)) { throw 'env `DATABASE_DIR` not defined'; }
f.log.info(`Opening database with path: ${path}/database.db`);
const db: Database = new DbImpl(`${path}/database.db`) as Database;
if (!f.hasDecorator('db')) { f.decorate('db', db); }
});
export default useDatabase;