File size: 1,428 Bytes
a2b2aac | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | import Helper from './helper.js'
import { Low, JSONFile } from 'lowdb'
import { cloudDBAdapter, mongoDB, mongoDBV2 } from './DB_Adapters/index.js'
import lodash from 'lodash'
const databaseUrl = Helper.opts['db'] || ''
const databaseAdapter = /https?:\/\//.test(databaseUrl) ?
new cloudDBAdapter(databaseUrl) : /mongodb(\+srv)?:\/\//i.test(databaseUrl) ?
(Helper.opts['mongodbv2'] ? new mongoDBV2(databaseUrl) :
new mongoDB(databaseUrl)) :
new JSONFile(`${Helper.opts._[0] ? Helper.opts._[0] + '_' : ''}database.json`)
/** @typedef {{ [Key: string]: {[Key: string]: any } }} DatabaseData */
let database = /** @type {Low<DatabaseData> & { chain: import('lodash').ObjectChain<DatabaseData>, _read: Promise<void> | void }} */
(new Low(databaseAdapter))
loadDatabase()
async function loadDatabase() {
// If database is processed to be loaded from cloud, wait for it to be done
if (database._read) await database._read
if (database.data !== null) return database.data
database._read = database.read().catch(console.error)
await database._read
console.log('- Database loaded -')
database.data = {
users: {},
chats: {},
stats: {},
msgs: {},
sticker: {},
settings: {},
...(database.data || {})
}
database.chain = lodash.chain(database.data)
return database.data
}
export {
databaseUrl,
databaseAdapter,
database,
loadDatabase
}
export default database
|