Execute Gun.js API via structured data.
A structured data API is useful for clients who don't have direct access to the gun
instance or the Javascript environment.
The "raison d'être" was an attempt to use Gun from Elm, a foreign client environment. With gun exec we can do:
let execute = createExec(Gun(), {logging: true})
app.ports.gunCommander.subscribe(function (command) {
console.log('gun command from elm', command)
// execute command via gun-exec
execute(command)
})
npm: npm i -S gun-exec
(soon)
yarn: yarn add gun-exec
(soon)
from github: npm i -S kristianmandrup/gun-exec
gun
v. 0.6 or higherAssumes Babel.js or similar transpiler setup
import Gun from 'gun/gun'
import createExec from 'gun-exec'
const gun = Gun();
let execute = createExec(gun, {
// default options for each execute context
logging: true
})
// equivalent to: gun.get('colors').put({color: 'blue'})
execute(gun, [{
get: 'colors'
}, {
put: {
color: 'blue'
}
}])
let cols = execute(gun, {
get: 'colors'
})
// ...
Using structured commands in the form name
and args
execute(gun, [{
name: 'get',
args: 'colors'
}, {
name: 'val',
args: [cb]
}])
For mutations such as set
and put
you can use explicit model
option
execute(gun, [{
name: 'get',
args: 'colors'
}, {
name: 'put',
model: myColors
}])
You can mix and match these command declaration variants as you like
Special options:
root: true
: resets the chain context to the gun instance. let blue = execute(gun, [{
get: 'kris'
}, {
put: {
name: 'kris',
role: 'developer'
},
}, {
get: 'kris',
root: true
}, {
val: cbEnd
}], {
// override options for this execution context
logging: false
})
Here we override the logging setting of this execute
context via logging: false
option in the final (optional) argument.
An Elm example app can be found in examples/user
Install dependency modules/packages
npm i
The project includes a gulpfile
configured to use Babel 6.
All /src
files are compiled to /dist
including source maps.
Scripts:
npm start
npm run build
(ie. compile)npm run watch
npm run watch:b
npm test
or simply ava test
MIT Kristian Mandrup