@iarayan/ch-orm
Version:
A Developer-First ClickHouse ORM with Powerful CLI Tools
71 lines (69 loc) • 2.02 kB
JavaScript
import * as fs from "fs";
import * as path from "path";
/**
* Command for seeding the database
*/
export class SeederCommand {
constructor(connection, seedersDir = "./seeders") {
this.connection = connection;
this.seedersDir = seedersDir;
}
/**
* Run all seeders
*/
async run() {
const seeders = this.getSeeders();
for (const seeder of seeders) {
await this.runSeeder(seeder);
}
}
/**
* Run a specific seeder
*/
async runSeeder(seederFile) {
const seederPath = path.join(this.seedersDir, seederFile);
const seeder = require(seederPath).default;
const instance = new seeder(this.connection);
if (typeof instance.run !== "function") {
throw new Error(`Seeder ${seederFile} must implement a run method`);
}
await instance.run();
}
/**
* Get all seeder files
*/
getSeeders() {
if (!fs.existsSync(this.seedersDir)) {
return [];
}
return fs
.readdirSync(this.seedersDir)
.filter((file) => file.endsWith(".ts") || file.endsWith(".js"))
.sort();
}
/**
* Create a new seeder file
*/
createSeeder(name) {
const template = `import { Seeder } from '@iarayan/ch-orm';
export default class ${name}Seeder implements Seeder {
constructor(private connection: any) {}
public async run(): Promise<void> {
// Add your seeding logic here
await this.connection.query(\`
-- Your seeding SQL here
\`);
}
}
`;
if (!fs.existsSync(this.seedersDir)) {
fs.mkdirSync(this.seedersDir, { recursive: true });
}
const timestamp = new Date().getTime();
const filename = `${timestamp}_${name.toLowerCase()}_seeder.ts`;
const filePath = path.join(this.seedersDir, filename);
fs.writeFileSync(filePath, template);
return filePath;
}
}
//# sourceMappingURL=SeederCommand.js.map