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 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.
import {
Field,
PrimaryKey,
Tigris,
TigrisCollection,
TigrisDataTypes,
} from "@tigrisdata/core";
export class ProductAttributes {
name!: string;
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>;
}
(async () => {
const client = new Tigris();
const db = client.getDatabase();
await db.createOrUpdateCollection<Catalog>(Catalog);
})();
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:
Operations | Description |
---|---|
CRUD | All the standard CRUD operations are supported: |
Transaction | You 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. |
Search | Documents in collections are searchable in real-time. Search operations, similar to what are provided by standalone search platforms such as Elasticsearch and Pinecone, are supported out of the box. See the Vector Search and Full-text Search sections to learn more. |