Git Product home page Git Product logo

lightblue.js's Introduction

lightblue.js

Build Status Coverage Status

A lightblue client written in JavaScript for frontend or Node.JS applications.

The same library may be used for servers or clients, with special support for AngularJS applications.

Install

bower install lightblue.js --save

npm install lightblue.js --save

git clone https://github.com/alechenninger/lightblue.js.git

Import

Browserify/NodeJS (CommonJS) or RequireJS (AMD)

// commonjs or NodeJS
var lightblue = require("lightblue");

// asynchronous module definition (amd)
require(["lightblue"], function(lightblue) {
  ...
});

Vanilla.js

// No module framework (use window.lightblue)
<script src="lightblue.min.js" type="text/javascript"></script>

Usage

Once you have a lightblue object, you can get a client:

// Assumes /data and /metadata for data and metadata services respectively,
// but you can override.
var client = lightblue.getClient("http://my.lightblue.host.com/rest");

And you can use builder API's for more readable queries and autocomplete if your IDE supports it:

var field = lightblue.query.field;

// Use in queries...
field("firstName").equalTo("Bob").and(field("age").greaterThan(21));
field("lastName").equalTo(field("firstName"));

The query builder API is not yet fully flushed out, but adding functionality is trivial. See issue #11.

Example find request

// Query
var bobsOlderThan20 = field("firstName").equalTo("bob")
    .and(field("age").greaterThan(20));

// Projection
// No projection builder yet but it would be something like this:
var everything = include("*").recursively();

var find = client.data.find({
  entity: "User",
  version: "1.0.0",
  query: bobsOlderThan20,
  projection: everything
})
.then(console.log.bind(console));

AngularJS

If angular is detected, a "lightblue" module will be registered with a "lightblue" service as a namespace for lightblue facilities. In this environment, Angular's $http service will be used instead of making XHR requests directly. You can configure the host(s) to use using providers.

var app = angular.module("app", ["lightblue"]);

app.config(["lightblueProvider", function(lightblueProvider) {
  lightblueProvider.setHost("http://my.lightblue.com");
}]);

app.controller("ctrl", ["lightblue", function(lightblue) {
  var field = lightblue.query.field;

  lightblue.data.find(field("foo").equalTo("bar"))
      .then(function(response) {
        var entity = response.processed[0];
      });
}]);

Multiple lightblue service instances with Angular

Angular services are singletons, and therefore you only get to configure one lightblue backend to talk to. If for some reason your application needs to talk to more than one lightblue host, you can create separate lightblue services using the global lightblue namespace, and wrap them in specific Angular services for your needs. Don't use the global namespace directly: wrap it in a service.

myModule.factory("otherLightblueInstance", ["$http", function() {
  // `lightblue` is globally defined on `window` if needed.
  // Don't use it directly: wrap it in a service.
  // Pass $http in via config object unless you want the client to use XHR
  // directly.
  return lightblue.getClient("my.other.lightblue.com", {$http: $http});
}]);

Auth

Basic using lightblue object (client or server)

lightblue.getClient("foo.com", {auth: {username: "foo", password: "bar"}});

Basic w/ Angular service (client)

Since lightblue.js uses $http, you can add interceptors / common headers to all $http requests if that works for you. You can also configure the lightblue clients directly:

// Inject lightblue.http...
module.controller("login", ["lightblue.http", function(lightblueHttp) {
  $scope.login = function() {
    lightblueHttp.setAuth({
      username: $scope.username,
      password: $scope.password
    });
  };
}]);

And of course if needed you can still use the global lightblue object as above to get a new client with basic auth credentials, just remember to pass $http as per above example.

SSL certs (client)

This is handled by the user's browser, each in their own way. You will generally have to import your cert into the browser and select it once you visit the web application. The JavaScript has no idea certs are involved. See each browser's documentation for more details.

SSL certs (server)

In the options object for the clients, you may define an "httpsAgent" key which has the same semantics as making an https request with node and defining an agent to use. If you pass undefined, it uses the global agent. Or, you may pass your own (via new https.Agent(options)). In either case, you will need to configure your certificates on the agent. See nodejs's https documentation for more information.

// Configure the global agent
var fs = require("fs");
var https = require("https");

https.globalAgent.key = fs.readFileSync('my-key.pem');
https.globalAgent.cert = fs.readFileSync('my-cert.pem');

// Defaults to global agent
var clientUsingGlobalAgent = lightblue.getClient("https://my.lightblue.com");

// Use a lightblue-specific agent
var clientUsingOwnAgent = lightblue.getClient("https://my.lightblue.com", {
  httpsAgent: new https.Agent({
    key: fs.readFileSync('my-key.pem'),
    cert: fs.readFileSync('my-cert.pem')
  })
});

lightblue.js's People

Contributors

alechenninger avatar

Stargazers

Abhishek koserwal avatar Naveen Malik avatar Jonathon Turel avatar

Watchers

James Cloos avatar  avatar

Forkers

akoserwal

lightblue.js's Issues

Create projection builder API

// single, simple projection
var allFields = include("*").recursively();

// include all except some
// multiple projections use array
var exceptSome = [
  include("*").recursively(),
  exclude("some")
];

// array projection
var jsonExampleFromDocs =  [ { "field": "address", "include": true,
     "match": { "city": "Raleigh" }, "project": { "streetaddress": true} } ]

var streetAddressesInRaleigh = include("address")
    .where(field(city).equalTo("Raleigh"))
    .project(include("streetaddress"));

// alternates:
include("streetaddress").in("address").where(field("city").equalTo("Raleigh"));
in("address").where(field("city").equalTo("Raleigh")).include("streetaddress");

Main question is what does it mean in array projection to "include": false?

Integration tests

Might be useful to be able to actually test the queries against lightblue, but not sure if it is worth it.

Actually make http requests

  • Define an interface for an http dependency
  • Implement with node http (browserify will wrap XMLHttpRequest)
  • Implement with angular $http

ES6-ify or port to Dart

ES6 to ES5 compilers are pretty mature.

Better would be dart's dev_compiler but that sounds like it might be a ways off.

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.