Data types
When using a schema you have a few datatypes at your disposal:
Collections and Schema types
The Collections
and Schema
schema types are used to define your collections and their attributes. They are simple record types but will help provide type hinting and validation to their parameters.
import { Schema as S } from '@triplit/client';
const schema = S.Collections({
todos: {
schema: S.Schema({
id: S.Id(),
// Additional attributes here...
}),
},
});
Value types
Value types are basic types for the database.
String
The string data type is used to store text.
import { Schema as S } from '@triplit/client';
const stringType = S.String();
Strings support =
, !=
, like
, nlike
, in
, nin
, and isDefined
operators in where
statements.
like
and nlike
You can use the like
operator in a where clause to do simple filtering with string similarity. A like
expression is true if the supplied attribute matches the supplied filter pattern.
An underscore (_
) in a pattern stands for (matches) any single character; a percent sign (%
) matches any sequence of zero or more characters.
For example:
['triplit', 'like', 'triplit'] true
['triplit', 'like', 'tri%'] true
['triplit', 'like', 'tr_pl_t'] true
['triplit', 'like', 'trip'] false
in
and nin
You can use the in
operator in a where clause to check if an attribute is in a set of values. The nin
operator is the opposite of in
.
For example:
['triplit', 'in', ['triplit', 'hello']] true
['triplit', 'nin', ['triplit', 'hello']] false
Number
The number data type is used to store integer or float numbers.
import { Schema as S } from '@triplit/client';
const numberType = S.Number();
Numbers support =
, !=
, >
, >=
, <
, <=
, in
, nin
, and isDefined
operators in where
statements.
Boolean
The boolean data type is used to store true or false values.
import { Schema as S } from '@triplit/client';
const booleanType = S.Boolean();
Booleans support =
, !=
, and isDefined
operators in where
statements.
Date
The date data type is used to store date and time values.
import { Schema as S } from '@triplit/client';
const dateType = S.Date();
Dates support =
, !=
, >
, >=
, <
, <=
, and isDefined
operators in where
statements.
Set
Set types are used to store a collection of non nullable value types. Sets are unordered and do not allow duplicate values.
Lists, which support ordering and duplicate values, are on the roadmap (opens in a new tab).
import { Schema as S } from '@triplit/client';
const stringSet = S.Set(S.String());
Sets support has
and !has
operators in where
statements, which check if the set does or does not contain the value. They also support the isDefined
operator.
Options
Value types have a few options that can be passed to their constructor.
nullable
You can indicate an attribute is nullable by passing the { nullable: true }
option to its constructor.
import { Schema as S } from '@triplit/client';
import { TriplitClient } from '@triplit/client';
const schema = S.Collections({
test: {
schema: S.Schema({
id: S.Id(),
nullableString: S.String({ nullable: true }),
}),
},
});
const client = new TriplitClient({
schema,
});
await client.insert('test', {
nullableString: null,
});
optional
You can indicate an attribute is optional by passing the { optional: true }
option to its constructor or wrapping the attribute in S.Optional
. Optional attributes are not required for insertion by the schema and will be undefined
at runtime if not provided. Optional attributes may also be deleted and assigned to null
in updater functions.
import { Schema as S } from '@triplit/client';
const schema = S.Collections({
test: {
schema: S.Schema({
id: S.Id(),
optionalString: S.String({ optional: true }),
}),
},
});
await client.insert('test', {
id: '123',
});
// { id: '123' }
await client.update('test', '123', (e) => {
e.optionalString = 'hello';
});
// { id: '123', optionalString: 'hello' }
await client.update('test', '123', (e) => {
delete e.optionalString;
});
// { id: '123', optionalString: null }
Optional attributes support the isDefined
operator, which checks if the specified attribute is defined.
import { Schema as S } from '@triplit/client';
const schema = S.Collections({
profiles: {
schema: S.Schema({
id: S.Id(),
name: S.String(),
email: S.Optional(S.String()),
}),
},
});
const query = client.query('profiles').Where('email', 'isDefined', true);
default
You can provide defaults values or functions for an attribute. Triplit currently support literal values and the following functions:
uuid()
(for Strings)now()
(for Dates)Set.empty()
(for Sets)
The below schema has literal and function default values.
import { Schema as S } from '@triplit/client';
import { TriplitClient } from '@triplit/client';
const schema = S.Collections({
messages: {
schema: S.Schema({
id: S.Id(),
text: S.String({ default: 'hello' }),
sent_at: S.Date({ default: S.Default.now() }),
reactions: S.Set(S.String(), { default: S.Default.Set.empty() }),
}),
},
});
await client.insert('test', {});
// { id: <uuid>, text: 'hello', sent_at: '2021-03-01T00:00:00.000Z',reactions: Set {} }
enum
(String only)
You can provide an array of strings to the enum
option to restrict the possible values of a string attribute.
import { Schema as S } from '@triplit/client';
const schema = S.Collections({
test: {
schema: S.Schema({
id: S.Id(),
status: S.String({ enum: ['active', 'inactive'] }),
}),
},
});
This will both perform runtime validation and provide autocomplete in your editor.
Record
Record types allow you model nested information. They support the nullable
option.
import { Schema as S } from '@triplit/client';
const recordType = S.Record({
street: S.String(),
city: S.String(),
state: S.String(),
zip: S.String(),
});