Models
In DolphJS, Models represent the data structure of your application and handle direct interactions with your database. While DolphJS does not enforce a specific ORM, it provides excellent integration and auto-initialization for Mongoose (MongoDB) and Sequelize (MySQL/PostgreSQL).
Defining a Mongoose Model
Section titled “Defining a Mongoose Model”If you have configured mongo in your dolph_config.yaml, the factory automatically connects to the database. You only need to define your schemas using standard Mongoose syntax.
import { Schema, Document, model } from 'mongoose';
export interface IUser extends Document { name: string; email: string; age: number;}
const UserSchema = new Schema( { name: { type: String, required: true, }, email: { type: String, required: true, unique: true, }, age: { type: Number, default: 18, }, }, { timestamps: true });
export const UserModel = model<IUser>('User', UserSchema);Using the Model in a Service
Section titled “Using the Model in a Service”Once your model is defined, you inject it into your Service using the @InjectMongo or @InjectMySQL decorators provided by Dolph.
import { DolphServiceHandler } from '@dolphjs/dolph/classes';import { Dolph } from '@dolphjs/dolph/common';import { InjectMongo } from '@dolphjs/dolph/decorators';import { Model } from 'mongoose';import { UserModel, IUser } from './user.model';
@InjectMongo('userModel', UserModel)export class UserService extends DolphServiceHandler<Dolph> { // The property name MUST match the first argument of @InjectMongo userModel!: Model<IUser>;
constructor() { super('userService'); }
async createUser(data: any) { // We can now use Mongoose methods directly const user = new this.userModel(data); return await user.save(); }}Validation Integration
Section titled “Validation Integration”While Mongoose handles database-level validation, you should typically validate incoming requests at the Controller level using DTOs (Data Transfer Objects) and class-validator before passing the data to the Model.
// DTO Exampleimport { IsString, IsEmail, IsNumber } from 'class-validator';
export class CreateUserDto { @IsString() name: string;
@IsEmail() email: string;
@IsNumber() age: number;}This ensures your Model only ever receives sanitized, valid data.