appbase-js is a universal JavaScript client library for working with the appbase.io database.

It can:

  • Index new documents or update / delete existing ones.
  • Work universally with Node.JS, Browser, and React Native.

It can't:

  • Configure mappings, change analyzers, or capture snapshots. All these are provided by elasticsearch.js - the official Elasticsearch JS client library.

Appbase.io - the database service is opinionated about cluster setup and hence doesn't support the Elasticsearch devops APIs. See rest.appbase.io for a full reference on the supported APIs.

Create Cluster

You can start by creating an Elasticsearch cluster with appbase.io or bring your Elasticsearch cluster or self-host appbase.io.

  • Log in toAppbase Dashboard, and create a new cluster.
  • Copy the URL of your cluster for further actions

You can read see the different options for creating a cluster and their pricing over here.

Creating an Index

This gif shows how to create an index on appbase.io cluster, which we will need for this quickstart guide.

For this tutorial, we will use an index called good-books-demo. The credentials for this index are 376aa692e5ab:8472bf31-b18a-454d-bd39-257c07d02854.

Note

Appbase.io uses HTTP Basic Auth, a widely used protocol for simple username/password authentication. It also support creating various API credentials with different access. You can read more about access control in docs.

Install appbase-js

We will fetch and install the appbase-js lib using npm. 4.0.0-beta is the most current version.

Copy
npm install appbase-js

Adding it in the browser should be a one line script addition.

Copy
<script defer src="https://unpkg.com/appbase-js/dist/appbase-js.umd.min.js"></script>

Alternatively, a UMD build of the library can be used directly from jsDelivr.

To write data to appbase.io, we need to first create a reference object. We do this by passing the appbase.io API URL, app name, and credentials into the Appbase constructor:

Copy
var appbaseRef = Appbase({
	url: 'https://appbase-demo-ansible-abxiydt-arc.searchbase.io',
	app: 'good-books-demo',
	credentials: 'c84fb24cbe08:db2a25b5-1267-404f-b8e6-cf0754953c68',
});

OR

Copy
var appbaseRef = Appbase({
	url:
		'https://c84fb24cbe08:db2a25b5-1267-404f-b8e6-cf0754953c68@appbase-demo-ansible-abxiydt-arc.searchbase.io',
	app: 'good-books-demo',
});

Credentials can also be directly passed as a part of the API URL.

Storing Data

Once we have the reference object (called appbaseRef in this tutorial), we can insert any JSON object into it with the index() method.

Copy
var jsonObject = {
	department_name: 'Books',
	department_name_analyzed: 'Books',
	department_id: 1,
	name: 'A Fake Book on Network Routing',
	price: 5595,
};
Copy
appbaseRef
	.index({
		type: '_doc',
		id: 'X1',
		body: jsonObject,
	})
	.then(function(response) {
		console.log(response);
	})
	.catch(function(error) {
		console.log(error);
	});

The index() method (and all the other appbase methods) return a promise.

Note

appbase.io uses the same APIs and data modeling conventions as Elasticsearch.

GET Data

Now that we are able to store data, let's try to get the data back from appbase.io with the get() method.

Copy
appbaseRef.get({
 type: "_doc",
 id: "X1"
}).then(function(response) {
 console.log(response)
}).catch(function(error) {
 console.log(error)
})

/* get() response */
{
 "_index": "good-books-demo",
 "_type": "books",
 "_id": "X1",
 "_version": 5,
 "found": true,
 "_source": {
  "department_name": "Books",
  "department_name_analyzed": "Books",
  "department_id": 1,
  "name": "A Fake Book on Network Routing",
  "price": 5595
 }
}

Search Queries

search method helps you query the Elasticsearch with query DSL as its body.

For example, here we are trying to run match_all query that returns all the documents.

Copy
appbaseRef.search({
 body: {
  query: {
   match_all: {}
  }
 }
}, function(response) {
 console.log(response);
}, function("search() response: ", error) {
 console.log("caught a error: ", error)
})

/* Response when a new data matches */
{
 "_type": "books",
 "_id": "X1",
 "_version": 5,
 "found": true,
 "_source": {
  "department_name": "Books",
  "department_name_analyzed": "Books",
  "department_id": 1,
  "name": "A Fake Book on Network Routing",
  "price": 6034
 }
}