@glazed/devtools
Advanced tools
Comparing version 0.2.0-alpha.4 to 0.2.0-alpha.5
@@ -5,3 +5,3 @@ import type { CeramicApi, CreateOpts, StreamMetadata, UpdateOpts } from '@ceramicnetwork/common'; | ||
import type { Definition } from '@glazed/did-datastore-model'; | ||
import type { EncodedManagedModel, ManagedEntry, ManagedID, ManagedModel, ManagedSchema, PublishedModel, Schema } from '@glazed/types'; | ||
import type { EncodedManagedModel, ManagedEntry, ManagedID, ManagedModel, ManagedSchema, ModelAliases, Schema } from '@glazed/types'; | ||
import type { DagJWSResult } from 'dids'; | ||
@@ -13,3 +13,3 @@ declare type CreateContentType = { | ||
}; | ||
declare type UsePublishedIDType = { | ||
declare type UseDeployedIDType = { | ||
definition: StreamID | string; | ||
@@ -19,5 +19,13 @@ schema: StreamRef | string; | ||
}; | ||
export declare function publishDataStoreSchemas(ceramic: CeramicApi, createOpts?: CreateOpts, commitOpts?: UpdateOpts): Promise<void>; | ||
export declare function publishModel(ceramic: CeramicApi, model: ManagedModel, createOpts?: CreateOpts, commitOpts?: UpdateOpts): Promise<PublishedModel>; | ||
export declare function publishEncodedModel(ceramic: CeramicApi, model: EncodedManagedModel): Promise<PublishedModel>; | ||
export declare function deployDataStoreSchemas(ceramic: CeramicApi, createOpts?: CreateOpts, commitOpts?: UpdateOpts): Promise<void>; | ||
export declare function deployModel(ceramic: CeramicApi, model: ManagedModel, createOpts?: CreateOpts, commitOpts?: UpdateOpts): Promise<ModelAliases>; | ||
export declare function deployEncodedModel(ceramic: CeramicApi, model: EncodedManagedModel): Promise<ModelAliases>; | ||
export declare type ModelManagerConfig = { | ||
ceramic: CeramicApi; | ||
model?: ManagedModel; | ||
}; | ||
export declare type FromJSONParams = { | ||
ceramic: CeramicApi; | ||
model: EncodedManagedModel; | ||
}; | ||
/** | ||
@@ -30,4 +38,4 @@ * ```sh | ||
#private; | ||
static fromJSON(ceramic: CeramicApi, encoded: EncodedManagedModel): ModelManager; | ||
constructor(ceramic: CeramicApi, model?: ManagedModel); | ||
static fromJSON(params: FromJSONParams): ModelManager; | ||
constructor(config: ModelManagerConfig); | ||
get model(): ManagedModel; | ||
@@ -47,3 +55,3 @@ get schemas(): Array<string>; | ||
create<T extends keyof CreateContentType, Content = CreateContentType[T]>(type: T, alias: string, content: Content, meta?: Partial<StreamMetadata>): Promise<ManagedID>; | ||
usePublished<T extends keyof UsePublishedIDType, ID = UsePublishedIDType[T]>(type: T, alias: string, id: ID): Promise<ManagedID>; | ||
useDeployed<T extends keyof UseDeployedIDType, ID = UseDeployedIDType[T]>(type: T, alias: string, id: ID): Promise<ManagedID>; | ||
getSchemaID(alias: string): ManagedID | null; | ||
@@ -55,3 +63,3 @@ hasSchemaAlias(alias: string): boolean; | ||
createSchema(alias: string, schema: Schema): Promise<ManagedID>; | ||
usePublishedSchema(alias: string, id: StreamRef | string): Promise<ManagedID>; | ||
useDeployedSchema(alias: string, id: StreamRef | string): Promise<ManagedID>; | ||
getDefinitionID(alias: string): ManagedID | null; | ||
@@ -61,3 +69,3 @@ hasDefinitionAlias(alias: string): boolean; | ||
createDefinition(alias: string, definition: Definition): Promise<ManagedID>; | ||
usePublishedDefinition(alias: string, id: StreamID | string): Promise<ManagedID>; | ||
useDeployedDefinition(alias: string, id: StreamID | string): Promise<ManagedID>; | ||
getTileID(alias: string): ManagedID | null; | ||
@@ -67,6 +75,6 @@ hasTileAlias(alias: string): boolean; | ||
createTile<T extends Record<string, unknown>>(alias: string, contents: T, meta?: Partial<StreamMetadata>): Promise<ManagedID>; | ||
usePublishedTile(alias: string, id: StreamID | string): Promise<ManagedID>; | ||
toPublished(): Promise<PublishedModel>; | ||
useDeployedTile(alias: string, id: StreamID | string): Promise<ManagedID>; | ||
deploy(): Promise<ModelAliases>; | ||
toJSON(): EncodedManagedModel; | ||
} | ||
export {}; |
@@ -1,8 +0,1 @@ | ||
import { CommitID, StreamRef } from '@ceramicnetwork/streamid'; | ||
import { TileDocument } from '@ceramicnetwork/stream-tile'; | ||
import { CIP11_DEFINITION_SCHEMA_URL } from '@glazed/constants'; | ||
import { model as encodedDataStoreModel } from '@glazed/did-datastore-model'; | ||
import { decodeModel, encodeModel } from './encoding.js'; | ||
import { publishCommits } from './publishing.js'; | ||
import { extractSchemaReferences } from './schema.js'; | ||
function _classPrivateFieldGet(receiver, privateMap) { | ||
@@ -25,2 +18,9 @@ if (!privateMap.has(receiver)) { | ||
} | ||
import { CommitID, StreamRef } from '@ceramicnetwork/streamid'; | ||
import { TileDocument } from '@ceramicnetwork/stream-tile'; | ||
import { CIP11_DEFINITION_SCHEMA_URL } from '@glazed/constants'; | ||
import { model as encodedDataStoreModel } from '@glazed/did-datastore-model'; | ||
import { decodeModel, encodeModel } from './encoding.js'; | ||
import { publishCommits } from './publishing.js'; | ||
import { extractSchemaReferences } from './schema.js'; | ||
function getManagedIDAndVersion(id) { | ||
@@ -44,3 +44,3 @@ const streamID = typeof id === 'string' ? StreamRef.from(id) : id; | ||
const dataStoreModel = decodeModel(encodedDataStoreModel); | ||
export async function publishDataStoreSchemas(ceramic, createOpts, commitOpts) { | ||
export async function deployDataStoreSchemas(ceramic, createOpts, commitOpts) { | ||
await Promise.all(Object.values(dataStoreModel.schemas).map(async (schema)=>{ | ||
@@ -50,4 +50,4 @@ return await publishCommits(ceramic, schema.commits, createOpts, commitOpts); | ||
} | ||
// Publish a managed model to the given Ceramic node | ||
export async function publishModel(ceramic, model, createOpts, commitOpts) { | ||
// Deploy a managed model to the given Ceramic node | ||
export async function deployModel(ceramic, model, createOpts, commitOpts) { | ||
const [schemas] = await Promise.all([ | ||
@@ -61,3 +61,3 @@ Promise.all(Object.values(model.schemas).map(async (schema)=>{ | ||
})), | ||
publishDataStoreSchemas(ceramic), | ||
deployDataStoreSchemas(ceramic), | ||
]); | ||
@@ -86,5 +86,5 @@ const [definitions, tiles] = await Promise.all([ | ||
} | ||
// Publish a JSON-encoded managed model to the given Ceramic node | ||
export async function publishEncodedModel(ceramic, model) { | ||
return await publishModel(ceramic, decodeModel(model)); | ||
// Deploy a JSON-encoded managed model to the given Ceramic node | ||
export async function deployEncodedModel(ceramic, model) { | ||
return await deployModel(ceramic, decodeModel(model)); | ||
} | ||
@@ -96,4 +96,7 @@ /** | ||
*/ export class ModelManager { | ||
static fromJSON(ceramic, encoded) { | ||
return new ModelManager(ceramic, decodeModel(encoded)); | ||
static fromJSON(params) { | ||
return new ModelManager({ | ||
ceramic: params.ceramic, | ||
model: decodeModel(params.model) | ||
}); | ||
} | ||
@@ -265,10 +268,10 @@ // Getters | ||
} | ||
async usePublished(type, alias, id) { | ||
async useDeployed(type, alias, id) { | ||
switch(type){ | ||
case 'schema': | ||
return await this.usePublishedSchema(alias, id); | ||
return await this.useDeployedSchema(alias, id); | ||
case 'definition': | ||
return await this.usePublishedDefinition(alias, id); | ||
return await this.useDeployedDefinition(alias, id); | ||
case 'tile': | ||
return await this.usePublishedTile(alias, id); | ||
return await this.useDeployedTile(alias, id); | ||
default: | ||
@@ -320,3 +323,3 @@ throw new Error(`Unsupported type: ${type}`); | ||
} | ||
async usePublishedSchema(alias, id) { | ||
async useDeployedSchema(alias, id) { | ||
if (alias == null) { | ||
@@ -347,3 +350,3 @@ throw new Error('Schema alias must be provided'); | ||
} | ||
await publishDataStoreSchemas(_classPrivateFieldGet(this, _ceramic)); | ||
await deployDataStoreSchemas(_classPrivateFieldGet(this, _ceramic)); | ||
const [stream, schemaID] = await Promise.all([ | ||
@@ -365,3 +368,3 @@ this._createDoc(definition, { | ||
} | ||
async usePublishedDefinition(alias, id) { | ||
async useDeployedDefinition(alias, id) { | ||
if (this.hasDefinitionAlias(alias)) { | ||
@@ -421,3 +424,3 @@ throw new Error(`Definition ${alias} already exists`); | ||
} | ||
async usePublishedTile(alias, id) { | ||
async useDeployedTile(alias, id) { | ||
if (this.hasTileAlias(alias)) { | ||
@@ -444,4 +447,4 @@ throw new Error(`Tile ${alias} already exists`); | ||
// Exports | ||
async toPublished() { | ||
return await publishModel(_classPrivateFieldGet(this, _ceramic), _classPrivateFieldGet(this, _model)); | ||
async deploy() { | ||
return await deployModel(_classPrivateFieldGet(this, _ceramic), _classPrivateFieldGet(this, _model)); | ||
} | ||
@@ -451,3 +454,3 @@ toJSON() { | ||
} | ||
constructor(ceramic, model){ | ||
constructor(config){ | ||
_aliases.set(this, { | ||
@@ -481,5 +484,5 @@ writable: true, | ||
}); | ||
_classPrivateFieldSet(this, _ceramic, ceramic); | ||
if (model != null) { | ||
this.addModel(model); | ||
_classPrivateFieldSet(this, _ceramic, config.ceramic); | ||
if (config.model != null) { | ||
this.addModel(config.model); | ||
} | ||
@@ -486,0 +489,0 @@ } |
import type { EncodedDagJWS, EncodedDagJWSResult, ManagedEntry, ManagedModel } from '@glazed/types'; | ||
import type { DagJWS, DagJWSResult } from 'dids'; | ||
/** @internal */ | ||
export declare function decodeDagJWS({ payload, signatures, link }: EncodedDagJWS): DagJWS; | ||
/** @internal */ | ||
export declare function encodeDagJWS({ payload, signatures, link }: DagJWS): EncodedDagJWS; | ||
/** @internal */ | ||
export declare function decodeDagJWSResult({ jws, linkedBlock }: EncodedDagJWSResult): DagJWSResult; | ||
/** @internal */ | ||
export declare function encodeDagJWSResult({ jws, linkedBlock }: DagJWSResult): EncodedDagJWSResult; | ||
@@ -13,5 +17,7 @@ /** @internal */ | ||
export declare function decodeEntryCommits(entry: ManagedEntry<EncodedDagJWSResult>): ManagedEntry<DagJWSResult>; | ||
/** @internal */ | ||
export declare function decodeModel(model: ManagedModel<EncodedDagJWSResult>): ManagedModel<DagJWSResult>; | ||
/** @internal */ | ||
export declare function encodeEntryCommits(entry: ManagedEntry<DagJWSResult>): ManagedEntry<EncodedDagJWSResult>; | ||
/** @internal */ | ||
export declare function encodeModel(model: ManagedModel<DagJWSResult>): ManagedModel<EncodedDagJWSResult>; |
import { CID } from 'multiformats/cid'; | ||
import { fromString, toString } from 'uint8arrays'; | ||
import { applyMap } from './utils.js'; | ||
export function decodeDagJWS({ payload , signatures , link }) { | ||
/** @internal */ export function decodeDagJWS({ payload , signatures , link }) { | ||
return { | ||
@@ -11,3 +11,3 @@ payload, | ||
} | ||
export function encodeDagJWS({ payload , signatures , link }) { | ||
/** @internal */ export function encodeDagJWS({ payload , signatures , link }) { | ||
return { | ||
@@ -19,3 +19,3 @@ payload, | ||
} | ||
export function decodeDagJWSResult({ jws , linkedBlock }) { | ||
/** @internal */ export function decodeDagJWSResult({ jws , linkedBlock }) { | ||
return { | ||
@@ -26,3 +26,3 @@ jws: decodeDagJWS(jws), | ||
} | ||
export function encodeDagJWSResult({ jws , linkedBlock }) { | ||
/** @internal */ export function encodeDagJWSResult({ jws , linkedBlock }) { | ||
return { | ||
@@ -47,3 +47,3 @@ jws: encodeDagJWS(jws), | ||
} | ||
export function decodeModel(model) { | ||
/** @internal */ export function decodeModel(model) { | ||
return { | ||
@@ -66,3 +66,3 @@ schemas: applyMap(model.schemas, (schema)=>{ | ||
} | ||
export function encodeModel(model) { | ||
/** @internal */ export function encodeModel(model) { | ||
return { | ||
@@ -69,0 +69,0 @@ schemas: applyMap(model.schemas, (schema)=>{ |
@@ -17,2 +17,3 @@ import type { GraphQLModel, ItemField } from '@glazed/graphql-types'; | ||
export declare function addModelSchema(model: GraphQLModel, schema: Schema, options?: AddModelSchemaOptions): string; | ||
/** @internal */ | ||
export declare function createGraphQLModel(manager: ModelManager): Promise<GraphQLModel>; |
@@ -131,3 +131,3 @@ /* eslint-disable @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access */ import { CIP88_APPEND_COLLECTION_PREFIX } from '@glazed/constants'; | ||
} | ||
export async function createGraphQLModel(manager) { | ||
/** @internal */ export async function createGraphQLModel(manager) { | ||
// TODO: throw error on using reserved names: | ||
@@ -134,0 +134,0 @@ // - "node" and "index" roots |
/** | ||
* Development tools library. | ||
* | ||
* ## Purpose | ||
* | ||
* The `devtools` library provides APIs to help support common use-cases when building applications | ||
* on top of Ceramic, notably as a complement to the runtime Glaze libraries. It is meant to be | ||
* used by developers in scripts or other tools such as the CLI, not as a runtime library. | ||
* | ||
* The {@linkcode ModelManager} class notably allows developers to create, update and deploy data | ||
* models to be used with the {@linkcode datamodel.DataModel DataModel} runtime. | ||
* | ||
* ## Installation | ||
* | ||
* ```sh | ||
@@ -6,2 +19,143 @@ * npm install --dev @glazed/devtools | ||
* | ||
* ## Common use-cases | ||
* | ||
* ### Add an existing schema to a model | ||
* | ||
* An existing schema can be added using the | ||
* {@linkcode ModelManager.useDeployedSchema useDeployedSchema} method, as shown below. | ||
* | ||
* ```ts | ||
* import { CeramicClient } from '@ceramicnetwork/http-client' | ||
* import { ModelManager } from '@glazed/devtools' | ||
* | ||
* const ceramic = new CeramicClient() | ||
* const manager = new ModelManager({ ceramic }) | ||
* | ||
* // Set the alias (human-readable name) and stream reference (stream or commit ID or URL) of the | ||
* // schema to add to the model. The schema must be already present on the Ceramic node. | ||
* await manager.useDeployedSchema('MySchema', 'ceramic://k2...ab') | ||
* ``` | ||
* | ||
* The {@linkcode ModelManager.useDeployedDefinition useDeployedDefinition} and | ||
* {@linkcode ModelManager.useDeployedTile useDeployedTile} methods can be used similarly to add | ||
* definitions and tiles to the model. | ||
* | ||
* ### Create and add a schema to a model | ||
* | ||
* Using the {@linkcode ModelManager.createSchema createSchema} method allows to create the schema | ||
* on the Ceramic node and add it to the model. Note that using this method creates a new schema | ||
* every time it is called, therefore generating different stream IDs. | ||
* | ||
* ```ts | ||
* import { CeramicClient } from '@ceramicnetwork/http-client' | ||
* import { ModelManager } from '@glazed/devtools' | ||
* | ||
* const ceramic = new CeramicClient() | ||
* const manager = new ModelManager({ ceramic }) | ||
* | ||
* // Set the alias (human-readable name) and JSON schema contents | ||
* await manager.createSchema('MySchema', { | ||
* $schema: 'http://json-schema.org/draft-07/schema#', | ||
* title: 'MySchema', | ||
* type: 'object', | ||
* properties: { | ||
* ... | ||
* }, | ||
* }) | ||
* ``` | ||
* | ||
* The {@linkcode ModelManager.createDefinition createDefinition} and | ||
* {@linkcode ModelManager.createTile createTile} methods can be used similarly to add definitions | ||
* and tiles to the model. | ||
* | ||
* ### Export a model to JSON | ||
* | ||
* A managed model can be serialized to JSON, making it portable and reusable, with the | ||
* {@linkcode ModelManager.toJSON toJSON} method. | ||
* | ||
* ```ts | ||
* import { CeramicClient } from '@ceramicnetwork/http-client' | ||
* import { ModelManager } from '@glazed/devtools' | ||
* | ||
* const ceramic = new CeramicClient() | ||
* const manager = new ModelManager({ ceramic }) | ||
* | ||
* await manager.useDeployedSchema('MySchema', 'ceramic://k2...ab') | ||
* const encodedModel = await manager.toJSON() | ||
* ``` | ||
* | ||
* ### Import a model from JSON | ||
* | ||
* A managed model serialized using the {@linkcode ModelManager.toJSON toJSON} method can be | ||
* deserialized with the {@linkcode ModelManager.fromJSON fromJSON} static method. | ||
* | ||
* ```ts | ||
* import { CeramicClient } from '@ceramicnetwork/http-client' | ||
* import { ModelManager } from '@glazed/devtools' | ||
* | ||
* const ceramic = new CeramicClient() | ||
* const manager = new ModelManager({ ceramic }) | ||
* | ||
* await manager.useDeployedSchema('MySchema', 'ceramic://k2...ab') | ||
* const encodedModel = await manager.toJSON() | ||
* | ||
* // The `clonedManager` instance will contain the same model as the `manager` instance | ||
* const clonedManager = ModelManager.fromJSON(ceramic, encodedModel) | ||
* ``` | ||
* | ||
* ### Deploy a model to Ceramic | ||
* | ||
* In order to use a model at runtime in an application, it is important to ensure all the streams | ||
* used by the model are present in the Ceramic network. This can be achieved by calling the | ||
* {@linkcode ModelManager.deploy deploy} method, which returns the aliases of the model that can | ||
* be used at runtime by a {@linkcode datamodel.DataModel DataModel} instance. | ||
* | ||
* ```ts | ||
* import { readFile, writeFile } from 'node:fs/promises' | ||
* import { CeramicClient } from '@ceramicnetwork/http-client' | ||
* import { ModelManager } from '@glazed/devtools' | ||
* | ||
* // The encoded model could be imported from the file system for example | ||
* const bytes = await readFile(new URL('encoded-model.json', import.meta.url)) | ||
* const encodedModel = JSON.parse(bytes.toString()) | ||
* | ||
* const ceramic = new CeramicClient() | ||
* const manager = ModelManager.fromJSON(ceramic, encodedModel) | ||
* | ||
* // The deployed model aliases could then be exported to be used at runtime | ||
* const aliases = await manager.deploy() | ||
* await writeFile(new URL('model-aliases.json', import.meta.url), JSON.stringify(aliases)) | ||
* ``` | ||
* | ||
* ### Use existing models | ||
* | ||
* A model can be created by combining other models. For example, using the models for the | ||
* [CIP-19 "Basic Profile"](https://github.com/ceramicnetwork/CIP/blob/main/CIPs/CIP-19/CIP-19.md), | ||
* [CIP-21 "Crypto Accounts"](https://github.com/ceramicnetwork/CIP/blob/main/CIPs/CIP-21/CIP-21.md) | ||
* and [CIP-23 "Also Known As"](https://github.com/ceramicnetwork/CIP/blob/main/CIPs/CIP-23/CIP-23.md) | ||
* specifications provided by the following packages: | ||
* | ||
* ```sh | ||
* npm install --dev @datamodels/identity-profile-basic @datamodels/identity-accounts-crypto @datamodels/identity-accounts-web | ||
* ``` | ||
* | ||
* ```ts | ||
* import { CeramicClient } from '@ceramicnetwork/http-client' | ||
* import { ModelManager } from '@glazed/devtools' | ||
* import { model as basicProfileModel } from '@datamodels/identity-profile-basic' | ||
* import { model as cryptoAccountsModel } from '@datamodels/identity-accounts-crypto' | ||
* import { model as webAccountsModel } from '@datamodels/identity-accounts-web' | ||
* | ||
* const ceramic = new CeramicClient() | ||
* const manager = new ModelManager({ ceramic }) | ||
* | ||
* // Add the imported models to the manager | ||
* manager.addJSONModel(basicProfileModel) | ||
* manager.addJSONModel(cryptoAccountsModel) | ||
* manager.addJSONModel(webAccountsModel) | ||
* | ||
* // Once deployed, the streams are available on the Ceramic node | ||
* await manager.deploy() | ||
* ``` | ||
* | ||
* @module devtools | ||
@@ -8,0 +162,0 @@ */ |
/** | ||
* Development tools library. | ||
* | ||
* ## Purpose | ||
* | ||
* The `devtools` library provides APIs to help support common use-cases when building applications | ||
* on top of Ceramic, notably as a complement to the runtime Glaze libraries. It is meant to be | ||
* used by developers in scripts or other tools such as the CLI, not as a runtime library. | ||
* | ||
* The {@linkcode ModelManager} class notably allows developers to create, update and deploy data | ||
* models to be used with the {@linkcode datamodel.DataModel DataModel} runtime. | ||
* | ||
* ## Installation | ||
* | ||
* ```sh | ||
@@ -6,2 +19,143 @@ * npm install --dev @glazed/devtools | ||
* | ||
* ## Common use-cases | ||
* | ||
* ### Add an existing schema to a model | ||
* | ||
* An existing schema can be added using the | ||
* {@linkcode ModelManager.useDeployedSchema useDeployedSchema} method, as shown below. | ||
* | ||
* ```ts | ||
* import { CeramicClient } from '@ceramicnetwork/http-client' | ||
* import { ModelManager } from '@glazed/devtools' | ||
* | ||
* const ceramic = new CeramicClient() | ||
* const manager = new ModelManager({ ceramic }) | ||
* | ||
* // Set the alias (human-readable name) and stream reference (stream or commit ID or URL) of the | ||
* // schema to add to the model. The schema must be already present on the Ceramic node. | ||
* await manager.useDeployedSchema('MySchema', 'ceramic://k2...ab') | ||
* ``` | ||
* | ||
* The {@linkcode ModelManager.useDeployedDefinition useDeployedDefinition} and | ||
* {@linkcode ModelManager.useDeployedTile useDeployedTile} methods can be used similarly to add | ||
* definitions and tiles to the model. | ||
* | ||
* ### Create and add a schema to a model | ||
* | ||
* Using the {@linkcode ModelManager.createSchema createSchema} method allows to create the schema | ||
* on the Ceramic node and add it to the model. Note that using this method creates a new schema | ||
* every time it is called, therefore generating different stream IDs. | ||
* | ||
* ```ts | ||
* import { CeramicClient } from '@ceramicnetwork/http-client' | ||
* import { ModelManager } from '@glazed/devtools' | ||
* | ||
* const ceramic = new CeramicClient() | ||
* const manager = new ModelManager({ ceramic }) | ||
* | ||
* // Set the alias (human-readable name) and JSON schema contents | ||
* await manager.createSchema('MySchema', { | ||
* $schema: 'http://json-schema.org/draft-07/schema#', | ||
* title: 'MySchema', | ||
* type: 'object', | ||
* properties: { | ||
* ... | ||
* }, | ||
* }) | ||
* ``` | ||
* | ||
* The {@linkcode ModelManager.createDefinition createDefinition} and | ||
* {@linkcode ModelManager.createTile createTile} methods can be used similarly to add definitions | ||
* and tiles to the model. | ||
* | ||
* ### Export a model to JSON | ||
* | ||
* A managed model can be serialized to JSON, making it portable and reusable, with the | ||
* {@linkcode ModelManager.toJSON toJSON} method. | ||
* | ||
* ```ts | ||
* import { CeramicClient } from '@ceramicnetwork/http-client' | ||
* import { ModelManager } from '@glazed/devtools' | ||
* | ||
* const ceramic = new CeramicClient() | ||
* const manager = new ModelManager({ ceramic }) | ||
* | ||
* await manager.useDeployedSchema('MySchema', 'ceramic://k2...ab') | ||
* const encodedModel = await manager.toJSON() | ||
* ``` | ||
* | ||
* ### Import a model from JSON | ||
* | ||
* A managed model serialized using the {@linkcode ModelManager.toJSON toJSON} method can be | ||
* deserialized with the {@linkcode ModelManager.fromJSON fromJSON} static method. | ||
* | ||
* ```ts | ||
* import { CeramicClient } from '@ceramicnetwork/http-client' | ||
* import { ModelManager } from '@glazed/devtools' | ||
* | ||
* const ceramic = new CeramicClient() | ||
* const manager = new ModelManager({ ceramic }) | ||
* | ||
* await manager.useDeployedSchema('MySchema', 'ceramic://k2...ab') | ||
* const encodedModel = await manager.toJSON() | ||
* | ||
* // The `clonedManager` instance will contain the same model as the `manager` instance | ||
* const clonedManager = ModelManager.fromJSON(ceramic, encodedModel) | ||
* ``` | ||
* | ||
* ### Deploy a model to Ceramic | ||
* | ||
* In order to use a model at runtime in an application, it is important to ensure all the streams | ||
* used by the model are present in the Ceramic network. This can be achieved by calling the | ||
* {@linkcode ModelManager.deploy deploy} method, which returns the aliases of the model that can | ||
* be used at runtime by a {@linkcode datamodel.DataModel DataModel} instance. | ||
* | ||
* ```ts | ||
* import { readFile, writeFile } from 'node:fs/promises' | ||
* import { CeramicClient } from '@ceramicnetwork/http-client' | ||
* import { ModelManager } from '@glazed/devtools' | ||
* | ||
* // The encoded model could be imported from the file system for example | ||
* const bytes = await readFile(new URL('encoded-model.json', import.meta.url)) | ||
* const encodedModel = JSON.parse(bytes.toString()) | ||
* | ||
* const ceramic = new CeramicClient() | ||
* const manager = ModelManager.fromJSON(ceramic, encodedModel) | ||
* | ||
* // The deployed model aliases could then be exported to be used at runtime | ||
* const aliases = await manager.deploy() | ||
* await writeFile(new URL('model-aliases.json', import.meta.url), JSON.stringify(aliases)) | ||
* ``` | ||
* | ||
* ### Use existing models | ||
* | ||
* A model can be created by combining other models. For example, using the models for the | ||
* [CIP-19 "Basic Profile"](https://github.com/ceramicnetwork/CIP/blob/main/CIPs/CIP-19/CIP-19.md), | ||
* [CIP-21 "Crypto Accounts"](https://github.com/ceramicnetwork/CIP/blob/main/CIPs/CIP-21/CIP-21.md) | ||
* and [CIP-23 "Also Known As"](https://github.com/ceramicnetwork/CIP/blob/main/CIPs/CIP-23/CIP-23.md) | ||
* specifications provided by the following packages: | ||
* | ||
* ```sh | ||
* npm install --dev @datamodels/identity-profile-basic @datamodels/identity-accounts-crypto @datamodels/identity-accounts-web | ||
* ``` | ||
* | ||
* ```ts | ||
* import { CeramicClient } from '@ceramicnetwork/http-client' | ||
* import { ModelManager } from '@glazed/devtools' | ||
* import { model as basicProfileModel } from '@datamodels/identity-profile-basic' | ||
* import { model as cryptoAccountsModel } from '@datamodels/identity-accounts-crypto' | ||
* import { model as webAccountsModel } from '@datamodels/identity-accounts-web' | ||
* | ||
* const ceramic = new CeramicClient() | ||
* const manager = new ModelManager({ ceramic }) | ||
* | ||
* // Add the imported models to the manager | ||
* manager.addJSONModel(basicProfileModel) | ||
* manager.addJSONModel(cryptoAccountsModel) | ||
* manager.addJSONModel(webAccountsModel) | ||
* | ||
* // Once deployed, the streams are available on the Ceramic node | ||
* await manager.deploy() | ||
* ``` | ||
* | ||
* @module devtools | ||
@@ -8,0 +162,0 @@ */ export * from './datamodel.js'; |
import type { StreamRef } from '@ceramicnetwork/streamid'; | ||
/** @internal */ | ||
export declare function streamIDToString(id: StreamRef | string): string; | ||
@@ -3,0 +4,0 @@ /** @internal */ |
@@ -1,2 +0,2 @@ | ||
export function streamIDToString(id) { | ||
/** @internal */ export function streamIDToString(id) { | ||
return typeof id === 'string' ? id : id.toString(); | ||
@@ -3,0 +3,0 @@ } |
{ | ||
"name": "@glazed/devtools", | ||
"version": "0.2.0-alpha.4", | ||
"version": "0.2.0-alpha.5", | ||
"author": "3Box Labs", | ||
@@ -46,7 +46,7 @@ "license": "(Apache-2.0 OR MIT)", | ||
"@glazed/did-datastore-model": "^0.2.0-alpha.0", | ||
"ajv": "^8.9.0", | ||
"ajv": "^8.10.0", | ||
"ajv-formats": "^2.1.1", | ||
"change-case": "^4.1.2", | ||
"fast-deep-equal": "^3.1.3", | ||
"multiformats": "^9.6.2", | ||
"multiformats": "^9.6.3", | ||
"uint8arrays": "^3.0.0" | ||
@@ -57,3 +57,3 @@ }, | ||
"@glazed/graphql-types": "^0.2.0-alpha.0", | ||
"@glazed/types": "^0.2.0-alpha.0", | ||
"@glazed/types": "^0.2.0-alpha.2", | ||
"@types/jest": "^27.0.3", | ||
@@ -60,0 +60,0 @@ "dids": "^3.0.0-alpha.1", |
58988
1473
Updatedajv@^8.10.0
Updatedmultiformats@^9.6.3