Creating Composites
Composites are the abstraction ComposeDB tools and client use to represent and manipulate data models used by applications.
In order to create new composites, especially to create new models that do not already exist on the Ceramic network, a high-level schema can be used as an abstraction for the composite structure.
Composite schema
The recommended way to create composites including new models is to use a schema file based on GraphQL, described in the dedicated documentation page.
Converting a schema to a composite
After a schema file has been created, it must be converted to a composite to be usable by the other tools and client.
- Using the CLI
- Using the API
Make sure you have installed the packages imported in the code below before running the script.
import { CeramicClient } from '@ceramicnetwork/http-client'
import { createComposite, writeEncodedComposite } from '@composedb/devtools-node'
import { DID } from 'dids'
import { Ed25519Provider } from 'key-did-provider-ed25519'
import { getResolver } from 'key-did-resolver'
import { fromString } from 'uint8arrays/from-string'
// Hexadecimal-encoded private key for a DID having admin access to the target Ceramic node
// Replace the example key here by your admin private key
const privateKey = fromString('b0cb[...]515f', 'base16')
const did = new DID({
resolver: getResolver(),
provider: new Ed25519Provider(privateKey),
})
await did.authenticate()
// Replace by the URL of the Ceramic node you want to deploy the Models to
const ceramic = new CeramicClient('http://localhost:7007')
// An authenticated DID with admin access must be set on the Ceramic instance
ceramic.did = did
// Replace by the path to the source schema file
const composite = await createComposite(ceramic, 'source-schema.graphql')
// Replace by the path to the encoded composite file
await writeEncodedComposite(composite, 'my-composite.json')
More details: createComposite
, writeEncodedComposite
Running this command requires an admin DID private key to be
provided, using the DID_PRIVATE_KEY
environment variable or the --did-private-key
flag.
composedb composite:create source-schema.graphql --output=my-composite.json