Skip to main content

Automatic Schema Management

Made changes to your data models and forgot to apply them in production before deployment? Have to communicate schema changes to others or wait for that project tracker issue to resolve? Unexpected changes happen all the time, often by mistake and without warning. Even with change control policies carved in stone, they may not always be followed.

Tigris lets you automate the schema management process completely and ensures that the data models in your application are always in sync with the corresponding schema in the database. This makes the change control as simple as a "Code review". Let's see how this works.

How does this work?

Define your data models in your TypeScript project. Register them with the Tigris client during build time and let the Tigris SDK automatically synchronize your data models with the database. This includes creating collections that do not exist, as well as applying schema changes if the data model has evolved.

For example, let's use following Product and User data model:

./db/models/product.ts
import {
TigrisCollection,
TigrisDataTypes,
PrimaryKey,
Field,
} from "@tigrisdata/core";

@TigrisCollection("products")
export class Product {
@PrimaryKey(TigrisDataTypes.INT64, { order: 1, autoGenerate: true })
id?: string;

@Field()
title: string;

@Field()
description: string;

@Field()
price: number;
}
./db/models/user.ts
import {
TigrisCollection,
TigrisDataTypes,
PrimaryKey,
Field,
} from "@tigrisdata/core";

export class Status {
@Field(TigrisDataTypes.DATE_TIME)
lastSeenAt: string;

@Field(TigrisDataTypes.DATE_TIME)
memberSince: string;
}

@TigrisCollection("users")
export class User {
@PrimaryKey({ order: 1 })
name: number;

@Field()
status: Status;
}

Use this script to register models

Now, let's write a script that can trigger build time to automatically create or update the collections

setup.ts
import { Tigris } from "@tigrisdata/core";
import { Product } from "./db/models/product";
import { User } from "./db/models/user";

async function main() {
// setup client
const tigrisClient = new Tigris();
// ensure branch exists, create it if it needs to be created dynamically
await tigrisClient.getDatabase().initializeBranch();
// register schemas
await tigrisClient.registerSchemas([Product, User]);
}

main()
.then(async () => {
console.log("Setup complete ...");
process.exit(0);
})
.catch(async (e) => {
console.error(e);
process.exit(1);
});

The script depends on the following environment variables to be set

Environment variable nameValue
TIGRIS_URIThe URI to your Tigris deployment. For Tigris Cloud this is one of the Tigris regions
TIGRIS_PROJECTyour project name
TIGRIS_CLIENT_IDyour production app key client ID
TIGRIS_CLIENT_SECRETyour production app key client secret
TIGRIS_DB_BRANCHmain

Execute setup script at build time

Include this script in package.json of your npm project and register triggers depending how your project builds. Here are some examples for popular frameworks:

package.json
{
"scripts": {
"setup": "npx ts-node scripts/setup.ts",
"predev": "npm run setup",
"dev": "next dev",
"build": "next build",
"postbuild": "npm run setup",
"start": "next start"
}
}

This way your data models are always synchronized with the database and you never have to step out of your development workflow.

FAQs

Q - Will a new collection be created if it already exists?
Q - Will a collection be deleted if corresponding model is removed?
  • No, that would be an unsafe operation. Users have to explicitly delete collection(s).