- 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
36 lines
1,020 B
TypeScript
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;
|
|
|