@trafficgroup/knex-rel 0.1.20 → 0.1.22
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/constants/study.constants.d.ts +10 -0
- package/dist/constants/study.constants.js +23 -0
- package/dist/constants/study.constants.js.map +1 -0
- package/dist/dao/study/study.dao.d.ts +8 -1
- package/dist/dao/study/study.dao.js +22 -4
- package/dist/dao/study/study.dao.js.map +1 -1
- package/dist/index.d.ts +3 -0
- package/dist/index.js +4 -1
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/constants/study.constants.ts +22 -0
- package/src/dao/study/study.dao.ts +32 -4
- package/src/index.ts +8 -1
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Study sorting and filtering constants
|
|
3
|
+
* Shared between DAO and API controller to ensure validation consistency
|
|
4
|
+
*/
|
|
5
|
+
export declare const VALID_STUDY_SORT_FIELDS: readonly ["created_at", "name", "type", "status"];
|
|
6
|
+
export type StudySortField = (typeof VALID_STUDY_SORT_FIELDS)[number];
|
|
7
|
+
/**
|
|
8
|
+
* Maps API sort field names to database column names with table aliases
|
|
9
|
+
*/
|
|
10
|
+
export declare const STUDY_SORT_COLUMN_MAP: Record<StudySortField, string>;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Study sorting and filtering constants
|
|
4
|
+
* Shared between DAO and API controller to ensure validation consistency
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.STUDY_SORT_COLUMN_MAP = exports.VALID_STUDY_SORT_FIELDS = void 0;
|
|
8
|
+
exports.VALID_STUDY_SORT_FIELDS = [
|
|
9
|
+
"created_at",
|
|
10
|
+
"name",
|
|
11
|
+
"type",
|
|
12
|
+
"status",
|
|
13
|
+
];
|
|
14
|
+
/**
|
|
15
|
+
* Maps API sort field names to database column names with table aliases
|
|
16
|
+
*/
|
|
17
|
+
exports.STUDY_SORT_COLUMN_MAP = {
|
|
18
|
+
created_at: "s.created_at",
|
|
19
|
+
name: "s.name",
|
|
20
|
+
type: "s.type",
|
|
21
|
+
status: "s.status",
|
|
22
|
+
};
|
|
23
|
+
//# sourceMappingURL=study.constants.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"study.constants.js","sourceRoot":"","sources":["../../src/constants/study.constants.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAEU,QAAA,uBAAuB,GAAG;IACrC,YAAY;IACZ,MAAM;IACN,MAAM;IACN,QAAQ;CACA,CAAC;AAGX;;GAEG;AACU,QAAA,qBAAqB,GAAmC;IACnE,UAAU,EAAE,cAAc;IAC1B,IAAI,EAAE,QAAQ;IACd,IAAI,EAAE,QAAQ;IACd,MAAM,EAAE,UAAU;CACnB,CAAC"}
|
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
import { IBaseDAO, IDataPaginator } from "../../d.types";
|
|
2
2
|
import { IStudy } from "../../interfaces/study/study.interfaces";
|
|
3
|
+
import { StudySortField } from "../../constants/study.constants";
|
|
4
|
+
import { SortOrder } from "../../constants/video.constants";
|
|
5
|
+
export interface IStudyFilters {
|
|
6
|
+
createdBy?: number | null;
|
|
7
|
+
sortBy?: StudySortField;
|
|
8
|
+
sortOrder?: SortOrder;
|
|
9
|
+
}
|
|
3
10
|
export declare class StudyDAO implements IBaseDAO<IStudy> {
|
|
4
11
|
private _knex;
|
|
5
12
|
create(item: IStudy): Promise<IStudy>;
|
|
@@ -7,6 +14,6 @@ export declare class StudyDAO implements IBaseDAO<IStudy> {
|
|
|
7
14
|
getByUuid(uuid: string): Promise<IStudy | null>;
|
|
8
15
|
update(id: number, item: Partial<IStudy>): Promise<IStudy | null>;
|
|
9
16
|
delete(id: number): Promise<boolean>;
|
|
10
|
-
getAll(page: number, limit: number,
|
|
17
|
+
getAll(page: number, limit: number, filters?: IStudyFilters): Promise<IDataPaginator<IStudy>>;
|
|
11
18
|
getStudiesByLocation(locationId: number): Promise<IStudy[]>;
|
|
12
19
|
}
|
|
@@ -14,6 +14,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
15
|
exports.StudyDAO = void 0;
|
|
16
16
|
const KnexConnection_1 = __importDefault(require("../../KnexConnection"));
|
|
17
|
+
const study_constants_1 = require("../../constants/study.constants");
|
|
17
18
|
class StudyDAO {
|
|
18
19
|
constructor() {
|
|
19
20
|
this._knex = KnexConnection_1.default.getConnection();
|
|
@@ -63,17 +64,34 @@ class StudyDAO {
|
|
|
63
64
|
return result > 0;
|
|
64
65
|
});
|
|
65
66
|
}
|
|
66
|
-
getAll(page, limit,
|
|
67
|
+
getAll(page, limit, filters) {
|
|
67
68
|
return __awaiter(this, void 0, void 0, function* () {
|
|
68
69
|
const offset = (page - 1) * limit;
|
|
69
70
|
const query = this._knex("study as s")
|
|
70
71
|
.innerJoin("users as u", "s.createdBy", "u.id")
|
|
71
72
|
.leftJoin("locations as l", "s.locationId", "l.id")
|
|
72
73
|
.select("s.*", this._knex.raw("to_jsonb(u.*) as user"), this._knex.raw("to_jsonb(l.*) as location"));
|
|
73
|
-
if (
|
|
74
|
-
|
|
74
|
+
if (filters) {
|
|
75
|
+
if (filters.createdBy !== undefined && filters.createdBy !== null) {
|
|
76
|
+
query.where("s.createdBy", filters.createdBy);
|
|
77
|
+
}
|
|
78
|
+
if (filters.sortBy) {
|
|
79
|
+
const columnName = study_constants_1.STUDY_SORT_COLUMN_MAP[filters.sortBy];
|
|
80
|
+
const order = filters.sortOrder || "DESC";
|
|
81
|
+
query.orderBy(columnName, order);
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
query.orderBy("s.created_at", "DESC");
|
|
85
|
+
}
|
|
75
86
|
}
|
|
76
|
-
|
|
87
|
+
else {
|
|
88
|
+
query.orderBy("s.created_at", "DESC");
|
|
89
|
+
}
|
|
90
|
+
const [countResult] = yield query
|
|
91
|
+
.clone()
|
|
92
|
+
.clearSelect()
|
|
93
|
+
.clearOrder()
|
|
94
|
+
.count("* as count");
|
|
77
95
|
const totalCount = +countResult.count;
|
|
78
96
|
const studies = yield query.clone().limit(limit).offset(offset);
|
|
79
97
|
return {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"study.dao.js","sourceRoot":"","sources":["../../../src/dao/study/study.dao.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAGA,0EAA+C;
|
|
1
|
+
{"version":3,"file":"study.dao.js","sourceRoot":"","sources":["../../../src/dao/study/study.dao.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAGA,0EAA+C;AAC/C,qEAGyC;AASzC,MAAa,QAAQ;IAArB;QACU,UAAK,GAAyB,wBAAW,CAAC,aAAa,EAAE,CAAC;IAoHpE,CAAC;IAlHO,MAAM,CAAC,IAAY;;YACvB,MAAM,CAAC,YAAY,CAAC,GAAG,MAAM,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC;iBAC7C,MAAM,CAAC,IAAI,CAAC;iBACZ,SAAS,CAAC,GAAG,CAAC,CAAC;YAClB,OAAO,YAAY,CAAC;QACtB,CAAC;KAAA;IAEK,OAAO,CAAC,EAAU;;YACtB,MAAM,KAAK,GAAG,MAAM,IAAI,CAAC,KAAK,CAAC,YAAY,CAAC;iBACzC,SAAS,CAAC,YAAY,EAAE,aAAa,EAAE,MAAM,CAAC;iBAC9C,QAAQ,CAAC,gBAAgB,EAAE,cAAc,EAAE,MAAM,CAAC;iBAClD,MAAM,CACL,KAAK,EACL,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,uBAAuB,CAAC,EACvC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,2BAA2B,CAAC,CAC5C;iBACA,KAAK,CAAC,MAAM,EAAE,EAAE,CAAC;iBACjB,KAAK,EAAE,CAAC;YACX,OAAO,KAAK,IAAI,IAAI,CAAC;QACvB,CAAC;KAAA;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L,KAAK,EAAE,OAAO,CAAC,MAAM;gBACrB,UAAU;gBACV,UAAU,EAAE,IAAI,CAAC,IAAI,CAAC,UAAU,GAAG,KAAK,CAAC;aAC1C,CAAC;QACJ,CAAC;KAAA;IAEK,oBAAoB,CAAC,UAAkB;;YAC3C,MAAM,OAAO,GAAG,MAAM,IAAI,CAAC,KAAK,CAAC,YAAY,CAAC;iBAC3C,SAAS,CAAC,YAAY,EAAE,aAAa,EAAE,MAAM,CAAC;iBAC9C,QAAQ,CAAC,gBAAgB,EAAE,cAAc,EAAE,MAAM,CAAC;iBAClD,MAAM,CACL,KAAK,EACL,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,uBAAuB,CAAC,EACvC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,2BAA2B,CAAC,CAC5C;iBACA,KAAK,CAAC,cAAc,EAAE,UAAU,CAAC;iBACjC,OAAO,CAAC,cAAc,EAAE,MAAM,CAAC;iBAC/B,KAAK,CAAC,EAAE,CAAC,CAAC;YAEb,OAAO,OAAO,CAAC;QACjB,CAAC;KAAA;CACF;AArHD,4BAqHC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -17,7 +17,10 @@ export { VALID_VIDEO_SORT_FIELDS, VALID_SORT_ORDERS, VIDEO_SORT_COLUMN_MAP, } fr
|
|
|
17
17
|
export type { VideoSortField, SortOrder } from "./constants/video.constants";
|
|
18
18
|
export { VALID_FOLDER_SORT_FIELDS, VALID_FOLDER_STATUSES, FOLDER_SORT_COLUMN_MAP, } from "./constants/folder.constants";
|
|
19
19
|
export type { FolderSortField, FolderStatus, } from "./constants/folder.constants";
|
|
20
|
+
export { VALID_STUDY_SORT_FIELDS, STUDY_SORT_COLUMN_MAP, } from "./constants/study.constants";
|
|
21
|
+
export type { StudySortField } from "./constants/study.constants";
|
|
20
22
|
export type { IFolderFilters } from "./dao/folder/folder.dao";
|
|
23
|
+
export type { IStudyFilters } from "./dao/study/study.dao";
|
|
21
24
|
export { IDataPaginator } from "./d.types";
|
|
22
25
|
export { IAuth } from "./interfaces/auth/auth.interfaces";
|
|
23
26
|
export { IBatch } from "./interfaces/batch/batch.interfaces";
|
package/dist/index.js
CHANGED
|
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.KnexManager = exports.FOLDER_SORT_COLUMN_MAP = exports.VALID_FOLDER_STATUSES = exports.VALID_FOLDER_SORT_FIELDS = exports.VIDEO_SORT_COLUMN_MAP = exports.VALID_SORT_ORDERS = exports.VALID_VIDEO_SORT_FIELDS = exports.VideoMinuteResultDAO = exports.VideoDAO = exports.ReconciliationLogDAO = exports.UserPushNotificationTokenDAO = exports.UserDAO = exports.SystemConfigurationDAO = exports.StudyDAO = exports.ReportConfigurationDAO = exports.MessageDAO = exports.LocationDAO = exports.FolderDAO = exports.ChatDAO = exports.CameraDAO = exports.BatchDAO = exports.AuthDAO = void 0;
|
|
6
|
+
exports.KnexManager = exports.STUDY_SORT_COLUMN_MAP = exports.VALID_STUDY_SORT_FIELDS = exports.FOLDER_SORT_COLUMN_MAP = exports.VALID_FOLDER_STATUSES = exports.VALID_FOLDER_SORT_FIELDS = exports.VIDEO_SORT_COLUMN_MAP = exports.VALID_SORT_ORDERS = exports.VALID_VIDEO_SORT_FIELDS = exports.VideoMinuteResultDAO = exports.VideoDAO = exports.ReconciliationLogDAO = exports.UserPushNotificationTokenDAO = exports.UserDAO = exports.SystemConfigurationDAO = exports.StudyDAO = exports.ReportConfigurationDAO = exports.MessageDAO = exports.LocationDAO = exports.FolderDAO = exports.ChatDAO = exports.CameraDAO = exports.BatchDAO = exports.AuthDAO = void 0;
|
|
7
7
|
// DAOs
|
|
8
8
|
var auth_dao_1 = require("./dao/auth/auth.dao");
|
|
9
9
|
Object.defineProperty(exports, "AuthDAO", { enumerable: true, get: function () { return auth_dao_1.AuthDAO; } });
|
|
@@ -44,6 +44,9 @@ var folder_constants_1 = require("./constants/folder.constants");
|
|
|
44
44
|
Object.defineProperty(exports, "VALID_FOLDER_SORT_FIELDS", { enumerable: true, get: function () { return folder_constants_1.VALID_FOLDER_SORT_FIELDS; } });
|
|
45
45
|
Object.defineProperty(exports, "VALID_FOLDER_STATUSES", { enumerable: true, get: function () { return folder_constants_1.VALID_FOLDER_STATUSES; } });
|
|
46
46
|
Object.defineProperty(exports, "FOLDER_SORT_COLUMN_MAP", { enumerable: true, get: function () { return folder_constants_1.FOLDER_SORT_COLUMN_MAP; } });
|
|
47
|
+
var study_constants_1 = require("./constants/study.constants");
|
|
48
|
+
Object.defineProperty(exports, "VALID_STUDY_SORT_FIELDS", { enumerable: true, get: function () { return study_constants_1.VALID_STUDY_SORT_FIELDS; } });
|
|
49
|
+
Object.defineProperty(exports, "STUDY_SORT_COLUMN_MAP", { enumerable: true, get: function () { return study_constants_1.STUDY_SORT_COLUMN_MAP; } });
|
|
47
50
|
const KnexConnection_1 = __importDefault(require("./KnexConnection"));
|
|
48
51
|
exports.KnexManager = KnexConnection_1.default;
|
|
49
52
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;AAAA,OAAO;AACP,gDAA8C;AAArC,mGAAA,OAAO,OAAA;AAChB,mDAAiD;AAAxC,qGAAA,QAAQ,OAAA;AACjB,sDAAoD;AAA3C,uGAAA,SAAS,OAAA;AAClB,gDAAsE;AAA7D,mGAAA,OAAO,OAAA;AAChB,sDAAoD;AAA3C,uGAAA,SAAS,OAAA;AAClB,4DAA0D;AAAjD,2GAAA,WAAW,OAAA;AACpB,yDAAuD;AAA9C,yGAAA,UAAU,OAAA;AACnB,gGAA6F;AAApF,kIAAA,sBAAsB,OAAA;AAC/B,mDAAiD;AAAxC,qGAAA,QAAQ,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,uBAAuB,OAAA;AACvB,oHAAA,iBAAiB,OAAA;AACjB,wHAAA,qBAAqB,OAAA;AAIvB,iEAIsC;AAHpC,4HAAA,wBAAwB,OAAA;AACxB,yHAAA,qBAAqB,OAAA;AACrB,0HAAA,sBAAsB,OAAA;AAOxB,+DAGqC;AAFnC,0HAAA,uBAAuB,OAAA;AACvB,wHAAA,qBAAqB,OAAA;AAoDvB,sEAA2C;AAClC,sBADF,wBAAW,CACE"}
|
package/package.json
CHANGED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Study sorting and filtering constants
|
|
3
|
+
* Shared between DAO and API controller to ensure validation consistency
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
export const VALID_STUDY_SORT_FIELDS = [
|
|
7
|
+
"created_at",
|
|
8
|
+
"name",
|
|
9
|
+
"type",
|
|
10
|
+
"status",
|
|
11
|
+
] as const;
|
|
12
|
+
export type StudySortField = (typeof VALID_STUDY_SORT_FIELDS)[number];
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Maps API sort field names to database column names with table aliases
|
|
16
|
+
*/
|
|
17
|
+
export const STUDY_SORT_COLUMN_MAP: Record<StudySortField, string> = {
|
|
18
|
+
created_at: "s.created_at",
|
|
19
|
+
name: "s.name",
|
|
20
|
+
type: "s.type",
|
|
21
|
+
status: "s.status",
|
|
22
|
+
};
|
|
@@ -2,6 +2,17 @@ import { Knex } from "knex";
|
|
|
2
2
|
import { IBaseDAO, IDataPaginator } from "../../d.types";
|
|
3
3
|
import { IStudy } from "../../interfaces/study/study.interfaces";
|
|
4
4
|
import KnexManager from "../../KnexConnection";
|
|
5
|
+
import {
|
|
6
|
+
StudySortField,
|
|
7
|
+
STUDY_SORT_COLUMN_MAP,
|
|
8
|
+
} from "../../constants/study.constants";
|
|
9
|
+
import { SortOrder } from "../../constants/video.constants";
|
|
10
|
+
|
|
11
|
+
export interface IStudyFilters {
|
|
12
|
+
createdBy?: number | null;
|
|
13
|
+
sortBy?: StudySortField;
|
|
14
|
+
sortOrder?: SortOrder;
|
|
15
|
+
}
|
|
5
16
|
|
|
6
17
|
export class StudyDAO implements IBaseDAO<IStudy> {
|
|
7
18
|
private _knex: Knex<any, unknown[]> = KnexManager.getConnection();
|
|
@@ -57,7 +68,7 @@ export class StudyDAO implements IBaseDAO<IStudy> {
|
|
|
57
68
|
async getAll(
|
|
58
69
|
page: number,
|
|
59
70
|
limit: number,
|
|
60
|
-
|
|
71
|
+
filters?: IStudyFilters,
|
|
61
72
|
): Promise<IDataPaginator<IStudy>> {
|
|
62
73
|
const offset = (page - 1) * limit;
|
|
63
74
|
|
|
@@ -69,11 +80,28 @@ export class StudyDAO implements IBaseDAO<IStudy> {
|
|
|
69
80
|
this._knex.raw("to_jsonb(u.*) as user"),
|
|
70
81
|
this._knex.raw("to_jsonb(l.*) as location"),
|
|
71
82
|
);
|
|
72
|
-
|
|
73
|
-
|
|
83
|
+
|
|
84
|
+
if (filters) {
|
|
85
|
+
if (filters.createdBy !== undefined && filters.createdBy !== null) {
|
|
86
|
+
query.where("s.createdBy", filters.createdBy);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (filters.sortBy) {
|
|
90
|
+
const columnName = STUDY_SORT_COLUMN_MAP[filters.sortBy];
|
|
91
|
+
const order = filters.sortOrder || "DESC";
|
|
92
|
+
query.orderBy(columnName, order);
|
|
93
|
+
} else {
|
|
94
|
+
query.orderBy("s.created_at", "DESC");
|
|
95
|
+
}
|
|
96
|
+
} else {
|
|
97
|
+
query.orderBy("s.created_at", "DESC");
|
|
74
98
|
}
|
|
75
99
|
|
|
76
|
-
const [countResult] = await query
|
|
100
|
+
const [countResult] = await query
|
|
101
|
+
.clone()
|
|
102
|
+
.clearSelect()
|
|
103
|
+
.clearOrder()
|
|
104
|
+
.count("* as count");
|
|
77
105
|
const totalCount = +countResult.count;
|
|
78
106
|
const studies = await query.clone().limit(limit).offset(offset);
|
|
79
107
|
|
package/src/index.ts
CHANGED
|
@@ -33,8 +33,15 @@ export type {
|
|
|
33
33
|
FolderStatus,
|
|
34
34
|
} from "./constants/folder.constants";
|
|
35
35
|
|
|
36
|
-
|
|
36
|
+
export {
|
|
37
|
+
VALID_STUDY_SORT_FIELDS,
|
|
38
|
+
STUDY_SORT_COLUMN_MAP,
|
|
39
|
+
} from "./constants/study.constants";
|
|
40
|
+
export type { StudySortField } from "./constants/study.constants";
|
|
41
|
+
|
|
42
|
+
// Filters interfaces
|
|
37
43
|
export type { IFolderFilters } from "./dao/folder/folder.dao";
|
|
44
|
+
export type { IStudyFilters } from "./dao/study/study.dao";
|
|
38
45
|
|
|
39
46
|
// Interfaces
|
|
40
47
|
export { IDataPaginator } from "./d.types";
|