Skip to main content

TypeScript Reference

npm

Tigris provides an easy-to-use and intuitive interface for TypeScript. Setting up the database is instantaneous, as well - no need for tedious configuration. You define the data model as part of the application code, which then drives the database infrastructure without you having to configure and provision database resources.

1. Install package

npm install @tigrisdata/core

Getting started →

2. Define data models

const tigris: Tigris = new Tigris({
serverUrl: "localhost:8081",
});

interface Review extends TigrisCollectionType {
author: string;
rating: number;
}

interface Catalog extends TigrisCollectionType {
id?: number;
name: string;
price: number;
brand: string;
labels: string;
popularity: number;
reviews?: object;
}

const reviewSchema: TigrisSchema<Review> = {
author: {
type: TigrisDataTypes.STRING,
},
rating: {
type: TigrisDataTypes.NUMBER,
},
};

const catalogSchema: TigrisSchema<Catalog> = {
id: {
type: TigrisDataTypes.INT32,
primary_key: {
order: 1,
autoGenerate: true,
},
},
name: {
type: TigrisDataTypes.STRING,
},
price: {
type: TigrisDataTypes.NUMBER,
},
brand: {
type: TigrisDataTypes.STRING,
},
labels: {
type: TigrisDataTypes.STRING,
},
popularity: {
type: TigrisDataTypes.INT32,
},
reviews: {
type: reviewSchema,
},
};

const db: DB = await tigris.createDatabaseIfNotExists("catalogdb");

const catalog: Collection<Catalog> = await db.createOrUpdateCollection(
"catalog",
catalogSchema
);

Declaring models →

3. Persist data

const product: Catalog = {
name: "fiona handbag",
price: 99.9,
brand: "michael kors",
labels: "purses",
popularity: 8,
};

const insertedProduct: Catalog = await catalog.insert(product);

Insert documents →

4. Query data

catalog.findOne({
op: SelectorFilterOperator.EQ,
fields: {
brand: "adidas"
}
}).then(value => {
const product: Catalog = <Catalog> value;
console.log(product.name); // 'sneakers shoes'
console.log(product.price); // 40
});

Query documents →

5. Search data

const request: SearchRequest<Catalog> = {
q: "running",
};

catalog.search(request, {
onEnd() {
// when search completes
},
onNext(result: SearchResult<Catalog>) {
// when a search result is fetched
},
onError(error: Error) {
// in case of an error
},
});

Search documents →

6. Stream events

6.1 Create topic

export interface OrderEvent extends TigrisTopicType {
orderId: number;
orderName: string;
orderStatus: string;
}

export const orderEventSchema: TigrisSchema<OrderEvent> = {
orderId: {
type: TigrisDataTypes.INT32,
},
orderName: {
type: TigrisDataTypes.STRING,
},
orderStatus: {
type: TigrisDataTypes.STRING,
},
};

const topic = await db.createOrUpdateTopic<OrderEvent>("order_events", orderEventSchema);

6.2 Publish message

const orderEvent: OrderEvent = {
orderId: 1,
orderName: "order of shoes",
orderStatus: "completed"
};

topic.publish(orderEvent);

Event Streaming →