Model Module

The Model module provides the main interface for interacting with collections. It wraps Schema definitions and provides CRUD operations, queries, and document management.

What is it?

Model is a class that represents a collection in ArangoDB. It combines a Schema (structure definition) with database operations (CRUD, queries, relationships). Models are created using the model() function and provide type-safe methods for working with documents.

Why do you need it?

  • CRUD Operations: Create, read, update, and delete documents
  • Type Safety: Full TypeScript support with inferred types
  • Validation: Automatic validation using schema rules
  • Query Builder: Chainable query methods
  • Multi-tenancy: Automatic tenant filtering when enabled
  • Hooks: Execute code before/after operations

How it works

Models are created from Schemas using the model() function. When you perform operations (create, find, update), the Model validates data against the schema, applies defaults and setters, executes hooks, and manages indexes. Models automatically create collections and indexes on first use.

Exports

  • Model - Main Model class
  • model() - Factory function to create models
  • Document - Document class representing individual documents
  • Subdocument - For nested documents
  • ModelOptions - TypeScript type for model options
  • ModelStatic - TypeScript type for model static methods

Model Static Methods

  • model<T>(name: string, schema: Schema, options?: ModelOptions): Model<T> - Create model
  • Model.create(data: Partial<T> | Partial<T>[]): Promise<Document<T> | Document<T>[]>
  • Model.find(filter?: Record<string, any>): Query<T> - Find documents
  • Model.findOne(filter?: Record<string, any>): Promise<Document<T> | null>
  • Model.findById(id: string): Promise<Document<T> | null>
  • Model.updateOne(filter: Record<string, any>, update: Partial<T>): Promise<void>
  • Model.updateMany(filter: Record<string, any>, update: Partial<T>): Promise<number>
  • Model.findOneAndUpdate(filter: Record<string, any>, update: Partial<T>): Promise<Document<T> | null>
  • Model.deleteOne(filter: Record<string, any>): Promise<void>
  • Model.deleteMany(filter: Record<string, any>): Promise<number>
  • Model.findOneAndDelete(filter: Record<string, any>): Promise<Document<T> | null>
  • Model.count(filter?: Record<string, any>): Promise<number>

Document Instance Methods

  • document.save(options?: { validateBeforeSave?: boolean }): Promise<this>
  • document.remove(): Promise<void>
  • document.isModified(field?: string): boolean
  • document.getModifiedPaths(): string[]
  • document.toObject(): any
  • document.toJSON(): any

TypeScript Types

interface ModelOptions {
  connection?: Database;
  tenantEnabled?: boolean;
  tenantField?: string;
}

interface ArangoDocument {
  _id?: string;
  _key?: string;
  _rev?: string;
  [key: string]: any;
}
📖 For usage examples and best practices: See Models & Schemas Guide for detailed model documentation, examples, and best practices.