@trafficgroup/knex-rel 0.1.21 → 0.1.23-rc.0

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 (121) hide show
  1. package/.claude/settings.local.json +5 -2
  2. package/CLAUDE.md +2 -11
  3. package/dist/constants/folder.constants.d.ts +2 -2
  4. package/dist/constants/folder.constants.js +6 -15
  5. package/dist/constants/folder.constants.js.map +1 -1
  6. package/dist/constants/study.constants.d.ts +1 -1
  7. package/dist/constants/study.constants.js +1 -1
  8. package/dist/constants/video.constants.d.ts +2 -2
  9. package/dist/constants/video.constants.js +5 -9
  10. package/dist/constants/video.constants.js.map +1 -1
  11. package/dist/dao/VideoMinuteResultDAO.d.ts +19 -2
  12. package/dist/dao/VideoMinuteResultDAO.js +75 -29
  13. package/dist/dao/VideoMinuteResultDAO.js.map +1 -1
  14. package/dist/dao/auth/auth.dao.js +1 -4
  15. package/dist/dao/auth/auth.dao.js.map +1 -1
  16. package/dist/dao/batch/batch.dao.d.ts +0 -10
  17. package/dist/dao/batch/batch.dao.js +14 -51
  18. package/dist/dao/batch/batch.dao.js.map +1 -1
  19. package/dist/dao/camera/camera.dao.js +7 -10
  20. package/dist/dao/camera/camera.dao.js.map +1 -1
  21. package/dist/dao/chat/chat.dao.d.ts +1 -1
  22. package/dist/dao/chat/chat.dao.js +40 -27
  23. package/dist/dao/chat/chat.dao.js.map +1 -1
  24. package/dist/dao/folder/folder.dao.js +7 -18
  25. package/dist/dao/folder/folder.dao.js.map +1 -1
  26. package/dist/dao/location/location.dao.js +9 -16
  27. package/dist/dao/location/location.dao.js.map +1 -1
  28. package/dist/dao/message/message.dao.d.ts +1 -1
  29. package/dist/dao/message/message.dao.js +26 -18
  30. package/dist/dao/message/message.dao.js.map +1 -1
  31. package/dist/dao/report-configuration/report-configuration.dao.js +68 -39
  32. package/dist/dao/report-configuration/report-configuration.dao.js.map +1 -1
  33. package/dist/dao/study/study.dao.d.ts +8 -1
  34. package/dist/dao/study/study.dao.js +20 -15
  35. package/dist/dao/study/study.dao.js.map +1 -1
  36. package/dist/dao/systemConfiguration/SystemConfigurationDAO.d.ts +2 -2
  37. package/dist/dao/systemConfiguration/SystemConfigurationDAO.js +26 -26
  38. package/dist/dao/systemConfiguration/SystemConfigurationDAO.js.map +1 -1
  39. package/dist/dao/user/user.dao.js +1 -4
  40. package/dist/dao/user/user.dao.js.map +1 -1
  41. package/dist/dao/user-push-notification-token/user-push-notification-token.dao.js +8 -26
  42. package/dist/dao/user-push-notification-token/user-push-notification-token.dao.js.map +1 -1
  43. package/dist/dao/video/video.dao.js +28 -30
  44. package/dist/dao/video/video.dao.js.map +1 -1
  45. package/dist/index.d.ts +10 -9
  46. package/dist/index.js +4 -4
  47. package/dist/index.js.map +1 -1
  48. package/dist/interfaces/batch/batch.interfaces.d.ts +1 -1
  49. package/dist/interfaces/camera/camera.interfaces.d.ts +1 -1
  50. package/dist/interfaces/chat/chat.interfaces.d.ts +3 -3
  51. package/dist/interfaces/folder/folder.interfaces.d.ts +1 -1
  52. package/dist/interfaces/message/message.interfaces.d.ts +2 -2
  53. package/dist/interfaces/report-configuration/report-configuration.interfaces.d.ts +1 -0
  54. package/dist/interfaces/study/study.interfaces.d.ts +2 -8
  55. package/dist/interfaces/user/user.interfaces.d.ts +1 -1
  56. package/dist/interfaces/user-push-notification-token/user-push-notification-token.interfaces.d.ts +1 -1
  57. package/dist/interfaces/video/video.interfaces.d.ts +2 -2
  58. package/migrations/20250717160737_migration.ts +1 -1
  59. package/migrations/20250717160908_migration.ts +2 -5
  60. package/migrations/20250717161310_migration.ts +1 -1
  61. package/migrations/20250717161406_migration.ts +3 -3
  62. package/migrations/20250717162431_migration.ts +1 -1
  63. package/migrations/20250717173228_migration.ts +2 -2
  64. package/migrations/20250717204731_migration.ts +1 -1
  65. package/migrations/20250722210109_migration.ts +4 -8
  66. package/migrations/20250722211019_migration.ts +1 -1
  67. package/migrations/20250723153852_migration.ts +10 -13
  68. package/migrations/20250723162257_migration.ts +7 -4
  69. package/migrations/20250723171109_migration.ts +7 -4
  70. package/migrations/20250723205331_migration.ts +9 -6
  71. package/migrations/20250724191345_migration.ts +11 -8
  72. package/migrations/20250730180932_migration.ts +13 -14
  73. package/migrations/20250730213625_migration.ts +11 -8
  74. package/migrations/20250804124509_migration.ts +21 -26
  75. package/migrations/20250804132053_migration.ts +8 -5
  76. package/migrations/20250804164518_migration.ts +7 -7
  77. package/migrations/20250823223016_migration.ts +21 -32
  78. package/migrations/20250910015452_migration.ts +6 -18
  79. package/migrations/20250911000000_migration.ts +4 -18
  80. package/migrations/20250917144153_migration.ts +7 -14
  81. package/migrations/20250930200521_migration.ts +4 -8
  82. package/migrations/20251010143500_migration.ts +6 -27
  83. package/migrations/20251020225758_migration.ts +15 -51
  84. package/migrations/20251112120000_migration.ts +2 -10
  85. package/migrations/20251112120200_migration.ts +7 -19
  86. package/migrations/20251112120300_migration.ts +2 -7
  87. package/migrations/20260109140000_migration.ts +2 -7
  88. package/package.json +3 -3
  89. package/src/constants/folder.constants.ts +8 -17
  90. package/src/constants/study.constants.ts +1 -1
  91. package/src/constants/video.constants.ts +7 -11
  92. package/src/d.types.ts +14 -18
  93. package/src/dao/VideoMinuteResultDAO.ts +127 -83
  94. package/src/dao/auth/auth.dao.ts +55 -58
  95. package/src/dao/batch/batch.dao.ts +100 -145
  96. package/src/dao/camera/camera.dao.ts +121 -124
  97. package/src/dao/chat/chat.dao.ts +45 -45
  98. package/src/dao/folder/folder.dao.ts +90 -105
  99. package/src/dao/location/location.dao.ts +87 -109
  100. package/src/dao/message/message.dao.ts +32 -32
  101. package/src/dao/reconciliation-log/reconciliation-log.dao.ts +1 -1
  102. package/src/dao/report-configuration/report-configuration.dao.ts +381 -370
  103. package/src/dao/study/study.dao.ts +83 -94
  104. package/src/dao/systemConfiguration/SystemConfigurationDAO.ts +35 -41
  105. package/src/dao/user/user.dao.ts +50 -52
  106. package/src/dao/user-push-notification-token/user-push-notification-token.dao.ts +48 -80
  107. package/src/dao/video/video.dao.ts +345 -396
  108. package/src/entities/BaseEntity.ts +1 -1
  109. package/src/index.ts +30 -43
  110. package/src/interfaces/auth/auth.interfaces.ts +10 -10
  111. package/src/interfaces/batch/batch.interfaces.ts +1 -1
  112. package/src/interfaces/camera/camera.interfaces.ts +9 -9
  113. package/src/interfaces/chat/chat.interfaces.ts +4 -4
  114. package/src/interfaces/folder/folder.interfaces.ts +2 -2
  115. package/src/interfaces/location/location.interfaces.ts +7 -7
  116. package/src/interfaces/message/message.interfaces.ts +3 -3
  117. package/src/interfaces/report-configuration/report-configuration.interfaces.ts +17 -16
  118. package/src/interfaces/study/study.interfaces.ts +3 -10
  119. package/src/interfaces/user/user.interfaces.ts +9 -9
  120. package/src/interfaces/user-push-notification-token/user-push-notification-token.interfaces.ts +9 -9
  121. package/src/interfaces/video/video.interfaces.ts +34 -34
@@ -1,10 +1,6 @@
1
1
  import { Knex } from "knex";
2
2
  import { IBaseDAO, IDataPaginator } from "../../d.types";
3
- import {
4
- IChat,
5
- IChatCreate,
6
- IChatUpdate,
7
- } from "../../interfaces/chat/chat.interfaces";
3
+ import { IChat, IChatCreate, IChatUpdate } from '../../interfaces/chat/chat.interfaces';
8
4
  import KnexManager from "../../KnexConnection";
9
5
 
10
6
  export interface IChatPaginatorResponse extends IDataPaginator<IChat> {
@@ -15,17 +11,23 @@ export class ChatDAO implements IBaseDAO<IChat> {
15
11
  private _knex: Knex<any, unknown[]> = KnexManager.getConnection();
16
12
 
17
13
  async create(item: IChatCreate): Promise<IChat> {
18
- const [result] = await this._knex("chat").insert(item).returning("*");
14
+ const [result] = await this._knex('chat')
15
+ .insert(item)
16
+ .returning('*');
19
17
  return result;
20
18
  }
21
19
 
22
20
  async getById(id: number): Promise<IChat | null> {
23
- const result = await this._knex("chat").where("id", id).first();
21
+ const result = await this._knex('chat')
22
+ .where('id', id)
23
+ .first();
24
24
  return result || null;
25
25
  }
26
26
 
27
27
  async getByUuid(uuid: string): Promise<IChat | null> {
28
- const result = await this._knex("chat").where("uuid", uuid).first();
28
+ const result = await this._knex('chat')
29
+ .where('uuid', uuid)
30
+ .first();
29
31
  return result || null;
30
32
  }
31
33
 
@@ -33,11 +35,11 @@ export class ChatDAO implements IBaseDAO<IChat> {
33
35
  const offset = (page - 1) * limit;
34
36
 
35
37
  const [results, [{ count }]] = await Promise.all([
36
- this._knex("chat")
37
- .orderBy("created_at", "desc")
38
+ this._knex('chat')
39
+ .orderBy('created_at', 'desc')
38
40
  .limit(limit)
39
41
  .offset(offset),
40
- this._knex("chat").count("* as count"),
42
+ this._knex('chat').count('* as count')
41
43
  ]);
42
44
 
43
45
  const totalCount = parseInt(count as string);
@@ -48,40 +50,40 @@ export class ChatDAO implements IBaseDAO<IChat> {
48
50
  limit,
49
51
  count: results.length,
50
52
  totalCount,
51
- totalPages: Math.ceil(totalCount / limit),
53
+ totalPages: Math.ceil(totalCount / limit)
52
54
  };
53
55
  }
54
56
 
55
57
  async update(id: number, item: IChatUpdate): Promise<IChat | null> {
56
- const [result] = await this._knex("chat")
57
- .where("id", id)
58
+ const [result] = await this._knex('chat')
59
+ .where('id', id)
58
60
  .update({
59
61
  ...item,
60
- updated_at: new Date(),
62
+ updated_at: new Date()
61
63
  })
62
- .returning("*");
64
+ .returning('*');
63
65
  return result || null;
64
66
  }
65
67
 
66
68
  async delete(id: number): Promise<boolean> {
67
- const result = await this._knex("chat").where("id", id).delete();
69
+ const result = await this._knex('chat')
70
+ .where('id', id)
71
+ .delete();
68
72
  return result > 0;
69
73
  }
70
74
 
71
- async getByUserId(
72
- userId: number,
73
- page = 1,
74
- limit = 10,
75
- ): Promise<IDataPaginator<IChat>> {
75
+ async getByUserId(userId: number, page = 1, limit = 10): Promise<IDataPaginator<IChat>> {
76
76
  const offset = (page - 1) * limit;
77
77
 
78
78
  const [results, [{ count }]] = await Promise.all([
79
- this._knex("chat")
80
- .where("userId", userId)
81
- .orderBy("created_at", "desc")
79
+ this._knex('chat')
80
+ .where('userId', userId)
81
+ .orderBy('created_at', 'desc')
82
82
  .limit(limit)
83
83
  .offset(offset),
84
- this._knex("chat").where("userId", userId).count("* as count"),
84
+ this._knex('chat')
85
+ .where('userId', userId)
86
+ .count('* as count')
85
87
  ]);
86
88
 
87
89
  const totalCount = parseInt(count as string);
@@ -92,38 +94,36 @@ export class ChatDAO implements IBaseDAO<IChat> {
92
94
  limit,
93
95
  count: results.length,
94
96
  totalCount,
95
- totalPages: Math.ceil(totalCount / limit),
97
+ totalPages: Math.ceil(totalCount / limit)
96
98
  };
97
99
  }
98
100
 
99
- async getActiveByUserId(
100
- userId: number,
101
- page = 1,
102
- limit = 10,
103
- query?: string,
104
- ): Promise<IChatPaginatorResponse> {
101
+ async getActiveByUserId(userId: number, page = 1, limit = 10, query?: string): Promise<IChatPaginatorResponse> {
105
102
  const offset = (page - 1) * limit;
106
103
 
107
104
  // Build data query
108
- let dataQuery = this._knex("chat")
109
- .where("userId", userId)
110
- .where("status", "active");
105
+ let dataQuery = this._knex('chat')
106
+ .where('userId', userId)
107
+ .where('status', 'active');
111
108
 
112
109
  // Build count query
113
- let countQuery = this._knex("chat")
114
- .where("userId", userId)
115
- .where("status", "active");
110
+ let countQuery = this._knex('chat')
111
+ .where('userId', userId)
112
+ .where('status', 'active');
116
113
 
117
114
  // Apply search filter if query is provided
118
115
  if (query && query.trim()) {
119
116
  const searchTerm = `%${query.trim()}%`;
120
- dataQuery = dataQuery.where("title", "ILIKE", searchTerm);
121
- countQuery = countQuery.where("title", "ILIKE", searchTerm);
117
+ dataQuery = dataQuery.where('title', 'ILIKE', searchTerm);
118
+ countQuery = countQuery.where('title', 'ILIKE', searchTerm);
122
119
  }
123
120
 
124
121
  const [results, [{ count }]] = await Promise.all([
125
- dataQuery.orderBy("created_at", "desc").limit(limit).offset(offset),
126
- countQuery.count("* as count"),
122
+ dataQuery
123
+ .orderBy('created_at', 'desc')
124
+ .limit(limit)
125
+ .offset(offset),
126
+ countQuery.count('* as count')
127
127
  ]);
128
128
 
129
129
  const totalCount = parseInt(count as string);
@@ -138,7 +138,7 @@ export class ChatDAO implements IBaseDAO<IChat> {
138
138
  count: results.length,
139
139
  totalCount,
140
140
  totalPages,
141
- hasMore,
141
+ hasMore
142
142
  };
143
143
  }
144
- }
144
+ }
@@ -2,128 +2,113 @@ import { Knex } from "knex";
2
2
  import { IBaseDAO, IDataPaginator } from "../../d.types";
3
3
  import { IFolder } from "../../interfaces/folder/folder.interfaces";
4
4
  import KnexManager from "../../KnexConnection";
5
- import {
6
- FolderSortField,
7
- FolderStatus,
8
- FOLDER_SORT_COLUMN_MAP,
9
- } from "../../constants/folder.constants";
5
+ import { FolderSortField, FolderStatus, FOLDER_SORT_COLUMN_MAP } from "../../constants/folder.constants";
10
6
  import { SortOrder } from "../../constants/video.constants";
11
7
 
12
8
  export interface IFolderFilters {
13
- studyId?: number | null;
14
- status?: FolderStatus;
15
- search?: string;
16
- sortBy?: FolderSortField;
17
- sortOrder?: SortOrder;
9
+ studyId?: number | null;
10
+ status?: FolderStatus;
11
+ search?: string;
12
+ sortBy?: FolderSortField;
13
+ sortOrder?: SortOrder;
18
14
  }
19
15
 
20
16
  export class FolderDAO implements IBaseDAO<IFolder> {
21
- private _knex: Knex<any, unknown[]> = KnexManager.getConnection();
17
+ private _knex: Knex<any, unknown[]> = KnexManager.getConnection();
22
18
 
23
- async create(item: IFolder): Promise<IFolder> {
24
- const [createdFolder] = await this._knex("folders")
25
- .insert(item)
26
- .returning("*");
27
- return createdFolder;
28
- }
19
+ async create(item: IFolder): Promise<IFolder> {
20
+ const [createdFolder] = await this._knex("folders").insert(item).returning("*");
21
+ return createdFolder;
22
+ }
29
23
 
30
- async getById(id: number): Promise<IFolder | null> {
31
- const folder = await this._knex("folders as f")
32
- .innerJoin("study as s", "f.studyId", "s.id")
33
- .select("f.*", this._knex.raw("to_jsonb(s.*) as study"))
34
- .where("f.id", id)
35
- .first();
36
- return folder || null;
37
- }
24
+ async getById(id: number): Promise<IFolder | null> {
25
+ const folder = await this._knex("folders as f")
26
+ .innerJoin("study as s", "f.studyId", "s.id")
27
+ .select("f.*", this._knex.raw("to_jsonb(s.*) as study"))
28
+ .where("f.id", id)
29
+ .first();
30
+ return folder || null;
31
+ }
38
32
 
39
- async getByUuid(uuid: string): Promise<IFolder | null> {
40
- const folder = await this._knex("folders as f")
41
- .innerJoin("study as s", "f.studyId", "s.id")
42
- .select("f.*", this._knex.raw("to_jsonb(s.*) as study"))
43
- .where("f.uuid", uuid)
44
- .first();
45
- return folder || null;
46
- }
33
+ async getByUuid(uuid: string): Promise<IFolder | null> {
34
+ const folder = await this._knex("folders as f")
35
+ .innerJoin("study as s", "f.studyId", "s.id")
36
+ .select("f.*", this._knex.raw("to_jsonb(s.*) as study"))
37
+ .where("f.uuid", uuid)
38
+ .first();
39
+ return folder || null;
40
+ }
47
41
 
48
- async update(id: number, item: Partial<IFolder>): Promise<IFolder | null> {
49
- const [updatedFolder] = await this._knex("folders")
50
- .where({ id })
51
- .update(item)
52
- .returning("*");
53
- return updatedFolder || null;
54
- }
42
+ async update(id: number, item: Partial<IFolder>): Promise<IFolder | null> {
43
+ const [updatedFolder] = await this._knex("folders").where({ id }).update(item).returning("*");
44
+ return updatedFolder || null;
45
+ }
55
46
 
56
- async delete(id: number): Promise<boolean> {
57
- const result = await this._knex("folders").where({ id }).del();
58
- return result > 0;
59
- }
47
+ async delete(id: number): Promise<boolean> {
48
+ const result = await this._knex("folders").where({ id }).del();
49
+ return result > 0;
50
+ }
60
51
 
61
- async getAll(
62
- page: number,
63
- limit: number,
64
- filters?: IFolderFilters,
65
- ): Promise<IDataPaginator<IFolder>> {
66
- const offset = (page - 1) * limit;
52
+ async getAll(
53
+ page: number,
54
+ limit: number,
55
+ filters?: IFolderFilters
56
+ ): Promise<IDataPaginator<IFolder>> {
57
+ const offset = (page - 1) * limit;
67
58
 
68
- const query = this._knex("folders as f")
69
- .innerJoin("study as s", "f.studyId", "s.id")
70
- .leftJoin("locations as l", "s.locationId", "l.id")
71
- .select(
72
- "f.*",
73
- this._knex.raw("to_jsonb(s.*) as study"),
74
- "l.name as locationName",
75
- "l.uuid as locationUuid",
76
- );
59
+ const query = this._knex("folders as f")
60
+ .innerJoin("study as s", "f.studyId", "s.id")
61
+ .leftJoin("locations as l", "s.locationId", "l.id")
62
+ .select(
63
+ "f.*",
64
+ this._knex.raw("to_jsonb(s.*) as study"),
65
+ "l.name as locationName",
66
+ "l.uuid as locationUuid"
67
+ );
77
68
 
78
- // Apply filters
79
- if (filters) {
80
- // Filter by studyId
81
- if (filters.studyId !== undefined && filters.studyId !== null) {
82
- query.where("f.studyId", filters.studyId);
83
- }
69
+ // Apply filters
70
+ if (filters) {
71
+ // Filter by studyId
72
+ if (filters.studyId !== undefined && filters.studyId !== null) {
73
+ query.where("f.studyId", filters.studyId);
74
+ }
84
75
 
85
- // Filter by status
86
- if (filters.status) {
87
- query.where("f.status", filters.status);
88
- }
76
+ // Filter by status
77
+ if (filters.status) {
78
+ query.where("f.status", filters.status);
79
+ }
89
80
 
90
- // Search by name (case-insensitive)
91
- if (filters.search) {
92
- query.whereRaw("LOWER(f.name) LIKE ?", [
93
- `%${filters.search.toLowerCase()}%`,
94
- ]);
95
- }
81
+ // Search by name (case-insensitive)
82
+ if (filters.search) {
83
+ query.whereRaw("LOWER(f.name) LIKE ?", [`%${filters.search.toLowerCase()}%`]);
84
+ }
96
85
 
97
- // Apply sorting
98
- if (filters.sortBy) {
99
- const columnName = FOLDER_SORT_COLUMN_MAP[filters.sortBy];
100
- const order = filters.sortOrder || "DESC";
101
- query.orderBy(columnName, order);
102
- } else {
103
- // Default sort by created_at DESC
104
- query.orderBy("f.created_at", "DESC");
105
- }
106
- } else {
107
- // Default sort by created_at DESC
108
- query.orderBy("f.created_at", "DESC");
109
- }
86
+ // Apply sorting
87
+ if (filters.sortBy) {
88
+ const columnName = FOLDER_SORT_COLUMN_MAP[filters.sortBy];
89
+ const order = filters.sortOrder || 'DESC';
90
+ query.orderBy(columnName, order);
91
+ } else {
92
+ // Default sort by created_at DESC
93
+ query.orderBy('f.created_at', 'DESC');
94
+ }
95
+ } else {
96
+ // Default sort by created_at DESC
97
+ query.orderBy('f.created_at', 'DESC');
98
+ }
110
99
 
111
- const [countResult] = await query
112
- .clone()
113
- .clearSelect()
114
- .clearOrder()
115
- .count("* as count");
116
- const totalCount = +countResult.count;
117
- const folders = await query.clone().limit(limit).offset(offset);
100
+ const [countResult] = await query.clone().clearSelect().clearOrder().count("* as count");
101
+ const totalCount = +countResult.count;
102
+ const folders = await query.clone().limit(limit).offset(offset);
118
103
 
119
- return {
120
- success: true,
121
- data: folders,
122
- page,
123
- limit,
124
- count: folders.length,
125
- totalCount,
126
- totalPages: Math.ceil(totalCount / limit),
127
- };
128
- }
104
+ return {
105
+ success: true,
106
+ data: folders,
107
+ page,
108
+ limit,
109
+ count: folders.length,
110
+ totalCount,
111
+ totalPages: Math.ceil(totalCount / limit),
112
+ };
113
+ }
129
114
  }
@@ -5,119 +5,97 @@ import { IVideo } from "../../interfaces/video/video.interfaces";
5
5
  import KnexManager from "../../KnexConnection";
6
6
 
7
7
  export class LocationDAO implements IBaseDAO<ILocation> {
8
- private _knex: Knex<any, unknown[]> = KnexManager.getConnection();
9
-
10
- async create(item: ILocation): Promise<ILocation> {
11
- const [createdLocation] = await this._knex("locations")
12
- .insert(item)
13
- .returning("*");
14
- return createdLocation;
15
- }
16
-
17
- async getById(id: number): Promise<ILocation | null> {
18
- const location = await this._knex("locations").where({ id }).first();
19
- return location || null;
20
- }
21
-
22
- async getByUuid(uuid: string): Promise<ILocation | null> {
23
- const location = await this._knex("locations").where({ uuid }).first();
24
- return location || null;
25
- }
26
-
27
- async update(
28
- id: number,
29
- item: Partial<ILocation>,
30
- ): Promise<ILocation | null> {
31
- const [updatedLocation] = await this._knex("locations")
32
- .where({ id })
33
- .update(item)
34
- .returning("*");
35
- return updatedLocation || null;
36
- }
37
-
38
- async delete(id: number): Promise<boolean> {
39
- const result = await this._knex("locations").where({ id }).del();
40
- return result > 0;
41
- }
42
-
43
- async getAll(
44
- page: number,
45
- limit: number,
46
- ): Promise<IDataPaginator<ILocation>> {
47
- const offset = (page - 1) * limit;
48
-
49
- const [countResult] = await this._knex("locations").count("* as count");
50
- const totalCount = +countResult.count;
51
- const locations = await this._knex("locations").limit(limit).offset(offset);
52
-
53
- return {
54
- success: true,
55
- data: locations,
56
- page,
57
- limit,
58
- count: locations.length,
59
- totalCount,
60
- totalPages: Math.ceil(totalCount / limit),
61
- };
62
- }
63
-
64
- async getByName(name: string): Promise<ILocation | null> {
65
- const location = await this._knex("locations").where({ name }).first();
66
- return location || null;
67
- }
68
-
69
- async getLocationsNearCoordinates(
70
- longitude: number,
71
- latitude: number,
72
- radiusKm: number = 1,
73
- ): Promise<ILocation[]> {
74
- // Using ST_DWithin for geographic distance calculation
75
- // This is a PostgreSQL-specific query for geospatial operations
76
- const locations = await this._knex("locations").whereRaw(
77
- `ST_DWithin(
8
+ private _knex: Knex<any, unknown[]> = KnexManager.getConnection();
9
+
10
+ async create(item: ILocation): Promise<ILocation> {
11
+ const [createdLocation] = await this._knex("locations").insert(item).returning("*");
12
+ return createdLocation;
13
+ }
14
+
15
+ async getById(id: number): Promise<ILocation | null> {
16
+ const location = await this._knex("locations").where({ id }).first();
17
+ return location || null;
18
+ }
19
+
20
+ async getByUuid(uuid: string): Promise<ILocation | null> {
21
+ const location = await this._knex("locations").where({ uuid }).first();
22
+ return location || null;
23
+ }
24
+
25
+ async update(id: number, item: Partial<ILocation>): Promise<ILocation | null> {
26
+ const [updatedLocation] = await this._knex("locations").where({ id }).update(item).returning("*");
27
+ return updatedLocation || null;
28
+ }
29
+
30
+ async delete(id: number): Promise<boolean> {
31
+ const result = await this._knex("locations").where({ id }).del();
32
+ return result > 0;
33
+ }
34
+
35
+ async getAll(page: number, limit: number): Promise<IDataPaginator<ILocation>> {
36
+ const offset = (page - 1) * limit;
37
+
38
+ const [countResult] = await this._knex("locations").count("* as count");
39
+ const totalCount = +countResult.count;
40
+ const locations = await this._knex("locations").limit(limit).offset(offset);
41
+
42
+ return {
43
+ success: true,
44
+ data: locations,
45
+ page,
46
+ limit,
47
+ count: locations.length,
48
+ totalCount,
49
+ totalPages: Math.ceil(totalCount / limit),
50
+ };
51
+ }
52
+
53
+ async getByName(name: string): Promise<ILocation | null> {
54
+ const location = await this._knex("locations").where({ name }).first();
55
+ return location || null;
56
+ }
57
+
58
+ async getLocationsNearCoordinates(longitude: number, latitude: number, radiusKm: number = 1): Promise<ILocation[]> {
59
+ // Using ST_DWithin for geographic distance calculation
60
+ // This is a PostgreSQL-specific query for geospatial operations
61
+ const locations = await this._knex("locations")
62
+ .whereRaw(
63
+ `ST_DWithin(
78
64
  ST_MakePoint(longitude, latitude)::geography,
79
65
  ST_MakePoint(?, ?)::geography,
80
66
  ?
81
67
  )`,
82
- [longitude, latitude, radiusKm * 1000], // Convert km to meters
83
- );
84
- return locations;
85
- }
86
-
87
- /**
88
- * Get all locations with optional search filter by name (case-insensitive partial match)
89
- */
90
- async getAllWithSearch(
91
- page: number,
92
- limit: number,
93
- name?: string,
94
- ): Promise<IDataPaginator<ILocation>> {
95
- const offset = (page - 1) * limit;
96
-
97
- let query = this._knex("locations");
98
-
99
- // Apply search filter if name provided (escape special chars to prevent pattern injection)
100
- if (name && name.trim().length > 0) {
101
- const escapedName = name.trim().replace(/[%_\\]/g, "\\$&");
102
- query = query.where("name", "ilike", `%${escapedName}%`);
68
+ [longitude, latitude, radiusKm * 1000] // Convert km to meters
69
+ );
70
+ return locations;
103
71
  }
104
72
 
105
- const [countResult] = await query.clone().count("* as count");
106
- const totalCount = +countResult.count;
107
- const locations = await query
108
- .clone()
109
- .limit(limit)
110
- .offset(offset)
111
- .orderBy("name", "asc");
112
-
113
- return {
114
- success: true,
115
- data: locations,
116
- page,
117
- limit,
118
- count: locations.length,
119
- totalCount,
120
- totalPages: Math.ceil(totalCount / limit),
121
- };
122
- }
73
+ /**
74
+ * Get all locations with optional search filter by name (case-insensitive partial match)
75
+ */
76
+ async getAllWithSearch(page: number, limit: number, name?: string): Promise<IDataPaginator<ILocation>> {
77
+ const offset = (page - 1) * limit;
78
+
79
+ let query = this._knex("locations");
80
+
81
+ // Apply search filter if name provided (escape special chars to prevent pattern injection)
82
+ if (name && name.trim().length > 0) {
83
+ const escapedName = name.trim().replace(/[%_\\]/g, '\\$&');
84
+ query = query.where('name', 'ilike', `%${escapedName}%`);
85
+ }
86
+
87
+ const [countResult] = await query.clone().count("* as count");
88
+ const totalCount = +countResult.count;
89
+ const locations = await query.clone().limit(limit).offset(offset).orderBy('name', 'asc');
90
+
91
+ return {
92
+ success: true,
93
+ data: locations,
94
+ page,
95
+ limit,
96
+ count: locations.length,
97
+ totalCount,
98
+ totalPages: Math.ceil(totalCount / limit),
99
+ };
100
+ }
123
101
  }