@atscript/db 0.1.38 → 0.1.39

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 +41 -302
  2. package/package.json +3 -3
package/README.md CHANGED
@@ -1,24 +1,20 @@
1
- # @atscript/db
1
+ <p align="center">
2
+ <img src="https://atscript.moost.org/logo.svg" alt="Atscript" width="120" />
3
+ </p>
2
4
 
3
- Generic database abstraction layer for Atscript. Provides a unified CRUD interface driven by `@db.*` annotations, with pluggable database adapters.
5
+ <h1 align="center">@atscript/db</h1>
4
6
 
5
- ## Purpose
7
+ <p align="center">
8
+ <strong>Define your models once</strong> — get TypeScript types, runtime validation, and DB metadata from a single <code>.as</code> model.
9
+ </p>
6
10
 
7
- Full-stack Atscript projects define data models with `@db.*` annotations — table names, indexes, column mappings, defaults, primary keys. This package extracts all that metadata and provides:
11
+ <p align="center">
12
+ <a href="https://atscript.moost.org">Documentation</a> · <a href="https://atscript.moost.org/db/guide/">Database Guide</a>
13
+ </p>
8
14
 
9
- - **`AtscriptDbTable`** — a concrete class that reads `@db.*` annotations, pre-computes indexes and field metadata, orchestrates validation/defaults/column mapping, and delegates actual database calls to an adapter.
10
- - **`BaseDbAdapter`** — an abstract class that adapter authors extend to connect any database (MongoDB, SQLite, MySQL, PostgreSQL, etc.).
15
+ ---
11
16
 
12
- The same annotated type works with any adapter. Cross-cutting concerns (field-level permissions, audit logging, soft deletes) are added by subclassing `AtscriptDbTable` they work with every adapter automatically.
13
-
14
- ## Architecture
15
-
16
- ```
17
- AtscriptDbTable ──delegates CRUD──▶ BaseDbAdapter
18
- ◀──reads metadata── (via this._table)
19
- ```
20
-
21
- **One adapter per table.** The adapter gets a back-reference to the table instance via `registerTable()`, giving it full access to computed metadata (flatMap, indexes, primaryKeys, columnMap, etc.) for internal use in query rendering, index sync, and other adapter-specific logic.
17
+ Generic database abstraction layer for Atscript. Provides unified CRUD, relations, views, aggregations, and schema sync all driven by `@db.*` annotations in your `.as` models. Pluggable adapters connect any database engine (SQLite, PostgreSQL, MongoDB, MySQL).
22
18
 
23
19
  ## Installation
24
20
 
@@ -26,318 +22,61 @@ AtscriptDbTable ──delegates CRUD──▶ BaseDbAdapter
26
22
  pnpm add @atscript/db
27
23
  ```
28
24
 
29
- Peer dependencies: `@atscript/core`, `@atscript/typescript`.
30
-
31
25
  ## Quick Start
32
26
 
33
- ### 1. Define your type in Atscript (`.as` file)
34
-
35
27
  ```atscript
36
28
  @db.table "users"
37
- @db.schema "auth"
38
29
  interface User {
39
30
  @meta.id
31
+ @db.default.increment
40
32
  id: number
41
33
 
42
34
  @db.index.unique "email_idx"
43
- @db.column "email_address"
44
35
  email: string
45
36
 
46
- @db.index.plain "name_idx"
47
37
  name: string
48
-
49
- @db.default "active"
50
- status: string
51
-
52
- @db.ignore
53
- displayName?: string
54
38
  }
55
39
  ```
56
40
 
57
- ### 2. Create an adapter and table
58
-
59
41
  ```typescript
60
- import { AtscriptDbTable } from '@atscript/db'
61
- import { MyAdapter } from './my-adapter'
62
- import { User } from './user.as'
42
+ import { DbSpace } from '@atscript/db'
43
+ import { createAdapter } from '@atscript/db-sqlite'
63
44
 
64
- const adapter = new MyAdapter(/* db connection */)
65
- const users = new AtscriptDbTable(User, adapter)
45
+ const db = createAdapter('./myapp.db')
46
+ const users = db.getTable(User)
66
47
 
67
- // CRUD operations
68
48
  await users.insertOne({ name: 'John', email: 'john@example.com' })
69
- await users.findMany({ status: 'active' }, { limit: 10 })
70
- await users.deleteOne(123)
71
- await users.syncIndexes()
72
- ```
73
-
74
- ## Writing a Database Adapter
75
-
76
- Extend `BaseDbAdapter` and implement the abstract methods. The adapter receives a back-reference to the `AtscriptDbTable` instance — use `this._table` to access all computed metadata.
77
-
78
- ### Minimal adapter
79
-
80
- ```typescript
81
- import {
82
- BaseDbAdapter,
83
- type TDbFilter,
84
- type TDbFindOptions,
85
- type TDbInsertResult,
86
- type TDbInsertManyResult,
87
- type TDbUpdateResult,
88
- type TDbDeleteResult,
89
- } from '@atscript/db'
90
-
91
- class SqliteAdapter extends BaseDbAdapter {
92
- constructor(private db: SqliteDatabase) {
93
- super()
94
- }
95
-
96
- // Access table metadata via this._table:
97
- // this._table.tableName — resolved table name
98
- // this._table.schema — database schema/namespace
99
- // this._table.flatMap — Map<string, TAtscriptAnnotatedType>
100
- // this._table.indexes — Map<string, TDbIndex>
101
- // this._table.primaryKeys — readonly string[]
102
- // this._table.columnMap — Map<string, string> (logical → physical)
103
- // this._table.defaults — Map<string, TDbDefaultValue>
104
- // this._table.ignoredFields — Set<string>
105
- // this._table.uniqueProps — Set<string>
106
-
107
- async insertOne(data: Record<string, unknown>): Promise<TDbInsertResult> {
108
- const table = this._table.tableName
109
- const keys = Object.keys(data)
110
- const placeholders = keys.map(() => '?').join(', ')
111
- const sql = `INSERT INTO ${table} (${keys.join(', ')}) VALUES (${placeholders})`
112
- const result = this.db.run(sql, Object.values(data))
113
- return { insertedId: result.lastInsertRowid }
114
- }
115
-
116
- async insertMany(data: Record<string, unknown>[]): Promise<TDbInsertManyResult> {
117
- const ids: unknown[] = []
118
- for (const row of data) {
119
- const result = await this.insertOne(row)
120
- ids.push(result.insertedId)
121
- }
122
- return { insertedCount: ids.length, insertedIds: ids }
123
- }
124
-
125
- async findOne(
126
- filter: TDbFilter,
127
- options?: TDbFindOptions
128
- ): Promise<Record<string, unknown> | null> {
129
- const { sql, params } = this.buildSelect(filter, { ...options, limit: 1 })
130
- return this.db.get(sql, params) ?? null
131
- }
132
-
133
- async findMany(
134
- filter: TDbFilter,
135
- options?: TDbFindOptions
136
- ): Promise<Record<string, unknown>[]> {
137
- const { sql, params } = this.buildSelect(filter, options)
138
- return this.db.all(sql, params)
139
- }
140
-
141
- async updateOne(
142
- filter: TDbFilter,
143
- data: Record<string, unknown>
144
- ): Promise<TDbUpdateResult> {
145
- // ... build UPDATE ... SET ... WHERE ...
146
- }
147
-
148
- async replaceOne(
149
- filter: TDbFilter,
150
- data: Record<string, unknown>
151
- ): Promise<TDbUpdateResult> {
152
- // ... INSERT OR REPLACE ...
153
- }
154
-
155
- async deleteOne(filter: TDbFilter): Promise<TDbDeleteResult> {
156
- // ... DELETE FROM ... WHERE ...
157
- }
158
-
159
- async count(filter: TDbFilter): Promise<number> {
160
- // ... SELECT COUNT(*) ...
161
- }
162
-
163
- async updateMany(filter: TDbFilter, data: Record<string, unknown>): Promise<TDbUpdateResult> {
164
- // ... UPDATE ... SET ... WHERE ...
165
- }
166
-
167
- async replaceMany(filter: TDbFilter, data: Record<string, unknown>): Promise<TDbUpdateResult> {
168
- // ... batch replace ...
169
- }
170
-
171
- async deleteMany(filter: TDbFilter): Promise<TDbDeleteResult> {
172
- // ... DELETE FROM ... WHERE ...
173
- }
174
-
175
- async syncIndexes(): Promise<void> {
176
- // Read this._table.indexes and CREATE INDEX / DROP INDEX as needed
177
- for (const [key, index] of this._table.indexes) {
178
- const cols = index.fields.map(f =>
179
- `${f.name} ${f.sort === 'desc' ? 'DESC' : 'ASC'}`
180
- ).join(', ')
181
- const unique = index.type === 'unique' ? 'UNIQUE' : ''
182
- this.db.run(
183
- `CREATE ${unique} INDEX IF NOT EXISTS ${index.name}
184
- ON ${this._table.tableName} (${cols})`
185
- )
186
- }
187
- }
188
-
189
- async ensureTable(): Promise<void> {
190
- // Use this._table.flatMap, primaryKeys, etc. to build CREATE TABLE
191
- }
192
- }
193
- ```
194
-
195
- ### Adapter hooks
196
-
197
- Override these optional methods to process adapter-specific annotations during field scanning:
198
-
199
- | Hook | When it runs | Use case |
200
- |---|---|---|
201
- | `onBeforeFlatten(type)` | Before field scanning begins | Extract table-level adapter annotations |
202
- | `onFieldScanned(field, type, metadata)` | For each field during scanning | Extract field-level adapter annotations |
203
- | `onAfterFlatten()` | After all fields are scanned | Finalize adapter-specific computed state |
204
- | `getAdapterTableName(type)` | During constructor | Return adapter-specific table name (e.g., from `@db.mongo.collection`) |
205
- | `getTopLevelArrayTag()` | During flatten | Return custom tag for top-level array detection |
206
-
207
- ### Overridable behaviors
208
-
209
- | Method | Default | Override to... |
210
- |---|---|---|
211
- | `prepareId(id, fieldType)` | passthrough | Convert string → ObjectId, parse UUIDs, etc. |
212
- | `getValidatorPlugins()` | `[]` | Add adapter-specific validation (e.g., ObjectId format) |
213
- | `supportsNativePatch()` | `false` | Enable native array patch operations |
214
- | `nativePatch(filter, patch)` | throws | Implement native patch (e.g., MongoDB `$push`/`$pull`) |
215
-
216
- ## What `AtscriptDbTable` Does For You
217
-
218
- When you call `insertOne(payload)`, the table automatically:
219
-
220
- 1. **Flattens** the annotated type (lazy, cached) — extracts all fields, indexes, metadata
221
- 2. **Applies defaults** — fills `@db.default` fields that are missing
222
- 3. **Validates** — runs Atscript validators + adapter plugins
223
- 4. **Prepares IDs** — calls `adapter.prepareId()` on primary key fields
224
- 5. **Strips ignored fields** — removes `@db.ignore` fields
225
- 6. **Maps columns** — renames `@db.column` logical names to physical names
226
- 7. **Delegates** — calls `adapter.insertOne()` with the cleaned data
227
-
228
- For `updateOne()`, it additionally:
229
- - Extracts a filter from primary key fields in the payload
230
- - Routes to `adapter.nativePatch()` if supported, otherwise decomposes the patch generically
231
-
232
- ## Supported Annotations
233
-
234
- These `@db.*` annotations are defined in `@atscript/core` and processed by `AtscriptDbTable`:
235
-
236
- | Annotation | Level | Purpose |
237
- |---|---|---|
238
- | `@db.table "name"` | Interface | Table/collection name |
239
- | `@db.schema "name"` | Interface | Database schema/namespace |
240
- | `@meta.id` | Field | Marks primary key (no args; multiple = composite key) |
241
- | `@db.column "name"` | Field | Physical column name override |
242
- | `@db.default "val"` | Field | Default value on insert |
243
- | `@db.default.increment` | Field | Auto-incrementing integer default |
244
- | `@db.default.uuid` | Field | UUID generation default |
245
- | `@db.default.now` | Field | Current timestamp default |
246
- | `@db.ignore` | Field | Exclude from database operations |
247
- | `@db.index.plain "name"` | Field | B-tree index (optional sort: `"name", "desc"`) |
248
- | `@db.index.unique "name"` | Field | Unique index |
249
- | `@db.index.fulltext "name"` | Field | Full-text search index |
250
- | `@db.json` | Field | Store as a single JSON column (skip flattening) |
251
-
252
- Multiple fields with the same index name form a **composite index**.
253
-
254
- ## Type-Safe Queries with `__flat`
255
-
256
- Interfaces annotated with `@db.table` get a `__flat` static property in the generated `.d.ts` file, mapping all dot-notation paths to their value types. This enables autocomplete and type checking for filter expressions and `$select`/`$sort` operations.
257
-
258
- Use `FlatOf<T>` to extract the flat type:
259
-
260
- ```typescript
261
- import type { FlatOf } from '@atscript/db'
262
-
263
- type UserFlat = FlatOf<typeof User>
264
- // → { id: number; name: string; contact: never; "contact.email": string; ... }
49
+ const all = await users.findMany({ filter: { name: { $eq: 'John' } } })
265
50
  ```
266
51
 
267
- `AtscriptDbTable` uses `FlatOf<T>` as the type parameter for query methods (`findOne`, `findMany`, `count`, `updateMany`, `replaceMany`, `deleteMany`), giving you autocomplete on filter keys and select paths. When `__flat` is not present (no `@db.table`), `FlatOf<T>` falls back to the regular data shape — fully backward-compatible.
52
+ ## Sub-entries
268
53
 
269
- ### `$select` Parent Path Expansion
54
+ | Entry | Purpose |
55
+ |---|---|
56
+ | `@atscript/db/plugin` | `dbPlugin()` — registers all `@db.*` annotations |
57
+ | `@atscript/db/rel` | Relation loading and nested writes |
58
+ | `@atscript/db/agg` | Aggregation query validation |
59
+ | `@atscript/db/sync` | Schema sync with drift detection and distributed locking |
60
+ | `@atscript/db/shared` | Annotation helpers for adapter plugins |
270
61
 
271
- Selecting a parent object path (e.g., `$select: ['contact']`) automatically expands to all its leaf physical columns for relational databases. Sorting by parent paths is silently ignored.
62
+ ## Features
272
63
 
273
- ## Cross-Cutting Concerns
64
+ - Annotation-driven schema: table names, indexes, column mappings, defaults, primary keys
65
+ - Pluggable adapters via `BaseDbAdapter` — same code works with any database
66
+ - Automatic write pipeline: defaults, validation, ID preparation, column mapping
67
+ - Embedded object flattening (`__`-separated columns) and `@db.json` storage
68
+ - Relations: `@db.rel.to`, `@db.rel.from`, `@db.rel.via` with nested writes
69
+ - Views: `@db.view` with joins, filters, materialized views, aggregation
70
+ - Schema sync: FNV-1a hash drift detection, distributed locking, column/table renames
71
+ - Array patch operations: `$insert`, `$upsert`, `$update`, `$remove`, `$replace`
72
+ - Type-safe queries with `FlatOf<T>` for autocomplete on filters and projections
274
73
 
275
- Since `AtscriptDbTable` is concrete, extend it for cross-cutting concerns that work with any adapter:
74
+ ## Documentation
276
75
 
277
- ```typescript
278
- class SecureDbTable extends AtscriptDbTable {
279
- constructor(type, adapter, private permissions: PermissionConfig) {
280
- super(type, adapter)
281
- }
282
-
283
- async insertOne(payload) {
284
- this.checkPermission('write', payload)
285
- return super.insertOne(payload)
286
- }
287
-
288
- async findOne(filter, options) {
289
- const result = await super.findOne(filter, options)
290
- return result ? this.filterFields(result, 'read') : null
291
- }
292
- }
293
-
294
- // Works with any adapter:
295
- const secureUsers = new SecureDbTable(User, new MongoAdapter(db), perms)
296
- const secureOrders = new SecureDbTable(Order, new SqliteAdapter(db), perms)
297
- ```
298
-
299
- ## Array Patch Operations
300
-
301
- For fields that are arrays of objects, `updateOne()` supports structured patch operators:
302
-
303
- ```typescript
304
- await users.updateOne({
305
- id: 123,
306
- tags: {
307
- $insert: [{ name: 'new-tag' }], // append items
308
- $upsert: [{ name: 'existing', value: 1 }], // insert or replace by key
309
- $update: [{ name: 'existing', value: 2 }], // partial update by key
310
- $remove: [{ name: 'old-tag' }], // remove by key
311
- $replace: [/* full replacement */], // replace entire array
312
- }
313
- })
314
- ```
315
-
316
- Array element identity uses `@expect.array.key` annotations. Adapters with native patch support (e.g., MongoDB's `$push`/`$pull`) can implement `nativePatch()` for optimal performance. Otherwise, `decomposePatch()` provides a generic decomposition.
317
-
318
- ## Exports
319
-
320
- ```typescript
321
- // Classes
322
- export { AtscriptDbTable } from './db-table'
323
- export { BaseDbAdapter } from './base-adapter'
324
-
325
- // Utilities
326
- export { decomposePatch } from './patch-decomposer'
327
- export { getKeyProps } from './patch-types'
328
-
329
- // Types
330
- export type { FlatOf } from '@atscript/typescript/utils'
331
- export type {
332
- TDbFilter, TDbFindOptions,
333
- TDbInsertResult, TDbInsertManyResult, TDbUpdateResult, TDbDeleteResult,
334
- TDbIndex, TDbIndexField, TDbDefaultValue, TIdDescriptor, TDbFieldMeta,
335
- TArrayPatch, TDbPatch,
336
- } from './types'
337
- export type { TGenericLogger } from './logger'
338
- export { NoopLogger } from './logger'
339
- ```
76
+ - [Database Guide](https://atscript.moost.org/db/guide/)
77
+ - [API & Annotations](https://atscript.moost.org/db/api/tables)
78
+ - [Full Documentation](https://atscript.moost.org)
340
79
 
341
80
  ## License
342
81
 
343
- ISC
82
+ MIT
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atscript/db",
3
- "version": "0.1.38",
3
+ "version": "0.1.39",
4
4
  "description": "Database adapter utilities for atscript.",
5
5
  "keywords": [
6
6
  "atscript",
@@ -70,8 +70,8 @@
70
70
  },
71
71
  "peerDependencies": {
72
72
  "@uniqu/core": "^0.1.2",
73
- "@atscript/core": "^0.1.38",
74
- "@atscript/typescript": "^0.1.38"
73
+ "@atscript/core": "^0.1.39",
74
+ "@atscript/typescript": "^0.1.39"
75
75
  },
76
76
  "scripts": {
77
77
  "before-build": "node ../typescript/cli.cjs -f js",