multiple.db
Advanced tools
+59
-20
@@ -5,5 +5,24 @@ const mongoose = require('mongoose'); | ||
| let storageType = 'json'; | ||
| let storageType = 'json'; | ||
| if (!fs.existsSync(`data.json`)) { | ||
| fs.writeFileSync(`data.json`, "{}"); | ||
| } | ||
| const dataPath = path.resolve(__dirname, 'data.json'); | ||
| const dataPath = path.resolve(__dirname, 'superbase.json'); | ||
| let data = {}; | ||
| let dataChanged = false; | ||
| function readData() { | ||
| try { | ||
| data = JSON.parse(fs.readFileSync(dataPath).toString()); | ||
| } catch (error) { | ||
| data = {}; | ||
| } | ||
| } | ||
| function writeData() { | ||
| fs.writeFileSync(dataPath, JSON.stringify(data)); | ||
| } | ||
| const dataSchema = new mongoose.Schema({ | ||
@@ -32,4 +51,3 @@ key: String, | ||
| if (storageType === 'json') { | ||
| const data = JSON.parse(fs.readFileSync(dataPath)); | ||
| if (data[key]) { | ||
| if (data[key]) { | ||
| data[key] += value; | ||
@@ -39,3 +57,3 @@ } else { | ||
| } | ||
| fs.writeFileSync(dataPath, JSON.stringify(data)) | ||
| dataChanged = true; | ||
| } else { | ||
@@ -54,4 +72,3 @@ const doc = await Data.findOne({ key }).exec(); | ||
| if (storageType === 'json') { | ||
| const data = JSON.parse(fs.readFileSync(dataPath)); | ||
| if (data[key]) { | ||
| if (data[key]) { | ||
| data[key] -= value; | ||
@@ -61,3 +78,3 @@ } else { | ||
| } | ||
| fs.writeFileSync(dataPath, JSON.stringify(data)); | ||
| dataChanged = true; | ||
| } else { | ||
@@ -76,5 +93,4 @@ const doc = await Data.findOne({ key }).exec(); | ||
| if (storageType === 'json') { | ||
| const data = JSON.parse(fs.readFileSync(dataPath)) | ||
| data[key] = value; | ||
| fs.writeFileSync(dataPath, JSON.stringify(data)) | ||
| data[key] = value; | ||
| dataChanged = true; | ||
| } else { | ||
@@ -85,8 +101,19 @@ const doc = await Data.findOneAndUpdate({ key }, { value }, { upsert: true, new: true }).exec(); | ||
| } | ||
| async function get(key) { | ||
| if (storageType === 'json') { | ||
| return data[key]; | ||
| } else { | ||
| const doc = await Data.findOne({ key }).exec(); | ||
| return doc ? doc.value : undefined; | ||
| } | ||
| } | ||
| async function trash(key) { | ||
| if (storageType === 'json') { | ||
| const data = JSON.parse(fs.readFileSync(dataPath)) | ||
| const value = data[key]; | ||
| delete data[key]; | ||
| fs.writeFileSync(dataPath, JSON.stringify(data)) | ||
| dataChanged = true; | ||
| return value; | ||
| } else { | ||
@@ -99,4 +126,3 @@ const doc = await Data.findOneAndDelete({ key }).exec(); | ||
| if (storageType === 'json') { | ||
| const data = JSON.parse(fs.readFileSync(dataPath)); | ||
| return data.hasOwnProperty(key); | ||
| return data.hasOwnProperty(key); | ||
| } else { | ||
@@ -109,4 +135,3 @@ const doc = await Data.findOne({ key }).exec(); | ||
| if (storageType === 'json') { | ||
| const data = JSON.parse(fs.readFileSync(dataPath)); | ||
| return Object.entries(data).map(([key, value]) => ({ key, value })); | ||
| return Object.entries(data).map(([key, value]) => ({ key, value })); | ||
| } else { | ||
@@ -119,4 +144,3 @@ const docs = await Data.find().exec(); | ||
| if (storageType === 'json') { | ||
| const data = JSON.parse(fs.readFileSync(dataPath)); | ||
| return data[key]; | ||
| return data[key]; | ||
| } else { | ||
@@ -129,3 +153,4 @@ const doc = await Data.findOne({ key }).exec(); | ||
| if (storageType === 'json') { | ||
| fs.writeFileSync(dataPath, '{}'); | ||
| data = {}; | ||
| dataChanged = true; | ||
| } else { | ||
@@ -135,2 +160,15 @@ await Data.deleteMany({}); | ||
| } | ||
| function save() { | ||
| if (dataChanged) { | ||
| writeData(); | ||
| dataChanged = false; | ||
| } | ||
| } | ||
| setInterval(save, 1000); | ||
| readData(); | ||
| module.exports = { | ||
@@ -143,2 +181,3 @@ connect, | ||
| set, | ||
| get, | ||
| trash, | ||
@@ -145,0 +184,0 @@ has, |
+1
-1
| { | ||
| "name": "multiple.db", | ||
| "version": "0.0.1", | ||
| "version": "0.0.2-beta", | ||
| "description": "", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
12458
1.7%159
21.37%