@astratra/store-postgres 1.0.0 → 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.
- package/package.json +1 -1
- package/src/index.d.ts +4 -2
- package/src/index.js +34 -3
package/package.json
CHANGED
package/src/index.d.ts
CHANGED
|
@@ -43,6 +43,8 @@ export interface PostgresUsersStore<TUser extends PostgresUserBase = PostgresUse
|
|
|
43
43
|
findById(id: string): Promise<TUser | null>;
|
|
44
44
|
create(userData: Partial<TUser> & Record<string, unknown>): Promise<TUser>;
|
|
45
45
|
list(options?: UsersListOptions): Promise<TUser[]>;
|
|
46
|
+
count(options?: Pick<UsersListOptions, 'role'>): Promise<number>;
|
|
47
|
+
countByRole(): Promise<Record<string, number>>;
|
|
46
48
|
update(id: string, patch: Partial<TUser> & Record<string, unknown>): Promise<TUser | null>;
|
|
47
49
|
disconnect(): Promise<void>;
|
|
48
50
|
}
|
|
@@ -50,8 +52,8 @@ export interface PostgresUsersStore<TUser extends PostgresUserBase = PostgresUse
|
|
|
50
52
|
export interface PostgresSettingsStore<TSettings extends object = Record<string, unknown>> {
|
|
51
53
|
get<TKey extends keyof TSettings & string>(key: TKey): Promise<TSettings[TKey] | null>;
|
|
52
54
|
get(key: string): Promise<unknown | null>;
|
|
53
|
-
set<TKey extends keyof TSettings & string>(key: TKey, value: TSettings[TKey]): Promise<
|
|
54
|
-
set(key: string, value: unknown): Promise<
|
|
55
|
+
set<TKey extends keyof TSettings & string>(key: TKey, value: TSettings[TKey]): Promise<TSettings[TKey]>;
|
|
56
|
+
set(key: string, value: unknown): Promise<unknown>;
|
|
55
57
|
getAll(): Promise<Partial<TSettings>>;
|
|
56
58
|
disconnect(): Promise<void>;
|
|
57
59
|
}
|
package/src/index.js
CHANGED
|
@@ -49,6 +49,10 @@ function mergeUserRow(row) {
|
|
|
49
49
|
return { ...row.data, id: row.id };
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
+
function normalizeEmail(email) {
|
|
53
|
+
return String(email || '').trim().toLowerCase();
|
|
54
|
+
}
|
|
55
|
+
|
|
52
56
|
function createPostgresUsersStore(options = {}) {
|
|
53
57
|
const table = options.usersTable || DEFAULT_USERS_TABLE;
|
|
54
58
|
assertValidIdentifier(table, 'usersTable');
|
|
@@ -73,7 +77,7 @@ function createPostgresUsersStore(options = {}) {
|
|
|
73
77
|
return {
|
|
74
78
|
async findByEmail(email) {
|
|
75
79
|
await ready;
|
|
76
|
-
const result = await pool.query(`SELECT id, data FROM ${table} WHERE email = $1 LIMIT 1`, [email]);
|
|
80
|
+
const result = await pool.query(`SELECT id, data FROM ${table} WHERE email = $1 LIMIT 1`, [normalizeEmail(email)]);
|
|
77
81
|
return mergeUserRow(result.rows[0]);
|
|
78
82
|
},
|
|
79
83
|
|
|
@@ -93,6 +97,9 @@ function createPostgresUsersStore(options = {}) {
|
|
|
93
97
|
await ready;
|
|
94
98
|
const data = { ...(userData || {}) };
|
|
95
99
|
delete data.id;
|
|
100
|
+
if (data.email !== undefined) {
|
|
101
|
+
data.email = normalizeEmail(data.email);
|
|
102
|
+
}
|
|
96
103
|
const id = randomUUID();
|
|
97
104
|
const email = data.email ?? null;
|
|
98
105
|
const role = data.role ?? null;
|
|
@@ -122,11 +129,33 @@ function createPostgresUsersStore(options = {}) {
|
|
|
122
129
|
return result.rows.map(mergeUserRow);
|
|
123
130
|
},
|
|
124
131
|
|
|
132
|
+
async count({ role } = {}) {
|
|
133
|
+
await ready;
|
|
134
|
+
const result = role
|
|
135
|
+
? await pool.query(`SELECT COUNT(*)::int AS count FROM ${table} WHERE role = $1`, [role])
|
|
136
|
+
: await pool.query(`SELECT COUNT(*)::int AS count FROM ${table}`);
|
|
137
|
+
return result.rows[0].count;
|
|
138
|
+
},
|
|
139
|
+
|
|
140
|
+
async countByRole() {
|
|
141
|
+
await ready;
|
|
142
|
+
const result = await pool.query(`
|
|
143
|
+
SELECT COALESCE(role, 'unknown') AS role, COUNT(*)::int AS count
|
|
144
|
+
FROM ${table}
|
|
145
|
+
GROUP BY COALESCE(role, 'unknown')
|
|
146
|
+
ORDER BY role
|
|
147
|
+
`);
|
|
148
|
+
return Object.fromEntries(result.rows.map((row) => [row.role, row.count]));
|
|
149
|
+
},
|
|
150
|
+
|
|
125
151
|
async update(id, patch) {
|
|
126
152
|
await ready;
|
|
127
153
|
if (!isValidUuid(id)) return null;
|
|
128
154
|
const safePatch = { ...(patch || {}) };
|
|
129
155
|
delete safePatch.id;
|
|
156
|
+
if (safePatch.email !== undefined) {
|
|
157
|
+
safePatch.email = normalizeEmail(safePatch.email);
|
|
158
|
+
}
|
|
130
159
|
|
|
131
160
|
try {
|
|
132
161
|
const existing = await pool.query(`SELECT id, data FROM ${table} WHERE id = $1 LIMIT 1`, [id]);
|
|
@@ -179,11 +208,13 @@ function createPostgresSettingsStore(options = {}) {
|
|
|
179
208
|
|
|
180
209
|
async set(key, value) {
|
|
181
210
|
await ready;
|
|
182
|
-
await pool.query(
|
|
211
|
+
const result = await pool.query(
|
|
183
212
|
`INSERT INTO ${table} (key, value) VALUES ($1, $2)
|
|
184
|
-
ON CONFLICT (key) DO UPDATE SET value = EXCLUDED.value
|
|
213
|
+
ON CONFLICT (key) DO UPDATE SET value = EXCLUDED.value
|
|
214
|
+
RETURNING value`,
|
|
185
215
|
[key, JSON.stringify(value)]
|
|
186
216
|
);
|
|
217
|
+
return result.rows[0].value;
|
|
187
218
|
},
|
|
188
219
|
|
|
189
220
|
async getAll() {
|