Create a Chaincode Project with the Blockchain App Builder CLI
To create a chaincode project when using the Blockchain App Builder CLI, you scaffold a chaincode project from a detailed specification file. The generated project then contains all the files than you need.
Background
init
command initializes and scaffolds a
ready-to-use chaincode project. Based on simple input, the init
command can generate complex chaincode projects that include the following
features:
- Multiple assets (models) and their behaviors (controllers)
- Auto-generate CRUD (Create/Read/Update/Delete) and non-CRUD methods
- Automatic validation of arguments
- Marshalling/unmarshalling of arguments
- Transparent persistence capability (ORM)
- The ability to call rich queries
The generated project follows the model/controller and decorator pattern, which allows an asset's properties that are maintained on the ledger to be specified as typed fields and extended with specific behaviors and validation rules. This pattern reduces the number of lines of code, which helps in readability and scalability.
Prerequisites
Before you scaffold a project, you must create an input specification file. For more information, see Input Specification File.
Scaffolding the Chaincode Project with the init
Command
ochain init -h
will list the command usage with all its
options. The init
command has the following options:
- --cc/-c:
The name of the chaincode project. The default value is
MyChaincode
. - --lang/-l
The language of the scaffolded chaincode. Blockchain App Builder supports Typescript (
ts
) and Go (go
). If no option is specified, the language defaults tots
. - --conf/-f or --spec
The path to an input specification file. Blockchain App Builder reads the input specification file and generates the scaffolded project with many helper tools, which help in reducing the overall development effort. Taking full advantage of the input specification file can significantly reduce the development time.
The specification file can be in
yaml
orjson
format. If the path is not specified, it defaults to the current directory. See Input Specification File. - --out/-o
The output directory of the scaffolded chaincode project. If not specified, the scaffolded project is generated in the current directory.
The output is a fully contained, lightweight, and scalable Typescript or Go chaincode project.
- --root/-r
Valid and required only for Go. The root directory in the GOHOME variable for your Go chaincodes. The default value is
example.com
.
my-mac:~ name$ ochain init --cc MyNewTsProject --lang ts --conf spec.yml
Defaults
If no options are specified in the ochain init
command, the name of
the scaffolded project is MyChaincode
and the language is
TypeScript.
The MyChaincode.model.ts
file contains only one asset, called
MyAsset
, with one property named value
. The
MyChaincode.controller.ts
file contains one controller with the
corresponding CRUD methods for the MyAsset
model.
Output