@jskit-ai/users-core 0.2.21 → 0.2.23
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/CHANGELOG.md +2 -0
- package/package.json +10 -10
- package/patterns/user-administration-server/example/packages/users/package.json +3 -3
- package/patterns/user-administration-server/example/packages/users-workspace/package.json +6 -6
- package/src/server/common/repositories/userProfilesRepository.js +4 -0
- package/src/server/common/repositories/userSettingsRepository.js +3 -0
- package/src/server/common/services/authProfileSyncService.js +7 -2
- package/test/joinSource.mysql.test.js +5 -0
- package/test/joinSource.postgres.test.js +6 -0
- package/test/repositories.mysql.test.js +33 -0
- package/test/support/joinSourceCases.js +59 -0
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
## Unreleased
|
|
4
4
|
|
|
5
|
+
- `users.profiles` and `users.settings` answer `joinSource({ alias, filters }, options)`, the handle's join source, as `workspaces` and `workspaces.memberships` do: another owner's report reads people and their settings beside its own rows in one statement, under the users table's row policy, where it needed an app-side raw-SQL exception. Its tests run on MariaDB/MySQL (`joinSource.mysql.test.js`) and PostgreSQL (`joinSource.postgres.test.js`).
|
|
6
|
+
- Two first sign-ins of one identity at once (a double-clicked sign-in, two tabs) make one user, and both answer it: the sync that loses the insert (`auth.profile.projector`'s `syncIdentityProfile`) rolls back in full and runs again, reading the winner's profile as a returning sign-in does, with `created: false` for the lifecycle contributors. It failed with the database's duplicate-entry error (`ER_DUP_ENTRY`). Inside a caller's transaction (`trx`) the loss still fails that transaction, for the caller's own retry.
|
|
5
7
|
- `users.profiles.listByIds(ids, options)` answers every profile of a list of ids in one call, however many, sorted by id. An id named twice answers its profile once, and an id naming no profile the caller can see, or that is no id, is left out. It reads 1,000 distinct ids per statement, each joining the caller's transaction and seeing what `query` sees for the caller's context. Code that names people by id read them in `query` pages of at most 100, walked by hand, once a group passed 100 people. Its tests run on MariaDB/MySQL (`listByIds.mysql.test.js`) and PostgreSQL (`listByIds.postgres.test.js`, in CI's PostgreSQL job).
|
|
6
8
|
- `users.settings.listByUserIds(userIds, options)` answers the settings of a list of users in one call, the same way: sorted by user id, each user once, users without settings left out, and each locale canonical as `findByUserId` reads it. Code that showed many people's settings read them with one `findByUserId` per person.
|
|
7
9
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jskit-ai/users-core",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.23",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"test": "jskit test"
|
|
@@ -18,10 +18,10 @@
|
|
|
18
18
|
"./locales/en.json": "./locales/en.json"
|
|
19
19
|
},
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"@jskit-ai/resource-core": "0.1.
|
|
22
|
-
"@jskit-ai/resource-crud-core": "0.1.
|
|
23
|
-
"@jskit-ai/storage-runtime": "0.1.
|
|
24
|
-
"@jskit-ai/uploads-runtime": "0.2.
|
|
21
|
+
"@jskit-ai/resource-core": "0.1.208",
|
|
22
|
+
"@jskit-ai/resource-crud-core": "0.1.208",
|
|
23
|
+
"@jskit-ai/storage-runtime": "0.1.263",
|
|
24
|
+
"@jskit-ai/uploads-runtime": "0.2.17",
|
|
25
25
|
"json-rest-schema": "^1.0.17"
|
|
26
26
|
},
|
|
27
27
|
"description": "Users/account runtime plus HTTP routes for account features.",
|
|
@@ -196,10 +196,10 @@
|
|
|
196
196
|
}
|
|
197
197
|
},
|
|
198
198
|
"peerDependencies": {
|
|
199
|
-
"@jskit-ai/auth-core": "0.2.
|
|
200
|
-
"@jskit-ai/database-runtime": "0.1.
|
|
201
|
-
"@jskit-ai/http-runtime": "0.2.
|
|
202
|
-
"@jskit-ai/kernel": "0.2.
|
|
203
|
-
"@jskit-ai/resource-runtime": "0.2.
|
|
199
|
+
"@jskit-ai/auth-core": "0.2.17",
|
|
200
|
+
"@jskit-ai/database-runtime": "0.1.266",
|
|
201
|
+
"@jskit-ai/http-runtime": "0.2.17",
|
|
202
|
+
"@jskit-ai/kernel": "0.2.17",
|
|
203
|
+
"@jskit-ai/resource-runtime": "0.2.19"
|
|
204
204
|
}
|
|
205
205
|
}
|
|
@@ -10,9 +10,9 @@
|
|
|
10
10
|
"./shared": "./src/shared/index.js"
|
|
11
11
|
},
|
|
12
12
|
"dependencies": {
|
|
13
|
-
"@jskit-ai/crud-core": "0.2.
|
|
14
|
-
"@jskit-ai/resource-crud-core": "0.1.
|
|
15
|
-
"@jskit-ai/users-core": "0.2.
|
|
13
|
+
"@jskit-ai/crud-core": "0.2.22",
|
|
14
|
+
"@jskit-ai/resource-crud-core": "0.1.208",
|
|
15
|
+
"@jskit-ai/users-core": "0.2.23"
|
|
16
16
|
},
|
|
17
17
|
"jskit": {
|
|
18
18
|
"kind": "runtime",
|
|
@@ -10,14 +10,14 @@
|
|
|
10
10
|
"./shared": "./src/shared/index.js"
|
|
11
11
|
},
|
|
12
12
|
"dependencies": {
|
|
13
|
-
"@jskit-ai/crud-core": "0.2.
|
|
14
|
-
"@jskit-ai/resource-crud-core": "0.1.
|
|
15
|
-
"@jskit-ai/users-core": "0.2.
|
|
16
|
-
"@jskit-ai/workspaces-core": "0.2.
|
|
13
|
+
"@jskit-ai/crud-core": "0.2.22",
|
|
14
|
+
"@jskit-ai/resource-crud-core": "0.1.208",
|
|
15
|
+
"@jskit-ai/users-core": "0.2.23",
|
|
16
|
+
"@jskit-ai/workspaces-core": "0.2.23"
|
|
17
17
|
},
|
|
18
18
|
"peerDependencies": {
|
|
19
|
-
"@jskit-ai/kernel": "0.2.
|
|
20
|
-
"@jskit-ai/resource-runtime": "0.2.
|
|
19
|
+
"@jskit-ai/kernel": "0.2.17",
|
|
20
|
+
"@jskit-ai/resource-runtime": "0.2.19"
|
|
21
21
|
},
|
|
22
22
|
"jskit": {
|
|
23
23
|
"kind": "runtime",
|
|
@@ -490,6 +490,10 @@ function createRepository({ userProfiles, userSettings }) {
|
|
|
490
490
|
|
|
491
491
|
return Object.freeze({
|
|
492
492
|
withTransaction: userProfiles.withTransaction,
|
|
493
|
+
// The users as a join source for another owner's report, which reads them in its own statement: the handle's
|
|
494
|
+
// joinSource, the users under `alias` matching `filters` (this resource's search fields), with the users table's
|
|
495
|
+
// row policy applied.
|
|
496
|
+
joinSource: userProfiles.joinSource,
|
|
493
497
|
findById,
|
|
494
498
|
findByEmail,
|
|
495
499
|
findByIdentity,
|
|
@@ -158,6 +158,9 @@ function createRepository({ userSettings }) {
|
|
|
158
158
|
|
|
159
159
|
return Object.freeze({
|
|
160
160
|
withTransaction: userSettings.withTransaction,
|
|
161
|
+
// The users' settings as a join source for another owner's report, which reads them in its own statement: the
|
|
162
|
+
// handle's joinSource, under `alias`, keyed by the user's id (`column("id")`).
|
|
163
|
+
joinSource: userSettings.joinSource,
|
|
161
164
|
findByUserId,
|
|
162
165
|
listByUserIds,
|
|
163
166
|
ensureForUserId,
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { retryLostInsertRace } from "@jskit-ai/database-runtime/shared";
|
|
1
2
|
import { normalizeLowerText, normalizeRecordId, normalizeText } from "@jskit-ai/kernel/shared/support/normalize";
|
|
2
3
|
import { normalizeIdentity } from "../support/identity.js";
|
|
3
4
|
|
|
@@ -90,7 +91,9 @@ function createService({ userProfilesRepository, resolveLifecycleContributors, u
|
|
|
90
91
|
/**
|
|
91
92
|
* Creates or updates the users row of a signed-in identity, ensures its settings and runs the lifecycle
|
|
92
93
|
* contributors (personal workspace provisioning). With `onlyWhenChanged`, an existing row that already
|
|
93
|
-
* matches the identity is answered after one read, without that work: an ordinary request's check.
|
|
94
|
+
* matches the identity is answered after one read, without that work: an ordinary request's check. A first
|
|
95
|
+
* sign-in that loses its insert to the same identity's other first sign-in answers the winner's profile, so there
|
|
96
|
+
* is one user either way; inside a caller's transaction (`trx`) that loss fails the caller's transaction instead.
|
|
94
97
|
* @param {object} profileLike
|
|
95
98
|
* @param {{ onlyWhenChanged?: boolean, trx?: object, context?: object }} [options]
|
|
96
99
|
*/
|
|
@@ -133,7 +136,9 @@ function createService({ userProfilesRepository, resolveLifecycleContributors, u
|
|
|
133
136
|
return synchronizedProfile;
|
|
134
137
|
};
|
|
135
138
|
|
|
136
|
-
|
|
139
|
+
// Two first sign-ins of one identity at once both find no profile and both insert one: the one that loses the
|
|
140
|
+
// insert rolls back in full and runs again, finding the winner's profile as a returning sign-in does.
|
|
141
|
+
return retryLostInsertRace(options, () => userProfilesRepository.withTransaction(options, runSync));
|
|
137
142
|
}
|
|
138
143
|
|
|
139
144
|
return Object.freeze({
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { joinSourceCases } from "./support/joinSourceCases.js";
|
|
2
|
+
import { POSTGRES_ELSEWHERE, postgresUsersDatabase } from "./support/usersDatabases.js";
|
|
3
|
+
|
|
4
|
+
// users.profiles.joinSource and users.settings.joinSource on PostgreSQL: run with TEST_DB_NAME (and the usual DB_HOST,
|
|
5
|
+
// DB_PORT, DB_USER and DB_PASSWORD) naming a PostgreSQL database, and DB_CLIENT unset or pg.
|
|
6
|
+
joinSourceCases(postgresUsersDatabase("users_join"), { skip: POSTGRES_ELSEWHERE });
|
|
@@ -4,6 +4,7 @@ import { requireTestDatabase } from "@jskit-ai/database-runtime/server/testDatab
|
|
|
4
4
|
import { findDuplicateEntryError } from "@jskit-ai/database-runtime/shared";
|
|
5
5
|
import { createRepository as createUserProfilesRepository } from "../src/server/common/repositories/userProfilesRepository.js";
|
|
6
6
|
import { createRepository as createUserSettingsRepository } from "../src/server/common/repositories/userSettingsRepository.js";
|
|
7
|
+
import { createService as createAuthProfileSyncService } from "../src/server/common/services/authProfileSyncService.js";
|
|
7
8
|
import { DEFAULT_USER_SETTINGS } from "../src/shared/settings.js";
|
|
8
9
|
import { usersDatabase } from "./support/usersDatabases.js";
|
|
9
10
|
|
|
@@ -230,6 +231,38 @@ test("a standalone profile insert that loses its identity to another answers the
|
|
|
230
231
|
assert.ok(findDuplicateEntryError(participant));
|
|
231
232
|
});
|
|
232
233
|
|
|
234
|
+
test("two first sign-ins of one identity at once make one user, and the one that lost the insert answers it", async () => {
|
|
235
|
+
requireTestDatabase(process.env);
|
|
236
|
+
const db = await database.open();
|
|
237
|
+
const synced = [];
|
|
238
|
+
const projectorOn = (userProfiles) => createAuthProfileSyncService({
|
|
239
|
+
userProfilesRepository: createUserProfilesRepository({ userProfiles, userSettings: db.settings }),
|
|
240
|
+
userSettingsRepository: db.settings,
|
|
241
|
+
resolveLifecycleContributors: () => [{
|
|
242
|
+
async afterIdentityProfileSynced({ profile, created }) { synced.push({ id: profile.id, created }); }
|
|
243
|
+
}]
|
|
244
|
+
});
|
|
245
|
+
const projector = projectorOn(db.resources.userProfiles);
|
|
246
|
+
let other = null;
|
|
247
|
+
// The other first sign-in runs whole, and commits, between this one's read (no profile yet) and its insert.
|
|
248
|
+
const racing = projectorOn({
|
|
249
|
+
...db.resources.userProfiles,
|
|
250
|
+
async create(attributes, options) {
|
|
251
|
+
other ??= await projector.syncIdentityProfile(identityOf("twin"));
|
|
252
|
+
return db.resources.userProfiles.create(attributes, options);
|
|
253
|
+
}
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
const answered = await racing.syncIdentityProfile(identityOf("twin"));
|
|
257
|
+
assert.ok(other, "the other sign-in ran inside this one");
|
|
258
|
+
assert.equal(answered.id, other.id, "both answer the one user");
|
|
259
|
+
const rows = await db.knex("users").where({ auth_provider_user_sid: "identity-twin" }).select("id");
|
|
260
|
+
assert.deepEqual(rows.map((row) => String(row.id)), [other.id]);
|
|
261
|
+
assert.equal((await db.settings.findByUserId(other.id)).id, other.id, "with its settings");
|
|
262
|
+
// The winner's sync reports a created user; the run that lost was rolled back and ran again as a returning sign-in.
|
|
263
|
+
assert.deepEqual(synced.map(({ created }) => created), [true, false]);
|
|
264
|
+
});
|
|
265
|
+
|
|
233
266
|
test("user settings start from the defaults once, join a caller's transaction, and a lost standalone insert reads the winner", async () => {
|
|
234
267
|
requireTestDatabase(process.env);
|
|
235
268
|
const db = await database.open();
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import test from "node:test";
|
|
3
|
+
import { countQueries, requireTestDatabase } from "@jskit-ai/database-runtime/server/testDatabases";
|
|
4
|
+
|
|
5
|
+
// users.profiles.joinSource and users.settings.joinSource on a database of the test file's own (usersDatabases.js), run
|
|
6
|
+
// on MariaDB/MySQL and on PostgreSQL alike: another owner's report reads the users beside its own rows in one
|
|
7
|
+
// statement, through the users table's scope.
|
|
8
|
+
|
|
9
|
+
const identityOf = (tag) => ({
|
|
10
|
+
authProvider: "local",
|
|
11
|
+
authProviderUserSid: `identity-${tag}`,
|
|
12
|
+
email: `${tag}@example.test`,
|
|
13
|
+
displayName: tag,
|
|
14
|
+
username: tag
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Registers the join source tests on `database`.
|
|
19
|
+
* @param {{ open: () => Promise<any> }} database
|
|
20
|
+
* @param {{ skip?: string | false }} [options]
|
|
21
|
+
*/
|
|
22
|
+
function joinSourceCases(database, { skip = false } = {}) {
|
|
23
|
+
test("users.profiles and users.settings answer join sources a report reads in one statement, under the users table's row policy", { skip }, async () => {
|
|
24
|
+
requireTestDatabase(process.env);
|
|
25
|
+
const db = await database.open();
|
|
26
|
+
const shown = await db.profiles.upsert(identityOf("join-shown"));
|
|
27
|
+
const hidden = await db.profiles.upsert(identityOf("join-hidden"));
|
|
28
|
+
await db.settings.ensureForUserId(shown.id);
|
|
29
|
+
await db.settings.ensureForUserId(hidden.id);
|
|
30
|
+
await db.knex("user_settings").where({ user_id: shown.id }).update({ locale: "en-GB" });
|
|
31
|
+
await db.knex("user_settings").where({ user_id: hidden.id }).update({ locale: "it-IT" });
|
|
32
|
+
// The users table owned again, with a row policy that hides one profile from every caller but background work.
|
|
33
|
+
const narrowed = await db.ownedWith({
|
|
34
|
+
rowPolicy: ({ query, column, caller }) => {
|
|
35
|
+
if (caller.kind !== "system") query.whereNot(column("username"), hidden.username);
|
|
36
|
+
return true;
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
// A report beside the users: each listed user's display name and locale, the settings joined to the profiles.
|
|
41
|
+
async function localesOf(options) {
|
|
42
|
+
const people = await narrowed.profiles.joinSource({ alias: "u", filters: { id: [shown.id, hidden.id] } }, options);
|
|
43
|
+
const settings = await narrowed.settings.joinSource({ alias: "s" }, options);
|
|
44
|
+
const named = people.query.select({ id: people.column("id"), name: people.column("displayName") }).as("p");
|
|
45
|
+
return settings.query
|
|
46
|
+
.join(named, "p.id", settings.column("id"))
|
|
47
|
+
.select({ name: "p.name", locale: settings.column("locale") })
|
|
48
|
+
.orderBy("p.name");
|
|
49
|
+
}
|
|
50
|
+
const { result, count } = await countQueries(db.knex, () => localesOf({}));
|
|
51
|
+
assert.deepEqual(result, [{ name: "join-shown", locale: "en-GB" }]);
|
|
52
|
+
assert.equal(count, 1);
|
|
53
|
+
assert.deepEqual(await localesOf({ context: narrowed.systemContext }),
|
|
54
|
+
[{ name: "join-hidden", locale: "it-IT" }, { name: "join-shown", locale: "en-GB" }]);
|
|
55
|
+
await assert.rejects(narrowed.profiles.joinSource({}, {}), /userProfiles\.joinSource\(\) needs the alias/u);
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export { joinSourceCases };
|