-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
102 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,5 +18,4 @@ node_modules | |
|
||
config.mine.js | ||
|
||
data-entities/json-data | ||
data-entities/level-data | ||
data-entities/*-data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
}) | ||
}) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters