adonis-odm
Version:
A comprehensive MongoDB ODM for AdonisJS with Lucid-style API, type-safe relationships, embedded documents, and transaction support
124 lines (123 loc) • 3.65 kB
JavaScript
/**
* Abstract base class for all database seeders
*
* This class provides the foundation for creating database seeders that can populate
* MongoDB collections with initial or test data. All seeders must extend this class
* and implement the abstract `run()` method.
*
* @example
* ```typescript
* import { BaseSeeder } from 'adonis-odm/seeders'
* import User from '#models/user'
*
* export default class UserSeeder extends BaseSeeder {
* static environment = ['development', 'testing']
*
* async run() {
* await User.createMany([
* { name: 'John Doe', email: 'john@example.com' },
* { name: 'Jane Smith', email: 'jane@example.com' },
* ])
* }
* }
* ```
*/
export class BaseSeeder {
/**
* Environment restrictions for the seeder
*
* If specified, the seeder will only run in the listed environments.
* If not specified, the seeder will run in all environments.
*
* @example
* ```typescript
* static environment = ['development', 'testing']
* ```
*/
static environment;
/**
* Execution order for the seeder
*
* Seeders with lower order values run first. If not specified, defaults to 999.
* Main seeders (index.ts or main.ts) are automatically given order 0.
*
* @example
* ```typescript
* static order = 1
* ```
*/
static order;
/**
* Dependencies for the seeder
*
* Array of seeder class names that must run before this seeder.
* Dependencies are resolved using topological sorting.
*
* @example
* ```typescript
* static dependencies = ['UserSeeder', 'RoleSeeder']
* ```
*/
static dependencies;
/**
* Database manager instance providing access to MongoDB connections
*/
client;
/**
* Optional connection name to use for this seeder
*
* If not specified, the default connection will be used.
* Useful for multi-tenant scenarios where different seeders
* need to target different databases.
*/
connection;
/**
* Create a new seeder instance
*
* @param client - The MongoDB database manager instance
* @param connection - Optional connection name to use
*/
constructor(client, connection) {
this.client = client;
this.connection = connection;
}
/**
* Get the database instance for the configured connection
*
* @returns The MongoDB database instance
*/
getDatabase() {
return this.client.db(this.connection);
}
/**
* Get a collection instance for the configured connection
*
* @param name - The collection name
* @returns The MongoDB collection instance
*/
getCollection(name) {
return this.client.collection(name, this.connection);
}
/**
* Check if the seeder should run in the current environment
*
* @param currentEnvironment - The current environment (e.g., 'development', 'production')
* @returns True if the seeder should run, false otherwise
*/
static shouldRun(currentEnvironment) {
// If no environment restrictions are specified, run in all environments
if (!this.environment || this.environment.length === 0) {
return true;
}
// Check if current environment is in the allowed list
return this.environment.includes(currentEnvironment);
}
/**
* Get the seeder class name for logging and identification
*
* @returns The class name of the seeder
*/
static getSeederName() {
return this.name;
}
}