Screwdriver In-Memory Datastore
In-memory datastore for use with the Screwdriver API
Usage
npm install screwdriver-datastore-imdb
Initialization
The IMDB is an extension of the screwdriver-datastore-base class and implements all of the functions exposed.
The IMDB takes in an optional config parameter as a JSON object that will be used to initialize the in-memory database.
Arguments
config
- Optional Anobject
passed to the initialization of the classconfig.db
- Optional Anobject
to initialize the database withconfig.filename
- Optional Afilename
to initialize the database with. If bothdb
andfilename
are passed in, thedb
value will be ignored
const Imdb = ;
Without a database (will initialize with {} as default)
const imdb = ; imdb; // Outputs null
With a filename
Example file some/path/to/config.json
:
{
table1: {
key1: {
foo: 'bar'
}
}
}
const imdb2 = filename: 'some/path/to/config.json'; imdb2; // Outputs { foo: 'bar' }
With a database
const imdb3 = db: table1: key1: foo: 'bar' ; imdb3; // Outputs { foo: 'bar' }
get
Obtain a single record given an id. Returns null
if the record does not exist.
Arguments
config
- Anobject
. Each of its properties defines your get operationconfig.table
- Astring
. The datastore table nameconfig.params
- An object. Each of its properties defines the get parametersconfig.params.id
- Astring
. The ID of the item to fetch from the datastore
Example
const Imdb = ;const imdb = ; // successful get operationimdb; // { color: 'red', type: 'fruit' } // get operation on a non-existing entryimdb; // null
save
Save a record in the datastore. Returns saved data.
Arguments
config
- Anobject
. Each of its properties defines your save operationconfig.table
- Astring
. The datastore table nameconfig.params
- An object. Each of its properties defines the save parametersconfig.params.id
- Astring
. The ID to associate the data withconfig.params.data
- An object. This is what will be saved in the datastore
const Imdb = ;const imdb = db: favorites: fruit: name: 'cherry' ; // overwrite pre-existing entryimdb; // { id: 'fruit', name: 'cherry' } // save new entryimdb; // { id: 'meal', name: 'mac & cheese' }
update
Update an existing record in the datastore. Returns null
if the record does not exist.
Arguments
config
- Anobject
. Each of its properties defines your save operationconfig.table
- Astring
. The datastore table nameconfig.params
- An object. Each of its properties defines the save parametersconfig.params.id
- Astring
. The ID to associate the data withconfig.params.data
- An object. This is what will be saved in the datastore
const Imdb = ;const imdb = db: meals: lunch: main: 'sandwich' side: 'chips' ; // update entryimdb; // { id: 'lunch', main: 'sandwich', side: 'chips', drink: 'ice tea' } // update a non-existing entryimdb; // null
scan
Fetch multiple records from the datastore. Returns []
if the table is empty. Rejects with an error if table does not exist.
Arguments
config
- Anobject
. Each of its properties defines your scan operationconfig.table
- Astring
. The datastore table nameconfig.params
- An object. Each of its properties defines the query parametersconfig.paginate
- An object. Each of its properties further defines the characteristics for paginationconfig.paginate.count
- Aninteger
. This is the number of items per pageconfig.paginate.page
- Aninteger
. This is the page number of the set you wish for the datastore to return
Example
const Imdb = ;const imdb = ; // valid scanimdb; // [{ id: 0, name: 'blue' }, { id: 1, name: 'green'}] // best effort based on given criteriaimdb; // [{ id: 2, name: 'red' }] // no results foundimdb; // [] // scan operation on a non-existing entrydatastore; // [Error: Invalid table name "unicorns"]
Testing
npm test
License
Code licensed under the BSD 3-Clause license. See LICENSE file for terms.