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:
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;
}
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
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 name | Value |
---|---|
TIGRIS_URI | The URI to your Tigris deployment. For Tigris Cloud this is one of the Tigris regions |
TIGRIS_PROJECT | your project name |
TIGRIS_CLIENT_ID | your production app key client ID |
TIGRIS_CLIENT_SECRET | your production app key client secret |
TIGRIS_DB_BRANCH | main |
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:
- Next.js
- NuxtJS
- NestJS
- Remix
{
"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"
}
}
{
"scripts": {
"setup": "npx ts-node scripts/setup.ts",
"predev": "npm run setup",
"dev": "nuxt dev",
"build": "nuxt build",
"postbuild": "npm run setup",
"start": "nuxt start"
}
}
{
"scripts": {
"setup": "npx ts-node scripts/setup.ts",
"predev": "npm run setup",
"dev": "nest start --watch",
"build": "nest build",
"postbuild": "npm run setup"
"start": "node dist/main"
}
}
{
"scripts": {
"setup": "npx ts-node scripts/setup.ts",
"predev": "npm run setup",
"dev": "remix dev",
"build": "remix build",
"postbuild": "npm run setup",
"start": "remix-serve build"
}
}
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?
- No, a new collection will not be created. However, Tigris will attempt to modify the existing schema if there are changes. Learn more about schema modification in Tigris
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).