Document Schema
Document Schema is a generic type and special configuration object that defines the structure of stored documents. Schema keys are associated with document properties, values are special configuration objects that determine the type and storage rules. Each configuration object is the set of the next options (that depends on described field type):
| Option | Type of Value | Description | Required | Default Value |
|---|---|---|---|---|
type | Types | See Types | Yes | – |
required | boolean | Indicates that this property is required | No | false |
unique | boolean | Indicates that this property should be unique | No | false |
mutable | boolean | Whether the value can be changed after creation | No | true |
lowercase | boolean | Converts value to lower case. Applicable for strings | No | false |
uppercase | boolean | Converts value to upper case. Applicable for strings | No | false |
lowercase and uppercase are mutually exclusive | ||||
default | T[prop], () => T[prop], or () => Promise<T[prop]> | Default value or function returning one. Functions are evaluated in the context of the new document. | No | – |
set | (...args: any[]) => void | Promise<void> | Setter for value, may be an async function | No | – |
get | () => T[prop] | Promise<T[prop]> | Getter for value, may be an async function | No | – |
default and get/set are mutually exclusive | ||||
items | Types | Required when type is Types.ARRAY. Specifies item type. | Yes (if type is ARRAY) | – |
reference | string | Name of the referenced collection | Yes (if type is REFERENCE or items is REFERENCE) | – |
triggerRemove | boolean | If true, document will be removed when referenced item is deleted, applicable for Types.REFERENCE only | No | false |
ttl | number | Time to live in ms, counted from the field's time value: a document is removed once that time is more than ttl ms in the past. Applicable for Types.TIME. | No | Minimum value is 5 |
precision | integer from 0 to 8 | Decimal places kept, e.g. 2 for money. Without it numbers are stored and returned as given (since 3.0.0; earlier versions required it). Applicable for Types.NUMBER | No | – |
dimensions | integer from 1 to 65536 | Length of every vector in the field. Required for Types.VECTOR, not allowed for other types. Can't change while the field holds vectors | Yes, for VECTOR | – |
Data (field) Types
DianaDb supports next types
| Type | Description | Notes |
|---|---|---|
Types.STRING | Document property value is a string | – |
Types.NUMBER | Document property value is a number | Optional precision |
Types.BOOLEAN | Document property value is a boolean | – |
Types.POSITION | Document property is an object with x and y coordinates representing a point | Enables spatial/geometric queries |
Types.OBJECT_ID | Document identifier: <server id>-<pid>-<timestamp>-<random>, e.g. 'dc4628514b81-7517-198a24caed7-cd75cd6bd'. The server id is the server's MAC address unless server_id is set (/^[A-Za-z0-9_]{1,64}-[0-9a-f]{1,}-[0-9a-f]{11}-[0-9a-f]{9}$/) | Not enforced as BSON; it's a string |
Types.REFERENCE | Like Types.OBJECT_ID but includes a link to another collection | Requires reference property. Enables sub-queries |
Types.TIME | Stores date/time as ISO string | Supports time-based filters and TTL indexing |
Types.VECTOR | Array of numbers with a fixed length (dimensions), e.g. a text or image embedding: [0.12, -0.5, 0.91] | Similarity search with $neighbors and $direction. Stored as 8-byte floats, exactly as sent; one vector per document field (no arrays of vectors). See Storing Vectors |
Types.ARRAY | Array field with homogeneous types defined in items | Mixed-type arrays are not allowed. Items may be strings, numbers or positions. Repeated values are kept, in the order given (since Server 4.8.27; from 4.6.10 to 4.8.26 an array held each value once). A document is found once, whatever the number of its matching items. An array of TIME keeps each time once. An array of positions is found by a spatial query when any of its points matches |
Define User and Post Schemas:
import { Types } from '@diana-db/odm';
type User = {
name: string;
positions: { x: number, y: number };
profileEmbedding: number[];
isActive: boolean;
createdAt: string;
}
export const userSchema: Schema<User> = {
name: {
type: Types.STRING,
unique: true,
required: true,
lowercase: true
},
isActive: {
type: Types.BOOLEAN,
default: true
},
position: {
type: Types.POSITION
},
// e.g. the embedding of the user's profile text
profileEmbedding: {
type: Types.VECTOR,
dimensions: 384
},
createdAt: {
type: Types.TIME,
default: () => new Date().getTime()
}
}
import { Types } from '@diana-db/odm';
type Post = {
user: string;
title: string;
content: string;
isPublished: boolean;
createdAt: string;
publishedAt: string;
}
export const postSchema: Schema<Post> = {
user: {
type: Types.REFERENCE,
reference: 'user',
required: true,
triggerRemove: true
},
title: {
type: Types.STRING,
required: true
},
content: {
type: Types.STRING,
required: true
},
isPublished: {
type: Types.BOOLEAN,
default: true
},
createdAt: {
type: Types.TIME,
required: true
},
publishedAt: {
type: Types.TIME,
required: true
}
}
Be careful with schema changes: there is automatic detection of the difference between the old and the new schema. After updating the schema and running your code, it will also be updated on the server side and you may lose some data. To prevent this, only store schemas in one place, for example in a separate (shared/common) package that will be used as a dependency in all parts of your project