New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

appbase-js

Package Overview
Dependencies
Maintainers
2
Versions
118
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

appbase-js - npm Package Compare versions

Comparing version 1.0.0 to 2.0.0-alpha

.babelrc

35

Gruntfile.js
module.exports = function(grunt) {
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
watch: {
files: [ "./*.js"],
tasks: [ 'browserify', 'uglify' ]
},
browserify: {
'browser/appbase.js': ['appbase.js']
},
uglify: {
'browser/appbase.min.js': ['browser/appbase.js']
}
})
grunt.loadNpmTasks('grunt-contrib-watch')
grunt.loadNpmTasks('grunt-browserify')
grunt.loadNpmTasks('grunt-contrib-uglify')
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
mochaTest: {
test: {
options: {
reporter: 'spec'
},
src: ['test/**/*.js']
}
}
})
grunt.loadNpmTasks('grunt-babel')
grunt.loadNpmTasks('grunt-contrib-watch')
grunt.loadNpmTasks('grunt-browserify')
grunt.loadNpmTasks('grunt-contrib-uglify')
grunt.loadNpmTasks('grunt-mocha-test')
grunt.registerTask('test', [ 'mochaTest' ])
}
{
"name": "appbase-js",
"version": "1.0.0",
"description": "",
"main": "appbase.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"appbase",
"elasticsearch",
"stream",
"search"
],
"author": "Sacheendra Talluri",
"license": "ISC",
"dependencies": {
"JSONStream": "^1.0.4",
"hyperquest": "^1.2.0"
},
"devDependencies": {
"elasticsearch": "^5.0.0",
"grunt": "^0.4.5",
"grunt-browserify": "^3.8.0",
"grunt-contrib-uglify": "^0.9.1",
"grunt-contrib-watch": "^0.6.1",
"mocha": "^2.2.5"
}
"name": "appbase-js",
"version": "2.0.0-alpha",
"description": "Appbase.io streaming client lib for Javascript",
"main": "lib/index.js",
"scripts": {
"build": "webpack -p",
"build:lib": "babel src --out-dir lib",
"test": "grunt test"
},
"keywords": [
"appbase",
"elasticsearch",
"streams",
"search"
],
"author": "Sacheendra Talluri",
"license": "MIT",
"dependencies": {
"es6-promise": "^4.1.1",
"eventemitter2": "^4.1.2",
"fetch-everywhere": "^1.0.5",
"guid": "0.0.12",
"json-stable-stringify": "^1.0.1",
"json-stream": "^1.0.0",
"stream": "^0.0.2",
"through2": "^2.0.0",
"url": "^0.11.0",
"ws": "^2.2.0"
},
"devDependencies": {
"babel-core": "^6.26.0",
"babel-loader": "^7.1.2",
"babel-plugin-add-module-exports": "^0.2.1",
"babel-preset-es2015": "^6.24.1",
"grunt": "^0.4.5",
"grunt-babel": "^5.0.3",
"grunt-browserify": "^3.8.0",
"grunt-contrib-uglify": "^0.9.1",
"grunt-contrib-watch": "^0.6.1",
"grunt-mocha-test": "^0.12.7",
"json-loader": "^0.5.7",
"mocha": "^2.2.5",
"node-noop": "^1.0.0",
"webpack": "^3.6.0"
},
"browser": {
"ws": "./src/ws-shim.js"
}
}

@@ -0,1 +1,145 @@

![Build Status Image](https://img.shields.io/badge/build-passing-brightgreen.svg) [![Code Climate](https://codeclimate.com/github/appbaseio/appbase-js/badges/gpa.svg)](https://codeclimate.com/github/appbaseio/appbase-js)
# appbase-js
Appbase.io is a data streams library for Node.JS and Javascript (browser build is in the [browser/](https://github.com/appbaseio/appbase-js/tree/master/browser) directory); compatible with [elasticsearch.js](https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/index.html).
An up-to-date documentation for Node.JS API is available at http://docs.appbase.io/scalr/javascript/nodejs-intro.html.
## Quick Example
Working code snippets where each step builds on the previous ones.
#### Step 1: Add some data into the app (uses elasticsearch.js)
```js
// app and authentication configurations
const HOST_URL = "https://scalr.api.appbase.io"
const APPNAME = "createnewtestapp01"
const CREDENTIALS = "RIvfxo1u1:dee8ee52-8b75-4b5b-be4f-9df3c364f59f"
// Add data into our ES "app index"
var Appbase = require("appbase-js")
var appbase = new Appbase({
url: HOST_URL,
app: APPNAME,
credentials: CREDENTIALS
});
appbase.index({
type: "product",
id: "1",
body: {
name: "A green door",
price: 12.50,
tags: ["home", "green"],
stores: ["Walmart", "Target"]
}
}).on("data", function(res) {
console.log(res);
}).on("error", function(err) {
console.log(err);
});
```
#### Step 2: Read the data stream from a particular DB location
Returns continous updates on a JSON document from a particular ``type``.
```js
appbase.getStream({
type: "product",
id: "1"
}).on("data", function(res) {
// "data" handler is triggered every time there is a **new** document update.
console.log(res);
}).on("error", function(err) {
console.log("caught a stream error", err);
})
```
``Note:`` Existing document value is returned via ``get()`` method.
##### Console Output
```js
{ _index: "app`248",
_type: "product",
_id: "1",
_version: 4,
found: true,
_source:
{ name: "A green door",
price: 12.5,
tags: [ "home", "green" ],
stores: [ "Walmart", "Target" ] } }
```
getStream() returns a ``stream.Readable`` object, which can be conveniently listened via the `on("data")` event listener. Check out the [stream_document_test.js](https://github.com/appbaseio/appbase-js/blob/master/test/stream_document_test.js) where we make an update to the document and see any further updates to it via the "data" event.
#### Step 3: Apply queries on data streams
Get continuous results by searching across the database streams. A query can be written using the [ElasticSearch Query DSL](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl.html) - which supports composing boolean, regex, geo, fuzzy, range queries. Let's stream the results of a simple **``match_all``** query on the ``product`` type:
```js
appbase.searchStream({
type: "product",
body: {
query: {
match_all: {}
}
}
}).on("data", function(res, err) {
console.log(res);
}).on("error", function(err) {
console.log("caught a stream error", err);
})
```
##### Console Output
```js
{ took: 1,
timed_out: false,
_shards: { total: 1, successful: 1, failed: 0 },
hits:
{ total: 4,
max_score: 1,
hits: [ [Object], [Object], [Object], [Object] ] } }
```
searchStream() also returns a ``stream.Readable`` object, which can be conveniently listened via the `on("data")` event listener. Check out the [stream_search_test.js](https://github.com/appbaseio/appbase-js/blob/master/test/stream_search_test.js) where we make an update that matches the query and see the results in the event stream.
## API Reference
For a complete API reference, check out [JS API Ref doc](http://docs.appbase.io/scalr/javascript/api-reference.html).
### Global
**[new Appbase(args)](https://github.com/appbaseio/appbase-js/blob/master/appbase.js#L16)**
Returns a **reference** object on which streaming requests can be performed.
> **args** - A set of key/value pairs that configures the ElasticSearch Index
    url: "https://scalr.api.appbase.io"
    app: App name (equivalent to an ElasticSearch Index)
    credentials: A `username:password` combination used for Basic Auth.
Optionally (and like in the quick example above), ``url`` can contain the credentials field in the format: https://<credentials>@scalr.appbase.io.
### Reference
**[reference.getStream(args)](https://github.com/appbaseio/appbase-js/blob/master/appbase.js#L99)**
Get continuous updates on a JSON document with a ``type`` and ``id``. Returns a [``stream.Readable``](https://nodejs.org/api/stream.html#stream_class_stream_readable) object.
> **args** - A set of key/value pairs that makes the document URL
    type: ElasticSearch Type, a string
    id: Valid Document ID
**[reference.searchStream(args)](https://github.com/appbaseio/appbase-js/blob/master/appbase.js#L103)**
Get continuous updates on search queries (fuzzy, boolean, geolocation, range, full-text). Returns a [``stream.Readable``](https://nodejs.org/api/stream.html#stream_class_stream_readable) object.
> **args** - A set of key/value pairs that makes the document URL
    type: ElasticSearch Type, a string
    body: A JSON Query Body (Any query matching the [ElasticSearch Query DSL](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl.html))

@@ -1,24 +0,18 @@

var assert = require('assert')
var getTests = {}
var streamDocumentTests = {}
streamDocumentTests.streamOneDocument = function streamOneDocument(client, streamingClient, done) {
getTests.getOneDoc = function getOneDoc(streamingClient, done) {
var tweet = {"user": "olivere", "message": "Welcome to Golang and Elasticsearch."}
client.index({
index: 'testindex',
type: 'tweet',
id: '1',
streamingClient.index({
type: "tweet",
id: "1",
body: tweet
}, function(err, res) {
if(err) {
done(err)
return
}
})
.on("error", done)
.on("data", function(res) {
var first = true
var responseStream = streamingClient.streamDocument({
type: 'tweet',
id: '1'
var responseStream = streamingClient.get({
type: "tweet",
id: "1"
})
responseStream.on('error', function(err) {
responseStream.on("error", function(err) {
if(err) {

@@ -29,27 +23,7 @@ done(err)

})
responseStream.on('data', function(res) {
if(first) {
client.index({
index: 'testindex',
type: 'tweet',
id: '1',
body: tweet
}, function(err, res) {
if(err) {
done(err)
return
}
})
first = false
responseStream.on("data", function(res) {
if(res) {
done()
} else {
assert.deepEqual(res, {
_index: 'testindex',
_type: 'tweet',
_id: '1',
_source: tweet
}, 'event not as expected')
responseStream.pause()
done()
done(new Error("Atleast 1 document should have been present."))
}

@@ -60,2 +34,2 @@ })

module.exports = streamDocumentTests
module.exports = getTests

@@ -1,28 +0,25 @@

var assert = require('assert')
var searchTests = {}
var streamSearchTests = {}
streamSearchTests.streamMatchAll = function streamMatchAll(client, streamingClient, done) {
searchTests.searchForOneDoc = function searchForOneDoc(streamingClient, done) {
var tweet = {"user": "olivere", "message": "Welcome to Golang and Elasticsearch."}
client.index({
index: 'testindex',
type: 'tweet',
id: '1',
streamingClient.index({
type: "tweet",
id: "1",
body: tweet
}, function(err, res) {
if(err) {
done(err)
return
}
})
.on("error", function(e) {
console.log(e);
done();
})
.on("data", function(res) {
var first = true
var responseStream = streamingClient.streamSearch({
type: 'tweet',
var responseStream = streamingClient.search({
type: "tweet",
body: {
query: {
match_all: {}
"query": {
"match_all": {}
}
}
})
responseStream.on('error', function(err) {
responseStream.on("error", function(err) {
if(err) {

@@ -33,29 +30,7 @@ done(err)

})
responseStream.on('data', function(res) {
if(first) {
setTimeout(function() {
client.index({
index: 'testindex',
type: 'tweet',
id: '1',
body: tweet
}, function(err, res) {
if(err) {
done(err)
return
}
})
}, 2000)
first = false
responseStream.on("data", function(res) {
if(res.hits.total > 0) {
done()
} else {
assert.deepEqual(res, {
_index: 'testindex',
_type: 'tweet',
_id: '1',
_source: tweet
}, 'event not as expected')
responseStream.pause()
done()
done(new Error("Atleast 1 document should have been present."))
}

@@ -66,2 +41,2 @@ })

module.exports = streamSearchTests
module.exports = searchTests

@@ -1,37 +0,110 @@

var elasticsearch = require('elasticsearch')
var appbase = require('../')
var appbase = require("../lib");
var streamDocumentTests = require('./get_test.js')
var streamSearchTests = require('./search_test.js')
var indexTest = require("./index_test.js")
var updateTest = require("./update_test.js")
var getTest = require("./get_test.js")
var searchTest = require("./search_test.js")
var bulkTest = require("./bulk_test.js")
var getStreamTests = require("./get_stream_test.js")
var searchStreamTests = require("./search_stream_test.js")
var getTypesTest = require("./get_types_test.js")
var getMappingsTest = require("./get_mappings_test.js")
var helper = require("../lib/helpers");
describe("Appbase", function() {
this.timeout(10000)
describe('Appbase', function() {
this.timeout(5000)
var client, streamingClient;
var client, streamingClient
before(function() {
client = new elasticsearch.Client({
host: 'http://testuser:testpass@localhost:7999',
apiVersion: '1.6'
});
streamingClient = new appbase({
url: "https://scalr.api.appbase.io",
app: "appbasejs-test-app",
credentials: "zuGt16TPP:1ede0dc2-e727-476e-bc35-ee2956e750ef"
})
})
streamingClient = appbase.newClient({
url: 'http://localhost:7999',
username: 'testuser',
password: 'testpass',
appname: 'testindex'
describe("#index()", function() {
it("should index one document", function(done) {
indexTest.indexOneDocument(streamingClient, done)
})
})
describe('#streamDocument()', function () {
it('should receive event when new document is inserted', function(done) {
streamDocumentTests.streamOneDocument(client, streamingClient, done)
describe("#update()", function() {
it("should update one document", function(done) {
updateTest.updateOneDocument(streamingClient, done)
})
})
describe('#streamSearch()', function () {
it('should receive event when new document is inserted', function(done) {
streamSearchTests.streamMatchAll(client, streamingClient, done)
describe("#get()", function() {
it("should get one document", function(done) {
getTest.getOneDoc(streamingClient, done)
})
})
})
describe("#search()", function() {
it("should return results", function(done) {
searchTest.searchForOneDoc(streamingClient, done)
})
})
describe("#bulk()", function() {
it("should bulk index one document", function(done) {
bulkTest.bulkIndexOneDocument(streamingClient, done)
})
})
describe("#getStream()", function() {
it("should receive event when new document is inserted", function(done) {
getStreamTests.streamOneDocument(streamingClient, done)
})
it("should not receive initial data", function(done) {
getStreamTests.onlyStreamOneDocument(streamingClient, done)
})
it("should receive only one event", function(done) {
getStreamTests.stopStreamingDocument(streamingClient, done)
})
})
describe("#searchStream()", function() {
it("should receive event when new document is inserted", function(done) {
searchStreamTests.streamMatchAllSingleType(streamingClient, done)
})
it("should receive event when new document is inserted while querying multiple types", function(done) {
searchStreamTests.streamMatchAllMultipleTypes(streamingClient, done)
})
})
describe("#getTypes()", function() {
it("should receive an array of types", function(done) {
getTypesTest.getAllTypes(streamingClient, done)
})
})
describe("#getMappings()", function() {
it("should get mappings object", function(done) {
getMappingsTest.getAllMappings(streamingClient, done)
})
})
describe("#helpers", function() {
it("validate() : should check for body and type", function(done) {
var mock = {
"body": {
test: "test"
},
"type": "test"
}
var e = helper.validate(mock, {
"body": "object",
"type": "string"
})
if (e !== true) {
done(e)
} else {
done()
}
})
})
})

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc