-
Notifications
You must be signed in to change notification settings - Fork 731
feat: add DAL (CM-951) #3836
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: feat/add-project-discovery-worker
Are you sure you want to change the base?
feat: add DAL (CM-951) #3836
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,397 @@ | ||||||
| import { QueryExecutor } from '../queryExecutor' | ||||||
| import { prepareSelectColumns } from '../utils' | ||||||
|
|
||||||
| import { | ||||||
| EvaluationStatus, | ||||||
| IDbEvaluatedProject, | ||||||
| IDbEvaluatedProjectCreate, | ||||||
| IDbEvaluatedProjectUpdate, | ||||||
| } from './types' | ||||||
|
|
||||||
| const EVALUATED_PROJECT_COLUMNS = [ | ||||||
| 'id', | ||||||
| 'projectCatalogId', | ||||||
| 'evaluationStatus', | ||||||
| 'evaluationScore', | ||||||
| 'evaluation', | ||||||
| 'evaluationReason', | ||||||
| 'evaluatedAt', | ||||||
| 'starsCount', | ||||||
| 'forksCount', | ||||||
| 'commitsCount', | ||||||
| 'pullRequestsCount', | ||||||
| 'issuesCount', | ||||||
| 'onboarded', | ||||||
| 'onboardedAt', | ||||||
| 'createdAt', | ||||||
| 'updatedAt', | ||||||
| ] | ||||||
|
|
||||||
| export async function findEvaluatedProjectById( | ||||||
| qx: QueryExecutor, | ||||||
| id: string, | ||||||
| ): Promise<IDbEvaluatedProject | null> { | ||||||
| return qx.selectOneOrNone( | ||||||
| ` | ||||||
| SELECT ${prepareSelectColumns(EVALUATED_PROJECT_COLUMNS)} | ||||||
| FROM "evaluatedProjects" | ||||||
| WHERE id = $(id) | ||||||
| `, | ||||||
| { id }, | ||||||
| ) | ||||||
| } | ||||||
|
|
||||||
| export async function findEvaluatedProjectByProjectCatalogId( | ||||||
| qx: QueryExecutor, | ||||||
| projectCatalogId: string, | ||||||
| ): Promise<IDbEvaluatedProject | null> { | ||||||
| return qx.selectOneOrNone( | ||||||
| ` | ||||||
| SELECT ${prepareSelectColumns(EVALUATED_PROJECT_COLUMNS)} | ||||||
| FROM "evaluatedProjects" | ||||||
| WHERE "projectCatalogId" = $(projectCatalogId) | ||||||
| `, | ||||||
| { projectCatalogId }, | ||||||
| ) | ||||||
| } | ||||||
|
|
||||||
| export async function findEvaluatedProjectsByStatus( | ||||||
| qx: QueryExecutor, | ||||||
| evaluationStatus: EvaluationStatus, | ||||||
| options: { limit?: number; offset?: number } = {}, | ||||||
| ): Promise<IDbEvaluatedProject[]> { | ||||||
| const { limit, offset } = options | ||||||
|
|
||||||
| return qx.select( | ||||||
| ` | ||||||
| SELECT ${prepareSelectColumns(EVALUATED_PROJECT_COLUMNS)} | ||||||
| FROM "evaluatedProjects" | ||||||
| WHERE "evaluationStatus" = $(evaluationStatus) | ||||||
| ORDER BY "createdAt" ASC | ||||||
| ${limit !== undefined ? 'LIMIT $(limit)' : ''} | ||||||
| ${offset !== undefined ? 'OFFSET $(offset)' : ''} | ||||||
| `, | ||||||
| { evaluationStatus, limit, offset }, | ||||||
| ) | ||||||
| } | ||||||
|
|
||||||
| export async function findAllEvaluatedProjects( | ||||||
| qx: QueryExecutor, | ||||||
| options: { limit?: number; offset?: number } = {}, | ||||||
| ): Promise<IDbEvaluatedProject[]> { | ||||||
| const { limit, offset } = options | ||||||
|
|
||||||
| return qx.select( | ||||||
| ` | ||||||
| SELECT ${prepareSelectColumns(EVALUATED_PROJECT_COLUMNS)} | ||||||
| FROM "evaluatedProjects" | ||||||
| ORDER BY "createdAt" DESC | ||||||
| ${limit !== undefined ? 'LIMIT $(limit)' : ''} | ||||||
| ${offset !== undefined ? 'OFFSET $(offset)' : ''} | ||||||
| `, | ||||||
| { limit, offset }, | ||||||
| ) | ||||||
| } | ||||||
|
|
||||||
| export async function countEvaluatedProjects( | ||||||
| qx: QueryExecutor, | ||||||
| evaluationStatus?: EvaluationStatus, | ||||||
| ): Promise<number> { | ||||||
| const statusFilter = evaluationStatus ? 'WHERE "evaluationStatus" = $(evaluationStatus)' : '' | ||||||
|
|
||||||
| const result = await qx.selectOne( | ||||||
| ` | ||||||
| SELECT COUNT(*) AS count | ||||||
| FROM "evaluatedProjects" | ||||||
| ${statusFilter} | ||||||
| `, | ||||||
| { evaluationStatus }, | ||||||
| ) | ||||||
| return parseInt(result.count, 10) | ||||||
| } | ||||||
|
|
||||||
| export async function insertEvaluatedProject( | ||||||
| qx: QueryExecutor, | ||||||
| data: IDbEvaluatedProjectCreate, | ||||||
| ): Promise<IDbEvaluatedProject> { | ||||||
| return qx.selectOne( | ||||||
| ` | ||||||
| INSERT INTO "evaluatedProjects" ( | ||||||
| "projectCatalogId", | ||||||
| "evaluationStatus", | ||||||
| "evaluationScore", | ||||||
| evaluation, | ||||||
| "evaluationReason", | ||||||
| "starsCount", | ||||||
| "forksCount", | ||||||
| "commitsCount", | ||||||
| "pullRequestsCount", | ||||||
| "issuesCount", | ||||||
| "createdAt", | ||||||
| "updatedAt" | ||||||
| ) | ||||||
| VALUES ( | ||||||
| $(projectCatalogId), | ||||||
| $(evaluationStatus), | ||||||
| $(evaluationScore), | ||||||
| $(evaluation), | ||||||
| $(evaluationReason), | ||||||
| $(starsCount), | ||||||
| $(forksCount), | ||||||
| $(commitsCount), | ||||||
| $(pullRequestsCount), | ||||||
| $(issuesCount), | ||||||
| NOW(), | ||||||
| NOW() | ||||||
| ) | ||||||
| RETURNING ${prepareSelectColumns(EVALUATED_PROJECT_COLUMNS)} | ||||||
| `, | ||||||
| { | ||||||
| projectCatalogId: data.projectCatalogId, | ||||||
| evaluationStatus: data.evaluationStatus ?? 'pending', | ||||||
| evaluationScore: data.evaluationScore ?? null, | ||||||
| evaluation: data.evaluation ? JSON.stringify(data.evaluation) : null, | ||||||
| evaluationReason: data.evaluationReason ?? null, | ||||||
| starsCount: data.starsCount ?? null, | ||||||
| forksCount: data.forksCount ?? null, | ||||||
| commitsCount: data.commitsCount ?? null, | ||||||
| pullRequestsCount: data.pullRequestsCount ?? null, | ||||||
| issuesCount: data.issuesCount ?? null, | ||||||
| }, | ||||||
| ) | ||||||
| } | ||||||
|
|
||||||
| export async function bulkInsertEvaluatedProjects( | ||||||
| qx: QueryExecutor, | ||||||
| items: IDbEvaluatedProjectCreate[], | ||||||
| ): Promise<void> { | ||||||
| if (items.length === 0) { | ||||||
| return | ||||||
| } | ||||||
|
|
||||||
| const values = items.map((item) => ({ | ||||||
| projectCatalogId: item.projectCatalogId, | ||||||
| evaluationStatus: item.evaluationStatus ?? 'pending', | ||||||
| evaluationScore: item.evaluationScore ?? null, | ||||||
| evaluation: item.evaluation ? JSON.stringify(item.evaluation) : null, | ||||||
|
||||||
| evaluation: item.evaluation ? JSON.stringify(item.evaluation) : null, | |
| evaluation: item.evaluation ?? null, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
insertEvaluatedProjectignoresonboarded,onboardedAt, andevaluatedAteven though they are part of the writable model/type. This can lead to unexpected behavior if callers pass these fields (they'll be silently dropped). Include these columns in the INSERT (or remove them from the create type if they should only be set via dedicated update helpers).