@rexezuge/identity 1.22.1 → 2.0.0
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/README.md +1 -1
- package/dist/applyMigrations.d.ts +1 -7
- package/dist/applyMigrations.d.ts.map +1 -1
- package/dist/applyMigrations.js +1 -1
- package/dist/applyMigrations.js.map +3 -3
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +3 -3
- package/dist/migrations.d.ts +1 -1
- package/dist/migrations.js.map +1 -1
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -52,7 +52,7 @@ await applyIdentityMigrations(env.DB); // fresh install: users.sql
|
|
|
52
52
|
await applyIdentityMigrations(env.DB, { upgrade: true }); // legacy DB: + upgrade-user-identity.sql (rerunnable)
|
|
53
53
|
```
|
|
54
54
|
|
|
55
|
-
Executed through `@rexezuge/d1`'s splitter — one `batch()` per file, matching D1's own per-file apply, so a failure names its file. `IDENTITY_MIGRATIONS` — the ordered `{ name, sql }` list — is the one canonical export for repos that apply migrations through their own runner.
|
|
55
|
+
Executed through `@rexezuge/d1`'s splitter — one `batch()` per file, matching D1's own per-file apply, so a failure names its file. `IDENTITY_MIGRATIONS` — the ordered `{ name, sql }` list — is the one canonical export for repos that apply migrations through their own runner. The upgrade backfills a verified registry row for every anchor, which is what lets pre-identity databases resolve addresses the old way until they migrate.
|
|
56
56
|
|
|
57
57
|
## Extending
|
|
58
58
|
|
|
@@ -9,12 +9,6 @@ declare const MIGRATION_SQL_BY_NAME: {
|
|
|
9
9
|
readonly 'upgrade-user-identity.sql': "\n-- Phase 1: stable account key.\n-- SQLite cannot add a PRIMARY KEY column, so the id is a plain column with a\n-- unique index. A unique index is a valid foreign key parent, which is all the\n-- user_id references need.\nALTER TABLE users ADD COLUMN id TEXT;\n\nUPDATE users SET id = 'usr_' || lower(hex(randomblob(16))) WHERE id IS NULL;\n\nCREATE UNIQUE INDEX IF NOT EXISTS idx_users_id ON users(id);\n\n-- Phase 2: mutable sign-in address.\n-- 'email' stays as the frozen anchor; 'current_email' is what the account signs\n-- in with and what the API reports. Uniqueness is enforced here, so an address\n-- can never be claimed by two accounts.\nALTER TABLE users ADD COLUMN current_email TEXT;\n\nUPDATE users SET current_email = lower(email) WHERE current_email IS NULL;\n\nCREATE UNIQUE INDEX IF NOT EXISTS idx_users_current_email ON users(current_email);\n\n-- Phase 3: address registry.\n-- Login resolution consults is_verified = 1 only. Backfilled from the frozen\n-- anchor address of every existing account, lowercased so a legacy mixed-case\n-- row still yields exactly one login identity.\nCREATE TABLE IF NOT EXISTS user_emails (\n email TEXT PRIMARY KEY,\n user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,\n is_verified INTEGER NOT NULL DEFAULT 0,\n created_at INTEGER NOT NULL\n);\n\nCREATE INDEX IF NOT EXISTS idx_user_emails_user ON user_emails(user_id);\n\nINSERT OR IGNORE INTO user_emails (email, user_id, is_verified, created_at)\nSELECT lower(email), id, 1, created_at FROM users;\n";
|
|
10
10
|
};
|
|
11
11
|
type IdentityMigrationName = keyof typeof MIGRATION_SQL_BY_NAME;
|
|
12
|
-
/**
|
|
13
|
-
* @deprecated Use `IDENTITY_MIGRATIONS` — the one canonical migrations export,
|
|
14
|
-
* an ordered `{ name, sql }` list. This map carries the same two SQL strings
|
|
15
|
-
* under different names. Removed in 2.0.
|
|
16
|
-
*/
|
|
17
|
-
declare const IDENTITY_MIGRATION_SQL: Readonly<Record<IdentityMigrationName, string>>;
|
|
18
12
|
/**
|
|
19
13
|
The full fresh-install order.
|
|
20
14
|
*/
|
|
@@ -38,6 +32,6 @@ declare const UPGRADE_MIGRATIONS: readonly IdentityMigrationName[];
|
|
|
38
32
|
declare function applyIdentityMigrations(db: D1Queryable, options?: {
|
|
39
33
|
upgrade?: boolean;
|
|
40
34
|
}): Promise<void>;
|
|
41
|
-
export { applyIdentityMigrations,
|
|
35
|
+
export { applyIdentityMigrations, FRESH_MIGRATIONS, UPGRADE_MIGRATIONS };
|
|
42
36
|
export type { IdentityMigrationName };
|
|
43
37
|
//# sourceMappingURL=applyMigrations.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"applyMigrations.d.ts","sourceRoot":"","sources":["../src/applyMigrations.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAGhD;;;;GAIG;AACH,QAAA,MAAM,qBAAqB;;;CAGjB,CAAC;AAEX,KAAK,qBAAqB,GAAG,MAAM,OAAO,qBAAqB,CAAC;AAEhE
|
|
1
|
+
{"version":3,"file":"applyMigrations.d.ts","sourceRoot":"","sources":["../src/applyMigrations.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAGhD;;;;GAIG;AACH,QAAA,MAAM,qBAAqB;;;CAGjB,CAAC;AAEX,KAAK,qBAAqB,GAAG,MAAM,OAAO,qBAAqB,CAAC;AAEhE;;EAEE;AACF,QAAA,MAAM,gBAAgB,EAAE,SAAS,qBAAqB,EAAkB,CAAC;AAEzE;;EAEE;AACF,QAAA,MAAM,kBAAkB,EAAE,SAAS,qBAAqB,EAA+C,CAAC;AAExG;;;;;;;;;;;GAWG;AACH,iBAAe,uBAAuB,CAAC,EAAE,EAAE,WAAW,EAAE,OAAO,GAAE;IAAE,OAAO,CAAC,EAAE,OAAO,CAAA;CAAO,GAAG,OAAO,CAAC,IAAI,CAAC,CAa1G;AAED,OAAO,EAAE,uBAAuB,EAAE,gBAAgB,EAAE,kBAAkB,EAAE,CAAC;AACzE,YAAY,EAAE,qBAAqB,EAAE,CAAC"}
|
package/dist/applyMigrations.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var
|
|
1
|
+
var u=Object.defineProperty;var r=(t,e)=>u(t,"name",{value:e,configurable:!0});import{executableStatements as p}from"@rexezuge/d1";import{UPGRADE_USER_IDENTITY as f,USERS_SCHEMA as l}from"./migrations.js";const m={"users.sql":l,"upgrade-user-identity.sql":f},a=["users.sql"],o=["users.sql","upgrade-user-identity.sql"];async function I(t,e={}){const i=e.upgrade===!0?o:a;for(const c of i){const s=p(m[c]);if(s.length!==0){if(t.batch){await t.batch(s.map(n=>t.prepare(n)));continue}for(const n of s)await t.prepare(n).run()}}}r(I,"applyIdentityMigrations");export{a as FRESH_MIGRATIONS,o as UPGRADE_MIGRATIONS,I as applyIdentityMigrations};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/applyMigrations.ts"],
|
|
4
|
-
"sourcesContent": ["import { executableStatements } from '@rexezuge/d1';\nimport type { D1Queryable } from '@rexezuge/d1';\nimport { UPGRADE_USER_IDENTITY, USERS_SCHEMA } from './migrations.ts';\n\n/**\n * The executable form, keyed by the names `FRESH_MIGRATIONS` /\n * `UPGRADE_MIGRATIONS` use. Private: `IDENTITY_MIGRATIONS` is the one public\n * migrations export.\n */\nconst MIGRATION_SQL_BY_NAME = {\n 'users.sql': USERS_SCHEMA,\n 'upgrade-user-identity.sql': UPGRADE_USER_IDENTITY,\n} as const;\n\ntype IdentityMigrationName = keyof typeof MIGRATION_SQL_BY_NAME;\n\n/**\
|
|
5
|
-
"mappings": "+EAAA,OAAS,wBAAAA,MAA4B,eAErC,OAAS,yBAAAC,EAAuB,gBAAAC,MAAoB,kBAOpD,MAAMC,EAAwB,CAC5B,YAAaD,EACb,4BAA6BD,
|
|
6
|
-
"names": ["executableStatements", "UPGRADE_USER_IDENTITY", "USERS_SCHEMA", "MIGRATION_SQL_BY_NAME", "
|
|
4
|
+
"sourcesContent": ["import { executableStatements } from '@rexezuge/d1';\nimport type { D1Queryable } from '@rexezuge/d1';\nimport { UPGRADE_USER_IDENTITY, USERS_SCHEMA } from './migrations.ts';\n\n/**\n * The executable form, keyed by the names `FRESH_MIGRATIONS` /\n * `UPGRADE_MIGRATIONS` use. Private: `IDENTITY_MIGRATIONS` is the one public\n * migrations export.\n */\nconst MIGRATION_SQL_BY_NAME = {\n 'users.sql': USERS_SCHEMA,\n 'upgrade-user-identity.sql': UPGRADE_USER_IDENTITY,\n} as const;\n\ntype IdentityMigrationName = keyof typeof MIGRATION_SQL_BY_NAME;\n\n/**\nThe full fresh-install order.\n*/\nconst FRESH_MIGRATIONS: readonly IdentityMigrationName[] = ['users.sql'];\n\n/**\nA legacy database applies the upgrade after the fresh schema is in place.\n*/\nconst UPGRADE_MIGRATIONS: readonly IdentityMigrationName[] = ['users.sql', 'upgrade-user-identity.sql'];\n\n/**\n * Apply the identity migrations to a D1 binding.\n *\n * Split and executed through `@rexezuge/d1` — the same statement splitter the\n * test helpers use — so a kit migration and an integration suite can never\n * disagree about where one statement ends. One `batch()` per file, matching\n * D1's own per-file apply, so a failure names its file.\n *\n * `upgrade: false` (the default) applies only the fresh-install schema. Pass\n * `upgrade: true` for a database whose `users` table predates the identity\n * columns; the upgrade file is rerunnable, so applying both is safe.\n */\nasync function applyIdentityMigrations(db: D1Queryable, options: { upgrade?: boolean } = {}): Promise<void> {\n const names = options.upgrade === true ? UPGRADE_MIGRATIONS : FRESH_MIGRATIONS;\n for (const name of names) {\n const statements = executableStatements(MIGRATION_SQL_BY_NAME[name]);\n if (statements.length === 0) continue;\n if (db.batch) {\n await db.batch(statements.map((sql) => db.prepare(sql)));\n continue;\n }\n for (const sql of statements) {\n await db.prepare(sql).run();\n }\n }\n}\n\nexport { applyIdentityMigrations, FRESH_MIGRATIONS, UPGRADE_MIGRATIONS };\nexport type { IdentityMigrationName };\n"],
|
|
5
|
+
"mappings": "+EAAA,OAAS,wBAAAA,MAA4B,eAErC,OAAS,yBAAAC,EAAuB,gBAAAC,MAAoB,kBAOpD,MAAMC,EAAwB,CAC5B,YAAaD,EACb,4BAA6BD,GAQzBG,EAAqD,CAAC,WAAW,EAKjEC,EAAuD,CAAC,YAAa,2BAA2B,EActG,eAAeC,EAAwBC,EAAiBC,EAAiC,CAAA,EAAE,CACzF,MAAMC,EAAQD,EAAQ,UAAY,GAAOH,EAAqBD,EAC9D,UAAWM,KAAQD,EAAO,CACxB,MAAME,EAAaX,EAAqBG,EAAsBO,CAAI,CAAC,EACnE,GAAIC,EAAW,SAAW,EAC1B,IAAIJ,EAAG,MAAO,CACZ,MAAMA,EAAG,MAAMI,EAAW,IAAKC,GAAQL,EAAG,QAAQK,CAAG,CAAC,CAAC,EACvD,QACF,CACA,UAAWA,KAAOD,EAChB,MAAMJ,EAAG,QAAQK,CAAG,EAAE,IAAG,EAE7B,CACF,CAbeC,EAAAP,EAAA",
|
|
6
|
+
"names": ["executableStatements", "UPGRADE_USER_IDENTITY", "USERS_SCHEMA", "MIGRATION_SQL_BY_NAME", "FRESH_MIGRATIONS", "UPGRADE_MIGRATIONS", "applyIdentityMigrations", "db", "options", "names", "name", "statements", "sql", "__name"]
|
|
7
7
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
*/
|
|
9
9
|
export { registerAccount, resolveAccount, resolveUserId } from './accountLookup.ts';
|
|
10
10
|
export type { AccountLookupDeps } from './accountLookup.ts';
|
|
11
|
-
export { applyIdentityMigrations,
|
|
11
|
+
export { applyIdentityMigrations, FRESH_MIGRATIONS, UPGRADE_MIGRATIONS } from './applyMigrations.ts';
|
|
12
12
|
export type { IdentityMigrationName } from './applyMigrations.ts';
|
|
13
13
|
export { IDENTITY_MIGRATIONS, UPGRADE_USER_IDENTITY, USERS_SCHEMA } from './migrations.ts';
|
|
14
14
|
export { newAccountId, newAnchor, summarize } from './ResolvedAccount.ts';
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,eAAe,EAAE,cAAc,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACpF,YAAY,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AAC5D,OAAO,EAAE,uBAAuB,EAAE,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,eAAe,EAAE,cAAc,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACpF,YAAY,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AAC5D,OAAO,EAAE,uBAAuB,EAAE,gBAAgB,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AACrG,YAAY,EAAE,qBAAqB,EAAE,MAAM,sBAAsB,CAAC;AAClE,OAAO,EAAE,mBAAmB,EAAE,qBAAqB,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAC3F,OAAO,EAAE,YAAY,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;AAC1E,YAAY,EAAE,cAAc,EAAE,eAAe,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AAClG,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AACvC,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACjD,YAAY,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAClE,OAAO,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAC;AAC/D,YAAY,EAAE,gBAAgB,EAAE,eAAe,EAAE,MAAM,0BAA0B,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{registerAccount as o,resolveAccount as
|
|
1
|
+
import{registerAccount as o,resolveAccount as t,resolveUserId as I}from"./accountLookup.js";import{applyIdentityMigrations as m,FRESH_MIGRATIONS as n,UPGRADE_MIGRATIONS as p}from"./applyMigrations.js";import{IDENTITY_MIGRATIONS as E,UPGRADE_USER_IDENTITY as S,USERS_SCHEMA as c}from"./migrations.js";import{newAccountId as R,newAnchor as U,summarize as f}from"./ResolvedAccount.js";import{UserDAO as T}from"./UserDAO.js";import{UserEmailDAO as _}from"./UserEmailDAO.js";import{UserIdentityService as M}from"./UserIdentityService.js";export{n as FRESH_MIGRATIONS,E as IDENTITY_MIGRATIONS,p as UPGRADE_MIGRATIONS,S as UPGRADE_USER_IDENTITY,c as USERS_SCHEMA,T as UserDAO,_ as UserEmailDAO,M as UserIdentityService,m as applyIdentityMigrations,R as newAccountId,U as newAnchor,o as registerAccount,t as resolveAccount,I as resolveUserId,f as summarize};
|
package/dist/index.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/index.ts"],
|
|
4
|
-
"sourcesContent": ["/**\n * `@rexezuge/identity` — the account layer: the address is not the account.\n *\n * Converged from the seven repos that authenticate users (all but\n * ChordDHT-Tracker, whose callers are certificates). Durable-DAV's invariants\n * are the canonical ones because they are the strictest: the frozen anchor,\n * the verified-only registry, and the fail-closed resolution order.\n */\n\nexport { registerAccount, resolveAccount, resolveUserId } from './accountLookup.ts';\nexport type { AccountLookupDeps } from './accountLookup.ts';\nexport { applyIdentityMigrations,
|
|
5
|
-
"mappings": "AASA,OAAS,mBAAAA,EAAiB,kBAAAC,EAAgB,iBAAAC,MAAqB,qBAE/D,OAAS,2BAAAC,EAAyB,
|
|
6
|
-
"names": ["registerAccount", "resolveAccount", "resolveUserId", "applyIdentityMigrations", "
|
|
4
|
+
"sourcesContent": ["/**\n * `@rexezuge/identity` — the account layer: the address is not the account.\n *\n * Converged from the seven repos that authenticate users (all but\n * ChordDHT-Tracker, whose callers are certificates). Durable-DAV's invariants\n * are the canonical ones because they are the strictest: the frozen anchor,\n * the verified-only registry, and the fail-closed resolution order.\n */\n\nexport { registerAccount, resolveAccount, resolveUserId } from './accountLookup.ts';\nexport type { AccountLookupDeps } from './accountLookup.ts';\nexport { applyIdentityMigrations, FRESH_MIGRATIONS, UPGRADE_MIGRATIONS } from './applyMigrations.ts';\nexport type { IdentityMigrationName } from './applyMigrations.ts';\nexport { IDENTITY_MIGRATIONS, UPGRADE_USER_IDENTITY, USERS_SCHEMA } from './migrations.ts';\nexport { newAccountId, newAnchor, summarize } from './ResolvedAccount.ts';\nexport type { AccountSummary, ResolvedAccount, UserRow, UserRowLike } from './ResolvedAccount.ts';\nexport { UserDAO } from './UserDAO.ts';\nexport { UserEmailDAO } from './UserEmailDAO.ts';\nexport type { EmailClaim, UserEmailRow } from './UserEmailDAO.ts';\nexport { UserIdentityService } from './UserIdentityService.ts';\nexport type { UserIdentityDeps, UserIdentityEnv } from './UserIdentityService.ts';\n"],
|
|
5
|
+
"mappings": "AASA,OAAS,mBAAAA,EAAiB,kBAAAC,EAAgB,iBAAAC,MAAqB,qBAE/D,OAAS,2BAAAC,EAAyB,oBAAAC,EAAkB,sBAAAC,MAA0B,uBAE9E,OAAS,uBAAAC,EAAqB,yBAAAC,EAAuB,gBAAAC,MAAoB,kBACzE,OAAS,gBAAAC,EAAc,aAAAC,EAAW,aAAAC,MAAiB,uBAEnD,OAAS,WAAAC,MAAe,eACxB,OAAS,gBAAAC,MAAoB,oBAE7B,OAAS,uBAAAC,MAA2B",
|
|
6
|
+
"names": ["registerAccount", "resolveAccount", "resolveUserId", "applyIdentityMigrations", "FRESH_MIGRATIONS", "UPGRADE_MIGRATIONS", "IDENTITY_MIGRATIONS", "UPGRADE_USER_IDENTITY", "USERS_SCHEMA", "newAccountId", "newAnchor", "summarize", "UserDAO", "UserEmailDAO", "UserIdentityService"]
|
|
7
7
|
}
|
package/dist/migrations.d.ts
CHANGED
|
@@ -57,7 +57,7 @@ declare const USERS_SCHEMA = "\n-- The account. The anchor (email) is immutable;
|
|
|
57
57
|
declare const UPGRADE_USER_IDENTITY = "\n-- Phase 1: stable account key.\n-- SQLite cannot add a PRIMARY KEY column, so the id is a plain column with a\n-- unique index. A unique index is a valid foreign key parent, which is all the\n-- user_id references need.\nALTER TABLE users ADD COLUMN id TEXT;\n\nUPDATE users SET id = 'usr_' || lower(hex(randomblob(16))) WHERE id IS NULL;\n\nCREATE UNIQUE INDEX IF NOT EXISTS idx_users_id ON users(id);\n\n-- Phase 2: mutable sign-in address.\n-- 'email' stays as the frozen anchor; 'current_email' is what the account signs\n-- in with and what the API reports. Uniqueness is enforced here, so an address\n-- can never be claimed by two accounts.\nALTER TABLE users ADD COLUMN current_email TEXT;\n\nUPDATE users SET current_email = lower(email) WHERE current_email IS NULL;\n\nCREATE UNIQUE INDEX IF NOT EXISTS idx_users_current_email ON users(current_email);\n\n-- Phase 3: address registry.\n-- Login resolution consults is_verified = 1 only. Backfilled from the frozen\n-- anchor address of every existing account, lowercased so a legacy mixed-case\n-- row still yields exactly one login identity.\nCREATE TABLE IF NOT EXISTS user_emails (\n email TEXT PRIMARY KEY,\n user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,\n is_verified INTEGER NOT NULL DEFAULT 0,\n created_at INTEGER NOT NULL\n);\n\nCREATE INDEX IF NOT EXISTS idx_user_emails_user ON user_emails(user_id);\n\nINSERT OR IGNORE INTO user_emails (email, user_id, is_verified, created_at)\nSELECT lower(email), id, 1, created_at FROM users;\n";
|
|
58
58
|
/**
|
|
59
59
|
* Every migration this package ships, in apply order — **the canonical
|
|
60
|
-
* migrations export
|
|
60
|
+
* migrations export**.
|
|
61
61
|
*
|
|
62
62
|
* `upgrade-user-identity.sql` is *not* applied on a fresh database — the
|
|
63
63
|
* fresh-install path is `users.sql` alone, and the upgrade exists only for a
|
package/dist/migrations.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/migrations.ts"],
|
|
4
|
-
"sourcesContent": ["/**\n * The canonical `users` / `user_emails` schema.\n *\n * Converged from migration `0001_init.sql` + `0004_user_identity.sql` across the\n * identity-carrying repos, expressed as the *final* shape so a fresh database\n * never has to be upgraded: `users.id` is the account key from the start, and\n * `user_emails` exists from the start.\n *\n * The invariants that make an address change safe (Durable-DAV's design, which\n * is the strictest in the family and the one every other repo's upgrade is\n * migrating toward):\n *\n * - `users.id` is an opaque, unique account key — never an address.\n * - `users.email` is the **frozen anchor**: immutable, the foreign-key target\n * every legacy `*_email` value resolves against, so history survives a change.\n * - `users.current_email` is the mutable sign-in address; unique, so an address\n * can never be claimed by two accounts.\n * - `users.username` is a separately-mutable public handle, not the identity.\n * - `user_emails` is the address registry: `is_verified = 1` may authenticate;\n * a changed-from address stays at `0` (attributable, non-authenticating) and\n * is re-pointable by a later claimant.\n *\n * The anchor design is forced on D1: it honours neither\n * `PRAGMA foreign_keys = off` nor `legacy_alter_table`, and `defer_foreign_keys`\n * does not suppress `ON DELETE CASCADE`, so a live reference cannot be repointed\n * without losing rows. A fresh install just starts with the anchor in place.\n *\n * A repo with legacy tables that reference `users(email)` applies this file's\n * statements and then adds its own additive backfill (the converged pattern is\n * in `upgrade-user-identity.sql`), repointing each legacy column through\n * `user_emails` rather than through the anchor.\n */\nconst USERS_SCHEMA = `\n-- The account. The anchor (email) is immutable; current_email is the login.\nCREATE TABLE IF NOT EXISTS users (\n id TEXT NOT NULL PRIMARY KEY,\n email TEXT NOT NULL UNIQUE,\n current_email TEXT NOT NULL UNIQUE,\n username TEXT,\n created_at INTEGER NOT NULL,\n updated_at INTEGER\n);\n\nCREATE UNIQUE INDEX IF NOT EXISTS idx_users_id ON users(id);\nCREATE UNIQUE INDEX IF NOT EXISTS idx_users_current_email ON users(current_email);\n\n-- The address registry: which account an address authenticates.\nCREATE TABLE IF NOT EXISTS user_emails (\n email TEXT PRIMARY KEY,\n user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,\n is_verified INTEGER NOT NULL DEFAULT 0,\n created_at INTEGER NOT NULL\n);\n\nCREATE INDEX IF NOT EXISTS idx_user_emails_user ON user_emails(user_id);\n`;\n\n/**\n * The additive upgrade for a database whose `users` predates the identity\n * columns — converged from `0004_user_identity.sql` (Durable-DAV) with the\n * table-specific phases (dav_volumes, namespaces) generalized into a\n * documented extension point, because every repo's legacy tables differ.\n *\n * Run order per phase matches Durable-DAV's, and each backfill is guarded so\n * the file is rerunnable: `users.id` is only filled where still missing, and\n * every registry insert is `INSERT OR IGNORE`.\n *\n * Why the address stays in `users` at all (the anchor question, answered once):\n * SQLite rewrites a child's foreign key clause when the parent is renamed and\n * drops the parent by cascading, so the reference from a legacy\n * `owner_email`-style column cannot be repointed at `users.id` without losing\n * rows. Keeping `email` as a frozen anchor sidesteps the rebuild entirely:\n * this migration is purely additive.\n *\n * A consumer with its own legacy tables appends one additive phase per table:\n * add the `*_user_id` column with `REFERENCES users(id) ON DELETE CASCADE`\n * (`NO ACTION` would *restrict* where the existing `owner_email` cascade\n * removed rows — the two references must cascade together), backfill it by\n * resolving the stored address through `user_emails`, and index it.\n */\nconst UPGRADE_USER_IDENTITY = `\n-- Phase 1: stable account key.\n-- SQLite cannot add a PRIMARY KEY column, so the id is a plain column with a\n-- unique index. A unique index is a valid foreign key parent, which is all the\n-- user_id references need.\nALTER TABLE users ADD COLUMN id TEXT;\n\nUPDATE users SET id = 'usr_' || lower(hex(randomblob(16))) WHERE id IS NULL;\n\nCREATE UNIQUE INDEX IF NOT EXISTS idx_users_id ON users(id);\n\n-- Phase 2: mutable sign-in address.\n-- 'email' stays as the frozen anchor; 'current_email' is what the account signs\n-- in with and what the API reports. Uniqueness is enforced here, so an address\n-- can never be claimed by two accounts.\nALTER TABLE users ADD COLUMN current_email TEXT;\n\nUPDATE users SET current_email = lower(email) WHERE current_email IS NULL;\n\nCREATE UNIQUE INDEX IF NOT EXISTS idx_users_current_email ON users(current_email);\n\n-- Phase 3: address registry.\n-- Login resolution consults is_verified = 1 only. Backfilled from the frozen\n-- anchor address of every existing account, lowercased so a legacy mixed-case\n-- row still yields exactly one login identity.\nCREATE TABLE IF NOT EXISTS user_emails (\n email TEXT PRIMARY KEY,\n user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,\n is_verified INTEGER NOT NULL DEFAULT 0,\n created_at INTEGER NOT NULL\n);\n\nCREATE INDEX IF NOT EXISTS idx_user_emails_user ON user_emails(user_id);\n\nINSERT OR IGNORE INTO user_emails (email, user_id, is_verified, created_at)\nSELECT lower(email), id, 1, created_at FROM users;\n`;\n\n/**\n * Every migration this package ships, in apply order — **the canonical\n * migrations export
|
|
4
|
+
"sourcesContent": ["/**\n * The canonical `users` / `user_emails` schema.\n *\n * Converged from migration `0001_init.sql` + `0004_user_identity.sql` across the\n * identity-carrying repos, expressed as the *final* shape so a fresh database\n * never has to be upgraded: `users.id` is the account key from the start, and\n * `user_emails` exists from the start.\n *\n * The invariants that make an address change safe (Durable-DAV's design, which\n * is the strictest in the family and the one every other repo's upgrade is\n * migrating toward):\n *\n * - `users.id` is an opaque, unique account key — never an address.\n * - `users.email` is the **frozen anchor**: immutable, the foreign-key target\n * every legacy `*_email` value resolves against, so history survives a change.\n * - `users.current_email` is the mutable sign-in address; unique, so an address\n * can never be claimed by two accounts.\n * - `users.username` is a separately-mutable public handle, not the identity.\n * - `user_emails` is the address registry: `is_verified = 1` may authenticate;\n * a changed-from address stays at `0` (attributable, non-authenticating) and\n * is re-pointable by a later claimant.\n *\n * The anchor design is forced on D1: it honours neither\n * `PRAGMA foreign_keys = off` nor `legacy_alter_table`, and `defer_foreign_keys`\n * does not suppress `ON DELETE CASCADE`, so a live reference cannot be repointed\n * without losing rows. A fresh install just starts with the anchor in place.\n *\n * A repo with legacy tables that reference `users(email)` applies this file's\n * statements and then adds its own additive backfill (the converged pattern is\n * in `upgrade-user-identity.sql`), repointing each legacy column through\n * `user_emails` rather than through the anchor.\n */\nconst USERS_SCHEMA = `\n-- The account. The anchor (email) is immutable; current_email is the login.\nCREATE TABLE IF NOT EXISTS users (\n id TEXT NOT NULL PRIMARY KEY,\n email TEXT NOT NULL UNIQUE,\n current_email TEXT NOT NULL UNIQUE,\n username TEXT,\n created_at INTEGER NOT NULL,\n updated_at INTEGER\n);\n\nCREATE UNIQUE INDEX IF NOT EXISTS idx_users_id ON users(id);\nCREATE UNIQUE INDEX IF NOT EXISTS idx_users_current_email ON users(current_email);\n\n-- The address registry: which account an address authenticates.\nCREATE TABLE IF NOT EXISTS user_emails (\n email TEXT PRIMARY KEY,\n user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,\n is_verified INTEGER NOT NULL DEFAULT 0,\n created_at INTEGER NOT NULL\n);\n\nCREATE INDEX IF NOT EXISTS idx_user_emails_user ON user_emails(user_id);\n`;\n\n/**\n * The additive upgrade for a database whose `users` predates the identity\n * columns — converged from `0004_user_identity.sql` (Durable-DAV) with the\n * table-specific phases (dav_volumes, namespaces) generalized into a\n * documented extension point, because every repo's legacy tables differ.\n *\n * Run order per phase matches Durable-DAV's, and each backfill is guarded so\n * the file is rerunnable: `users.id` is only filled where still missing, and\n * every registry insert is `INSERT OR IGNORE`.\n *\n * Why the address stays in `users` at all (the anchor question, answered once):\n * SQLite rewrites a child's foreign key clause when the parent is renamed and\n * drops the parent by cascading, so the reference from a legacy\n * `owner_email`-style column cannot be repointed at `users.id` without losing\n * rows. Keeping `email` as a frozen anchor sidesteps the rebuild entirely:\n * this migration is purely additive.\n *\n * A consumer with its own legacy tables appends one additive phase per table:\n * add the `*_user_id` column with `REFERENCES users(id) ON DELETE CASCADE`\n * (`NO ACTION` would *restrict* where the existing `owner_email` cascade\n * removed rows — the two references must cascade together), backfill it by\n * resolving the stored address through `user_emails`, and index it.\n */\nconst UPGRADE_USER_IDENTITY = `\n-- Phase 1: stable account key.\n-- SQLite cannot add a PRIMARY KEY column, so the id is a plain column with a\n-- unique index. A unique index is a valid foreign key parent, which is all the\n-- user_id references need.\nALTER TABLE users ADD COLUMN id TEXT;\n\nUPDATE users SET id = 'usr_' || lower(hex(randomblob(16))) WHERE id IS NULL;\n\nCREATE UNIQUE INDEX IF NOT EXISTS idx_users_id ON users(id);\n\n-- Phase 2: mutable sign-in address.\n-- 'email' stays as the frozen anchor; 'current_email' is what the account signs\n-- in with and what the API reports. Uniqueness is enforced here, so an address\n-- can never be claimed by two accounts.\nALTER TABLE users ADD COLUMN current_email TEXT;\n\nUPDATE users SET current_email = lower(email) WHERE current_email IS NULL;\n\nCREATE UNIQUE INDEX IF NOT EXISTS idx_users_current_email ON users(current_email);\n\n-- Phase 3: address registry.\n-- Login resolution consults is_verified = 1 only. Backfilled from the frozen\n-- anchor address of every existing account, lowercased so a legacy mixed-case\n-- row still yields exactly one login identity.\nCREATE TABLE IF NOT EXISTS user_emails (\n email TEXT PRIMARY KEY,\n user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,\n is_verified INTEGER NOT NULL DEFAULT 0,\n created_at INTEGER NOT NULL\n);\n\nCREATE INDEX IF NOT EXISTS idx_user_emails_user ON user_emails(user_id);\n\nINSERT OR IGNORE INTO user_emails (email, user_id, is_verified, created_at)\nSELECT lower(email), id, 1, created_at FROM users;\n`;\n\n/**\n * Every migration this package ships, in apply order — **the canonical\n * migrations export**.\n *\n * `upgrade-user-identity.sql` is *not* applied on a fresh database — the\n * fresh-install path is `users.sql` alone, and the upgrade exists only for a\n * legacy one.\n */\nconst IDENTITY_MIGRATIONS: ReadonlyArray<{ name: string; sql: string }> = [\n { name: '0001_users.sql', sql: USERS_SCHEMA },\n { name: '0002_upgrade_user_identity.sql', sql: UPGRADE_USER_IDENTITY },\n];\n\nexport { IDENTITY_MIGRATIONS, UPGRADE_USER_IDENTITY, USERS_SCHEMA };\n"],
|
|
5
5
|
"mappings": "AAgCA,MAAMA,EAAe;;;;;;;;;;;;;;;;;;;;;;;EAgDfC,EAAwB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EA8CxBC,EAAoE,CACxE,CAAE,KAAM,iBAAkB,IAAKF,CAAY,EAC3C,CAAE,KAAM,iCAAkC,IAAKC,CAAqB",
|
|
6
6
|
"names": ["USERS_SCHEMA", "UPGRADE_USER_IDENTITY", "IDENTITY_MIGRATIONS"]
|
|
7
7
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rexezuge/identity",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -25,9 +25,9 @@
|
|
|
25
25
|
"dist"
|
|
26
26
|
],
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@rexezuge/
|
|
29
|
-
"@rexezuge/
|
|
30
|
-
"@rexezuge/shared": "
|
|
28
|
+
"@rexezuge/errors": "2.0.0",
|
|
29
|
+
"@rexezuge/d1": "2.0.0",
|
|
30
|
+
"@rexezuge/shared": "2.0.0"
|
|
31
31
|
},
|
|
32
32
|
"devDependencies": {
|
|
33
33
|
"@types/node": "26.6.4",
|