1.5.6 • Published 2 years ago

loopback-nodejs-client v1.5.6

Weekly downloads
51
License
MIT
Repository
github
Last release
2 years ago

loopback-nodejs-client

NodeJS client library with loopback SDK syntax

Install

$ npm install --save loopback-nodejs-client

Usage

Without authentification

var LoopbackClient = require('loopback-nodejs-client')

var loopbackClient = new LoopbackClient('http://localhost:3000/api');

var Customers = loopbackClient.getModel('customers')

Customers.create({name: 'ido4pro', city: 'plougonvelin'})
  .then(function (customer) {
    console.log(customer)

    Customers.find({filter: {where: {name: 'ido4pro'}}})
      .then(function (customers) {
        console.log(customers)

      })
      .catch(function (err) {
        console.log(err)
      })

    Customers.count({where: {name: 'ido4pro'}})
      .then(function (customers) {
        console.log(customers)

      })
      .catch(function (err) {
        console.log(err)
      })

  })
  .catch(function (err) {
    console.log(err)
  })

With authentification

var loopbackClientWithAuth = new LoopbackClient('http://localhost:3000/api','xxx EMAIL USER xxx', 'xxx PASSWORD xxx')

loopbackClientWithAuth.createToken()
  .then(function()
  {
      
       var Customers = loopbackClientWithAuth.getModel('customers')
       
       Customers.count({where: {name: 'ido4pro'}})
            .then(function (customers) {
              console.log(customers)
      
            })
            .catch(function (err) {
              console.log(err)
            })
      
  })

With custom login path

var loopbackClientWithAuth = new LoopbackClient('http://localhost:3000/api','xxx EMAIL USER xxx', 'xxx PASSWORD xxx')


// Default Value is : '/users/login?include=user'
loopbackClientWithAuth.setCustomLoginPath('/user/login?include=user')

loopbackClientWithAuth.createToken()
  .then(function()
  {
      
       var Customers = loopbackClientWithAuth.getModel('customers')
       
       Customers.count({where: {name: 'ido4pro'}})
            .then(function (customers) {
              console.log(customers)
      
            })
            .catch(function (err) {
              console.log(err)
            })
      
  })

Methods

Try to be compliant with https://loopback.io/doc/en/lb3/Working-with-data.html

create(data) --> data = { id: 'xxx', filter: {...} } (filter is optional)

count(query) --> query = { where : {name: 'xxx'} }, where = { where: {and: {name: 'xxx'},{city: 'xxx'}

NOT AVAILABLE IN LOOPBACK EXTERNALY updateAll(query,data)

updateById(id,data)

updateAttributesById(id,data)

find(filter)

findById(data)

findOne(query)

deleteById(id)

replaceOrCreate(data)

upsertWithWhere(query,data) --> query = { where : {name: 'xxx'} }

replaceOrCreate(data)

remote(customRoute,data) -> your customRoute name (example: greet) see https://loopback.io/doc/en/lb3/Remote-methods.html

remote(customRoute,query,data) -> your customRoute name (example: greet) see https://loopback.io/doc/en/lb3/Remote-methods.html

remoteGet(customRoute,query)

remotePut(customRoute,data)

remotePatch(customRoute,data)

Options

All methods accepts an option object to define timeout and/or additionnals headers

find({},{ timeout:2000, headers:{'TAG_ID':'146464646464}})

Unit Tests

Check the test/index.js

DEBUG

This lib use debug (https://github.com/visionmedia/debug) to log request

Activate log trace with environment variable "DEBUG=LoopBackClient"

example : DEBUG=LoopBackClient npm run test

License

MIT © Stéphane GUILLY

1.5.6

2 years ago

1.5.5

6 years ago

1.5.4

6 years ago

1.5.3

6 years ago

1.4.4

6 years ago

1.4.3

6 years ago

1.4.1

6 years ago

1.4.0

6 years ago

1.3.1

6 years ago

1.3.0

6 years ago

1.2.3

6 years ago

1.2.2

6 years ago

1.2.1

6 years ago

1.0.1

7 years ago

1.0.0

7 years ago