informa-db.js
Advanced tools
Comparing version 1.0.8 to 1.0.9
182
index.js
const fs = require("fs"); | ||
const utils = require("./utils"); | ||
<<<<<<< HEAD | ||
let mongo; | ||
@@ -16,55 +15,6 @@ let MongoClient; | ||
constructor(settings) { | ||
this.saveOnChange = false; | ||
======= | ||
const { mongo, MongoClient } = utils.tryMongo(); | ||
class BaseDb { | ||
constructor(settings) {} | ||
/** | ||
* Checks if this.readOnlyValue[index] exists | ||
* @param {number} index - the index in the dataBase/jsonfile | ||
*/ | ||
exist(index) { | ||
return !!this.readOnlyValue[index]; | ||
this.saveOnChange = settings.soc; | ||
if(this.saveOnChange==null) | ||
this.saveOnChange = true | ||
} | ||
/** | ||
* Defines this.readOnlyValue[index] to value. | ||
* If this.readOnlyValue[index] already exists, will throw an error | ||
* @param {number} index - index in the dataBase/jsonfile | ||
* @param {any} newValue - the new value | ||
*/ | ||
addSafe(index, value) { | ||
if (this.exist(index)) { | ||
throw console.error(`this.readOnlyValue[${index}] already exists`); | ||
} | ||
if (this.saveOnChange) { | ||
this.update()[index]; | ||
} | ||
} | ||
/** | ||
* Splices out/deletes this.readOnlyValue[index] | ||
* @param {number} index - the index in the dataBase/jsonfile | ||
*/ | ||
remove(index) { | ||
this.readOnlyValue.splice(index, 1); | ||
return undefined; | ||
} | ||
/** | ||
* Defines this.readOnlyValue[index] to value. | ||
* @param {number} index - index in the dataBase/jsonfile | ||
* @param {any} newValue - the new value | ||
*/ | ||
add(index, newValue) { | ||
this.readOnlyValue[index] = newValue; | ||
if (this.saveOnChange) { | ||
return this.update(this.readOnlyValue)[index]; | ||
} | ||
return newValue; | ||
>>>>>>> d72665bb7ce05e23b596390d36e15652e70fae98 | ||
} | ||
genProxy(data) { | ||
@@ -80,3 +30,2 @@ return new Proxy(data, { | ||
deleteProperty: (pObj, prop) => { | ||
<<<<<<< HEAD | ||
const obj = pObj; | ||
@@ -88,7 +37,2 @@ try { | ||
} | ||
======= | ||
let obj = pObj; | ||
obj = utils.spliceOrDelete(obj, prop); | ||
>>>>>>> d72665bb7ce05e23b596390d36e15652e70fae98 | ||
if (this.saveOnChange) { | ||
@@ -99,13 +43,7 @@ this.update(); | ||
}, | ||
<<<<<<< HEAD | ||
get: (obj, prop) => (typeof obj[prop] === 'object' || Array.isArray(obj[prop]) ? | ||
this.genProxy(obj[prop]) : | ||
obj[prop]), | ||
======= | ||
get: (obj, prop) => | ||
typeof obj[prop] === "object" || Array.isArray(obj[prop]) | ||
typeof obj[prop] === "object" && obj[prop] | ||
? this.genProxy(obj[prop]) | ||
: obj[prop], | ||
>>>>>>> d72665bb7ce05e23b596390d36e15652e70fae98 | ||
}); | ||
@@ -120,22 +58,2 @@ } | ||
super(); | ||
<<<<<<< HEAD | ||
//Extends is a pai | ||
const { | ||
path, | ||
defaultStr | ||
} = settings; | ||
if (!path) throw new Error('No path provided'); | ||
if (typeof path !== 'string') throw new Error('Provided path is not a string'); | ||
this.path = path; | ||
if (!fs.existsSync(path)) { | ||
fs.writeFileSync(path, defaultStr || '{}', (err) => { | ||
if (err) { | ||
throw err; | ||
} | ||
}); | ||
} | ||
this.readOnlyValue = JSON.parse( | ||
fs.readFileSync(path, 'utf8') | ||
); | ||
======= | ||
//Extends is a pain | ||
@@ -161,3 +79,2 @@ const { path, defaultStr } = settings; | ||
})(); | ||
>>>>>>> d72665bb7ce05e23b596390d36e15652e70fae98 | ||
} | ||
@@ -168,9 +85,4 @@ /** | ||
*/ | ||
<<<<<<< HEAD | ||
update() { | ||
fs.writeFileSync(this.path, JSON.stringify(this.readOnlyValue, null, '\t')); | ||
======= | ||
async update() { | ||
fs.writeFileSync(this.path, JSON.stringify(this.readOnlyValue, null, "\t")); | ||
>>>>>>> d72665bb7ce05e23b596390d36e15652e70fae98 | ||
return this.readOnlyValue; | ||
@@ -194,12 +106,2 @@ } | ||
constructor(settings) { | ||
<<<<<<< HEAD | ||
super() | ||
const { | ||
path, | ||
db: dbProp, | ||
collection: collectionProp | ||
} = settings; | ||
if (!path) throw new Error('No path provided'); | ||
if (typeof path !== 'string') throw new Error('Provided path is not a string'); | ||
======= | ||
super(); | ||
@@ -211,15 +113,9 @@ const { url, db, collection } = settings; | ||
throw new Error("Provided path is not a string"); | ||
>>>>>>> d72665bb7ce05e23b596390d36e15652e70fae98 | ||
const dis = this; | ||
return (async () => { | ||
<<<<<<< HEAD | ||
dis.path = path; | ||
======= | ||
dis.path = url; | ||
>>>>>>> d72665bb7ce05e23b596390d36e15652e70fae98 | ||
if (!mongo) { | ||
throw new Error("Mongodb is not installed. Please install it."); | ||
} | ||
<<<<<<< HEAD | ||
dis.client = await MongoClient.connect(path, { | ||
@@ -256,62 +152,2 @@ useNewUrlParser: true, | ||
setInterval() | ||
======= | ||
dis.client = await MongoClient.connect(url, { | ||
useNewUrlParser: true, | ||
useUnifiedTopology: true, | ||
}); | ||
let db = dbProp || "informadb-dbs"; | ||
let collection = collectionProp || "db"; | ||
const disListDatabase = dis.client.db().admin().listDatabases(); | ||
if (!db) { | ||
if ( | ||
!(await dislistdatabase).databases.some((v) => v.name === "infodbs") | ||
) { | ||
throw new Error("'infodbs' is not a valid db."); | ||
} | ||
dis.collection = dis.client.db("infodbs"); | ||
} else { | ||
if (!(await dislistdatabase).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); | ||
} | ||
dis.readOnlyValue = utils.jsonContentWithFind(this.collection, {}); | ||
dis.rawContent = utils.jsonContent(dis.readOnlyValue); | ||
dis.readOnlyValue = dis.readOnlyValue.map(utils.deleteId); | ||
process.on("exit", dis.client.close); | ||
dis.saveOnChange = true; | ||
>>>>>>> d72665bb7ce05e23b596390d36e15652e70fae98 | ||
return dis; | ||
@@ -330,3 +166,2 @@ })(); | ||
}); | ||
<<<<<<< HEAD | ||
if (this.readOnlyValue.length > 0) this.collection.insertMany(this.readOnlyValue); | ||
@@ -347,11 +182,2 @@ this.rawContent = JSON.parse( | ||
}); | ||
======= | ||
if (this.readOnlyValue.length > 0) | ||
this.collection.insertMany(this.readOnlyValue); | ||
this.rawContent = utils.jsonContentWithFind(this.collection, {}); | ||
this.readOnlyValue = utils.jsonContent(this.rawContent).map(utils.deleteId); | ||
>>>>>>> d72665bb7ce05e23b596390d36e15652e70fae98 | ||
return this.readOnlyValue; | ||
@@ -358,0 +184,0 @@ } |
{ | ||
"name": "informa-db.js", | ||
"version": "1.0.8", | ||
"version": "1.0.9", | ||
"description": "DataBases made easier", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13447
353