a homebrewed DnD campaign based in the Honkai: Star Rail universe
hsr honkaistarrail dnd
1
fork

Configure Feed

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

db: simplify count queries

+38 -99
+3 -9
app/src/lib/server/db/repos/background.ts
··· 1 - import { count, eq, type InferInsertModel, type InferSelectModel } from 'drizzle-orm' 1 + import { eq, type InferInsertModel, type InferSelectModel } from 'drizzle-orm' 2 2 import type { DatabaseConn } from '..' 3 3 import type { Tool } from './equipment' 4 4 import type { Feat } from './feat' ··· 10 10 } from '../schemas/dnd/background' 11 11 import { tool } from '../schemas/dnd/equipment' 12 12 import { feat } from '../schemas/dnd/feat' 13 - import { 14 - queryCount, 15 - querySingle, 16 - selectColumns, 17 - type TableColumnNames, 18 - type UpdateQueryParams, 19 - } from '../utils' 13 + import { querySingle, selectColumns, type TableColumnNames, type UpdateQueryParams } from '../utils' 20 14 21 15 export type BackgroundTable = typeof background 22 16 export type Background = InferSelectModel<BackgroundTable> ··· 126 120 } 127 121 128 122 public async getCount(): Promise<number> { 129 - return await this.db.select({ count: count() }).from(background).then(queryCount) 123 + return await this.db.$count(background) 130 124 } 131 125 }
+3 -3
app/src/lib/server/db/repos/class-feature.ts
··· 1 - import { count, eq, type InferInsertModel, type InferSelectModel } from 'drizzle-orm' 1 + import { eq, type InferInsertModel, type InferSelectModel } from 'drizzle-orm' 2 2 import type { DatabaseConn } from '..' 3 3 import type { RepositoryCountable } from './repo' 4 4 import { characterClassFeature } from '../schemas/dnd/class' 5 - import { queryCount, querySingle } from '../utils' 5 + import { querySingle } from '../utils' 6 6 7 7 export type ClassFeatureTable = typeof characterClassFeature 8 8 export type ClassFeature = InferSelectModel<ClassFeatureTable> ··· 25 25 } 26 26 27 27 public async getCount(): Promise<number> { 28 - return await this.db.select({ count: count() }).from(characterClassFeature).then(queryCount) 28 + return await this.db.$count(characterClassFeature) 29 29 } 30 30 }
+3 -9
app/src/lib/server/db/repos/class.ts
··· 1 - import { count, eq, type InferInsertModel, type InferSelectModel } from 'drizzle-orm' 1 + import { eq, type InferInsertModel, type InferSelectModel } from 'drizzle-orm' 2 2 import type { DatabaseConn } from '..' 3 3 import type { RepositoryCountable } from './repo' 4 4 import { characterClass } from '../schemas/dnd/class' 5 5 import { sourcebook } from '../schemas/dnd/sourcebook' 6 - import { 7 - queryCount, 8 - querySingle, 9 - selectColumns, 10 - type TableColumnNames, 11 - type UpdateQueryParams, 12 - } from '../utils' 6 + import { querySingle, selectColumns, type TableColumnNames, type UpdateQueryParams } from '../utils' 13 7 14 8 export type ClassTable = typeof characterClass 15 9 export type Class = InferSelectModel<ClassTable> ··· 53 47 } 54 48 55 49 public async getCount(): Promise<number> { 56 - return this.db.select({ count: count() }).from(characterClass).then(queryCount) 50 + return this.db.$count(characterClass) 57 51 } 58 52 }
+3 -3
app/src/lib/server/db/repos/condition.ts
··· 1 - import { count, eq, type InferInsertModel, type InferSelectModel } from 'drizzle-orm' 1 + import { eq, type InferInsertModel, type InferSelectModel } from 'drizzle-orm' 2 2 import type { DatabaseConn } from '..' 3 3 import type { RepositoryCountable } from './repo' 4 4 import { condition } from '../schemas/dnd/mechanic' 5 5 import { sourcebook } from '../schemas/dnd/sourcebook' 6 - import { queryCount, querySingle, type UpdateQueryParams } from '../utils' 6 + import { querySingle, type UpdateQueryParams } from '../utils' 7 7 8 8 export type ConditionTable = typeof condition 9 9 export type Condition = InferSelectModel<ConditionTable> ··· 39 39 } 40 40 41 41 public async getCount(): Promise<number> { 42 - return await this.db.select({ count: count() }).from(condition).then(queryCount) 42 + return await this.db.$count(condition) 43 43 } 44 44 }
+3 -9
app/src/lib/server/db/repos/equipment.ts
··· 1 - import { count, eq, type InferInsertModel, type InferSelectModel } from 'drizzle-orm' 1 + import { eq, type InferInsertModel, type InferSelectModel } from 'drizzle-orm' 2 2 import type { DatabaseConn } from '..' 3 3 import type { RepositoryCountable } from './repo' 4 4 import { equipment, tool } from '../schemas/dnd/equipment' 5 5 import { sourcebook } from '../schemas/dnd/sourcebook' 6 - import { 7 - queryCount, 8 - querySingle, 9 - selectColumns, 10 - type TableColumnNames, 11 - type UpdateQueryParams, 12 - } from '../utils' 6 + import { querySingle, selectColumns, type TableColumnNames, type UpdateQueryParams } from '../utils' 13 7 14 8 export type EquipmentTable = typeof equipment 15 9 export type Equipment = InferSelectModel<EquipmentTable> ··· 57 51 } 58 52 59 53 public async getCount(): Promise<number> { 60 - return await this.db.select({ count: count() }).from(equipment).then(queryCount) 54 + return await this.db.$count(equipment) 61 55 } 62 56 }
+3 -9
app/src/lib/server/db/repos/faction.ts
··· 1 - import { count, eq, type InferInsertModel, type InferSelectModel } from 'drizzle-orm' 1 + import { eq, type InferInsertModel, type InferSelectModel } from 'drizzle-orm' 2 2 import type { RepositoryCountable } from './repo' 3 3 import { type DatabaseConn } from '..' 4 4 import { faction } from '../schemas/dnd/faction' 5 5 import { sourcebook } from '../schemas/dnd/sourcebook' 6 - import { 7 - queryCount, 8 - querySingle, 9 - selectColumns, 10 - type TableColumnNames, 11 - type UpdateQueryParams, 12 - } from '../utils' 6 + import { querySingle, selectColumns, type TableColumnNames, type UpdateQueryParams } from '../utils' 13 7 14 8 export type FactionTable = typeof faction 15 9 export type Faction = InferSelectModel<FactionTable> ··· 50 44 } 51 45 52 46 public async getCount(): Promise<number> { 53 - return await this.db.select({ count: count() }).from(faction).then(queryCount) 47 + return await this.db.$count(faction) 54 48 } 55 49 }
+3 -9
app/src/lib/server/db/repos/feat.ts
··· 1 - import { count, eq, type InferInsertModel, type InferSelectModel } from 'drizzle-orm' 1 + import { eq, type InferInsertModel, type InferSelectModel } from 'drizzle-orm' 2 2 import type { DatabaseConn } from '..' 3 3 import type { RepositoryCountable } from './repo' 4 4 import { feat } from '../schemas/dnd/feat' 5 - import { 6 - queryCount, 7 - querySingle, 8 - selectColumns, 9 - type TableColumnNames, 10 - type UpdateQueryParams, 11 - } from '../utils' 5 + import { querySingle, selectColumns, type TableColumnNames, type UpdateQueryParams } from '../utils' 12 6 13 7 export type FeatTable = typeof feat 14 8 export type Feat = InferSelectModel<FeatTable> ··· 43 37 } 44 38 45 39 public async getCount(): Promise<number> { 46 - return await this.db.select({ count: count() }).from(feat).then(queryCount) 40 + return await this.db.$count(feat) 47 41 } 48 42 }
+4 -8
app/src/lib/server/db/repos/language.ts
··· 1 - import { count, eq, type InferInsertModel, type InferSelectModel } from 'drizzle-orm' 1 + import { eq, type InferInsertModel, type InferSelectModel } from 'drizzle-orm' 2 2 import type { DatabaseConn } from '..' 3 3 import type { RepositoryCountable, RepositoryCountableWithSourceBook } from './repo' 4 4 import type { SourceBook } from './source-book' 5 5 import { language } from '../schemas/dnd/language' 6 6 import { sourcebook } from '../schemas/dnd/sourcebook' 7 - import { queryCount, querySingle, type UpdateQueryParams } from '../utils' 7 + import { querySingle, type UpdateQueryParams } from '../utils' 8 8 9 9 export type LanguageTable = typeof language 10 10 export type Language = InferSelectModel<LanguageTable> ··· 41 41 } 42 42 43 43 public async getCount(): Promise<number> { 44 - return await this.db.select({ count: count() }).from(language).then(queryCount) 44 + return await this.db.$count(language) 45 45 } 46 46 47 47 public async getCountFromSourceBook(id: SourceBook['id']): Promise<number> { 48 - return await this.db 49 - .select({ count: count() }) 50 - .from(language) 51 - .where(eq(language.sourcebookId, id)) 52 - .then(queryCount) 48 + return await this.db.$count(language, eq(language.sourcebookId, id)) 53 49 } 54 50 }
+4 -8
app/src/lib/server/db/repos/monster.ts
··· 1 - import { count, eq, type InferInsertModel, type InferSelectModel } from 'drizzle-orm' 1 + import { eq, type InferInsertModel, type InferSelectModel } from 'drizzle-orm' 2 2 import type { DatabaseConn } from '..' 3 3 import type { RepositoryCountable, RepositoryCountableWithSourceBook } from './repo' 4 4 import type { SourceBook } from './source-book' ··· 10 10 monsterDmgVulnerability, 11 11 } from '../schemas/dnd/monster' 12 12 import { sourcebook } from '../schemas/dnd/sourcebook' 13 - import { queryCount, querySingle, selectColumns, type TableColumnNames } from '../utils' 13 + import { querySingle, selectColumns, type TableColumnNames } from '../utils' 14 14 import { 15 15 MonsterDefenseRepository, 16 16 type NewMonsterImmunity, ··· 115 115 } 116 116 117 117 public async getCount(): Promise<number> { 118 - return await this.db.select({ count: count() }).from(monster).then(queryCount) 118 + return await this.db.$count(monster) 119 119 } 120 120 121 121 public async getCountFromSourceBook(id: SourceBook['id']): Promise<number> { 122 - return await this.db 123 - .select({ count: count() }) 124 - .from(monster) 125 - .where(eq(monster.sourcebookId, id)) 126 - .then(queryCount) 122 + return await this.db.$count(monster, eq(monster.sourcebookId, id)) 127 123 } 128 124 }
+3 -10
app/src/lib/server/db/repos/source-book.ts
··· 1 - import { count, eq, type InferInsertModel, type InferSelectModel } from 'drizzle-orm' 1 + import { eq, type InferInsertModel, type InferSelectModel } from 'drizzle-orm' 2 2 import type { DatabaseConn } from '..' 3 3 import type { RepositoryCountable } from './repo' 4 - import { monster } from '../schemas/dnd/monster' 5 4 import { sourcebook } from '../schemas/dnd/sourcebook' 6 - import { 7 - queryCount, 8 - querySingle, 9 - selectColumns, 10 - type TableColumnNames, 11 - type UpdateQueryParams, 12 - } from '../utils' 5 + import { querySingle, selectColumns, type TableColumnNames, type UpdateQueryParams } from '../utils' 13 6 14 7 export type SourceBookTable = typeof sourcebook 15 8 export type SourceBook = InferSelectModel<SourceBookTable> ··· 74 67 } 75 68 76 69 public async getCount(): Promise<number> { 77 - return await this.db.select({ count: count() }).from(monster).then(queryCount) 70 + return await this.db.$count(sourcebook) 78 71 } 79 72 }
+3 -9
app/src/lib/server/db/repos/species.ts
··· 1 - import { count, eq, type InferSelectModel } from 'drizzle-orm' 1 + import { eq, type InferSelectModel } from 'drizzle-orm' 2 2 import type { DatabaseConn } from '..' 3 3 import type { RepositoryCountable } from './repo' 4 4 import { sourcebook } from '../schemas/dnd/sourcebook' 5 5 import { species } from '../schemas/dnd/species' 6 - import { 7 - queryCount, 8 - querySingle, 9 - selectColumns, 10 - type TableColumnNames, 11 - type UpdateQueryParams, 12 - } from '../utils' 6 + import { querySingle, selectColumns, type TableColumnNames, type UpdateQueryParams } from '../utils' 13 7 14 8 export type SpeciesTable = typeof species 15 9 export type Species = InferSelectModel<SpeciesTable> ··· 52 46 } 53 47 54 48 public async getCount(): Promise<number> { 55 - return await this.db.select({ count: count() }).from(species).then(queryCount) 49 + return await this.db.$count(species) 56 50 } 57 51 }
+3 -9
app/src/lib/server/db/repos/spell.ts
··· 1 1 import type { SpellDuration } from '@starlight/types/dnd' 2 - import { count, eq, type InferInsertModel, type InferSelectModel } from 'drizzle-orm' 2 + import { eq, type InferInsertModel, type InferSelectModel } from 'drizzle-orm' 3 3 import type { DatabaseConn } from '..' 4 4 import type { RepositoryCountable } from './repo' 5 5 import { damage } from '../schemas/dnd/mechanic' 6 6 import { sourcebook } from '../schemas/dnd/sourcebook' 7 7 import { spell } from '../schemas/dnd/spell' 8 - import { 9 - queryCount, 10 - querySingle, 11 - selectColumns, 12 - type TableColumnNames, 13 - type UpdateQueryParams, 14 - } from '../utils' 8 + import { querySingle, selectColumns, type TableColumnNames, type UpdateQueryParams } from '../utils' 15 9 16 10 export type SpellTable = typeof spell 17 11 export type Spell = Omit<InferSelectModel<SpellTable>, 'duration'> & { ··· 59 53 } 60 54 61 55 public async getCount(): Promise<number> { 62 - return await this.db.select({ count: count() }).from(spell).then(queryCount) 56 + return await this.db.$count(spell) 63 57 } 64 58 }
-4
app/src/lib/server/db/utils.ts
··· 26 26 return results.at(0) ?? null 27 27 } 28 28 29 - export function queryCount<T extends { count: number }>(results: T[]): number { 30 - return results[0].count 31 - } 32 - 33 29 export type UpdateQueryParams<Id, Model> = { 34 30 id: Id 35 31 model: Model