Files
DuckQ1u 93d1b7c3d3
Copilot Setup Steps / copilot-setup-steps (push) Has been cancelled
first commit
2026-04-22 19:51:20 +07:00

39 lines
1.3 KiB
TypeScript

import type {PersistenceAdapter} from './persistence/adapter';
export abstract class Factory<TOptions extends Record<string, unknown> = Record<string, unknown>, TResult = TOptions> {
abstract entityType: string;
protected adapter?: PersistenceAdapter;
constructor(adapter?: PersistenceAdapter) {
this.adapter = adapter;
}
abstract build(options?: Partial<TOptions>): TResult;
buildMany(optionsList: Partial<TOptions>[]): TResult[] {
return optionsList.map(options => this.build(options));
}
async create(options?: Partial<TOptions>): Promise<TResult> {
if (!this.adapter) {
throw new Error('Cannot create without a persistence adapter. Use build() for in-memory objects.');
}
const data = this.build(options);
return await this.adapter.insert(this.entityType, data) as Promise<TResult>;
}
async createMany(optionsList: Partial<TOptions>[]): Promise<TResult[]> {
if (!this.adapter) {
throw new Error('Cannot create without a persistence adapter. Use buildMany() for in-memory objects.');
}
const results: TResult[] = [];
for (const options of optionsList) {
const result = await this.create(options);
results.push(result);
}
return results;
}
}