rollun-ts-datastore
v1.2.0
Published
rollun-datastore written in TS
Downloads
33
Readme
rollun-ts-datastore
Basically rollun-datastore, but in TS. For now contains only HTTP Datastore. Supports either Node and Browser environment. Datastore clients use fetch and node-fetch for http requests.
Installation
preferred way to install this library is via npm. Run
npm install rollun-ts-datastore
or add
"rollun-ts-datastore": "*",
to the dependencies section of your package.json
Important to know
To query data from client.query()
method from datastore, you need
to use query object from
rollun-ts-rql library.
Basic usage
import HttpDatastore from "rollun-ts-datastore/dist/HttpDatastore";
import {Query, And, Le, Ge} from "rollun-ts-rql";
// keep in mind, using HttpDatastore server side (node.js),
// You MUST spesify absolute path to your api
const datastore = new HttpDatastore('my/users/datastore');
const newItem = {id: '123', name: 'Alex', age: 23};
//add item to store
datastore
.create(newItem)
.then((item) => {
console.log(item);
// will log newly created item
// useful when using autogenerated IDs
});
//read item from store
datastore
.read('123')
.then((item) => {
// do something with item
// ...
})
.catch((error) => {
console.warn('an error occurred while reading from datastore', error)
});
//update item (by id)
datastore
.update({id: '123', age: 24})
.then((item) => {
console.log(item);
// will log updated item;
});
//delete item
datastore
.delete('123')
.then((item) => {
console.log(item);
// will log deleted item;
});
//get items for query
const query = new Query({
query: new And([
new Le('age', 25),
new Ge('age', 18),
])
});
datastore
.query(query)
.then((items) => {
//do something with result
//...
})
.catch((error) => {
console.warn(error)
});