mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-19 13:56:04 +00:00
public commit
This commit is contained in:
46
app/src/data/fields/indices/EntityIndex.ts
Normal file
46
app/src/data/fields/indices/EntityIndex.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import type { Entity } from "../../entities";
|
||||
import { Field } from "../Field";
|
||||
|
||||
export class EntityIndex {
|
||||
constructor(
|
||||
public entity: Entity,
|
||||
public fields: Field[],
|
||||
public unique: boolean = false,
|
||||
public name?: string
|
||||
) {
|
||||
if (fields.length === 0) {
|
||||
throw new Error("Indices must contain at least one field");
|
||||
}
|
||||
if (fields.some((f) => !(f instanceof Field))) {
|
||||
throw new Error("All fields must be instances of Field");
|
||||
}
|
||||
|
||||
if (unique) {
|
||||
const firstRequired = fields[0]?.isRequired();
|
||||
if (!firstRequired) {
|
||||
throw new Error(
|
||||
`Unique indices must have first field as required: ${fields
|
||||
.map((f) => f.name)
|
||||
.join(", ")}`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (!name) {
|
||||
this.name = [
|
||||
unique ? "idx_unique" : "idx",
|
||||
entity.name,
|
||||
...fields.map((f) => f.name)
|
||||
].join("_");
|
||||
}
|
||||
}
|
||||
|
||||
toJSON() {
|
||||
return {
|
||||
entity: this.entity.name,
|
||||
fields: this.fields.map((f) => f.name),
|
||||
//name: this.name,
|
||||
unique: this.unique
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user