Giter Club home page Giter Club logo

datastore-base's Introduction

datastore base

Version Downloads Build Status Open Issues Dependency Status License

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.

Usage

npm install screwdriver-datastore-base

Extending

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

Validation

get

Obtain a single record given an id. Returns null if the record or table does not exist.

Arguments

  • config - An object. Each of its properties defines your get operation
  • config.table - A string. The datastore table name
  • config.params - An object. Each of its properties defines the get parameters
  • config.params.id - A string. The ID of the item to fetch from the datastore
  • callback(err, result) - A callback which is called when the task has succeeded. It receives the err and result. The result is always returned, with a null value designating that there is no item to be found.

save

Save a record in the datastore. Returns saved data.

Arguments

  • config - An object. Each of its properties defines your save operation
  • config.table - A string. The datastore table name
  • config.params - An object. Each of its properties defines the save parameters
  • config.params.id - A string. The ID to associate the data with
  • config.params.data - An object. This is what will be saved in the datastore
  • callback(err, result) - A callback which is called when the task has succeeded. It receives the err and result, where result is the data that was saved in the datastore.

update

Update a record in the datastore. Returns null if the record does not exist.

Arguments

  • config - An object. Each of its properties defines your save operation
  • config.table - A string. The datastore table name
  • config.params - An object. Each of its properties defines the save parameters
  • config.params.id - A string. The ID to associate the data with
  • config.params.data - An object. This is what will be saved in the datastore
  • callback(err, result) - A callback which is called when the task is completed. It receives the err and result. The result is always returned, with a null value if the record does not exist.

scan

Fetch multiple records from the datastore. Returns error if table does not exist. Returns [] if the table is empty.

Arguments

  • config - An object. Each of its properties defines your scan operation
  • config.table - A string. The datastore table name
  • config.params - An object. Each of its properties defines the query parameters
  • config.paginate - An object. Each of its properties further defines the characteristics for pagination
  • config.paginate.count - An integer. This is the number of items per page
  • config.paginate.page - An integer. This is the page number of the set you wish for the datastore to return
  • callback(err, result) - A callback which is called when the task has succeeded. It receives the err and result. 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.

Example datastores

Testing

npm test

License

Code licensed under the BSD 3-Clause license. See LICENSE file for terms.

datastore-base's People

Contributors

d2lam avatar filbird avatar nkatzman avatar petey avatar stjohnjohnson avatar tkyi avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.