@an5/adapters 0.2.5 → 0.2.9
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 +283 -305
- package/dist/an5Adapter.d.ts +142 -7
- package/dist/an5Adapter.js +998 -68
- package/dist/base/metadata.d.ts +10 -0
- package/dist/base/metadata.js +107 -0
- package/dist/base/sql.d.ts +19 -1
- package/dist/base/sql.js +152 -24
- package/dist/base/types.d.ts +10 -1
- package/dist/browser.d.ts +4 -1
- package/dist/browser.js +9 -2
- package/dist/googlesheets/adapter.d.ts +1 -0
- package/dist/googlesheets/adapter.js +9 -2
- package/dist/googlesheets/config.d.ts +4 -4
- package/dist/googlesheets/helpers.js +2 -0
- package/dist/googlesheets/parseConnectionString.js +5 -2
- package/dist/googlesheets/sqlExecutor.js +29 -24
- package/dist/googlesheets/tableClient.js +14 -5
- package/dist/index.d.ts +5 -1
- package/dist/index.js +15 -1
- package/dist/mssql/engine.js +8 -4
- package/dist/mysql/engine.js +1 -1
- package/dist/nbase/client.d.ts +92 -0
- package/dist/nbase/client.js +147 -0
- package/dist/nbase/index.d.ts +47 -0
- package/dist/nbase/index.js +80 -0
- package/dist/postgres/engine.js +1 -1
- package/dist/sqlite/browserEngine.d.ts +27 -0
- package/dist/sqlite/browserEngine.js +161 -0
- package/dist/sqlite/engine.js +15 -3
- package/dist/sqlite/index.d.ts +1 -0
- package/dist/sqlite/index.js +1 -0
- package/dist/typed.d.ts +5 -0
- package/dist/typed.js +2 -0
- package/dotnet/An5Adapter.cs +1112 -275
- package/dotnet/Base/Core.cs +380 -82
- package/dotnet/Mssql/MssqlEngine.cs +145 -144
- package/dotnet/Postgres/PostgresEngine.cs +149 -121
- package/dotnet/Sqlite/SqliteEngine.cs +214 -0
- package/golang/an5_adapter.go +314 -0
- package/golang/base/base.go +100 -0
- package/golang/base/metadata.go +116 -0
- package/golang/base/where.go +311 -0
- package/golang/go.mod +3 -0
- package/golang/mssql/mssql.go +32 -0
- package/golang/postgres/postgres.go +37 -0
- package/golang/sqlite/sqlite.go +14 -0
- package/golang/sqlite_test.go +42 -0
- package/golang/table_client.go +1329 -0
- package/package.json +26 -9
- package/python/an5_adapter.py +184 -97
- package/python/base/__init__.py +3 -3
- package/python/base/dialects.py +11 -8
- package/python/base/metadata.py +59 -10
- package/python/base/sql.py +202 -121
- package/python/mssql/__init__.py +1 -1
- package/python/mssql/provider.py +62 -62
- package/python/postgres/__init__.py +1 -1
- package/python/postgres/provider.py +15 -15
- package/python/sqlite/__init__.py +3 -0
- package/python/sqlite/provider.py +83 -0
- package/python/table_client.py +636 -216
- package/rust/Cargo.toml +31 -0
- package/rust/README.md +71 -0
- package/rust/src/adapter.rs +1312 -0
- package/rust/src/base/dialect.rs +172 -0
- package/rust/src/base/metadata.rs +134 -0
- package/rust/src/base/mod.rs +17 -0
- package/rust/src/base/where_builder.rs +405 -0
- package/rust/src/lib.rs +75 -0
- package/typescript/src/an5Adapter.ts +1515 -505
- package/typescript/src/base/index.ts +3 -3
- package/typescript/src/base/metadata.ts +148 -28
- package/typescript/src/base/sql.ts +264 -98
- package/typescript/src/base/types.ts +33 -24
- package/typescript/src/browser.ts +10 -6
- package/typescript/src/config.ts +95 -95
- package/typescript/src/googlesheets/adapter.ts +269 -259
- package/typescript/src/googlesheets/an5SheetsAdapter.ts +3 -3
- package/typescript/src/googlesheets/config.ts +47 -47
- package/typescript/src/googlesheets/helpers.ts +144 -143
- package/typescript/src/googlesheets/index.ts +3 -3
- package/typescript/src/googlesheets/parseConnectionString.ts +48 -46
- package/typescript/src/googlesheets/retry.ts +126 -126
- package/typescript/src/googlesheets/sqlExecutor.ts +304 -301
- package/typescript/src/googlesheets/tableClient.ts +461 -455
- package/typescript/src/googlesheets/types.ts +6 -6
- package/typescript/src/index.ts +48 -27
- package/typescript/src/mssql/engine.ts +134 -132
- package/typescript/src/mssql/index.ts +1 -1
- package/typescript/src/mysql/engine.ts +80 -80
- package/typescript/src/mysql/index.ts +1 -1
- package/typescript/src/nbase/client.ts +214 -0
- package/typescript/src/nbase/index.ts +104 -0
- package/typescript/src/postgres/engine.ts +87 -87
- package/typescript/src/postgres/index.ts +1 -1
- package/typescript/src/sqlite/browserEngine.ts +192 -0
- package/typescript/src/sqlite/engine.ts +66 -55
- package/typescript/src/sqlite/index.ts +3 -1
- package/typescript/src/typed.ts +52 -0
|
@@ -1,55 +1,66 @@
|
|
|
1
|
-
import type { An5AdapterConfig, Dialect, QueryEngine, TransactionHandle } from '../base/types';
|
|
2
|
-
|
|
3
|
-
// ─── SQLite Engine ─────────────────────────────────────────────────────────────────
|
|
4
|
-
|
|
5
|
-
let BetterSqlite3: any = null;
|
|
6
|
-
try { BetterSqlite3 = require('better-sqlite3'); } catch { }
|
|
7
|
-
|
|
8
|
-
export class SqliteEngine implements QueryEngine {
|
|
9
|
-
dialect: Dialect = 'sqlite';
|
|
10
|
-
private db: any = null;
|
|
11
|
-
private readonly filePath: string;
|
|
12
|
-
|
|
13
|
-
constructor(adapterConfig: An5AdapterConfig) {
|
|
14
|
-
if (!BetterSqlite3) throw new Error('better-sqlite3 package is required for SQLite support. Run: npm install better-sqlite3');
|
|
15
|
-
this.filePath = adapterConfig.connectionString
|
|
16
|
-
.replace(/^sqlite:\/\/\//i, '/')
|
|
17
|
-
.replace(/^sqlite:\/\//i, '');
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
private getDb(): any {
|
|
21
|
-
if (!this.db) this.db = new BetterSqlite3(this.filePath);
|
|
22
|
-
return this.db;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
// better-sqlite3 natively supports @name parameters
|
|
26
|
-
async exec<T = any>(query: string, params?: Record<string, any>): Promise<T[]> {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
return
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
async
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
db
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
}
|
|
1
|
+
import type { An5AdapterConfig, Dialect, QueryEngine, TransactionHandle } from '../base/types';
|
|
2
|
+
|
|
3
|
+
// ─── SQLite Engine ─────────────────────────────────────────────────────────────────
|
|
4
|
+
|
|
5
|
+
let BetterSqlite3: any = null;
|
|
6
|
+
try { BetterSqlite3 = require('better-sqlite3'); } catch { }
|
|
7
|
+
|
|
8
|
+
export class SqliteEngine implements QueryEngine {
|
|
9
|
+
dialect: Dialect = 'sqlite';
|
|
10
|
+
private db: any = null;
|
|
11
|
+
private readonly filePath: string;
|
|
12
|
+
|
|
13
|
+
constructor(adapterConfig: An5AdapterConfig) {
|
|
14
|
+
if (!BetterSqlite3) throw new Error('better-sqlite3 package is required for SQLite support. Run: npm install better-sqlite3');
|
|
15
|
+
this.filePath = (adapterConfig.connectionString ?? '')
|
|
16
|
+
.replace(/^sqlite:\/\/\//i, '/')
|
|
17
|
+
.replace(/^sqlite:\/\//i, '');
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
private getDb(): any {
|
|
21
|
+
if (!this.db) this.db = new BetterSqlite3(this.filePath);
|
|
22
|
+
return this.db;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// better-sqlite3 natively supports @name parameters
|
|
26
|
+
async exec<T = any>(query: string, params?: Record<string, any>): Promise<T[]> {
|
|
27
|
+
const stmt = this.getDb().prepare(query);
|
|
28
|
+
if (stmt.reader) {
|
|
29
|
+
return stmt.all(params ?? {}) as T[];
|
|
30
|
+
}
|
|
31
|
+
stmt.run(params ?? {});
|
|
32
|
+
return [] as T[];
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
async executeRaw(query: string, params?: Record<string, any>): Promise<number> {
|
|
36
|
+
const info = this.getDb().prepare(query).run(params ?? {});
|
|
37
|
+
return info.changes ?? 0;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async connect(): Promise<void> { this.getDb(); }
|
|
41
|
+
|
|
42
|
+
async disconnect(): Promise<void> {
|
|
43
|
+
if (this.db) { this.db.close(); this.db = null; }
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async beginTransaction(): Promise<TransactionHandle> {
|
|
47
|
+
const db = this.getDb();
|
|
48
|
+
db.prepare('BEGIN').run();
|
|
49
|
+
let done = false;
|
|
50
|
+
|
|
51
|
+
return {
|
|
52
|
+
exec: async <T>(q: string, p?: Record<string, any>) => {
|
|
53
|
+
const stmt = db.prepare(q);
|
|
54
|
+
if (stmt.reader) {
|
|
55
|
+
return stmt.all(p ?? {}) as T[];
|
|
56
|
+
}
|
|
57
|
+
stmt.run(p ?? {});
|
|
58
|
+
return [] as T[];
|
|
59
|
+
},
|
|
60
|
+
executeRaw: async (q: string, p?: Record<string, any>) =>
|
|
61
|
+
(db.prepare(q).run(p ?? {}).changes ?? 0),
|
|
62
|
+
commit: async () => { if (!done) { db.prepare('COMMIT').run(); done = true; } },
|
|
63
|
+
rollback: async () => { if (!done) { db.prepare('ROLLBACK').run(); done = true; } },
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { AdapterTableClient, An5Adapter } from './an5Adapter';
|
|
2
|
+
|
|
3
|
+
// ─── Typed model access ──────────────────────────────────────────────────────
|
|
4
|
+
// The runtime adapter resolves `db.user` / `db.User` dynamically through a
|
|
5
|
+
// Proxy, which TypeScript normally types as `any` (An5Adapter declares
|
|
6
|
+
// `[key: string]: any`). These helpers let app code bind generated model
|
|
7
|
+
// delegate types (e.g. `UserTableClient` emitted by the schema generator) so
|
|
8
|
+
// `db.user` gets full autocomplete and compile-time argument checking — the
|
|
9
|
+
// standard ORM experience — without changing any runtime behavior.
|
|
10
|
+
//
|
|
11
|
+
// Usage:
|
|
12
|
+
// import { createAn5Adapter } from '@an5/adapters';
|
|
13
|
+
// import type { UserTableClient } from './generated/typescript/User';
|
|
14
|
+
// import type { OrderTableClient } from './generated/typescript/Order';
|
|
15
|
+
// const db = createAn5Adapter<{
|
|
16
|
+
// user: UserTableClient;
|
|
17
|
+
// order: OrderTableClient;
|
|
18
|
+
// }>({ connectionString: process.env.DATABASE_URL! });
|
|
19
|
+
// await db.user.findMany({ where: { email: { contains: '@example.com' } } });
|
|
20
|
+
|
|
21
|
+
// Public adapter surface (methods/getters) without the open-ended index
|
|
22
|
+
// signature, so explicit model props in TModels take effect instead of `any`.
|
|
23
|
+
type AdapterMethodKeys =
|
|
24
|
+
| 'dialect'
|
|
25
|
+
| 'exec'
|
|
26
|
+
| '$queryRawUnsafe'
|
|
27
|
+
| '$executeRaw'
|
|
28
|
+
| '$executeRawUnsafe'
|
|
29
|
+
| '$connect'
|
|
30
|
+
| '$disconnect'
|
|
31
|
+
| '$transaction'
|
|
32
|
+
| '$begin'
|
|
33
|
+
| 'table'
|
|
34
|
+
| '$on'
|
|
35
|
+
| '$off'
|
|
36
|
+
| '$emit'
|
|
37
|
+
| '$use'
|
|
38
|
+
| 'view'
|
|
39
|
+
| '$queryProc'
|
|
40
|
+
| '$executeProc'
|
|
41
|
+
| 'readRange'
|
|
42
|
+
| 'writeRange'
|
|
43
|
+
| 'appendRange'
|
|
44
|
+
| 'listSheets'
|
|
45
|
+
| 'deleteSheet';
|
|
46
|
+
|
|
47
|
+
export type AdapterAPI = Pick<An5Adapter, AdapterMethodKeys>;
|
|
48
|
+
|
|
49
|
+
// An5Adapter with dynamically-resolved model delegates typed as TModels.
|
|
50
|
+
// NOTE: keep AdapterMethodKeys in sync when adding public An5Adapter methods.
|
|
51
|
+
export type TypedAn5Adapter<TModels extends object = Record<string, AdapterTableClient<any>>> =
|
|
52
|
+
AdapterAPI & TModels;
|