@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
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { Dialect, QueryEngine, TransactionHandle } from '../base/types';
|
|
2
|
+
export interface SqliteDriver {
|
|
3
|
+
exec<T = any>(query: string, params?: Record<string, any>): Promise<T[]> | T[];
|
|
4
|
+
executeRaw?(query: string, params?: Record<string, any>): Promise<number> | number;
|
|
5
|
+
close?(): Promise<void> | void;
|
|
6
|
+
}
|
|
7
|
+
export interface SqliteBrowserConfig {
|
|
8
|
+
db?: any;
|
|
9
|
+
driver?: SqliteDriver;
|
|
10
|
+
exec?: <T = any>(query: string, params?: Record<string, any>) => Promise<T[]> | T[];
|
|
11
|
+
executeRaw?: (query: string, params?: Record<string, any>) => Promise<number> | number;
|
|
12
|
+
}
|
|
13
|
+
export declare class SqliteBrowserEngine implements QueryEngine {
|
|
14
|
+
dialect: Dialect;
|
|
15
|
+
private db;
|
|
16
|
+
private driver;
|
|
17
|
+
private customExec;
|
|
18
|
+
private customExecuteRaw;
|
|
19
|
+
constructor(config?: SqliteBrowserConfig | any);
|
|
20
|
+
private normalizeSqlJsParams;
|
|
21
|
+
exec<T = any>(query: string, params?: Record<string, any>): Promise<T[]>;
|
|
22
|
+
executeRaw(query: string, params?: Record<string, any>): Promise<number>;
|
|
23
|
+
connect(): Promise<void>;
|
|
24
|
+
disconnect(): Promise<void>;
|
|
25
|
+
beginTransaction(): Promise<TransactionHandle>;
|
|
26
|
+
}
|
|
27
|
+
export declare function createBrowserSqliteAdapter(config: SqliteBrowserConfig | any): any;
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SqliteBrowserEngine = void 0;
|
|
4
|
+
exports.createBrowserSqliteAdapter = createBrowserSqliteAdapter;
|
|
5
|
+
// ─── Browser SQLite Engine ──────────────────────────────────────────────────────────
|
|
6
|
+
class SqliteBrowserEngine {
|
|
7
|
+
constructor(config) {
|
|
8
|
+
this.dialect = 'sqlite';
|
|
9
|
+
this.db = null;
|
|
10
|
+
this.driver = null;
|
|
11
|
+
this.customExec = null;
|
|
12
|
+
this.customExecuteRaw = null;
|
|
13
|
+
if (!config)
|
|
14
|
+
return;
|
|
15
|
+
if (config.exec && typeof config.exec === 'function') {
|
|
16
|
+
this.customExec = config.exec;
|
|
17
|
+
this.customExecuteRaw = config.executeRaw ?? null;
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
if (config.driver) {
|
|
21
|
+
this.driver = config.driver;
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
// Direct DB instance (e.g. sql.js Database or sqlite-wasm)
|
|
25
|
+
if (config.db) {
|
|
26
|
+
this.db = config.db;
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
// If config itself looks like a driver with exec()
|
|
30
|
+
if (typeof config.exec === 'function' && typeof config.run !== 'function' && typeof config.prepare !== 'function') {
|
|
31
|
+
this.driver = config;
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
// If config itself is a Database instance (has prepare, exec, run)
|
|
35
|
+
if (typeof config.prepare === 'function' || typeof config.run === 'function' || typeof config.exec === 'function') {
|
|
36
|
+
this.db = config;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
normalizeSqlJsParams(params) {
|
|
40
|
+
if (!params || typeof params !== 'object')
|
|
41
|
+
return {};
|
|
42
|
+
const normalized = {};
|
|
43
|
+
for (const [k, v] of Object.entries(params)) {
|
|
44
|
+
const key = k.startsWith('@') || k.startsWith(':') || k.startsWith('$') ? k : `@${k}`;
|
|
45
|
+
normalized[key] = v;
|
|
46
|
+
}
|
|
47
|
+
return normalized;
|
|
48
|
+
}
|
|
49
|
+
async exec(query, params) {
|
|
50
|
+
if (this.customExec) {
|
|
51
|
+
return await this.customExec(query, params);
|
|
52
|
+
}
|
|
53
|
+
if (this.driver) {
|
|
54
|
+
return await this.driver.exec(query, params);
|
|
55
|
+
}
|
|
56
|
+
if (this.db) {
|
|
57
|
+
// sql.js Database with prepare()
|
|
58
|
+
if (typeof this.db.prepare === 'function') {
|
|
59
|
+
const stmt = this.db.prepare(query);
|
|
60
|
+
try {
|
|
61
|
+
if (params && Object.keys(params).length > 0) {
|
|
62
|
+
stmt.bind(this.normalizeSqlJsParams(params));
|
|
63
|
+
}
|
|
64
|
+
const results = [];
|
|
65
|
+
while (stmt.step()) {
|
|
66
|
+
results.push(stmt.getAsObject());
|
|
67
|
+
}
|
|
68
|
+
return results;
|
|
69
|
+
}
|
|
70
|
+
finally {
|
|
71
|
+
stmt.free();
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
// Fallback sql.js db.exec()
|
|
75
|
+
if (typeof this.db.exec === 'function') {
|
|
76
|
+
const res = this.db.exec(query);
|
|
77
|
+
if (!res || res.length === 0)
|
|
78
|
+
return [];
|
|
79
|
+
const { columns, values } = res[0];
|
|
80
|
+
return values.map((row) => {
|
|
81
|
+
const obj = {};
|
|
82
|
+
columns.forEach((col, i) => {
|
|
83
|
+
obj[col] = row[i];
|
|
84
|
+
});
|
|
85
|
+
return obj;
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
throw new Error('SqliteBrowserEngine: No SQLite database or driver provided. Pass a sql.js db instance or custom driver.');
|
|
90
|
+
}
|
|
91
|
+
async executeRaw(query, params) {
|
|
92
|
+
if (this.customExecuteRaw) {
|
|
93
|
+
return await this.customExecuteRaw(query, params);
|
|
94
|
+
}
|
|
95
|
+
if (this.driver && typeof this.driver.executeRaw === 'function') {
|
|
96
|
+
return await this.driver.executeRaw(query, params);
|
|
97
|
+
}
|
|
98
|
+
if (this.db) {
|
|
99
|
+
if (typeof this.db.prepare === 'function') {
|
|
100
|
+
const stmt = this.db.prepare(query);
|
|
101
|
+
try {
|
|
102
|
+
if (params && Object.keys(params).length > 0) {
|
|
103
|
+
stmt.bind(this.normalizeSqlJsParams(params));
|
|
104
|
+
}
|
|
105
|
+
stmt.step();
|
|
106
|
+
if (typeof this.db.getRowsModified === 'function') {
|
|
107
|
+
return this.db.getRowsModified();
|
|
108
|
+
}
|
|
109
|
+
return 1;
|
|
110
|
+
}
|
|
111
|
+
finally {
|
|
112
|
+
stmt.free();
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
if (typeof this.db.run === 'function') {
|
|
116
|
+
this.db.run(query, this.normalizeSqlJsParams(params));
|
|
117
|
+
if (typeof this.db.getRowsModified === 'function') {
|
|
118
|
+
return this.db.getRowsModified();
|
|
119
|
+
}
|
|
120
|
+
return 1;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
const rows = await this.exec(query, params);
|
|
124
|
+
return rows.length;
|
|
125
|
+
}
|
|
126
|
+
async connect() { }
|
|
127
|
+
async disconnect() {
|
|
128
|
+
if (this.driver && typeof this.driver.close === 'function') {
|
|
129
|
+
await this.driver.close();
|
|
130
|
+
}
|
|
131
|
+
else if (this.db && typeof this.db.close === 'function') {
|
|
132
|
+
this.db.close();
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
async beginTransaction() {
|
|
136
|
+
await this.executeRaw('BEGIN');
|
|
137
|
+
let done = false;
|
|
138
|
+
return {
|
|
139
|
+
exec: async (q, p) => this.exec(q, p),
|
|
140
|
+
executeRaw: async (q, p) => this.executeRaw(q, p),
|
|
141
|
+
commit: async () => {
|
|
142
|
+
if (!done) {
|
|
143
|
+
await this.executeRaw('COMMIT');
|
|
144
|
+
done = true;
|
|
145
|
+
}
|
|
146
|
+
},
|
|
147
|
+
rollback: async () => {
|
|
148
|
+
if (!done) {
|
|
149
|
+
await this.executeRaw('ROLLBACK');
|
|
150
|
+
done = true;
|
|
151
|
+
}
|
|
152
|
+
},
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
exports.SqliteBrowserEngine = SqliteBrowserEngine;
|
|
157
|
+
// ─── Browser SQLite Adapter Factory ──────────────────────────────────────────────────
|
|
158
|
+
function createBrowserSqliteAdapter(config) {
|
|
159
|
+
const { An5Adapter } = require('../an5Adapter');
|
|
160
|
+
return new An5Adapter({ engine: new SqliteBrowserEngine(config) });
|
|
161
|
+
}
|
package/dist/sqlite/engine.js
CHANGED
|
@@ -13,7 +13,7 @@ class SqliteEngine {
|
|
|
13
13
|
this.db = null;
|
|
14
14
|
if (!BetterSqlite3)
|
|
15
15
|
throw new Error('better-sqlite3 package is required for SQLite support. Run: npm install better-sqlite3');
|
|
16
|
-
this.filePath = adapterConfig.connectionString
|
|
16
|
+
this.filePath = (adapterConfig.connectionString ?? '')
|
|
17
17
|
.replace(/^sqlite:\/\/\//i, '/')
|
|
18
18
|
.replace(/^sqlite:\/\//i, '');
|
|
19
19
|
}
|
|
@@ -24,7 +24,12 @@ class SqliteEngine {
|
|
|
24
24
|
}
|
|
25
25
|
// better-sqlite3 natively supports @name parameters
|
|
26
26
|
async exec(query, params) {
|
|
27
|
-
|
|
27
|
+
const stmt = this.getDb().prepare(query);
|
|
28
|
+
if (stmt.reader) {
|
|
29
|
+
return stmt.all(params ?? {});
|
|
30
|
+
}
|
|
31
|
+
stmt.run(params ?? {});
|
|
32
|
+
return [];
|
|
28
33
|
}
|
|
29
34
|
async executeRaw(query, params) {
|
|
30
35
|
const info = this.getDb().prepare(query).run(params ?? {});
|
|
@@ -42,7 +47,14 @@ class SqliteEngine {
|
|
|
42
47
|
db.prepare('BEGIN').run();
|
|
43
48
|
let done = false;
|
|
44
49
|
return {
|
|
45
|
-
exec: async (q, p) =>
|
|
50
|
+
exec: async (q, p) => {
|
|
51
|
+
const stmt = db.prepare(q);
|
|
52
|
+
if (stmt.reader) {
|
|
53
|
+
return stmt.all(p ?? {});
|
|
54
|
+
}
|
|
55
|
+
stmt.run(p ?? {});
|
|
56
|
+
return [];
|
|
57
|
+
},
|
|
46
58
|
executeRaw: async (q, p) => (db.prepare(q).run(p ?? {}).changes ?? 0),
|
|
47
59
|
commit: async () => { if (!done) {
|
|
48
60
|
db.prepare('COMMIT').run();
|
package/dist/sqlite/index.d.ts
CHANGED
package/dist/sqlite/index.js
CHANGED
package/dist/typed.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { AdapterTableClient, An5Adapter } from './an5Adapter';
|
|
2
|
+
type AdapterMethodKeys = 'dialect' | 'exec' | '$queryRawUnsafe' | '$executeRaw' | '$executeRawUnsafe' | '$connect' | '$disconnect' | '$transaction' | '$begin' | 'table' | '$on' | '$off' | '$emit' | '$use' | 'view' | '$queryProc' | '$executeProc' | 'readRange' | 'writeRange' | 'appendRange' | 'listSheets' | 'deleteSheet';
|
|
3
|
+
export type AdapterAPI = Pick<An5Adapter, AdapterMethodKeys>;
|
|
4
|
+
export type TypedAn5Adapter<TModels extends object = Record<string, AdapterTableClient<any>>> = AdapterAPI & TModels;
|
|
5
|
+
export {};
|
package/dist/typed.js
ADDED