Skip to main content

ISMEModel

Used to add, delete, modify, and query the data model

constructor

Type

class ISMEModel<T = any> {
constructor(client: ISMEClient, modelId: string)
}

Details

To create an instance, you need to pass in client, or you can create it directly through client.model(), such as

interface IMyNote {
title: string
content: string
}

const notes = client.model<IMyNote>('my-note')

client.model receives a generic parameter, which used to specify the data type in the data model, so that the type can be automatically deduced when adding, deleting, modifying.

Property

modelId

string, the unique identifier of the model, which is the name when the schema was created earlier

Method

find

Query for a list of eligible data

Type

type WithMeta<T> = T & {
__owner: string
createdAt: number
updatedAt: number
dataId: string
}

interface FindOptions<T = any> {
identifier?: string
query: Partial<T> & {
dataid?: string
}
fields?: string[]
offset?: number
limit?: number
}

class ISMEModel<T = any> {
find(options: FindOptions<T>): Promise<WithMeta<T>[]>
}

Parameters

  • identifier: DID to be queried, here indicates the ownership of the data
  • query: query condition
  • fields: the fields to return
  • offset: number of skips, used for pagination
  • limit: the number of items to be returned by the query

Details

find is the common used data model query method. Let's take a look at a example.

interface IMyNote {
title: string
content: string
}

const notes = client.model<IMyNote>('my-note')

async function run() {
// Query all the data of did:nft3:bob and return the first 5
const items = await notes.find({
identifier: 'did:nft3:bob',
offset: 0,
limit: 5
})
console.log(items)
}
info

Each piece of data will automatically include metadata such as creation time, so the data type returned by find is WithMeta<T>, where WithMeta will automatically add corresponding attributes to the data type.

findOne

Returns the first piece of data that matches the condition

Type

class ISMEModel<T = any> {
findOne(options: FindOptions<T>): Promise<WithMeta<T>>
}

Details

The use of findOne is basically the same as find, the difference is that if the specified query condition has data that meets the conditions, it will return the first data, otherwise it will return undefined

insertOne

insert one new record

Type

class ISMEModel<T = any> {
insertOne(data: T): Promise<{
dataId: string
}>
}

Details

After successful insertion, the dataId associate with the inserted data will be returned, which is the unique identifier of that data.

updateOne

update a new record

Type

class ISMEModel<T = any> {
updateOne(dataId: string, data: T): Promise<{
dataId: string
}>
}

Details

Updating data will completely overwrite the original data record

deleteOne

delete record

Type

class ISMEModel<T = any> {
deleteOne(dataId: string): Promise<{
dataId: string
}>
}

count

Count the number of data items that meet the conditions

Type

interface CountOptions<T = any> {
identifier?: string
count: Partial<T>
}

class ISMEModel<T = any> {
count(options: CountOptions): Promise<{
count: number
}>
}