Schema
A schema defines all the fields that make up the document in a collection. All documents stored in a collection must conform to the collection's schema.
Schema Validation
The validation happens during the inserts or updates to the collection. The validation performed like this:
Modifying Schema
Tigris provides a lightweight way to modify the schema. Within Tigris, the schema change is a metadata operation and hence doesn't require rebuilding the entire collection. The updated schema is made available immediately to new write operations.
However, there are certain restrictions on the type of modifications allowed. These restrictions are in place to ensure that we don't need to rebuild collections on schema changes.
Allowed modifications
The following schema changes are currently supported:
- Adding a new field to an existing collection
- Defining a maximum length constraint on existing string or byte type fields. The constraint will only apply to documents added after the constraint is defined
- Increasing the maximum length constraint on existing string or byte type fields.
Unsupported modifications
The following schema changes are currently not supported:
- Changing the data type of a field
- Removing a field from the schema
- Adding or removing a field from the primary key definition
How to modify the schema
Suppose you have the following data model defined:
export class ProductAttributes {
@Field()
name: string;
@Field()
value: string;
}
@TigrisCollection("catalog")
export class Catalog {
@PrimaryKey(TigrisDataTypes.INT64, { order: 1, autoGenerate: true })
id: bigint;
@Field()
name: string;
@Field()
price: number;
@Field({ elements: ProductAttributes })
attributes: Array<ProductAttributes>;
}
And you would like to modify the model and add a new field brand
of type
string
. The steps to make this change are as follows:
Step 1: Update the data model in your application code
export class ProductAttributes {
@Field()
name: string;
@Field()
value: string;
}
@TigrisCollection("catalog")
export class Catalog {
@PrimaryKey(TigrisDataTypes.INT64, { order: 1, autoGenerate: true })
id: bigint;
@Field()
name: string;
@Field()
price: number;
@Field({ elements: ProductAttributes })
attributes: Array<ProductAttributes>;
+
+ @Field()
+ brand: string;
}
Step 2: Update the Collection
Updates the collection with the new model and any new operation will see the changes.
const catalog = await db.createOrUpdateCollection<Catalog>(
Catalog
);