ml27299 / restheart-client

MIT License
4 stars 0 forks source link

Introduction

Restheart is a popular mongo rest api, this package aims to make it easier to use by abstracting the endpoints to mongoose-like methods.

This package also aims to optimize working with Restheart by

Limitations

Future features

Supports >=node@4.x.x

Getting started

In case you want to configure jwt auth mechanism in Restheart V3

restheart.yml

auth-mechanism:
  implementation-class: org.restheart.security.impl.JwtAuthenticationManagerFactory
  algorithm: HS256
  key: secret
  base64Encoded: false
  usernameClaim: sub
  rolesClaim: roles
  issuer: myIssuer
  audience: myAudience

config.properties

auth-token-enabled = true
auth-token-ttl = 15

Options

options can be passed in on all levels of the package

RestHeartClient(params{})

client.getModels(options{})

This method implicitly figures out the collections in your database by making a request to restheart

client.getModels().then(({mycollection: MyCollection}) => {
    /*...do stuff with MyCollection*/
}); 

client.getModel(name, options{})

This method targets a collection explicitly

find(query{}, options{})

This method returns a list of records from a collection

(async () => {
    await MyCollection.find().catch(err => {
        /*...do something on error*/
    });
    /*...do something with the response*/
})();

Supported sub-methods

count(query{}, options{})

This method returns the count of a target collection

(async () => {
    await MyCollection.count({field1: "someValue"}).catch(err => {
        /*...do something on error*/
    });
    /*...do something with the response*/
})();

Supported sub-methods

create(document = object | array, options{})

This method creates a record or a list of records in a target collection. If the create method is called on a collection that doesnt exist yet, then it will create the collection then create the record.

(async () => {
    const response = await MyCollection.create({field1: "someValue"}).catch(err => {
        /*...do something on error*/
    });
    console.log(response) //{_id: "someObjectId", field1: "someValue"}
    /*...do something with the response*/
})();

Supported sub-methods

update(query = object | array, document{}, options{})

This method updates a record or a list of records in a target collection. If query is an array, then a bulk update will be done, which means each record in the array MUST contain the _id

(async () => {
    await MyCollection.update({field1: "someValue"}, {"$set": {field1: "otherValue"}}).catch(err => {
        /*...do something on error*/
    });
    //alternatively
    await MyCollection.update({field1: "someValue"}, {field1: "otherValue"}).catch(err => {
        /*...do something on error*/    
    });
    //alternatively
    await MyCollection.update([{_id: "someObjectId", field1: "otherValue"}]).catch(err => {
        /*...do something on error*/    
    });
})();

Supported sub-methods

remove(document{}, options{})

This method removes a record or a list of records in a target collection

(async () => {
    await MyCollection.remove({field1: "someValue"}).catch(err => {
        /*...do something on error*/
    });
    /*...do something with the response*/
})();

Supported sub-methods

Sub-Methods (examples in async/await but promises work too)

sub-methods can be chained

raw(val = (true | false) default = true)

Call the raw method when you want to return the response without serialization, which means objectids look like {"$oid": "someObjectId"}

(async () => {
    const response = await MyCollection.findOne({_id: "someObjectId"}).raw();
    console.log(response); //{_id: {"$oid": "someObjectId"}, ...}
})();

select(val = string | object)

Call the select method when you want select fields only (_id is always returned no matter what)

(async () => {
    const response = await MyCollection.find().select("field1 field2"); 
    console.log(response); //[{_id: "someObjectId", field1: "someVal", field2: "otherValue"}  ...}]
    //alternatively
    await MyCollection.findOne({_id: "someObjectId"}).select({field1: 1, field2: 1});
})();

skip(num = number default = 0)

Call the skip method to control where restheart begins returning results. If there're more rows in a collection than the set "pagesize" then the skip method will make multiple calls (each call less than or equal to the set "pagesize") to respond with the set "pagesize".

(async () => {
    await MyCollection.find().skip(10);
})();

limit(num = number default = 0)

Call the limit method to control how many results to return in the response. If the limit number is greater than the pagesize, then multiple calls will be made to reach the limit

(async () => {
    await MyCollection.find().limit(10);
})();

sort(val = {})

Call the sort method when you want to return the records sorted by some field

(async () => {
    await MyCollection.find().sort({field: 1}); //ascending order
})();

page(num = number default = 1)

Call the page method when you want to control which page to return

(async () => {
    await MyCollection.find().page(5); //assuming pagesize = 100, returns results 400-500
})();

noPageLimit(val = (true | false) default true)

Call the noPageLimit method when you want to return all records in a collection. If there're more records in the collection than the set "pagesize" then multiple requests will be made

(async () => {
    const count = await MyCollection.count();
    const response = await MyCollection.find().noPageLimit();
    console.log(response.length === count) //true
})();