@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.
- package/.claude/settings.local.json +5 -2
- package/.env.prod +5 -0
- package/CLAUDE.md +2 -11
- package/dist/constants/video.constants.d.ts +12 -0
- package/dist/constants/video.constants.js +18 -0
- package/dist/constants/video.constants.js.map +1 -0
- package/dist/dao/VideoMinuteResultDAO.d.ts +4 -1
- package/dist/dao/VideoMinuteResultDAO.js +28 -31
- package/dist/dao/VideoMinuteResultDAO.js.map +1 -1
- package/dist/dao/auth/auth.dao.js +1 -4
- package/dist/dao/auth/auth.dao.js.map +1 -1
- package/dist/dao/batch/batch.dao.js +14 -13
- package/dist/dao/batch/batch.dao.js.map +1 -1
- package/dist/dao/camera/camera.dao.d.ts +17 -7
- package/dist/dao/camera/camera.dao.js +38 -56
- package/dist/dao/camera/camera.dao.js.map +1 -1
- package/dist/dao/chat/chat.dao.d.ts +1 -1
- package/dist/dao/chat/chat.dao.js +35 -25
- package/dist/dao/chat/chat.dao.js.map +1 -1
- package/dist/dao/folder/folder.dao.js +4 -8
- package/dist/dao/folder/folder.dao.js.map +1 -1
- package/dist/dao/location/location.dao.d.ts +17 -0
- package/dist/dao/location/location.dao.js +116 -0
- package/dist/dao/location/location.dao.js.map +1 -0
- package/dist/dao/message/message.dao.d.ts +1 -1
- package/dist/dao/message/message.dao.js +26 -18
- package/dist/dao/message/message.dao.js.map +1 -1
- package/dist/dao/report-configuration/report-configuration.dao.js +31 -32
- package/dist/dao/report-configuration/report-configuration.dao.js.map +1 -1
- package/dist/dao/study/study.dao.d.ts +1 -1
- package/dist/dao/study/study.dao.js +12 -17
- package/dist/dao/study/study.dao.js.map +1 -1
- package/dist/dao/user/user.dao.js +1 -4
- package/dist/dao/user/user.dao.js.map +1 -1
- package/dist/dao/user-push-notification-token/user-push-notification-token.dao.js +8 -26
- package/dist/dao/user-push-notification-token/user-push-notification-token.dao.js.map +1 -1
- package/dist/dao/video/video.dao.d.ts +9 -1
- package/dist/dao/video/video.dao.js +73 -27
- package/dist/dao/video/video.dao.js.map +1 -1
- package/dist/index.d.ts +8 -4
- package/dist/index.js +8 -1
- package/dist/index.js.map +1 -1
- package/dist/interfaces/batch/batch.interfaces.d.ts +1 -1
- package/dist/interfaces/camera/camera.interfaces.d.ts +4 -2
- package/dist/interfaces/chat/chat.interfaces.d.ts +3 -3
- package/dist/interfaces/folder/folder.interfaces.d.ts +1 -1
- package/dist/interfaces/location/location.interfaces.d.ts +9 -0
- package/dist/interfaces/location/location.interfaces.js +3 -0
- package/dist/interfaces/location/location.interfaces.js.map +1 -0
- package/dist/interfaces/message/message.interfaces.d.ts +2 -2
- package/dist/interfaces/study/study.interfaces.d.ts +6 -5
- package/dist/interfaces/user/user.interfaces.d.ts +1 -1
- package/dist/interfaces/user-push-notification-token/user-push-notification-token.interfaces.d.ts +1 -1
- package/dist/interfaces/video/video.interfaces.d.ts +3 -2
- package/migrations/20250717160737_migration.ts +1 -1
- package/migrations/20250717160908_migration.ts +2 -5
- package/migrations/20250717161310_migration.ts +1 -1
- package/migrations/20250717161406_migration.ts +3 -3
- package/migrations/20250717162431_migration.ts +1 -1
- package/migrations/20250717173228_migration.ts +2 -2
- package/migrations/20250717204731_migration.ts +1 -1
- package/migrations/20250722210109_migration.ts +4 -8
- package/migrations/20250722211019_migration.ts +1 -1
- package/migrations/20250723153852_migration.ts +10 -13
- package/migrations/20250723162257_migration.ts +7 -4
- package/migrations/20250723171109_migration.ts +7 -4
- package/migrations/20250723205331_migration.ts +9 -6
- package/migrations/20250724191345_migration.ts +11 -8
- package/migrations/20250730180932_migration.ts +13 -14
- package/migrations/20250730213625_migration.ts +11 -8
- package/migrations/20250804124509_migration.ts +21 -26
- package/migrations/20250804132053_migration.ts +8 -5
- package/migrations/20250804164518_migration.ts +7 -7
- package/migrations/20250823223016_migration.ts +21 -32
- package/migrations/20250910015452_migration.ts +6 -18
- package/migrations/20250911000000_migration.ts +4 -18
- package/migrations/20250917144153_migration.ts +7 -14
- package/migrations/20250930200521_migration.ts +4 -8
- package/migrations/20251010143500_migration.ts +6 -27
- package/migrations/20251020225758_migration.ts +15 -51
- package/migrations/20251112120000_migration.ts +81 -0
- package/migrations/20251112120100_migration.ts +21 -0
- package/migrations/20251112120200_migration.ts +38 -0
- package/migrations/20251112120300_migration.ts +22 -0
- package/package.json +1 -1
- package/src/constants/video.constants.ts +19 -0
- package/src/d.types.ts +14 -18
- package/src/dao/VideoMinuteResultDAO.ts +54 -72
- package/src/dao/auth/auth.dao.ts +55 -58
- package/src/dao/batch/batch.dao.ts +98 -101
- package/src/dao/camera/camera.dao.ts +125 -145
- package/src/dao/chat/chat.dao.ts +43 -45
- package/src/dao/folder/folder.dao.ts +56 -59
- package/src/dao/location/location.dao.ts +101 -0
- package/src/dao/message/message.dao.ts +32 -32
- package/src/dao/report-configuration/report-configuration.dao.ts +342 -370
- package/src/dao/study/study.dao.ts +63 -88
- package/src/dao/user/user.dao.ts +50 -52
- package/src/dao/user-push-notification-token/user-push-notification-token.dao.ts +49 -83
- package/src/dao/video/video.dao.ts +339 -313
- package/src/entities/BaseEntity.ts +1 -1
- package/src/index.ts +24 -26
- package/src/interfaces/auth/auth.interfaces.ts +10 -10
- package/src/interfaces/batch/batch.interfaces.ts +1 -1
- package/src/interfaces/camera/camera.interfaces.ts +9 -7
- package/src/interfaces/chat/chat.interfaces.ts +4 -4
- package/src/interfaces/folder/folder.interfaces.ts +2 -2
- package/src/interfaces/location/location.interfaces.ts +9 -0
- package/src/interfaces/message/message.interfaces.ts +3 -3
- package/src/interfaces/report-configuration/report-configuration.interfaces.ts +16 -16
- package/src/interfaces/study/study.interfaces.ts +7 -6
- package/src/interfaces/user/user.interfaces.ts +9 -9
- package/src/interfaces/user-push-notification-token/user-push-notification-token.interfaces.ts +9 -9
- package/src/interfaces/video/video.interfaces.ts +34 -33
- package/plan.md +0 -684
package/src/dao/chat/chat.dao.ts
CHANGED
|
@@ -1,39 +1,41 @@
|
|
|
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 class ChatDAO implements IBaseDAO<IChat> {
|
|
11
7
|
private _knex: Knex<any, unknown[]> = KnexManager.getConnection();
|
|
12
8
|
|
|
13
9
|
async create(item: IChatCreate): Promise<IChat> {
|
|
14
|
-
const [result] = await this._knex(
|
|
10
|
+
const [result] = await this._knex('chat')
|
|
11
|
+
.insert(item)
|
|
12
|
+
.returning('*');
|
|
15
13
|
return result;
|
|
16
14
|
}
|
|
17
15
|
|
|
18
16
|
async getById(id: number): Promise<IChat | null> {
|
|
19
|
-
const result = await this._knex(
|
|
17
|
+
const result = await this._knex('chat')
|
|
18
|
+
.where('id', id)
|
|
19
|
+
.first();
|
|
20
20
|
return result || null;
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
async getByUuid(uuid: string): Promise<IChat | null> {
|
|
24
|
-
const result = await this._knex(
|
|
24
|
+
const result = await this._knex('chat')
|
|
25
|
+
.where('uuid', uuid)
|
|
26
|
+
.first();
|
|
25
27
|
return result || null;
|
|
26
28
|
}
|
|
27
29
|
|
|
28
30
|
async getAll(page = 1, limit = 10): Promise<IDataPaginator<IChat>> {
|
|
29
31
|
const offset = (page - 1) * limit;
|
|
30
|
-
|
|
32
|
+
|
|
31
33
|
const [results, [{ count }]] = await Promise.all([
|
|
32
|
-
this._knex(
|
|
33
|
-
.orderBy(
|
|
34
|
+
this._knex('chat')
|
|
35
|
+
.orderBy('created_at', 'desc')
|
|
34
36
|
.limit(limit)
|
|
35
37
|
.offset(offset),
|
|
36
|
-
this._knex(
|
|
38
|
+
this._knex('chat').count('* as count')
|
|
37
39
|
]);
|
|
38
40
|
|
|
39
41
|
const totalCount = parseInt(count as string);
|
|
@@ -44,40 +46,40 @@ export class ChatDAO implements IBaseDAO<IChat> {
|
|
|
44
46
|
limit,
|
|
45
47
|
count: results.length,
|
|
46
48
|
totalCount,
|
|
47
|
-
totalPages: Math.ceil(totalCount / limit)
|
|
49
|
+
totalPages: Math.ceil(totalCount / limit)
|
|
48
50
|
};
|
|
49
51
|
}
|
|
50
52
|
|
|
51
53
|
async update(id: number, item: IChatUpdate): Promise<IChat | null> {
|
|
52
|
-
const [result] = await this._knex(
|
|
53
|
-
.where(
|
|
54
|
+
const [result] = await this._knex('chat')
|
|
55
|
+
.where('id', id)
|
|
54
56
|
.update({
|
|
55
57
|
...item,
|
|
56
|
-
updated_at: new Date()
|
|
58
|
+
updated_at: new Date()
|
|
57
59
|
})
|
|
58
|
-
.returning(
|
|
60
|
+
.returning('*');
|
|
59
61
|
return result || null;
|
|
60
62
|
}
|
|
61
63
|
|
|
62
64
|
async delete(id: number): Promise<boolean> {
|
|
63
|
-
const result = await this._knex(
|
|
65
|
+
const result = await this._knex('chat')
|
|
66
|
+
.where('id', id)
|
|
67
|
+
.delete();
|
|
64
68
|
return result > 0;
|
|
65
69
|
}
|
|
66
70
|
|
|
67
|
-
async getByUserId(
|
|
68
|
-
userId: number,
|
|
69
|
-
page = 1,
|
|
70
|
-
limit = 10,
|
|
71
|
-
): Promise<IDataPaginator<IChat>> {
|
|
71
|
+
async getByUserId(userId: number, page = 1, limit = 10): Promise<IDataPaginator<IChat>> {
|
|
72
72
|
const offset = (page - 1) * limit;
|
|
73
|
-
|
|
73
|
+
|
|
74
74
|
const [results, [{ count }]] = await Promise.all([
|
|
75
|
-
this._knex(
|
|
76
|
-
.where(
|
|
77
|
-
.orderBy(
|
|
75
|
+
this._knex('chat')
|
|
76
|
+
.where('userId', userId)
|
|
77
|
+
.orderBy('created_at', 'desc')
|
|
78
78
|
.limit(limit)
|
|
79
79
|
.offset(offset),
|
|
80
|
-
this._knex(
|
|
80
|
+
this._knex('chat')
|
|
81
|
+
.where('userId', userId)
|
|
82
|
+
.count('* as count')
|
|
81
83
|
]);
|
|
82
84
|
|
|
83
85
|
const totalCount = parseInt(count as string);
|
|
@@ -88,28 +90,24 @@ export class ChatDAO implements IBaseDAO<IChat> {
|
|
|
88
90
|
limit,
|
|
89
91
|
count: results.length,
|
|
90
92
|
totalCount,
|
|
91
|
-
totalPages: Math.ceil(totalCount / limit)
|
|
93
|
+
totalPages: Math.ceil(totalCount / limit)
|
|
92
94
|
};
|
|
93
95
|
}
|
|
94
96
|
|
|
95
|
-
async getActiveByUserId(
|
|
96
|
-
userId: number,
|
|
97
|
-
page = 1,
|
|
98
|
-
limit = 10,
|
|
99
|
-
): Promise<IDataPaginator<IChat>> {
|
|
97
|
+
async getActiveByUserId(userId: number, page = 1, limit = 10): Promise<IDataPaginator<IChat>> {
|
|
100
98
|
const offset = (page - 1) * limit;
|
|
101
|
-
|
|
99
|
+
|
|
102
100
|
const [results, [{ count }]] = await Promise.all([
|
|
103
|
-
this._knex(
|
|
104
|
-
.where(
|
|
105
|
-
.where(
|
|
106
|
-
.orderBy(
|
|
101
|
+
this._knex('chat')
|
|
102
|
+
.where('userId', userId)
|
|
103
|
+
.where('status', 'active')
|
|
104
|
+
.orderBy('created_at', 'desc')
|
|
107
105
|
.limit(limit)
|
|
108
106
|
.offset(offset),
|
|
109
|
-
this._knex(
|
|
110
|
-
.where(
|
|
111
|
-
.where(
|
|
112
|
-
.count(
|
|
107
|
+
this._knex('chat')
|
|
108
|
+
.where('userId', userId)
|
|
109
|
+
.where('status', 'active')
|
|
110
|
+
.count('* as count')
|
|
113
111
|
]);
|
|
114
112
|
|
|
115
113
|
const totalCount = parseInt(count as string);
|
|
@@ -120,7 +118,7 @@ export class ChatDAO implements IBaseDAO<IChat> {
|
|
|
120
118
|
limit,
|
|
121
119
|
count: results.length,
|
|
122
120
|
totalCount,
|
|
123
|
-
totalPages: Math.ceil(totalCount / limit)
|
|
121
|
+
totalPages: Math.ceil(totalCount / limit)
|
|
124
122
|
};
|
|
125
123
|
}
|
|
126
|
-
}
|
|
124
|
+
}
|
|
@@ -4,72 +4,69 @@ import { IFolder } from "../../interfaces/folder/folder.interfaces";
|
|
|
4
4
|
import KnexManager from "../../KnexConnection";
|
|
5
5
|
|
|
6
6
|
export class FolderDAO implements IBaseDAO<IFolder> {
|
|
7
|
-
|
|
7
|
+
private _knex: Knex<any, unknown[]> = KnexManager.getConnection();
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
return createdFolder;
|
|
14
|
-
}
|
|
9
|
+
async create(item: IFolder): Promise<IFolder> {
|
|
10
|
+
const [createdFolder] = await this._knex("folders").insert(item).returning("*");
|
|
11
|
+
return createdFolder;
|
|
12
|
+
}
|
|
15
13
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
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
|
+
}
|
|
24
22
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
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
|
+
}
|
|
33
31
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
.returning("*");
|
|
39
|
-
return updatedFolder || null;
|
|
40
|
-
}
|
|
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
|
+
}
|
|
41
36
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
37
|
+
async delete(id: number): Promise<boolean> {
|
|
38
|
+
const result = await this._knex("folders").where({ id }).del();
|
|
39
|
+
return result > 0;
|
|
40
|
+
}
|
|
46
41
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
limit: number,
|
|
50
|
-
studyId?: number | null,
|
|
51
|
-
): Promise<IDataPaginator<IFolder>> {
|
|
52
|
-
const offset = (page - 1) * limit;
|
|
42
|
+
async getAll(page: number, limit: number, studyId?: number | null): Promise<IDataPaginator<IFolder>> {
|
|
43
|
+
const offset = (page - 1) * limit;
|
|
53
44
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
45
|
+
const query = this._knex("folders as f")
|
|
46
|
+
.innerJoin("study as s", "f.studyId", "s.id")
|
|
47
|
+
.leftJoin("locations as l", "s.locationId", "l.id")
|
|
48
|
+
.select(
|
|
49
|
+
"f.*",
|
|
50
|
+
this._knex.raw("to_jsonb(s.*) as study"),
|
|
51
|
+
"l.name as locationName",
|
|
52
|
+
"l.uuid as locationUuid"
|
|
53
|
+
);
|
|
54
|
+
if (studyId !== undefined && studyId !== null) {
|
|
55
|
+
query.where("f.studyId", studyId);
|
|
56
|
+
}
|
|
60
57
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
58
|
+
const [countResult] = await query.clone().clearSelect().count("* as count");
|
|
59
|
+
const totalCount = +countResult.count;
|
|
60
|
+
const folders = await query.clone().limit(limit).offset(offset);
|
|
64
61
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
62
|
+
return {
|
|
63
|
+
success: true,
|
|
64
|
+
data: folders,
|
|
65
|
+
page,
|
|
66
|
+
limit,
|
|
67
|
+
count: folders.length,
|
|
68
|
+
totalCount,
|
|
69
|
+
totalPages: Math.ceil(totalCount / limit),
|
|
70
|
+
};
|
|
71
|
+
}
|
|
75
72
|
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { Knex } from "knex";
|
|
2
|
+
import { IBaseDAO, IDataPaginator } from "../../d.types";
|
|
3
|
+
import { ILocation } from "../../interfaces/location/location.interfaces";
|
|
4
|
+
import { IVideo } from "../../interfaces/video/video.interfaces";
|
|
5
|
+
import KnexManager from "../../KnexConnection";
|
|
6
|
+
|
|
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").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(
|
|
64
|
+
ST_MakePoint(longitude, latitude)::geography,
|
|
65
|
+
ST_MakePoint(?, ?)::geography,
|
|
66
|
+
?
|
|
67
|
+
)`,
|
|
68
|
+
[longitude, latitude, radiusKm * 1000] // Convert km to meters
|
|
69
|
+
);
|
|
70
|
+
return locations;
|
|
71
|
+
}
|
|
72
|
+
|
|
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
|
+
}
|
|
101
|
+
}
|
|
@@ -1,22 +1,22 @@
|
|
|
1
1
|
import { Knex } from "knex";
|
|
2
2
|
import { IBaseDAO, IDataPaginator } from "../../d.types";
|
|
3
|
-
import {
|
|
4
|
-
IMessage,
|
|
5
|
-
IMessageCreate,
|
|
6
|
-
IMessageUpdate,
|
|
7
|
-
} from "../../interfaces/message/message.interfaces";
|
|
3
|
+
import { IMessage, IMessageCreate, IMessageUpdate } from '../../interfaces/message/message.interfaces';
|
|
8
4
|
import KnexManager from "../../KnexConnection";
|
|
9
5
|
|
|
10
6
|
export class MessageDAO implements IBaseDAO<IMessage> {
|
|
11
7
|
private _knex: Knex<any, unknown[]> = KnexManager.getConnection();
|
|
12
8
|
|
|
13
9
|
async create(item: IMessageCreate): Promise<IMessage> {
|
|
14
|
-
const [result] = await this._knex(
|
|
10
|
+
const [result] = await this._knex('message')
|
|
11
|
+
.insert(item)
|
|
12
|
+
.returning('*');
|
|
15
13
|
return result;
|
|
16
14
|
}
|
|
17
15
|
|
|
18
16
|
async getById(id: number): Promise<IMessage | null> {
|
|
19
|
-
const result = await this._knex(
|
|
17
|
+
const result = await this._knex('message')
|
|
18
|
+
.where('id', id)
|
|
19
|
+
.first();
|
|
20
20
|
return result || null;
|
|
21
21
|
}
|
|
22
22
|
|
|
@@ -27,13 +27,13 @@ export class MessageDAO implements IBaseDAO<IMessage> {
|
|
|
27
27
|
|
|
28
28
|
async getAll(page = 1, limit = 10): Promise<IDataPaginator<IMessage>> {
|
|
29
29
|
const offset = (page - 1) * limit;
|
|
30
|
-
|
|
30
|
+
|
|
31
31
|
const [results, [{ count }]] = await Promise.all([
|
|
32
|
-
this._knex(
|
|
33
|
-
.orderBy(
|
|
32
|
+
this._knex('message')
|
|
33
|
+
.orderBy('created_at', 'asc')
|
|
34
34
|
.limit(limit)
|
|
35
35
|
.offset(offset),
|
|
36
|
-
this._knex(
|
|
36
|
+
this._knex('message').count('* as count')
|
|
37
37
|
]);
|
|
38
38
|
|
|
39
39
|
const totalCount = parseInt(count as string);
|
|
@@ -44,40 +44,40 @@ export class MessageDAO implements IBaseDAO<IMessage> {
|
|
|
44
44
|
limit,
|
|
45
45
|
count: results.length,
|
|
46
46
|
totalCount,
|
|
47
|
-
totalPages: Math.ceil(totalCount / limit)
|
|
47
|
+
totalPages: Math.ceil(totalCount / limit)
|
|
48
48
|
};
|
|
49
49
|
}
|
|
50
50
|
|
|
51
51
|
async update(id: number, item: IMessageUpdate): Promise<IMessage | null> {
|
|
52
|
-
const [result] = await this._knex(
|
|
53
|
-
.where(
|
|
52
|
+
const [result] = await this._knex('message')
|
|
53
|
+
.where('id', id)
|
|
54
54
|
.update({
|
|
55
55
|
...item,
|
|
56
|
-
updated_at: new Date()
|
|
56
|
+
updated_at: new Date()
|
|
57
57
|
})
|
|
58
|
-
.returning(
|
|
58
|
+
.returning('*');
|
|
59
59
|
return result || null;
|
|
60
60
|
}
|
|
61
61
|
|
|
62
62
|
async delete(id: number): Promise<boolean> {
|
|
63
|
-
const result = await this._knex(
|
|
63
|
+
const result = await this._knex('message')
|
|
64
|
+
.where('id', id)
|
|
65
|
+
.delete();
|
|
64
66
|
return result > 0;
|
|
65
67
|
}
|
|
66
68
|
|
|
67
|
-
async getByChatId(
|
|
68
|
-
chatId: number,
|
|
69
|
-
page = 1,
|
|
70
|
-
limit = 50,
|
|
71
|
-
): Promise<IDataPaginator<IMessage>> {
|
|
69
|
+
async getByChatId(chatId: number, page = 1, limit = 50): Promise<IDataPaginator<IMessage>> {
|
|
72
70
|
const offset = (page - 1) * limit;
|
|
73
|
-
|
|
71
|
+
|
|
74
72
|
const [results, [{ count }]] = await Promise.all([
|
|
75
|
-
this._knex(
|
|
76
|
-
.where(
|
|
77
|
-
.orderBy(
|
|
73
|
+
this._knex('message')
|
|
74
|
+
.where('chatId', chatId)
|
|
75
|
+
.orderBy('created_at', 'asc')
|
|
78
76
|
.limit(limit)
|
|
79
77
|
.offset(offset),
|
|
80
|
-
this._knex(
|
|
78
|
+
this._knex('message')
|
|
79
|
+
.where('chatId', chatId)
|
|
80
|
+
.count('* as count')
|
|
81
81
|
]);
|
|
82
82
|
|
|
83
83
|
const totalCount = parseInt(count as string);
|
|
@@ -88,15 +88,15 @@ export class MessageDAO implements IBaseDAO<IMessage> {
|
|
|
88
88
|
limit,
|
|
89
89
|
count: results.length,
|
|
90
90
|
totalCount,
|
|
91
|
-
totalPages: Math.ceil(totalCount / limit)
|
|
91
|
+
totalPages: Math.ceil(totalCount / limit)
|
|
92
92
|
};
|
|
93
93
|
}
|
|
94
94
|
|
|
95
95
|
async getLatestMessages(chatId: number, limit = 50): Promise<IMessage[]> {
|
|
96
|
-
return await this._knex(
|
|
97
|
-
.where(
|
|
98
|
-
.orderBy(
|
|
96
|
+
return await this._knex('message')
|
|
97
|
+
.where('chatId', chatId)
|
|
98
|
+
.orderBy('created_at', 'desc')
|
|
99
99
|
.limit(limit)
|
|
100
100
|
.then((messages: IMessage[]) => messages.reverse());
|
|
101
101
|
}
|
|
102
|
-
}
|
|
102
|
+
}
|