mirror of
https://github.com/codex-team/codex.docs.git
synced 2025-07-19 13:19:42 +02:00
🤩MongoDB support 🤩 (#272)
* implement configuration through YAML * remove rcparser * use password from appConfig * update docker configs * fix dockerignore * implement mongodb driver * update eslint packages * fix bugs * refactor code for grouping by parent * fix yet another bug * use unique symbol to the EntityId type * fix more bugs * implement db converter * fix bug with parent selector * fix eslint * db-converter refactoring * create cli program for db-converter * add readme and gitignore * update development docs * update development docs and default config * add docs about converter * add src/test to docker ignore * move database code from utils * improve docs * eslint fix * add more docs * fix docs * remove env_file from docker-compose * implement duplicate detection in db-converter * use published version of the config-loader * fix bug * Update DEVELOPMENT.md Co-authored-by: Ilya Maroz <37909603+ilyamore88@users.noreply.github.com> * fix bugs * fix next/prev buttons * fix more bugs * fix sorting Co-authored-by: Ilya Maroz <37909603+ilyamore88@users.noreply.github.com>
This commit is contained in:
parent
13762096c4
commit
55b4b3ee61
72 changed files with 12614 additions and 665 deletions
48
bin/db-converter/lib.js
Normal file
48
bin/db-converter/lib.js
Normal file
|
@ -0,0 +1,48 @@
|
|||
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`);
|
||||
const rawData = fs.readFileSync(filePath);
|
||||
|
||||
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<void>}
|
||||
*/
|
||||
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);
|
||||
console.log(`Saved ${data.length} items to ${collectionName}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes connection to MongoDB
|
||||
*
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
export async function closeConnection() {
|
||||
await mongoClient.close();
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue