firestorm-db
Advanced tools
Comparing version 1.6.0 to 1.7.0
71
index.js
@@ -124,6 +124,7 @@ try { | ||
* Search through collection | ||
* @param {SearchOption[]} searchOptions | ||
* @param {SearchOption[]} searchOptions Array of search options | ||
* @param {(Number|false|true)?} random Random result seed, disabled by default, but can activated with true or a given seed | ||
* @returns {Promise<T[]>} | ||
*/ | ||
search(searchOptions) { | ||
search(searchOptions, random=false) { | ||
if(!Array.isArray(searchOptions)) | ||
@@ -145,8 +146,22 @@ return Promise.reject(new Error('searchOptions shall be an array')) | ||
let params = { | ||
"collection": this.collectionName, | ||
"command": "search", | ||
"search": searchOptions | ||
} | ||
if(random !== false) { | ||
if(random === true) { | ||
params.random = {} | ||
} else { | ||
let seed = parseInt(random) | ||
if(isNaN(seed)) return Promise.reject(new Error('random takes as parameter true, false or an integer value')) | ||
params.random = { | ||
"seed": seed | ||
} | ||
} | ||
} | ||
return new Promise((resolve, reject) => { | ||
this.__get_request({ | ||
"collection": this.collectionName, | ||
"command": "search", | ||
"search": searchOptions | ||
}).then(res => { | ||
this.__get_request(params).then(res => { | ||
const arr = [] | ||
@@ -240,2 +255,44 @@ | ||
/** | ||
* Returns random max entries offsetted with a given seed | ||
* @param {Integer} max | ||
* @param {Integer} seed | ||
* @param {Integer} offset | ||
* @returns {Promise} entries | ||
*/ | ||
random(max, seed, offset) { | ||
const params = {} | ||
if(max !== undefined) { | ||
if(typeof(max) !== 'number' || !Number.isInteger(max) || max < -1) return Promise.reject(new Error('Expected integer >= -1 for the max')) | ||
params.max = max | ||
} | ||
const hasSeed = seed !== undefined | ||
const hasOffset = offset !== undefined | ||
if(hasOffset && !hasSeed) return Promise.reject(new Error('You can\'t put an offset without a seed')) | ||
if(hasOffset && (typeof(offset) !== 'number' || !Number.isInteger(offset) || offset < 0)) return Promise.reject(new Error('Expected integer >= -1 for the max')) | ||
if(hasSeed) { | ||
if((typeof(seed) !== 'number' || !Number.isInteger(seed))) return Promise.reject(new Error('Expected integer for the seed')) | ||
if(!hasOffset) offset = 0 | ||
params.seed = seed | ||
params.offset = offset | ||
} | ||
return this.__get_request({ | ||
'collection': this.collectionName, | ||
'command': 'random', | ||
'random': params | ||
}).then(data => { | ||
Object.keys(data).forEach(key => { | ||
data[key][ID_FIELD_NAME] = key | ||
this.addMethods(data[key]) | ||
}) | ||
return Promise.resolve(data) | ||
}) | ||
} | ||
/** | ||
* | ||
@@ -242,0 +299,0 @@ * @param {String} command The write command you want |
{ | ||
"name": "firestorm-db", | ||
"version": "1.6.0", | ||
"version": "1.7.0", | ||
"description": "Self hosted Firestore-like database with API endpoints based on micro bulk operations", | ||
@@ -11,3 +11,3 @@ "main": "index.js", | ||
"full": "npm run php_start && npm run test ; npm run php_stop", | ||
"local_tests": "sudo act" | ||
"local_tests": "sudo act -P ubuntu-latest=shivammathur/node:latest" | ||
}, | ||
@@ -14,0 +14,0 @@ "repository": { |
@@ -83,5 +83,6 @@ <div align="center"> | ||
| get(id) | id: ``String\|Name`` | Tries to get one element by its key | | ||
| search(searchOptions) | searchOptions: ``SearchOption[]`` | Searches collections and returns matching results | | ||
| search(searchOptions, random) | searchOptions: ``SearchOption[]`` random?:`false|true|Number`| Searches collections and returns matching results | | ||
| searchKeys(keys) | keys: ``String[] \| Number[]`` | Searches collections with given keys and returns matching results | | ||
| select(selectOption) | selectOption: ``{ field: String[] }`` | Improved read_raw with field selection | | ||
| random(max, seed, offset) | max?: ``Integer >= -1`` seed?: ``Integer`` offset?:``Integer >= 0`` | Reads random entries of collection | | ||
@@ -88,0 +89,0 @@ Search method can take one or more options to filter entries in a collection. A search option studies a ``field`` with a ``criteria`` and compares it to a ``value``. |
25708
417
197