Kaizen
Browse modulesRulesrules/serverClasses

Class: RuleVersionRepository

Defined in: server/rules/rule-version-repository.ts:33

Repository for version entities in parent-child versioning patterns.

Use for entities that:

  • link to a parent entity via parentId
  • order versions by versionNumber
  • are append-only (immutable once created)

Provides findByParentId, findLatestByParentId, getNextVersionNumber, countByParentId, and an audit-aware create.

Extends

  • VersionRepository<PrismaClient, RuleVersions, RuleVersionCreateInput, string, Tx<PrismaClient>>

Constructors

Constructor

new RuleVersionRepository(prismaClient: PrismaClientLike<Tx<PrismaClient<PrismaClientOptions, never, DefaultArgs>>> & PrismaClient<PrismaClientOptions, never, DefaultArgs>, options?: RepositoryOptions): RuleVersionRepository;

Defined in: server/persistence/base-repository.ts:68

Parameters

ParameterType
prismaClientPrismaClientLike<Tx<PrismaClient<PrismaClientOptions, never, DefaultArgs>>> & PrismaClient<PrismaClientOptions, never, DefaultArgs>
optionsRepositoryOptions

Returns

RuleVersionRepository

Inherited from

VersionRepository.constructor

Properties

ENTITY_TYPE?

protected readonly optional ENTITY_TYPE?: string;

Defined in: server/persistence/base-repository.ts:61

Set ENTITY_TYPE in child repositories to enable automatic activity logging. When set, create/update/delete operations auto-log unless silenced with .silent. Requires an activity model on the consumer's Prisma schema.

Inherited from

VersionRepository.ENTITY_TYPE


logger?

protected readonly optional logger?: Logger;

Defined in: server/persistence/base-repository.ts:64

Inherited from

VersionRepository.logger


prismaClient

protected prismaClient: PrismaClientLike<Tx<PrismaClient<PrismaClientOptions, never, DefaultArgs>>> & PrismaClient<PrismaClientOptions, never, DefaultArgs>;

Defined in: server/persistence/base-repository.ts:69

Inherited from

VersionRepository.prismaClient

Accessors

actorId

Get Signature

get protected actorId(): string | null | undefined;

Defined in: server/persistence/base-repository.ts:76

Returns

string | null | undefined

Inherited from

VersionRepository.actorId


auditCreate

Get Signature

get protected auditCreate(): {
  createdBy: string | null | undefined;
  updatedBy: string | null | undefined;
};

Defined in: server/persistence/base-repository.ts:90

Returns
{
  createdBy: string | null | undefined;
  updatedBy: string | null | undefined;
}
createdBy
createdBy: string | null | undefined = actorId;
updatedBy
updatedBy: string | null | undefined = actorId;

Inherited from

VersionRepository.auditCreate


auditDelete

Get Signature

get protected auditDelete(): {
  deletedAt: Date;
  deletedBy: string | null | undefined;
};

Defined in: server/persistence/base-repository.ts:99

Returns
{
  deletedAt: Date;
  deletedBy: string | null | undefined;
}
deletedAt
deletedAt: Date;
deletedBy
deletedBy: string | null | undefined;

Inherited from

VersionRepository.auditDelete


auditUpdate

Get Signature

get protected auditUpdate(): {
  updatedBy: string | null | undefined;
};

Defined in: server/persistence/base-repository.ts:95

Returns
{
  updatedBy: string | null | undefined;
}
updatedBy
updatedBy: string | null | undefined;

Inherited from

VersionRepository.auditUpdate


silent

Get Signature

get silent(): this;

Defined in: server/persistence/base-repository.ts:115

Suppress activity logging for the next mutation call. One-shot: the flag is consumed by the next create/update/delete.

Example
await repo.silent.update(id, data);   // no activity logged
await repo.update(id, data);          // activity logged normally
Returns

this

Inherited from

VersionRepository.silent


tx

Get Signature

get protected tx(): TTx;

Defined in: server/persistence/base-repository.ts:86

The current Prisma handle: the active transaction if one is running on this async context, otherwise the raw client.

Returns

TTx

Inherited from

VersionRepository.tx

Methods

_count()

protected _count(): Promise<number>;

Defined in: server/rules/rule-version-repository.ts:57

Returns

Promise<number>

Overrides

VersionRepository._count


_countByParentId()

protected _countByParentId(ruleId: string): Promise<number>;

Defined in: server/rules/rule-version-repository.ts:85

Parameters

ParameterType
ruleIdstring

Returns

Promise<number>

Overrides

VersionRepository._countByParentId


_create()

protected _create(input: RuleVersionCreateInput): Promise<{
  changeReason: string | null;
  conditionLogic: JsonValue;
  createdAt: Date;
  createdBy: string | null;
  deletedAt: Date | null;
  deletedBy: string | null;
  domainConfig: JsonValue;
  effectiveEnd: Date | null;
  effectiveStart: Date;
  id: string;
  priority: number;
  ruleEffectiveDate: Date | null;
  ruleExpirationDate: Date | null;
  ruleId: string;
  scopes: JsonValue;
  versionNumber: number;
}>;

Defined in: server/rules/rule-version-repository.ts:91

Parameters

ParameterType
inputRuleVersionCreateInput

Returns

Promise<{ changeReason: string | null; conditionLogic: JsonValue; createdAt: Date; createdBy: string | null; deletedAt: Date | null; deletedBy: string | null; domainConfig: JsonValue; effectiveEnd: Date | null; effectiveStart: Date; id: string; priority: number; ruleEffectiveDate: Date | null; ruleExpirationDate: Date | null; ruleId: string; scopes: JsonValue; versionNumber: number; }>

Overrides

VersionRepository._create


_exists()

protected _exists(id: string): Promise<boolean>;

Defined in: server/rules/rule-version-repository.ts:50

Parameters

ParameterType
idstring

Returns

Promise<boolean>

Overrides

VersionRepository._exists


_findById()

protected _findById(id: string): Promise<
  | {
  changeReason: string | null;
  conditionLogic: JsonValue;
  createdAt: Date;
  createdBy: string | null;
  deletedAt: Date | null;
  deletedBy: string | null;
  domainConfig: JsonValue;
  effectiveEnd: Date | null;
  effectiveStart: Date;
  id: string;
  priority: number;
  ruleEffectiveDate: Date | null;
  ruleExpirationDate: Date | null;
  ruleId: string;
  scopes: JsonValue;
  versionNumber: number;
}
| null>;

Defined in: server/rules/rule-version-repository.ts:44

Parameters

ParameterType
idstring

Returns

Promise< | { changeReason: string | null; conditionLogic: JsonValue; createdAt: Date; createdBy: string | null; deletedAt: Date | null; deletedBy: string | null; domainConfig: JsonValue; effectiveEnd: Date | null; effectiveStart: Date; id: string; priority: number; ruleEffectiveDate: Date | null; ruleExpirationDate: Date | null; ruleId: string; scopes: JsonValue; versionNumber: number; } | null>

Overrides

VersionRepository._findById


_findByParentId()

protected _findByParentId(ruleId: string): Promise<{
  changeReason: string | null;
  conditionLogic: JsonValue;
  createdAt: Date;
  createdBy: string | null;
  deletedAt: Date | null;
  deletedBy: string | null;
  domainConfig: JsonValue;
  effectiveEnd: Date | null;
  effectiveStart: Date;
  id: string;
  priority: number;
  ruleEffectiveDate: Date | null;
  ruleExpirationDate: Date | null;
  ruleId: string;
  scopes: JsonValue;
  versionNumber: number;
}[]>;

Defined in: server/rules/rule-version-repository.ts:78

Parameters

ParameterType
ruleIdstring

Returns

Promise<{ changeReason: string | null; conditionLogic: JsonValue; createdAt: Date; createdBy: string | null; deletedAt: Date | null; deletedBy: string | null; domainConfig: JsonValue; effectiveEnd: Date | null; effectiveStart: Date; id: string; priority: number; ruleEffectiveDate: Date | null; ruleExpirationDate: Date | null; ruleId: string; scopes: JsonValue; versionNumber: number; }[]>

Overrides

VersionRepository._findByParentId


_findMany()

protected _findMany(pagination: {
  page: number;
  pageSize: number;
}): Promise<{
  items: {
     changeReason: string | null;
     conditionLogic: JsonValue;
     createdAt: Date;
     createdBy: string | null;
     deletedAt: Date | null;
     deletedBy: string | null;
     domainConfig: JsonValue;
     effectiveEnd: Date | null;
     effectiveStart: Date;
     id: string;
     priority: number;
     ruleEffectiveDate: Date | null;
     ruleExpirationDate: Date | null;
     ruleId: string;
     scopes: JsonValue;
     versionNumber: number;
  }[];
  total: number;
}>;

Defined in: server/rules/rule-version-repository.ts:61

Parameters

ParameterType
pagination{ page: number; pageSize: number; }
pagination.pagenumber
pagination.pageSizenumber

Returns

Promise<{ items: { changeReason: string | null; conditionLogic: JsonValue; createdAt: Date; createdBy: string | null; deletedAt: Date | null; deletedBy: string | null; domainConfig: JsonValue; effectiveEnd: Date | null; effectiveStart: Date; id: string; priority: number; ruleEffectiveDate: Date | null; ruleExpirationDate: Date | null; ruleId: string; scopes: JsonValue; versionNumber: number; }[]; total: number; }>

Overrides

VersionRepository._findMany


consumeSilent()

protected consumeSilent(): boolean;

Defined in: server/persistence/base-repository.ts:128

Consume and reset the silent flag. Called by template methods.

Returns

boolean

Inherited from

VersionRepository.consumeSilent


count()

count(): Promise<number>;

Defined in: server/persistence/base-repository.ts:191

Returns

Promise<number>

Inherited from

VersionRepository.count


countByParentId()

countByParentId(parentId: string): Promise<number>;

Defined in: server/persistence/base-repository.ts:261

Parameters

ParameterType
parentIdstring

Returns

Promise<number>

Inherited from

VersionRepository.countByParentId


create()

create(input: RuleVersionCreateInput): Promise<{
  changeReason: string | null;
  conditionLogic: JsonValue;
  createdAt: Date;
  createdBy: string | null;
  deletedAt: Date | null;
  deletedBy: string | null;
  domainConfig: JsonValue;
  effectiveEnd: Date | null;
  effectiveStart: Date;
  id: string;
  priority: number;
  ruleEffectiveDate: Date | null;
  ruleExpirationDate: Date | null;
  ruleId: string;
  scopes: JsonValue;
  versionNumber: number;
}>;

Defined in: server/persistence/base-repository.ts:265

Parameters

ParameterType
inputRuleVersionCreateInput

Returns

Promise<{ changeReason: string | null; conditionLogic: JsonValue; createdAt: Date; createdBy: string | null; deletedAt: Date | null; deletedBy: string | null; domainConfig: JsonValue; effectiveEnd: Date | null; effectiveStart: Date; id: string; priority: number; ruleEffectiveDate: Date | null; ruleExpirationDate: Date | null; ruleId: string; scopes: JsonValue; versionNumber: number; }>

Inherited from

VersionRepository.create


createCopy()

createCopy(
   source: {
  changeReason: string | null;
  conditionLogic: JsonValue;
  createdAt: Date;
  createdBy: string | null;
  deletedAt: Date | null;
  deletedBy: string | null;
  domainConfig: JsonValue;
  effectiveEnd: Date | null;
  effectiveStart: Date;
  id: string;
  priority: number;
  ruleEffectiveDate: Date | null;
  ruleExpirationDate: Date | null;
  ruleId: string;
  scopes: JsonValue;
  versionNumber: number;
}, 
   versionNumber: number, 
   overrides: RuleVersionCopyOverrides
): Promise<{
  changeReason: string | null;
  conditionLogic: JsonValue;
  createdAt: Date;
  createdBy: string | null;
  deletedAt: Date | null;
  deletedBy: string | null;
  domainConfig: JsonValue;
  effectiveEnd: Date | null;
  effectiveStart: Date;
  id: string;
  priority: number;
  ruleEffectiveDate: Date | null;
  ruleExpirationDate: Date | null;
  ruleId: string;
  scopes: JsonValue;
  versionNumber: number;
}>;

Defined in: server/rules/rule-version-repository.ts:157

Copy a version verbatim, replacing only explicitly supplied fields.

Builds a RuleVersionCreateInput and goes through create() — the template method — rather than issuing its own this.tx.ruleVersions.create. _create stays the ONE place that maps a version onto Prisma's data shape, which is the whole point of this class's shape (review finding).

The hand-written second payload it replaces had already drifted: _create populates effectiveStart conditionally, the copy omitted the field outright. Both reached the same result via @default(now()), so nothing was broken — but two independent transcriptions of a ~10-field mapping with nothing keeping them in step is a field-addition away from a real bug, and the copy's own comment ("mirror _create") documented the duplication instead of removing it.

effectiveStart is still left out, now deliberately and in one place: a copy is a NEW version, so it starts now rather than inheriting the source's start. That is exactly what _create's ...(input.effectiveStart && {...}) does with an absent value, so routing through it preserves today's behaviour.

fromJsonColumn re-applies the domain types that toJsonColumn erased on the way in — the paired read boundary, so this adds no new assertion. Its type argument is left to contextual inference from RuleVersionCreateInput rather than spelled out: writing <Record<string, unknown>> here would both restate a type the input already declares and add a no-unknown warning to the ratchet.

Parameters

ParameterType
source{ changeReason: string | null; conditionLogic: JsonValue; createdAt: Date; createdBy: string | null; deletedAt: Date | null; deletedBy: string | null; domainConfig: JsonValue; effectiveEnd: Date | null; effectiveStart: Date; id: string; priority: number; ruleEffectiveDate: Date | null; ruleExpirationDate: Date | null; ruleId: string; scopes: JsonValue; versionNumber: number; }
source.changeReasonstring | null
source.conditionLogicJsonValue
source.createdAtDate
source.createdBystring | null
source.deletedAtDate | null
source.deletedBystring | null
source.domainConfigJsonValue
source.effectiveEndDate | null
source.effectiveStartDate
source.idstring
source.prioritynumber
source.ruleEffectiveDateDate | null
source.ruleExpirationDateDate | null
source.ruleIdstring
source.scopesJsonValue
source.versionNumbernumber
versionNumbernumber
overridesRuleVersionCopyOverrides

Returns

Promise<{ changeReason: string | null; conditionLogic: JsonValue; createdAt: Date; createdBy: string | null; deletedAt: Date | null; deletedBy: string | null; domainConfig: JsonValue; effectiveEnd: Date | null; effectiveStart: Date; id: string; priority: number; ruleEffectiveDate: Date | null; ruleExpirationDate: Date | null; ruleId: string; scopes: JsonValue; versionNumber: number; }>


exists()

exists(id: string): Promise<boolean>;

Defined in: server/persistence/base-repository.ts:187

Parameters

ParameterType
idstring

Returns

Promise<boolean>

Inherited from

VersionRepository.exists


findById()

findById(id: string): Promise<
  | {
  changeReason: string | null;
  conditionLogic: JsonValue;
  createdAt: Date;
  createdBy: string | null;
  deletedAt: Date | null;
  deletedBy: string | null;
  domainConfig: JsonValue;
  effectiveEnd: Date | null;
  effectiveStart: Date;
  id: string;
  priority: number;
  ruleEffectiveDate: Date | null;
  ruleExpirationDate: Date | null;
  ruleId: string;
  scopes: JsonValue;
  versionNumber: number;
}
| null>;

Defined in: server/persistence/base-repository.ts:177

Parameters

ParameterType
idstring

Returns

Promise< | { changeReason: string | null; conditionLogic: JsonValue; createdAt: Date; createdBy: string | null; deletedAt: Date | null; deletedBy: string | null; domainConfig: JsonValue; effectiveEnd: Date | null; effectiveStart: Date; id: string; priority: number; ruleEffectiveDate: Date | null; ruleExpirationDate: Date | null; ruleId: string; scopes: JsonValue; versionNumber: number; } | null>

Inherited from

VersionRepository.findById


findByIdOrThrow()

findByIdOrThrow(id: string): Promise<{
  changeReason: string | null;
  conditionLogic: JsonValue;
  createdAt: Date;
  createdBy: string | null;
  deletedAt: Date | null;
  deletedBy: string | null;
  domainConfig: JsonValue;
  effectiveEnd: Date | null;
  effectiveStart: Date;
  id: string;
  priority: number;
  ruleEffectiveDate: Date | null;
  ruleExpirationDate: Date | null;
  ruleId: string;
  scopes: JsonValue;
  versionNumber: number;
}>;

Defined in: server/persistence/base-repository.ts:181

Parameters

ParameterType
idstring

Returns

Promise<{ changeReason: string | null; conditionLogic: JsonValue; createdAt: Date; createdBy: string | null; deletedAt: Date | null; deletedBy: string | null; domainConfig: JsonValue; effectiveEnd: Date | null; effectiveStart: Date; id: string; priority: number; ruleEffectiveDate: Date | null; ruleExpirationDate: Date | null; ruleId: string; scopes: JsonValue; versionNumber: number; }>

Inherited from

VersionRepository.findByIdOrThrow


findByParentId()

findByParentId(parentId: string): Promise<{
  changeReason: string | null;
  conditionLogic: JsonValue;
  createdAt: Date;
  createdBy: string | null;
  deletedAt: Date | null;
  deletedBy: string | null;
  domainConfig: JsonValue;
  effectiveEnd: Date | null;
  effectiveStart: Date;
  id: string;
  priority: number;
  ruleEffectiveDate: Date | null;
  ruleExpirationDate: Date | null;
  ruleId: string;
  scopes: JsonValue;
  versionNumber: number;
}[]>;

Defined in: server/persistence/base-repository.ts:233

Parameters

ParameterType
parentIdstring

Returns

Promise<{ changeReason: string | null; conditionLogic: JsonValue; createdAt: Date; createdBy: string | null; deletedAt: Date | null; deletedBy: string | null; domainConfig: JsonValue; effectiveEnd: Date | null; effectiveStart: Date; id: string; priority: number; ruleEffectiveDate: Date | null; ruleExpirationDate: Date | null; ruleId: string; scopes: JsonValue; versionNumber: number; }[]>

Inherited from

VersionRepository.findByParentId


findByRuleId()

findByRuleId(ruleId: string): Promise<{
  changeReason: string | null;
  conditionLogic: JsonValue;
  createdAt: Date;
  createdBy: string | null;
  deletedAt: Date | null;
  deletedBy: string | null;
  domainConfig: JsonValue;
  effectiveEnd: Date | null;
  effectiveStart: Date;
  id: string;
  priority: number;
  ruleEffectiveDate: Date | null;
  ruleExpirationDate: Date | null;
  ruleId: string;
  scopes: JsonValue;
  versionNumber: number;
}[]>;

Defined in: server/rules/rule-version-repository.ts:117

Parameters

ParameterType
ruleIdstring

Returns

Promise<{ changeReason: string | null; conditionLogic: JsonValue; createdAt: Date; createdBy: string | null; deletedAt: Date | null; deletedBy: string | null; domainConfig: JsonValue; effectiveEnd: Date | null; effectiveStart: Date; id: string; priority: number; ruleEffectiveDate: Date | null; ruleExpirationDate: Date | null; ruleId: string; scopes: JsonValue; versionNumber: number; }[]>


findLatestByParentId()

findLatestByParentId(parentId: string): Promise<
  | {
  changeReason: string | null;
  conditionLogic: JsonValue;
  createdAt: Date;
  createdBy: string | null;
  deletedAt: Date | null;
  deletedBy: string | null;
  domainConfig: JsonValue;
  effectiveEnd: Date | null;
  effectiveStart: Date;
  id: string;
  priority: number;
  ruleEffectiveDate: Date | null;
  ruleExpirationDate: Date | null;
  ruleId: string;
  scopes: JsonValue;
  versionNumber: number;
}
| null>;

Defined in: server/persistence/base-repository.ts:237

Parameters

ParameterType
parentIdstring

Returns

Promise< | { changeReason: string | null; conditionLogic: JsonValue; createdAt: Date; createdBy: string | null; deletedAt: Date | null; deletedBy: string | null; domainConfig: JsonValue; effectiveEnd: Date | null; effectiveStart: Date; id: string; priority: number; ruleEffectiveDate: Date | null; ruleExpirationDate: Date | null; ruleId: string; scopes: JsonValue; versionNumber: number; } | null>

Inherited from

VersionRepository.findLatestByParentId


findLatestByRuleId()

findLatestByRuleId(ruleId: string): Promise<
  | {
  changeReason: string | null;
  conditionLogic: JsonValue;
  createdAt: Date;
  createdBy: string | null;
  deletedAt: Date | null;
  deletedBy: string | null;
  domainConfig: JsonValue;
  effectiveEnd: Date | null;
  effectiveStart: Date;
  id: string;
  priority: number;
  ruleEffectiveDate: Date | null;
  ruleExpirationDate: Date | null;
  ruleId: string;
  scopes: JsonValue;
  versionNumber: number;
}
| null>;

Defined in: server/rules/rule-version-repository.ts:121

Parameters

ParameterType
ruleIdstring

Returns

Promise< | { changeReason: string | null; conditionLogic: JsonValue; createdAt: Date; createdBy: string | null; deletedAt: Date | null; deletedBy: string | null; domainConfig: JsonValue; effectiveEnd: Date | null; effectiveStart: Date; id: string; priority: number; ruleEffectiveDate: Date | null; ruleExpirationDate: Date | null; ruleId: string; scopes: JsonValue; versionNumber: number; } | null>


findMany()

findMany(pagination: {
  page: number;
  pageSize: number;
}): Promise<OffsetPaginatedResult<{
  changeReason: string | null;
  conditionLogic: JsonValue;
  createdAt: Date;
  createdBy: string | null;
  deletedAt: Date | null;
  deletedBy: string | null;
  domainConfig: JsonValue;
  effectiveEnd: Date | null;
  effectiveStart: Date;
  id: string;
  priority: number;
  ruleEffectiveDate: Date | null;
  ruleExpirationDate: Date | null;
  ruleId: string;
  scopes: JsonValue;
  versionNumber: number;
}>>;

Defined in: server/persistence/base-repository.ts:195

Parameters

ParameterType
pagination{ page: number; pageSize: number; }
pagination.pagenumber
pagination.pageSizenumber

Returns

Promise<OffsetPaginatedResult<{ changeReason: string | null; conditionLogic: JsonValue; createdAt: Date; createdBy: string | null; deletedAt: Date | null; deletedBy: string | null; domainConfig: JsonValue; effectiveEnd: Date | null; effectiveStart: Date; id: string; priority: number; ruleEffectiveDate: Date | null; ruleExpirationDate: Date | null; ruleId: string; scopes: JsonValue; versionNumber: number; }>>

Inherited from

VersionRepository.findMany


getNextVersionNumber()

getNextVersionNumber(parentId: string): Promise<number>;

Defined in: server/persistence/base-repository.ts:256

Next version number = highest versionNumber among the rows _findByParentId returns, plus one. Subclasses typically filter _findByParentId to deletedAt: null, so this counts only live rows.

That is collision-safe only while the subclass never soft-deletes version rows. If a subclass gains a discard/soft-delete path AND its @@unique(parentId, versionNumber) constraint counts soft-deleted rows (Postgres unique indexes do), reusing a discarded row's number will violate the constraint. Such a subclass must override to compute the max over all rows including soft-deleted — see FormSchemaVersionRepository.nextVersionNumber. Promote that into a shared abstract hook here once a second repo needs it.

Parameters

ParameterType
parentIdstring

Returns

Promise<number>

Inherited from

VersionRepository.getNextVersionNumber


logActivity()

protected logActivity(
   _entityId: string, 
   _action: string, 
   _metadata?: Record<string, unknown>
): Promise<void>;

Defined in: server/persistence/base-repository.ts:140

Log an activity entry.

NOTE: This is not implemented because there's no activity model implemented in taproot yet! https://linear.app/kaizenlabs/issue/ENG-7846/add-activity-log-tables

Parameters

ParameterType
_entityIdstring
_actionstring
_metadata?Record<string, unknown>

Returns

Promise<void>

Inherited from

VersionRepository.logActivity


nextVersionNumber()

nextVersionNumber(ruleId: string): Promise<number>;

Defined in: server/rules/rule-version-repository.ts:125

Parameters

ParameterType
ruleIdstring

Returns

Promise<number>


notFoundError()

protected notFoundError(id: string): Error;

Defined in: server/rules/rule-version-repository.ts:40

Create a not-found error. Override to provide custom error types.

Parameters

ParameterType
idstring

Returns

Error

Overrides

VersionRepository.notFoundError

On this page