informa-db.js
Advanced tools
Comparing version 1.0.7-beta.0 to 1.0.8
236
index.js
@@ -1,3 +0,5 @@ | ||
const fs = require('fs'); | ||
const fs = require("fs"); | ||
const utils = require("./utils"); | ||
<<<<<<< HEAD | ||
let mongo; | ||
@@ -7,3 +9,2 @@ let MongoClient; | ||
mongo = require('mongodb'); | ||
MongoClient = mongo.MongoClient; | ||
@@ -17,3 +18,7 @@ } catch (err) { | ||
this.saveOnChange = false; | ||
} | ||
======= | ||
const { mongo, MongoClient } = utils.tryMongo(); | ||
class BaseDb { | ||
constructor(settings) {} | ||
/** | ||
@@ -24,4 +29,3 @@ * Checks if this.readOnlyValue[index] exists | ||
exist(index) { | ||
return !!this.readOnlyValue[index] | ||
return !!this.readOnlyValue[index]; | ||
} | ||
@@ -35,12 +39,7 @@ /** | ||
addSafe(index, value) { | ||
if (this.exist(index)) { | ||
throw console.error(`this.readOnlyValue[${index}] already exists`); | ||
} | ||
this.readOnlyValue[index] = value; | ||
if (this.saveOnChange) { | ||
this.update()[index]; | ||
@@ -55,3 +54,2 @@ } | ||
remove(index) { | ||
this.readOnlyValue.splice(index, 1); | ||
@@ -67,3 +65,2 @@ return undefined; | ||
add(index, newValue) { | ||
this.readOnlyValue[index] = newValue; | ||
@@ -76,7 +73,6 @@ | ||
return newValue; | ||
>>>>>>> d72665bb7ce05e23b596390d36e15652e70fae98 | ||
} | ||
genProxy(data) { | ||
return new Proxy(data, { | ||
set: (obj, prop, val) => { | ||
@@ -89,6 +85,5 @@ obj[prop] = val; | ||
}, | ||
deleteProperty: (pObj, prop) => { | ||
<<<<<<< HEAD | ||
const obj = pObj; | ||
try { | ||
@@ -99,17 +94,26 @@ obj.splice(prop, 1); | ||
} | ||
======= | ||
let obj = pObj; | ||
obj = utils.spliceOrDelete(obj, prop); | ||
>>>>>>> d72665bb7ce05e23b596390d36e15652e70fae98 | ||
if (this.saveOnChange) { | ||
this.update(); | ||
} | ||
return true; | ||
}, | ||
<<<<<<< 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]) | ||
? this.genProxy(obj[prop]) | ||
: obj[prop], | ||
>>>>>>> d72665bb7ce05e23b596390d36e15652e70fae98 | ||
}); | ||
} | ||
} | ||
/** | ||
@@ -121,3 +125,4 @@ * Class represents the concept to interact with storage units (such as Dbs or JSON files) by defining a proxy property. | ||
super(); | ||
//Extends is a pain | ||
<<<<<<< HEAD | ||
//Extends is a pai | ||
const { | ||
@@ -127,26 +132,37 @@ path, | ||
} = 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 | ||
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"); | ||
const dis = this; | ||
return (() => { | ||
dis.path = path; | ||
if (!fs.existsSync(path)) { | ||
fs.writeFileSync(path, defaultStr || '{}', (err) => { | ||
if (err) { | ||
throw err; | ||
} | ||
}); | ||
fs.writeFileSync(path, defaultStr || "{}", utils.throwErrorIfError); | ||
} | ||
dis.readOnlyValue = JSON.parse( | ||
fs.readFileSync(path, 'utf8') | ||
); | ||
dis.readOnlyValue = JSON.parse(fs.readFileSync(path, "utf8")); | ||
dis.saveOnChange = true; | ||
return dis; | ||
})(); | ||
>>>>>>> d72665bb7ce05e23b596390d36e15652e70fae98 | ||
} | ||
@@ -157,7 +173,11 @@ /** | ||
*/ | ||
<<<<<<< HEAD | ||
update() { | ||
fs.writeFileSync(this.path, JSON.stringify(this.readOnlyValue, null, '\t')); | ||
======= | ||
async update() { | ||
fs.writeFileSync(this.path, JSON.stringify(this.readOnlyValue, null, '\t')); | ||
fs.writeFileSync(this.path, JSON.stringify(this.readOnlyValue, null, "\t")); | ||
>>>>>>> d72665bb7ce05e23b596390d36e15652e70fae98 | ||
return this.readOnlyValue; | ||
} | ||
/** | ||
@@ -167,10 +187,6 @@ * @type {any} | ||
set value(setTo) { | ||
this.readOnlyValue = setTo; | ||
if (this.saveOnChange) { | ||
this.update(); | ||
} | ||
return true; | ||
@@ -184,23 +200,65 @@ } | ||
constructor(settings) { | ||
<<<<<<< HEAD | ||
super() | ||
const { | ||
path, | ||
db, | ||
collection | ||
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(); | ||
const { url, db, collection } = settings; | ||
if (!url) throw new Error("No path provided"); | ||
if (typeof url !== "string") | ||
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.'); | ||
throw new Error("Mongodb is not installed. Please install it."); | ||
} | ||
<<<<<<< HEAD | ||
dis.client = await MongoClient.connect(path, { | ||
useNewUrlParser: true, | ||
useUnifiedTopology: true, | ||
}); | ||
let db = dbProp||'db'; | ||
let collection = collectionProp||'collection'; | ||
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 (!(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 = JSON.parse( | ||
JSON.stringify( | ||
await dis.collection.find({}).toArray() | ||
) | ||
); | ||
dis.rawContent = JSON.parse( | ||
JSON.stringify( | ||
dis.readOnlyValue | ||
) | ||
); | ||
dis.readOnlyValue = dis.readOnlyValue.map((val) => { | ||
const v = val; | ||
delete v._id; | ||
return v; | ||
}); | ||
process.on('exit', () => dis.client.close); | ||
setInterval() | ||
======= | ||
dis.client = await MongoClient.connect(url, { | ||
useNewUrlParser: true, | ||
@@ -210,16 +268,16 @@ useUnifiedTopology: true, | ||
let db = dbProp || 'informadb-dbs'; | ||
let collection = collectionProp || 'db'; | ||
let db = dbProp || "informadb-dbs"; | ||
let collection = collectionProp || "db"; | ||
const disListDatabase = dis.client.db().admin().listDatabases(); | ||
if (!db) { | ||
if (!(await dis.client.db().admin().listDatabases()).databases.some((v) => v.name === 'infodbs')) { | ||
throw new Error('\'infodbs\' is not a valid 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'); | ||
dis.collection = dis.client.db("infodbs"); | ||
} else { | ||
if (!(await dis.client.db().admin().listDatabases()).databases.some((v) => v.name === db)) { | ||
if (!(await dislistdatabase).databases.some((v) => v.name === db)) { | ||
throw new Error(`'${db}' is not a valid db.`); | ||
@@ -231,13 +289,21 @@ } | ||
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.'); | ||
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'); | ||
dis.collection = dis.collection.collection("db"); | ||
} else { | ||
if (!(await (await dis.client.db(db || 'infodbs').listCollections()).toArray()).some((v) => v.name === collection)) { | ||
if ( | ||
!( | ||
await ( | ||
await dis.client.db(db || "infodbs").listCollections() | ||
).toArray() | ||
).some((v) => v.name === collection) | ||
) { | ||
throw new Error(`'${collection}' is not a valid collection.`); | ||
@@ -249,21 +315,10 @@ } | ||
dis.readOnlyValue = JSON.parse( | ||
JSON.stringify( | ||
await dis.collection.find({}).toArray() | ||
) | ||
); | ||
dis.rawContent = JSON.parse( | ||
JSON.stringify( | ||
dis.readOnlyValue | ||
) | ||
); | ||
dis.readOnlyValue = dis.readOnlyValue.map((val) => { | ||
dis.readOnlyValue = utils.jsonContentWithFind(this.collection, {}); | ||
dis.rawContent = utils.jsonContent(dis.readOnlyValue); | ||
dis.readOnlyValue = dis.readOnlyValue.map(utils.deleteId); | ||
const v = val; | ||
delete v._id; | ||
return v; | ||
}); | ||
process.on("exit", dis.client.close); | ||
process.on('exit', dis.client.close); | ||
dis.saveOnChange = true; | ||
>>>>>>> d72665bb7ce05e23b596390d36e15652e70fae98 | ||
return dis; | ||
@@ -274,13 +329,11 @@ })(); | ||
* async Updates the file/db to this.readOnlyValue | ||
* @returns {any} - the dataBase/jsonfile | ||
* @returns {any} - this.value | ||
*/ | ||
async update() { | ||
this.props.rawContent.forEach(async (val) => { | ||
await this.props.collection.deleteOne({ | ||
_id: new mongo.ObjectID(val._id) | ||
_id: new mongo.ObjectID(val._id), | ||
}); | ||
}); | ||
<<<<<<< HEAD | ||
if (this.readOnlyValue.length > 0) this.collection.insertMany(this.readOnlyValue); | ||
@@ -301,6 +354,13 @@ 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; | ||
} | ||
/** | ||
@@ -310,10 +370,6 @@ * @type {any} | ||
set value(setTo) { | ||
this.readOnlyValue = setTo; | ||
if (this.saveOnChange) { | ||
this.update(); | ||
} | ||
return true; | ||
@@ -333,3 +389,3 @@ } | ||
RemoteDb, | ||
LocaleDb | ||
}; | ||
LocaleDb, | ||
}; |
{ | ||
"name": "informa-db.js", | ||
"version": "1.0.7-beta.0", | ||
"version": "1.0.8", | ||
"description": "DataBases made easier", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -8,31 +8,29 @@ # Informa-Db.js | ||
## How to install it: | ||
`npm i informa-db.js` | ||
`npm i --save informa-db.js` | ||
## How to use it: | ||
Here's a code example on how to use it: | ||
```js | ||
const Db = require('informa-db.js'); // Require the package | ||
const {LocaleDb} = require('informa-db.js'); // Require the package | ||
(async ()=>{ | ||
const players = await new Db("players.json"); // Uses data.json to store data | ||
// I implemented these functions but they're boring: | ||
if (!players.exist(process.env.PLAYER)) | ||
players.add(process.env.PLAYER,{ | ||
inventory: Array(20), | ||
equipment: Array(5), | ||
temporaryData: { | ||
hp: 20, | ||
xp: 0 | ||
} | ||
}); | ||
// Instead, use this more elegant way of doing it: | ||
if(!players.value[process.env.PLAYER]) | ||
players.value[process.env.PLAYER]={ | ||
inventory: Array(20), | ||
equipment: Array(5), | ||
temporaryData: { | ||
hp: 20, | ||
xp: 0 | ||
} | ||
}; | ||
})() | ||
const players = new LocaleDb("players.json"); // Uses data.json to store data | ||
// I implemented these functions but they're boring: | ||
if (!players.exist(process.env.PLAYER)) | ||
players.add(process.env.PLAYER,{ | ||
inventory: Array(20), | ||
equipment: Array(5), | ||
temporaryData: { | ||
hp: 20, | ||
xp: 0 | ||
} | ||
}); | ||
// Instead, use this more elegant way of doing it: | ||
if(!players.value[process.env.PLAYER]) | ||
players.value[process.env.PLAYER]={ | ||
inventory: Array(20), | ||
equipment: Array(5), | ||
temporaryData: { | ||
hp: 20, | ||
xp: 0 | ||
} | ||
}; | ||
``` | ||
@@ -48,3 +46,2 @@ Before you ask, those all work. | ||
Defaults to '{}' | ||
Will be ignored if this.isMongo is truthy (See isMongo<Boolean>) | ||
### new RemoteDb( options{path<String>, db<String>, collection<String>}<Object> ) | ||
@@ -59,24 +56,5 @@ #### options.path<String> | ||
#### options.db<String> | ||
Database name, defaulting to "infodbs" | ||
Database name, defaulting to "db" | ||
#### options.collection<String> | ||
Collection name, defaulting to "db" | ||
### BaseDb class | ||
#### BaseDb.readOnlyValue<Any> | ||
File/collection content | ||
#### BaseDb.value<Proxy<Any>> | ||
Proxy to this.readOnlyValue | ||
#### BaseDb.saveOnChange<Boolean> | ||
If is true, runs this.update() everytime a change is made (this.add(), this.addOverwrite(), this.remove() and changing this.value ). | ||
Defaults to false. | ||
#### BaseDb.exist(index<Number, String>)<Boolean> | ||
Checks if this.readOnlyValue[index] exists | ||
#### BaseDb.addSafe(index<Number, String>, value<Any>)<Boolean, Any> | ||
Defines this.readOnlyValue[index] to value. | ||
If this.readOnlyValue[index] already exists, will ignore and return false. | ||
#### BaseDb.add(index<Number, String>, value<Any>)<Any> | ||
Defines this.readOnlyValue[index] to value. | ||
#### BaseDb.remove(index<Number, String>)<Undefined> | ||
Splices out/deletes this.readOnlyValue[index] | ||
#### BaseDb.update()<Any> | ||
Updates the file/db to this.readOnlyValue | ||
Collection name, defaulting to "collection" | ||
@@ -83,0 +61,0 @@ ## Contributors ✨ |
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
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
18508
11
507
1
0
76