Initial Commit
This commit is contained in:
119
packages/persistence/database/seed/src/builder/builder.ts
Normal file
119
packages/persistence/database/seed/src/builder/builder.ts
Normal file
@@ -0,0 +1,119 @@
|
||||
import type {
|
||||
SeedDataFactory,
|
||||
SeedDefinition,
|
||||
SeedEnvironment,
|
||||
SeedRecord,
|
||||
UpsertStrategy,
|
||||
} from "../types/common.js";
|
||||
import type { ISeedBuilder } from "./builder.types.js";
|
||||
|
||||
/**
|
||||
* Fluent builder for SeedDefinition objects. Tracks which optional fields the
|
||||
* caller has set so `build()` can omit unset fields entirely (no empty arrays,
|
||||
* no empty strings, no `truncate: false` slipping through to the output).
|
||||
*/
|
||||
export class SeedBuilder implements ISeedBuilder {
|
||||
private readonly _name: string;
|
||||
private _table: string;
|
||||
private _environments: SeedEnvironment[] = [];
|
||||
private _dependencies: string[] = [];
|
||||
private _data: SeedRecord[] | SeedDataFactory = [];
|
||||
private _upsertStrategy: UpsertStrategy = "skip";
|
||||
private _upsertKeys: string[] = [];
|
||||
private _description = "";
|
||||
private _priority = 0;
|
||||
private _truncate = false;
|
||||
|
||||
constructor(name: string) {
|
||||
this._name = name;
|
||||
this._table = name;
|
||||
}
|
||||
|
||||
table(table: string): this {
|
||||
this._table = table;
|
||||
return this;
|
||||
}
|
||||
|
||||
environments(...envs: SeedEnvironment[]): this {
|
||||
this._environments = envs;
|
||||
return this;
|
||||
}
|
||||
|
||||
dependsOn(...names: string[]): this {
|
||||
this._dependencies = names;
|
||||
return this;
|
||||
}
|
||||
|
||||
data(records: SeedRecord[]): this {
|
||||
this._data = records;
|
||||
return this;
|
||||
}
|
||||
|
||||
dataFactory(factory: SeedDataFactory): this {
|
||||
this._data = factory;
|
||||
return this;
|
||||
}
|
||||
|
||||
upsert(strategy: UpsertStrategy, keys: string[]): this {
|
||||
this._upsertStrategy = strategy;
|
||||
this._upsertKeys = keys;
|
||||
return this;
|
||||
}
|
||||
|
||||
description(desc: string): this {
|
||||
this._description = desc;
|
||||
return this;
|
||||
}
|
||||
|
||||
priority(p: number): this {
|
||||
this._priority = p;
|
||||
return this;
|
||||
}
|
||||
|
||||
truncateFirst(enabled = true): this {
|
||||
this._truncate = enabled;
|
||||
return this;
|
||||
}
|
||||
|
||||
build(): SeedDefinition {
|
||||
const definition: {
|
||||
name: string;
|
||||
table: string;
|
||||
environments?: SeedEnvironment[];
|
||||
dependencies?: string[];
|
||||
data: SeedRecord[] | SeedDataFactory;
|
||||
upsertStrategy: UpsertStrategy;
|
||||
upsertKeys?: string[];
|
||||
description?: string;
|
||||
priority: number;
|
||||
truncate?: boolean;
|
||||
} = {
|
||||
name: this._name,
|
||||
table: this._table,
|
||||
data: this._data,
|
||||
upsertStrategy: this._upsertStrategy,
|
||||
priority: this._priority,
|
||||
};
|
||||
if (this._environments.length > 0) {
|
||||
definition.environments = this._environments;
|
||||
}
|
||||
if (this._dependencies.length > 0) {
|
||||
definition.dependencies = this._dependencies;
|
||||
}
|
||||
if (this._upsertKeys.length > 0) {
|
||||
definition.upsertKeys = this._upsertKeys;
|
||||
}
|
||||
if (this._description.length > 0) {
|
||||
definition.description = this._description;
|
||||
}
|
||||
if (this._truncate) {
|
||||
definition.truncate = true;
|
||||
}
|
||||
return definition;
|
||||
}
|
||||
}
|
||||
|
||||
/** Convenience function for creating a SeedBuilder. */
|
||||
export function defineSeed(name: string): SeedBuilder {
|
||||
return new SeedBuilder(name);
|
||||
}
|
||||
Reference in New Issue
Block a user