Add data layer with MongoDB and JSON fallback store

Introduce database config, MongoDB and JSON store implementations,
and a datastore factory that connects to MongoDB by default and falls
back to a local JSON file when MongoDB is unavailable.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
francesco448
2026-03-18 16:52:34 +01:00
parent 3e3fa11951
commit e0d53d6e74
5 changed files with 455 additions and 0 deletions
+34
View File
@@ -0,0 +1,34 @@
const path = require('path')
const JsonStore = require('./stores/jsonStore')
async function createDataStore() {
const requestedStore = process.env.DATA_STORE
const forceJson = requestedStore === 'json'
const strictMongo = requestedStore === 'mongo'
if (forceJson) {
const filePath = path.join(__dirname, '..', 'data', 'db.json')
console.log(`Using JSON fallback store at ${filePath}`)
return new JsonStore(filePath)
}
try {
const createMongoStore = require('./stores/mongoStore')
return await createMongoStore()
} catch (error) {
if (strictMongo) {
throw error
}
console.warn(
'MongoDB store unavailable, falling back to JSON store.',
error.message,
)
}
const filePath = path.join(__dirname, '..', 'data', 'db.json')
console.log(`Using JSON fallback store at ${filePath}`)
return new JsonStore(filePath)
}
module.exports = createDataStore