@astratra/store-postgres 1.2.0 → 1.2.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.
- package/package.json +5 -5
- package/src/index.js +22 -12
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@astratra/store-postgres",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.1",
|
|
4
4
|
"description": "PostgreSQL persistence adapters for Astratra SaaS stores.",
|
|
5
5
|
"engines": {
|
|
6
6
|
"node": ">=22"
|
|
@@ -11,9 +11,6 @@
|
|
|
11
11
|
"files": [
|
|
12
12
|
"src"
|
|
13
13
|
],
|
|
14
|
-
"scripts": {
|
|
15
|
-
"test": "jest"
|
|
16
|
-
},
|
|
17
14
|
"jest": {
|
|
18
15
|
"testMatch": [
|
|
19
16
|
"**/__tests__/**/*.test.js"
|
|
@@ -42,5 +39,8 @@
|
|
|
42
39
|
"jest": "30.4.2",
|
|
43
40
|
"pg": "^8.13.1",
|
|
44
41
|
"pg-mem": "^3.0.5"
|
|
42
|
+
},
|
|
43
|
+
"scripts": {
|
|
44
|
+
"test": "jest"
|
|
45
45
|
}
|
|
46
|
-
}
|
|
46
|
+
}
|
package/src/index.js
CHANGED
|
@@ -53,6 +53,9 @@ function normalizeEmail(email) {
|
|
|
53
53
|
return String(email || '').trim().toLowerCase();
|
|
54
54
|
}
|
|
55
55
|
|
|
56
|
+
/* Combien de fois une mise à jour relit le compte quand une autre écriture est passée entre sa lecture et son écriture. */
|
|
57
|
+
const UPDATE_ATTEMPTS = 20;
|
|
58
|
+
|
|
56
59
|
function createPostgresUsersStore(options = {}) {
|
|
57
60
|
const table = options.usersTable || DEFAULT_USERS_TABLE;
|
|
58
61
|
assertValidIdentifier(table, 'usersTable');
|
|
@@ -158,18 +161,25 @@ function createPostgresUsersStore(options = {}) {
|
|
|
158
161
|
}
|
|
159
162
|
|
|
160
163
|
try {
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
164
|
+
/* La ligne est relue, fusionnée, puis réécrite SEULEMENT si elle n'a pas changé entre-temps (comparaison de
|
|
165
|
+
son contenu dans le même UPDATE). Sans cette condition, deux écritures simultanées du même compte lisaient
|
|
166
|
+
toutes deux l'ancien compte et la seconde effaçait la première : un rôle retiré revenait. Si la ligne a
|
|
167
|
+
changé, on relit et on recommence. */
|
|
168
|
+
for (let attempt = 0; attempt < UPDATE_ATTEMPTS; attempt += 1) {
|
|
169
|
+
const existing = await pool.query(`SELECT id, data FROM ${table} WHERE id = $1 LIMIT 1`, [id]);
|
|
170
|
+
if (existing.rows.length === 0) return null;
|
|
171
|
+
|
|
172
|
+
const mergedData = { ...existing.rows[0].data, ...safePatch };
|
|
173
|
+
const email = mergedData.email ?? null;
|
|
174
|
+
const role = mergedData.role ?? null;
|
|
175
|
+
|
|
176
|
+
const result = await pool.query(
|
|
177
|
+
`UPDATE ${table} SET email = $1, role = $2, data = $3 WHERE id = $4 AND data = $5::jsonb RETURNING id, data`,
|
|
178
|
+
[email, role, JSON.stringify(mergedData), id, JSON.stringify(existing.rows[0].data)]
|
|
179
|
+
);
|
|
180
|
+
if (result.rows.length > 0) return mergeUserRow(result.rows[0]);
|
|
181
|
+
}
|
|
182
|
+
throw new Error('@astratra/store-postgres: the account kept changing while it was being updated.');
|
|
173
183
|
} catch (error) {
|
|
174
184
|
if (isInvalidTextRepresentation(error)) return null;
|
|
175
185
|
throw error;
|