Skip to main content

Storage

Storage is widely used in Condition, Attributive, and SideEffect contexts, and can also be directly utilized in Custom APIs. It can be initially understood as a powerful ORM (Object-Relational Mapping) tool. Here is an overview of the APIs it offers.

find

argumenttyperequireddescription
entitystringtrueName of the entity to be queried
matchBoolExpfalseQuery conditions
modifierModifierDatafalseSorting and pagination conditions
attributeQueryAttributeQueryDatafalseAttributes to query

ModifierData

Type Definition:

type ModifierData = {
limit?: number
offset?: number
orderBy?: {
[k: string]: 'ASC'|'DESC'
},
}

AttributeQueryData

Type Definition

type AttributeQueryData = AttributeQueryDataItem[]

type AttributeQueryDataRecordItem = [string, RecordQueryData, boolean?]
type AttributeQueryDataItem = string | AttributeQueryDataRecordItem
type RecordQueryData = {
matchExpression?: MatchExpressionData,
attributeQuery?: AttributeQueryData,
modifier?: ModifierData,
label?: string,
goto?: string
exit? : (data:RecursiveContext) => Promise<any>
}
type MatchExpressionData = BoolExp<MatchAtom>

Note that within the attributeQuery, you can use label, goto, and exit to construct queries for tree-like data structures.

Example:

const exit = async (context: RecursiveContext) => {
// exit condition
}

const departmentWithChildDepts = (await storage.find('Department',
BoolExp.atom({key: 'name', value: ['=', 'dept1']}),
undefined,
['*', ['children', {
label: 'childDept',
attributeQuery: ['*', ['children', { goto: 'childDept', exit}]]
}]],
))

findOne

findOne is a special case of find. It returns only one record, instead of an array of records. Its parameters are consistent with find, but it automatically adds limit: 1 to the modifier.

create

argumenttyperequireddescription
entitystringtrueName of the entity to create
rawDataobjecttrueData for the new entity

Example:

const rawData = {
name: 'A',
age: 18,
isStudent: true,
}

const newUser = await storage.create('User', rawData)

update

argumenttyperequireddescription
entitystringtrueName of the entity to update
matchBoolExpfalseMatch conditions for update
newRecordDataobjecttrueData for the updated entity

Example:

const match = BoolExp.atom({
key: 'id',
value: ['=', 1]
})
const newRecordData = {
name: 'B',
age: 19,
isStudent: false,
}
const newUser = await storage.update('User', match, newRecordData)

delete

argumenttyperequireddescription
entitystringtrueName of the entity to delete
matchBoolExpfalseMatch conditions for deletion

Example:

const match = BoolExp.atom({
key: 'id',
value: ['=', 1]
})
await storage.delete('User', match)

getRelationName

Retrieve the relation name using the entity name and attribute name.

Suppose we have an ownerRelation defined as follows:

const ownerRelation = Relation.create({
sourceEntity: 'User',
sourceProperty: 'posts',
targetEntity: 'Post',
targetProperty: 'owner',
relType: '1:n',
})

We can obtain the relation name through User.posts or Post.owner. Example:

const relationName = storage.getRelationName('User', 'supervisor')

findRelationByName

Query a relation by its name.

argumenttyperequireddescription
relationstringtrueName of the relation to query
matchBoolExpfalseQuery conditions
modifierModifierDatafalseSorting and pagination conditions
attributeQueryAttributeQueryDatafalseAttributes to query

findOneRelationByName

A special case of findRelationByName, returning only one relation. Internally, it calls findRelationByName and adds limit: 1 to the modifier.

updateRelationByName

argumenttyperequireddescription
relationstringtrueName of the relation to update
matchBoolExpfalseMatch conditions for update
newRecordDataobjecttrueData for the updated relation

removeRelationByName

argumenttyperequireddescription
relationstringtrueName of the relation to delete
matchBoolExpfalseMatch conditions for deletion

addRelationByNameById

argumenttyperequireddescription
relationstringtrueName of the relation to create
sourceIdstringtrueID of the relation source entity
targetIdstringtrueID of the relation target entity
propertiesobjecttrueAttribute data on the relation

Note: If the relation’s relType is 1:n, 1:1, or n:1, and a relation already exists, creating a new relation will automatically delete the conflicting relation, ensuring the data always conforms to the relType definition.