Skip to content

Commit

Permalink
refactor: ensure associations are deduped and reorder parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
calebpitan committed Nov 11, 2024
1 parent bc721b9 commit e5de513
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
19 changes: 19 additions & 0 deletions packages/common/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,22 @@ export function* range(stopOrStart: number, stop?: number, step?: number) {
yield i
}
}

/**
* Removes duplicates from a list of items using a function to
* compute which of the items keys should be used to index the item
*
* @param items The items from which to filter out duplicates
* @param indexer A function used for indexing the items
* @returns The items with all duplicates removed
*/
export function unique<T extends Record<string, unknown>, K>(items: T[], indexer: (item: T) => K) {
const map = new Map<K, T>()

items.forEach((item) => {
const key = indexer(item)
return void (map.has(key) ? void 0 : map.set(key, item))
})

return Array.from(map.values())
}
14 changes: 8 additions & 6 deletions packages/io/src/repositories/associations.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { never } from '@stitches/common'
import { never, unique } from '@stitches/common'

import { and, eq, or } from 'drizzle-orm'
import { SQLJsDatabase } from 'drizzle-orm/sql-js'
Expand Down Expand Up @@ -46,12 +46,14 @@ export class TagsToTaskAssociation<R extends Relation = undefined> {
switch (type) {
case undefined: {
const result = await query
.innerJoin(schema.tasks, eq(schema.tagsToTasks.taskId, schema.tasks.id))
.innerJoin(schema.tasks, or(eq(schema.tagsToTasks.taskId, schema.tasks.id)))
.innerJoin(schema.tags, eq(schema.tagsToTasks.tagId, schema.tags.id))
.then((result) => {
const tags = result.map((v) => v.tags)
const tasks = result.map((v) => v.tasks)
const data: Association<undefined> = {
tags: result.map((v) => v.tags),
tasks: result.map((v) => v.tasks),
tags: unique(tags, (i) => i.id),
tasks: unique(tasks, (i) => i.id),
}
return data as Association<T>
})
Expand Down Expand Up @@ -93,7 +95,7 @@ export class TagsToTaskAssociation<R extends Relation = undefined> {
* @param task The ID of the task to associate with a tag specifed as ID as `tag`
* @returns A promise that resolves when successful, otherwise rejects
*/
async associate(tag: string, task: string) {
async associate(task: string, tag: string) {
return await this.db.insert(schema.tagsToTasks).values({ tagId: tag, taskId: task }).execute()
}

Expand All @@ -104,7 +106,7 @@ export class TagsToTaskAssociation<R extends Relation = undefined> {
* @param task The ID of the task to associate with a tag specifed as ID as `tag`
* @returns A promise that resolves when successful, otherwise rejects
*/
async unassociate(tag: string, task: string) {
async unassociate(task: string, tag: string) {
return await this.db
.delete(schema.tagsToTasks)
.where(and(eq(schema.tagsToTasks.tagId, tag), eq(schema.tagsToTasks.taskId, task)))
Expand Down

0 comments on commit e5de513

Please sign in to comment.