Dataset Store
Sometimes the value of a specific field needs knowledge of data generated by other schemas. To solve this problem, the functions executed in the custom fields and the ref fields receive a store in which information from the entire dataset can be accessed.
Continuing with the case of users and article posts. You can define in the user schema a field countPosts
that contains an integer that indicates the number of posts created by the user.
const userSchema = chaca.schema({
countPosts: ({ store, currentFields: userFields }) => {
// get all posts belonging to this user
// returns an array with all the ids of the user's posts
const allUserPostId = store.getValue('Post.id', {
where: (postFields) => {
return postFields.userId === userFields.id
},
})
return allUserPostId.length
},
})
The store only works in generation of multiple schemas
getValue
If you want to obtain an array of values belonging to a specific external schema, you must define it with the following pattern. store.getValue('Schema.[...fields]')
const schema = chaca.schema({
field1: chaca.schema({
field2: schemas.id.uuid(),
}),
})
store.getValue('Schema.field1') // [{ field1: 'value1' }, { field1: 'value2' }, ...]
store.getValue('Schema.field1.field2') // ['value1', 'value2', ...]
If you pass only the name of the schema, it will return an array with all the objects created from the schema.
store.getValue('Schema') // objects array
Configuration
To configure the obtaining of data from an external schema, an object with the following parameters can be passed as the second parameter of the getValue
function.
Param | Type | Required | Description |
---|---|---|---|
where | function | no | A function that should return a boolean indicating whether the schema document meets the required conditions |
❌ Be careful with cyclic errors
It is important to clarify that when wanting to access a field of an external schema through the store, a dependency is created on that selected schema, therefore the creation of the current schema is stopped to start building the data of the other one.
Then, trying to access one of the schemas that make it up should be avoided in that dependency chain.
Now a case will be presented where this dependency is broken creating a cyclical error.
const userSchema = chaca.schema({
countPosts: ({ store, currentFields: userFields }) => {
const allUserPostId = store.getValue('Post.id', {
where: (postFields) => {
return postFields.userId === userFields.id
},
})
return allUserPostId.length
},
})
const postSchema = chaca.schema({
id: chaca.sequence(),
userId: chaca.ref('User.id'),
// this field will throw a cyclic error
userAge: ({ currentFields }) => {
const allUser = store.getValue('User')
const foundUser = allUser.find((u) => u.id === currentFields.userId)
return foundUser.age
},
})
const data = chaca.multiGenerate([
{ name: 'User', schema: UserSchema, documents: 50 },
{ name: 'Post', schema: PostSchema, documents: 50 },
])
The Post.userAge
field will throw a cyclic error when trying to access the data from the User schema when this one has stopped the creation process since it depends on the data from the Post schema.
getSchemaDocuments
With this function you can obtain all the documents created up to that moment. Skipping information from the document currently being created
store.getSchemaDocuments()