blend-state-dynamo
Advanced tools
Comparing version 1.0.2 to 2.0.1
'use strict' | ||
const vstamp = require('vigour-stamp') | ||
// var lek // { exclusiveStartKey: lek } for large sets (latest ending key) | ||
module.exports = function (done) { | ||
module.exports = function (context, done) { | ||
if (typeof context === 'function') { | ||
done = context | ||
} else { | ||
context = String(context) | ||
} | ||
const db = this | ||
const state = this.root | ||
// var lek // { exclusiveStartKey: lek } for large sets | ||
if (!context) { | ||
context === false | ||
} | ||
if (context === '*') { | ||
all(db, state, done) | ||
} else { | ||
filter(context, db, state, done) | ||
} | ||
} | ||
function parse (val) { | ||
for (let field in val) { | ||
for (let key in val[field]) { | ||
val[field] = val[field][key] | ||
} | ||
} | ||
return val | ||
} | ||
function all (db, state, done) { | ||
db.hasTable.is(true, () => { | ||
@@ -21,3 +46,3 @@ db.db.scan({ | ||
vstamp.close(stamp) | ||
done() | ||
if (done) { done() } | ||
} | ||
@@ -28,9 +53,25 @@ }) | ||
function parse (val) { | ||
for (let field in val) { | ||
for (let key in val[field]) { | ||
val[field] = val[field][key] | ||
} | ||
} | ||
return val | ||
function filter (context, db, state, done) { | ||
db.hasTable.is(true, () => { | ||
db.db.query({ | ||
TableName: db.table.compute(), | ||
IndexName: 'context', | ||
'KeyConditionExpression': 'context = :v_title', | ||
'ExpressionAttributeValues': { | ||
':v_title': { 'S': context } // not only string ofcourse... | ||
} | ||
}, (err, result) => { | ||
if (err) { | ||
state.emit('error', err) | ||
} else { | ||
const items = result.Items | ||
const stamp = vstamp.create('db') | ||
for (var key in items) { | ||
state.set(db.in(parse(items[key])), stamp) | ||
} | ||
vstamp.close(stamp) | ||
if (done) { done() } | ||
} | ||
}) | ||
}) | ||
} |
@@ -8,5 +8,5 @@ 'use strict' | ||
if (val.val && typeof val.val === 'object' && val.val.isBase) { | ||
val.val = db.out(val.val).key.length ? '$root.' + db.out(val.val).key : '$root' | ||
val.val = db.out(val.val).key.length ? '$root.' + db.out(val.val).key.join('.') : '$root' | ||
} | ||
queue[val.key + '_' + val.context] = val | ||
queue[val.key.join('.')] = val | ||
if (!inProgress) { | ||
@@ -36,3 +36,3 @@ db.hasTable.is(true, () => { | ||
queue[i].val = type(queue[i].val) | ||
queue[i].context = type(queue[i].context) | ||
queue[i].context = { S: String(queue[i].context) } | ||
items.push({ PutRequest: { Item: queue[i] } }) | ||
@@ -39,0 +39,0 @@ } |
@@ -9,5 +9,11 @@ 'use strict' | ||
const vstamp = require('vigour-stamp') | ||
const is = require('vigour-is') | ||
exports.properties = { | ||
db: { | ||
type: 'observable', | ||
child: { | ||
inject: is, | ||
child: 'Constructor' | ||
}, | ||
define: { load }, | ||
@@ -20,4 +26,11 @@ properties: { | ||
hasTable: false, | ||
// @todo: default behevaiour for state that has context fields (hubs) | ||
in (val) { | ||
const obj = {} | ||
val.key = val.key.split('.') | ||
if (val.key[0] === 'no-context') { | ||
val.key.shift() | ||
} | ||
set(obj, val.key, { val: val.val, stamp: val.stamp }) | ||
@@ -27,8 +40,13 @@ return obj | ||
out (state, context, key, val, stamp) { | ||
return { | ||
context: context || this.root.get('context', false).compute(), | ||
key: key || state.path().join('.'), | ||
context = context || this.root.context && this.root.context.compute() || 'no-context' | ||
context = typeof context === 'object' ? 'no-context' : String(context) | ||
var path = state.path() | ||
path.unshift(context) | ||
const obj = { | ||
context, | ||
key: path, | ||
stamp: stamp || state.stamp, | ||
val: val || state.val | ||
} | ||
return obj | ||
}, | ||
@@ -35,0 +53,0 @@ id: { |
{ | ||
"name": "blend-state-dynamo", | ||
"version": "1.0.2", | ||
"version": "2.0.1", | ||
"description": "Amazon dynamodb integration for vigour-state", | ||
@@ -32,2 +32,3 @@ "engines": { | ||
"dependencies": { | ||
"vigour-is": "^2.0.0", | ||
"vigour-stamp": "^1.3.6", | ||
@@ -54,3 +55,3 @@ "aws-sdk": "^2.4.10", | ||
"tape": "^4.4.0", | ||
"vigour-state": "^4.0.0", | ||
"vigour-state": "^4.4.4", | ||
"tap-difflet": "0.4.0", | ||
@@ -57,0 +58,0 @@ "vigour-performance": "^1.3.0", |
@@ -10,5 +10,4 @@ # blend-state-dynamo | ||
```javascript | ||
state.inject(require('blend-state-dynamo')) | ||
state.set({ | ||
inject: require('blend-state-dynamo') | ||
db: { | ||
@@ -25,6 +24,22 @@ id: AMAZON_ID, | ||
}) | ||
state.db.hasTable.is(true).then(() => { | ||
t.ok(true, 'hasTable') | ||
t.end() | ||
console.log('got a table!') | ||
}) | ||
``` | ||
**Loading data** | ||
```javascript | ||
state.set({ | ||
inject: require('blend-state-dynamo'), | ||
db: { | ||
id: AMAZON_ID, | ||
secret: AMAZON_SECRET, | ||
table: testTable | ||
} | ||
}) | ||
state.db.load() // loads all data without context | ||
state.db.load('somecontextid') // loads all data with somecontextid | ||
state.db.load('*') //loads all data -- warn maybe very heavy | ||
``` |
'use strict' | ||
const State = require('vigour-state') | ||
const test = require('tape') | ||
const testTable = 'blend-state-dyanamo-test' | ||
const state = new State({ inject: require('../') }) | ||
const AMAZON_ID = process.env.AMAZON_ID | ||
const AMAZON_SECRET = process.env.AMAZON_SECRET | ||
function out (state) { | ||
const path = state.path() | ||
const context = path[0] | ||
path.shift() | ||
return { | ||
key: path, | ||
context: context, | ||
val: state.val, | ||
stamp: state.stamp | ||
} | ||
} | ||
test('initialize, create table', (t) => { | ||
state.set({ | ||
db: { | ||
id: AMAZON_ID, | ||
secret: AMAZON_SECRET, | ||
table: testTable, | ||
out | ||
}, | ||
on: { | ||
error (err) { | ||
console.log(err) | ||
} | ||
} | ||
}) | ||
state.db.hasTable.is(true).then(() => { | ||
t.ok(true, 'hasTable') | ||
t.end() | ||
}) | ||
}) | ||
test('set a field', (t) => { | ||
state.set({ | ||
token: { | ||
field: 'bye', | ||
other: '$root.token.bla', | ||
deeper: { hello: true } | ||
} | ||
}) | ||
state.set({ | ||
token: { | ||
field: 'hello' | ||
} | ||
}) | ||
state.on('error', () => t.fail('throws error')) | ||
t.ok(true, 'does not throw error') | ||
t.end() | ||
}) | ||
test('load a table', (t) => { | ||
const state = new State({ inject: require('../') }) | ||
// loads whole table | ||
state.set({ | ||
db: { | ||
id: AMAZON_ID, | ||
secret: AMAZON_SECRET, | ||
table: testTable, | ||
out, | ||
define: { | ||
extend: { | ||
in (parse, val) { | ||
val.key = val.context + '.' + val.key | ||
return parse(val) | ||
} | ||
} | ||
} | ||
}, | ||
on: { | ||
error (err) { | ||
console.log(err) | ||
} | ||
} | ||
}) | ||
state.db.load(() => { | ||
t.same(state.token.deeper.hello.val, true, 'returns correct data') | ||
t.end() | ||
}) | ||
}) | ||
require('./init') | ||
require('./load') |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17372
19
531
44
6
4
+ Addedvigour-is@^2.0.0
+ Addedbuble-promisify@1.0.0(transitive)
+ Addedcore-util-is@1.0.3(transitive)
+ Addedprocess-nextick-args@2.0.1(transitive)
+ Addedpromise-polyfill@6.1.0(transitive)
+ Addedreadable-stream@2.3.8(transitive)
+ Addedsafe-buffer@5.1.2(transitive)
+ Addedstring_decoder@1.1.1(transitive)
+ Addedthrough2@2.0.5(transitive)
+ Addedutil-deprecate@1.0.2(transitive)
+ Addedvigour-is@2.3.3(transitive)
+ Addedxtend@4.0.2(transitive)