db-crud-wrapper 1.0.1

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,196 @@
1
+ 'use strict';
2
+
3
+ import { config } from './config.js';
4
+ import session from 'express-session';
5
+ // import * as Factory from './factory.js';
6
+ import { GetBuilder, PutBuilder, DeleteBuilder, PatchBuilder } from './builder.js';
7
+ import log from './log.js';
8
+
9
+ // Custom Store per express-session
10
+ export default class sessionStore extends session.Store {
11
+
12
+ #getSession;
13
+ #putSession;
14
+ #delSessions;
15
+ #countSessions;
16
+ #getAllSessions;
17
+ #patchSession;
18
+ #tablePath;
19
+
20
+ /**
21
+ * sessionStore
22
+ * @param {Object} options - express-session options + tablePath
23
+ * @param {string} [options.tablePath] - 'server.database.table', default is 'sessions'.
24
+ * @param {...} [options.others] - (other options for 'express-session').
25
+ */
26
+ constructor(options) {
27
+ super(options);
28
+ if (options?.tablePath && typeof options.tablePath === 'string' && options.tablePath.length > 0) {
29
+ this.#tablePath = options.tablePath;
30
+ } else {
31
+ this.#tablePath = config.session.tablePath;
32
+ }
33
+ // this.sessions = Factory.newFullApi(schema.session.tablePath);
34
+ this.#getSession = new GetBuilder(this.#tablePath);
35
+ this.#putSession = new PutBuilder(this.#tablePath);
36
+ this.#delSessions = new DeleteBuilder(this.#tablePath);
37
+ this.#countSessions = new GetBuilder(this.#tablePath);
38
+ this.#getAllSessions = new GetBuilder(this.#tablePath);
39
+ this.#patchSession = new PatchBuilder(this.#tablePath);
40
+ }
41
+
42
+ /**
43
+ * get session.
44
+ * @param {string} sid - L'ID univoco della sessione (Session ID).
45
+ * @param {function} callback - Funzione di callback: function(err, session).
46
+ */
47
+ get(sid, callback) {
48
+
49
+ log(`Get session '${sid}' from store.`, 30);
50
+ // this.sessions.getById(sid, { appLog: false })
51
+ this.#getSession.byId(sid).log(false).execute()
52
+ .then((data) => {
53
+ if (data) {
54
+ try {
55
+ const _sessionData = Array.isArray(data) ? JSON.parse(data[0].SessionData) : JSON.parse(data.SessionData);
56
+ callback(null, _sessionData);
57
+ } catch (error) { callback(error, null); }
58
+ }
59
+ else { callback(null, null); }
60
+ })
61
+ .catch((error) => {
62
+ callback(error, null);
63
+ });
64
+
65
+ }
66
+
67
+ /**
68
+ * set session.
69
+ * @param {string} sid - L'ID univoco della sessione.
70
+ * @param {object} session - L'oggetto sessione da salvare (incl. cookie).
71
+ * @param {function} callback - Funzione di callback: function(err).
72
+ */
73
+ set(sid, session, callback) {
74
+ log(`Set session '${sid}' in store.`, 30);
75
+
76
+ // Costruisci l'oggetto sessione da salvare
77
+ const _session = {
78
+ SessionData: JSON.stringify(session),
79
+ Expires: session.cookie.expires ? new Date(session.cookie.expires) : null
80
+ };
81
+
82
+ // Salva la sessione nel DB (disabilito logging).
83
+ // this.sessions.putById({ put: { values: _session }, appLog: false }, sid)
84
+ this.#putSession.byId(sid).values(_session).log(false).execute()
85
+ .then(() => {
86
+ callback(null); // Successo
87
+ })
88
+ .catch((error) => {
89
+ callback(error); // Errore
90
+ });
91
+ }
92
+
93
+ /**
94
+ * destroy session.
95
+ * @param {string} sid - L'ID univoco della sessione da cancellare.
96
+ * @param {function} callback - Funzione di callback: function(err).
97
+ */
98
+ destroy(sid, callback) {
99
+ log(`Destroy session '${sid}' from store.`, 30);
100
+ // this.sessions.delById(sid, { appLog: false })
101
+ this.#delSessions.byId(sid).log(false).execute()
102
+ .then(() => {
103
+ callback(null); // Successo
104
+ })
105
+ .catch((error) => {
106
+ callback(error); // Errore
107
+ });
108
+ }
109
+
110
+ /**
111
+ * clear sessions (delete all sessions).
112
+ * @param {function} callback - Funzione di callback: function(err).
113
+ */
114
+ clear(callback) {
115
+ log(`Clear all sessions from store.`, 30);
116
+ // this.sessions.delByFilter({ delete: { filters: [] }, appLog: false })
117
+ this.#delSessions.log(false).execute()
118
+ .then(() => {
119
+ callback(null); // Successo
120
+ })
121
+ .catch((error) => {
122
+ callback(error); // Errore
123
+ });
124
+ }
125
+
126
+ /**
127
+ * length sessions (count num of session).
128
+ * @param {function} callback - Funzione di callback: function(err).
129
+ */
130
+ length(callback) {
131
+ log(`Count all sessions from store.`, 30);
132
+ // this.sessions.getByFilter({ get: { fields: ['count(*) as NumOfRecords'], filters: [] }, appLog: false })
133
+ this.#countSessions.fields(['count(*) as NumOfRecords']).log(false).execute()
134
+ .then((_sessions) => {
135
+ try {
136
+ const _count = (_sessions && _sessions.length > 0 && _sessions[0].NumOfRecords) ? parseInt(_sessions[0].NumOfRecords) : 0;
137
+ callback(null, _count);
138
+ } catch (error) {
139
+ callback(error); // Errore
140
+ }
141
+ })
142
+ .catch((error) => {
143
+ callback(error); // Errore
144
+ });
145
+ }
146
+
147
+ /**
148
+ * get all sessions.
149
+ * @param {function} callback - Funzione di callback: function(err).
150
+ */
151
+ all(callback) {
152
+ log(`Get all sessions from store.`, 30);
153
+ const now = new Date();
154
+ // this.sessions.getByFilter({ get: { fields: ['*'] }, appLog: false })
155
+ this.#getAllSessions.log(false).execute()
156
+ .then((_sessions) => {
157
+ try {
158
+ let _result = [];
159
+ if (_sessions && _sessions.length > 0) {
160
+ _result = _sessions.map(item => {
161
+ return JSON.parse(item.SessionData);
162
+ });
163
+ }
164
+ callback(null, _result);
165
+ } catch (error) {
166
+ callback(error); // Errore
167
+ }
168
+ })
169
+ .catch((error) => {
170
+ callback(error); // Errore
171
+ });
172
+ }
173
+
174
+ /**
175
+ * touch session.
176
+ * Usato quando la sessione non viene modificata ma l'utente la sta ancora usando.
177
+ * @param {string} sid - L'ID della sessione.
178
+ * @param {object} session - L'oggetto sessione con il campo cookie aggiornato (expires).
179
+ * @param {function} callback - Funzione di callback: function(err).
180
+ */
181
+ touch(sid, session, callback) {
182
+ log(`Touch session '${sid}' in store.`, 30);
183
+ const _values = {
184
+ Expires: session.cookie.expires ? new Date(session.cookie.expires) : null
185
+ };
186
+ // this.sessions.patchById({ patch: { values: _values }, appLog: false }, sid)
187
+ this.#patchSession.byId(sid).values(_values).log(false).execute()
188
+ .then(() => {
189
+ callback(null); // Successo
190
+ })
191
+ .catch((error) => {
192
+ callback(error); // Errore
193
+ });
194
+ }
195
+
196
+ }
package/package.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "db-crud-wrapper",
3
+ "version": "1.0.1",
4
+ "type": "module",
5
+ "description": "Wrapper for database CRUD operations",
6
+ "main": "index.js",
7
+ "scripts": {
8
+ "test": "node ./test.js"
9
+ },
10
+ "author": "FF",
11
+ "license": "MIT",
12
+ "dependencies": {
13
+ "mssql": "^12.0.0",
14
+ "mysql2": "^3.15.3",
15
+ "uuid": "^11.1.1",
16
+ "express-session": "^1.18.2"
17
+ },
18
+ "keywords": [
19
+ "db",
20
+ "database",
21
+ "crud",
22
+ "wrapper"
23
+ ],
24
+ "files": [
25
+ "lib/",
26
+ "CHANGELOG.md",
27
+ "README.md",
28
+ "index.js",
29
+ "package.json"
30
+ ]
31
+ }