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):

OptionType of ValueDescriptionRequiredDefault Value
typeTypesSee TypesYes–
requiredbooleanIndicates that this property is requiredNofalse
uniquebooleanIndicates that this property should be uniqueNofalse
mutablebooleanWhether the value can be changed after creationNotrue
lowercasebooleanConverts value to lower case. Applicable for stringsNofalse
uppercasebooleanConverts value to upper case. Applicable for stringsNofalse
lowercase and uppercase are mutually exclusive
defaultT[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
itemsTypesRequired when type is Types.ARRAY. Specifies item type.Yes (if type is ARRAY)–
referencestringName of the referenced collectionYes (if type is REFERENCE or items is REFERENCE)–
triggerRemovebooleanIf true, document will be removed when referenced item is deleted, applicable for Types.REFERENCE onlyNofalse
ttlnumberTime 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.NoMinimum value is 5
precisioninteger from 0 to 8Decimal 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.NUMBERNo–
dimensionsinteger from 1 to 65536Length of every vector in the field. Required for Types.VECTOR, not allowed for other types. Can't change while the field holds vectorsYes, for VECTOR–

Data (field) Types

DianaDb supports next types

TypeDescriptionNotes
Types.STRINGDocument property value is a string–
Types.NUMBERDocument property value is a numberOptional precision
Types.BOOLEANDocument 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.REFERENCELike Types.OBJECT_ID but includes a link to another collectionRequires reference property. Enables sub-queries
Types.TIMEStores date/time as ISO stringSupports 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.ARRAYArray field with homogeneous types defined in itemsMixed-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
  }
}