@trafficgroup/knex-rel 0.1.8 → 0.1.9
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/dist/dao/VideoMinuteResultDAO.d.ts +3 -0
- package/dist/dao/VideoMinuteResultDAO.js +5 -2
- package/dist/dao/VideoMinuteResultDAO.js.map +1 -1
- package/dist/dao/camera/camera.dao.d.ts +17 -7
- package/dist/dao/camera/camera.dao.js +33 -48
- package/dist/dao/camera/camera.dao.js.map +1 -1
- package/dist/dao/folder/folder.dao.js +2 -1
- 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 +123 -0
- package/dist/dao/location/location.dao.js.map +1 -0
- package/dist/dao/study/study.dao.d.ts +1 -1
- package/dist/dao/study/study.dao.js +10 -10
- package/dist/dao/study/study.dao.js.map +1 -1
- package/dist/dao/video/video.dao.d.ts +7 -0
- package/dist/dao/video/video.dao.js +33 -1
- package/dist/dao/video/video.dao.js.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -1
- package/dist/interfaces/camera/camera.interfaces.d.ts +4 -2
- 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/study/study.interfaces.d.ts +4 -3
- package/dist/interfaces/video/video.interfaces.d.ts +1 -0
- package/migrations/20251112120000_migration.ts +89 -0
- package/migrations/20251112120100_migration.ts +21 -0
- package/migrations/20251112120200_migration.ts +50 -0
- package/migrations/20251112120300_migration.ts +27 -0
- package/package.json +1 -1
- package/src/dao/VideoMinuteResultDAO.ts +7 -2
- package/src/dao/camera/camera.dao.ts +44 -61
- package/src/dao/folder/folder.dao.ts +7 -1
- package/src/dao/location/location.dao.ts +123 -0
- package/src/dao/study/study.dao.ts +10 -10
- package/src/dao/video/video.dao.ts +45 -1
- package/src/index.ts +2 -0
- package/src/interfaces/camera/camera.interfaces.ts +4 -2
- package/src/interfaces/location/location.interfaces.ts +9 -0
- package/src/interfaces/study/study.interfaces.ts +4 -3
- package/src/interfaces/video/video.interfaces.ts +1 -0
- package/plan.md +0 -684
|
@@ -0,0 +1,123 @@
|
|
|
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")
|
|
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(
|
|
78
|
+
ST_MakePoint(longitude, latitude)::geography,
|
|
79
|
+
ST_MakePoint(?, ?)::geography,
|
|
80
|
+
?
|
|
81
|
+
)`,
|
|
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}%`);
|
|
103
|
+
}
|
|
104
|
+
|
|
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
|
+
}
|
|
123
|
+
}
|
|
@@ -16,11 +16,11 @@ export class StudyDAO implements IBaseDAO<IStudy> {
|
|
|
16
16
|
async getById(id: number): Promise<IStudy | null> {
|
|
17
17
|
const study = await this._knex("study as s")
|
|
18
18
|
.innerJoin("users as u", "s.createdBy", "u.id")
|
|
19
|
-
.leftJoin("
|
|
19
|
+
.leftJoin("locations as l", "s.locationId", "l.id")
|
|
20
20
|
.select(
|
|
21
21
|
"s.*",
|
|
22
22
|
this._knex.raw("to_jsonb(u.*) as user"),
|
|
23
|
-
this._knex.raw("to_jsonb(
|
|
23
|
+
this._knex.raw("to_jsonb(l.*) as location"),
|
|
24
24
|
)
|
|
25
25
|
.where("s.id", id)
|
|
26
26
|
.first();
|
|
@@ -30,11 +30,11 @@ export class StudyDAO implements IBaseDAO<IStudy> {
|
|
|
30
30
|
async getByUuid(uuid: string): Promise<IStudy | null> {
|
|
31
31
|
const study = await this._knex("study as s")
|
|
32
32
|
.innerJoin("users as u", "s.createdBy", "u.id")
|
|
33
|
-
.leftJoin("
|
|
33
|
+
.leftJoin("locations as l", "s.locationId", "l.id")
|
|
34
34
|
.select(
|
|
35
35
|
"s.*",
|
|
36
36
|
this._knex.raw("to_jsonb(u.*) as user"),
|
|
37
|
-
this._knex.raw("to_jsonb(
|
|
37
|
+
this._knex.raw("to_jsonb(l.*) as location"),
|
|
38
38
|
)
|
|
39
39
|
.where("s.uuid", uuid)
|
|
40
40
|
.first();
|
|
@@ -63,11 +63,11 @@ export class StudyDAO implements IBaseDAO<IStudy> {
|
|
|
63
63
|
|
|
64
64
|
const query = this._knex("study as s")
|
|
65
65
|
.innerJoin("users as u", "s.createdBy", "u.id")
|
|
66
|
-
.leftJoin("
|
|
66
|
+
.leftJoin("locations as l", "s.locationId", "l.id")
|
|
67
67
|
.select(
|
|
68
68
|
"s.*",
|
|
69
69
|
this._knex.raw("to_jsonb(u.*) as user"),
|
|
70
|
-
this._knex.raw("to_jsonb(
|
|
70
|
+
this._knex.raw("to_jsonb(l.*) as location"),
|
|
71
71
|
);
|
|
72
72
|
if (createdBy !== undefined && createdBy !== null) {
|
|
73
73
|
query.where("s.createdBy", createdBy);
|
|
@@ -88,16 +88,16 @@ export class StudyDAO implements IBaseDAO<IStudy> {
|
|
|
88
88
|
};
|
|
89
89
|
}
|
|
90
90
|
|
|
91
|
-
async
|
|
91
|
+
async getStudiesByLocation(locationId: number): Promise<IStudy[]> {
|
|
92
92
|
const studies = await this._knex("study as s")
|
|
93
93
|
.innerJoin("users as u", "s.createdBy", "u.id")
|
|
94
|
-
.leftJoin("
|
|
94
|
+
.leftJoin("locations as l", "s.locationId", "l.id")
|
|
95
95
|
.select(
|
|
96
96
|
"s.*",
|
|
97
97
|
this._knex.raw("to_jsonb(u.*) as user"),
|
|
98
|
-
this._knex.raw("to_jsonb(
|
|
98
|
+
this._knex.raw("to_jsonb(l.*) as location"),
|
|
99
99
|
)
|
|
100
|
-
.where("s.
|
|
100
|
+
.where("s.locationId", locationId)
|
|
101
101
|
.orderBy("s.created_at", "desc")
|
|
102
102
|
.limit(10);
|
|
103
103
|
|
|
@@ -58,7 +58,13 @@ export class VideoDAO implements IBaseDAO<IVideo> {
|
|
|
58
58
|
|
|
59
59
|
const query = this._knex("video as v")
|
|
60
60
|
.innerJoin("folders as f", "v.folderId", "f.id")
|
|
61
|
-
.
|
|
61
|
+
.leftJoin("cameras as c", "v.cameraId", "c.id")
|
|
62
|
+
.select(
|
|
63
|
+
"v.*",
|
|
64
|
+
this._knex.raw("to_jsonb(f.*) as folder"),
|
|
65
|
+
"c.name as cameraName",
|
|
66
|
+
"c.uuid as cameraUuid",
|
|
67
|
+
);
|
|
62
68
|
if (folderId !== undefined && folderId !== null) {
|
|
63
69
|
query.where("v.folderId", folderId);
|
|
64
70
|
}
|
|
@@ -347,4 +353,42 @@ export class VideoDAO implements IBaseDAO<IVideo> {
|
|
|
347
353
|
.where("v.trimEnabled", true)
|
|
348
354
|
.orderBy("v.recordingStartedAt", "asc");
|
|
349
355
|
}
|
|
356
|
+
|
|
357
|
+
/**
|
|
358
|
+
* Get paginated videos for a specific camera
|
|
359
|
+
* @param cameraId - The camera ID
|
|
360
|
+
* @param page - Page number
|
|
361
|
+
* @param limit - Items per page
|
|
362
|
+
*/
|
|
363
|
+
async getVideosByCameraId(
|
|
364
|
+
cameraId: number,
|
|
365
|
+
page: number,
|
|
366
|
+
limit: number,
|
|
367
|
+
): Promise<IDataPaginator<IVideo>> {
|
|
368
|
+
const offset = (page - 1) * limit;
|
|
369
|
+
|
|
370
|
+
const query = this._knex("video as v")
|
|
371
|
+
.innerJoin("folders as f", "v.folderId", "f.id")
|
|
372
|
+
.select("v.*", this._knex.raw("to_jsonb(f.*) as folder"))
|
|
373
|
+
.where("v.cameraId", cameraId);
|
|
374
|
+
|
|
375
|
+
const [countResult] = await query.clone().clearSelect().count("* as count");
|
|
376
|
+
const totalCount = +countResult.count;
|
|
377
|
+
|
|
378
|
+
const videos = await query
|
|
379
|
+
.clone()
|
|
380
|
+
.limit(limit)
|
|
381
|
+
.offset(offset)
|
|
382
|
+
.orderBy("v.created_at", "desc");
|
|
383
|
+
|
|
384
|
+
return {
|
|
385
|
+
success: true,
|
|
386
|
+
data: videos,
|
|
387
|
+
page,
|
|
388
|
+
limit,
|
|
389
|
+
count: videos.length,
|
|
390
|
+
totalCount,
|
|
391
|
+
totalPages: Math.ceil(totalCount / limit),
|
|
392
|
+
};
|
|
393
|
+
}
|
|
350
394
|
}
|
package/src/index.ts
CHANGED
|
@@ -4,6 +4,7 @@ export { BatchDAO } from "./dao/batch/batch.dao";
|
|
|
4
4
|
export { CameraDAO } from "./dao/camera/camera.dao";
|
|
5
5
|
export { ChatDAO } from "./dao/chat/chat.dao";
|
|
6
6
|
export { FolderDAO } from "./dao/folder/folder.dao";
|
|
7
|
+
export { LocationDAO } from "./dao/location/location.dao";
|
|
7
8
|
export { MessageDAO } from "./dao/message/message.dao";
|
|
8
9
|
export { ReportConfigurationDAO } from "./dao/report-configuration/report-configuration.dao";
|
|
9
10
|
export { StudyDAO } from "./dao/study/study.dao";
|
|
@@ -23,6 +24,7 @@ export {
|
|
|
23
24
|
IChatUpdate,
|
|
24
25
|
} from "./interfaces/chat/chat.interfaces";
|
|
25
26
|
export { IFolder } from "./interfaces/folder/folder.interfaces";
|
|
27
|
+
export { ILocation } from "./interfaces/location/location.interfaces";
|
|
26
28
|
export {
|
|
27
29
|
IMessage,
|
|
28
30
|
IMessageCreate,
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
export interface ICamera {
|
|
2
2
|
id: number;
|
|
3
3
|
uuid: string;
|
|
4
|
+
locationId: number;
|
|
4
5
|
name: string;
|
|
5
|
-
|
|
6
|
-
|
|
6
|
+
description?: string;
|
|
7
|
+
status: "ACTIVE" | "INACTIVE" | "MAINTENANCE";
|
|
8
|
+
metadata: Record<string, any>;
|
|
7
9
|
created_at: string;
|
|
8
10
|
updated_at: string;
|
|
9
11
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { IUser } from "../user/user.interfaces";
|
|
2
|
-
import type {
|
|
2
|
+
import type { ILocation } from "../location/location.interfaces";
|
|
3
3
|
|
|
4
4
|
export interface IStudy {
|
|
5
5
|
id: number;
|
|
@@ -8,10 +8,11 @@ export interface IStudy {
|
|
|
8
8
|
description?: string;
|
|
9
9
|
type: "TMC" | "ATR";
|
|
10
10
|
createdBy: number;
|
|
11
|
-
|
|
11
|
+
locationId?: number;
|
|
12
|
+
isMultiCamera: boolean;
|
|
12
13
|
status: "COMPLETE" | "IN PROGRESS" | "FAILED";
|
|
13
14
|
created_at: string;
|
|
14
15
|
updated_at: string;
|
|
15
16
|
user?: IUser;
|
|
16
|
-
|
|
17
|
+
location?: ILocation;
|
|
17
18
|
}
|