firestorm-db
Advanced tools
Comparing version 1.0.0 to 1.1.0
118
index.js
const { default: axios } = require("axios") | ||
/** | ||
* @typedef {SearchOption} | ||
* @typedef {Object} SearchOption | ||
* @property {String} field The field you want to search in | ||
@@ -13,2 +13,4 @@ * @property {"!=" | "==" | ">=" | "<=" | "<" | ">" | "in" | "includes" | "startsWith" | "endsWith" | "array-contains" | "array-contains-any"} criteria // filter criteria | ||
const ID_FIELD_NAME = 'id' | ||
const readAddress = () => { | ||
@@ -24,3 +26,3 @@ if(!_address) | ||
return _address + 'put.php' | ||
return _address + 'post.php' | ||
} | ||
@@ -35,11 +37,36 @@ | ||
/** | ||
* Class representing a collection | ||
* @template T | ||
*/ | ||
class Collection { | ||
/** @param {String} name The name of the Collection */ | ||
constructor(name) { | ||
/** | ||
* @param {String} name The name of the Collection | ||
* @param {Function?} addMethods Additional methods and data to add to the objects | ||
*/ | ||
constructor(name, addMethods = el => el) { | ||
this.addMethods = addMethods | ||
this.collectionName = name | ||
} | ||
__add_methods(req) { | ||
return new Promise((resolve, reject) => { | ||
req | ||
.then(el => { | ||
if(Array.isArray(el)) { | ||
return resolve(el.map(e => this.addMethods(e))) | ||
} | ||
el[Object.keys(el)[0]][ID_FIELD_NAME] = Object.keys(el)[0] | ||
el = el[Object.keys(el)[0]] | ||
// else on the object itself | ||
return resolve(this.addMethods(el)) | ||
}).catch(err => reject(err)) | ||
}) | ||
} | ||
/** | ||
* Auto-extracts data from Axios request | ||
* @param {Promise<any>} request The Axios concerned request | ||
* @param {Promise<T>} request The Axios concerned request | ||
*/ | ||
@@ -59,10 +86,10 @@ __extract_data(request) { | ||
* @param {String|Number} id The entry ID | ||
* @returns {Promise} Result entry you may be looking for | ||
* @returns {Promise<T>} Result entry you may be looking for | ||
*/ | ||
get(id) { | ||
return this.__extract_data(axios.get(readAddress(), { data: { | ||
return this.__add_methods(this.__extract_data(axios.get(readAddress(), { data: { | ||
"collection": this.collectionName, | ||
"command": "get", | ||
"id": id | ||
}})) | ||
}}))) | ||
} | ||
@@ -72,10 +99,25 @@ | ||
* Search through collection | ||
* @param {SearchOption[]} searchOptions | ||
* @param {SearchOption[]} searchOptions | ||
* @returns {Promise<T[]>} | ||
*/ | ||
search(searchOptions) { | ||
return this.__extract_data(axios.get(readAddress(), { data: { | ||
"collection": this.collectionName, | ||
"command": "search", | ||
"search": searchOptions | ||
}})) | ||
return new Promise((resolve, reject) => { | ||
this.__extract_data(axios.get(readAddress(), { | ||
data: { | ||
"collection": this.collectionName, | ||
"command": "search", | ||
"search": searchOptions | ||
} | ||
})).then(res => { | ||
const arr = [] | ||
Object.keys(res).forEach(contribID => { | ||
const tmp = res[contribID] | ||
tmp[ID_FIELD_NAME] = contribID | ||
arr.push(tmp) | ||
}) | ||
resolve(this.__add_methods(Promise.resolve(arr))) | ||
}).catch(err => reject(err)) | ||
}) | ||
} | ||
@@ -88,6 +130,14 @@ | ||
read_raw() { | ||
return this.__extract_data(axios.get(readAddress(), { data: { | ||
"collection": this.collectionName, | ||
"command": "read_raw" | ||
}})) | ||
let data = this.__extract_data(axios.get(readAddress(), { | ||
data: { | ||
"collection": this.collectionName, | ||
"command": "read_raw" | ||
} | ||
})) | ||
Object.keys(data).forEach(key => { | ||
this.addMethods(data[key]) | ||
}) | ||
return data | ||
} | ||
@@ -101,3 +151,3 @@ | ||
*/ | ||
__write_data(command, value = undefined) { | ||
__write_data(command, value = undefined, multiple = false) { | ||
const obj = { | ||
@@ -108,3 +158,8 @@ "token": writeToken(), | ||
} | ||
if(value) obj["value"] = value | ||
if(value) { | ||
if(multiple) | ||
obj["values"] = value | ||
else | ||
obj["value"] = value | ||
} | ||
@@ -138,3 +193,3 @@ return obj | ||
addBulk(values) { | ||
return this.__extract_data(axios.post(writeAddress(), this.__write_data('addBulk', values))) | ||
return this.__extract_data(axios.post(writeAddress(), this.__write_data('addBulk', values, true))) | ||
} | ||
@@ -156,4 +211,4 @@ | ||
*/ | ||
removeBuks(keys) { | ||
return this.__extract_data(axios.post(writeAddress(), this.__write_data('write_raw', keys))) | ||
removeBulk(keys) { | ||
return this.__extract_data(axios.post(writeAddress(), this.__write_data('removeBulk', keys))) | ||
} | ||
@@ -164,7 +219,7 @@ | ||
* @param {String} key The key of the value you want to set | ||
* @param {Object} value The value tou want for this key | ||
* @param {Object} value The value you want for this key | ||
* @returns {Promise<any>} | ||
*/ | ||
set(key, value) { | ||
const data = this.__write_data('write_raw', value) | ||
const data = this.__write_data('set', value) | ||
data['key'] = key | ||
@@ -181,5 +236,4 @@ return this.__extract_data(axios.post(writeAddress(), data)) | ||
setBulk(keys, values) { | ||
const data = this.__write_data('write_raw') | ||
const data = this.__write_data('setBulk', values, true) | ||
data['keys'] = keys | ||
data['values'] = values | ||
return this.__extract_data(axios.post(writeAddress(), data)) | ||
@@ -208,7 +262,7 @@ } | ||
/** | ||
* | ||
* @param {String} name Collection name to get | ||
* @param {Function?} addMethods Additional methods and data to add to the objects | ||
*/ | ||
collection: function(name) { | ||
return new Collection(name) | ||
collection: function(name, addMethods = el => el) { | ||
return new Collection(name, addMethods) | ||
}, | ||
@@ -222,3 +276,5 @@ | ||
return this.collection(name) | ||
} | ||
}, | ||
ID_FIELD: ID_FIELD_NAME | ||
} |
{ | ||
"name": "firestorm-db", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Self hosted Firestore-like database with API endpoints based on micro bulk operations", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -1,3 +0,10 @@ | ||
# firestorm-db | ||
<div align="center"> | ||
<img src="img/firestorm-128.png"> | ||
<h1>firestorm-db</h1> | ||
<a href="https://www.npmjs.com/package/firestorm-db" targtet="_blank" ><img alt="npm" src="https://img.shields.io/npm/v/firestorm-db?color=cb0000&logo=npm&style=flat-square"> <img alt="npm bundle size" src="https://img.shields.io/bundlephobia/min/firestorm-db?label=NPM%20minified%20size&style=flat-square"> </a> <img alt="GitHub file size in bytes" src="https://img.shields.io/github/size/TheRolfFR/firestorm-db/index.js?color=43A047&label=Script%20size&logoColor=green&style=flat-square"> | ||
</div> | ||
*Self hosted Firestore-like database with API endpoints based on micro bulk operations* | ||
@@ -4,0 +11,0 @@ |
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
14860
230
146