mirror of https://github.com/tycrek/ass
Merge pull request #245 from XWasHere/dev/0.15.0
commit
347b80e5c9
@ -0,0 +1,105 @@
|
|||||||
|
import { AssFile, AssUser, NID, UploadToken } from "ass";
|
||||||
|
|
||||||
|
export type DatabaseValue = AssFile | AssUser | UploadToken;
|
||||||
|
export type DatabaseTable = 'assfiles' | 'assusers' | 'asstokens';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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<{ [index: string]: DatabaseValue }>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class DBManager {
|
||||||
|
private static _db: Database;
|
||||||
|
private static _dbReady: boolean = false;
|
||||||
|
public static get ready() {
|
||||||
|
return this._dbReady;
|
||||||
|
}
|
||||||
|
|
||||||
|
static {
|
||||||
|
process.on('exit', () => {
|
||||||
|
if (DBManager._db) DBManager._db.close();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* activate a database
|
||||||
|
*/
|
||||||
|
public static use(db: Database): Promise<void> {
|
||||||
|
return new Promise(async (resolve, reject) => {
|
||||||
|
if (this._db != undefined) {
|
||||||
|
await this._db.close();
|
||||||
|
this._dbReady = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
this._db = db;
|
||||||
|
await this._db.open();
|
||||||
|
await this._db.configure();
|
||||||
|
|
||||||
|
this._dbReady = true;
|
||||||
|
resolve();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public static configure(): Promise<void> {
|
||||||
|
if (this._db && this._dbReady) {
|
||||||
|
return this._db.configure();
|
||||||
|
} else throw new Error("No database active");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* put a value in the database
|
||||||
|
*/
|
||||||
|
public static put(table: DatabaseTable, key: NID, data: DatabaseValue): Promise<void> {
|
||||||
|
if (this._db && this._dbReady) {
|
||||||
|
return this._db.put(table, key, data);
|
||||||
|
} else throw new Error("No database active");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get a value from the database
|
||||||
|
*/
|
||||||
|
public static get(table: DatabaseTable, key: NID): Promise<DatabaseValue | undefined> {
|
||||||
|
if (this._db && this._dbReady) {
|
||||||
|
return this._db.get(table, key);
|
||||||
|
} else throw new Error("No database active");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get all values from the database
|
||||||
|
*/
|
||||||
|
public static getAll(table: DatabaseTable): Promise<{ [index: string]: DatabaseValue }> {
|
||||||
|
if (this._db && this._dbReady) {
|
||||||
|
return this._db.getAll(table);
|
||||||
|
} else throw new Error("No database active");
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,152 @@
|
|||||||
|
import { AssFile, AssUser, FilesSchema, UsersSchema } from 'ass';
|
||||||
|
|
||||||
|
import path, { resolve } from 'path';
|
||||||
|
import fs from 'fs-extra';
|
||||||
|
|
||||||
|
import { Database, DatabaseTable, DatabaseValue } from './database';
|
||||||
|
import { log } from '../log';
|
||||||
|
import { nanoid } from '../generators';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Absolute filepaths for JSON data files
|
||||||
|
*/
|
||||||
|
const PATHS = {
|
||||||
|
files: path.join('.ass-data/files.json'),
|
||||||
|
users: path.join('.ass-data/users.json')
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* map from tables to paths
|
||||||
|
*/
|
||||||
|
const PATHMAP = {
|
||||||
|
assfiles: PATHS.files,
|
||||||
|
assusers: PATHS.users
|
||||||
|
} as { [index: string]: string };
|
||||||
|
|
||||||
|
/**
|
||||||
|
* map from tables to sectors
|
||||||
|
*/
|
||||||
|
const SECTORMAP = {
|
||||||
|
assfiles: 'files',
|
||||||
|
assusers: 'users'
|
||||||
|
} as { [index: string]: string };
|
||||||
|
|
||||||
|
const bothWriter = async (files: FilesSchema, users: UsersSchema) => {
|
||||||
|
await fs.writeJson(PATHS.files, files, { spaces: '\t' });
|
||||||
|
await fs.writeJson(PATHS.users, users, { spaces: '\t' });
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a JSON file with a given empty data template
|
||||||
|
*/
|
||||||
|
const createEmptyJson = (filepath: string, emptyData: any): Promise<void> => new Promise(async (resolve, reject) => {
|
||||||
|
try {
|
||||||
|
if (!(await fs.pathExists(filepath))) {
|
||||||
|
await fs.ensureFile(filepath);
|
||||||
|
await fs.writeJson(filepath, emptyData, { spaces: '\t' });
|
||||||
|
}
|
||||||
|
resolve(void 0);
|
||||||
|
} catch (err) {
|
||||||
|
reject(err);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Ensures the data files exist and creates them if required
|
||||||
|
*/
|
||||||
|
export const ensureFiles = (): Promise<void> => new Promise(async (resolve, reject) => {
|
||||||
|
log.debug('Checking data files');
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
// * Default files.json
|
||||||
|
await createEmptyJson(PATHS.files, {
|
||||||
|
files: {},
|
||||||
|
useSql: false,
|
||||||
|
meta: {}
|
||||||
|
} as FilesSchema);
|
||||||
|
|
||||||
|
// * Default users.json
|
||||||
|
await createEmptyJson(PATHS.users, {
|
||||||
|
tokens: [],
|
||||||
|
users: {},
|
||||||
|
cliKey: nanoid(32),
|
||||||
|
useSql: false,
|
||||||
|
meta: {}
|
||||||
|
} as UsersSchema);
|
||||||
|
|
||||||
|
log.debug('Data files exist');
|
||||||
|
resolve();
|
||||||
|
} catch (err) {
|
||||||
|
log.error('Failed to verify existence of data files');
|
||||||
|
reject(err);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* JSON database. i know json isnt sql, shut up.
|
||||||
|
*/
|
||||||
|
export class JSONDatabase implements Database {
|
||||||
|
public open(): Promise<void> { return Promise.resolve() }
|
||||||
|
public close(): Promise<void> { return Promise.resolve() }
|
||||||
|
|
||||||
|
public configure(): Promise<void> {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
ensureFiles();
|
||||||
|
|
||||||
|
resolve();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public put(table: DatabaseTable, key: string, data: DatabaseValue): Promise<void> {
|
||||||
|
return new Promise(async (resolve, reject) => {
|
||||||
|
if (table == 'assfiles') {
|
||||||
|
// ? Local JSON
|
||||||
|
const filesJson = await fs.readJson(PATHS.files) as FilesSchema;
|
||||||
|
|
||||||
|
// Check if key already exists
|
||||||
|
if (filesJson.files[key] != null) return reject(new Error(`File key ${key} already exists`));
|
||||||
|
|
||||||
|
// Otherwise add the data
|
||||||
|
filesJson.files[key] = data as AssFile;
|
||||||
|
|
||||||
|
// Also save the key to the users file
|
||||||
|
const usersJson = await fs.readJson(PATHS.users) as UsersSchema;
|
||||||
|
// todo: uncomment this once users are implemented
|
||||||
|
// usersJson.users[data.uploader].files.push(key);
|
||||||
|
|
||||||
|
// Save the files
|
||||||
|
await bothWriter(filesJson, usersJson);
|
||||||
|
|
||||||
|
resolve()
|
||||||
|
} else if (table == 'assusers') {
|
||||||
|
// ? Local JSON
|
||||||
|
const usersJson = await fs.readJson(PATHS.users) as UsersSchema;
|
||||||
|
|
||||||
|
// Check if key already exists
|
||||||
|
if (usersJson.users[key] != null) return reject(new Error(`User key ${key} already exists`));
|
||||||
|
|
||||||
|
// Otherwise add the data
|
||||||
|
usersJson.users[key] = data as AssUser;
|
||||||
|
|
||||||
|
await fs.writeJson(PATHS.users, usersJson, { spaces: '\t' });
|
||||||
|
|
||||||
|
resolve();
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
public get(table: DatabaseTable, key: string): Promise<DatabaseValue | undefined> {
|
||||||
|
return new Promise(async (resolve, reject) => {
|
||||||
|
const data = (await fs.readJson(PATHMAP[table]))[SECTORMAP[table]][key];
|
||||||
|
(!data) ? resolve(undefined) : resolve(data);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public getAll(table: DatabaseTable): Promise<{ [index: string]: DatabaseValue }> {
|
||||||
|
return new Promise(async (resolve, reject) => {
|
||||||
|
const data = (await fs.readJson(PATHMAP[table]))[SECTORMAP[table]];
|
||||||
|
(!data) ? resolve({}) : resolve(data);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,201 @@
|
|||||||
|
import { PostgresConfiguration } from 'ass';
|
||||||
|
|
||||||
|
import { Client } from 'pg';
|
||||||
|
|
||||||
|
import { log } from '../log';
|
||||||
|
import { Database, DatabaseTable, DatabaseValue } from './database';
|
||||||
|
import { UserConfig } from '../UserConfig';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* database adapter for postgresql
|
||||||
|
*/
|
||||||
|
export class PostgreSQLDatabase implements Database {
|
||||||
|
private _client: Client;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* validate config
|
||||||
|
*/
|
||||||
|
private _validateConfig(): string | undefined {
|
||||||
|
// make sure the configuration exists
|
||||||
|
if (!UserConfig.ready) return 'User configuration not ready';
|
||||||
|
if (typeof UserConfig.config.database != 'object') return 'PostgreSQL configuration missing';
|
||||||
|
if (UserConfig.config.database.kind != "postgres") return 'Database not set to PostgreSQL, but PostgreSQL is in use, something has gone terribly wrong';
|
||||||
|
if (typeof UserConfig.config.database.options != 'object') return 'PostgreSQL configuration missing';
|
||||||
|
|
||||||
|
let config = UserConfig.config.database.options;
|
||||||
|
|
||||||
|
// check the postgres config
|
||||||
|
const checker = (val: string) => val != null && val !== '';
|
||||||
|
const issue =
|
||||||
|
!checker(config.host) ? 'Missing PostgreSQL Host'
|
||||||
|
: !checker(config.user) ? 'Missing PostgreSQL User'
|
||||||
|
: !checker(config.password) ? 'Missing PostgreSQL Password'
|
||||||
|
: !checker(config.database) ? 'Missing PostgreSQL Database'
|
||||||
|
// ! Blame VS Code for this weird indentation
|
||||||
|
: undefined;
|
||||||
|
|
||||||
|
return issue;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public open(): Promise<void> {
|
||||||
|
return new Promise(async (resolve, reject) => {
|
||||||
|
try {
|
||||||
|
// config check
|
||||||
|
let configError = this._validateConfig();
|
||||||
|
if (configError) throw new Error(configError);
|
||||||
|
|
||||||
|
// grab the config
|
||||||
|
let config = UserConfig.config.database!.options! as PostgresConfiguration;
|
||||||
|
|
||||||
|
// set up the client
|
||||||
|
this._client = new Client({
|
||||||
|
host: config.host,
|
||||||
|
port: config.port,
|
||||||
|
user: config.user,
|
||||||
|
password: config.password,
|
||||||
|
database: config.database,
|
||||||
|
});
|
||||||
|
|
||||||
|
// connect to the database
|
||||||
|
log.info('PostgreSQL', `connecting to ${config.host}:${config.port}`);
|
||||||
|
await this._client.connect();
|
||||||
|
log.success('PostgreSQL', 'ok');
|
||||||
|
|
||||||
|
resolve();
|
||||||
|
} catch (err) {
|
||||||
|
log.error('PostgreSQL', 'failed to connect');
|
||||||
|
console.error(err);
|
||||||
|
reject(err);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public close(): Promise<void> {
|
||||||
|
return new Promise(async (resolve, reject) => {
|
||||||
|
try {
|
||||||
|
// gracefully disconnect
|
||||||
|
await this._client.end();
|
||||||
|
|
||||||
|
resolve();
|
||||||
|
} catch (err) {
|
||||||
|
log.error('PostgreSQL', 'failed to disconnect');
|
||||||
|
console.error(err);
|
||||||
|
reject(err);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public configure(): Promise<void> {
|
||||||
|
return new Promise(async (resolve, reject) => {
|
||||||
|
try {
|
||||||
|
await this._client.query(
|
||||||
|
`CREATE TABLE IF NOT EXISTS asstables (
|
||||||
|
name TEXT PRIMARY KEY,
|
||||||
|
version INT NOT NULL
|
||||||
|
);`);
|
||||||
|
|
||||||
|
log.info('PostgreSQL', 'checking database');
|
||||||
|
|
||||||
|
// update tables
|
||||||
|
let seenRows = new Set<string>();
|
||||||
|
let versions = await this._client.query('SELECT * FROM asstables;');
|
||||||
|
for (let row of versions.rows) {
|
||||||
|
seenRows.add(row.name);
|
||||||
|
}
|
||||||
|
|
||||||
|
const assTableSchema = '(id TEXT PRIMARY KEY, data JSON NOT NULL)'
|
||||||
|
|
||||||
|
// add missing tables
|
||||||
|
if (!seenRows.has('assfiles')) {
|
||||||
|
log.warn('PostgreSQL', 'assfiles missing, repairing...')
|
||||||
|
await this._client.query(
|
||||||
|
`CREATE TABLE assfiles ${assTableSchema};` +
|
||||||
|
`INSERT INTO asstables (name, version) VALUES ('assfiles', 1);`
|
||||||
|
);
|
||||||
|
log.success('PostgreSQL', 'ok');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!seenRows.has('assusers')) {
|
||||||
|
log.warn('PostgreSQL', 'asstokens missing, repairing...')
|
||||||
|
await this._client.query(
|
||||||
|
`CREATE TABLE assusers ${assTableSchema};` +
|
||||||
|
`INSERT INTO asstables (name, version) VALUES ('assusers', 1);`
|
||||||
|
);
|
||||||
|
log.success('PostgreSQL', 'ok');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!seenRows.has('asstokens')) {
|
||||||
|
log.warn('PostgreSQL', 'asstokens missing, repairing...')
|
||||||
|
await this._client.query(
|
||||||
|
`CREATE TABLE asstokens ${assTableSchema};` +
|
||||||
|
`INSERT INTO asstables (name, version) VALUES ('asstokens', 1);`
|
||||||
|
);
|
||||||
|
log.success('PostgreSQL', 'ok');
|
||||||
|
}
|
||||||
|
|
||||||
|
log.success('PostgreSQL', 'database is ok').callback(() => {
|
||||||
|
resolve();
|
||||||
|
});
|
||||||
|
} catch (err) {
|
||||||
|
log.error('PostgreSQL', 'failed to set up');
|
||||||
|
console.error(err);
|
||||||
|
reject(err);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public put(table: DatabaseTable, key: string, data: DatabaseValue): Promise<void> {
|
||||||
|
return new Promise(async (resolve, reject) => {
|
||||||
|
try {
|
||||||
|
const queries = {
|
||||||
|
assfiles: 'INSERT INTO assfiles (id, data) VALUES ($1, $2);',
|
||||||
|
assusers: 'INSERT INTO assusers (id, data) VALUES ($1, $2);',
|
||||||
|
asstokens: 'INSERT INTO asstokens (id, data) VALUES ($1, $2);'
|
||||||
|
};
|
||||||
|
|
||||||
|
let result = await this._client.query(queries[table], [key, data]);
|
||||||
|
|
||||||
|
resolve();
|
||||||
|
} catch (err) {
|
||||||
|
reject(err);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public get(table: DatabaseTable, key: string): Promise<DatabaseValue | undefined> {
|
||||||
|
return new Promise(async (resolve, reject) => {
|
||||||
|
try {
|
||||||
|
const queries = {
|
||||||
|
assfiles: 'SELECT data FROM assfiles WHERE id = $1::text;',
|
||||||
|
assusers: 'SELECT data FROM assusers WHERE id = $1::text;',
|
||||||
|
asstokens: 'SELECT data FROM asstokens WHERE id = $1::text;'
|
||||||
|
};
|
||||||
|
|
||||||
|
let result = await this._client.query(queries[table], [key]);
|
||||||
|
|
||||||
|
resolve(result.rowCount ? result.rows[0].data : void 0);
|
||||||
|
} catch (err) {
|
||||||
|
reject(err);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public getAll(table: DatabaseTable): Promise<{ [index: string]: DatabaseValue; }> {
|
||||||
|
return new Promise(async (resolve, reject) => {
|
||||||
|
try {
|
||||||
|
const queries = {
|
||||||
|
assfiles: 'SELECT json_object_agg(id, data) AS stuff FROM assfiles;',
|
||||||
|
assusers: 'SELECT json_object_agg(id, data) AS stuff FROM assusers;',
|
||||||
|
asstokens: 'SELECT json_object_agg(id, data) AS stuff FROM asstokens;'
|
||||||
|
};
|
||||||
|
|
||||||
|
let result = await this._client.query(queries[table]);
|
||||||
|
|
||||||
|
resolve(result.rowCount ? result.rows[0].stuff : void 0);
|
||||||
|
} catch (err) {
|
||||||
|
reject(err);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue