misc: migrated json filters to new op

This commit is contained in:
Sheen Capadngan
2024-10-03 00:31:23 +08:00
parent dc8c3a30bd
commit 9b1615f2fb
2 changed files with 9 additions and 2 deletions

View File

@@ -8,8 +8,14 @@ export async function up(knex: Knex): Promise<void> {
`CREATE INDEX "audit_logs_actorMetadata_idx" ON ${TableName.AuditLog} USING gin("actorMetadata" jsonb_path_ops)`
);
}
if (await knex.schema.hasColumn(TableName.AuditLog, "eventMetadata")) {
await knex.raw(
`CREATE INDEX "audit_logs_eventMetadata_idx" ON ${TableName.AuditLog} USING gin("eventMetadata" jsonb_path_ops)`
);
}
}
export async function down(knex: Knex): Promise<void> {
await knex.raw(`DROP INDEX IF EXISTS "audit_logs_actorMetadata_idx"`);
await knex.raw(`DROP INDEX IF EXISTS "audit_logs_eventMetadata_idx"`);
}

View File

@@ -77,13 +77,13 @@ export const auditLogDALFactory = (db: TDbClient) => {
// Special case: Filter by actor ID
if (actorId) {
void sqlQuery.whereRaw(`"actorMetadata"->>'userId' = ?`, [actorId]);
void sqlQuery.whereRaw(`"actorMetadata" @> jsonb_build_object('userId', ?::text)`, [actorId]);
}
// Special case: Filter by key/value pairs in eventMetadata field
if (eventMetadata && Object.keys(eventMetadata).length) {
Object.entries(eventMetadata).forEach(([key, value]) => {
void sqlQuery.whereRaw(`"eventMetadata"->>'${key}' = ?`, [value]);
void sqlQuery.whereRaw(`"eventMetadata" @> jsonb_build_object(?::text, ?::text)`, [key, value]);
});
}
@@ -104,6 +104,7 @@ export const auditLogDALFactory = (db: TDbClient) => {
if (endDate) {
void sqlQuery.where(`${TableName.AuditLog}.createdAt`, "<=", endDate);
}
const docs = await sqlQuery;
return docs;