Files
HiveGrid-Wireframes/hivegrid-cloud/apps/web/app/utils/db/entities/entityTemplates.db.server.ts
T

71 lines
1.9 KiB
TypeScript

import { db } from "~/utils/db.server";
export async function getEntityTemplates(entityId: string, { tenantId }: { tenantId: string | null }) {
return await db.entityTemplate.findMany({
where: { entityId, tenantId },
orderBy: { entity: { order: "asc" } },
});
}
type EntityTemplateScope = {
id: string;
tenantId: string | null;
entityId: string;
};
export async function getEntityTemplate(id: string, { tenantId, entityId }: { tenantId: string | null; entityId?: string }) {
return await db.entityTemplate
.findFirstOrThrow({
where: { id, tenantId, ...(entityId ? { entityId } : {}) },
})
.catch(() => {
return null;
});
}
export async function createEntityTemplate(data: { tenantId: string | null; entityId: string; title: string; config: string }) {
return await db.entityTemplate.create({
data: {
tenantId: data.tenantId,
entityId: data.entityId,
title: data.title,
config: data.config,
},
});
}
export async function updateEntityTemplate(id: string, data: { title: string; config: string }) {
return await db.entityTemplate.update({
where: {
id,
},
data,
});
}
export async function updateScopedEntityTemplate(scope: EntityTemplateScope, data: { title: string; config: string }) {
const result = await db.entityTemplate.updateMany({
where: scope,
data,
});
if (result.count !== 1) {
throw Error("Entity template not found");
}
return await getEntityTemplate(scope.id, { tenantId: scope.tenantId, entityId: scope.entityId });
}
export async function deleteEntityTemplate(id: string) {
return await db.entityTemplate.delete({
where: { id },
});
}
export async function deleteScopedEntityTemplate(scope: EntityTemplateScope) {
const result = await db.entityTemplate.deleteMany({
where: scope,
});
if (result.count !== 1) {
throw Error("Entity template not found");
}
}