diff --git a/bin/db-converter/.eslintrc b/bin/db-converter/.eslintrc new file mode 100644 index 0000000..c3ef213 --- /dev/null +++ b/bin/db-converter/.eslintrc @@ -0,0 +1,14 @@ +{ + "extends": [ + "codex" + ], + "env": { + "es2022": true + }, + "parser": "@babel/eslint-parser", + "parserOptions": { + "requireConfigFile": false, + "sourceType": "module", + "allowImportExportEverywhere": true + } +} diff --git a/bin/db-converter/index.js b/bin/db-converter/index.js index 86aab91..6a44fc0 100644 --- a/bin/db-converter/index.js +++ b/bin/db-converter/index.js @@ -1,55 +1,60 @@ -import './program.js' -import {ObjectId} from "mongodb"; -import {closeConnection, getFromLocalDB, saveData} from "./lib.js"; +import './program.js'; +import { ObjectId } from 'mongodb'; +import { closeConnection, getFromLocalDB, saveData } from './lib.js'; -console.log('Start converting...') -const [pages, aliases, files, pagesOrder] = ['pages', 'aliases', 'files', 'pagesOrder'].map(getFromLocalDB) +console.log('Start converting...'); +const [pages, aliases, files, pagesOrder] = ['pages', 'aliases', 'files', 'pagesOrder'].map(getFromLocalDB); const pagesIdsMap = pages.reduce((acc, curr) => { const newId = new ObjectId(); - acc.set(curr._id, newId) - return acc -}, new Map()) -// Explicitly set the root page id -pagesIdsMap.set('0', '0') + acc.set(curr._id, newId); + + return acc; +}, new Map()); + +// Explicitly set the root page id +pagesIdsMap.set('0', '0'); const newPages = pages.map(page => { return { ...page, _id: pagesIdsMap.get(page._id), - parent: page.parent ? pagesIdsMap.get(page.parent) : null - } -}) -await saveData('pages', newPages) + parent: page.parent ? pagesIdsMap.get(page.parent) : null, + }; +}); + +await saveData('pages', newPages); const newAliases = aliases.map(alias => { return { ...alias, _id: new ObjectId(), - id: pagesIdsMap.get(alias.id) - } -}) -await saveData('aliases', newAliases) + id: pagesIdsMap.get(alias.id), + }; +}); + +await saveData('aliases', newAliases); const newFiles = files.map(file => { return { ...file, _id: new ObjectId(), - } -}) -await saveData('files', newFiles) + }; +}); +await saveData('files', newFiles); const newPagesOrder = pagesOrder.map(pageOrder => { return { ...pageOrder, _id: new ObjectId(), page: pagesIdsMap.get(pageOrder.page), - order: pageOrder.order.map(page => pagesIdsMap.get(page)) - } -}) -await saveData('pagesOrder', newPagesOrder) + order: pageOrder.order.map(page => pagesIdsMap.get(page)), + }; +}); -await closeConnection() -console.log('Done!') +await saveData('pagesOrder', newPagesOrder); + +await closeConnection(); +console.log('Done!'); diff --git a/bin/db-converter/lib.js b/bin/db-converter/lib.js index 20df5c3..d08550e 100644 --- a/bin/db-converter/lib.js +++ b/bin/db-converter/lib.js @@ -1,29 +1,48 @@ -import fs from "fs"; -import path from "path"; -import {MongoClient} from "mongodb"; -import {options} from "./program.js"; +import fs from 'fs'; +import path from 'path'; +import { MongoClient } from 'mongodb'; +import { options } from './program.js'; const mongoClient = await MongoClient.connect(options.mongodbUri); const db = mongoClient.db(); +/** + * Returns data from local database as JSON object + * + * @param {string} filename - name of the file to read + * @returns {object} - JSON data + */ export function getFromLocalDB(filename) { - const filePath = path.resolve(process.cwd(), `${options.dbPath}/${filename}.db`) - let rawData = fs.readFileSync(filePath); + const filePath = path.resolve(process.cwd(), `${options.dbPath}/${filename}.db`); + const rawData = fs.readFileSync(filePath); - let convertedData = String(rawData) + const convertedData = String(rawData) .replace(/\n/gi, ',') .slice(0, -1); return JSON.parse(`[${convertedData}]`); } +/** + * Saves data to MongoDB + * + * @param {string} collectionName - collection to which data will be saved + * @param {object[]} data - data to save + * @returns {Promise} + */ export async function saveData(collectionName, data) { console.log(`Saving ${data.length} items to ${collectionName}...`); const collection = db.collection(collectionName); - await collection.deleteMany({}) - await collection.insertMany(data) + + await collection.deleteMany({}); + await collection.insertMany(data); console.log(`Saved ${data.length} items to ${collectionName}`); } +/** + * Closes connection to MongoDB + * + * @returns {Promise} + */ export async function closeConnection() { - await mongoClient.close() + await mongoClient.close(); } diff --git a/bin/db-converter/program.js b/bin/db-converter/program.js index 13c58e2..f523de2 100644 --- a/bin/db-converter/program.js +++ b/bin/db-converter/program.js @@ -1,4 +1,4 @@ -import { Command } from "commander"; +import { Command } from 'commander'; const program = new Command(); @@ -7,8 +7,8 @@ program .description('Converts data from local database to MongoDB') .option('--db-path ', 'Path to the local database', './db') .option('--mongodb-uri ', 'URI to the MongoDB database', 'mongodb://localhost:27017/docs') - .parse() + .parse(); const options = program.opts(); -export {options} +export { options }; diff --git a/package.json b/package.json index 0376ce9..6f4aef2 100644 --- a/package.json +++ b/package.json @@ -47,7 +47,8 @@ "zod": "^3.19.1" }, "devDependencies": { - "@babel/core": "^7.17.5", + "@babel/core": "^7.19.3", + "@babel/eslint-parser": "^7.19.1", "@babel/plugin-syntax-dynamic-import": "^7.0.0", "@babel/polyfill": "^7.12.1", "@babel/preset-env": "^7.16.11", diff --git a/src/backend/database/index.ts b/src/backend/database/index.ts index c6ebdf1..2e188e6 100644 --- a/src/backend/database/index.ts +++ b/src/backend/database/index.ts @@ -12,6 +12,7 @@ const Database = appConfig.database.driver === 'mongodb' ? MongoDatabaseDriver : /** * Convert a string to an EntityId (string or ObjectId depending on the database driver) + * * @param id - id to convert */ export function toEntityId(id: string): EntityId { @@ -20,6 +21,7 @@ export function toEntityId(id: string): EntityId { /** * Check if provided ids are equal + * * @param id1 - first id * @param id2 - second id */ @@ -29,6 +31,7 @@ export function isEqualIds(id1?: EntityId, id2?: EntityId): boolean { /** * Check if provided ids are valid + * * @param id - id to check */ export function isEntityId(id?: EntityId): id is EntityId { diff --git a/src/backend/database/local.ts b/src/backend/database/local.ts index 99d879b..bf84aca 100644 --- a/src/backend/database/local.ts +++ b/src/backend/database/local.ts @@ -37,7 +37,6 @@ export interface RejectFunction { } - /** * Simple decorator class to work with nedb datastore */ diff --git a/src/backend/database/mongodb.ts b/src/backend/database/mongodb.ts index b0b4041..0427428 100644 --- a/src/backend/database/mongodb.ts +++ b/src/backend/database/mongodb.ts @@ -21,6 +21,7 @@ export default class MongoDatabaseDriver implements DatabaseDriver