Base class defining the interface for datastore implementations
This base class should be used for defining different datastore options for the screwdriver api. The functions exposed contain input validation for to define the contract for datastores so that it is agnostic of the actual implementation.
npm install screwdriver-datastore-base
class MyDatastore extends Datastore {
// Implement the interface
_get(config, callback) {
// do something to fetch data...
if (config.params.id > 0) {
return callback(null, { id: config.params.id });
}
return process.nextTick(() => {
callback(new Error('invalid id'));
});
}
}
const store = new MyDatastore({});
store.get({ table: 'tablename', params: { id: 1 } }, (err, data) => {
// do something....
});
The base class exposes a set of functions:
get
save
update
scan
remove
All of those functions provide input validation on the config object being passed in, and call an underlying function with the arguments passed in.
To take advantage of the input validation, override these functions:
_get
_save
_update
_scan
_remove
Obtain a single record given an id. Returns null
if the record or table does not exist.
Arguments
config
- Anobject
. Each of its properties defines your get operationconfig.table
- Astring
. The datastore table nameconfig.params
- Anobject
. Each of its properties defines the get parametersconfig.params.id
- Astring
. The ID of the item to fetch from the datastorecallback(err, result)
- A callback which is called when the task has succeeded. It receives theerr
andresult
. The result is always returned, with anull
value designating that there is no item to be found.
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
- Anobject
. Each of its properties defines the save parametersconfig.params.id
- Astring
. The ID to associate the data withconfig.params.data
- Anobject
. This is what will be saved in the datastorecallback(err, result)
- A callback which is called when the task has succeeded. It receives theerr
andresult
, whereresult
is the data that was saved in the datastore.
Update a 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
- Anobject
. Each of its properties defines the save parametersconfig.params.id
- Astring
. The ID to associate the data withconfig.params.data
- Anobject
. This is what will be saved in the datastorecallback(err, result)
- A callback which is called when the task is completed. It receives theerr
andresult
. The result is always returned, with anull
value if the record does not exist.
Fetch multiple records from the datastore. Returns error if table does not exist. Returns []
if the table is empty.
Arguments
config
- Anobject
. Each of its properties defines your scan operationconfig.table
- Astring
. The datastore table nameconfig.params
- Anobject
. Each of its properties defines the query parametersconfig.paginate
- Anobject
. 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 returncallback(err, result)
- A callback which is called when the task has succeeded. It receives theerr
andresult
. The expected behavior is:- Return an error if the table does not exist.
- Return an error if the call fails for any reason.
- Return an empty array
[]
if the table is empty. - Return an array of entries if the table is not empty.
npm test
Code licensed under the BSD 3-Clause license. See LICENSE file for terms.