@@ -16,7 +16,10 @@ export class TagService {
16
16
// Get all tags for a user with file counts
17
17
async getUserTags ( userId : string ) : Promise < Tag [ ] > {
18
18
// 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
+ } ) ;
20
23
21
24
// Get file counts for each tag
22
25
const tagsWithCounts = await Promise . all (
@@ -42,22 +45,17 @@ export class TagService {
42
45
43
46
// Get a specific tag by ID
44
47
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
+ } ) ;
50
51
51
- if ( ! tagRecord . length ) return null ;
52
+ if ( ! record ) return null ;
52
53
53
54
const fileCount = await this . c . var . db
54
55
. select ( { count : count ( ) } )
55
56
. from ( fileTag )
56
57
. where ( and ( eq ( fileTag . tagId , tagId ) , eq ( fileTag . userId , userId ) ) ) ;
57
58
58
- const record = tagRecord [ 0 ] ;
59
- if ( ! record ) return null ;
60
-
61
59
return {
62
60
id : record . id ,
63
61
name : record . name ,
@@ -85,9 +83,11 @@ export class TagService {
85
83
? and ( eq ( tag . name , name ) , eq ( tag . userId , userId ) , eq ( tag . parentId , parentId ) )
86
84
: and ( eq ( tag . name , name ) , eq ( tag . userId , userId ) , isNull ( tag . parentId ) ) ;
87
85
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
+ } ) ;
89
89
90
- if ( existingTag . length > 0 ) {
90
+ if ( existingTag ) {
91
91
throw new Error ( "Tag with this name already exists" ) ;
92
92
}
93
93
@@ -140,9 +140,11 @@ export class TagService {
140
140
? and ( eq ( tag . name , updates . name ) , eq ( tag . userId , userId ) , eq ( tag . parentId , newParentId ) )
141
141
: and ( eq ( tag . name , updates . name ) , eq ( tag . userId , userId ) , isNull ( tag . parentId ) ) ;
142
142
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
+ } ) ;
144
146
145
- if ( nameConflict . length > 0 ) {
147
+ if ( nameConflict ) {
146
148
throw new Error ( "Tag with this name already exists" ) ;
147
149
}
148
150
}
@@ -191,10 +193,10 @@ export class TagService {
191
193
}
192
194
193
195
// 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
+ } ) ;
198
200
199
201
const existingTagIds = existingAssociations . map ( assoc => assoc . tagId ) ;
200
202
const newTagIds = tagIds . filter ( tagId => ! existingTagIds . includes ( tagId ) ) ;
@@ -239,21 +241,17 @@ export class TagService {
239
241
240
242
// Get all tags for a specific file
241
243
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
+ } ) ;
248
247
249
248
const tagIds = fileTagAssociations . map ( assoc => assoc . tagId ) ;
250
249
251
250
if ( tagIds . length === 0 ) return [ ] ;
252
251
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
+ } ) ;
257
255
258
256
return tags . map ( tagRecord => ( {
259
257
...tagRecord ,
@@ -265,10 +263,9 @@ export class TagService {
265
263
266
264
// Get all child tag IDs recursively
267
265
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
+ } ) ;
272
269
273
270
const childIds = childTags . map ( tag => tag . id ) ;
274
271
const grandChildIds = await Promise . all ( childIds . map ( childId => this . getAllChildTagIds ( childId , userId ) ) ) ;
0 commit comments