njsa
AStore In NodeJS. The astore is Avro Data Store based on Akka.
NJSA include two modules:
- njsa_api.js
- NJSAA: AStore standard api/interfaces for njsa
- njsa.js
- NJSA: AStore In NodeJS
about AStore to see: https://github.com/wandoulabs/astore
Installation
install NJSA package by npm:
$ npm install njsa
Usage
1). install astore and run it, see:
https://github.com/wandoulabs/astore#run-astore
2). get astore configure ($ASTORE is install direcotry)
for develop environment:
$ cd $ASTORE/src/main/resources
or in released directory:
$ cd $ASTORE/conf
read these configures:
$ grep -A3 'wandou.astore.web' application.confwandou.astore.web
and, try base detect interface (must run astore first)
$ echo `curl -s 'http://127.0.0.1:8080/ping'`pong
3). in nodejs, run samples
change testcase connection arguments to your astore configures:
$ cd njsa/test$ grep -H -Pne 'astore_options |require\(.*\.option\(' t_*.jst_njsa.js:12:var astore_options = {addr: {uri: 'http://localhost:8080'}};t_njsa_api.js:10:var user_centre = require.option.att_njsa_api.js:11:var activities = require.option.at
and run it
$ node t_njsa_api.js$ node t_njsa.js
Base avro schema (and schema define by json/js)
you must initialition astore with a entity schema define. the define must is a record or union type, and putschema to astore(if union, need a fullname argument).
examples:
var modelName = 'user'var centre = var schemaContent = JSONvar entityOption = fullname: 'com.wandoujia.astore.demoschema.User' timeout: 2*60*60*1000var {}
modelName
NJSA can work at multi-model astore environment, a model stands for a schema and its activities for all entities. a entity is actor in astore runtime(scala actor).
centre
It's command centre, got it by require njsa_api module:
centre =
The centre is a function with base command/function call format:
// op of <id> in <model>// orfunction(modelName, opName, ...) // op of <model> in astore
you can re-option it(the options see: nodejs <request> module):
centre = centre
and/or, bind the centre to a model:
centre = centre
now, next calls will include a default mode name:
// op of <id> in <model>// orfunction(opName, ...) // op of <model> in astore
schemaContent
The "./schema/demo.js" file is a schema with js format define(no, it's not json define). so you can require the model and call JSON.stringify() of the result.
the js define need return a object(for arvo RECORD type), or array(for avro UNION type). example:
// export array as union typesmoduleexports = ActivityRecord User;
the ActivityRecord/User is Javascript Object define.
fullname
if schema include union type, then must set <fullname> option when putschema. the fullname include full-namespace, example:
var ActivityRecord = name: 'ActivityRecord' namespace: "com.wandoujia.astore.demoschema" type: 'record' fields: // ... // export array as union typesmoduleexports = ActivityRecord ...;
for the case, <ActivityRecord> fullname is "com.wandoujia.astore.demoschema.ActivityRecord".
callback
It's nodejs request module callback function, see: nodejs <request> module
if none the argument, njsa_api will use a default callback as console error logger:
{ var c = response && responsestatusCode && responsestatusCode if error || c != '2' console }
NJSAA: standard api/interfaces
the NJSAA module implement full api/interfaces for astore. include:
// op for record // op for record with key // op for record with avpath // is select_op with avpath: '.' // op for Array / Map only. use avpath. // op for schema // params = {fullname=aString, timeout=aNumber} // op for script // <script> is function/string, or object with 'run' property. // is 'GET' http_method
AVPATH see: https://github.com/wandoulabs/avpath RESTful-api see: https://github.com/wandoulabs/astore#restful-api
enhanced script support for astore
a script snippet is a "OnUpdate trigger" of entity field/member in astore. the script is full support javascript(see Nashorn javascript and extensions in java8. you can use <putscript> op with a scriptContent block to hook it:
or remove it:
field argument
script snippet must hook a filed of entity record. when the field updated by any instance of the record, astore will throw a event/callback and call your script snippet.
once tigger will incluced some fileds(multi OnUpdate is merged). so you need check who is changed(but, the NJSA can got current fieldName to help you process it).
scriptid argument
any uniqued string to mark your scriptContent. a script content can invoke more fields, if you can check/tag each filed. but, fieldName+scriptid must unique in global of the astore.
script argument
in astore, script/scriptContent must is string and the content will run into a function context. so, your <putscript> op must submit some lines/statements only.
but NJSA support expanded script snippet/content/environment. you can get enhanced features at the script argument. ex:
// 1. use script line/string // 2. use function // 3. use a object as custom context, the object has custom properties as variants,// and 'run' peroperty will as scriptContent put into astore.
expanded script environment
- global variant <record> is standard js object in astore native environment, the <record> is warped java object. so you cant direct access any field/properties, only do these:
// in astore native environmentvalue_1 = recordvalue_2 = record...
with NJSA, ex:
// with NJSAvalue_1 = recordfield_avalue_2 = recordfield_b...
- global variant <fields> is standard js object too in astore native environment, the <fields> is array of OnUpdate changeset. so you must enumerate fields to get a field/fieldValue by name. ex:
// in astore native environmentvar findFieldName = 'field_b' gotValuefor i=0; i<fieldslength; i++ if fieldsi_1name == findFieldName gotValue = fieldsi_2; break }...
with NJSA, you can do these:
// with NJSA...
- global variant <fieldName> will set to current changed field name in astore native environment, you cant know who changing. so you must put field names into script content in advance. but in NJSA, the <fieldName> is global variant. ex:
// in NJSA
- configurable script content in NJSA, a function or code snippet can invoke any fields, and support configurable arguments. ex:
// in NJSA { http_get} // now you can invoke any fields with the the script, and notify some web servicesvar script_1 = config: notify_uri: 'http://localhost:8080/notify' run: showUpdating var script_2 = config: notify_uri: 'http://remote_host_name/notify_from_astore' run: showUpdating
NJSA: AStore In NodeJS
The NJSA isn't NJSAA. NJSA is a framework support for astore, but NJSAA is standard interfaces.
NJSA work at multi-model environment too, and support custom system environment version.
try run the testcase:
> node node ./t_njsa.js astore[user] initializing successed, result: OKastore[activities] initializing successed, result: OKastore system core information module initializing successed, result: OKastore start time: Tue Feb 17 2015 02:08:02 GMT+0800 version: 1.0
try run again:
> node node ./t_njsa.js astore start time: Tue Feb 17 2015 02:08:02 GMT+0800 version: 1.0
so, the framework initializing once run anywhere. and you can check schemas/environment version in astore, or restart/reset it.
put more system information into the environment:
var astore_options = addr: uri: 'http://localhost:8080';var astore = astore_options; // server_info write/readastore
or get any njsaa instance/model
// work at multi-modeluser_centre = astoreactivities_centre = astoresystem_centre = astore
or check system_info schema, and/or load new schema as a new model
// print/recheck system core/framework schemaconsole // schema loader for <putschema> opnew_centre = astore