@atscript/db-sqlite 0.1.39 → 0.1.40

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/dist/index.d.ts DELETED
@@ -1,195 +0,0 @@
1
- import { BaseDbAdapter, TDbInsertResult, TDbInsertManyResult, DbQuery, FilterExpr, TDbUpdateResult, TDbDeleteResult, TExistingColumn, TColumnDiff, TSyncColumnResult, TSearchIndexInfo, DbSpace } from '@atscript/db';
2
- import { TAtscriptAnnotatedType } from '@atscript/typescript/utils';
3
- import * as better_sqlite3 from 'better-sqlite3';
4
- import { FilterExpr as FilterExpr$1 } from '@uniqu/core';
5
- import { TSqlFragment } from '@atscript/db-sql-tools';
6
- export { TSqlFragment } from '@atscript/db-sql-tools';
7
-
8
- /**
9
- * Result of a SQL statement that modifies data (INSERT, UPDATE, DELETE).
10
- */
11
- interface TSqliteRunResult {
12
- /** Number of rows changed by the statement. */
13
- changes: number;
14
- /** Rowid of the last inserted row (for INSERT statements). */
15
- lastInsertRowid: number | bigint;
16
- }
17
- /**
18
- * Minimal driver interface for SQLite engines.
19
- *
20
- * Intentionally synchronous — SQLite is an embedded engine with no network I/O.
21
- * Both `better-sqlite3` and `node:sqlite` (DatabaseSync) are synchronous.
22
- * The {@link SqliteAdapter} wraps these calls in promises for the async
23
- * {@link BaseDbAdapter} contract.
24
- *
25
- * For async drivers (e.g., `sql.js`), create a synchronous wrapper
26
- * or implement a custom adapter.
27
- */
28
- interface TSqliteDriver {
29
- /**
30
- * Execute a SQL statement that doesn't return rows.
31
- * Used for INSERT, UPDATE, DELETE, CREATE, DROP, etc.
32
- */
33
- run(sql: string, params?: unknown[]): TSqliteRunResult;
34
- /**
35
- * Execute a query and return all matching rows.
36
- */
37
- all<T = Record<string, unknown>>(sql: string, params?: unknown[]): T[];
38
- /**
39
- * Execute a query and return the first matching row, or null.
40
- */
41
- get<T = Record<string, unknown>>(sql: string, params?: unknown[]): T | null;
42
- /**
43
- * Execute raw SQL without returning results.
44
- * Used for multi-statement strings like PRAGMA or BEGIN/COMMIT.
45
- */
46
- exec(sql: string): void;
47
- /**
48
- * Close the database connection.
49
- */
50
- close(): void;
51
- }
52
-
53
- /**
54
- * SQLite adapter for {@link AtscriptDbTable}.
55
- *
56
- * Accepts any {@link TSqliteDriver} implementation — the actual SQLite engine
57
- * is fully swappable (better-sqlite3, node:sqlite, sql.js, etc.).
58
- *
59
- * Usage:
60
- * ```typescript
61
- * import { BetterSqlite3Driver } from '@atscript/db-sqlite'
62
- *
63
- * const driver = new BetterSqlite3Driver(':memory:')
64
- * const adapter = new SqliteAdapter(driver)
65
- * const users = new AtscriptDbTable(UsersType, adapter)
66
- * ```
67
- */
68
- declare class SqliteAdapter extends BaseDbAdapter {
69
- protected readonly driver: TSqliteDriver;
70
- supportsNativeValueDefaults(): boolean;
71
- constructor(driver: TSqliteDriver);
72
- protected _beginTransaction(): Promise<unknown>;
73
- protected _commitTransaction(): Promise<void>;
74
- protected _rollbackTransaction(): Promise<void>;
75
- /** SQLite does not use schemas — override to always exclude schema. */
76
- resolveTableName(): string;
77
- /** SQLite enforces FK constraints natively via PRAGMA foreign_keys. */
78
- supportsNativeForeignKeys(): boolean;
79
- prepareId(id: unknown, _fieldType: TAtscriptAnnotatedType): unknown;
80
- /**
81
- * Wraps a write operation to catch native SQLite constraint errors
82
- * and rethrow as structured `DbError`.
83
- */
84
- private _wrapConstraintError;
85
- insertOne(data: Record<string, unknown>): Promise<TDbInsertResult>;
86
- insertMany(data: Array<Record<string, unknown>>): Promise<TDbInsertManyResult>;
87
- findOne(query: DbQuery): Promise<Record<string, unknown> | null>;
88
- findMany(query: DbQuery): Promise<Array<Record<string, unknown>>>;
89
- count(query: DbQuery): Promise<number>;
90
- aggregate(query: DbQuery): Promise<Array<Record<string, unknown>>>;
91
- updateOne(filter: FilterExpr, data: Record<string, unknown>): Promise<TDbUpdateResult>;
92
- updateMany(filter: FilterExpr, data: Record<string, unknown>): Promise<TDbUpdateResult>;
93
- replaceOne(filter: FilterExpr, data: Record<string, unknown>): Promise<TDbUpdateResult>;
94
- replaceMany(filter: FilterExpr, data: Record<string, unknown>): Promise<TDbUpdateResult>;
95
- deleteOne(filter: FilterExpr): Promise<TDbDeleteResult>;
96
- deleteMany(filter: FilterExpr): Promise<TDbDeleteResult>;
97
- ensureTable(): Promise<void>;
98
- private _incrementSeeded;
99
- /**
100
- * Seeds the sqlite_sequence table for auto-increment fields that have a start value.
101
- * Only applies once per adapter instance (idempotent via INSERT OR IGNORE + flag).
102
- */
103
- private _seedIncrementStart;
104
- ensureView(): Promise<void>;
105
- getExistingColumns(): Promise<TExistingColumn[]>;
106
- syncColumns(diff: TColumnDiff): Promise<TSyncColumnResult>;
107
- recreateTable(): Promise<void>;
108
- dropTable(): Promise<void>;
109
- dropColumns(columns: string[]): Promise<void>;
110
- dropTableByName(tableName: string): Promise<void>;
111
- dropViewByName(viewName: string): Promise<void>;
112
- renameTable(oldName: string): Promise<void>;
113
- typeMapper(field: {
114
- designType: string;
115
- isPrimaryKey: boolean;
116
- }): string;
117
- getExistingColumnsForTable(tableName: string): Promise<TExistingColumn[]>;
118
- syncIndexes(): Promise<void>;
119
- getSearchIndexes(): TSearchIndexInfo[];
120
- search(text: string, query: DbQuery, indexName?: string): Promise<Array<Record<string, unknown>>>;
121
- searchWithCount(text: string, query: DbQuery, indexName?: string): Promise<{
122
- data: Array<Record<string, unknown>>;
123
- count: number;
124
- }>;
125
- /** Builds FTS table name from index name: `<table>__fts__<indexName>`. */
126
- private _ftsTableName;
127
- /** Returns fulltext indexes from table metadata. */
128
- private _getFulltextIndexes;
129
- /** Resolves a fulltext index by name, or returns the first available. */
130
- private _resolveFtsIndex;
131
- /**
132
- * Builds the shared FROM+JOIN+WHERE fragment for FTS5 queries.
133
- * Both data and count queries reuse this to avoid duplicating index resolution and filter translation.
134
- */
135
- private _buildFtsBase;
136
- /**
137
- * Creates/drops FTS5 virtual tables and sync triggers to match desired fulltext indexes.
138
- */
139
- private _syncFtsIndexes;
140
- /** Creates an FTS5 virtual table with sync triggers and rebuilds the index. */
141
- private _createFtsTable;
142
- /** Drops an FTS5 virtual table and its sync triggers. */
143
- private _dropFtsTable;
144
- /** Drops all FTS virtual tables and triggers for a content table. */
145
- private _dropAllFtsTables;
146
- }
147
-
148
- /**
149
- * {@link TSqliteDriver} implementation backed by `better-sqlite3`.
150
- *
151
- * Accepts either a file path (opens a new database) or a pre-created
152
- * `Database` instance from `better-sqlite3`.
153
- *
154
- * ```typescript
155
- * import { BetterSqlite3Driver } from '@atscript/db-sqlite'
156
- *
157
- * // In-memory database
158
- * const driver = new BetterSqlite3Driver(':memory:')
159
- *
160
- * // File-based database
161
- * const driver = new BetterSqlite3Driver('./my-data.db')
162
- *
163
- * // Pre-created instance
164
- * import Database from 'better-sqlite3'
165
- * const db = new Database(':memory:', { verbose: console.log })
166
- * const driver = new BetterSqlite3Driver(db)
167
- * ```
168
- *
169
- * Requires `better-sqlite3` to be installed:
170
- * ```bash
171
- * pnpm add better-sqlite3
172
- * ```
173
- */
174
- declare class BetterSqlite3Driver implements TSqliteDriver {
175
- private db;
176
- constructor(pathOrDb: string | better_sqlite3.Database, options?: Record<string, unknown>);
177
- run(sql: string, params?: unknown[]): TSqliteRunResult;
178
- all<T = Record<string, unknown>>(sql: string, params?: unknown[]): T[];
179
- get<T = Record<string, unknown>>(sql: string, params?: unknown[]): T | null;
180
- exec(sql: string): void;
181
- close(): void;
182
- }
183
-
184
- /**
185
- * Translates a uniqu filter expression into a parameterized SQL WHERE clause.
186
- *
187
- * @returns `{ sql, params }` — the WHERE clause (without "WHERE") and bound params.
188
- * Returns `{ sql: '1=1', params: [] }` for empty/null filters.
189
- */
190
- declare function buildWhere(filter: FilterExpr$1): TSqlFragment;
191
-
192
- declare function createAdapter(connection: string, options?: Record<string, unknown>): DbSpace;
193
-
194
- export { BetterSqlite3Driver, SqliteAdapter, buildWhere, createAdapter };
195
- export type { TSqliteDriver, TSqliteRunResult };