@an5/adapters 0.2.0 → 0.2.2

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 (43) hide show
  1. package/README.md +226 -226
  2. package/package.json +46 -38
  3. package/typescript/an5Adapter.d.ts +4 -2
  4. package/typescript/an5Adapter.js +58 -21
  5. package/typescript/browser.d.ts +5 -0
  6. package/typescript/browser.js +12 -0
  7. package/typescript/googlesheets/adapter.d.ts +3 -2
  8. package/typescript/googlesheets/adapter.js +77 -7
  9. package/typescript/googlesheets/an5SheetsAdapter.d.ts +1 -1
  10. package/typescript/googlesheets/config.d.ts +11 -0
  11. package/typescript/googlesheets/config.js +7 -0
  12. package/typescript/googlesheets/helpers.js +1 -1
  13. package/typescript/googlesheets/index.d.ts +2 -1
  14. package/typescript/googlesheets/retry.js +21 -8
  15. package/typescript/googlesheets/tableClient.js +4 -3
  16. package/typescript/index.d.ts +4 -2
  17. package/typescript/{an5Adapter.ts → src/an5Adapter.ts} +505 -474
  18. package/typescript/{base → src/base}/index.ts +3 -3
  19. package/typescript/{base → src/base}/metadata.ts +28 -28
  20. package/typescript/{base → src/base}/sql.ts +98 -98
  21. package/typescript/{base → src/base}/types.ts +24 -24
  22. package/typescript/src/browser.ts +6 -0
  23. package/typescript/{config.ts → src/config.ts} +95 -95
  24. package/typescript/{googlesheets → src/googlesheets}/adapter.ts +242 -167
  25. package/typescript/{googlesheets → src/googlesheets}/an5SheetsAdapter.ts +3 -3
  26. package/typescript/{googlesheets → src/googlesheets}/config.ts +44 -33
  27. package/typescript/{googlesheets → src/googlesheets}/helpers.ts +124 -124
  28. package/typescript/{googlesheets → src/googlesheets}/index.ts +3 -2
  29. package/typescript/{googlesheets → src/googlesheets}/parseConnectionString.ts +44 -44
  30. package/typescript/src/googlesheets/retry.ts +32 -0
  31. package/typescript/{googlesheets → src/googlesheets}/sqlExecutor.ts +301 -301
  32. package/typescript/{googlesheets → src/googlesheets}/tableClient.ts +435 -434
  33. package/typescript/{googlesheets → src/googlesheets}/types.ts +6 -6
  34. package/typescript/{index.ts → src/index.ts} +19 -15
  35. package/typescript/{mssql → src/mssql}/engine.ts +132 -132
  36. package/typescript/{mysql → src/mssql}/index.ts +1 -1
  37. package/typescript/{mysql → src/mysql}/engine.ts +80 -80
  38. package/typescript/{postgres → src/mysql}/index.ts +1 -1
  39. package/typescript/{postgres → src/postgres}/engine.ts +87 -87
  40. package/typescript/{sqlite → src/postgres}/index.ts +1 -1
  41. package/typescript/{sqlite → src/sqlite}/engine.ts +55 -55
  42. package/typescript/{mssql → src/sqlite}/index.ts +1 -1
  43. package/typescript/googlesheets/retry.ts +0 -23
@@ -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,23 +0,0 @@
1
- // ─── Retry helper ─────────────────────────────────────────────────────────────
2
-
3
- export const MAX_RETRIES = 3;
4
- export const BASE_DELAY = 1000;
5
-
6
- export async function withRetry<T>(fn: () => Promise<T>, attempt = 1): Promise<T> {
7
- try {
8
- return await fn();
9
- } catch (err: any) {
10
- const code = err?.code || err?.status || err?.response?.status;
11
- if ((code === 429 || code === 500 || code === 503) && attempt <= MAX_RETRIES) {
12
- const delay = BASE_DELAY * Math.pow(2, attempt - 1) + Math.random() * 500;
13
- await new Promise(r => setTimeout(r, delay));
14
- return withRetry(fn, attempt + 1);
15
- }
16
- if (code === 403 && err?.errors?.[0]?.reason === 'rateLimitExceeded' && attempt <= MAX_RETRIES) {
17
- const delay = BASE_DELAY * Math.pow(2, attempt - 1) + Math.random() * 500;
18
- await new Promise(r => setTimeout(r, delay));
19
- return withRetry(fn, attempt + 1);
20
- }
21
- throw err;
22
- }
23
- }