Skip to content

Commit

Permalink
fix: type safe db access
Browse files Browse the repository at this point in the history
  • Loading branch information
StarpTech committed May 30, 2021
1 parent f1b3dcb commit fa869df
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/registry/maintanance/garbage-collect.ts
Original file line number Diff line number Diff line change
@@ -1,6 1,7 @@
import S from 'fluent-json-schema'
import { FastifyInstance, FastifySchema } from 'fastify'
import { SchemaDBModel } from '../../core/models/schemaModel'
import { SchemaTagDBModel } from '../../core/models/schemaTagModel'

export interface RequestContext {
Body: {
Expand Down Expand Up @@ -33,21 34,21 @@ export default function garbageCollect(fastify: FastifyInstance) {
return fastify.knex.transaction(async function (trx) {
const schemasToKeep = await trx
.from(`${SchemaDBModel.table}`)
.orderBy('updatedAt', 'desc')
.orderBy(SchemaDBModel.field('updatedAt'), 'desc')
.limit(req.body.numSchemasKeep)

const deletedSchemaTags = await trx
.from('schema_tag')
.from(`${SchemaTagDBModel.table}`)
.whereNotIn(
`schema_tag.schemaId`,
`${SchemaTagDBModel.field('schemaId')}`,
schemasToKeep.map((s) => s.id),
)
.delete()

const deletedSchemas = await trx
.from('schema')
.from(`${SchemaDBModel.table}`)
.whereNotIn(
`schema.id`,
`${SchemaDBModel.field('id')}`,
schemasToKeep.map((s) => s.id),
)
.delete()
Expand Down

0 comments on commit fa869df

Please sign in to comment.