iptv/scripts/core/db.js

83 lines
1.4 KiB
JavaScript
Raw Normal View History

2022-02-06 22:04:56 +01:00
const nedb = require('nedb-promises')
2023-06-21 12:05:34 +02:00
const fs = require('fs-extra')
2021-12-12 05:10:18 +01:00
const file = require('./file')
2023-04-30 04:16:45 +02:00
const DB_DIR = process.env.DB_DIR || './scripts/tmp/database'
2022-02-06 22:04:56 +01:00
2023-06-21 12:05:34 +02:00
fs.ensureDirSync(DB_DIR)
2022-02-06 22:04:56 +01:00
class Database {
constructor(filepath) {
this.filepath = filepath
}
load() {
this.db = nedb.create({
filename: file.resolve(this.filepath),
autoload: true,
onload: err => {
if (err) console.error(err)
},
compareStrings: (a, b) => {
a = a.replace(/\s/g, '_')
b = b.replace(/\s/g, '_')
return a.localeCompare(b, undefined, {
sensitivity: 'accent',
numeric: true
})
}
2021-12-12 05:10:18 +01:00
})
}
2022-02-06 22:04:56 +01:00
removeIndex(field) {
return this.db.removeIndex(field)
}
2021-12-12 05:10:18 +01:00
2022-02-06 22:04:56 +01:00
addIndex(options) {
return this.db.ensureIndex(options)
}
2021-12-12 05:10:18 +01:00
2022-02-06 22:04:56 +01:00
compact() {
return this.db.persistence.compactDatafile()
}
2021-12-12 05:10:18 +01:00
2022-02-06 22:04:56 +01:00
stopAutocompact() {
return this.db.persistence.stopAutocompaction()
}
2021-12-12 05:10:18 +01:00
2022-02-06 22:04:56 +01:00
reset() {
return file.clear(this.filepath)
}
2021-12-12 05:10:18 +01:00
2022-02-06 22:04:56 +01:00
count(query) {
return this.db.count(query)
}
2021-12-12 05:10:18 +01:00
2022-02-06 22:04:56 +01:00
insert(doc) {
return this.db.insert(doc)
}
2021-12-12 05:10:18 +01:00
2022-02-06 22:04:56 +01:00
update(query, update) {
return this.db.update(query, update)
}
2021-12-12 05:10:18 +01:00
2022-02-06 22:04:56 +01:00
find(query) {
return this.db.find(query)
}
2021-12-12 05:10:18 +01:00
2022-08-15 01:22:36 +02:00
all() {
return this.find({})
}
2022-02-06 22:04:56 +01:00
remove(query, options) {
return this.db.remove(query, options)
}
2021-12-12 05:10:18 +01:00
}
2022-02-06 22:04:56 +01:00
const db = {}
db.streams = new Database(`${DB_DIR}/streams.db`)
2021-12-12 05:10:18 +01:00
module.exports = db