@trafficgroup/knex-rel 0.0.27 → 0.0.29
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 +2 -5
- package/CLAUDE.md +72 -63
- package/dist/dao/VideoMinuteResultDAO.d.ts +48 -0
- package/dist/dao/VideoMinuteResultDAO.js +238 -0
- package/dist/dao/VideoMinuteResultDAO.js.map +1 -0
- package/dist/dao/auth/auth.dao.js +4 -1
- package/dist/dao/auth/auth.dao.js.map +1 -1
- package/dist/dao/chat/chat.dao.d.ts +1 -1
- package/dist/dao/chat/chat.dao.js +25 -35
- package/dist/dao/chat/chat.dao.js.map +1 -1
- package/dist/dao/folder/folder.dao.js +7 -2
- package/dist/dao/folder/folder.dao.js.map +1 -1
- package/dist/dao/message/message.dao.d.ts +1 -1
- package/dist/dao/message/message.dao.js +18 -26
- package/dist/dao/message/message.dao.js.map +1 -1
- package/dist/dao/study/study.dao.js +7 -2
- package/dist/dao/study/study.dao.js.map +1 -1
- package/dist/dao/user/user.dao.js +4 -1
- package/dist/dao/user/user.dao.js.map +1 -1
- package/dist/dao/user-push-notification-token/user-push-notification-token.dao.js +26 -8
- package/dist/dao/user-push-notification-token/user-push-notification-token.dao.js.map +1 -1
- package/dist/dao/video/video.dao.d.ts +15 -0
- package/dist/dao/video/video.dao.js +85 -2
- package/dist/dao/video/video.dao.js.map +1 -1
- package/dist/entities/BaseEntity.d.ts +4 -0
- package/dist/entities/BaseEntity.js +3 -0
- package/dist/entities/BaseEntity.js.map +1 -0
- package/dist/entities/VideoMinuteResult.d.ts +21 -0
- package/dist/entities/VideoMinuteResult.js +3 -0
- package/dist/entities/VideoMinuteResult.js.map +1 -0
- package/dist/index.d.ts +4 -2
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -1
- package/dist/interfaces/chat/chat.interfaces.d.ts +3 -3
- package/dist/interfaces/folder/folder.interfaces.d.ts +1 -1
- package/dist/interfaces/message/message.interfaces.d.ts +2 -2
- package/dist/interfaces/study/study.interfaces.d.ts +1 -1
- 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 +5 -2
- 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 +8 -4
- package/migrations/20250722211019_migration.ts +1 -1
- package/migrations/20250723153852_migration.ts +13 -10
- package/migrations/20250723162257_migration.ts +4 -7
- package/migrations/20250723171109_migration.ts +4 -7
- package/migrations/20250723205331_migration.ts +6 -9
- package/migrations/20250724191345_migration.ts +8 -11
- package/migrations/20250730180932_migration.ts +14 -13
- package/migrations/20250730213625_migration.ts +8 -11
- package/migrations/20250804124509_migration.ts +26 -21
- package/migrations/20250804132053_migration.ts +5 -8
- package/migrations/20250804164518_migration.ts +17 -0
- package/migrations/20250823223016_migration.ts +46 -0
- package/package.json +47 -47
- package/plan.md +304 -0
- package/src/d.types.ts +22 -18
- package/src/dao/VideoMinuteResultDAO.ts +273 -0
- package/src/dao/auth/auth.dao.ts +58 -55
- package/src/dao/chat/chat.dao.ts +45 -43
- package/src/dao/folder/folder.dao.ts +75 -66
- package/src/dao/message/message.dao.ts +32 -32
- package/src/dao/study/study.dao.ts +75 -66
- package/src/dao/user/user.dao.ts +59 -57
- package/src/dao/user-push-notification-token/user-push-notification-token.dao.ts +83 -49
- package/src/dao/video/video.dao.ts +199 -71
- package/src/entities/BaseEntity.ts +4 -0
- package/src/entities/VideoMinuteResult.ts +24 -0
- package/src/index.ts +37 -23
- package/src/interfaces/auth/auth.interfaces.ts +10 -10
- package/src/interfaces/chat/chat.interfaces.ts +4 -4
- package/src/interfaces/folder/folder.interfaces.ts +13 -13
- package/src/interfaces/message/message.interfaces.ts +3 -3
- package/src/interfaces/study/study.interfaces.ts +12 -12
- package/src/interfaces/user/user.interfaces.ts +11 -11
- package/src/interfaces/user-push-notification-token/user-push-notification-token.interfaces.ts +9 -9
- package/src/interfaces/video/video.interfaces.ts +22 -21
package/src/dao/auth/auth.dao.ts
CHANGED
|
@@ -4,58 +4,61 @@ import { IAuth } from "../../interfaces/auth/auth.interfaces";
|
|
|
4
4
|
import KnexManager from "../../KnexConnection";
|
|
5
5
|
|
|
6
6
|
export class AuthDAO implements IBaseDAO<IAuth> {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
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
|
+
}
|
package/src/dao/chat/chat.dao.ts
CHANGED
|
@@ -1,41 +1,39 @@
|
|
|
1
1
|
import { Knex } from "knex";
|
|
2
2
|
import { IBaseDAO, IDataPaginator } from "../../d.types";
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
IChat,
|
|
5
|
+
IChatCreate,
|
|
6
|
+
IChatUpdate,
|
|
7
|
+
} from "../../interfaces/chat/chat.interfaces";
|
|
4
8
|
import KnexManager from "../../KnexConnection";
|
|
5
9
|
|
|
6
10
|
export class ChatDAO implements IBaseDAO<IChat> {
|
|
7
11
|
private _knex: Knex<any, unknown[]> = KnexManager.getConnection();
|
|
8
12
|
|
|
9
13
|
async create(item: IChatCreate): Promise<IChat> {
|
|
10
|
-
const [result] = await this._knex(
|
|
11
|
-
.insert(item)
|
|
12
|
-
.returning('*');
|
|
14
|
+
const [result] = await this._knex("chat").insert(item).returning("*");
|
|
13
15
|
return result;
|
|
14
16
|
}
|
|
15
17
|
|
|
16
18
|
async getById(id: number): Promise<IChat | null> {
|
|
17
|
-
const result = await this._knex(
|
|
18
|
-
.where('id', id)
|
|
19
|
-
.first();
|
|
19
|
+
const result = await this._knex("chat").where("id", id).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(
|
|
25
|
-
.where('uuid', uuid)
|
|
26
|
-
.first();
|
|
24
|
+
const result = await this._knex("chat").where("uuid", uuid).first();
|
|
27
25
|
return result || null;
|
|
28
26
|
}
|
|
29
27
|
|
|
30
28
|
async getAll(page = 1, limit = 10): Promise<IDataPaginator<IChat>> {
|
|
31
29
|
const offset = (page - 1) * limit;
|
|
32
|
-
|
|
30
|
+
|
|
33
31
|
const [results, [{ count }]] = await Promise.all([
|
|
34
|
-
this._knex(
|
|
35
|
-
.orderBy(
|
|
32
|
+
this._knex("chat")
|
|
33
|
+
.orderBy("created_at", "desc")
|
|
36
34
|
.limit(limit)
|
|
37
35
|
.offset(offset),
|
|
38
|
-
this._knex(
|
|
36
|
+
this._knex("chat").count("* as count"),
|
|
39
37
|
]);
|
|
40
38
|
|
|
41
39
|
const totalCount = parseInt(count as string);
|
|
@@ -46,40 +44,40 @@ export class ChatDAO implements IBaseDAO<IChat> {
|
|
|
46
44
|
limit,
|
|
47
45
|
count: results.length,
|
|
48
46
|
totalCount,
|
|
49
|
-
totalPages: Math.ceil(totalCount / limit)
|
|
47
|
+
totalPages: Math.ceil(totalCount / limit),
|
|
50
48
|
};
|
|
51
49
|
}
|
|
52
50
|
|
|
53
51
|
async update(id: number, item: IChatUpdate): Promise<IChat | null> {
|
|
54
|
-
const [result] = await this._knex(
|
|
55
|
-
.where(
|
|
52
|
+
const [result] = await this._knex("chat")
|
|
53
|
+
.where("id", id)
|
|
56
54
|
.update({
|
|
57
55
|
...item,
|
|
58
|
-
updated_at: new Date()
|
|
56
|
+
updated_at: new Date(),
|
|
59
57
|
})
|
|
60
|
-
.returning(
|
|
58
|
+
.returning("*");
|
|
61
59
|
return result || null;
|
|
62
60
|
}
|
|
63
61
|
|
|
64
62
|
async delete(id: number): Promise<boolean> {
|
|
65
|
-
const result = await this._knex(
|
|
66
|
-
.where('id', id)
|
|
67
|
-
.delete();
|
|
63
|
+
const result = await this._knex("chat").where("id", id).delete();
|
|
68
64
|
return result > 0;
|
|
69
65
|
}
|
|
70
66
|
|
|
71
|
-
async getByUserId(
|
|
67
|
+
async getByUserId(
|
|
68
|
+
userId: number,
|
|
69
|
+
page = 1,
|
|
70
|
+
limit = 10,
|
|
71
|
+
): 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(
|
|
81
|
-
.where('userId', userId)
|
|
82
|
-
.count('* as count')
|
|
80
|
+
this._knex("chat").where("userId", userId).count("* as count"),
|
|
83
81
|
]);
|
|
84
82
|
|
|
85
83
|
const totalCount = parseInt(count as string);
|
|
@@ -90,24 +88,28 @@ export class ChatDAO implements IBaseDAO<IChat> {
|
|
|
90
88
|
limit,
|
|
91
89
|
count: results.length,
|
|
92
90
|
totalCount,
|
|
93
|
-
totalPages: Math.ceil(totalCount / limit)
|
|
91
|
+
totalPages: Math.ceil(totalCount / limit),
|
|
94
92
|
};
|
|
95
93
|
}
|
|
96
94
|
|
|
97
|
-
async getActiveByUserId(
|
|
95
|
+
async getActiveByUserId(
|
|
96
|
+
userId: number,
|
|
97
|
+
page = 1,
|
|
98
|
+
limit = 10,
|
|
99
|
+
): Promise<IDataPaginator<IChat>> {
|
|
98
100
|
const offset = (page - 1) * limit;
|
|
99
|
-
|
|
101
|
+
|
|
100
102
|
const [results, [{ count }]] = await Promise.all([
|
|
101
|
-
this._knex(
|
|
102
|
-
.where(
|
|
103
|
-
.where(
|
|
104
|
-
.orderBy(
|
|
103
|
+
this._knex("chat")
|
|
104
|
+
.where("userId", userId)
|
|
105
|
+
.where("status", "active")
|
|
106
|
+
.orderBy("created_at", "desc")
|
|
105
107
|
.limit(limit)
|
|
106
108
|
.offset(offset),
|
|
107
|
-
this._knex(
|
|
108
|
-
.where(
|
|
109
|
-
.where(
|
|
110
|
-
.count(
|
|
109
|
+
this._knex("chat")
|
|
110
|
+
.where("userId", userId)
|
|
111
|
+
.where("status", "active")
|
|
112
|
+
.count("* as count"),
|
|
111
113
|
]);
|
|
112
114
|
|
|
113
115
|
const totalCount = parseInt(count as string);
|
|
@@ -118,7 +120,7 @@ export class ChatDAO implements IBaseDAO<IChat> {
|
|
|
118
120
|
limit,
|
|
119
121
|
count: results.length,
|
|
120
122
|
totalCount,
|
|
121
|
-
totalPages: Math.ceil(totalCount / limit)
|
|
123
|
+
totalPages: Math.ceil(totalCount / limit),
|
|
122
124
|
};
|
|
123
125
|
}
|
|
124
|
-
}
|
|
126
|
+
}
|
|
@@ -1,66 +1,75 @@
|
|
|
1
|
-
import { Knex } from "knex";
|
|
2
|
-
import { IBaseDAO, IDataPaginator } from "../../d.types";
|
|
3
|
-
import { IFolder } from "../../interfaces/folder/folder.interfaces";
|
|
4
|
-
import KnexManager from "../../KnexConnection";
|
|
5
|
-
|
|
6
|
-
export class FolderDAO implements IBaseDAO<IFolder> {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
1
|
+
import { Knex } from "knex";
|
|
2
|
+
import { IBaseDAO, IDataPaginator } from "../../d.types";
|
|
3
|
+
import { IFolder } from "../../interfaces/folder/folder.interfaces";
|
|
4
|
+
import KnexManager from "../../KnexConnection";
|
|
5
|
+
|
|
6
|
+
export class FolderDAO implements IBaseDAO<IFolder> {
|
|
7
|
+
private _knex: Knex<any, unknown[]> = KnexManager.getConnection();
|
|
8
|
+
|
|
9
|
+
async create(item: IFolder): Promise<IFolder> {
|
|
10
|
+
const [createdFolder] = await this._knex("folders")
|
|
11
|
+
.insert(item)
|
|
12
|
+
.returning("*");
|
|
13
|
+
return createdFolder;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
async getById(id: number): Promise<IFolder | null> {
|
|
17
|
+
const folder = await this._knex("folders as f")
|
|
18
|
+
.innerJoin("study as s", "f.studyId", "s.id")
|
|
19
|
+
.select("f.*", this._knex.raw("to_jsonb(s.*) as study"))
|
|
20
|
+
.where("f.id", id)
|
|
21
|
+
.first();
|
|
22
|
+
return folder || null;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
async getByUuid(uuid: string): Promise<IFolder | null> {
|
|
26
|
+
const folder = await this._knex("folders as f")
|
|
27
|
+
.innerJoin("study as s", "f.studyId", "s.id")
|
|
28
|
+
.select("f.*", this._knex.raw("to_jsonb(s.*) as study"))
|
|
29
|
+
.where("f.uuid", uuid)
|
|
30
|
+
.first();
|
|
31
|
+
return folder || null;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async update(id: number, item: Partial<IFolder>): Promise<IFolder | null> {
|
|
35
|
+
const [updatedFolder] = await this._knex("folders")
|
|
36
|
+
.where({ id })
|
|
37
|
+
.update(item)
|
|
38
|
+
.returning("*");
|
|
39
|
+
return updatedFolder || null;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
async delete(id: number): Promise<boolean> {
|
|
43
|
+
const result = await this._knex("folders").where({ id }).del();
|
|
44
|
+
return result > 0;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
async getAll(
|
|
48
|
+
page: number,
|
|
49
|
+
limit: number,
|
|
50
|
+
studyId?: number | null,
|
|
51
|
+
): Promise<IDataPaginator<IFolder>> {
|
|
52
|
+
const offset = (page - 1) * limit;
|
|
53
|
+
|
|
54
|
+
const query = this._knex("folders as f")
|
|
55
|
+
.innerJoin("study as s", "f.studyId", "s.id")
|
|
56
|
+
.select("f.*", this._knex.raw("to_jsonb(s.*) as study"));
|
|
57
|
+
if (studyId !== undefined && studyId !== null) {
|
|
58
|
+
query.where("f.studyId", studyId);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const [countResult] = await query.clone().clearSelect().count("* as count");
|
|
62
|
+
const totalCount = +countResult.count;
|
|
63
|
+
const folders = await query.clone().limit(limit).offset(offset);
|
|
64
|
+
|
|
65
|
+
return {
|
|
66
|
+
success: true,
|
|
67
|
+
data: folders,
|
|
68
|
+
page,
|
|
69
|
+
limit,
|
|
70
|
+
count: folders.length,
|
|
71
|
+
totalCount,
|
|
72
|
+
totalPages: Math.ceil(totalCount / limit),
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
}
|
|
@@ -1,22 +1,22 @@
|
|
|
1
1
|
import { Knex } from "knex";
|
|
2
2
|
import { IBaseDAO, IDataPaginator } from "../../d.types";
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
IMessage,
|
|
5
|
+
IMessageCreate,
|
|
6
|
+
IMessageUpdate,
|
|
7
|
+
} from "../../interfaces/message/message.interfaces";
|
|
4
8
|
import KnexManager from "../../KnexConnection";
|
|
5
9
|
|
|
6
10
|
export class MessageDAO implements IBaseDAO<IMessage> {
|
|
7
11
|
private _knex: Knex<any, unknown[]> = KnexManager.getConnection();
|
|
8
12
|
|
|
9
13
|
async create(item: IMessageCreate): Promise<IMessage> {
|
|
10
|
-
const [result] = await this._knex(
|
|
11
|
-
.insert(item)
|
|
12
|
-
.returning('*');
|
|
14
|
+
const [result] = await this._knex("message").insert(item).returning("*");
|
|
13
15
|
return result;
|
|
14
16
|
}
|
|
15
17
|
|
|
16
18
|
async getById(id: number): Promise<IMessage | null> {
|
|
17
|
-
const result = await this._knex(
|
|
18
|
-
.where('id', id)
|
|
19
|
-
.first();
|
|
19
|
+
const result = await this._knex("message").where("id", id).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(
|
|
64
|
-
.where('id', id)
|
|
65
|
-
.delete();
|
|
63
|
+
const result = await this._knex("message").where("id", id).delete();
|
|
66
64
|
return result > 0;
|
|
67
65
|
}
|
|
68
66
|
|
|
69
|
-
async getByChatId(
|
|
67
|
+
async getByChatId(
|
|
68
|
+
chatId: number,
|
|
69
|
+
page = 1,
|
|
70
|
+
limit = 50,
|
|
71
|
+
): Promise<IDataPaginator<IMessage>> {
|
|
70
72
|
const offset = (page - 1) * limit;
|
|
71
|
-
|
|
73
|
+
|
|
72
74
|
const [results, [{ count }]] = await Promise.all([
|
|
73
|
-
this._knex(
|
|
74
|
-
.where(
|
|
75
|
-
.orderBy(
|
|
75
|
+
this._knex("message")
|
|
76
|
+
.where("chatId", chatId)
|
|
77
|
+
.orderBy("created_at", "asc")
|
|
76
78
|
.limit(limit)
|
|
77
79
|
.offset(offset),
|
|
78
|
-
this._knex(
|
|
79
|
-
.where('chatId', chatId)
|
|
80
|
-
.count('* as count')
|
|
80
|
+
this._knex("message").where("chatId", chatId).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
|
+
}
|