From 488f5c15e3c08a852acff0c49ce990ba52f37df9 Mon Sep 17 00:00:00 2001 From: Josh Moore Date: Sun, 16 Jul 2023 21:00:50 -0600 Subject: [PATCH] feat: half-implement useSql in data.ts --- backend/data.ts | 45 +++++++++++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/backend/data.ts b/backend/data.ts index 817c20d..f4c76d9 100644 --- a/backend/data.ts +++ b/backend/data.ts @@ -4,6 +4,7 @@ import { nanoid } from 'nanoid'; import { log } from './log'; import { AssFile, AssUser, NID, FilesSchema, UsersSchema } from 'ass'; import { UserConfig } from './UserConfig'; +import { MySql } from './sql/mysql'; /** * Switcher type for exported functions @@ -104,40 +105,48 @@ export const setDataModeToSql = (): Promise => new Promise(async (resolve, export const put = (sector: DataSector, key: NID, data: AssFile | AssUser): Promise => new Promise(async (resolve, reject) => { try { - const useSql = false; + const useSql = MySql.ready; if (sector === 'files') { data = data as AssFile; // * 1: Save as files (image, video, etc) - 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`)); + // ? Local JSON + if (!useSql) { + const filesJson = await fs.readJson(PATHS.files) as FilesSchema; - // Otherwise add the data - filesJson.files[key] = data; + // Check if key already exists + if (filesJson.files[key] != null) return reject(new Error(`File key ${key} already exists`)); - // 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); + // Otherwise add the data + filesJson.files[key] = data; - // Save the files - await bothWriter(filesJson, usersJson); + // 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 { data = data as AssUser; // * 2: Save as users - 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`)); + // ? Local JSON + if (!useSql) { + 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; + // Otherwise add the data + usersJson.users[key] = data; - await fs.writeJson(PATHS.users, usersJson, { spaces: '\t' }); + await fs.writeJson(PATHS.users, usersJson, { spaces: '\t' }); + } } log.info(`PUT data (${useSql ? 'SQL' : 'local JSON'})`, `${key}`, `${sector} sector`);