mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-30 04:44:28 +00:00
fix(db): automatically update project updatedAt on asset modification
This commit is contained in:
@@ -26,6 +26,28 @@ function createPrismaClient() {
|
||||
const pool = new Pool({ connectionString })
|
||||
const adapter = new PrismaPg(pool)
|
||||
const client = new PrismaClient({ adapter, log: ['error'] })
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
function touchProjectsByAssetWhere(assetWhere: any) {
|
||||
if (!assetWhere) return
|
||||
client.project
|
||||
.updateMany({
|
||||
where: { assets: { some: assetWhere } },
|
||||
data: { updatedAt: new Date() },
|
||||
})
|
||||
.catch((e) => console.error('Failed to touch project updatedAt by asset where', e))
|
||||
}
|
||||
|
||||
function touchProjectById(projectId: string) {
|
||||
if (!projectId) return
|
||||
client.project
|
||||
.updateMany({
|
||||
where: { id: projectId },
|
||||
data: { updatedAt: new Date() },
|
||||
})
|
||||
.catch((e) => console.error('Failed to touch project updatedAt by id', e))
|
||||
}
|
||||
|
||||
return client.$extends({
|
||||
query: {
|
||||
workflowTask: {
|
||||
@@ -42,13 +64,34 @@ function createPrismaClient() {
|
||||
if (typeof args.data.name === 'string') {
|
||||
args.data.nameNgram = generateNgrams(args.data.name)
|
||||
}
|
||||
return query(args)
|
||||
const result = await query(args)
|
||||
// Use any here because result and args.data are complex Prisma types in extensions
|
||||
const projectId =
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
(result as any)?.projectId ||
|
||||
args.data?.projectId ||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
(args.data?.project as any)?.connect?.id
|
||||
if (projectId) {
|
||||
touchProjectById(projectId)
|
||||
} else if (
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
(result as any)?.id
|
||||
) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
touchProjectsByAssetWhere({ id: (result as any).id })
|
||||
}
|
||||
return result
|
||||
},
|
||||
async update({ args, query }) {
|
||||
if (typeof args.data.name === 'string') {
|
||||
args.data.nameNgram = generateNgrams(args.data.name)
|
||||
}
|
||||
return query(args)
|
||||
const result = await query(args)
|
||||
if (args.where) {
|
||||
touchProjectsByAssetWhere(args.where)
|
||||
}
|
||||
return result
|
||||
},
|
||||
async upsert({ args, query }) {
|
||||
if (typeof args.create.name === 'string') {
|
||||
@@ -57,13 +100,21 @@ function createPrismaClient() {
|
||||
if (typeof args.update.name === 'string') {
|
||||
args.update.nameNgram = generateNgrams(args.update.name)
|
||||
}
|
||||
return query(args)
|
||||
const result = await query(args)
|
||||
if (args.where) {
|
||||
touchProjectsByAssetWhere(args.where)
|
||||
}
|
||||
return result
|
||||
},
|
||||
async updateMany({ args, query }) {
|
||||
if (typeof args.data.name === 'string') {
|
||||
args.data.nameNgram = generateNgrams(args.data.name)
|
||||
}
|
||||
return query(args)
|
||||
const result = await query(args)
|
||||
if (args.where) {
|
||||
touchProjectsByAssetWhere(args.where)
|
||||
}
|
||||
return result
|
||||
},
|
||||
async createMany({ args, query }) {
|
||||
if (Array.isArray(args.data)) {
|
||||
@@ -73,7 +124,27 @@ function createPrismaClient() {
|
||||
}
|
||||
}
|
||||
}
|
||||
return query(args)
|
||||
const result = await query(args)
|
||||
const projectIds = new Set<string>()
|
||||
if (Array.isArray(args.data)) {
|
||||
for (const item of args.data) {
|
||||
if (item.projectId) projectIds.add(item.projectId)
|
||||
}
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
} else if ((args.data as any)?.projectId) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
projectIds.add((args.data as any).projectId)
|
||||
}
|
||||
|
||||
if (projectIds.size > 0) {
|
||||
client.project
|
||||
.updateMany({
|
||||
where: { id: { in: Array.from(projectIds) } },
|
||||
data: { updatedAt: new Date() },
|
||||
})
|
||||
.catch((e) => console.error('Failed to touch projects by createMany', e))
|
||||
}
|
||||
return result
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user