Reference Field
Reference to another schema, indicates that it will have one of the generated values of the selected field.
The field to reference must be a key field.
const userSchema = chaca.schema({
id: chaca.key(chaca.sequence()),
// rest fields
})
const postSchema = chaca.schema({
author: chaca.ref('User.id'),
// rest fields
})
chaca.multiGenerate([
{ name: 'User', schema: userSchema, documents: 50 },
{ name: 'Post', schema: postSchema, documents: 50 },
])
As seen in the previous code, the field will have one of the values id
generated by the User
schema, which can be repeated in other documents generated by the Post
schema.
If you try to reference a ref field an exception will be thrown
export const schemaOne = chaca.schema({
id: schemas.id.uuid(),
ref: chaca.ref('SchemaTwo.ref'), // this will throw an error because 'SchemaTwo.ref' is a ref field,
})
export const schemaTwo = chaca.schema({
id: schemas.id.uuid(),
ref: chaca.ref('SchemaThree.name'),
})
export const schemaThree = chaca.schema({
id: schemas.id.uuid(),
name: schemas.person.fullName(),
})
Config
where
Function that indicates if the ref document is valid.
It works as a filter for documents that do not meet a certain condition.
const postSchema = chaca.schema({
author: chaca.ref('User.id', ({ refFields: userFields }) => {
// will only choose the ids of users older than 18
return userFields.age > 18
}),
// rest fields
})
This function receives as a parameter an object with 3 properties:
refFields
An object with the fields created up to that point in the document that can be referencedconst postSchema = chaca.schema({
id: schemas.id.uuid(),
author: chaca.ref('User.id', ({ refFields }) => {
return refFields.age >= 18
}),
})currentFields
An object with the fields already created up to that momentconst userSchema = chaca.schema({
id: schemas.id.uuid(),
favoriteCategory: chaca.enum(['Horror', 'Comedy', 'Action']),
posts: {
type: chaca.ref('Post.id', ({ currentFields, refFields }) => {
return currentFields.favoriteCategory === refFields.category
}),
isArray: 20,
},
})store
Dataset store to interact with all the data in the dataset. Learn about Dataset Store
unique
Indicates if the selected value can not be chosen by another documents of that schema.
const postSchema = chaca.schema({
author: chaca.ref('User.id', { unique: true }),
// rest fields
})