refactor: move these types to `types.d.ts`

pull/249/head
Sylvie 6 months ago
parent 495bbcf1db
commit fa38f14a0c
No known key found for this signature in database
GPG Key ID: 75AB0FE5B983A3AF

@ -1,43 +1,4 @@
import { AssFile, AssUser, NID, UploadToken } from "ass";
export type DatabaseValue = AssFile | AssUser | UploadToken;
export type DatabaseTable = 'assfiles' | 'assusers' | 'asstokens';
// todo: move this to types.d.ts
/**
* interface for database classes
*/
export interface Database {
/**
* preform database initialization tasks
*/
open(): Promise<void>;
/**
* preform database suspension tasks
*/
close(): Promise<void>;
/**
* set up database
*/
configure(): Promise<void>;
/**
* put a value in the database
*/
put(table: DatabaseTable, key: NID, data: DatabaseValue): Promise<void>;
/**
* get a value from the database
*/
get(table: DatabaseTable, key: NID): Promise<DatabaseValue | undefined>;
/**
* get all values from the database
*/
getAll(table: DatabaseTable): Promise<{ [key: string]: DatabaseValue | undefined }[]>;
}
import { NID, Database, DatabaseTable, DatabaseValue } from "ass";
export class DBManager {
private static _db: Database;

@ -1,9 +1,8 @@
import { AssFile, AssUser, FilesSchema, UsersSchema } from 'ass';
import { AssFile, AssUser, FilesSchema, UsersSchema, Database, DatabaseTable, DatabaseValue } from 'ass';
import path, { resolve } from 'path';
import fs from 'fs-extra';
import { Database, DatabaseTable, DatabaseValue } from './database.js';
import { log } from '../log.js';
import { nanoid } from '../generators.js';

@ -1,10 +1,9 @@
import { AssFile, AssUser, NID, UploadToken } from 'ass';
import { AssFile, AssUser, NID, UploadToken, Database, DatabaseTable, DatabaseValue } from 'ass';
import mysql, { Pool } from 'mysql2/promise';
import { log } from '../log.js';
import { UserConfig } from '../UserConfig.js';
import { Database, DatabaseTable, DatabaseValue } from './database.js';
export class MySQLDatabase implements Database {
private _pool: Pool;

@ -1,8 +1,7 @@
import { PostgresConfiguration } from 'ass';
import { PostgresConfiguration, Database, DatabaseTable, DatabaseValue } from 'ass';
import pg from 'pg';
import { log } from '../log.js';
import { Database, DatabaseTable, DatabaseValue } from './database.js';
import { UserConfig } from '../UserConfig.js';
/**

39
common/types.d.ts vendored

@ -3,6 +3,9 @@ declare module 'ass' {
type IdType = 'random' | 'original' | 'gfycat' | 'timestamp' | 'zws'
export type DatabaseValue = AssFile | AssUser | UploadToken;
export type DatabaseTable = 'assfiles' | 'assusers' | 'asstokens';
/**
* Core Express server config.
* This is separate from the user configuration starting in 0.15.0
@ -51,6 +54,42 @@ declare module 'ass' {
}
}
/**
* interface for database classes
*/
export interface Database {
/**
* preform database initialization tasks
*/
open(): Promise<void>;
/**
* preform database suspension tasks
*/
close(): Promise<void>;
/**
* set up database
*/
configure(): Promise<void>;
/**
* put a value in the database
*/
put(table: DatabaseTable, key: NID, data: DatabaseValue): Promise<void>;
/**
* get a value from the database
*/
get(table: DatabaseTable, key: NID): Promise<DatabaseValue | undefined>;
/**
* get all values from the database
*/
getAll(table: DatabaseTable): Promise<{ [key: string]: DatabaseValue | undefined; }[]>;
}
interface DatabaseConfiguration {
kind: 'mysql' | 'postgres' | 'json';
options?: MySQLConfiguration | PostgresConfiguration;

Loading…
Cancel
Save