@trafficgroup/knex-rel 0.0.25 → 0.0.27

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.
@@ -0,0 +1,13 @@
1
+ import { IBaseDAO, IDataPaginator } from "../../d.types";
2
+ import { IChat, IChatCreate, IChatUpdate } from '../../interfaces/chat/chat.interfaces';
3
+ export declare class ChatDAO implements IBaseDAO<IChat> {
4
+ private _knex;
5
+ create(item: IChatCreate): Promise<IChat>;
6
+ getById(id: number): Promise<IChat | null>;
7
+ getByUuid(uuid: string): Promise<IChat | null>;
8
+ getAll(page?: number, limit?: number): Promise<IDataPaginator<IChat>>;
9
+ update(id: number, item: IChatUpdate): Promise<IChat | null>;
10
+ delete(id: number): Promise<boolean>;
11
+ getByUserId(userId: number, page?: number, limit?: number): Promise<IDataPaginator<IChat>>;
12
+ getActiveByUserId(userId: number, page?: number, limit?: number): Promise<IDataPaginator<IChat>>;
13
+ }
@@ -0,0 +1,138 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.ChatDAO = void 0;
16
+ const KnexConnection_1 = __importDefault(require("../../KnexConnection"));
17
+ class ChatDAO {
18
+ constructor() {
19
+ this._knex = KnexConnection_1.default.getConnection();
20
+ }
21
+ create(item) {
22
+ return __awaiter(this, void 0, void 0, function* () {
23
+ const [result] = yield this._knex('chat')
24
+ .insert(item)
25
+ .returning('*');
26
+ return result;
27
+ });
28
+ }
29
+ getById(id) {
30
+ return __awaiter(this, void 0, void 0, function* () {
31
+ const result = yield this._knex('chat')
32
+ .where('id', id)
33
+ .first();
34
+ return result || null;
35
+ });
36
+ }
37
+ getByUuid(uuid) {
38
+ return __awaiter(this, void 0, void 0, function* () {
39
+ const result = yield this._knex('chat')
40
+ .where('uuid', uuid)
41
+ .first();
42
+ return result || null;
43
+ });
44
+ }
45
+ getAll() {
46
+ return __awaiter(this, arguments, void 0, function* (page = 1, limit = 10) {
47
+ const offset = (page - 1) * limit;
48
+ const [results, [{ count }]] = yield Promise.all([
49
+ this._knex('chat')
50
+ .orderBy('created_at', 'desc')
51
+ .limit(limit)
52
+ .offset(offset),
53
+ this._knex('chat').count('* as count')
54
+ ]);
55
+ const totalCount = parseInt(count);
56
+ return {
57
+ success: true,
58
+ data: results,
59
+ page,
60
+ limit,
61
+ count: results.length,
62
+ totalCount,
63
+ totalPages: Math.ceil(totalCount / limit)
64
+ };
65
+ });
66
+ }
67
+ update(id, item) {
68
+ return __awaiter(this, void 0, void 0, function* () {
69
+ const [result] = yield this._knex('chat')
70
+ .where('id', id)
71
+ .update(Object.assign(Object.assign({}, item), { updated_at: new Date() }))
72
+ .returning('*');
73
+ return result || null;
74
+ });
75
+ }
76
+ delete(id) {
77
+ return __awaiter(this, void 0, void 0, function* () {
78
+ const result = yield this._knex('chat')
79
+ .where('id', id)
80
+ .delete();
81
+ return result > 0;
82
+ });
83
+ }
84
+ getByUserId(userId_1) {
85
+ return __awaiter(this, arguments, void 0, function* (userId, page = 1, limit = 10) {
86
+ const offset = (page - 1) * limit;
87
+ const [results, [{ count }]] = yield Promise.all([
88
+ this._knex('chat')
89
+ .where('userId', userId)
90
+ .orderBy('created_at', 'desc')
91
+ .limit(limit)
92
+ .offset(offset),
93
+ this._knex('chat')
94
+ .where('userId', userId)
95
+ .count('* as count')
96
+ ]);
97
+ const totalCount = parseInt(count);
98
+ return {
99
+ success: true,
100
+ data: results,
101
+ page,
102
+ limit,
103
+ count: results.length,
104
+ totalCount,
105
+ totalPages: Math.ceil(totalCount / limit)
106
+ };
107
+ });
108
+ }
109
+ getActiveByUserId(userId_1) {
110
+ return __awaiter(this, arguments, void 0, function* (userId, page = 1, limit = 10) {
111
+ const offset = (page - 1) * limit;
112
+ const [results, [{ count }]] = yield Promise.all([
113
+ this._knex('chat')
114
+ .where('userId', userId)
115
+ .where('status', 'active')
116
+ .orderBy('created_at', 'desc')
117
+ .limit(limit)
118
+ .offset(offset),
119
+ this._knex('chat')
120
+ .where('userId', userId)
121
+ .where('status', 'active')
122
+ .count('* as count')
123
+ ]);
124
+ const totalCount = parseInt(count);
125
+ return {
126
+ success: true,
127
+ data: results,
128
+ page,
129
+ limit,
130
+ count: results.length,
131
+ totalCount,
132
+ totalPages: Math.ceil(totalCount / limit)
133
+ };
134
+ });
135
+ }
136
+ }
137
+ exports.ChatDAO = ChatDAO;
138
+ //# sourceMappingURL=chat.dao.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"chat.dao.js","sourceRoot":"","sources":["../../../src/dao/chat/chat.dao.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAGA,0EAA+C;AAE/C,MAAa,OAAO;IAApB;QACU,UAAK,GAAyB,wBAAW,CAAC,aAAa,EAAE,CAAC;IAqHpE,CAAC;IAnHO,MAAM,CAAC,IAAiB;;YAC5B,MAAM,CAAC,MAAM,CAAC,GAAG,MAAM,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC;iBACtC,MAAM,CAAC,IAAI,CAAC;iBACZ,SAAS,CAAC,GAAG,CAAC,CAAC;YAClB,OAAO,MAAM,CAAC;QAChB,CAAC;KAAA;IAEK,OAAO,CAAC,EAAU;;YACtB,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC;iBACpC,KAAK,CAAC,IAAI,EAA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AAA;IAEK,iBAAiB;6DAAC,MAAc,EAAE,IAAI,GAAG,CAAC,EAAE,KAAK,GAAG,EAAE;YAC1D,MAAM,MAAM,GAAG,CAAC,IAAI,GAAG,CAAC,CAAC,GAAG,KAAK,CAAC;YAElC,MAAM,CAAC,OAAO,EAAE,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC,GAAG,MAAM,OAAO,CAAC,GAAG,CAAC;gBAC/C,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC;qBACf,KAAK,CAAC,QAAQ,EAAE,MAAM,CAAC;qBACvB,KAAK,CAAC,QAAQ,EAAE,QAAQ,CAAC;qBACzB,OAAO,CAAC,YAAY,EAAE,MAAM,CAAC;qBAC7B,KAAK,CAAC,KAAK,CAAC;qBACZ,MAAM,CAAC,MAAM,CAAC;gBACjB,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC;qBACf,KAAK,CAAC,QAAQ,EAAE,MAAM,CAAC;qBACvB,KAAK,CAAC,QAAQ,EAAE,QAAQ,CAAC;qBACzB,KAAK,CAAC,YAAY,CAAC;aACvB,CAAC,CAAC;YAEH,MAAM,UAAU,GAAG,QAAQ,CAAC,KAAe,CAAC,CAAC;YAC7C,OAAO;gBACL,OAAO,EAAE,IAAI;gBACb,IAAI,EAAE,OAAO;gBACb,IAAI;gBACJ,KAAK;gBACL,KAAK,EAAE,OAAO,CAAC,MAAM;gBACrB,UAAU;gBACV,UAAU,EAAE,IAAI,CAAC,IAAI,CAAC,UAAU,GAAG,KAAK,CAAC;aAC1C,CAAC;QACJ,CAAC;KAAA;CACF;AAtHD,0BAsHC"}
@@ -0,0 +1,13 @@
1
+ import { IBaseDAO, IDataPaginator } from "../../d.types";
2
+ import { IMessage, IMessageCreate, IMessageUpdate } from '../../interfaces/message/message.interfaces';
3
+ export declare class MessageDAO implements IBaseDAO<IMessage> {
4
+ private _knex;
5
+ create(item: IMessageCreate): Promise<IMessage>;
6
+ getById(id: number): Promise<IMessage | null>;
7
+ getByUuid(uuid: string): Promise<IMessage | null>;
8
+ getAll(page?: number, limit?: number): Promise<IDataPaginator<IMessage>>;
9
+ update(id: number, item: IMessageUpdate): Promise<IMessage | null>;
10
+ delete(id: number): Promise<boolean>;
11
+ getByChatId(chatId: number, page?: number, limit?: number): Promise<IDataPaginator<IMessage>>;
12
+ getLatestMessages(chatId: number, limit?: number): Promise<IMessage[]>;
13
+ }
@@ -0,0 +1,118 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.MessageDAO = void 0;
16
+ const KnexConnection_1 = __importDefault(require("../../KnexConnection"));
17
+ class MessageDAO {
18
+ constructor() {
19
+ this._knex = KnexConnection_1.default.getConnection();
20
+ }
21
+ create(item) {
22
+ return __awaiter(this, void 0, void 0, function* () {
23
+ const [result] = yield this._knex('message')
24
+ .insert(item)
25
+ .returning('*');
26
+ return result;
27
+ });
28
+ }
29
+ getById(id) {
30
+ return __awaiter(this, void 0, void 0, function* () {
31
+ const result = yield this._knex('message')
32
+ .where('id', id)
33
+ .first();
34
+ return result || null;
35
+ });
36
+ }
37
+ getByUuid(uuid) {
38
+ return __awaiter(this, void 0, void 0, function* () {
39
+ // Message table doesn't have uuid column, returning null
40
+ return null;
41
+ });
42
+ }
43
+ getAll() {
44
+ return __awaiter(this, arguments, void 0, function* (page = 1, limit = 10) {
45
+ const offset = (page - 1) * limit;
46
+ const [results, [{ count }]] = yield Promise.all([
47
+ this._knex('message')
48
+ .orderBy('created_at', 'asc')
49
+ .limit(limit)
50
+ .offset(offset),
51
+ this._knex('message').count('* as count')
52
+ ]);
53
+ const totalCount = parseInt(count);
54
+ return {
55
+ success: true,
56
+ data: results,
57
+ page,
58
+ limit,
59
+ count: results.length,
60
+ totalCount,
61
+ totalPages: Math.ceil(totalCount / limit)
62
+ };
63
+ });
64
+ }
65
+ update(id, item) {
66
+ return __awaiter(this, void 0, void 0, function* () {
67
+ const [result] = yield this._knex('message')
68
+ .where('id', id)
69
+ .update(Object.assign(Object.assign({}, item), { updated_at: new Date() }))
70
+ .returning('*');
71
+ return result || null;
72
+ });
73
+ }
74
+ delete(id) {
75
+ return __awaiter(this, void 0, void 0, function* () {
76
+ const result = yield this._knex('message')
77
+ .where('id', id)
78
+ .delete();
79
+ return result > 0;
80
+ });
81
+ }
82
+ getByChatId(chatId_1) {
83
+ return __awaiter(this, arguments, void 0, function* (chatId, page = 1, limit = 50) {
84
+ const offset = (page - 1) * limit;
85
+ const [results, [{ count }]] = yield Promise.all([
86
+ this._knex('message')
87
+ .where('chatId', chatId)
88
+ .orderBy('created_at', 'asc')
89
+ .limit(limit)
90
+ .offset(offset),
91
+ this._knex('message')
92
+ .where('chatId', chatId)
93
+ .count('* as count')
94
+ ]);
95
+ const totalCount = parseInt(count);
96
+ return {
97
+ success: true,
98
+ data: results,
99
+ page,
100
+ limit,
101
+ count: results.length,
102
+ totalCount,
103
+ totalPages: Math.ceil(totalCount / limit)
104
+ };
105
+ });
106
+ }
107
+ getLatestMessages(chatId_1) {
108
+ return __awaiter(this, arguments, void 0, function* (chatId, limit = 50) {
109
+ return yield this._knex('message')
110
+ .where('chatId', chatId)
111
+ .orderBy('created_at', 'desc')
112
+ .limit(limit)
113
+ .then((messages) => messages.reverse());
114
+ });
115
+ }
116
+ }
117
+ exports.MessageDAO = MessageDAO;
118
+ //# sourceMappingURL=message.dao.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"message.dao.js","sourceRoot":"","sources":["../../../src/dao/message/message.dao.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAGA,0EAA+C;AAE/C,MAAa,UAAU;IAAvB;QACU,UAAK,GAAyB,wBAAW,CAAC,aAAa,EAAE,CAAC;IA+FpE,CAAC;IA7FO,MAAM,CAAC,IAAoB;;YAC/B,MAAM,CAAC,MAAM,CAAC,GAAG,MAAM,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC;iBACzC,MAAM,CAAC,IAAI,CAAC;iBACZ,SAAS,CAAC,GAAG,CAAC,CAAC;YAClB,OAAO,MAAM,CAAC;QAChB,CAAC;KAAA;IAEK,OAAO,CAAC,EAAU;;YACtB,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC;iBACvC,KAAK,CAAC,IAAI,EAAE,EAAE,CAAC;iBACf,KAAK,EAAE,CAAC;YACX,OAAO,MAAM,IAAI,IAAI,CAAC;QACxB,CAAC;KAAA;IAEK,SAAS,CAAC,IAAY;;YAC1B,yDAAyD;YACzD,OAAO,IAAI,CAAC;QACd,CAAC;KAAA;IAEK,MAAM;6DAAC,IAAI,GAAG,CAAC,EAAE,KAAK,GAAG,EAAE;YAC/B,MAAM,MAAM,GAAG,CAAC,IAAI,GAAG,CAAC,CAAC,GAAG,KAAK,CAAC;YAElC,MAAM,CAAC,OAAO,EAAE,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC,GAAG,MAAM,OAAO,CAAC,GAAG,CAAC;gBAC/C,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC;qBAClB,OAAO,CAAC,YAAY,EAAE,KAAK,CAAC;qBAC5B,KAAK,CAAC,KAAK,CAAC;qBACZ,MAAM,CAAC,MAAM,CAAC;gBACjB,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC,KAAK,CAAC,YAAY,CAAC;aAC1C,CAAC,CAAC;YAE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,MAAM,CAAC,MAAM,CAAC;gBACjB,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC;qBAClB,KAAK,CAAC,QAAQ,EAAE,MAAM,CAAC;qBACvB,KAAK,CAAC,YAAY,CAAC;aACvB,CAAC,CAAC;YAEH,MAAM,UAAU,GAAG,QAAQ,CAAC,KAAe,CAAC,CAAC;YAC7C,OAAO;gBACL,OAAO,EAAE,IAAI;gBACb,IAAI,EAAE,OAAO;gBACb,IAAI;gBACJ,KAAK;gBACL,KAAK,EAAE,OAAO,CAAC,MAAM;gBACrB,UAAU;gBACV,UAAU,EAAE,IAAI,CAAC,IAAI,CAAC,UAAU,GAAG,KAAK,CAAC;aAC1C,CAAC;QACJ,CAAC;KAAA;IAEK,iBAAiB;6DAAC,MAAc,EAAE,KAAK,GAAG,EAAE;YAChD,OAAO,MAAM,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC;iBAC/B,KAAK,CAAC,QAAQ,EAAE,MAAM,CAAC;iBACvB,OAAO,CAAC,YAAY,EAAE,MAAM,CAAC;iBAC7B,KAAK,CAAC,KAAK,CAAC;iBACZ,IAAI,CAAC,CAAC,QAAoB,EAAE,EAAE,CAAC,QAAQ,CAAC,OAAO,EAAE,CAAC,CAAC;QACxD,CAAC;KAAA;CACF;AAhGD,gCAgGC"}
package/dist/index.d.ts CHANGED
@@ -4,6 +4,8 @@ export { FolderDAO } from "./dao/folder/folder.dao";
4
4
  export { VideoDAO } from "./dao/video/video.dao";
5
5
  export { AuthDAO } from "./dao/auth/auth.dao";
6
6
  export { UserPushNotificationTokenDAO } from "./dao/user-push-notification-token/user-push-notification-token.dao";
7
+ export { ChatDAO } from "./dao/chat/chat.dao";
8
+ export { MessageDAO } from "./dao/message/message.dao";
7
9
  export { IDataPaginator } from "./d.types";
8
10
  export { IUser } from "./interfaces/user/user.interfaces";
9
11
  export { IStudy } from "./interfaces/study/study.interfaces";
@@ -11,5 +13,7 @@ export { IFolder } from "./interfaces/folder/folder.interfaces";
11
13
  export { IVideo } from "./interfaces/video/video.interfaces";
12
14
  export { IAuth } from "./interfaces/auth/auth.interfaces";
13
15
  export { IUserPushNotificationToken } from "./interfaces/user-push-notification-token/user-push-notification-token.interfaces";
16
+ export { IChat, IChatCreate, IChatUpdate } from "./interfaces/chat/chat.interfaces";
17
+ export { IMessage, IMessageCreate, IMessageUpdate } from "./interfaces/message/message.interfaces";
14
18
  import KnexManager from "./KnexConnection";
15
19
  export { KnexManager };
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.UserPushNotificationTokenDAO = exports.AuthDAO = exports.VideoDAO = exports.FolderDAO = exports.StudyDAO = exports.UserDAO = void 0;
6
+ exports.KnexManager = exports.MessageDAO = exports.ChatDAO = exports.UserPushNotificationTokenDAO = exports.AuthDAO = exports.VideoDAO = exports.FolderDAO = exports.StudyDAO = exports.UserDAO = void 0;
7
7
  // DAOs
8
8
  var user_dao_1 = require("./dao/user/user.dao");
9
9
  Object.defineProperty(exports, "UserDAO", { enumerable: true, get: function () { return user_dao_1.UserDAO; } });
@@ -17,6 +17,10 @@ var auth_dao_1 = require("./dao/auth/auth.dao");
17
17
  Object.defineProperty(exports, "AuthDAO", { enumerable: true, get: function () { return auth_dao_1.AuthDAO; } });
18
18
  var user_push_notification_token_dao_1 = require("./dao/user-push-notification-token/user-push-notification-token.dao");
19
19
  Object.defineProperty(exports, "UserPushNotificationTokenDAO", { enumerable: true, get: function () { return user_push_notification_token_dao_1.UserPushNotificationTokenDAO; } });
20
+ var chat_dao_1 = require("./dao/chat/chat.dao");
21
+ Object.defineProperty(exports, "ChatDAO", { enumerable: true, get: function () { return chat_dao_1.ChatDAO; } });
22
+ var message_dao_1 = require("./dao/message/message.dao");
23
+ Object.defineProperty(exports, "MessageDAO", { enumerable: true, get: function () { return message_dao_1.MessageDAO; } });
20
24
  const KnexConnection_1 = __importDefault(require("./KnexConnection"));
21
25
  exports.KnexManager = KnexConnection_1.default;
22
26
  //# 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,mDAAiD;AAAxC,qGAAA,QAAQ,OAAA;AACjB,gDAA8C;AAArC,mGAAA,OAAO,OAAA;AAChB,wHAAmH;AAA1G,gJAAA,4BAA4B,OAAA;AAWrC,sEAA2C;AAClC,sBADF,wBAAW,CACE"}
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,mDAAiD;AAAxC,qGAAA,QAAQ,OAAA;AACjB,gDAA8C;AAArC,mGAAA,OAAO,OAAA;AAChB,wHAAmH;AAA1G,gJAAA,4BAA4B,OAAA;AACrC,gDAA8C;AAArC,mGAAA,OAAO,OAAA;AAChB,yDAAuD;AAA9C,yGAAA,UAAU,OAAA;AAanB,sEAA2C;AAClC,sBADF,wBAAW,CACE"}
@@ -0,0 +1,18 @@
1
+ export interface IChat {
2
+ id?: number;
3
+ uuid?: string;
4
+ userId: number;
5
+ title: string;
6
+ status?: 'active' | 'archived';
7
+ created_at?: Date;
8
+ updated_at?: Date;
9
+ }
10
+ export interface IChatCreate {
11
+ userId: number;
12
+ title: string;
13
+ status?: 'active' | 'archived';
14
+ }
15
+ export interface IChatUpdate {
16
+ title?: string;
17
+ status?: 'active' | 'archived';
18
+ }
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=chat.interfaces.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"chat.interfaces.js","sourceRoot":"","sources":["../../../src/interfaces/chat/chat.interfaces.ts"],"names":[],"mappings":""}
@@ -0,0 +1,16 @@
1
+ export interface IMessage {
2
+ id?: number;
3
+ chatId: number;
4
+ role: 'user' | 'assistant';
5
+ content: string;
6
+ created_at?: Date;
7
+ updated_at?: Date;
8
+ }
9
+ export interface IMessageCreate {
10
+ chatId: number;
11
+ role: 'user' | 'assistant';
12
+ content: string;
13
+ }
14
+ export interface IMessageUpdate {
15
+ content?: string;
16
+ }
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=message.interfaces.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"message.interfaces.js","sourceRoot":"","sources":["../../../src/interfaces/message/message.interfaces.ts"],"names":[],"mappings":""}
@@ -0,0 +1,43 @@
1
+ import type { Knex } from "knex";
2
+
3
+
4
+ export async function up(knex: Knex): Promise<void> {
5
+ // Create chat table
6
+ await knex.schema.createTable('chat', (table) => {
7
+ table.increments('id').primary();
8
+ table.integer('userId').unsigned().notNullable();
9
+ table.string('title', 255).notNullable();
10
+ table.enum('status', ['active', 'archived']).defaultTo('active');
11
+ table.timestamps(true, true);
12
+
13
+ // Foreign key
14
+ table.foreign('userId').references('id').inTable('users').onDelete('CASCADE');
15
+
16
+ // Indexes
17
+ table.index('userId');
18
+ table.index('status');
19
+ });
20
+
21
+ // Create message table
22
+ await knex.schema.createTable('message', (table) => {
23
+ table.increments('id').primary();
24
+ table.integer('chatId').unsigned().notNullable();
25
+ table.enum('role', ['user', 'assistant']).notNullable();
26
+ table.text('content').notNullable();
27
+ table.timestamps(true, true);
28
+
29
+ // Foreign key
30
+ table.foreign('chatId').references('id').inTable('chat').onDelete('CASCADE');
31
+
32
+ // Indexes
33
+ table.index('chatId');
34
+ table.index('created_at');
35
+ });
36
+ }
37
+
38
+
39
+ export async function down(knex: Knex): Promise<void> {
40
+ await knex.schema.dropTableIfExists('message');
41
+ await knex.schema.dropTableIfExists('chat');
42
+ }
43
+
@@ -0,0 +1,18 @@
1
+ import type { Knex } from "knex";
2
+
3
+
4
+ export async function up(knex: Knex): Promise<void> {
5
+ // Add uuid column to chat table
6
+ await knex.schema.alterTable('chat', (table) => {
7
+ table.uuid('uuid').defaultTo(knex.raw('gen_random_uuid()')).notNullable();
8
+ table.index('uuid');
9
+ });
10
+ }
11
+
12
+
13
+ export async function down(knex: Knex): Promise<void> {
14
+ await knex.schema.alterTable('chat', (table) => {
15
+ table.dropColumn('uuid');
16
+ });
17
+ }
18
+
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trafficgroup/knex-rel",
3
- "version": "0.0.25",
3
+ "version": "0.0.27",
4
4
  "description": "Knex Module",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -0,0 +1,124 @@
1
+ import { Knex } from "knex";
2
+ import { IBaseDAO, IDataPaginator } from "../../d.types";
3
+ import { IChat, IChatCreate, IChatUpdate } from '../../interfaces/chat/chat.interfaces';
4
+ import KnexManager from "../../KnexConnection";
5
+
6
+ export class ChatDAO implements IBaseDAO<IChat> {
7
+ private _knex: Knex<any, unknown[]> = KnexManager.getConnection();
8
+
9
+ async create(item: IChatCreate): Promise<IChat> {
10
+ const [result] = await this._knex('chat')
11
+ .insert(item)
12
+ .returning('*');
13
+ return result;
14
+ }
15
+
16
+ async getById(id: number): Promise<IChat | null> {
17
+ const result = await this._knex('chat')
18
+ .where('id', id)
19
+ .first();
20
+ return result || null;
21
+ }
22
+
23
+ async getByUuid(uuid: string): Promise<IChat | null> {
24
+ const result = await this._knex('chat')
25
+ .where('uuid', uuid)
26
+ .first();
27
+ return result || null;
28
+ }
29
+
30
+ async getAll(page = 1, limit = 10): Promise<IDataPaginator<IChat>> {
31
+ const offset = (page - 1) * limit;
32
+
33
+ const [results, [{ count }]] = await Promise.all([
34
+ this._knex('chat')
35
+ .orderBy('created_at', 'desc')
36
+ .limit(limit)
37
+ .offset(offset),
38
+ this._knex('chat').count('* as count')
39
+ ]);
40
+
41
+ const totalCount = parseInt(count as string);
42
+ return {
43
+ success: true,
44
+ data: results,
45
+ page,
46
+ limit,
47
+ count: results.length,
48
+ totalCount,
49
+ totalPages: Math.ceil(totalCount / limit)
50
+ };
51
+ }
52
+
53
+ async update(id: number, item: IChatUpdate): Promise<IChat | null> {
54
+ const [result] = await this._knex('chat')
55
+ .where('id', id)
56
+ .update({
57
+ ...item,
58
+ updated_at: new Date()
59
+ })
60
+ .returning('*');
61
+ return result || null;
62
+ }
63
+
64
+ async delete(id: number): Promise<boolean> {
65
+ const result = await this._knex('chat')
66
+ .where('id', id)
67
+ .delete();
68
+ return result > 0;
69
+ }
70
+
71
+ async getByUserId(userId: number, page = 1, limit = 10): Promise<IDataPaginator<IChat>> {
72
+ const offset = (page - 1) * limit;
73
+
74
+ const [results, [{ count }]] = await Promise.all([
75
+ this._knex('chat')
76
+ .where('userId', userId)
77
+ .orderBy('created_at', 'desc')
78
+ .limit(limit)
79
+ .offset(offset),
80
+ this._knex('chat')
81
+ .where('userId', userId)
82
+ .count('* as count')
83
+ ]);
84
+
85
+ const totalCount = parseInt(count as string);
86
+ return {
87
+ success: true,
88
+ data: results,
89
+ page,
90
+ limit,
91
+ count: results.length,
92
+ totalCount,
93
+ totalPages: Math.ceil(totalCount / limit)
94
+ };
95
+ }
96
+
97
+ async getActiveByUserId(userId: number, page = 1, limit = 10): Promise<IDataPaginator<IChat>> {
98
+ const offset = (page - 1) * limit;
99
+
100
+ const [results, [{ count }]] = await Promise.all([
101
+ this._knex('chat')
102
+ .where('userId', userId)
103
+ .where('status', 'active')
104
+ .orderBy('created_at', 'desc')
105
+ .limit(limit)
106
+ .offset(offset),
107
+ this._knex('chat')
108
+ .where('userId', userId)
109
+ .where('status', 'active')
110
+ .count('* as count')
111
+ ]);
112
+
113
+ const totalCount = parseInt(count as string);
114
+ return {
115
+ success: true,
116
+ data: results,
117
+ page,
118
+ limit,
119
+ count: results.length,
120
+ totalCount,
121
+ totalPages: Math.ceil(totalCount / limit)
122
+ };
123
+ }
124
+ }
@@ -0,0 +1,102 @@
1
+ import { Knex } from "knex";
2
+ import { IBaseDAO, IDataPaginator } from "../../d.types";
3
+ import { IMessage, IMessageCreate, IMessageUpdate } from '../../interfaces/message/message.interfaces';
4
+ import KnexManager from "../../KnexConnection";
5
+
6
+ export class MessageDAO implements IBaseDAO<IMessage> {
7
+ private _knex: Knex<any, unknown[]> = KnexManager.getConnection();
8
+
9
+ async create(item: IMessageCreate): Promise<IMessage> {
10
+ const [result] = await this._knex('message')
11
+ .insert(item)
12
+ .returning('*');
13
+ return result;
14
+ }
15
+
16
+ async getById(id: number): Promise<IMessage | null> {
17
+ const result = await this._knex('message')
18
+ .where('id', id)
19
+ .first();
20
+ return result || null;
21
+ }
22
+
23
+ async getByUuid(uuid: string): Promise<IMessage | null> {
24
+ // Message table doesn't have uuid column, returning null
25
+ return null;
26
+ }
27
+
28
+ async getAll(page = 1, limit = 10): Promise<IDataPaginator<IMessage>> {
29
+ const offset = (page - 1) * limit;
30
+
31
+ const [results, [{ count }]] = await Promise.all([
32
+ this._knex('message')
33
+ .orderBy('created_at', 'asc')
34
+ .limit(limit)
35
+ .offset(offset),
36
+ this._knex('message').count('* as count')
37
+ ]);
38
+
39
+ const totalCount = parseInt(count as string);
40
+ return {
41
+ success: true,
42
+ data: results,
43
+ page,
44
+ limit,
45
+ count: results.length,
46
+ totalCount,
47
+ totalPages: Math.ceil(totalCount / limit)
48
+ };
49
+ }
50
+
51
+ async update(id: number, item: IMessageUpdate): Promise<IMessage | null> {
52
+ const [result] = await this._knex('message')
53
+ .where('id', id)
54
+ .update({
55
+ ...item,
56
+ updated_at: new Date()
57
+ })
58
+ .returning('*');
59
+ return result || null;
60
+ }
61
+
62
+ async delete(id: number): Promise<boolean> {
63
+ const result = await this._knex('message')
64
+ .where('id', id)
65
+ .delete();
66
+ return result > 0;
67
+ }
68
+
69
+ async getByChatId(chatId: number, page = 1, limit = 50): Promise<IDataPaginator<IMessage>> {
70
+ const offset = (page - 1) * limit;
71
+
72
+ const [results, [{ count }]] = await Promise.all([
73
+ this._knex('message')
74
+ .where('chatId', chatId)
75
+ .orderBy('created_at', 'asc')
76
+ .limit(limit)
77
+ .offset(offset),
78
+ this._knex('message')
79
+ .where('chatId', chatId)
80
+ .count('* as count')
81
+ ]);
82
+
83
+ const totalCount = parseInt(count as string);
84
+ return {
85
+ success: true,
86
+ data: results,
87
+ page,
88
+ limit,
89
+ count: results.length,
90
+ totalCount,
91
+ totalPages: Math.ceil(totalCount / limit)
92
+ };
93
+ }
94
+
95
+ async getLatestMessages(chatId: number, limit = 50): Promise<IMessage[]> {
96
+ return await this._knex('message')
97
+ .where('chatId', chatId)
98
+ .orderBy('created_at', 'desc')
99
+ .limit(limit)
100
+ .then((messages: IMessage[]) => messages.reverse());
101
+ }
102
+ }
package/src/index.ts CHANGED
@@ -5,6 +5,8 @@ export { FolderDAO } from "./dao/folder/folder.dao";
5
5
  export { VideoDAO } from "./dao/video/video.dao";
6
6
  export { AuthDAO } from "./dao/auth/auth.dao";
7
7
  export { UserPushNotificationTokenDAO } from "./dao/user-push-notification-token/user-push-notification-token.dao";
8
+ export { ChatDAO } from "./dao/chat/chat.dao";
9
+ export { MessageDAO } from "./dao/message/message.dao";
8
10
 
9
11
  // Interfaces
10
12
  export { IDataPaginator } from "./d.types";
@@ -14,6 +16,8 @@ export { IFolder } from "./interfaces/folder/folder.interfaces";
14
16
  export { IVideo } from "./interfaces/video/video.interfaces";
15
17
  export { IAuth } from "./interfaces/auth/auth.interfaces";
16
18
  export { IUserPushNotificationToken } from "./interfaces/user-push-notification-token/user-push-notification-token.interfaces";
19
+ export { IChat, IChatCreate, IChatUpdate } from "./interfaces/chat/chat.interfaces";
20
+ export { IMessage, IMessageCreate, IMessageUpdate } from "./interfaces/message/message.interfaces";
17
21
 
18
22
  import KnexManager from "./KnexConnection";
19
23
  export { KnexManager };
@@ -0,0 +1,20 @@
1
+ export interface IChat {
2
+ id?: number;
3
+ uuid?: string;
4
+ userId: number;
5
+ title: string;
6
+ status?: 'active' | 'archived';
7
+ created_at?: Date;
8
+ updated_at?: Date;
9
+ }
10
+
11
+ export interface IChatCreate {
12
+ userId: number;
13
+ title: string;
14
+ status?: 'active' | 'archived';
15
+ }
16
+
17
+ export interface IChatUpdate {
18
+ title?: string;
19
+ status?: 'active' | 'archived';
20
+ }
@@ -0,0 +1,18 @@
1
+ export interface IMessage {
2
+ id?: number;
3
+ chatId: number;
4
+ role: 'user' | 'assistant';
5
+ content: string;
6
+ created_at?: Date;
7
+ updated_at?: Date;
8
+ }
9
+
10
+ export interface IMessageCreate {
11
+ chatId: number;
12
+ role: 'user' | 'assistant';
13
+ content: string;
14
+ }
15
+
16
+ export interface IMessageUpdate {
17
+ content?: string;
18
+ }