@puzzle-ui/storage 0.5.0 → 0.6.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 (2) hide show
  1. package/README.md +32 -95
  2. package/package.json +6 -1
package/README.md CHANGED
@@ -1,119 +1,56 @@
1
1
  # @puzzle-ui/storage
2
2
 
3
- Vendor-neutral storage contracts for Puzzle.
3
+ Vendor-neutral storage contracts for Puzzle Auth and other packages. This package defines the typed adapter API, query language, migration protocol, and reusable adapter conformance suites; it does not bundle a database implementation.
4
4
 
5
- This package defines the persistence API used by Puzzle Auth and other
6
- packages. It does not depend on a database vendor, database SDK, framework,
7
- or runtime-specific implementation.
5
+ **Documentation: [puzzle-ui.com/docs/storage](https://puzzle-ui.com/docs/storage)**
8
6
 
9
- ## Responsibilities
7
+ ## Install
10
8
 
11
- - Typed collection and record contracts.
12
- - Query AST and cursor pagination contracts.
13
- - CRUD, upsert and conditional write contracts.
14
- - Optimistic concurrency through record revisions.
15
- - Capability discovery.
16
- - Atomic batch contract.
17
- - Collection schemas and index definitions.
18
- - Migration contracts and migration status.
19
- - Reusable adapter conformance requirements.
20
-
21
- ## Architecture
22
-
23
- Database-specific implementations belong in separate packages:
24
-
25
- - `@puzzle-ui/storage-mongodb`
26
- - `@puzzle-ui/storage-d1`
27
-
28
- Neither adapter may leak its database SDK or query language through this
29
- package.
30
-
31
- ## Records
32
-
33
- Every persisted record has:
34
-
35
- - `id`
36
- - `createdAt`
37
- - `updatedAt`
38
- - `revision`
39
-
40
- The revision is incremented by successful updates. Adapters must use it
41
- for optimistic concurrency when a caller supplies `expectedRevision`.
42
-
43
- ## Atomicity
44
-
45
- An adapter may advertise `atomicBatch: true` only when it guarantees
46
- all-or-nothing execution.
47
-
48
- If a batch fails, all changes made by that batch must be rolled back.
49
- Adapters without this guarantee must reject `atomicBatch` calls with
50
- `StorageAtomicityError`.
51
-
52
- Security-critical operations must fail closed when the required
53
- capability is unavailable.
54
-
55
- ## Query contract
56
-
57
- Queries use a vendor-neutral AST:
58
-
59
- - `eq`, `ne`
60
- - `in`, `notIn`
61
- - `gt`, `gte`, `lt`, `lte`
62
- - `exists`
63
- - nested `and` and `or`
64
- - ordering
65
- - offset and limit
66
- - cursor pagination
67
-
68
- Adapters are responsible for translating this AST into their native
69
- query mechanism without changing its semantics.
9
+ ```sh
10
+ npm install @puzzle-ui/storage
11
+ # or: bun add @puzzle-ui/storage
12
+ ```
70
13
 
71
- ## Schemas and indexes
14
+ Add a database adapter when you need an implementation: [`@puzzle-ui/storage-d1`](https://puzzle-ui.com/docs/storage#cloudflare-d1-adapter) for Cloudflare D1 or [`@puzzle-ui/storage-mongodb`](https://puzzle-ui.com/docs/storage#mongodb-adapter) for MongoDB.
72
15
 
73
- Collection schemas validate records at the storage boundary.
16
+ ## Use
74
17
 
75
- Indexes can declare:
18
+ Define record types by extending `StorageRecord`, then use an adapter supplied by your application:
76
19
 
77
- - Multiple indexed fields.
78
- - Uniqueness.
79
- - TTL expiration.
20
+ ```ts
21
+ import type { StorageAdapter, StorageRecord } from "@puzzle-ui/storage";
80
22
 
81
- An adapter must report unsupported index capabilities instead of
82
- silently ignoring requested guarantees.
23
+ type Session = StorageRecord & { userId: string; expiresAt: Date };
83
24
 
84
- ## Migrations
25
+ const storage: StorageAdapter = getConfiguredStorage();
26
+ await storage.registerCollection<Session>({
27
+ name: "sessions",
28
+ indexes: [{ name: "by-user", fields: ["userId"] }],
29
+ });
85
30
 
86
- Migrations are explicit and versioned. They are never run implicitly
87
- during ordinary reads or writes.
31
+ const session = await storage.insert<Session>("sessions", {
32
+ id: "session_123",
33
+ userId: "user_123",
34
+ expiresAt: new Date(Date.now() + 60 * 60 * 1000),
35
+ });
36
+ ```
88
37
 
89
- A migration adapter must provide durable migration status and prevent
90
- concurrent migration runners from applying the same migration twice.
38
+ `getConfiguredStorage` is your own adapter factory. Records have an ID, creation/update timestamps, and a revision. The query AST supports comparisons, nested boolean conditions, sorting, offset/limit, and cursor pagination. Conditional writes use expected revisions or update times; adapters advertise guarantees through capability reports. Migrations are explicit and versioned.
91
39
 
92
- ## Adapter requirements
40
+ ## Entry points
93
41
 
94
- Every production adapter must pass the same conformance suite.
42
+ - `@puzzle-ui/storage`: storage types, adapter and migration contracts, query validators, and storage errors.
43
+ - `@puzzle-ui/storage/testing`: Bun conformance suites for adapter and migration implementations.
95
44
 
96
- At minimum, conformance must verify:
45
+ The conformance kit checks core CRUD/query semantics, serialization, revisions, constraints, pagination, batch atomicity, capabilities, and migration behavior. Adapters should run it against isolated test databases before release. `bun run typecheck` also checks the conformance sources with Bun test types.
97
46
 
98
- - CRUD and upsert semantics.
99
- - Query operator semantics.
100
- - Serialization of dates and null values.
101
- - Conditional writes and revision conflicts.
102
- - Unique constraint behavior.
103
- - Cursor pagination.
104
- - Atomic batch rollback.
105
- - Capability reporting.
106
- - Migration ordering and concurrency.
47
+ `StorageSchema` provides an optional type-guard shape, but the shipped D1 and MongoDB adapters do not invoke it automatically; validate untrusted records at your application boundary.
107
48
 
108
- ## Build
49
+ ## Development
109
50
 
110
51
  ```sh
111
52
  bun run build
112
53
  bun run typecheck
113
54
  ```
114
55
 
115
- The typecheck includes all files under `tests/conformance` and loads Bun's
116
- test types for their `bun:test` imports.
117
-
118
- This package contains contracts only. It does not provide a production
119
- database implementation or an in-memory persistence implementation.
56
+ See the [storage guide](https://puzzle-ui.com/docs/storage) for contract details, migrations, adapter setup, and custom-adapter guidance.
package/package.json CHANGED
@@ -1,7 +1,12 @@
1
1
  {
2
2
  "name": "@puzzle-ui/storage",
3
- "version": "0.5.0",
3
+ "version": "0.6.0",
4
4
  "description": "Vendor-neutral storage contracts for Puzzle Auth.",
5
+ "homepage": "https://puzzle-ui.com",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/senorprogramador/puzzle.git"
9
+ },
5
10
  "license": "MIT",
6
11
  "publishConfig": {
7
12
  "access": "public",