appbase-js
Advanced tools
Comparing version 1.0.0 to 2.0.0-alpha
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" | ||
} | ||
} |
144
README.md
@@ -0,1 +1,145 @@ | ||
 [](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 |
121
test/test.js
@@ -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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 2 instances in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 2 instances in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
Mixed license
License(Experimental) Package contains multiple licenses.
Found 1 instance in 1 package
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
406603
86
0
0
2
146
1
10
14
4301
4
+ Addedes6-promise@^4.1.1
+ Addedeventemitter2@^4.1.2
+ Addedfetch-everywhere@^1.0.5
+ Addedguid@0.0.12
+ Addedjson-stable-stringify@^1.0.1
+ Addedjson-stream@^1.0.0
+ Addedstream@^0.0.2
+ Addedthrough2@^2.0.0
+ Addedurl@^0.11.0
+ Addedws@^2.2.0
+ Addedcall-bind@1.0.8(transitive)
+ Addedcall-bind-apply-helpers@1.0.2(transitive)
+ Addedcall-bound@1.0.3(transitive)
+ Addeddefine-data-property@1.1.4(transitive)
+ Addeddunder-proto@1.0.1(transitive)
+ Addedemitter-component@1.1.2(transitive)
+ Addedencoding@0.1.13(transitive)
+ Addedes-define-property@1.0.1(transitive)
+ Addedes-errors@1.3.0(transitive)
+ Addedes-object-atoms@1.1.1(transitive)
+ Addedes6-promise@4.2.8(transitive)
+ Addedeventemitter2@4.1.2(transitive)
+ Addedfetch-everywhere@1.0.5(transitive)
+ Addedfunction-bind@1.1.2(transitive)
+ Addedget-intrinsic@1.3.0(transitive)
+ Addedget-proto@1.0.1(transitive)
+ Addedgopd@1.2.0(transitive)
+ Addedguid@0.0.12(transitive)
+ Addedhas-property-descriptors@1.0.2(transitive)
+ Addedhas-symbols@1.1.0(transitive)
+ Addedhasown@2.0.2(transitive)
+ Addediconv-lite@0.6.3(transitive)
+ Addedis-stream@1.1.0(transitive)
+ Addedisarray@1.0.02.0.5(transitive)
+ Addedjson-stable-stringify@1.2.1(transitive)
+ Addedjson-stream@1.0.0(transitive)
+ Addedjsonify@0.0.1(transitive)
+ Addedmath-intrinsics@1.1.0(transitive)
+ Addednode-fetch@1.7.3(transitive)
+ Addedobject-inspect@1.13.4(transitive)
+ Addedobject-keys@1.1.1(transitive)
+ Addedprocess-nextick-args@2.0.1(transitive)
+ Addedpunycode@1.4.1(transitive)
+ Addedqs@6.14.0(transitive)
+ Addedreadable-stream@2.3.8(transitive)
+ Addedsafe-buffer@5.0.15.1.2(transitive)
+ Addedsafer-buffer@2.1.2(transitive)
+ Addedset-function-length@1.2.2(transitive)
+ Addedside-channel@1.1.0(transitive)
+ Addedside-channel-list@1.0.0(transitive)
+ Addedside-channel-map@1.0.1(transitive)
+ Addedside-channel-weakmap@1.0.2(transitive)
+ Addedstream@0.0.2(transitive)
+ Addedstring_decoder@1.1.1(transitive)
+ Addedthrough2@2.0.5(transitive)
+ Addedultron@1.1.1(transitive)
+ Addedurl@0.11.4(transitive)
+ Addedutil-deprecate@1.0.2(transitive)
+ Addedwhatwg-fetch@3.6.20(transitive)
+ Addedws@2.3.1(transitive)
- RemovedJSONStream@^1.0.4
- Removedhyperquest@^1.2.0
- RemovedJSONStream@1.3.5(transitive)
- Removedduplexer2@0.0.2(transitive)
- Removedhyperquest@1.3.0(transitive)
- Removedisarray@0.0.1(transitive)
- Removedjsonparse@1.3.1(transitive)
- Removedreadable-stream@1.0.341.1.14(transitive)
- Removedstring_decoder@0.10.31(transitive)
- Removedthrough@2.3.8(transitive)
- Removedthrough2@0.6.5(transitive)