split json database out of data.ts

pull/245/head
xwashere 11 months ago
parent 5620d0bed3
commit ce3ad10281
No known key found for this signature in database
GPG Key ID: 042F8BFA1B0EF93B

@ -9,7 +9,8 @@ import { path, isProd } from '@tycrek/joint';
import { epcss } from '@tycrek/express-postcss';
import { log } from './log';
import { ensureFiles, get } from './data';
import { JSONDatabase, ensureFiles } from './db/json';
import { get } from './data';
import { UserConfig } from './UserConfig';
import { MySQLDatabase } from './db/mysql';
import { buildFrontendRouter } from './routers/_frontend';
@ -113,9 +114,12 @@ async function main() {
.catch((err) => (err.code && err.code === 'ENOENT' ? {} : console.error(err), resolve(void 0))));
// If user config is ready, try to configure SQL
if (UserConfig.ready && UserConfig.config.sql?.mySql != null)
if (UserConfig.ready && UserConfig.config.sql?.mySql != null) {
try { await DBManager.use(new MySQLDatabase()); }
catch (err) { throw new Error(`Failed to configure SQL`); }
} else {
await DBManager.use(new JSONDatabase());
}
// Set up Express
const app = express();

@ -4,7 +4,6 @@ import fs from 'fs-extra';
import { path } from '@tycrek/joint';
import { log } from './log';
import { nanoid } from './generators';
import { UserConfig } from './UserConfig';
import { DBManager } from './db/database';
@ -17,8 +16,8 @@ type DataSector = 'files' | 'users';
* Absolute filepaths for JSON data files
*/
const PATHS = {
files: path.join('.ass-data/files.json'),
users: path.join('.ass-data/users.json')
files: path.join('.ass-data/files.json'),
users: path.join('.ass-data/users.json')
};
const bothWriter = async (files: FilesSchema, users: UsersSchema) => {
@ -26,55 +25,6 @@ const bothWriter = async (files: FilesSchema, users: UsersSchema) => {
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 {
// Create data directory
await fs.ensureDir(path.join('.ass-data'));
// * 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);
}
});
export const setDataModeToSql = (): Promise<void> => new Promise(async (resolve, reject) => {
log.debug('Setting data mode to SQL');
@ -107,60 +57,14 @@ export const setDataModeToSql = (): Promise<void> => new Promise(async (resolve,
export const put = (sector: DataSector, key: NID, data: AssFile | AssUser): Promise<void> => new Promise(async (resolve, reject) => {
try {
const useSql = DBManager.ready;
const useSql = UserConfig.config.sql != undefined;
if (sector === 'files') {
// * 1: Save as files (image, video, etc)
data = data as AssFile;
if (!useSql) {
// ? 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;
// 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);
} else {
// ? SQL
if (!(await DBManager.get('assfiles', key))) await DBManager.put('assfiles', key, data);
else return reject(new Error(`File key ${key} already exists`));
// todo: modify users SQL files property
}
await DBManager.put('assfiles', key, data as AssFile);
} else {
// * 2: Save as users
data = data as AssUser;
if (!useSql) {
// ? 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;
await fs.writeJson(PATHS.users, usersJson, { spaces: '\t' });
} else {
// ? SQL
if (!(await DBManager.get('assusers', key))) await DBManager.put('assusers', key, data);
else return reject(new Error(`User key ${key} already exists`));
}
await DBManager.put('assusers', key, data as AssUser);
}
log.info(`PUT ${sector} data`, `using ${useSql ? 'SQL' : 'local JSON'}`, key);
@ -172,22 +76,18 @@ export const put = (sector: DataSector, key: NID, data: AssFile | AssUser): Prom
export const get = (sector: DataSector, key: NID): Promise<AssFile | AssUser | false> => new Promise(async (resolve, reject) => {
try {
const data: AssFile | AssUser | undefined = (DBManager.ready)
? (await DBManager.get(sector === 'files' ? 'assfiles' : 'assusers', key) as AssFile | AssUser | undefined)
: (await fs.readJson(PATHS[sector]))[sector][key];
const data: AssFile | AssUser | undefined = await DBManager.get(sector === 'files' ? 'assfiles' : 'assusers', key) as AssFile | AssUser | undefined
(!data) ? resolve(false) : resolve(data);
} catch (err) {
reject(err);
}
});
export const getAll = (sector: DataSector): Promise<{ [key: string]: AssFile | AssUser } | false> => new Promise(async (resolve, reject) => {
export const getAll = (sector: DataSector): Promise<{ [key: string]: AssFile | AssUser }> => new Promise(async (resolve, reject) => {
try {
const data: { [key: string]: AssFile | AssUser } | undefined = (DBManager.ready)
// todo: fix MySQL
? (await DBManager.getAll(sector === 'files' ? 'assfiles' : 'assusers') as /* AssFile[] | AssUser[] | */ [])
: (await fs.readJson(PATHS[sector]))[sector];
(!data) ? resolve(false) : resolve(data);
// todo: fix MySQL
const data: { [key: string]: AssFile | AssUser } = await DBManager.getAll(sector === 'files' ? 'assfiles' : 'assusers') as /* AssFile[] | AssUser[] | */ {}
resolve(data);
} catch (err) {
reject(err);
}

@ -35,7 +35,7 @@ export interface Database {
/**
* get all values from the database
*/
getAll(table: DatabaseTable): Promise<DatabaseValue[]>;
getAll(table: DatabaseTable): Promise<{ [index: string]: DatabaseValue }>;
}
export class DBManager {
@ -97,7 +97,7 @@ export class DBManager {
/**
* get all values from the database
*/
public static getAll(table: DatabaseTable): Promise<DatabaseValue[]> {
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,154 @@
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 {
// Create data directory
await fs.ensureDir(path.join('.ass-data'));
// * 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
*/
export class JSONDatabase implements Database {
open(): Promise<void> { return Promise.resolve() }
close(): Promise<void> { return Promise.resolve() }
configure(): Promise<void> {
return new Promise((resolve, reject) => {
ensureFiles();
resolve();
});
}
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();
}
})
}
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);
});
}
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);
});
}
}

@ -106,6 +106,8 @@ export class MySQLDatabase implements Database {
return new Promise(async (resolve, reject) => {
if (!this._ready) return reject(new Error('MySQL not ready'));
if (await this.get(table, key)) reject(new Error(`${table == 'assfiles' ? 'File' : table == 'assusers' ? 'User' : 'Token'} key ${key} already exists`));
const query = `
INSERT INTO ${table} ( NanoID, Data )
VALUES ('${key}', '${JSON.stringify(data)}');
@ -134,7 +136,7 @@ VALUES ('${key}', '${JSON.stringify(data)}');
}
// todo: unknown if this works
public getAll(table: DatabaseTable): Promise<DatabaseValue[]> {
public getAll(table: DatabaseTable): Promise<{ [index: string]: DatabaseValue }> {
return new Promise(async (resolve, reject) => {
try {
// Run query // ! this may not work as expected
@ -146,7 +148,7 @@ VALUES ('${key}', '${JSON.stringify(data)}');
// console.log(rows);
// aaaaaaaaaaaa
resolve([]);
resolve({});
} catch (err) {
reject(err);
}

Loading…
Cancel
Save