informa-db.js
Advanced tools
Comparing version 1.0.3 to 1.0.5
@@ -19,4 +19,5 @@ module.exports = { | ||
"no-console": "off", | ||
"no-param-reassign": "off", | ||
"no-underscore-dangle": "off", | ||
}, | ||
}; |
50
index.js
const fs = require('fs'); | ||
let MongoClient; | ||
let mongo; | ||
@@ -8,4 +9,3 @@ class Db { | ||
this.genProxy = (data) => new Proxy(data, { | ||
set: (pObj, prop, val) => { | ||
const obj = pObj; | ||
set: (obj, prop, val) => { | ||
obj[prop] = val; | ||
@@ -30,6 +30,7 @@ if (this.saveOnChange) { | ||
get: (obj, prop) => (typeof obj[prop] === 'object' || Array.isArray(obj[prop]) | ||
? this.genProxy(obj[prop].map((val) => { const v = val; delete v._id; return v; })) | ||
? this.genProxy(obj[prop]) | ||
: obj[prop]), | ||
}); | ||
if (!file) throw new Error('No path provided'); | ||
if (typeof file !== 'string') throw new Error('Provided path is not a string'); | ||
const dis = this; | ||
@@ -40,3 +41,4 @@ return (async () => { | ||
if (dis.isMongo) { | ||
MongoClient = (await import('mongodb')).MongoClient; | ||
mongo = (await import('mongodb')).default; | ||
MongoClient = mongo.MongoClient; | ||
dis.client = await MongoClient.connect(file, { | ||
@@ -47,9 +49,23 @@ useNewUrlParser: true, | ||
if (!db) { | ||
if (!(await dis.client.db().admin().listDatabases()).databases.some((v) => v.name === 'infodbs')) { | ||
throw new Error('\'infodbs\' is not a valid db.'); | ||
} | ||
dis.collection = dis.client.db('infodbs'); | ||
} else { | ||
if ( | ||
!(await dis.client.db().admin().listDatabases()).databases.some((v) => v.name === db) | ||
) { | ||
throw new Error(`'${db}' is not a valid db.`); | ||
} | ||
dis.collection = dis.client.db(db); | ||
} | ||
if (!collection) { | ||
if (!(await (await dis.client.db(db || 'infodbs').listCollections()).toArray()).some((v) => v.name === 'db')) { | ||
throw new Error('\'db\' is not a valid collection.'); | ||
} | ||
dis.collection = dis.collection.collection('db'); | ||
} else { | ||
if (!(await (await dis.client.db(db || 'infodbs').listCollections()).toArray()).some((v) => v.name === collection)) { | ||
throw new Error(`'${collection}' is not a valid collection.`); | ||
} | ||
dis.collection = dis.collection.collection(collection); | ||
@@ -65,3 +81,3 @@ } | ||
process.on('exit', dis.client.close); | ||
dis.value = dis.genProxy(dis.readOnlyValue); | ||
// dis.value = dis.genProxy(dis.readOnlyValue); | ||
} else { | ||
@@ -76,3 +92,3 @@ if (!fs.existsSync(file)) { | ||
dis.readOnlyValue = JSON.parse(fs.readFileSync(file, 'utf8')); | ||
dis.value = dis.genProxy(dis.readOnlyValue); | ||
// dis.value = dis.genProxy(dis.readOnlyValue); | ||
} | ||
@@ -96,5 +112,6 @@ dis.saveOnChange = true; | ||
} | ||
this.collection.drop(); | ||
this.rawContent.forEach(async (val) => { | ||
await this.collection.deleteOne({ _id: new mongo.ObjectID(val._id) }); | ||
}); | ||
if (this.readOnlyValue.length > 0) this.collection.insertMany(this.readOnlyValue); | ||
console.log(this.readOnlyValue, 'queue aire tit'); | ||
this.rawContent = JSON.parse(JSON.stringify(await this.collection.find({}).toArray())); | ||
@@ -106,5 +123,3 @@ this.readOnlyValue = JSON.parse( | ||
).map((val) => { const v = val; delete v._id; return v; }); | ||
console.log(this.readOnlyValue, 'ytreza'); | ||
this.value = this.genProxy(this.readOnlyValue); | ||
console.log('azerty'); | ||
// this.value = this.genProxy(this.readOnlyValue); | ||
return this.readOnlyValue; | ||
@@ -137,2 +152,15 @@ } | ||
} | ||
set value(setTo) { | ||
this.readOnlyValue = setTo; | ||
if (this.saveOnChange) { | ||
this.update(); | ||
} | ||
return true; | ||
} | ||
get value() { | ||
this.readOnlyValue = this.readOnlyValue.map((v) => { const r = v; delete r._id; return r; }); | ||
return this.genProxy(this.readOnlyValue); | ||
} | ||
} | ||
@@ -139,0 +167,0 @@ module.exports = Db; |
{ | ||
"name": "informa-db.js", | ||
"version": "1.0.3", | ||
"description": "", | ||
"version": "1.0.5", | ||
"description": "DataBases made easier", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "node test.js" | ||
}, | ||
"author": "InforMatheMusic", | ||
"license": "ISC", | ||
"repository": "https://github.com/informathemusic/informa-db.js", | ||
"devDependencies": { | ||
@@ -19,3 +17,6 @@ "eslint": "^6.8.0", | ||
"eslint-plugin-react-hooks": "^2.5.0" | ||
}, | ||
"dependencies": { | ||
"mongodb": "^3.5.8" | ||
} | ||
} |
@@ -40,3 +40,3 @@ # Informa-Db.js | ||
Path to file or URI to mongodb server. | ||
Will throw an error if none provided | ||
Will throw an error if none provided or if type is incorrect | ||
#### defaultString | ||
@@ -49,2 +49,4 @@ Default string to write on file if it doesn't exist. | ||
Defaults to true if the path starts with "mongodb", false otherwise. | ||
Notice: If you need to interact with a mongodb server, you need to install mongodb yourself | ||
#### db<String> | ||
@@ -51,0 +53,0 @@ Database name, defaulting to "infodbs" |
@@ -5,6 +5,6 @@ const Db = require('./index.js'); | ||
const test = await new Db(process.env.SERVER); | ||
const ee = test.value.length; | ||
test.value[ee] = { ee: 'ee' }; | ||
delete test.value[ee]; | ||
// const ee = test.value.length; | ||
// test.value = []; | ||
test.value[test.value.length] = { ee: 'eeeeee' }; | ||
console.log(test.value); | ||
})(); |
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
8624
181
71
1
+ Addedmongodb@^3.5.8
+ Addedbl@2.2.1(transitive)
+ Addedbson@1.1.6(transitive)
+ Addedcore-util-is@1.0.3(transitive)
+ Addeddenque@1.5.1(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedisarray@1.0.0(transitive)
+ Addedmemory-pager@1.5.0(transitive)
+ Addedmongodb@3.7.4(transitive)
+ Addedoptional-require@1.1.8(transitive)
+ Addedprocess-nextick-args@2.0.1(transitive)
+ Addedreadable-stream@2.3.8(transitive)
+ Addedrequire-at@1.0.6(transitive)
+ Addedsafe-buffer@5.1.25.2.1(transitive)
+ Addedsaslprep@1.0.3(transitive)
+ Addedsparse-bitfield@3.0.3(transitive)
+ Addedstring_decoder@1.1.1(transitive)
+ Addedutil-deprecate@1.0.2(transitive)