Skip to content

Commit

Permalink
Merge pull request #1 from bigchaindb/fix/readme-and-code-style
Browse files Browse the repository at this point in the history
Update README.md, add tests, code style fixes
  • Loading branch information
jernejpregelj authored Aug 28, 2017
2 parents 0bf7416 + 4769e5b commit e075863
Show file tree
Hide file tree
Showing 6 changed files with 284 additions and 119 deletions.
32 changes: 30 additions & 2 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,31 @@
{
"presets": ["es2015"]
}
"presets": [
"@ava/stage-4",
"@ava/transform-test-files",
"es2015-no-commonjs"
],

"plugins": [
"transform-export-extensions",
"transform-object-assign",
"transform-object-rest-spread"
],
"sourceMaps": true,

"env": {
"bundle": {
"plugins": [
["transform-runtime", {
"polyfill": true,
"regenerator": false
}]
]
},
"cjs": {
"plugins": [
"add-module-exports",
"transform-es2015-modules-commonjs"
]
}
}
}
176 changes: 99 additions & 77 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,24 @@

> A CRAB-based ORM for BigchainDB.
CRAB is the CRUD model in databases applied to blockchains:

| Database | Blockchain |
| ------------ | ----------- |
| **C**reate | **C**reate |
| **R**eceive | **R**eceive |
| **U**pdate | **A**ppend |
| **D**elete | **B**urn |

## Table of Contents

- [Setup](#setup)
- [Usage](#usage)
- [Examples](#examples)
- [Create a transaction](#create-a-transaction)
- [Receive a transaction](#receive-a-transaction)
- [Append a transaction](#append-a-transaction)
- [Burn a transaction](#burn-a-transaction)
- [Create an asset](#example-create-an-asset)
- [Receive assets](#example-receive-assets)
- [Append a transaction](#example-append-a-transaction)
- [Burn an asset](#example-burn-an-asset)
- [License](#license)

## Setup
Expand All @@ -21,95 +30,108 @@ $ npm install bigchaindb-orm

## Usage

```
```javascript
// import bigchaindb-orm
import * as Orm from 'bigchaindb-orm'
// connect to bigchaindb
const bdbOrm = new Orm("https://test.ipdb.io/api/v1/",{
app_id: "Get one from developers.ipdb.io",
app_key: "Same as app_id"
})
// connect to BigchainDB
const bdbOrm = new Orm(
"https://test.ipdb.io/api/v1/",
{
app_id: "Get one from developers.ipdb.io",
app_key: "Same as app_id"
}
)
// define our models and assets
bdbOrm.define("crab","https://example.com/v1/crab")
bdbOrm.define("myModel", "https://schema.org/v1/myModel")
// create a public and private key for Alice
const aliceKeypair = new driver.Ed25519Keypair()
```

## Examples

All examples need bdbOrm initialized as described in usage

### Example: Create a transaction

```
// create public and private key for Alice
const aliceKeypair = new driver.Ed25519Keypair()
// from defined models in our bdbOrm
// we create crab with Alice as owner
bdbOrm.crab.create({
keypair: aliceKeypair,
metadata: {key:'metavalue'}
}).then((crab)=>{
// crab is object with all our data and functions
// crab.id is id of crab
// crab.metadata is metadata of last transaction
// crab.transactionList is transaction history
console.log(crab.id)
}
### Example: Create an asset

```javascript
// from the defined models in our bdbOrm we create an asset with Alice as owner
bdbOrm.myModel
.create({
keypair: aliceKeypair,
metadata: { key: 'metadataValue' }
})
.then(asset => {
/*
asset is an object with all our data and functions
asset.id equals the id of the asset
asset.metadata is metadata of the last (unspent) transaction
asset.transactionList gives the full transaction history
*/
console.log(asset.id)
})
```

### Example: Receive a transaction

```
// get all crabs with retrieve()
// or get specific crab with retrieve("crabid")
bdbOrm.crab.retrieve().then((crabs)=>{
// output is array of crabs
// lets output ids of our crabs
console.log(crabs.map(crab=>{return crab.id}))
})
### Example: Receive assets

```javascript
// get all objects with retrieve()
// or get a specific object with retrieve(object.id)
bdbOrm.myModel
.retrieve()
.then(assets => {
// assets is an array of myModel
console.log(assets.map(asset => asset.id))
})
```

### Example: Append a transaction

```
// create public and private key for Alice
const aliceKeypair = new driver.Ed25519Keypair()
// create crab with Alice as owner
bdbOrm.crab.create({
keypair: aliceKeypair,
metadata: {key:'metavalue'}
}).then((crab)=>{
// lets append metadata of our crab
crab.append({
toKeypair: aliceKeypair.publicKey,
keypair: aliceKeypair,
metadata: {key:'newvalue'}
}).then((appendedCrab)=>{
// appendedCrab is last state
// of our crab so any actions
// need to be done to appendedCrab
console.log(appendedCrab.metadata)
})
})
> "Update" (Database) => "Append" (Blockchain)
```javascript
// create an asset with Alice as owner
bdbOrm.myModel
.create({
keypair: aliceKeypair,
metadata: { key: 'metadataValue' }
})
.then(asset => {
// lets append update the metadata of our asset
// since we use a blockchain, we can only append
return asset.append({
toPublicKey: aliceKeypair.publicKey,
keypair: aliceKeypair,
metadata: { key: 'updatedValue' }
})
})
.then(updatedAsset => {
// updatedAsset contains the last (unspent) state
// of our asset so any actions
// need to be done to updatedAsset
console.log(updatedAsset.metadata)
})
```

### Example: Burn a transaction

```
// create public and private key for Alice
const aliceKeypair = new driver.Ed25519Keypair()
// create crab with Alice as owner
bdbOrm.crab.create({
keypair: aliceKeypair,
metadata: {key:'metavalue'}
}).then((crab)=>{
// lets append metadata of our crab
crab.burn({
keypair: aliceKeypair
}).then((burnedCrab)=>{
// crab burned sent to away
console.log(burnedCrab.metadata)
})
})
### Example: Burn an asset
> "Delete" (Database) => "Burn" (Blockchain)
```javascript
// create an asset with Alice as owner
bdbOrm.myModel
.create({
keypair: aliceKeypair,
metadata: { key: 'metadataValue' }
})
.then(asset => {
// lets burn the asset by assigning to a random keypair
// since will not store the private key it's infeasible to redeem the asset
return asset.burn({
keypair: aliceKeypair
})
})
.then(burnedAsset => {
// asset is now tagged as "burned"
console.log(burnedAsset.metadata)
})
```

## License
Expand Down
37 changes: 34 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,27 @@
"uuid": "^3.1.0"
},
"devDependencies": {
"ava": "^0.22.0",
"babel-cli": "^6.24.1",
"babel-eslint": "^7.2.3",
"babel-loader": "^7.1.1",
"babel-plugin-add-module-exports": "^0.2.1",
"babel-plugin-transform-es2015-modules-commonjs": "^6.26.0",
"babel-plugin-transform-export-extensions": "^6.22.0",
"babel-plugin-transform-object-assign": "^6.22.0",
"babel-plugin-transform-object-rest-spread": "^6.26.0",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-es2015-no-commonjs": "^0.0.2",
"babel-runtime": "^6.26.0",
"cross-env": "^5.0.3",
"eslint": "^4.3.0",
"eslint-config-ascribe": "^3.0.4",
"eslint-plugin-import": "^2.7.0",
"nyc": "^11.1.0",
"release-it": "^2.7.3",
"rimraf": "^2.5.4",
"sinon": "^3.2.1",
"webpack": "^3.5.2"
},
"scripts": {
Expand All @@ -36,17 +47,37 @@
"build:cjs": "cross-env BABEL_ENV=cjs babel ./src -d dist/node",
"build:dist": "cross-env NODE_ENV=production webpack -p",
"clean": "rimraf dist/bundle dist/node",
"test": "npm run lint && ./node_modules/babel-cli/bin/babel-node.js ./test/queries.js",
"test": "npm run lint && nyc ava test/ && npm run thanks && npm run report-coverage",
"thanks": "cowsay Hi, thanks for your interest in BigchainDB. We appreciate your contribution!",
"prepublishOnly": "npm update && npm run build",
"release": "./node_modules/release-it/bin/release.js --src.tagName='v%s' --github.release --npm.publish --non-interactive",
"release-minor": "./node_modules/release-it/bin/release.js minor --src.tagName='v%s' --github.release --npm.publish --non-interactive",
"release-major": "./node_modules/release-it/bin/release.js major --src.tagName='v%s' --github.release --npm.publish --non-interactive"
"release-major": "./node_modules/release-it/bin/release.js major --src.tagName='v%s' --github.release --npm.publish --non-interactive",
"report-coverage": "nyc report --reporter=lcov > coverage.lcov && codecov"
},
"keywords": [
"bigchaindb",
"driver",
"blockchain",
"decentralized",
"orm"
]
],
"ava": {
"files": [
"test/*.js"
],
"source": [
"**/*.{js,jsx}",
"!node_modules/**/*",
"!dist/**/*"
],
"failFast": true,
"failWithoutAssertions": false,
"tap": true,
"powerAssert": false,
"require": [
"babel-register"
],
"babel": "inherit"
}
}
3 changes: 1 addition & 2 deletions src/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default class Connection {
this.conn = new driver.Connection(path, headers)
}

getAssetId(tx) {
getAssetId(tx) { // eslint-disable-line class-methods-use-this
return tx.operation === 'CREATE' ? tx.id : tx.asset.id
}

Expand Down Expand Up @@ -131,5 +131,4 @@ export default class Connection {
return txList
})
}

}
Loading

0 comments on commit e075863

Please sign in to comment.