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();
For information about operators that can be used with strings in where
statements, see the Where clause documentation.
Number
The number data type is used to store integer or float numbers.
import { Schema as S } from '@triplit/client';
const numberType = S.Number();
For information about operators that can be used with numbers in where
statements, see the Where clause documentation.
Boolean
The boolean data type is used to store true or false values.
import { Schema as S } from '@triplit/client';
const booleanType = S.Boolean();
For information about operators that can be used with booleans in where
statements, see the Where clause documentation.
Date
The date data type is used to store date and time values.
import { Schema as S } from '@triplit/client';
const dateType = S.Date();
For information about operators that can be used with dates in where
statements, see the Where clause documentation.
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());
For information about operators that can be used with sets in where
statements, see the Where clause documentation.
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 }
For information about operators that can be used with optional attributes in where
statements, see the Where clause documentation.
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(),
});