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 classmodel()- Factory function to create modelsDocument- Document class representing individual documentsSubdocument- For nested documentsModelOptions- TypeScript type for model optionsModelStatic- TypeScript type for model static methods
Model Static Methods
model<T>(name: string, schema: Schema, options?: ModelOptions): Model<T>- Create modelModel.create(data: Partial<T> | Partial<T>[]): Promise<Document<T> | Document<T>[]>Model.find(filter?: Record<string, any>): Query<T>- Find documentsModel.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): booleandocument.getModifiedPaths(): string[]document.toObject(): anydocument.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.