#
Creating a Provider
So, you want to create your own provider? I'm too slow to make them, aren't I? Well, you're in luck, because I've made every attempt to document what's required for new providers to be built. Specifically, below is a list of methods that they need to support.
For examples of how these methods might work, please look at existing providers and their code. If you can make those methods more efficient or performant using core features of your database then by all means, feel free to do so! But, they must return the data in the format described in the code.
Note that the documentation below doesn't include example return values, but the source code does.
#
JoshProvider API
Every Josh Provider must contain a certain number of methods that will be called by the main Josh code.
If your provider does not support a method, it should be present and throw an error indicating this fact.
#
new JoshProvider([options])
#
joshProvider.init(Josh) ⇒ Promise
Internal method called on persistent Josh to load data from the underlying database.
Kind: instance method of JoshProvider
Returns: Promise
- Returns the defer promise to await the ready state.
#
joshProvider.get(key, path) ⇒ Promise.<*>
Retrieves a single value from the database.
Kind: instance method of JoshProvider
Returns: Promise.<*>
- The data stored for the key, or at the path.
#
joshProvider.getAll() ⇒ Promise.<Object.<*>>
- Retrieves all values from the database.
Kind: instance method of JoshProvider
Returns: Promise.<Object.<*>>
- An object consisting of every key and value in the database. At the top level, the key is what the user providers when using set(key, value) and the value in the object is whatever's in the database. Every value should be parsed using this.parseData()
\
#
joshProvider.getMany(keys) ⇒ Promise.<Object.<*>>
Retrieves one or many values from the database.
Kind: instance method of JoshProvider
Returns: Promise.<Object.<*>>
- An object consisting every key requested by the user, and its value in the datbase. At the top level, the key is what the user providers when using set(key, value) and the value in the object is whatever's in the database. Every value should be parsed using this.parseData()
#
joshProvider.random(count) ⇒ Promise.<Object.<*>>
Retrieves one or more random values from the database.
Kind: instance method of JoshProvider
Returns: Promise.<Object.<*>>
- An object representing one or more random keys taken from the database, with their values. At the top level, the key is what the user providers when using set(key, value) and the value in the object is whatever's in the database. Every value should be parsed using this.parseData()
#
joshProvider.randomKey(count) ⇒ Promise.<Array.<string>>
Retrieves a random key from all the database keys for this Josh.
Kind: instance method of JoshProvider
Returns: Promise.<Array.<string>>
- An array of random keys taken from the database, or a single key if count is 1.
#
joshProvider.has(key, path) ⇒ Promise.<boolean>
Verifies whether a key, or value at path, exists in the database.
Kind: instance method of JoshProvider
Returns: Promise.<boolean>
- Whether the key (or value at path) exists.
#
joshProvider.keys() ⇒ Promise.<Array.<string>>
Retrieves all the indexes (keys) in the database.
Kind: instance method of JoshProvider
Returns: Promise.<Array.<string>>
- Array of all indexes (keys) in the database.\
#
joshProvider.values() ⇒ Promise.<array.<*>>
Retrieves all of the values in the database.
Kind: instance method of JoshProvider
#
joshProvider.count() ⇒ Promise.<integer>
Retrieves the number of rows in the database.
Kind: instance method of JoshProvider
Returns: Promise.<integer>
- The number of rows in the database.\
#
joshProvider.set(key, path, val) ⇒ Promise.<Provider>
Saves a key in the database, along with its value.
Kind: instance method of JoshProvider
Returns: Promise.<Provider>
- This provider.
#
joshProvider.setMany(data, overwrite) ⇒ Promise.<Provider>
Writes many different keys and their values to the database.
Kind: instance method of JoshProvider
Returns: Promise.<Provider>
- This provider.
#
joshProvider.delete(key, path) ⇒ Promise.<Provider>
Deletes a key and its value, or the part of an object or array value, from the database.
Kind: instance method of JoshProvider
Returns: Promise.<Provider>
- This provider.
#
joshProvider.clear() ⇒ Promise.<Provider>
Deletes every single entry in the database.
Kind: instance method of JoshProvider
Returns: Promise.<Provider>
- This provider.\
#
joshProvider.push(key, path, value, allowDupes) ⇒ Promise.<Provider>
Pushes a new value into an array stored in the database.
Kind: instance method of JoshProvider
Returns: Promise.<Provider>
- This provider.
#
joshProvider.remove(key, path, val) ⇒ Promise.<Provider>
Removes a value from an array stored in the database.
Kind: instance method of JoshProvider
Returns: Promise.<Provider>
- This provider.
#
joshProvider.inc(key, path) ⇒ Promise.<Provider>
Increments a numerical value within the database.
Kind: instance method of JoshProvider
Returns: Promise.<Provider>
- This provider.
#
joshProvider.dec(key, path) ⇒ Promise.<Provider>
Decrements a numerical value within the database.
Kind: instance method of JoshProvider
Returns: Promise.<Provider>
- This provider.
#
joshProvider.math(key, path, operation, operand) ⇒ Promise.<Provider>
Executes a mathematical operation on a numerical value within the database.
Kind: instance method of JoshProvider
Returns: Promise.<Provider>
- This provider.
#
joshProvider.findByFunction(fn, path) ⇒ Promise.<*>
Finds and returns a value using a function.
Kind: instance method of JoshProvider
Returns: Promise.<*>
- The first value found by the function, or null
if no value found.
#
joshProvider.findByValue(path, value) ⇒ Promise.<*>
Finds and returns an entire value, by checking whether a specific sub-value was found a the given path.
Kind: instance method of JoshProvider
Returns: Promise.<*>
- The first value found, or null
if no value found.
#
joshProvider.filterByFunction(fn, path) ⇒ Promise.<Object>
Finds and returns one or more values using a function to check whether the value is desired.
Kind: instance method of JoshProvider
Returns: Promise.<Object>
- The values found by the function, or {}
if no value found.
#
joshProvider.filterByValue(path, value) ⇒ Promise.<Object>
Finds and returns one or move value, by checking whether a specific sub-value was found at the given path.
Kind: instance method of JoshProvider
Returns: Promise.<Object>
- The values found by this function, or {}
if no value found.
#
joshProvider.mapByValue(path) ⇒ Promise.<Array.<string>>
Retrieves the value at the specified path for every stored object or array in the database.
Kind: instance method of JoshProvider
Returns: Promise.<Array.<string>>
- An array of the values at that path
#
joshProvider.mapByFunction(fn) ⇒ Promise.<Array.<*>>
Runs a function for every value in the database and returns an array with the return of that function for each value.
Kind: instance method of JoshProvider
Returns: Promise.<Array.<*>>
- An array of the values returned by the function for each value.
#
joshProvider.includes(key, path, val) ⇒ Promise.<boolean>
Verifies if a value is part of an array at the key (or the path within that key).
Kind: instance method of JoshProvider
Returns: Promise.<boolean>
- Whether the value is in the array.
#
joshProvider.someByPath(path, value) ⇒ Promise.<boolean>
Verifies if the provided value is located in any of the values stored in the database.
Kind: instance method of JoshProvider
Returns: Promise.<boolean>
- Should return true as soon as the value is found, or false if it hasn't been.
#
joshProvider.someByFunction(fn) ⇒ Promise.<boolean>
Verifies if something is true in any of the values stored in the database, through a function.
Kind: instance method of JoshProvider
Returns: Promise.<boolean>
- Whether the fn
has returned true for any value.
#
joshProvider.everyByPath(path, value) ⇒ Promise.<boolean>
Verifies if a value at a path is identical to the one provided, for every single value stored in the database.
Kind: instance method of JoshProvider
Returns: Promise.<boolean>
- Whether the value was equal to the one at the path for every single value in the database.
#
joshProvider.everyByFunction(fn) ⇒ Promise.<boolean>
Verifies if a condition is true on every single value stored in the database, using a function.
Kind: instance method of JoshProvider
Returns: Promise.<boolean>
- Whether the fn
has returned true for every value.
#
joshProvider.close()
Closes the database. This function should be used to shut down any connection or file access to the database this provider refers to.
Kind: instance method of JoshProvider
#
joshProvider.destroy()
Deletes the database. This function should delete everything in the specific table used by this database (for the josh's name specifically) It should also remove any temporary table, as well as the "autoid" saved for it. After this method is run, no trace of the specific josh should exist.
Kind: instance method of JoshProvider
#
joshProvider.autoId() ⇒ Promise.<string>
Returns the "next" automatic ID for this josh. AutoId should be a string, and can technically be anything you want - either a numerically incremented value or just an automatic row ID or DB ID (autonum, mongo's _id , etc). No 2 Ids should ever be identical.
Kind: instance method of JoshProvider
Returns: Promise.<string>
- An automatic ID.\
#
joshProvider.parseData(data) ⇒ *
Internal method to read data from the database. This is essentially the contrary of serialize-javascript
's "serialize()" method. Note: EVAL IS NORMAL. As long as 100% of the data you read from this has been written by serialize(), this is SAFE. If you have any doubts as to what data has been written, or if you have to deal with mixed or unsafe data, then you should take further action to ensure you are not subject to security breaches!
Kind: instance method of JoshProvider
Returns: *
- A value parsed through eval, which will be valid javascript.