@@ -16,7 +16,10 @@ export class TagService {
1616 // Get all tags for a user with file counts
1717 async getUserTags ( userId : string ) : Promise < Tag [ ] > {
1818 // Get all tags for the user
19- const userTags = await this . c . var . db . select ( ) . from ( tag ) . where ( eq ( tag . userId , userId ) ) . orderBy ( tag . name ) ;
19+ const userTags = await this . c . var . db . query . tag . findMany ( {
20+ where : ( table , { eq } ) => eq ( table . userId , userId ) ,
21+ orderBy : ( table , { asc } ) => asc ( table . name ) ,
22+ } ) ;
2023
2124 // Get file counts for each tag
2225 const tagsWithCounts = await Promise . all (
@@ -42,22 +45,17 @@ export class TagService {
4245
4346 // Get a specific tag by ID
4447 async getTagById ( tagId : string , userId : string ) : Promise < Tag | null > {
45- const tagRecord = await this . c . var . db
46- . select ( )
47- . from ( tag )
48- . where ( and ( eq ( tag . id , tagId ) , eq ( tag . userId , userId ) ) )
49- . limit ( 1 ) ;
48+ const record = await this . c . var . db . query . tag . findFirst ( {
49+ where : ( table , { and, eq } ) => and ( eq ( table . id , tagId ) , eq ( table . userId , userId ) ) ,
50+ } ) ;
5051
51- if ( ! tagRecord . length ) return null ;
52+ if ( ! record ) return null ;
5253
5354 const fileCount = await this . c . var . db
5455 . select ( { count : count ( ) } )
5556 . from ( fileTag )
5657 . where ( and ( eq ( fileTag . tagId , tagId ) , eq ( fileTag . userId , userId ) ) ) ;
5758
58- const record = tagRecord [ 0 ] ;
59- if ( ! record ) return null ;
60-
6159 return {
6260 id : record . id ,
6361 name : record . name ,
@@ -85,9 +83,11 @@ export class TagService {
8583 ? and ( eq ( tag . name , name ) , eq ( tag . userId , userId ) , eq ( tag . parentId , parentId ) )
8684 : and ( eq ( tag . name , name ) , eq ( tag . userId , userId ) , isNull ( tag . parentId ) ) ;
8785
88- const existingTag = await this . c . var . db . select ( ) . from ( tag ) . where ( existingTagQuery ) . limit ( 1 ) ;
86+ const existingTag = await this . c . var . db . query . tag . findFirst ( {
87+ where : existingTagQuery ,
88+ } ) ;
8989
90- if ( existingTag . length > 0 ) {
90+ if ( existingTag ) {
9191 throw new Error ( "Tag with this name already exists" ) ;
9292 }
9393
@@ -140,9 +140,11 @@ export class TagService {
140140 ? and ( eq ( tag . name , updates . name ) , eq ( tag . userId , userId ) , eq ( tag . parentId , newParentId ) )
141141 : and ( eq ( tag . name , updates . name ) , eq ( tag . userId , userId ) , isNull ( tag . parentId ) ) ;
142142
143- const nameConflict = await this . c . var . db . select ( ) . from ( tag ) . where ( nameConflictQuery ) . limit ( 1 ) ;
143+ const nameConflict = await this . c . var . db . query . tag . findFirst ( {
144+ where : nameConflictQuery ,
145+ } ) ;
144146
145- if ( nameConflict . length > 0 ) {
147+ if ( nameConflict ) {
146148 throw new Error ( "Tag with this name already exists" ) ;
147149 }
148150 }
@@ -191,10 +193,10 @@ export class TagService {
191193 }
192194
193195 // Check for existing associations
194- const existingAssociations = await this . c . var . db
195- . select ( )
196- . from ( fileTag )
197- . where ( and ( eq ( fileTag . fileId , fileId ) , inArray ( fileTag . tagId , tagIds ) , eq ( fileTag . userId , userId ) ) ) ;
196+ const existingAssociations = await this . c . var . db . query . fileTag . findMany ( {
197+ where : ( table , { and , eq , inArray } ) =>
198+ and ( eq ( table . fileId , fileId ) , inArray ( table . tagId , tagIds ) , eq ( table . userId , userId ) ) ,
199+ } ) ;
198200
199201 const existingTagIds = existingAssociations . map ( assoc => assoc . tagId ) ;
200202 const newTagIds = tagIds . filter ( tagId => ! existingTagIds . includes ( tagId ) ) ;
@@ -239,21 +241,17 @@ export class TagService {
239241
240242 // Get all tags for a specific file
241243 async getFileTags ( fileId : string , userId : string ) : Promise < Tag [ ] > {
242- const fileTagAssociations = await this . c . var . db
243- . select ( {
244- tagId : fileTag . tagId ,
245- } )
246- . from ( fileTag )
247- . where ( and ( eq ( fileTag . fileId , fileId ) , eq ( fileTag . userId , userId ) ) ) ;
244+ const fileTagAssociations = await this . c . var . db . query . fileTag . findMany ( {
245+ where : ( table , { and, eq } ) => and ( eq ( table . fileId , fileId ) , eq ( table . userId , userId ) ) ,
246+ } ) ;
248247
249248 const tagIds = fileTagAssociations . map ( assoc => assoc . tagId ) ;
250249
251250 if ( tagIds . length === 0 ) return [ ] ;
252251
253- const tags = await this . c . var . db
254- . select ( )
255- . from ( tag )
256- . where ( and ( inArray ( tag . id , tagIds ) , eq ( tag . userId , userId ) ) ) ;
252+ const tags = await this . c . var . db . query . tag . findMany ( {
253+ where : ( table , { and, inArray, eq } ) => and ( inArray ( table . id , tagIds ) , eq ( table . userId , userId ) ) ,
254+ } ) ;
257255
258256 return tags . map ( tagRecord => ( {
259257 ...tagRecord ,
@@ -265,10 +263,9 @@ export class TagService {
265263
266264 // Get all child tag IDs recursively
267265 private async getAllChildTagIds ( parentId : string , userId : string ) : Promise < string [ ] > {
268- const childTags = await this . c . var . db
269- . select ( { id : tag . id } )
270- . from ( tag )
271- . where ( and ( eq ( tag . parentId , parentId ) , eq ( tag . userId , userId ) ) ) ;
266+ const childTags = await this . c . var . db . query . tag . findMany ( {
267+ where : ( table , { and, eq } ) => and ( eq ( table . parentId , parentId ) , eq ( table . userId , userId ) ) ,
268+ } ) ;
272269
273270 const childIds = childTags . map ( tag => tag . id ) ;
274271 const grandChildIds = await Promise . all ( childIds . map ( childId => this . getAllChildTagIds ( childId , userId ) ) ) ;
0 commit comments