@an5/adapters 0.2.1 → 0.2.3
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 -263
- package/dotnet/An5Adapter.cs +275 -275
- package/package.json +56 -53
- package/python/base/metadata.py +10 -10
- package/python/table_client.py +216 -216
- package/typescript/an5Adapter.d.ts +4 -2
- package/typescript/an5Adapter.js +60 -23
- package/typescript/base/uuid.d.ts +5 -0
- package/typescript/base/uuid.js +17 -0
- package/typescript/browser.d.ts +5 -0
- package/typescript/browser.js +12 -0
- package/typescript/googlesheets/adapter.d.ts +1 -1
- package/typescript/googlesheets/adapter.js +22 -11
- package/typescript/googlesheets/an5SheetsAdapter.d.ts +1 -1
- package/typescript/googlesheets/config.d.ts +5 -1
- package/typescript/googlesheets/config.js +2 -1
- package/typescript/googlesheets/helpers.js +28 -9
- package/typescript/googlesheets/index.d.ts +2 -1
- package/typescript/googlesheets/parseConnectionString.js +4 -0
- package/typescript/googlesheets/retry.d.ts +23 -1
- package/typescript/googlesheets/retry.js +87 -11
- package/typescript/googlesheets/tableClient.d.ts +1 -0
- package/typescript/googlesheets/tableClient.js +59 -42
- package/typescript/index.d.ts +4 -2
- package/typescript/{an5Adapter.ts → src/an5Adapter.ts} +505 -474
- package/typescript/{base → src/base}/index.ts +3 -3
- package/typescript/{base → src/base}/metadata.ts +28 -28
- package/typescript/{base → src/base}/sql.ts +98 -98
- package/typescript/{base → src/base}/types.ts +24 -24
- package/typescript/src/base/uuid.ts +14 -0
- package/typescript/src/browser.ts +6 -0
- package/typescript/{config.ts → src/config.ts} +95 -95
- package/typescript/{googlesheets → src/googlesheets}/adapter.ts +259 -241
- package/typescript/{googlesheets → src/googlesheets}/an5SheetsAdapter.ts +3 -3
- package/typescript/{googlesheets → src/googlesheets}/config.ts +47 -44
- package/typescript/{googlesheets → src/googlesheets}/helpers.ts +143 -124
- package/typescript/{googlesheets → src/googlesheets}/index.ts +3 -2
- package/typescript/{googlesheets → src/googlesheets}/parseConnectionString.ts +46 -44
- package/typescript/src/googlesheets/retry.ts +126 -0
- package/typescript/{googlesheets → src/googlesheets}/sqlExecutor.ts +301 -301
- package/typescript/{googlesheets → src/googlesheets}/tableClient.ts +455 -435
- package/typescript/{googlesheets → src/googlesheets}/types.ts +6 -6
- package/typescript/{index.ts → src/index.ts} +19 -15
- package/typescript/{mssql → src/mssql}/engine.ts +132 -132
- package/typescript/{mysql → src/mssql}/index.ts +1 -1
- package/typescript/{mysql → src/mysql}/engine.ts +80 -80
- package/typescript/{postgres → src/mysql}/index.ts +1 -1
- package/typescript/{postgres → src/postgres}/engine.ts +87 -87
- package/typescript/{sqlite → src/postgres}/index.ts +1 -1
- package/typescript/{sqlite → src/sqlite}/engine.ts +55 -55
- package/typescript/{mssql → src/sqlite}/index.ts +1 -1
- package/typescript/googlesheets/retry.ts +0 -32
|
@@ -1 +1 @@
|
|
|
1
|
-
export * from './engine';
|
|
1
|
+
export * from './engine';
|
|
@@ -1,55 +1,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
|
-
return this.getDb().prepare(query).all(params ?? {}) as T[];
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
async executeRaw(query: string, params?: Record<string, any>): Promise<number> {
|
|
31
|
-
const info = this.getDb().prepare(query).run(params ?? {});
|
|
32
|
-
return info.changes ?? 0;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
async connect(): Promise<void> { this.getDb(); }
|
|
36
|
-
|
|
37
|
-
async disconnect(): Promise<void> {
|
|
38
|
-
if (this.db) { this.db.close(); this.db = null; }
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
async beginTransaction(): Promise<TransactionHandle> {
|
|
42
|
-
const db = this.getDb();
|
|
43
|
-
db.prepare('BEGIN').run();
|
|
44
|
-
let done = false;
|
|
45
|
-
|
|
46
|
-
return {
|
|
47
|
-
exec: async <T>(q: string, p?: Record<string, any>) =>
|
|
48
|
-
db.prepare(q).all(p ?? {}) as T[],
|
|
49
|
-
executeRaw: async (q: string, p?: Record<string, any>) =>
|
|
50
|
-
(db.prepare(q).run(p ?? {}).changes ?? 0),
|
|
51
|
-
commit: async () => { if (!done) { db.prepare('COMMIT').run(); done = true; } },
|
|
52
|
-
rollback: async () => { if (!done) { db.prepare('ROLLBACK').run(); done = true; } },
|
|
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
|
+
return this.getDb().prepare(query).all(params ?? {}) as T[];
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
async executeRaw(query: string, params?: Record<string, any>): Promise<number> {
|
|
31
|
+
const info = this.getDb().prepare(query).run(params ?? {});
|
|
32
|
+
return info.changes ?? 0;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
async connect(): Promise<void> { this.getDb(); }
|
|
36
|
+
|
|
37
|
+
async disconnect(): Promise<void> {
|
|
38
|
+
if (this.db) { this.db.close(); this.db = null; }
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
async beginTransaction(): Promise<TransactionHandle> {
|
|
42
|
+
const db = this.getDb();
|
|
43
|
+
db.prepare('BEGIN').run();
|
|
44
|
+
let done = false;
|
|
45
|
+
|
|
46
|
+
return {
|
|
47
|
+
exec: async <T>(q: string, p?: Record<string, any>) =>
|
|
48
|
+
db.prepare(q).all(p ?? {}) as T[],
|
|
49
|
+
executeRaw: async (q: string, p?: Record<string, any>) =>
|
|
50
|
+
(db.prepare(q).run(p ?? {}).changes ?? 0),
|
|
51
|
+
commit: async () => { if (!done) { db.prepare('COMMIT').run(); done = true; } },
|
|
52
|
+
rollback: async () => { if (!done) { db.prepare('ROLLBACK').run(); done = true; } },
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export * from './engine';
|
|
1
|
+
export * from './engine';
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
// ─── Retry helper ─────────────────────────────────────────────────────────────
|
|
2
|
-
|
|
3
|
-
export const MAX_RETRIES = 3;
|
|
4
|
-
export const BASE_DELAY = 1000;
|
|
5
|
-
|
|
6
|
-
function isRateLimitError(err: any): boolean {
|
|
7
|
-
const code = err?.code || err?.status || err?.response?.status;
|
|
8
|
-
if (code === 429) return true;
|
|
9
|
-
if (code === 500 || code === 503) return true;
|
|
10
|
-
if (code === 403) {
|
|
11
|
-
const reason = err?.errors?.[0]?.reason || '';
|
|
12
|
-
if (reason === 'rateLimitExceeded') return true;
|
|
13
|
-
if (typeof err?.body === 'string' && err.body.includes('rateLimitExceeded')) return true;
|
|
14
|
-
if (typeof err?.message === 'string' && err.message.includes('rateLimitExceeded')) return true;
|
|
15
|
-
}
|
|
16
|
-
return false;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export async function withRetry<T>(fn: () => Promise<T>, attempt = 1): Promise<T> {
|
|
20
|
-
try {
|
|
21
|
-
return await fn();
|
|
22
|
-
} catch (err: any) {
|
|
23
|
-
if (isRateLimitError(err) && attempt <= MAX_RETRIES) {
|
|
24
|
-
const delay = err.retryAfter
|
|
25
|
-
? err.retryAfter * 1000
|
|
26
|
-
: BASE_DELAY * Math.pow(2, attempt - 1) + Math.random() * 500;
|
|
27
|
-
await new Promise(r => setTimeout(r, delay));
|
|
28
|
-
return withRetry(fn, attempt + 1);
|
|
29
|
-
}
|
|
30
|
-
throw err;
|
|
31
|
-
}
|
|
32
|
-
}
|