Skip to main content

Documents

A collection is analogous to tables in a relational database. Records are stored in collection as documents which are analogous to JSON documents. Documents are composed of field-value pairs.

Document

Document structure

Schema

For Tigris, schemas are an integral part and form the basis of the rich features such as automatic indexing and real-time search.

Tigris enforces that all documents in a collection must conform to a schema. The schema is defined as part of creating the collection. See the section Modifying Schema to learn more about the schema can be evolved.

Since schemas are a way to structure your data according to the application logic, they are described as part of your application code using the programming language constructs.

Below is an example of defining a schema for a collection named catalog. All the documents in this collection will follow the defined schema.

interface Catalog extends TigrisCollectionType {
id?: string;
name: string;
price: number;
attributes: object;
}

// schema definition
const catalogSchema: TigrisSchema<Catalog> = {
id: {
type: TigrisDataTypes.INT64,
primary_key: {
order: 1,
autoGenerate: true,
},
},
name: {
type: TigrisDataTypes.STRING,
},
price: {
type: TigrisDataTypes.NUMBER,
},
attributes: {
type: TigrisDataTypes.OBJECT,
},
};

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

Supported field types

There are three categories of field types supported:

  • Primitive: Strings, Numbers, Binary Data, Booleans, UUIDs, DateTime
  • Complex: Arrays
  • Objects: A container data type defined by the user that stores fields of primitive types, complex types as well as other Objects

Primary Key

Every collection must specify one of the fields to be a primary key. In the example above, the field id is designated as the primary key. A primary key uniquely identifies a document in the collection and enforces the unique constraint. Documents are stored in the collection in sorted order according to the primary key.

You can optionally tag the primary key field as autoGenerate. The values for this field will then be autogenerated.

Handling of null values

null is an allowed value that can be used with any of the fields in the schema except for the ones that are part of the primary key definition. If a field is set to the null value, then the document stored in the database will have that field set to the null value. If you are looking for the behavior where you would like fields without any values to not be stored as part of the document, then simply skip setting them to any value.

Supported operations

Tigris enables you to perform rich set of operations on documents. The supported operations are covered below:

OperationsDescription
CRUDAll the standard CRUD operations are supported:
  • Insert: Insert a document by primary key
  • Replace: Insert or Replace a document by primary key
  • Update: Update documents by filtering on any field
  • Delete: Delete documents by filtering on any field
  • Read: Read documents by filtering on any field of the document
  • See the CRUD section to learn more.
    TransactionYou can group multiple CRUD operations together and perform them as part of a transaction. Transactions can be performed interactively, such as when performing read-modify-write operations. There are no restrictions on which collections or documents are allowed to be modified in a transaction. See the Transactions section to learn more.
    SearchDocuments in collections are searchable in real-time. Search operations, similar to what are provided by standalone search platforms such as Elasticsearch, are supported out of the box. See the Search section to learn more.