informa-db.js
Advanced tools
Comparing version 1.0.6 to 1.0.7-beta.0
@@ -7,5 +7,2 @@ module.exports = { | ||
}, | ||
extends: [ | ||
'airbnb-base', | ||
], | ||
globals: { | ||
@@ -12,0 +9,0 @@ Atomics: 'readonly', |
345
index.js
@@ -13,5 +13,64 @@ const fs = require('fs'); | ||
} | ||
class Db { | ||
constructor(file, defaultStr, isMongo, db, collection) { | ||
this.genProxy = (data) => new Proxy(data, { | ||
class BaseDb { | ||
constructor(settings) { | ||
this.saveOnChange = false; | ||
} | ||
/** | ||
* Checks if this.readOnlyValue[index] exists | ||
* @param {number} index - the index in the dataBase/jsonfile | ||
*/ | ||
exist(index) { | ||
return !!this.readOnlyValue[index] | ||
} | ||
/** | ||
* 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`); | ||
} | ||
this.readOnlyValue[index] = value; | ||
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; | ||
} | ||
genProxy(data) { | ||
return new Proxy(data, { | ||
set: (obj, prop, val) => { | ||
@@ -24,4 +83,6 @@ obj[prop] = val; | ||
}, | ||
deleteProperty: (pObj, prop) => { | ||
const obj = pObj; | ||
try { | ||
@@ -32,89 +93,185 @@ obj.splice(prop, 1); | ||
} | ||
if (this.saveOnChange) { | ||
this.update(); | ||
} | ||
return true; | ||
}, | ||
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]), | ||
}); | ||
if (!file) throw new Error('No path provided'); | ||
if (typeof file !== 'string') throw new Error('Provided path is not a string'); | ||
} | ||
} | ||
/** | ||
* Class represents the concept to interact with storage units (such as Dbs or JSON files) by defining a proxy property. | ||
*/ | ||
class LocaleDb extends BaseDb { | ||
constructor(settings) { | ||
super(); | ||
//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; | ||
} | ||
}); | ||
} | ||
dis.readOnlyValue = JSON.parse( | ||
fs.readFileSync(path, 'utf8') | ||
); | ||
return dis; | ||
})(); | ||
} | ||
/** | ||
* async Updates the file/db to this.readOnlyValue | ||
* @returns {any} - The database/jsonfile | ||
*/ | ||
async update() { | ||
fs.writeFileSync(this.path, JSON.stringify(this.readOnlyValue, null, '\t')); | ||
return this.readOnlyValue; | ||
} | ||
/** | ||
* @type {any} | ||
*/ | ||
set value(setTo) { | ||
this.readOnlyValue = setTo; | ||
if (this.saveOnChange) { | ||
this.update(); | ||
} | ||
return true; | ||
} | ||
get value() { | ||
return this.genProxy(this.readOnlyValue); | ||
} | ||
} | ||
class RemoteDb extends BaseDb { | ||
constructor(settings) { | ||
super() | ||
const { | ||
path, | ||
db, | ||
collection | ||
} = 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 (async () => { | ||
dis.path = file; | ||
dis.isMongo = typeof isMongo === 'boolean' ? isMongo : dis.path.startsWith('mongodb'); | ||
if (dis.isMongo) { | ||
if (!mongo) { | ||
throw new Error('Mongodb is not installed. Please install it.'); | ||
dis.path = path; | ||
if (!mongo) { | ||
throw new Error('Mongodb is not installed. Please install it.'); | ||
} | ||
dis.client = await MongoClient.connect(path, { | ||
useNewUrlParser: true, | ||
useUnifiedTopology: true, | ||
}); | ||
let db = dbProp || 'informadb-dbs'; | ||
let collection = collectionProp || 'db'; | ||
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.client = await MongoClient.connect(file, { | ||
useNewUrlParser: true, | ||
useUnifiedTopology: 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); | ||
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.`); | ||
} | ||
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.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.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); | ||
dis.collection = dis.collection.collection('db'); | ||
} else { | ||
if (!fs.existsSync(file)) { | ||
fs.writeFileSync(file, defaultStr || '{}', (err) => { | ||
if (err) { | ||
throw err; | ||
} | ||
}); | ||
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.readOnlyValue = JSON.parse(fs.readFileSync(file, 'utf8')); | ||
dis.collection = dis.collection.collection(collection); | ||
} | ||
dis.saveOnChange = true; | ||
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); | ||
return dis; | ||
})(); | ||
} | ||
/** | ||
* async Updates the file/db to this.readOnlyValue | ||
* @returns {any} - the dataBase/jsonfile | ||
*/ | ||
async update() { | ||
exist(index) { | ||
if (!this.readOnlyValue[index]) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
this.props.rawContent.forEach(async (val) => { | ||
async update() { | ||
if (!this.isMongo) { | ||
fs.writeFileSync(this.path, JSON.stringify(this.readOnlyValue, null, '\t')); | ||
return this.readOnlyValue; | ||
} | ||
this.rawContent.forEach(async (val) => { | ||
await this.collection.deleteOne({ _id: new mongo.ObjectID(val._id) }); | ||
await this.props.collection.deleteOne({ | ||
_id: new mongo.ObjectID(val._id) | ||
}); | ||
}); | ||
if (this.readOnlyValue.length > 0) this.collection.insertMany(this.readOnlyValue); | ||
this.rawContent = JSON.parse(JSON.stringify(await this.collection.find({}).toArray())); | ||
this.rawContent = JSON.parse( | ||
JSON.stringify( | ||
await this.collection.find({}).toArray() | ||
) | ||
); | ||
this.readOnlyValue = JSON.parse( | ||
@@ -124,44 +281,38 @@ JSON.stringify( | ||
), | ||
).map((val) => { const v = val; delete v._id; return v; }); | ||
).map((val) => { | ||
const v = val; | ||
delete v._id; | ||
return v; | ||
}); | ||
return this.readOnlyValue; | ||
} | ||
add(index, pseudoValue) { | ||
if (!this.exist(index)) { | ||
this.readOnlyValue[index] = pseudoValue; | ||
} else { | ||
return false; | ||
} | ||
if (this.saveOnChange) { | ||
return this.update()[index]; | ||
} | ||
return pseudoValue; | ||
} | ||
/** | ||
* @type {any} | ||
*/ | ||
set value(setTo) { | ||
remove(index) { | ||
this.readOnlyValue.splice(index, 1); | ||
return undefined; | ||
} | ||
this.readOnlyValue = setTo; | ||
addOverWrite(index, pseudoValue) { | ||
this.readOnlyValue[index] = pseudoValue; | ||
if (this.saveOnChange) { | ||
return this.update(this.readOnlyValue)[index]; | ||
} | ||
return pseudoValue; | ||
} | ||
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; }); | ||
this.readOnlyValue = this.readOnlyValue.map((v) => { | ||
const r = v; | ||
delete r._id; | ||
return r; | ||
}); | ||
return this.genProxy(this.readOnlyValue); | ||
} | ||
} | ||
module.exports = Db; | ||
module.exports = { | ||
RemoteDb, | ||
LocaleDb | ||
}; |
{ | ||
"name": "informa-db.js", | ||
"version": "1.0.6", | ||
"version": "1.0.7-beta.0", | ||
"description": "DataBases made easier", | ||
@@ -9,2 +9,5 @@ "main": "index.js", | ||
"repository": "https://github.com/informathemusic/informa-db.js", | ||
"publishConfig": { | ||
"registry": "https://registry.npmjs.org/" | ||
}, | ||
"devDependencies": { | ||
@@ -11,0 +14,0 @@ "mongodb": "^3.5.8", |
41
test.js
@@ -1,10 +0,33 @@ | ||
require('dotenv').config(); | ||
const Db = require('./index.js'); | ||
/*const { RemoteDb, LocaleDb } = require('./index.js'); | ||
(async ()=>{ | ||
const players = await new LocaleDb({ path: "players.json" }); // Uses data.json to store data | ||
console.log(process.env.PLAYER) | ||
// 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]&&process.env.PLAYER) | ||
players.value[process.env.PLAYER]={ | ||
inventory: Array(20), | ||
equipment: Array(5), | ||
temporaryData: { | ||
hp: 20, | ||
xp: 0 | ||
} | ||
}; | ||
})()*/ | ||
const { LocaleDb } = require('./index'); | ||
(async () => { | ||
const test = await new Db(process.env.SERVER); | ||
// const ee = test.value.length; | ||
test.value = []; | ||
test.value.push({ ee: 'eeeeee' }); | ||
console.log(test.value); | ||
})(); | ||
let l = new LocaleDb({path: 'test.json'}); | ||
l.value.number = 444444; // nothing show up in the file | ||
l.value.name = 44; // error | ||
l.value.wow = 'there you go'; | ||
console.log(l.value); |
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
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
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
15872
9
406
98
2
2
1