mirror of
https://github.com/bigchaindb/js-bigchaindb-driver.git
synced 2024-11-22 09:46:58 +01:00
Small fix query-all-example
This commit is contained in:
parent
1aa295830d
commit
10fab5323a
@ -1,12 +1,13 @@
|
|||||||
{
|
{
|
||||||
"name": "js-driver-bigchaindb-examples",
|
"name": "js-driver-bigchaindb-examples",
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"main": "src/app.js",
|
"main": "src/basic-usage.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "nodemon src/app.js --exec babel-node",
|
|
||||||
"build": "npm run clean && babel src -d dist",
|
"build": "npm run clean && babel src -d dist",
|
||||||
"serve": "node dist/app.js",
|
"serve": "node dist/basic-usage.js",
|
||||||
"clean": "rimraf ./dist"
|
"clean": "rimraf ./dist",
|
||||||
|
"start": "nodemon src/basic-usage.js --exec babel-node",
|
||||||
|
"query-assets": "nodemon src/query-assets.js --exec babel-node"
|
||||||
},
|
},
|
||||||
"author": "BigchainDB",
|
"author": "BigchainDB",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
// const driver = require('../../../src/index')
|
|
||||||
const driver = require('bigchaindb-driver')
|
const driver = require('bigchaindb-driver')
|
||||||
|
|
||||||
|
|
||||||
// ======== Preparation ======== //
|
// ======== Preparation ======== //
|
||||||
const conn = new driver.Connection('https://test.bigchaindb.com/api/v1/', {
|
const conn = new driver.Connection('https://test.bigchaindb.com/api/v1/', {
|
||||||
app_id: 'c17a9968',
|
app_id: 'c17a9968',
|
||||||
@ -19,6 +19,7 @@ const assetdata = {
|
|||||||
|
|
||||||
const metadata = { 'planet': 'earth' }
|
const metadata = { 'planet': 'earth' }
|
||||||
|
|
||||||
|
|
||||||
// ======== Create Transaction Bicycle ======== //
|
// ======== Create Transaction Bicycle ======== //
|
||||||
const txCreateAliceSimple = driver.Transaction.makeCreateTransaction(
|
const txCreateAliceSimple = driver.Transaction.makeCreateTransaction(
|
||||||
assetdata,
|
assetdata,
|
||||||
@ -32,11 +33,13 @@ const txCreateAliceSimple = driver.Transaction.makeCreateTransaction(
|
|||||||
const txCreateAliceSimpleSigned =
|
const txCreateAliceSimpleSigned =
|
||||||
driver.Transaction.signTransaction(txCreateAliceSimple, alice.privateKey)
|
driver.Transaction.signTransaction(txCreateAliceSimple, alice.privateKey)
|
||||||
|
|
||||||
|
|
||||||
// ======== Post Transaction and Fetch Result ======== //
|
// ======== Post Transaction and Fetch Result ======== //
|
||||||
conn.postTransaction(txCreateAliceSimpleSigned)
|
conn.postTransaction(txCreateAliceSimpleSigned)
|
||||||
// Check status of transaction every 0.5 seconds until fulfilled
|
// Check status of transaction every 0.5 seconds until fulfilled
|
||||||
.then(() => conn.pollStatusAndFetchTransaction(txCreateAliceSimpleSigned.id))
|
.then(() => conn.pollStatusAndFetchTransaction(txCreateAliceSimpleSigned.id))
|
||||||
|
|
||||||
|
|
||||||
// ======== Transfer Bicycle to Bob ======== //
|
// ======== Transfer Bicycle to Bob ======== //
|
||||||
.then(() => {
|
.then(() => {
|
||||||
const txTransferBob = driver.Transaction.makeTransferTransaction(
|
const txTransferBob = driver.Transaction.makeTransferTransaction(
|
||||||
@ -56,6 +59,7 @@ conn.postTransaction(txCreateAliceSimpleSigned)
|
|||||||
console.log('Was Alice the previous owner?', tx.inputs[0].owners_before[0] === alice.publicKey) // eslint-disable-line no-console
|
console.log('Was Alice the previous owner?', tx.inputs[0].owners_before[0] === alice.publicKey) // eslint-disable-line no-console
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
// ======== Search Asset by Serial Number ======== //
|
// ======== Search Asset by Serial Number ======== //
|
||||||
.then(() => conn.searchAssets('Bicycle Inc.'))
|
.then(() => conn.searchAssets('Bicycle Inc.'))
|
||||||
.then(assets => console.log('Found assets with serial number Bicycle Inc.:', assets)) // eslint-disable-line no-console
|
.then(assets => console.log('Found assets with serial number Bicycle Inc.:', assets)) // eslint-disable-line no-console
|
50
examples/basic-usage-boilerplate/src/query-assets.js
Normal file
50
examples/basic-usage-boilerplate/src/query-assets.js
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
const driver = require('bigchaindb-driver')
|
||||||
|
|
||||||
|
|
||||||
|
// ======== Preparation ======== //
|
||||||
|
const conn = new driver.Connection('https://test.bigchaindb.com/api/v1/', {
|
||||||
|
app_id: 'c17a9968',
|
||||||
|
app_key: '0b277b94893e7b0a5b4e6afd6bccb01d'
|
||||||
|
})
|
||||||
|
|
||||||
|
const alice = new driver.Ed25519Keypair()
|
||||||
|
|
||||||
|
|
||||||
|
// ======== Asset Array ======== //
|
||||||
|
const assetArray = []
|
||||||
|
assetArray.push({ 'bicycle': { 'serial_number': 'abc', 'manufacturer': 'BicyclesInc' } })
|
||||||
|
assetArray.push({ 'bicycle': { 'serial_number': 'cde', 'manufacturer': 'BicyclesInc' } })
|
||||||
|
assetArray.push({ 'bicycle': { 'serial_number': 'fgh', 'manufacturer': 'BicyclesInc' } })
|
||||||
|
|
||||||
|
const metadata = { 'planet': 'Pluto' }
|
||||||
|
|
||||||
|
|
||||||
|
// ======== Create Transactions for bicycles ======== //
|
||||||
|
function createTx(assetdata) {
|
||||||
|
const txCreate = driver.Transaction.makeCreateTransaction(
|
||||||
|
assetdata,
|
||||||
|
metadata,
|
||||||
|
[
|
||||||
|
driver.Transaction.makeOutput(driver.Transaction.makeEd25519Condition(alice.publicKey))
|
||||||
|
],
|
||||||
|
alice.publicKey
|
||||||
|
)
|
||||||
|
|
||||||
|
const txCreateSigned = driver.Transaction.signTransaction(txCreate, alice.privateKey)
|
||||||
|
return conn.postTransaction(txCreateSigned)
|
||||||
|
.then(() => conn.pollStatusAndFetchTransaction(txCreateSigned.id))
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// ======== Execute all promises in order to post transactions and fetch them ======== //
|
||||||
|
Promise.all(assetArray.map(createTx))
|
||||||
|
|
||||||
|
|
||||||
|
// ======== Querying Assets for Assetdata ======== //
|
||||||
|
.then(() => conn.searchAssets('BicyclesInc'))
|
||||||
|
.then(assets => console.log('Found assets with serial number "BicyclesInc":', assets)) // eslint-disable-line no-console
|
||||||
|
|
||||||
|
|
||||||
|
// ======== Querying Assets for Metadata ======== //
|
||||||
|
.then(() => conn.searchMetadata('Pluto'))
|
||||||
|
.then(assets => console.log('Found assets with metadata "Pluto":', assets)) // eslint-disable-line no-console
|
Loading…
Reference in New Issue
Block a user