Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
jajbshjahavahh
GitHub Repository: jajbshjahavahh/Gojo-Satoru
Path: blob/master/lib/mongoDB.js
2591 views
1
//═══════════════════════════════════════════════════════//
2
//If you want to recode, reupload
3
//or copy the codes/script,
4
//pls give credit
5
//no credit? i will take action immediately
6
//© 2022 Xeon Bot Inc. Cheems Bot MD
7
//Thank you to Lord Buddha, Family and Myself
8
//════════════════════════════//
9
const mongoose = require('mongoose')
10
const { Schema } = mongoose
11
12
module.exports = class mongoDB {
13
constructor(url, options = { useNewUrlParser: true, useUnifiedTopology: true }) {
14
this.url = url
15
this.data = this._data = this._schema = this._model = {}
16
this.db
17
this.options = options
18
}
19
async read() {
20
this.db = await mongoose.connect(this.url, { ...this.options })
21
this.connection = mongoose.connection
22
let schema = this._schema = new Schema({
23
data: {
24
type: Object,
25
required: true, //depends on whether the field is mandatory or not
26
default: {}
27
}
28
})
29
// this._model = mongoose.model('data', schema)
30
try { this._model = mongoose.model('data', schema) } catch { this._model = mongoose.model('data') }
31
this._data = await this._model.findOne({})
32
if (!this._data) {
33
this.data = {}
34
await this.write(this.data)
35
this._data = await this._model.findOne({})
36
} else this.data = this._data.data
37
return this.data
38
}
39
40
41
async write(data) {
42
if (!data) return data
43
if (!this._data) return (new this._model({ data })).save()
44
this._model.findById(this._data._id, (err, docs) => {
45
if (!err) {
46
if (!docs.data) docs.data = {}
47
docs.data = data
48
return docs.save()
49
}
50
})
51
}
52
}
53