@trafficgroup/knex-rel 0.0.26 → 0.0.28

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (94) hide show
  1. package/.claude/settings.local.json +9 -0
  2. package/CLAUDE.md +63 -0
  3. package/dist/KnexConnection.d.ts +17 -0
  4. package/dist/KnexConnection.js +99 -0
  5. package/dist/KnexConnection.js.map +1 -0
  6. package/dist/d.types.d.ts +17 -0
  7. package/dist/d.types.js +3 -0
  8. package/dist/d.types.js.map +1 -0
  9. package/dist/dao/auth/auth.dao.d.ts +13 -0
  10. package/dist/dao/auth/auth.dao.js +82 -0
  11. package/dist/dao/auth/auth.dao.js.map +1 -0
  12. package/dist/dao/chat/chat.dao.d.ts +13 -0
  13. package/dist/dao/chat/chat.dao.js +138 -0
  14. package/dist/dao/chat/chat.dao.js.map +1 -0
  15. package/dist/dao/folder/folder.dao.d.ts +11 -0
  16. package/dist/dao/folder/folder.dao.js +84 -0
  17. package/dist/dao/folder/folder.dao.js.map +1 -0
  18. package/dist/dao/message/message.dao.d.ts +13 -0
  19. package/dist/dao/message/message.dao.js +118 -0
  20. package/dist/dao/message/message.dao.js.map +1 -0
  21. package/dist/dao/study/study.dao.d.ts +11 -0
  22. package/dist/dao/study/study.dao.js +84 -0
  23. package/dist/dao/study/study.dao.js.map +1 -0
  24. package/dist/dao/user/user.dao.d.ts +12 -0
  25. package/dist/dao/user/user.dao.js +76 -0
  26. package/dist/dao/user/user.dao.js.map +1 -0
  27. package/dist/dao/user-push-notification-token/user-push-notification-token.dao.d.ts +14 -0
  28. package/dist/dao/user-push-notification-token/user-push-notification-token.dao.js +87 -0
  29. package/dist/dao/user-push-notification-token/user-push-notification-token.dao.js.map +1 -0
  30. package/dist/dao/video/video.dao.d.ts +20 -0
  31. package/dist/dao/video/video.dao.js +124 -0
  32. package/dist/dao/video/video.dao.js.map +1 -0
  33. package/dist/index.d.ts +19 -0
  34. package/dist/index.js +26 -0
  35. package/dist/index.js.map +1 -0
  36. package/dist/interfaces/auth/auth.interfaces.d.ts +11 -0
  37. package/dist/interfaces/auth/auth.interfaces.js +3 -0
  38. package/dist/interfaces/auth/auth.interfaces.js.map +1 -0
  39. package/dist/interfaces/chat/chat.interfaces.d.ts +18 -0
  40. package/dist/interfaces/chat/chat.interfaces.js +3 -0
  41. package/dist/interfaces/chat/chat.interfaces.js.map +1 -0
  42. package/dist/interfaces/folder/folder.interfaces.d.ts +12 -0
  43. package/dist/interfaces/folder/folder.interfaces.js +3 -0
  44. package/dist/interfaces/folder/folder.interfaces.js.map +1 -0
  45. package/dist/interfaces/message/message.interfaces.d.ts +16 -0
  46. package/dist/interfaces/message/message.interfaces.js +3 -0
  47. package/dist/interfaces/message/message.interfaces.js.map +1 -0
  48. package/dist/interfaces/study/study.interfaces.d.ts +11 -0
  49. package/dist/interfaces/study/study.interfaces.js +3 -0
  50. package/dist/interfaces/study/study.interfaces.js.map +1 -0
  51. package/dist/interfaces/user/user.interfaces.d.ts +11 -0
  52. package/dist/interfaces/user/user.interfaces.js +3 -0
  53. package/dist/interfaces/user/user.interfaces.js.map +1 -0
  54. package/dist/interfaces/user-push-notification-token/user-push-notification-token.interfaces.d.ts +10 -0
  55. package/dist/interfaces/user-push-notification-token/user-push-notification-token.interfaces.js +3 -0
  56. package/dist/interfaces/user-push-notification-token/user-push-notification-token.interfaces.js.map +1 -0
  57. package/dist/interfaces/video/video.interfaces.d.ts +20 -0
  58. package/dist/interfaces/video/video.interfaces.js +3 -0
  59. package/dist/interfaces/video/video.interfaces.js.map +1 -0
  60. package/knexfile.ts +5 -5
  61. package/migrations/20250717173228_migration.ts +9 -0
  62. package/migrations/20250717204731_migration.ts +13 -0
  63. package/migrations/20250722210109_migration.ts +60 -0
  64. package/migrations/20250722211019_migration.ts +15 -0
  65. package/migrations/20250723153852_migration.ts +19 -0
  66. package/migrations/20250723162257_migration.ts +16 -0
  67. package/migrations/20250723171109_migration.ts +16 -0
  68. package/migrations/20250723205331_migration.ts +18 -0
  69. package/migrations/20250724191345_migration.ts +18 -0
  70. package/migrations/20250730180932_migration.ts +22 -0
  71. package/migrations/20250730213625_migration.ts +18 -0
  72. package/migrations/20250804124509_migration.ts +43 -0
  73. package/migrations/20250804132053_migration.ts +18 -0
  74. package/migrations/20250804164518_migration.ts +17 -0
  75. package/package.json +47 -47
  76. package/src/d.types.ts +7 -7
  77. package/src/dao/auth/auth.dao.ts +61 -0
  78. package/src/dao/chat/chat.dao.ts +124 -0
  79. package/src/dao/folder/folder.dao.ts +66 -0
  80. package/src/dao/message/message.dao.ts +102 -0
  81. package/src/dao/study/study.dao.ts +66 -0
  82. package/src/dao/user/user.dao.ts +14 -10
  83. package/src/dao/user-push-notification-token/user-push-notification-token.dao.ts +65 -0
  84. package/src/dao/video/video.dao.ts +120 -0
  85. package/src/index.ts +23 -8
  86. package/src/interfaces/auth/auth.interfaces.ts +11 -0
  87. package/src/interfaces/chat/chat.interfaces.ts +20 -0
  88. package/src/interfaces/folder/folder.interfaces.ts +3 -0
  89. package/src/interfaces/message/message.interfaces.ts +18 -0
  90. package/src/interfaces/study/study.interfaces.ts +3 -0
  91. package/src/interfaces/user/user.interfaces.ts +2 -0
  92. package/src/interfaces/user-push-notification-token/user-push-notification-token.interfaces.ts +10 -0
  93. package/src/interfaces/video/video.interfaces.ts +9 -1
  94. package/.env +0 -5
@@ -0,0 +1,61 @@
1
+ import { Knex } from "knex";
2
+ import { IBaseDAO, IDataPaginator } from "../../d.types";
3
+ import { IAuth } from "../../interfaces/auth/auth.interfaces";
4
+ import KnexManager from "../../KnexConnection";
5
+
6
+ export class AuthDAO implements IBaseDAO<IAuth> {
7
+ private _knex: Knex<any, unknown[]> = KnexManager.getConnection();
8
+
9
+ async create(item: IAuth): Promise<IAuth> {
10
+ const [createdAuth] = await this._knex("auth").insert(item).returning("*");
11
+ return createdAuth;
12
+ }
13
+
14
+ async getById(id: number): Promise<IAuth | null> {
15
+ const auth = await this._knex("auth").where({ id }).first();
16
+ return auth || null;
17
+ }
18
+
19
+ async getByUuid(uuid: string): Promise<IAuth | null> {
20
+ // Auth table doesn't have uuid, so we'll return null or throw error
21
+ return null;
22
+ }
23
+
24
+ async getByUserId(userId: number): Promise<IAuth | null> {
25
+ const auth = await this._knex("auth").where({ userId }).first();
26
+ return auth || null;
27
+ }
28
+
29
+ async getByEmailToken(emailToken: string): Promise<IAuth | null> {
30
+ const auth = await this._knex("auth").where({ emailToken }).first();
31
+ return auth || null;
32
+ }
33
+
34
+ async update(id: number, item: Partial<IAuth>): Promise<IAuth | null> {
35
+ const [updatedAuth] = await this._knex("auth").where({ id }).update(item).returning("*");
36
+ return updatedAuth || null;
37
+ }
38
+
39
+ async delete(id: number): Promise<boolean> {
40
+ const result = await this._knex("auth").where({ id }).del();
41
+ return result > 0;
42
+ }
43
+
44
+ async getAll(page: number, limit: number): Promise<IDataPaginator<IAuth>> {
45
+ const offset = (page - 1) * limit;
46
+
47
+ const [countResult] = await this._knex("auth").count("* as count");
48
+ const totalCount = +countResult.count;
49
+ const auths = await this._knex("auth").limit(limit).offset(offset);
50
+
51
+ return {
52
+ success: true,
53
+ data: auths,
54
+ page,
55
+ limit,
56
+ count: auths.length,
57
+ totalCount,
58
+ totalPages: Math.ceil(totalCount / limit),
59
+ };
60
+ }
61
+ }
@@ -0,0 +1,124 @@
1
+ import { Knex } from "knex";
2
+ import { IBaseDAO, IDataPaginator } from "../../d.types";
3
+ import { IChat, IChatCreate, IChatUpdate } from '../../interfaces/chat/chat.interfaces';
4
+ import KnexManager from "../../KnexConnection";
5
+
6
+ export class ChatDAO implements IBaseDAO<IChat> {
7
+ private _knex: Knex<any, unknown[]> = KnexManager.getConnection();
8
+
9
+ async create(item: IChatCreate): Promise<IChat> {
10
+ const [result] = await this._knex('chat')
11
+ .insert(item)
12
+ .returning('*');
13
+ return result;
14
+ }
15
+
16
+ async getById(id: number): Promise<IChat | null> {
17
+ const result = await this._knex('chat')
18
+ .where('id', id)
19
+ .first();
20
+ return result || null;
21
+ }
22
+
23
+ async getByUuid(uuid: string): Promise<IChat | null> {
24
+ const result = await this._knex('chat')
25
+ .where('uuid', uuid)
26
+ .first();
27
+ return result || null;
28
+ }
29
+
30
+ async getAll(page = 1, limit = 10): Promise<IDataPaginator<IChat>> {
31
+ const offset = (page - 1) * limit;
32
+
33
+ const [results, [{ count }]] = await Promise.all([
34
+ this._knex('chat')
35
+ .orderBy('created_at', 'desc')
36
+ .limit(limit)
37
+ .offset(offset),
38
+ this._knex('chat').count('* as count')
39
+ ]);
40
+
41
+ const totalCount = parseInt(count as string);
42
+ return {
43
+ success: true,
44
+ data: results,
45
+ page,
46
+ limit,
47
+ count: results.length,
48
+ totalCount,
49
+ totalPages: Math.ceil(totalCount / limit)
50
+ };
51
+ }
52
+
53
+ async update(id: number, item: IChatUpdate): Promise<IChat | null> {
54
+ const [result] = await this._knex('chat')
55
+ .where('id', id)
56
+ .update({
57
+ ...item,
58
+ updated_at: new Date()
59
+ })
60
+ .returning('*');
61
+ return result || null;
62
+ }
63
+
64
+ async delete(id: number): Promise<boolean> {
65
+ const result = await this._knex('chat')
66
+ .where('id', id)
67
+ .delete();
68
+ return result > 0;
69
+ }
70
+
71
+ async getByUserId(userId: number, page = 1, limit = 10): Promise<IDataPaginator<IChat>> {
72
+ const offset = (page - 1) * limit;
73
+
74
+ const [results, [{ count }]] = await Promise.all([
75
+ this._knex('chat')
76
+ .where('userId', userId)
77
+ .orderBy('created_at', 'desc')
78
+ .limit(limit)
79
+ .offset(offset),
80
+ this._knex('chat')
81
+ .where('userId', userId)
82
+ .count('* as count')
83
+ ]);
84
+
85
+ const totalCount = parseInt(count as string);
86
+ return {
87
+ success: true,
88
+ data: results,
89
+ page,
90
+ limit,
91
+ count: results.length,
92
+ totalCount,
93
+ totalPages: Math.ceil(totalCount / limit)
94
+ };
95
+ }
96
+
97
+ async getActiveByUserId(userId: number, page = 1, limit = 10): Promise<IDataPaginator<IChat>> {
98
+ const offset = (page - 1) * limit;
99
+
100
+ const [results, [{ count }]] = await Promise.all([
101
+ this._knex('chat')
102
+ .where('userId', userId)
103
+ .where('status', 'active')
104
+ .orderBy('created_at', 'desc')
105
+ .limit(limit)
106
+ .offset(offset),
107
+ this._knex('chat')
108
+ .where('userId', userId)
109
+ .where('status', 'active')
110
+ .count('* as count')
111
+ ]);
112
+
113
+ const totalCount = parseInt(count as string);
114
+ return {
115
+ success: true,
116
+ data: results,
117
+ page,
118
+ limit,
119
+ count: results.length,
120
+ totalCount,
121
+ totalPages: Math.ceil(totalCount / limit)
122
+ };
123
+ }
124
+ }
@@ -0,0 +1,66 @@
1
+ import { Knex } from "knex";
2
+ import { IBaseDAO, IDataPaginator } from "../../d.types";
3
+ import { IFolder } from "../../interfaces/folder/folder.interfaces";
4
+ import KnexManager from "../../KnexConnection";
5
+
6
+ export class FolderDAO implements IBaseDAO<IFolder> {
7
+ private _knex: Knex<any, unknown[]> = KnexManager.getConnection();
8
+
9
+ async create(item: IFolder): Promise<IFolder> {
10
+ const [createdFolder] = await this._knex("folders").insert(item).returning("*");
11
+ return createdFolder;
12
+ }
13
+
14
+ async getById(id: number): Promise<IFolder | null> {
15
+ const folder = await this._knex("folders as f")
16
+ .innerJoin("study as s", "f.studyId", "s.id")
17
+ .select("f.*", this._knex.raw("to_jsonb(s.*) as study"))
18
+ .where("f.id", id)
19
+ .first();
20
+ return folder || null;
21
+ }
22
+
23
+ async getByUuid(uuid: string): Promise<IFolder | null> {
24
+ const folder = await this._knex("folders as f")
25
+ .innerJoin("study as s", "f.studyId", "s.id")
26
+ .select("f.*", this._knex.raw("to_jsonb(s.*) as study"))
27
+ .where("f.uuid", uuid)
28
+ .first();
29
+ return folder || null;
30
+ }
31
+
32
+ async update(id: number, item: Partial<IFolder>): Promise<IFolder | null> {
33
+ const [updatedFolder] = await this._knex("folders").where({ id }).update(item).returning("*");
34
+ return updatedFolder || null;
35
+ }
36
+
37
+ async delete(id: number): Promise<boolean> {
38
+ const result = await this._knex("folders").where({ id }).del();
39
+ return result > 0;
40
+ }
41
+
42
+ async getAll(page: number, limit: number, studyId?: number | null): Promise<IDataPaginator<IFolder>> {
43
+ const offset = (page - 1) * limit;
44
+
45
+ const query = this._knex("folders as f")
46
+ .innerJoin("study as s", "f.studyId", "s.id")
47
+ .select("f.*", this._knex.raw("to_jsonb(s.*) as study"));
48
+ if (studyId !== undefined && studyId !== null) {
49
+ query.where("f.studyId", studyId);
50
+ }
51
+
52
+ const [countResult] = await query.clone().clearSelect().count("* as count");
53
+ const totalCount = +countResult.count;
54
+ const folders = await query.clone().limit(limit).offset(offset);
55
+
56
+ return {
57
+ success: true,
58
+ data: folders,
59
+ page,
60
+ limit,
61
+ count: folders.length,
62
+ totalCount,
63
+ totalPages: Math.ceil(totalCount / limit),
64
+ };
65
+ }
66
+ }
@@ -0,0 +1,102 @@
1
+ import { Knex } from "knex";
2
+ import { IBaseDAO, IDataPaginator } from "../../d.types";
3
+ import { IMessage, IMessageCreate, IMessageUpdate } from '../../interfaces/message/message.interfaces';
4
+ import KnexManager from "../../KnexConnection";
5
+
6
+ export class MessageDAO implements IBaseDAO<IMessage> {
7
+ private _knex: Knex<any, unknown[]> = KnexManager.getConnection();
8
+
9
+ async create(item: IMessageCreate): Promise<IMessage> {
10
+ const [result] = await this._knex('message')
11
+ .insert(item)
12
+ .returning('*');
13
+ return result;
14
+ }
15
+
16
+ async getById(id: number): Promise<IMessage | null> {
17
+ const result = await this._knex('message')
18
+ .where('id', id)
19
+ .first();
20
+ return result || null;
21
+ }
22
+
23
+ async getByUuid(uuid: string): Promise<IMessage | null> {
24
+ // Message table doesn't have uuid column, returning null
25
+ return null;
26
+ }
27
+
28
+ async getAll(page = 1, limit = 10): Promise<IDataPaginator<IMessage>> {
29
+ const offset = (page - 1) * limit;
30
+
31
+ const [results, [{ count }]] = await Promise.all([
32
+ this._knex('message')
33
+ .orderBy('created_at', 'asc')
34
+ .limit(limit)
35
+ .offset(offset),
36
+ this._knex('message').count('* as count')
37
+ ]);
38
+
39
+ const totalCount = parseInt(count as string);
40
+ return {
41
+ success: true,
42
+ data: results,
43
+ page,
44
+ limit,
45
+ count: results.length,
46
+ totalCount,
47
+ totalPages: Math.ceil(totalCount / limit)
48
+ };
49
+ }
50
+
51
+ async update(id: number, item: IMessageUpdate): Promise<IMessage | null> {
52
+ const [result] = await this._knex('message')
53
+ .where('id', id)
54
+ .update({
55
+ ...item,
56
+ updated_at: new Date()
57
+ })
58
+ .returning('*');
59
+ return result || null;
60
+ }
61
+
62
+ async delete(id: number): Promise<boolean> {
63
+ const result = await this._knex('message')
64
+ .where('id', id)
65
+ .delete();
66
+ return result > 0;
67
+ }
68
+
69
+ async getByChatId(chatId: number, page = 1, limit = 50): Promise<IDataPaginator<IMessage>> {
70
+ const offset = (page - 1) * limit;
71
+
72
+ const [results, [{ count }]] = await Promise.all([
73
+ this._knex('message')
74
+ .where('chatId', chatId)
75
+ .orderBy('created_at', 'asc')
76
+ .limit(limit)
77
+ .offset(offset),
78
+ this._knex('message')
79
+ .where('chatId', chatId)
80
+ .count('* as count')
81
+ ]);
82
+
83
+ const totalCount = parseInt(count as string);
84
+ return {
85
+ success: true,
86
+ data: results,
87
+ page,
88
+ limit,
89
+ count: results.length,
90
+ totalCount,
91
+ totalPages: Math.ceil(totalCount / limit)
92
+ };
93
+ }
94
+
95
+ async getLatestMessages(chatId: number, limit = 50): Promise<IMessage[]> {
96
+ return await this._knex('message')
97
+ .where('chatId', chatId)
98
+ .orderBy('created_at', 'desc')
99
+ .limit(limit)
100
+ .then((messages: IMessage[]) => messages.reverse());
101
+ }
102
+ }
@@ -0,0 +1,66 @@
1
+ import { Knex } from "knex";
2
+ import { IBaseDAO, IDataPaginator } from "../../d.types";
3
+ import { IStudy } from "../../interfaces/study/study.interfaces";
4
+ import KnexManager from "../../KnexConnection";
5
+
6
+ export class StudyDAO implements IBaseDAO<IStudy> {
7
+ private _knex: Knex<any, unknown[]> = KnexManager.getConnection();
8
+
9
+ async create(item: IStudy): Promise<IStudy> {
10
+ const [createdStudy] = await this._knex("study").insert(item).returning("*");
11
+ return createdStudy;
12
+ }
13
+
14
+ async getById(id: number): Promise<IStudy | null> {
15
+ const study = await this._knex("study as s")
16
+ .innerJoin("users as u", "s.createdBy", "u.id")
17
+ .select("s.*", this._knex.raw("to_jsonb(u.*) as user"))
18
+ .where("s.id", id)
19
+ .first();
20
+ return study || null;
21
+ }
22
+
23
+ async getByUuid(uuid: string): Promise<IStudy | null> {
24
+ const study = await this._knex("study as s")
25
+ .innerJoin("users as u", "s.createdBy", "u.id")
26
+ .select("s.*", this._knex.raw("to_jsonb(u.*) as user"))
27
+ .where("s.uuid", uuid)
28
+ .first();
29
+ return study || null;
30
+ }
31
+
32
+ async update(id: number, item: Partial<IStudy>): Promise<IStudy | null> {
33
+ const [updatedStudy] = await this._knex("study").where({ id }).update(item).returning("*");
34
+ return updatedStudy || null;
35
+ }
36
+
37
+ async delete(id: number): Promise<boolean> {
38
+ const result = await this._knex("study").where({ id }).del();
39
+ return result > 0;
40
+ }
41
+
42
+ async getAll(page: number, limit: number, createdBy?: number | null): Promise<IDataPaginator<IStudy>> {
43
+ const offset = (page - 1) * limit;
44
+
45
+ const query = this._knex("study as s")
46
+ .innerJoin("users as u", "s.createdBy", "u.id")
47
+ .select("s.*", this._knex.raw("to_jsonb(u.*) as user"));
48
+ if (createdBy !== undefined && createdBy !== null) {
49
+ query.where("s.createdBy", createdBy);
50
+ }
51
+
52
+ const [countResult] = await query.clone().clearSelect().count("* as count");
53
+ const totalCount = +countResult.count;
54
+ const studies = await query.clone().limit(limit).offset(offset);
55
+
56
+ return {
57
+ success: true,
58
+ data: studies,
59
+ page,
60
+ limit,
61
+ count: studies.length,
62
+ totalCount,
63
+ totalPages: Math.ceil(totalCount / limit),
64
+ };
65
+ }
66
+ }
@@ -7,38 +7,42 @@ export class UserDAO implements IBaseDAO<IUser> {
7
7
  private _knex: Knex<any, unknown[]> = KnexManager.getConnection();
8
8
 
9
9
  async create(item: IUser): Promise<IUser> {
10
- const [createdUser] = await this._knex("user").insert(item).returning("*");
10
+ const [createdUser] = await this._knex("users").insert(item).returning("*");
11
11
  return createdUser;
12
12
  }
13
13
 
14
14
  async getById(id: number): Promise<IUser | null> {
15
- const user = await this._knex("user").where({ id }).first();
15
+ const user = await this._knex("users").where({ id }).first();
16
16
  return user || null;
17
17
  }
18
18
 
19
19
  async getByUuid(uuid: string): Promise<IUser | null> {
20
- const user = await this._knex("user").where({ uuid }).first();
20
+ const user = await this._knex("users").where({ uuid }).first();
21
+ return user || null;
22
+ }
23
+
24
+
25
+ async getByEmail(email: string): Promise<IUser | null> {
26
+ const user = await this._knex("users").where({ email }).first();
21
27
  return user || null;
22
28
  }
23
29
 
24
30
  async update(id: number, item: Partial<IUser>): Promise<IUser | null> {
25
- const [updatedUser] = await this._knex("user").where({ id }).update(item).returning("*");
31
+ const [updatedUser] = await this._knex("users").where({ id }).update(item).returning("*");
26
32
  return updatedUser || null;
27
33
  }
28
34
 
29
35
  async delete(id: number): Promise<boolean> {
30
- const result = await this._knex("user").where({ id }).del();
36
+ const result = await this._knex("users").where({ id }).del();
31
37
  return result > 0;
32
38
  }
33
39
 
34
- async getAll(): Promise<IDataPaginator<IUser>> {
35
- const page = 1; // Default to first page
36
- const limit = 10; // Default limit
40
+ async getAll(page: number, limit: number): Promise<IDataPaginator<IUser>> {
37
41
  const offset = (page - 1) * limit;
38
42
 
39
- const [countResult] = await this._knex("user").count("* as count");
43
+ const [countResult] = await this._knex("users").count("* as count");
40
44
  const totalCount = +countResult.count;
41
- const users = await this._knex("user").limit(limit).offset(offset);
45
+ const users = await this._knex("users").limit(limit).offset(offset);
42
46
 
43
47
  return {
44
48
  success: true,
@@ -0,0 +1,65 @@
1
+ import { Knex } from "knex";
2
+ import { IBaseDAO, IDataPaginator } from "../../d.types";
3
+ import { IUserPushNotificationToken } from "../../interfaces/user-push-notification-token/user-push-notification-token.interfaces";
4
+ import KnexManager from "../../KnexConnection";
5
+
6
+ export class UserPushNotificationTokenDAO implements IBaseDAO<IUserPushNotificationToken> {
7
+ private _knex: Knex<any, unknown[]> = KnexManager.getConnection();
8
+
9
+ async create(item: IUserPushNotificationToken): Promise<IUserPushNotificationToken> {
10
+ const [createdToken] = await this._knex("user_push_notification_token").insert(item).returning("*");
11
+ return createdToken;
12
+ }
13
+
14
+ async getById(id: number): Promise<IUserPushNotificationToken | null> {
15
+ const token = await this._knex("user_push_notification_token").where({ id }).first();
16
+ return token || null;
17
+ }
18
+
19
+ async getByUuid(uuid: string): Promise<IUserPushNotificationToken | null> {
20
+ throw new Error("Method not implemented. UserPushNotificationToken does not have UUID field.");
21
+ }
22
+
23
+ async getByUserId(userId: number): Promise<IUserPushNotificationToken[]> {
24
+ const tokens = await this._knex("user_push_notification_token").where({ userId });
25
+ return tokens;
26
+ }
27
+
28
+ async getByToken(token: string): Promise<IUserPushNotificationToken | null> {
29
+ const tokenRecord = await this._knex("user_push_notification_token").where({ token }).first();
30
+ return tokenRecord || null;
31
+ }
32
+
33
+ async getAvailableByUserId(userId: number): Promise<IUserPushNotificationToken[]> {
34
+ const tokens = await this._knex("user_push_notification_token").where({ userId, available: true });
35
+ return tokens;
36
+ }
37
+
38
+ async update(id: number, item: Partial<IUserPushNotificationToken>): Promise<IUserPushNotificationToken | null> {
39
+ const [updatedToken] = await this._knex("user_push_notification_token").where({ id }).update(item).returning("*");
40
+ return updatedToken || null;
41
+ }
42
+
43
+ async delete(id: number): Promise<boolean> {
44
+ const result = await this._knex("user_push_notification_token").where({ id }).del();
45
+ return result > 0;
46
+ }
47
+
48
+ async getAll(page: number, limit: number): Promise<IDataPaginator<IUserPushNotificationToken>> {
49
+ const offset = (page - 1) * limit;
50
+
51
+ const [countResult] = await this._knex("user_push_notification_token").count("* as count");
52
+ const totalCount = +countResult.count;
53
+ const tokens = await this._knex("user_push_notification_token").limit(limit).offset(offset);
54
+
55
+ return {
56
+ success: true,
57
+ data: tokens,
58
+ page,
59
+ limit,
60
+ count: tokens.length,
61
+ totalCount,
62
+ totalPages: Math.ceil(totalCount / limit),
63
+ };
64
+ }
65
+ }
@@ -0,0 +1,120 @@
1
+ import { Knex } from "knex";
2
+ import { IBaseDAO, IDataPaginator } from "../../d.types";
3
+ import { IVideo } from "../../interfaces/video/video.interfaces";
4
+ import KnexManager from "../../KnexConnection";
5
+
6
+ export class VideoDAO implements IBaseDAO<IVideo> {
7
+ private _knex: Knex<any, unknown[]> = KnexManager.getConnection();
8
+
9
+ static getInstance(): Knex<any, unknown[]> {
10
+ return KnexManager.getConnection();
11
+ }
12
+
13
+ async create(item: IVideo): Promise<IVideo> {
14
+ const [createdVideo] = await this._knex("video").insert(item).returning("*");
15
+ return createdVideo;
16
+ }
17
+
18
+ async getById(id: number): Promise<IVideo | null> {
19
+ const video = await this._knex("video as v")
20
+ .innerJoin("folders as f", "v.folderId", "f.id")
21
+ .select("v.*", this._knex.raw("to_jsonb(f.*) as folder"))
22
+ .where("v.id", id)
23
+ .first();
24
+ return video || null;
25
+ }
26
+
27
+ async getByUuid(uuid: string): Promise<IVideo | null> {
28
+ const video = await this._knex("video as v")
29
+ .innerJoin("folders as f", "v.folderId", "f.id")
30
+ .select("v.*", this._knex.raw("to_jsonb(f.*) as folder"))
31
+ .where("v.uuid", uuid)
32
+ .first();
33
+ return video || null;
34
+ }
35
+
36
+ async update(id: number, item: Partial<IVideo>): Promise<IVideo | null> {
37
+ const [updatedVideo] = await this._knex("video").where({ id }).update(item).returning("*");
38
+ return updatedVideo || null;
39
+ }
40
+
41
+ async delete(id: number): Promise<boolean> {
42
+ const result = await this._knex("video").where({ id }).del();
43
+ return result > 0;
44
+ }
45
+
46
+ // entityId corresponde al id del folder al que pertenece el video
47
+ async getAll(page: number, limit: number, folderId?: number | null): Promise<IDataPaginator<IVideo>> {
48
+ const offset = (page - 1) * limit;
49
+
50
+ const query = this._knex("video as v")
51
+ .innerJoin("folders as f", "v.folderId", "f.id")
52
+ .select("v.*", this._knex.raw("to_jsonb(f.*) as folder"));
53
+ if (folderId !== undefined && folderId !== null) {
54
+ query.where("v.folderId", folderId);
55
+ }
56
+
57
+ const [countResult] = await query.clone().clearSelect().count("* as count");
58
+ const totalCount = +countResult.count;
59
+ const videos = await query.clone().limit(limit).offset(offset);
60
+
61
+ return {
62
+ success: true,
63
+ data: videos,
64
+ page,
65
+ limit,
66
+ count: videos.length,
67
+ totalCount,
68
+ totalPages: Math.ceil(totalCount / limit),
69
+ };
70
+ }
71
+
72
+ async getVideoStatsByFolderIds(folderIds: number[]): Promise<{
73
+ total_videos: number;
74
+ completed_videos: number;
75
+ failed_videos: number;
76
+ processing_videos: number;
77
+ }> {
78
+ if (!folderIds || folderIds.length === 0) {
79
+ return {
80
+ total_videos: 0,
81
+ completed_videos: 0,
82
+ failed_videos: 0,
83
+ processing_videos: 0
84
+ };
85
+ }
86
+
87
+ const result = await this._knex("video")
88
+ .whereIn("folderId", folderIds)
89
+ .select(
90
+ this._knex.raw('COUNT(*) as total_videos'),
91
+ this._knex.raw("COUNT(CASE WHEN status = ? THEN 1 END) as completed_videos", ['COMPLETED']),
92
+ this._knex.raw("COUNT(CASE WHEN status = ? THEN 1 END) as failed_videos", ['FAILED']),
93
+ this._knex.raw("COUNT(CASE WHEN status = ? THEN 1 END) as processing_videos", ['PROCESSING'])
94
+ )
95
+ .first() as any;
96
+
97
+ return {
98
+ total_videos: parseInt(result?.total_videos) || 0,
99
+ completed_videos: parseInt(result?.completed_videos) || 0,
100
+ failed_videos: parseInt(result?.failed_videos) || 0,
101
+ processing_videos: parseInt(result?.processing_videos) || 0
102
+ };
103
+ }
104
+
105
+ async getCompletedVideosByFolderIds(folderIds: number[], videoType?: string): Promise<IVideo[]> {
106
+ if (!folderIds || folderIds.length === 0) {
107
+ return [];
108
+ }
109
+
110
+ const query = this._knex("video")
111
+ .whereIn("folderId", folderIds)
112
+ .where("status", "COMPLETED");
113
+
114
+ if (videoType) {
115
+ query.where("videoType", videoType);
116
+ }
117
+
118
+ return await query.select("id", "name", "metadata", "results", "created_at", this._knex.raw("EXTRACT(EPOCH FROM (updated_at - created_at)) as duration"));
119
+ }
120
+ }