@astratra/store-postgres 1.1.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/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2026 Astratra
3
+ Copyright (c) 2026 Astratra contributors
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "@astratra/store-postgres",
3
- "version": "1.1.0",
3
+ "version": "1.2.1",
4
4
  "description": "PostgreSQL persistence adapters for Astratra SaaS stores.",
5
5
  "engines": {
6
- "node": ">=20"
6
+ "node": ">=22"
7
7
  },
8
8
  "main": "src/index.js",
9
9
  "types": "src/index.d.ts",
@@ -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
@@ -6,8 +6,8 @@ const DEFAULT_SETTINGS_TABLE = 'astratra_settings';
6
6
  function loadPg() {
7
7
  try {
8
8
  return require('pg');
9
- } catch (_error) {
10
- throw new Error('@astratra/store-postgres requires pg. Install pg or pass it from an app that already depends on it.');
9
+ } catch (error) {
10
+ throw new Error('@astratra/store-postgres requires pg. Install pg or pass it from an app that already depends on it.', { cause: error });
11
11
  }
12
12
  }
13
13
 
@@ -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
- const existing = await pool.query(`SELECT id, data FROM ${table} WHERE id = $1 LIMIT 1`, [id]);
162
- if (existing.rows.length === 0) return null;
163
-
164
- const mergedData = { ...existing.rows[0].data, ...safePatch };
165
- const email = mergedData.email ?? null;
166
- const role = mergedData.role ?? null;
167
-
168
- const result = await pool.query(
169
- `UPDATE ${table} SET email = $1, role = $2, data = $3 WHERE id = $4 RETURNING id, data`,
170
- [email, role, JSON.stringify(mergedData), id]
171
- );
172
- return mergeUserRow(result.rows[0]);
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;
package/src/migrations.js CHANGED
@@ -87,7 +87,7 @@ function createPostgresMigrationRunner(options = {}) {
87
87
  applied.push(migration.id);
88
88
  } catch (error) {
89
89
  await client.query('ROLLBACK');
90
- throw new Error(`Migration "${migration.id}" failed and was rolled back: ${error.message}`);
90
+ throw new Error(`Migration "${migration.id}" failed and was rolled back: ${error.message}`, { cause: error });
91
91
  } finally {
92
92
  client.release();
93
93
  }