182 lines
6.2 KiB
TypeScript
182 lines
6.2 KiB
TypeScript
import { getDbClient } from "./client.server.js";
|
|
import type { EntityRelationship, Prisma, RowRelationship } from "./prisma-browser.js";
|
|
|
|
export type LegacyRowRelationshipEntity = {
|
|
id: string;
|
|
};
|
|
|
|
export type LegacyRowRelationshipDetails = Pick<RowRelationship, "id" | "relationshipId" | "parentId" | "childId" | "metadata" | "createdAt"> & {
|
|
relationship: Pick<EntityRelationship, "id" | "parentId" | "childId">;
|
|
};
|
|
|
|
export type LegacyEntityRelationshipDetails = Pick<EntityRelationship, "id" | "parentId" | "childId">;
|
|
|
|
export type LegacyRowRelationshipCreateInput = {
|
|
relationshipId: string;
|
|
parentId: string;
|
|
childId: string;
|
|
tenantId?: string | null;
|
|
metadata?: string | null;
|
|
};
|
|
|
|
export type LegacyRowRelationshipDeleteByIdInput = Pick<RowRelationship, "id" | "relationshipId" | "parentId" | "childId"> & {
|
|
tenantId: string | null;
|
|
};
|
|
|
|
export type LegacyRelationshipClient = {
|
|
rowRelationship: {
|
|
findUnique(args: {
|
|
where: { id: string };
|
|
include: typeof legacyRowRelationshipInclude;
|
|
}): Promise<LegacyRowRelationshipDetails | null>;
|
|
findFirst(args: {
|
|
where: Prisma.RowRelationshipWhereInput;
|
|
include: typeof legacyRowRelationshipInclude;
|
|
}): Promise<LegacyRowRelationshipDetails | null>;
|
|
create(args: {
|
|
data: LegacyRowRelationshipCreateInput;
|
|
include: typeof legacyRowRelationshipInclude;
|
|
}): Promise<LegacyRowRelationshipDetails>;
|
|
deleteMany(args: { where: Prisma.RowRelationshipWhereInput }): Promise<Prisma.BatchPayload>;
|
|
};
|
|
entityRelationship: {
|
|
findFirst(args: {
|
|
where: Prisma.EntityRelationshipWhereInput;
|
|
select: typeof selectLegacyEntityRelationshipProperties;
|
|
}): Promise<LegacyEntityRelationshipDetails | null>;
|
|
};
|
|
row: {
|
|
findFirst(args: {
|
|
where: Prisma.RowWhereInput;
|
|
select: { id: true };
|
|
}): Promise<LegacyRowRelationshipEntity | null>;
|
|
};
|
|
};
|
|
|
|
export const legacyRowRelationshipInclude = {
|
|
relationship: { select: { id: true, parentId: true, childId: true } },
|
|
} as const;
|
|
|
|
const selectLegacyEntityRelationshipProperties = {
|
|
id: true,
|
|
parentId: true,
|
|
childId: true,
|
|
} as const;
|
|
|
|
export async function findLegacyRowRelationshipById(
|
|
id: string,
|
|
tenantIdOrClient?: string | null | LegacyRelationshipClient,
|
|
client?: LegacyRelationshipClient,
|
|
): Promise<LegacyRowRelationshipDetails | null> {
|
|
const tenantId = typeof tenantIdOrClient === "string" || tenantIdOrClient === null ? tenantIdOrClient : undefined;
|
|
const db = tenantIdOrClient && typeof tenantIdOrClient === "object" ? tenantIdOrClient : client ?? (getDbClient() as unknown as LegacyRelationshipClient);
|
|
|
|
if (tenantId !== undefined) {
|
|
return db.rowRelationship.findFirst({
|
|
where: {
|
|
id,
|
|
parent: { tenantId, deletedAt: null },
|
|
child: { tenantId, deletedAt: null },
|
|
},
|
|
include: legacyRowRelationshipInclude,
|
|
});
|
|
}
|
|
|
|
return db.rowRelationship.findUnique({
|
|
where: { id },
|
|
include: legacyRowRelationshipInclude,
|
|
});
|
|
}
|
|
|
|
export async function createLegacyRowRelationship(
|
|
data: LegacyRowRelationshipCreateInput,
|
|
client: LegacyRelationshipClient = getDbClient() as unknown as LegacyRelationshipClient,
|
|
): Promise<LegacyRowRelationshipDetails> {
|
|
if (data.tenantId !== undefined) {
|
|
const entityRelationship = await client.entityRelationship.findFirst({
|
|
where: { id: data.relationshipId },
|
|
select: selectLegacyEntityRelationshipProperties,
|
|
});
|
|
if (!entityRelationship) {
|
|
throw new Error("Entity relationship not found.");
|
|
}
|
|
|
|
const [parent, child] = await Promise.all([
|
|
client.row.findFirst({
|
|
where: { id: data.parentId, tenantId: data.tenantId, entityId: entityRelationship.parentId, deletedAt: null },
|
|
select: { id: true },
|
|
}),
|
|
client.row.findFirst({
|
|
where: { id: data.childId, tenantId: data.tenantId, entityId: entityRelationship.childId, deletedAt: null },
|
|
select: { id: true },
|
|
}),
|
|
]);
|
|
|
|
if (!parent || !child) {
|
|
throw new Error("Relationship rows not found.");
|
|
}
|
|
}
|
|
|
|
const createData = { relationshipId: data.relationshipId, parentId: data.parentId, childId: data.childId };
|
|
return client.rowRelationship.create({
|
|
data: { ...createData, metadata: data.metadata ?? null },
|
|
include: legacyRowRelationshipInclude,
|
|
});
|
|
}
|
|
|
|
export async function deleteLegacyRowRelationshipById(
|
|
{ id, relationshipId, parentId, childId, tenantId }: LegacyRowRelationshipDeleteByIdInput,
|
|
client: LegacyRelationshipClient = getDbClient() as unknown as LegacyRelationshipClient,
|
|
): Promise<Prisma.BatchPayload> {
|
|
const deleted = await client.rowRelationship.deleteMany({
|
|
where: {
|
|
id,
|
|
relationshipId,
|
|
parentId,
|
|
childId,
|
|
parent: { tenantId, deletedAt: null },
|
|
child: { tenantId, deletedAt: null },
|
|
},
|
|
});
|
|
if (deleted.count !== 1) {
|
|
throw new Error(`Relationship ${id} not found`);
|
|
}
|
|
return deleted;
|
|
}
|
|
|
|
export async function deleteLegacyRowRelationship(
|
|
{ parentId, childId, relationshipId, tenantId }: { parentId: string; childId: string; relationshipId?: string; tenantId?: string | null },
|
|
client: LegacyRelationshipClient = getDbClient() as unknown as LegacyRelationshipClient,
|
|
): Promise<Prisma.BatchPayload> {
|
|
if (tenantId === undefined) {
|
|
throw new Error("Tenant id is required for relationship deletes.");
|
|
}
|
|
if (!relationshipId) {
|
|
throw new Error("Relationship id is required for scoped relationship deletes.");
|
|
}
|
|
const relationship = await client.rowRelationship.findFirst({
|
|
where: {
|
|
relationshipId,
|
|
parentId,
|
|
childId,
|
|
parent: { tenantId, deletedAt: null },
|
|
child: { tenantId, deletedAt: null },
|
|
},
|
|
include: legacyRowRelationshipInclude,
|
|
});
|
|
if (!relationship) {
|
|
throw new Error("Relationship rows not found.");
|
|
}
|
|
return deleteLegacyRowRelationshipById({ ...relationship, tenantId }, client);
|
|
}
|
|
|
|
export async function findLegacyEntityRelationship(
|
|
{ parentId, childId }: { parentId: string; childId: string },
|
|
client: LegacyRelationshipClient = getDbClient() as unknown as LegacyRelationshipClient,
|
|
): Promise<LegacyEntityRelationshipDetails | null> {
|
|
return client.entityRelationship.findFirst({
|
|
where: { parentId, childId },
|
|
select: selectLegacyEntityRelationshipProperties,
|
|
});
|
|
}
|