Mirror of https://github.com/roostorg/coop github.com/roostorg/coop
2
fork

Configure Feed

Select the types of activity you want to include in your feed.

[Kysely] Migrate MRT action/policy id lookups off Sequelize (#260)

* [Kysely] Migrate MRT action/policy id lookups off Sequelize

* code review changes

authored by

Juan Mrad and committed by
GitHub
99a391e2 f1d37149

+53 -16
+25 -16
server/services/manualReviewToolService/manualReviewToolQueries.ts
··· 1 - import { Op } from 'sequelize'; 2 - 3 1 import { inject } from '../../iocContainer/index.js'; 4 2 import { cached } from '../../utils/caching.js'; 5 3 import { jsonParse, jsonStringify } from '../../utils/encoding.js'; 6 4 import { type CollapseCases } from '../../utils/typescript-types.js'; 7 - import { type Action } from '../moderationConfigService/index.js'; 5 + import { type Action, type Policy } from '../moderationConfigService/index.js'; 8 6 9 7 type ActionKey = { ids: readonly string[]; orgId: string }; 10 8 11 9 export const makeGetActionsByIdEventuallyConsistent = inject( 12 - ['ActionModel'], 13 - (Action) => 10 + ['ModerationConfigService'], 11 + (moderationConfigService) => 14 12 cached({ 15 13 keyGeneration: { 16 14 toString: (it: ActionKey) => 17 15 jsonStringify({ ...it, ids: [...it.ids].sort() }), 18 16 fromString: (it) => jsonParse(it), 19 17 }, 20 - async producer(actionIds) { 21 - return Action.findAll({ 22 - where: { 23 - id: { [Op.in]: actionIds.ids }, 24 - orgId: actionIds.orgId, 25 - }, 26 - // NB: CollapseCases needed to prevent excessive stack depth TS errors downstream 18 + async producer(actionIds: ActionKey) { 19 + if (actionIds.ids.length === 0) { 20 + return [] as CollapseCases<Action>[]; 21 + } 22 + return moderationConfigService.getActions({ 23 + orgId: actionIds.orgId, 24 + ids: actionIds.ids, 25 + readFromReplica: true, 27 26 }) as Promise<CollapseCases<Action>[]>; 28 27 }, 29 28 directives: { freshUntilAge: 10, maxStale: [0, 2, 2] }, ··· 37 36 type PolicyKey = { ids: readonly string[]; orgId: string }; 38 37 39 38 export const makeGetPoliciesByIdEventuallyConsistent = inject( 40 - ['PolicyModel'], 41 - (Policy) => 39 + ['ModerationConfigService'], 40 + (moderationConfigService) => 42 41 cached({ 42 + keyGeneration: { 43 + toString: (it: PolicyKey) => 44 + jsonStringify({ ...it, ids: [...it.ids].sort() }), 45 + fromString: (it) => jsonParse(it), 46 + }, 43 47 async producer(key: PolicyKey) { 44 - return Policy.findAll({ 45 - where: { id: { [Op.in]: key.ids }, orgId: key.orgId }, 48 + if (key.ids.length === 0) { 49 + return [] as Policy[]; 50 + } 51 + return moderationConfigService.getPoliciesByIds({ 52 + orgId: key.orgId, 53 + ids: key.ids, 54 + readFromReplica: true, 46 55 }); 47 56 }, 48 57 directives: { freshUntilAge: 10, maxStale: [0, 2, 2] },
+8
server/services/moderationConfigService/moderationConfigService.ts
··· 309 309 return this.policyOps.getPolicies(opts); 310 310 } 311 311 312 + async getPoliciesByIds(opts: { 313 + orgId: string; 314 + ids: readonly string[]; 315 + readFromReplica?: boolean; 316 + }) { 317 + return this.policyOps.getPoliciesByIds(opts); 318 + } 319 + 312 320 async getPolicy(opts: { 313 321 orgId: string; 314 322 policyId: string;
+20
server/services/moderationConfigService/modules/PolicyOperations.ts
··· 84 84 return results.map((it) => this.#dbResultToPolicy(it)); 85 85 } 86 86 87 + async getPoliciesByIds(opts: { 88 + orgId: string; 89 + ids: readonly string[]; 90 + readFromReplica?: boolean; 91 + }): Promise<Policy[]> { 92 + const { orgId, ids, readFromReplica } = opts; 93 + if (ids.length === 0) { 94 + return []; 95 + } 96 + const pgQuery = this.#getPgQuery(readFromReplica ?? true); 97 + const results = (await pgQuery 98 + .selectFrom('public.policies') 99 + .select(policyDbSelection) 100 + .where('org_id', '=', orgId) 101 + .where('id', 'in', [...ids]) 102 + .execute()) as PolicyDbResult[]; 103 + 104 + return results.map((it) => this.#dbResultToPolicy(it)); 105 + } 106 + 87 107 async getPoliciesByRuleIds(opts: { 88 108 ruleIds: readonly string[]; 89 109 readFromReplica?: boolean;