@trafficgroup/knex-rel 0.1.8 → 0.1.10

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 (115) hide show
  1. package/.claude/settings.local.json +5 -2
  2. package/.env.prod +5 -0
  3. package/CLAUDE.md +2 -11
  4. package/dist/constants/video.constants.d.ts +12 -0
  5. package/dist/constants/video.constants.js +18 -0
  6. package/dist/constants/video.constants.js.map +1 -0
  7. package/dist/dao/VideoMinuteResultDAO.d.ts +4 -1
  8. package/dist/dao/VideoMinuteResultDAO.js +28 -31
  9. package/dist/dao/VideoMinuteResultDAO.js.map +1 -1
  10. package/dist/dao/auth/auth.dao.js +1 -4
  11. package/dist/dao/auth/auth.dao.js.map +1 -1
  12. package/dist/dao/batch/batch.dao.js +14 -13
  13. package/dist/dao/batch/batch.dao.js.map +1 -1
  14. package/dist/dao/camera/camera.dao.d.ts +17 -7
  15. package/dist/dao/camera/camera.dao.js +38 -56
  16. package/dist/dao/camera/camera.dao.js.map +1 -1
  17. package/dist/dao/chat/chat.dao.d.ts +1 -1
  18. package/dist/dao/chat/chat.dao.js +35 -25
  19. package/dist/dao/chat/chat.dao.js.map +1 -1
  20. package/dist/dao/folder/folder.dao.js +4 -8
  21. package/dist/dao/folder/folder.dao.js.map +1 -1
  22. package/dist/dao/location/location.dao.d.ts +17 -0
  23. package/dist/dao/location/location.dao.js +116 -0
  24. package/dist/dao/location/location.dao.js.map +1 -0
  25. package/dist/dao/message/message.dao.d.ts +1 -1
  26. package/dist/dao/message/message.dao.js +26 -18
  27. package/dist/dao/message/message.dao.js.map +1 -1
  28. package/dist/dao/report-configuration/report-configuration.dao.js +31 -32
  29. package/dist/dao/report-configuration/report-configuration.dao.js.map +1 -1
  30. package/dist/dao/study/study.dao.d.ts +1 -1
  31. package/dist/dao/study/study.dao.js +12 -17
  32. package/dist/dao/study/study.dao.js.map +1 -1
  33. package/dist/dao/user/user.dao.js +1 -4
  34. package/dist/dao/user/user.dao.js.map +1 -1
  35. package/dist/dao/user-push-notification-token/user-push-notification-token.dao.js +8 -26
  36. package/dist/dao/user-push-notification-token/user-push-notification-token.dao.js.map +1 -1
  37. package/dist/dao/video/video.dao.d.ts +9 -1
  38. package/dist/dao/video/video.dao.js +73 -27
  39. package/dist/dao/video/video.dao.js.map +1 -1
  40. package/dist/index.d.ts +8 -4
  41. package/dist/index.js +8 -1
  42. package/dist/index.js.map +1 -1
  43. package/dist/interfaces/batch/batch.interfaces.d.ts +1 -1
  44. package/dist/interfaces/camera/camera.interfaces.d.ts +4 -2
  45. package/dist/interfaces/chat/chat.interfaces.d.ts +3 -3
  46. package/dist/interfaces/folder/folder.interfaces.d.ts +1 -1
  47. package/dist/interfaces/location/location.interfaces.d.ts +9 -0
  48. package/dist/interfaces/location/location.interfaces.js +3 -0
  49. package/dist/interfaces/location/location.interfaces.js.map +1 -0
  50. package/dist/interfaces/message/message.interfaces.d.ts +2 -2
  51. package/dist/interfaces/study/study.interfaces.d.ts +6 -5
  52. package/dist/interfaces/user/user.interfaces.d.ts +1 -1
  53. package/dist/interfaces/user-push-notification-token/user-push-notification-token.interfaces.d.ts +1 -1
  54. package/dist/interfaces/video/video.interfaces.d.ts +3 -2
  55. package/migrations/20250717160737_migration.ts +1 -1
  56. package/migrations/20250717160908_migration.ts +2 -5
  57. package/migrations/20250717161310_migration.ts +1 -1
  58. package/migrations/20250717161406_migration.ts +3 -3
  59. package/migrations/20250717162431_migration.ts +1 -1
  60. package/migrations/20250717173228_migration.ts +2 -2
  61. package/migrations/20250717204731_migration.ts +1 -1
  62. package/migrations/20250722210109_migration.ts +4 -8
  63. package/migrations/20250722211019_migration.ts +1 -1
  64. package/migrations/20250723153852_migration.ts +10 -13
  65. package/migrations/20250723162257_migration.ts +7 -4
  66. package/migrations/20250723171109_migration.ts +7 -4
  67. package/migrations/20250723205331_migration.ts +9 -6
  68. package/migrations/20250724191345_migration.ts +11 -8
  69. package/migrations/20250730180932_migration.ts +13 -14
  70. package/migrations/20250730213625_migration.ts +11 -8
  71. package/migrations/20250804124509_migration.ts +21 -26
  72. package/migrations/20250804132053_migration.ts +8 -5
  73. package/migrations/20250804164518_migration.ts +7 -7
  74. package/migrations/20250823223016_migration.ts +21 -32
  75. package/migrations/20250910015452_migration.ts +6 -18
  76. package/migrations/20250911000000_migration.ts +4 -18
  77. package/migrations/20250917144153_migration.ts +7 -14
  78. package/migrations/20250930200521_migration.ts +4 -8
  79. package/migrations/20251010143500_migration.ts +6 -27
  80. package/migrations/20251020225758_migration.ts +15 -51
  81. package/migrations/20251112120000_migration.ts +81 -0
  82. package/migrations/20251112120100_migration.ts +21 -0
  83. package/migrations/20251112120200_migration.ts +38 -0
  84. package/migrations/20251112120300_migration.ts +22 -0
  85. package/package.json +1 -1
  86. package/src/constants/video.constants.ts +19 -0
  87. package/src/d.types.ts +14 -18
  88. package/src/dao/VideoMinuteResultDAO.ts +54 -72
  89. package/src/dao/auth/auth.dao.ts +55 -58
  90. package/src/dao/batch/batch.dao.ts +98 -101
  91. package/src/dao/camera/camera.dao.ts +125 -145
  92. package/src/dao/chat/chat.dao.ts +43 -45
  93. package/src/dao/folder/folder.dao.ts +56 -59
  94. package/src/dao/location/location.dao.ts +101 -0
  95. package/src/dao/message/message.dao.ts +32 -32
  96. package/src/dao/report-configuration/report-configuration.dao.ts +342 -370
  97. package/src/dao/study/study.dao.ts +63 -88
  98. package/src/dao/user/user.dao.ts +50 -52
  99. package/src/dao/user-push-notification-token/user-push-notification-token.dao.ts +49 -83
  100. package/src/dao/video/video.dao.ts +339 -313
  101. package/src/entities/BaseEntity.ts +1 -1
  102. package/src/index.ts +24 -26
  103. package/src/interfaces/auth/auth.interfaces.ts +10 -10
  104. package/src/interfaces/batch/batch.interfaces.ts +1 -1
  105. package/src/interfaces/camera/camera.interfaces.ts +9 -7
  106. package/src/interfaces/chat/chat.interfaces.ts +4 -4
  107. package/src/interfaces/folder/folder.interfaces.ts +2 -2
  108. package/src/interfaces/location/location.interfaces.ts +9 -0
  109. package/src/interfaces/message/message.interfaces.ts +3 -3
  110. package/src/interfaces/report-configuration/report-configuration.interfaces.ts +16 -16
  111. package/src/interfaces/study/study.interfaces.ts +7 -6
  112. package/src/interfaces/user/user.interfaces.ts +9 -9
  113. package/src/interfaces/user-push-notification-token/user-push-notification-token.interfaces.ts +9 -9
  114. package/src/interfaces/video/video.interfaces.ts +34 -33
  115. package/plan.md +0 -684
@@ -4,61 +4,58 @@ import { IAuth } from "../../interfaces/auth/auth.interfaces";
4
4
  import KnexManager from "../../KnexConnection";
5
5
 
6
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")
36
- .where({ id })
37
- .update(item)
38
- .returning("*");
39
- return updatedAuth || null;
40
- }
41
-
42
- async delete(id: number): Promise<boolean> {
43
- const result = await this._knex("auth").where({ id }).del();
44
- return result > 0;
45
- }
46
-
47
- async getAll(page: number, limit: number): Promise<IDataPaginator<IAuth>> {
48
- const offset = (page - 1) * limit;
49
-
50
- const [countResult] = await this._knex("auth").count("* as count");
51
- const totalCount = +countResult.count;
52
- const auths = await this._knex("auth").limit(limit).offset(offset);
53
-
54
- return {
55
- success: true,
56
- data: auths,
57
- page,
58
- limit,
59
- count: auths.length,
60
- totalCount,
61
- totalPages: Math.ceil(totalCount / limit),
62
- };
63
- }
64
- }
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
+ }
@@ -4,118 +4,115 @@ import { IBatch } from "../../interfaces/batch/batch.interfaces";
4
4
  import KnexManager from "../../KnexConnection";
5
5
 
6
6
  export class BatchDAO implements IBaseDAO<IBatch> {
7
- private _knex: Knex<any, unknown[]> = KnexManager.getConnection();
7
+ private _knex: Knex<any, unknown[]> = KnexManager.getConnection();
8
8
 
9
- async create(item: IBatch): Promise<IBatch> {
10
- const [createdBatch] = await this._knex("video_batch")
11
- .insert(item)
12
- .returning("*");
13
- return createdBatch;
14
- }
9
+ async create(item: IBatch): Promise<IBatch> {
10
+ const [createdBatch] = await this._knex("video_batch").insert(item).returning("*");
11
+ return createdBatch;
12
+ }
15
13
 
16
- async getById(id: number): Promise<IBatch | null> {
17
- const batch = await this._knex("video_batch as b")
18
- .innerJoin("folders as f", "b.folderId", "f.id")
19
- .select("b.*", this._knex.raw("to_jsonb(f.*) as folder"))
20
- .where("b.id", id)
21
- .first();
22
- return batch || null;
23
- }
14
+ async getById(id: number): Promise<IBatch | null> {
15
+ const batch = await this._knex("video_batch as b")
16
+ .innerJoin("folders as f", "b.folderId", "f.id")
17
+ .select("b.*", this._knex.raw("to_jsonb(f.*) as folder"))
18
+ .where("b.id", id)
19
+ .first();
20
+ return batch || null;
21
+ }
24
22
 
25
- async getByUuid(uuid: string): Promise<IBatch | null> {
26
- const batch = await this._knex("video_batch as b")
27
- .innerJoin("folders as f", "b.folderId", "f.id")
28
- .select("b.*", this._knex.raw("to_jsonb(f.*) as folder"))
29
- .where("b.uuid", uuid)
30
- .first();
31
- return batch || null;
32
- }
23
+ async getByUuid(uuid: string): Promise<IBatch | null> {
24
+ const batch = await this._knex("video_batch as b")
25
+ .innerJoin("folders as f", "b.folderId", "f.id")
26
+ .select("b.*", this._knex.raw("to_jsonb(f.*) as folder"))
27
+ .where("b.uuid", uuid)
28
+ .first();
29
+ return batch || null;
30
+ }
33
31
 
34
- async update(id: number, item: Partial<IBatch>): Promise<IBatch | null> {
35
- const [updatedBatch] = await this._knex("video_batch")
36
- .where({ id })
37
- .update(item)
38
- .returning("*");
39
- return updatedBatch || null;
40
- }
32
+ async update(id: number, item: Partial<IBatch>): Promise<IBatch | null> {
33
+ const [updatedBatch] = await this._knex("video_batch").where({ id }).update(item).returning("*");
34
+ return updatedBatch || null;
35
+ }
41
36
 
42
- async delete(id: number): Promise<boolean> {
43
- const result = await this._knex("video_batch").where({ id }).del();
44
- return result > 0;
45
- }
37
+ async delete(id: number): Promise<boolean> {
38
+ const result = await this._knex("video_batch").where({ id }).del();
39
+ return result > 0;
40
+ }
46
41
 
47
- async getAll(
48
- page: number,
49
- limit: number,
50
- folderId?: number | null,
51
- ): Promise<IDataPaginator<IBatch>> {
52
- const offset = (page - 1) * limit;
42
+ async getAll(page: number, limit: number, folderId?: number | null): Promise<IDataPaginator<IBatch>> {
43
+ const offset = (page - 1) * limit;
53
44
 
54
- const query = this._knex("video_batch as b")
55
- .innerJoin("folders as f", "b.folderId", "f.id")
56
- .select("b.*", this._knex.raw("to_jsonb(f.*) as folder"));
57
- if (folderId !== undefined && folderId !== null) {
58
- query.where("b.folderId", folderId);
59
- }
45
+ const query = this._knex("video_batch as b")
46
+ .innerJoin("folders as f", "b.folderId", "f.id")
47
+ .select("b.*", this._knex.raw("to_jsonb(f.*) as folder"));
48
+ if (folderId !== undefined && folderId !== null) {
49
+ query.where("b.folderId", folderId);
50
+ }
60
51
 
61
- const [countResult] = await query.clone().clearSelect().count("* as count");
62
- const totalCount = +countResult.count;
63
- const batches = await query.clone().limit(limit).offset(offset);
52
+ const [countResult] = await query.clone().clearSelect().count("* as count");
53
+ const totalCount = +countResult.count;
54
+ const batches = await query.clone().limit(limit).offset(offset);
64
55
 
65
- return {
66
- success: true,
67
- data: batches,
68
- page,
69
- limit,
70
- count: batches.length,
71
- totalCount,
72
- totalPages: Math.ceil(totalCount / limit),
73
- };
74
- }
56
+ return {
57
+ success: true,
58
+ data: batches,
59
+ page,
60
+ limit,
61
+ count: batches.length,
62
+ totalCount,
63
+ totalPages: Math.ceil(totalCount / limit),
64
+ };
65
+ }
75
66
 
76
- /**
77
- * Get batches by folder ID
78
- */
79
- async getByFolderId(folderId: number): Promise<IBatch[]> {
80
- return this._knex("video_batch as b")
81
- .innerJoin("folders as f", "b.folderId", "f.id")
82
- .select("b.*", this._knex.raw("to_jsonb(f.*) as folder"))
83
- .where("b.folderId", folderId)
84
- .orderBy("b.created_at", "desc");
85
- }
67
+ /**
68
+ * Get batches by folder ID
69
+ */
70
+ async getByFolderId(folderId: number): Promise<IBatch[]> {
71
+ return this._knex("video_batch as b")
72
+ .innerJoin("folders as f", "b.folderId", "f.id")
73
+ .select("b.*", this._knex.raw("to_jsonb(f.*) as folder"))
74
+ .where("b.folderId", folderId)
75
+ .orderBy("b.created_at", "desc");
76
+ }
86
77
 
87
- /**
88
- * Update batch progress
89
- */
90
- async updateProgress(
91
- id: number,
92
- completedVideos: number,
93
- failedVideos: number,
94
- ): Promise<void> {
95
- await this._knex("video_batch").where({ id }).update({
96
- completedVideos,
97
- failedVideos,
98
- updated_at: this._knex.fn.now(),
99
- });
100
- }
78
+ /**
79
+ * Update batch progress
80
+ */
81
+ async updateProgress(
82
+ id: number,
83
+ completedVideos: number,
84
+ failedVideos: number
85
+ ): Promise<void> {
86
+ await this._knex("video_batch")
87
+ .where({ id })
88
+ .update({
89
+ completedVideos,
90
+ failedVideos,
91
+ updated_at: this._knex.fn.now()
92
+ });
93
+ }
101
94
 
102
- /**
103
- * Mark batch as completed
104
- */
105
- async markCompleted(id: number): Promise<void> {
106
- await this._knex("video_batch").where({ id }).update({
107
- status: "COMPLETED",
108
- updated_at: this._knex.fn.now(),
109
- });
110
- }
95
+ /**
96
+ * Mark batch as completed
97
+ */
98
+ async markCompleted(id: number): Promise<void> {
99
+ await this._knex("video_batch")
100
+ .where({ id })
101
+ .update({
102
+ status: "COMPLETED",
103
+ updated_at: this._knex.fn.now()
104
+ });
105
+ }
111
106
 
112
- /**
113
- * Mark batch as failed
114
- */
115
- async markFailed(id: number): Promise<void> {
116
- await this._knex("video_batch").where({ id }).update({
117
- status: "FAILED",
118
- updated_at: this._knex.fn.now(),
119
- });
120
- }
107
+ /**
108
+ * Mark batch as failed
109
+ */
110
+ async markFailed(id: number): Promise<void> {
111
+ await this._knex("video_batch")
112
+ .where({ id })
113
+ .update({
114
+ status: "FAILED",
115
+ updated_at: this._knex.fn.now()
116
+ });
117
+ }
121
118
  }
@@ -1,154 +1,134 @@
1
1
  import { Knex } from "knex";
2
2
  import { IBaseDAO, IDataPaginator } from "../../d.types";
3
3
  import { ICamera } from "../../interfaces/camera/camera.interfaces";
4
- import { IVideo } from "../../interfaces/video/video.interfaces";
5
4
  import KnexManager from "../../KnexConnection";
6
5
 
7
6
  export class CameraDAO implements IBaseDAO<ICamera> {
8
- private _knex: Knex<any, unknown[]> = KnexManager.getConnection();
9
-
10
- async create(item: ICamera): Promise<ICamera> {
11
- const [createdCamera] = await this._knex("cameras")
12
- .insert(item)
13
- .returning("*");
14
- return createdCamera;
15
- }
16
-
17
- async getById(id: number): Promise<ICamera | null> {
18
- const camera = await this._knex("cameras").where({ id }).first();
19
- return camera || null;
20
- }
21
-
22
- async getByUuid(uuid: string): Promise<ICamera | null> {
23
- const camera = await this._knex("cameras").where({ uuid }).first();
24
- return camera || null;
25
- }
26
-
27
- async update(id: number, item: Partial<ICamera>): Promise<ICamera | null> {
28
- const [updatedCamera] = await this._knex("cameras")
29
- .where({ id })
30
- .update(item)
31
- .returning("*");
32
- return updatedCamera || null;
33
- }
34
-
35
- async delete(id: number): Promise<boolean> {
36
- const result = await this._knex("cameras").where({ id }).del();
37
- return result > 0;
38
- }
39
-
40
- async getAll(page: number, limit: number): Promise<IDataPaginator<ICamera>> {
41
- const offset = (page - 1) * limit;
42
-
43
- const [countResult] = await this._knex("cameras").count("* as count");
44
- const totalCount = +countResult.count;
45
- const cameras = await this._knex("cameras").limit(limit).offset(offset);
46
-
47
- return {
48
- success: true,
49
- data: cameras,
50
- page,
51
- limit,
52
- count: cameras.length,
53
- totalCount,
54
- totalPages: Math.ceil(totalCount / limit),
55
- };
56
- }
57
-
58
- async getByName(name: string): Promise<ICamera | null> {
59
- const camera = await this._knex("cameras").where({ name }).first();
60
- return camera || null;
61
- }
62
-
63
- async getCamerasNearCoordinates(
64
- longitude: number,
65
- latitude: number,
66
- radiusKm: number = 1,
67
- ): Promise<ICamera[]> {
68
- // Using ST_DWithin for geographic distance calculation
69
- // This is a PostgreSQL-specific query for geospatial operations
70
- const cameras = await this._knex("cameras").whereRaw(
71
- `ST_DWithin(
72
- ST_MakePoint(longitude, latitude)::geography,
73
- ST_MakePoint(?, ?)::geography,
74
- ?
75
- )`,
76
- [longitude, latitude, radiusKm * 1000], // Convert km to meters
77
- );
78
- return cameras;
79
- }
80
-
81
- /**
82
- * Get all cameras with optional search filter by name (case-insensitive partial match)
83
- */
84
- async getAllWithSearch(
85
- page: number,
86
- limit: number,
87
- name?: string,
88
- ): Promise<IDataPaginator<ICamera>> {
89
- const offset = (page - 1) * limit;
90
-
91
- let query = this._knex("cameras");
92
-
93
- // Apply search filter if name provided (escape special chars to prevent pattern injection)
94
- if (name && name.trim().length > 0) {
95
- const escapedName = name.trim().replace(/[%_\\]/g, "\\$&");
96
- query = query.where("name", "ilike", `%${escapedName}%`);
7
+ private _knex: Knex<any, unknown[]> = KnexManager.getConnection();
8
+
9
+ async create(item: ICamera): Promise<ICamera> {
10
+ const [createdCamera] = await this._knex("cameras").insert(item).returning("*");
11
+ return createdCamera;
12
+ }
13
+
14
+ async getById(id: number): Promise<ICamera | null> {
15
+ const camera = await this._knex("cameras").where({ id }).first();
16
+ return camera || null;
17
+ }
18
+
19
+ async getByUuid(uuid: string): Promise<ICamera | null> {
20
+ const camera = await this._knex("cameras").where({ uuid }).first();
21
+ return camera || null;
22
+ }
23
+
24
+ async update(id: number, item: Partial<ICamera>): Promise<ICamera | null> {
25
+ const [updatedCamera] = await this._knex("cameras").where({ id }).update(item).returning("*");
26
+ return updatedCamera || null;
27
+ }
28
+
29
+ async delete(id: number): Promise<boolean> {
30
+ const result = await this._knex("cameras").where({ id }).del();
31
+ return result > 0;
32
+ }
33
+
34
+ async getAll(page: number, limit: number): Promise<IDataPaginator<ICamera>> {
35
+ const offset = (page - 1) * limit;
36
+
37
+ const [countResult] = await this._knex("cameras").count("* as count");
38
+ const totalCount = +countResult.count;
39
+ const cameras = await this._knex("cameras").limit(limit).offset(offset);
40
+
41
+ return {
42
+ success: true,
43
+ data: cameras,
44
+ page,
45
+ limit,
46
+ count: cameras.length,
47
+ totalCount,
48
+ totalPages: Math.ceil(totalCount / limit),
49
+ };
50
+ }
51
+
52
+ /**
53
+ * Get cameras for a specific location
54
+ * @param locationId - The location ID
55
+ * @param status - Optional filter by camera status
56
+ */
57
+ async getCamerasByLocationId(
58
+ locationId: number,
59
+ status?: string
60
+ ): Promise<ICamera[]> {
61
+ let query = this._knex("cameras")
62
+ .where("locationId", locationId);
63
+
64
+ if (status) {
65
+ query = query.where("status", status);
66
+ }
67
+
68
+ return query.orderBy("name", "asc");
69
+ }
70
+
71
+ /**
72
+ * Get cameras for a specific location with pagination
73
+ * @param locationId - The location ID
74
+ * @param page - Page number
75
+ * @param limit - Items per page
76
+ * @param status - Optional filter by camera status
77
+ */
78
+ async getCamerasByLocationIdPaginated(
79
+ locationId: number,
80
+ page: number,
81
+ limit: number,
82
+ status?: string
83
+ ): Promise<IDataPaginator<ICamera>> {
84
+ const offset = (page - 1) * limit;
85
+
86
+ let query = this._knex("cameras")
87
+ .where("locationId", locationId);
88
+
89
+ if (status) {
90
+ query = query.where("status", status);
91
+ }
92
+
93
+ const [countResult] = await query.clone().count("* as count");
94
+ const totalCount = +countResult.count;
95
+ const cameras = await query
96
+ .clone()
97
+ .limit(limit)
98
+ .offset(offset)
99
+ .orderBy("name", "asc");
100
+
101
+ return {
102
+ success: true,
103
+ data: cameras,
104
+ page,
105
+ limit,
106
+ count: cameras.length,
107
+ totalCount,
108
+ totalPages: Math.ceil(totalCount / limit)
109
+ };
97
110
  }
98
111
 
99
- const [countResult] = await query.clone().count("* as count");
100
- const totalCount = +countResult.count;
101
- const cameras = await query
102
- .clone()
103
- .limit(limit)
104
- .offset(offset)
105
- .orderBy("name", "asc");
106
-
107
- return {
108
- success: true,
109
- data: cameras,
110
- page,
111
- limit,
112
- count: cameras.length,
113
- totalCount,
114
- totalPages: Math.ceil(totalCount / limit),
115
- };
116
- }
117
-
118
- /**
119
- * Get paginated videos for a specific camera with folder data included
120
- */
121
- async getVideosByCamera(
122
- cameraId: number,
123
- page: number,
124
- limit: number,
125
- ): Promise<IDataPaginator<IVideo>> {
126
- const offset = (page - 1) * limit;
127
-
128
- const query = this._knex("video as v")
129
- .innerJoin("folders as f", "v.folderId", "f.id")
130
- .select("v.*", this._knex.raw("to_jsonb(f.*) as folder"))
131
- .where("v.cameraId", cameraId);
132
-
133
- // Optimized count query without JOIN
134
- const [countResult] = await this._knex("video as v")
135
- .where("v.cameraId", cameraId)
136
- .count("* as count");
137
- const totalCount = +countResult.count;
138
- const videos = await query
139
- .clone()
140
- .limit(limit)
141
- .offset(offset)
142
- .orderBy("v.created_at", "desc");
143
-
144
- return {
145
- success: true,
146
- data: videos,
147
- page,
148
- limit,
149
- count: videos.length,
150
- totalCount,
151
- totalPages: Math.ceil(totalCount / limit),
152
- };
153
- }
112
+ /**
113
+ * Check if a camera name already exists for a location
114
+ * @param name - Camera name
115
+ * @param locationId - Location ID
116
+ * @param excludeId - Optional camera ID to exclude (for update operations)
117
+ */
118
+ async existsByNameAndLocation(
119
+ name: string,
120
+ locationId: number,
121
+ excludeId?: number
122
+ ): Promise<boolean> {
123
+ let query = this._knex("cameras")
124
+ .where("locationId", locationId)
125
+ .where("name", name);
126
+
127
+ if (excludeId !== undefined) {
128
+ query = query.whereNot("id", excludeId);
129
+ }
130
+
131
+ const result = await query.first();
132
+ return result !== null && result !== undefined;
133
+ }
154
134
  }