@plakboek/db 0.1.2 → 0.2.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.
Files changed (3) hide show
  1. package/README.md +5 -4
  2. package/dist/index.js +114 -5
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -67,10 +67,11 @@ order. A second overlapping call either observes the first call's
67
67
  migrations as already applied, or throws `MigrationLockTimeoutError` if it
68
68
  gives up waiting for the lock before the first call finishes.
69
69
 
70
- `MIGRATIONS` ships empty at `0.1.0` -- no product schema exists before
71
- Phase 2. See [`MIGRATIONS.md`](./MIGRATIONS.md) for how later phases add to
72
- it, the transactional-vs-existence-guarded-statement decision, and what
73
- each thrown error class means.
70
+ `MIGRATIONS` starts with `0001_auth_core`, which creates the authentication
71
+ tables (`"user"`, `session`, `account`, `verification`, `two_factor`) and
72
+ the append-only `audit_log`. See [`MIGRATIONS.md`](./MIGRATIONS.md) for how
73
+ later migrations are added, the transactional-vs-existence-guarded-statement
74
+ decision, and what each thrown error class means.
74
75
 
75
76
  ## Errors
76
77
 
package/dist/index.js CHANGED
@@ -141,12 +141,121 @@ async function withMigrationLock(client, fn, options) {
141
141
  * Corrections are always a new, higher-numbered migration. See
142
142
  * ../../MIGRATIONS.md for the full authoring convention.
143
143
  *
144
- * MIGRATIONS ships empty at 0.1.0: no product table exists before Phase 2
145
- * introduces the first schema. The runner, bookkeeping table, advisory
146
- * lock, and every safety check are fully exercised through fixture
147
- * registries in this package's own tests (tests/fixtures/migrations.ts).
144
+ * `0001_auth_core` is the first shipped entry: the better-auth core tables
145
+ * and the audit log. The runner's failure modes (ordering, checksums,
146
+ * partial application, locking) are exercised through fixture registries in
147
+ * this package's own tests (tests/fixtures/migrations.ts).
148
148
  */
149
- const MIGRATIONS = Object.freeze([]);
149
+ const MIGRATIONS = Object.freeze([{
150
+ name: "0001_auth_core",
151
+ transactional: true,
152
+ sql: `
153
+ CREATE TABLE IF NOT EXISTS "user" (
154
+ "id" text PRIMARY KEY NOT NULL,
155
+ "name" text NOT NULL,
156
+ "email" text NOT NULL,
157
+ "email_verified" boolean DEFAULT false NOT NULL,
158
+ "image" text,
159
+ "created_at" timestamp with time zone NOT NULL,
160
+ "updated_at" timestamp with time zone NOT NULL,
161
+ "role_key" text,
162
+ "banned" boolean,
163
+ "ban_reason" text,
164
+ "ban_expires" timestamp with time zone,
165
+ "two_factor_enabled" boolean DEFAULT false NOT NULL,
166
+ CONSTRAINT "user_email_unique" UNIQUE ("email")
167
+ );
168
+
169
+ CREATE TABLE IF NOT EXISTS "session" (
170
+ "id" text PRIMARY KEY NOT NULL,
171
+ "expires_at" timestamp with time zone NOT NULL,
172
+ "token" text NOT NULL,
173
+ "created_at" timestamp with time zone NOT NULL,
174
+ "updated_at" timestamp with time zone NOT NULL,
175
+ "ip_address" text,
176
+ "user_agent" text,
177
+ "user_id" text NOT NULL,
178
+ "impersonated_by" text,
179
+ CONSTRAINT "session_token_unique" UNIQUE ("token"),
180
+ CONSTRAINT "session_user_id_user_id_fk" FOREIGN KEY ("user_id")
181
+ REFERENCES "user" ("id") ON DELETE CASCADE
182
+ );
183
+
184
+ CREATE TABLE IF NOT EXISTS "account" (
185
+ "id" text PRIMARY KEY NOT NULL,
186
+ "account_id" text NOT NULL,
187
+ "provider_id" text NOT NULL,
188
+ "user_id" text NOT NULL,
189
+ "access_token" text,
190
+ "refresh_token" text,
191
+ "id_token" text,
192
+ "access_token_expires_at" timestamp with time zone,
193
+ "refresh_token_expires_at" timestamp with time zone,
194
+ "scope" text,
195
+ "password" text,
196
+ "created_at" timestamp with time zone NOT NULL,
197
+ "updated_at" timestamp with time zone NOT NULL,
198
+ CONSTRAINT "account_user_id_user_id_fk" FOREIGN KEY ("user_id")
199
+ REFERENCES "user" ("id") ON DELETE CASCADE
200
+ );
201
+
202
+ CREATE TABLE IF NOT EXISTS "verification" (
203
+ "id" text PRIMARY KEY NOT NULL,
204
+ "identifier" text NOT NULL,
205
+ "value" text NOT NULL,
206
+ "expires_at" timestamp with time zone NOT NULL,
207
+ "created_at" timestamp with time zone NOT NULL,
208
+ "updated_at" timestamp with time zone NOT NULL
209
+ );
210
+
211
+ CREATE TABLE IF NOT EXISTS "two_factor" (
212
+ "id" text PRIMARY KEY NOT NULL,
213
+ "secret" text NOT NULL,
214
+ "backup_codes" text NOT NULL,
215
+ "user_id" text NOT NULL,
216
+ "verified" boolean DEFAULT true,
217
+ "failed_verification_count" integer DEFAULT 0 NOT NULL,
218
+ "locked_until" timestamp with time zone,
219
+ CONSTRAINT "two_factor_user_id_user_id_fk" FOREIGN KEY ("user_id")
220
+ REFERENCES "user" ("id") ON DELETE CASCADE
221
+ );
222
+
223
+ CREATE TABLE IF NOT EXISTS "audit_log" (
224
+ "id" bigserial PRIMARY KEY NOT NULL,
225
+ "actor_user_id" text,
226
+ "actor_role_key" text NOT NULL,
227
+ "impersonator_user_id" text,
228
+ "permission" text NOT NULL,
229
+ "action" text NOT NULL,
230
+ "entity_type" text NOT NULL,
231
+ "entity_id" text,
232
+ "outcome" text NOT NULL,
233
+ "before" jsonb,
234
+ "after" jsonb,
235
+ "created_at" timestamp with time zone DEFAULT now() NOT NULL,
236
+ CONSTRAINT "audit_log_actor_user_id_user_id_fk" FOREIGN KEY ("actor_user_id")
237
+ REFERENCES "user" ("id") ON DELETE SET NULL,
238
+ CONSTRAINT "audit_log_impersonator_user_id_user_id_fk"
239
+ FOREIGN KEY ("impersonator_user_id")
240
+ REFERENCES "user" ("id") ON DELETE SET NULL
241
+ );
242
+
243
+ CREATE INDEX IF NOT EXISTS "session_user_id_idx"
244
+ ON "session" USING btree ("user_id");
245
+ CREATE INDEX IF NOT EXISTS "account_user_id_idx"
246
+ ON "account" USING btree ("user_id");
247
+ CREATE INDEX IF NOT EXISTS "verification_identifier_idx"
248
+ ON "verification" USING btree ("identifier");
249
+ CREATE INDEX IF NOT EXISTS "two_factor_user_id_idx"
250
+ ON "two_factor" USING btree ("user_id");
251
+ CREATE INDEX IF NOT EXISTS "two_factor_secret_idx"
252
+ ON "two_factor" USING btree ("secret");
253
+ CREATE INDEX IF NOT EXISTS "audit_log_actor_idx"
254
+ ON "audit_log" USING btree ("actor_user_id", "id");
255
+ CREATE INDEX IF NOT EXISTS "audit_log_created_at_idx"
256
+ ON "audit_log" USING btree ("created_at");
257
+ `
258
+ }]);
150
259
  //#endregion
151
260
  //#region src/migrate.ts
152
261
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plakboek/db",
3
- "version": "0.1.2",
3
+ "version": "0.2.0",
4
4
  "description": "Postgres connection factory and migration-lock primitives for Plakboek CMS installations",
5
5
  "homepage": "https://github.com/flovan/plakboek/tree/main/packages/db#readme",
6
6
  "bugs": {