Skip to content

Commit

Permalink
data-entities seneca 0.6.1
Browse files Browse the repository at this point in the history
  • Loading branch information
rjrodger committed Jan 30, 2015
1 parent f08517c commit f0d86d7
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 56 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,4 @@ node_modules

config.mine.js

data-entities/json-data
data-entities/level-data
data-entities/*-data
35 changes: 35 additions & 0 deletions data-entities/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

Notes:

The code in main.js shows you how to save data entities to different
places. The two jsonfile-store and single level-store data store
plugins instances are each associated with data entities with bases of
'foo', 'bar' and 'zed' respectively, for all data operations. All
other bases are stored in memory using mem-store, which is the
default.

The option setting: map:{'-/base/-','*'} achieves this. The map is
from data entity type (zone/base/name) to a set of operations (save,
load, etc).

The example code creates new entities using seneca.make$, of type
-/foo/red, -/bar/green and -/zed/blue, and saves them to local folders
on disk. The data is also loaded independently of Seneca to verify
that it actually has been persisted! The entity.native$ method is used
to do this, as it exposes the underlying database API.


Setup:
$ npm install

Note: seneca-level-store compiles the level modules, as they are
native. If this does not work on your platform (e.g. Windows), just
comment out the level-store code.

Run with:
$ node main.js

For detailed logging, try:
$ node main.js --seneca.log.all


33 changes: 0 additions & 33 deletions data-entities/README.txt

This file was deleted.

1 change: 0 additions & 1 deletion data-entities/json-data/README.txt

This file was deleted.

1 change: 0 additions & 1 deletion data-entities/level-data/README.txt

This file was deleted.

74 changes: 60 additions & 14 deletions data-entities/main.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,68 @@
/* Copyright (c) 2013-2014 Richard Rodger, MIT License */
/* Copyright (c) 2013-2015 Richard Rodger, MIT License */
"use strict";

var fs = require('fs')
var util = require('util')

var seneca = require('seneca')()

seneca.use( 'jsonfile-store', { folder:'json-data', map:{'-/json/-':'*'}})
seneca.use( 'level-store', { folder:'level-data', map:{'-/level/-':'*'}})
// Use two separate instances of jsonfile-store, each looking after a
// different set of data entities
seneca.use( 'jsonfile-store$foo', { folder:'foo-data', map:{'-/foo/-':'*'}})
seneca.use( 'jsonfile-store$bar', { folder:'bar-data', map:{'-/bar/-':'*'}})

// Use a different data store for another set of data entities
seneca.use( 'level-store', { folder:'zed-data', map:{'-/zed/-':'*'}})

// Everything else goes into the default mem-store

seneca
.make$('json','foo',{propA:'val1',propB:'val2'})
.save$(function(err,json_foo){
if(err) return console.error(err);
console.log(''+json_foo)

seneca
.make$('level','bar',{propA:'val3',propB:'val4'})
.save$(function(err,level_bar){
if(err) return console.error(err);
console.log(''+level_bar)
})
.make$('foo/red',{a:1})
.save$(function(err,foo_red){
if(err) return console.error('foo_red',err);
var fc = fs.readFileSync(__dirname+'/foo-data/foo_red/'+foo_red.id+'.json')
console.log('entity:'+foo_red+', file contents:'+fc)
do_bar_green(this)
})

function do_bar_green(seneca) {
seneca
.make$('bar/green',{b:2})
.save$(function(err,bar_green){
if(err) return console.error('bar_green',err);
var fc = fs.readFileSync(__dirname+'/bar-data/bar_green/'+
bar_green.id+'.json')
console.log('entity:'+bar_green+', file contents:'+fc)
do_zed_blue(seneca)
})
}

function do_zed_blue(seneca) {
seneca
.make$('zed/blue',{c:3})
.save$(function(err,zed_blue){
if(err) return console.error('zed_blue',err);
zed_blue.native$(function(err,level){
if(err) return console.error('zed_blue.native$',err);
level.get(zed_blue.id,function(err,data){
console.log('entity:'+zed_blue+' level data:'+util.inspect(data))
do_qaz_yellow(seneca)
})
})
})
}

function do_qaz_yellow(seneca) {
seneca
.make$('qaz/yellow',{d:4})
.save$(function(err,qaz_yellow){
if(err) return console.error('qaz_yellow',err);
qaz_yellow.native$(function(err,entmap){
if(err) return console.error('qaz_yellow.native$',err);
var dc = entmap.qaz.yellow[qaz_yellow.id]
console.log('entity:'+qaz_yellow+
', data contents:'+util.inspect(dc))
})
})
}

8 changes: 4 additions & 4 deletions data-entities/package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"name": "data-entities-seneca-example",
"version": "0.0.3",
"version": "0.1.0",
"description": "Data entities example",
"subdomain": "data-entities-seneca-example",
"main": "main.js",
"repository": "",
"author": "Richard Rodger",
"license": "MIT",
"dependencies": {
"seneca": "~0.5.21",
"seneca-level-store": "~0.1.3",
"seneca-jsonfile-store": "~0.1.9"
"seneca": "plugin",
"seneca-level-store": "~0.2.1",
"seneca-jsonfile-store": "~0.2.2"
}
}
3 changes: 2 additions & 1 deletion micro-services/test/offer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ seneca.act('role:offer,cmd:provide,nick:foo',function(err,out){
})



/*
var foo = require('./foo.js')
seneca.add('mod:foo',foo)
seneca.act('mod:foo,bar:1',console.log)
*/

0 comments on commit f0d86d7

Please sign in to comment.