Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

transaction for upsert

x0322051b11ab8b0369
diff --git a/backend/src/services/org-asset/org-asset-dal.ts b/backend/src/services/org-asset/org-asset-dal.ts
index b11a847c7a..2e3fcf67b7 100644
--- a/backend/src/services/org-asset/org-asset-dal.ts
+++ b/backend/src/services/org-asset/org-asset-dal.ts
@@ -20,11 +20,20 @@ export const orgAssetDALFactory = (db: TDbClient) => {
contentType: string,
size: number
) => {
- const existing = await getFirstAsset(orgId, assetType);
- if (existing) {
- return organizationAssetOrm.updateById(existing.id, { data, contentType, size });
- }
- return organizationAssetOrm.create({ orgId, assetType, data, contentType, size });
+ return db.transaction(async (tx) => {
+ const existing = await tx(TableName.OrganizationAsset).where({ orgId, assetType }).first();
+ if (existing) {
+ const [updated] = await tx(TableName.OrganizationAsset)
+ .where({ id: existing.id })
+ .update({ data, contentType, size })
+ .returning("*");
+ return updated;
+ }
+ const [created] = await tx(TableName.OrganizationAsset)
+ .insert({ orgId, assetType, data, contentType, size })
+ .returning("*");
+ return created;
+ });
};
const deleteAssetsByType = async (orgId: string, assetType: string) => {