Data Model
The ISME SDK helps developers create their own data models for decentralized applications.
Steps
How to Create a Data Model
The definition of a data model is based on JSON Schema. A data model is similar to a table in a traditional database. Users can directly add, delete, modify and check data based on it. All the data is public, but the ownership of the data belongs to a specific DID. For data modification, writing, and deletion, the ownership of the DID must be verified. The following example demonstrates how to create a Note
application, which contains The title
and content
fields, used to record the title and content respectively.
Each record of the data model includes some default properties, for example,
- dataId - ID of the data record
- createdAt - the time it was created
- updatedAt - the time it was revised
Thus you don't need to define the time of creation and modification in your APP.
Because the model needs to be created in advance by the developer, this step can be executed by a Node.js script and requires a DID maintained by the developer. The sample code is as follows:
import { ISMEClient } from '@ismelabs/client'
// maintain the private key and comlete the DID registration.
const privateKey = '0x'
// ISME Gateway address
const endpoint = ''
const client = new ISMEClient(endpoint)
client.did.config({
network: 'ethereum',
privateKey
})
const schema = {
name: 'my-note',
description: 'note list',
schema: {
title: 'Note',
type: 'object',
properties: {
title: {
type: 'string',
maxLength: 200
},
content: {
type: 'string',
maxLength: 5000
}
},
required: ['title', 'content']
}
}
async function run() {
const result = await client.schema().create(schema)
console.log(result.modelName)
}
How to use Data Models
The data model is referred to by modelName
, which is the value returned after the successful creation of schema
in the previous step. To refer a model, you need to create a corresponding instance first, and then perform addition, deletion, modification, and query operations based on this instance. The sample code is as follows:
import { useMemo } from 'react'
import { useISME } from '@ismelabs/did-manager'
interface IMyNote {
title: string
content: string
}
export default function DataModel() {
const { client, identifier } = useISME()
const noteModel = useMemo(() => {
return client.model<IMyNote>('my-note')
}, [client])
const find = async () => {
const result = await noteModel.find({
identifier,
offset: 0,
limit: 10
})
}
}
The above example will query the first 10 my-note
records of the currently logged in DID users.
Default Data Models
In order to facilitate development, the ISME SDK provides some default models, which developers can use directly via the API.
Profile
profile
is used to save the basic personal information of DID users. The data types are as follows:
export interface ProfileModel {
name: string
avatar: string
bio: string
url: string
gender: string
location: string
}
client
contains the profile
model instance, which can be used directly. For more details, please refer to the API documentation.
import { useISME } from '@ismelabs/did-manager'
export default function ProfileModel() {
const { client, identifier } = useISME()
const find = async () => {
const profile = await client.profile.info(identifier)
console.log(profile)
}
}
Social
social
is used to save the social account details of DID users. The data types are as follows:
export interface SocialAccountModel {
account: string
type: string
proof: string
verifier_key: string
msghash: string
}
Some of these fields are related to the verification function. For details, please refer to the social accounts
section. The usage of social
is very similar to profile
, which can be used through client.socialAccount
, such as
import { useISME } from '@ismelabs/did-manager'
export default function SocialModel() {
const { client, identifier } = useISME()
const list = async () => {
const items = await client.socialAccount.list(identifier)
console.log(items)
}
}
Follow
follow
is used to record the 'following' relationship between DID users, which is the most common data model in social networks. Its data structure is as follows:
export interface FollowModel {
following_did: string
}
This means that you only need to record the DID of followers in the follow relationship. To access the follow
model, you can also use client.follow
. The sample code is below:
import { useISME } from '@ismelabs/did-manager'
export default function FollowModel() {
const { client, identifier } = useISME()
const list = async () => {
const items = await client.follow.following({
identifier
})
console.log(items)
}
}