MetaID
What is this?
MetaID is an entity protocol based on Microvision Chain and MetaID DID protocol. Developers can use MetaID to create their own entity resource.
What do you mean by Entity
?
Entity is a concept in MetaID. It is a collection of resources. For example, an entity named Buzz
can be used to create a collection of resources called buzzes
. Each buzz
resource has its own unique id, which is a transaction id on Microvision Chain.
Why do we use such a concept?
We use this concept to make it easier for developers to create their own entity resources. Developers can use MetaID to create their own entity resources, and then use the entity resources to create their own applications.
Previously we used the concept of Brfc Node
to create metadata and build Web3 applications. But this concept is too verbose, too low-level, and not easy to use. So we created another abstraction layer on top of it, which is the concept of entity
.
We call this abstraction process EMM
(Entity-Metadata Mapping), similar to the ORM (Object-Relational Mapping) concept in the database field. By doing this, we can create and utilize a more semantic and developer-friendly way to code.
How to use?
The API examples listed below are still under development. Use with caution.
Define entity with schema
Define your entity schema in src/metaid-entities/*.entity.ts
.
const buzzEntitySchema: EntitySchema = {
name: 'buzz',
nodeName: 'SimpleMicroblog',
versions: [
{
version: 1,
id: 'b17e9e277bd7',
body: [
{
name: 'content',
type: 'string',
},
],
},
],
}
export default buzzEntitySchema
Load entity, or create one on the fly
import { define, use } from '@metaid/metaid'
const MyFirstEntity = define('my-first-entity', {
})
const Buzz = use('buzz')
const GroupMessage = use('group-message')
Connect to wallet
import { LocalWallet, MetaletWallet, connect } from '@metaid/metaid'
const localWallet = new LocalWallet('abchereisyourmnenonicstring')
const metaletWallet = new MetaletWallet()
const baseEntity = connect(localWallet)
const Buzz = connect(localWallet).use('buzz')
const GroupMessage = connect(metaletWallet).use('group-message')
Create MetaID
const baseEntity = connect(localWallet)
if (!baseEntity.hasMetaid()) {
await baseEntity.createMetaid()
}
const Buzz = baseEntity.use('buzz')
Use entity
const hasRoot: boolean = Buzz.hasRoot()
if (!hasRoot) {
await Buzz.createRoot()
}
const buzzes = await Buzz.list()
const buzzes = await Buzz.list({
where: {
title: 'Hello World',
},
orderBy,
orderByDesc,
limit,
metaid: '0x1234567890',
})
const myBuzzes = Buzz.myList()
const buzz = Buzz.one('0x1234567890')
const buzz = Buzz.first('0x1234567890')
const buzz = Buzz.get('0x1234567890')
const buzz = Buzz.one({
where: {
title: 'Hello World',
},
})
const newBuzz = Buzz.create(
{
title: 'Hello World',
content: 'This is my first buzz',
},
{
visible: true,
},
)
if (visible)
const updatedBuzz = Buzz.update(newBuzz.id, {
title: 'Hello World Again',
content: 'Here we go..',
})
const oldBuzz = Buzz.get('0x1234567890')
const updatedBuzz = oldBuzz.update({ content: 'Good day, commander!' })
const deletedBuzz = Buzz.delete('0x1234567890')
oldBuzz.delete()
Some more complex use cases
Create a buzz with 3 photos
const Buzz = use('buzz')
const File = use('metafile')
const photos = await File.create([
{
name: 'photo1.jpg',
type: 'image/jpeg',
content: 'base64 string',
},
{
name: 'photo2.jpg',
type: 'image/jpeg',
content: 'base64 string',
},
{
name: 'photo3.jpg',
type: 'image/jpeg',
content: 'base64 string',
},
])
const buzz = await Buzz.with(photos).create({ content: 'Have a nice day!' })
Give a like to a group message
const GroupMessage = use('group-message')
const Like = use('like')
const theMessage = await GroupMessage.get('0x1234567890')
await Like.belongsTo(theMessage).create()
Refer an NFT info in a buzz
const Reference = use('reference')
await Reference.belongsTo(nft).create({
content: 'Have a look at my gorgeous NFT!',
nftId: nft.id,
})```
---
## API Reference
### Wallet
Can have multiple wallet implementations as long as it implements the `Wallet` interface.
```ts
import { LocalWallet, MetaletWallet } from '@metaid/metaid'
LocalWallet.create(mnemonic: string): LocalWallet
MetaletWallet.create(): Promise<MetaletWallet>
Connector
A connector is the bridge between your wallet and the entity.
import { connect } from '@metaid/metaid'
connect(wallet: Wallet): Promise<Connector>
connector.isConnected(): boolean
connector.use(entityName: string): Entity
connector.hasMetaid(): boolean
connector.createMetaid(): Promise<string>
Entity
An entity is a controller class to operate on a specific resource.
connector.use(entityName: string): Entity
connector.define(entityName: string, schema: EntitySchema): Entity
entity.hasRoot(): boolean
entity.createRoot(): Promise<string>
entity.list(query?: Query): Promise<Resource[]>
entity.myList(query?: Query): Promise<Resource[]>
entity.one(query: Query | string): Promise<Resource>
entity.first(query: Query | string): Promise<Resource>
entity.get(query: Query | string): Promise<Resource>
entity.create(data: Record<string, any>): Promise<Resource>
entity.update(id: string, data: Record<string, any>): Promise<Resource>
entity.delete(id: string): Promise<Resource>
Resource
A resource is a data object that represents a specific entity.
entity.list()
returns an array of resources.
entity.one()
returns a single resource.
type Resource = {
txid: string
createdAt: timestamp
body: Record<string, any>
user: {
metaid: string
name: string
avatar: string
}
}