db-crud-api 0.3.15 → 0.3.19

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,165 @@
1
+ 'use strict';
2
+
3
+ import schema from './schema.js';
4
+ import session from 'express-session';
5
+ import * as apiFactory from './api-factory.js';
6
+ import log from './log.js';
7
+
8
+ // Custom Store per express-session
9
+ export default class sessionStore extends session.Store {
10
+ constructor(options) {
11
+ super(options);
12
+ if (options?.tablePath && typeof options.tablePath === 'string' && options.tablePath.length > 0) {
13
+ schema.config.session.tablePath = options.tablePath;
14
+ }
15
+ this.sessions = apiFactory.newFullApi(schema.config.session.tablePath);
16
+ }
17
+
18
+ /**
19
+ * get session.
20
+ * @param {string} sid - L'ID univoco della sessione (Session ID).
21
+ * @param {function} callback - Funzione di callback: function(err, session).
22
+ */
23
+ get(sid, callback) {
24
+
25
+ log(`Get session '${sid}' from store.`, 30);
26
+ this.sessions.getById(sid)
27
+ .then((data) => {
28
+ if (data) {
29
+ try {
30
+ const _sessionData = JSON.parse(data.SessionData);
31
+ callback(null, _sessionData);
32
+ } catch (error) { callback(error, null); }
33
+ }
34
+ else { callback(null, null); }
35
+ })
36
+ .catch((error) => {
37
+ callback(error, null);
38
+ });
39
+
40
+ }
41
+
42
+ /**
43
+ * set session.
44
+ * @param {string} sid - L'ID univoco della sessione.
45
+ * @param {object} session - L'oggetto sessione da salvare (incl. cookie).
46
+ * @param {function} callback - Funzione di callback: function(err).
47
+ */
48
+ set(sid, session, callback) {
49
+ log(`Set session '${sid}' in store.`, 30);
50
+
51
+ // Costruisci l'oggetto sessione da salvare
52
+ const _session = {
53
+ SessionData: JSON.stringify(session),
54
+ Expires: session.cookie.expires ? new Date(session.cookie.expires).toISOString() : null
55
+ };
56
+
57
+ // Salva la sessione nel DB
58
+ this.sessions.putById({ put: { sets: _session } }, sid)
59
+ .then(() => {
60
+ callback(null); // Successo
61
+ })
62
+ .catch((error) => {
63
+ callback(error); // Errore
64
+ });
65
+ }
66
+
67
+ /**
68
+ * destroy session.
69
+ * @param {string} sid - L'ID univoco della sessione da cancellare.
70
+ * @param {function} callback - Funzione di callback: function(err).
71
+ */
72
+ destroy(sid, callback) {
73
+ log(`Destroy session '${sid}' from store.`, 30);
74
+ this.sessions.delById(sid)
75
+ .then(() => {
76
+ callback(null); // Successo
77
+ })
78
+ .catch((error) => {
79
+ callback(error); // Errore
80
+ });
81
+ }
82
+
83
+ /**
84
+ * clear sessions (delete all sessions).
85
+ * @param {function} callback - Funzione di callback: function(err).
86
+ */
87
+ clear(callback) {
88
+ log(`Clear all sessions from store.`, 30);
89
+ this.sessions.delByFilter({ delete: { filters: [] } })
90
+ .then(() => {
91
+ callback(null); // Successo
92
+ })
93
+ .catch((error) => {
94
+ callback(error); // Errore
95
+ });
96
+ }
97
+
98
+ /**
99
+ * length sessions (count num of session).
100
+ * @param {function} callback - Funzione di callback: function(err).
101
+ */
102
+ length(callback) {
103
+ log(`Count all sessions from store.`, 30);
104
+ this.sessions.getByFilter({ get: { fields: ['count(*) as NumOfRecords'], filters: [] } })
105
+ .then((_sessions) => {
106
+ try {
107
+ const _count = (_sessions && _sessions.length > 0 && _sessions[0].NumOfRecords) ? parseInt(_sessions[0].NumOfRecords) : 0;
108
+ callback(null, _count);
109
+ } catch (error) {
110
+ callback(error); // Errore
111
+ }
112
+ })
113
+ .catch((error) => {
114
+ callback(error); // Errore
115
+ });
116
+ }
117
+
118
+ /**
119
+ * get all sessions.
120
+ * @param {function} callback - Funzione di callback: function(err).
121
+ */
122
+ all(callback) {
123
+ log(`Get all sessions from store.`, 30);
124
+ const now = new Date();
125
+ this.sessions.getByFilter({ get: { fields: ['*'] } })
126
+ .then((_sessions) => {
127
+ try {
128
+ let _result = [];
129
+ if (_sessions && _sessions.length > 0) {
130
+ _result = _sessions.map(item => {
131
+ return JSON.parse(item.SessionData);
132
+ });
133
+ }
134
+ callback(null, _result);
135
+ } catch (error) {
136
+ callback(error); // Errore
137
+ }
138
+ })
139
+ .catch((error) => {
140
+ callback(error); // Errore
141
+ });
142
+ }
143
+
144
+ /**
145
+ * touch session.
146
+ * Usato quando la sessione non viene modificata ma l'utente la sta ancora usando.
147
+ * @param {string} sid - L'ID della sessione.
148
+ * @param {object} session - L'oggetto sessione con il campo cookie aggiornato (expires).
149
+ * @param {function} callback - Funzione di callback: function(err).
150
+ */
151
+ touch(sid, session, callback) {
152
+ log(`Touch session '${sid}' in store.`, 30);
153
+ const _sets = {
154
+ Expires: session.cookie.expires ? new Date(session.cookie.expires).toISOString() : null
155
+ };
156
+ this.sessions.patchById({ patch: { sets: _sets } }, sid)
157
+ .then(() => {
158
+ callback(null); // Successo
159
+ })
160
+ .catch((error) => {
161
+ callback(error); // Errore
162
+ });
163
+ }
164
+
165
+ }