1
0
Fork 0
gallery-dl-archive-manager/lib/schema.js
2024-02-28 18:46:21 -05:00

37 lines
888 B
JavaScript

import { log } from './log.js';
const ctx = 'schema.js';
/**
* Returns valid db schema
*/
export const dbSchema = (userList) => {
return {
version: '1.0.0',
userList,
}
}
/**
* Returns valid user schema
*/
export const userSchema = (entry) => {
return {
lastError: entry.lastError,
lastUpdated: entry.lastUpdated,
pastUsernames: entry.pastUsernames,
}
}
/**
* Creates db at the specified directory
* @param {string} directory the directory the db.json file will be stored in
*/
export const createDb = async (directory) => {
log(ctx, 'Grabbing existing directories');
const children = await getChildDirectories(directory);
const db = dbSchema(Object.fromEntries(children.map(e => [e, userSchema({})])));
log(ctx, 'Writing database');
await writeFile(`${directory}/db.json`, JSON.stringify(db, null, 2));
log(ctx, 'Writing complete!');
}