@framers/sql-storage-adapter 0.1.0
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/LICENSE +21 -0
- package/README.md +363 -0
- package/dist/adapters/baseStorageAdapter.d.ts +204 -0
- package/dist/adapters/baseStorageAdapter.d.ts.map +1 -0
- package/dist/adapters/baseStorageAdapter.js +364 -0
- package/dist/adapters/baseStorageAdapter.js.map +1 -0
- package/dist/adapters/betterSqliteAdapter.d.ts +64 -0
- package/dist/adapters/betterSqliteAdapter.d.ts.map +1 -0
- package/dist/adapters/betterSqliteAdapter.js +206 -0
- package/dist/adapters/betterSqliteAdapter.js.map +1 -0
- package/dist/adapters/capacitorSqliteAdapter.d.ts +33 -0
- package/dist/adapters/capacitorSqliteAdapter.d.ts.map +1 -0
- package/dist/adapters/capacitorSqliteAdapter.js +95 -0
- package/dist/adapters/capacitorSqliteAdapter.js.map +1 -0
- package/dist/adapters/postgresAdapter.d.ts +180 -0
- package/dist/adapters/postgresAdapter.d.ts.map +1 -0
- package/dist/adapters/postgresAdapter.js +271 -0
- package/dist/adapters/postgresAdapter.js.map +1 -0
- package/dist/adapters/sqlJsAdapter.d.ts +28 -0
- package/dist/adapters/sqlJsAdapter.d.ts.map +1 -0
- package/dist/adapters/sqlJsAdapter.js +136 -0
- package/dist/adapters/sqlJsAdapter.js.map +1 -0
- package/dist/adapters/supabase.d.ts +58 -0
- package/dist/adapters/supabase.d.ts.map +1 -0
- package/dist/adapters/supabase.js +385 -0
- package/dist/adapters/supabase.js.map +1 -0
- package/dist/database.d.ts +124 -0
- package/dist/database.d.ts.map +1 -0
- package/dist/database.js +136 -0
- package/dist/database.js.map +1 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +25 -0
- package/dist/index.js.map +1 -0
- package/dist/resolver.d.ts +24 -0
- package/dist/resolver.d.ts.map +1 -0
- package/dist/resolver.js +91 -0
- package/dist/resolver.js.map +1 -0
- package/dist/types/context.d.ts +221 -0
- package/dist/types/context.d.ts.map +1 -0
- package/dist/types/context.js +9 -0
- package/dist/types/context.js.map +1 -0
- package/dist/types/events.d.ts +225 -0
- package/dist/types/events.d.ts.map +1 -0
- package/dist/types/events.js +8 -0
- package/dist/types/events.js.map +1 -0
- package/dist/types/extensions.d.ts +73 -0
- package/dist/types/extensions.d.ts.map +1 -0
- package/dist/types/extensions.js +7 -0
- package/dist/types/extensions.js.map +1 -0
- package/dist/types/limitations.d.ts +46 -0
- package/dist/types/limitations.d.ts.map +1 -0
- package/dist/types/limitations.js +154 -0
- package/dist/types/limitations.js.map +1 -0
- package/dist/types.d.ts +235 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +18 -0
- package/dist/types.js.map +1 -0
- package/dist/utils/cloudBackup.d.ts +219 -0
- package/dist/utils/cloudBackup.d.ts.map +1 -0
- package/dist/utils/cloudBackup.js +289 -0
- package/dist/utils/cloudBackup.js.map +1 -0
- package/dist/utils/dataExport.d.ts +77 -0
- package/dist/utils/dataExport.d.ts.map +1 -0
- package/dist/utils/dataExport.js +212 -0
- package/dist/utils/dataExport.js.map +1 -0
- package/dist/utils/dataImport.d.ts +54 -0
- package/dist/utils/dataImport.d.ts.map +1 -0
- package/dist/utils/dataImport.js +324 -0
- package/dist/utils/dataImport.js.map +1 -0
- package/dist/utils/migration.d.ts +89 -0
- package/dist/utils/migration.d.ts.map +1 -0
- package/dist/utils/migration.js +184 -0
- package/dist/utils/migration.js.map +1 -0
- package/dist/utils/parameterUtils.d.ts +9 -0
- package/dist/utils/parameterUtils.d.ts.map +1 -0
- package/dist/utils/parameterUtils.js +17 -0
- package/dist/utils/parameterUtils.js.map +1 -0
- package/dist/utils/syncManager.d.ts +342 -0
- package/dist/utils/syncManager.d.ts.map +1 -0
- package/dist/utils/syncManager.js +533 -0
- package/dist/utils/syncManager.js.map +1 -0
- package/package.json +108 -0
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { StorageAdapter, StorageCapability, StorageOpenOptions, StorageParameters, StorageRunResult } from '../types';
|
|
2
|
+
export interface CapacitorAdapterOptions {
|
|
3
|
+
/**
|
|
4
|
+
* Database name. Defaults to `app`.
|
|
5
|
+
*/
|
|
6
|
+
database?: string;
|
|
7
|
+
/**
|
|
8
|
+
* Whether to enable WAL when platform supports it.
|
|
9
|
+
*/
|
|
10
|
+
enableWal?: boolean;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Adapter using @capacitor-community/sqlite for native mobile targets.
|
|
14
|
+
*/
|
|
15
|
+
export declare class CapacitorSqliteAdapter implements StorageAdapter {
|
|
16
|
+
private readonly options;
|
|
17
|
+
readonly kind = "capacitor-sqlite";
|
|
18
|
+
readonly capabilities: ReadonlySet<StorageCapability>;
|
|
19
|
+
private plugin;
|
|
20
|
+
private connection;
|
|
21
|
+
private dbName;
|
|
22
|
+
constructor(options?: CapacitorAdapterOptions);
|
|
23
|
+
open(options?: StorageOpenOptions): Promise<void>;
|
|
24
|
+
run(statement: string, parameters?: StorageParameters): Promise<StorageRunResult>;
|
|
25
|
+
get<T>(statement: string, parameters?: StorageParameters): Promise<T | null>;
|
|
26
|
+
all<T>(statement: string, parameters?: StorageParameters): Promise<T[]>;
|
|
27
|
+
exec(script: string): Promise<void>;
|
|
28
|
+
transaction<T>(fn: (trx: StorageAdapter) => Promise<T>): Promise<T>;
|
|
29
|
+
close(): Promise<void>;
|
|
30
|
+
private ensureConnection;
|
|
31
|
+
}
|
|
32
|
+
export declare const createCapacitorSqliteAdapter: (options?: CapacitorAdapterOptions) => StorageAdapter;
|
|
33
|
+
//# sourceMappingURL=capacitorSqliteAdapter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"capacitorSqliteAdapter.d.ts","sourceRoot":"","sources":["../../src/adapters/capacitorSqliteAdapter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,iBAAiB,EAAE,kBAAkB,EAAE,iBA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
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { normaliseParameters } from '../utils/parameterUtils';
|
|
2
|
+
const loadCapacitorSqlite = async () => {
|
|
3
|
+
try {
|
|
4
|
+
return await import('@capacitor-community/sqlite');
|
|
5
|
+
}
|
|
6
|
+
catch (error) {
|
|
7
|
+
console.warn('[StorageAdapter] @capacitor-community/sqlite module not available.', error);
|
|
8
|
+
return null;
|
|
9
|
+
}
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Adapter using @capacitor-community/sqlite for native mobile targets.
|
|
13
|
+
*/
|
|
14
|
+
export class CapacitorSqliteAdapter {
|
|
15
|
+
constructor(options = {}) {
|
|
16
|
+
this.options = options;
|
|
17
|
+
this.kind = 'capacitor-sqlite';
|
|
18
|
+
this.capabilities = new Set([
|
|
19
|
+
'transactions',
|
|
20
|
+
'wal',
|
|
21
|
+
'locks',
|
|
22
|
+
'persistence'
|
|
23
|
+
]);
|
|
24
|
+
this.plugin = null;
|
|
25
|
+
this.connection = null;
|
|
26
|
+
this.dbName = options.database ?? 'app';
|
|
27
|
+
}
|
|
28
|
+
async open(options) {
|
|
29
|
+
if (this.connection) {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
this.plugin = await loadCapacitorSqlite();
|
|
33
|
+
if (!this.plugin) {
|
|
34
|
+
throw new Error('@capacitor-community/sqlite is unavailable. Install the plugin or choose a different adapter.');
|
|
35
|
+
}
|
|
36
|
+
const dbName = options?.adapterOptions?.database ?? this.options.database ?? this.dbName;
|
|
37
|
+
this.connection = await this.plugin.createConnection(dbName, false, 'no-encryption', 1, false);
|
|
38
|
+
await this.connection.open();
|
|
39
|
+
if (this.options.enableWal ?? true) {
|
|
40
|
+
try {
|
|
41
|
+
await this.connection.execute('PRAGMA journal_mode = WAL;');
|
|
42
|
+
}
|
|
43
|
+
catch (error) {
|
|
44
|
+
console.warn('[StorageAdapter] Failed to enable WAL on Capacitor SQLite.', error);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
async run(statement, parameters) {
|
|
49
|
+
const conn = this.ensureConnection();
|
|
50
|
+
const { positional } = normaliseParameters(parameters);
|
|
51
|
+
const result = await conn.run(statement, positional ?? []);
|
|
52
|
+
return { changes: result.changes ?? 0, lastInsertRowid: result.lastId ?? null };
|
|
53
|
+
}
|
|
54
|
+
async get(statement, parameters) {
|
|
55
|
+
const rows = await this.all(statement, parameters);
|
|
56
|
+
return rows.length > 0 ? rows[0] : null;
|
|
57
|
+
}
|
|
58
|
+
async all(statement, parameters) {
|
|
59
|
+
const conn = this.ensureConnection();
|
|
60
|
+
const { positional } = normaliseParameters(parameters);
|
|
61
|
+
const result = await conn.query(statement, positional ?? []);
|
|
62
|
+
return (result.values ?? []);
|
|
63
|
+
}
|
|
64
|
+
async exec(script) {
|
|
65
|
+
const conn = this.ensureConnection();
|
|
66
|
+
await conn.execute(script);
|
|
67
|
+
}
|
|
68
|
+
async transaction(fn) {
|
|
69
|
+
const conn = this.ensureConnection();
|
|
70
|
+
await conn.execute('BEGIN TRANSACTION;');
|
|
71
|
+
try {
|
|
72
|
+
const result = await fn(this);
|
|
73
|
+
await conn.execute('COMMIT;');
|
|
74
|
+
return result;
|
|
75
|
+
}
|
|
76
|
+
catch (error) {
|
|
77
|
+
await conn.execute('ROLLBACK;');
|
|
78
|
+
throw error;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
async close() {
|
|
82
|
+
if (this.connection) {
|
|
83
|
+
await this.connection.close();
|
|
84
|
+
this.connection = null;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
ensureConnection() {
|
|
88
|
+
if (!this.connection) {
|
|
89
|
+
throw new Error('Storage adapter not opened. Call open() before executing statements.');
|
|
90
|
+
}
|
|
91
|
+
return this.connection;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
export const createCapacitorSqliteAdapter = (options) => new CapacitorSqliteAdapter(options);
|
|
95
|
+
//# sourceMappingURL=capacitorSqli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B,CAAC;CACF;AAED,MAAM,CAAC,MAAM,4BAA4B,GAAG,CAAC,OAAiC,EAAkB,EAAE,CAChG,IAAI,sBAAsB,CAAC,OAAO,CAAC,CAAC"}
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
import type { StorageAdapter, StorageCapability, StorageOpenOptions, StorageParameters, StorageRunResult } from '../types';
|
|
2
|
+
/**
|
|
3
|
+
* Configuration options for PostgreSQL adapter.
|
|
4
|
+
* Supports both connection strings and granular configuration.
|
|
5
|
+
*/
|
|
6
|
+
export interface PostgresAdapterOptions {
|
|
7
|
+
/** PostgreSQL connection string (e.g., 'postgresql://user:pass@host:5432/dbname') */
|
|
8
|
+
connectionString?: string;
|
|
9
|
+
/** Host name or IP address (default: localhost) */
|
|
10
|
+
host?: string;
|
|
11
|
+
/** Port number (default: 5432) */
|
|
12
|
+
port?: number;
|
|
13
|
+
/** Database name */
|
|
14
|
+
database?: string;
|
|
15
|
+
/** Username for authentication */
|
|
16
|
+
user?: string;
|
|
17
|
+
/** Password for authentication */
|
|
18
|
+
password?: string;
|
|
19
|
+
/** Enable SSL/TLS connection (recommended for remote) */
|
|
20
|
+
ssl?: boolean | {
|
|
21
|
+
rejectUnauthorized?: boolean;
|
|
22
|
+
ca?: string;
|
|
23
|
+
cert?: string;
|
|
24
|
+
key?: string;
|
|
25
|
+
};
|
|
26
|
+
/** Connection pool size (default: 10) */
|
|
27
|
+
max?: number;
|
|
28
|
+
/** Minimum pool size (default: 0) */
|
|
29
|
+
min?: number;
|
|
30
|
+
/** Idle timeout in ms (default: 10000) */
|
|
31
|
+
idleTimeoutMillis?: number;
|
|
32
|
+
/** Connection timeout in ms (default: 0 = no timeout) */
|
|
33
|
+
connectionTimeoutMillis?: number;
|
|
34
|
+
/** Application name for connection tracking */
|
|
35
|
+
application_name?: string;
|
|
36
|
+
/** Statement timeout in ms (0 = no timeout) */
|
|
37
|
+
statement_timeout?: number;
|
|
38
|
+
/** Query timeout in ms (0 = no timeout) */
|
|
39
|
+
query_timeout?: number;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* PostgreSQL adapter for production-grade SQL operations.
|
|
43
|
+
*
|
|
44
|
+
* ## Performance Characteristics
|
|
45
|
+
* - Connection pooling for efficient resource usage
|
|
46
|
+
* - Excellent concurrent access with MVCC
|
|
47
|
+
* - Optimized for complex queries and large datasets
|
|
48
|
+
* - ~10,000 queries/second with connection pooling
|
|
49
|
+
*
|
|
50
|
+
* ## Advantages
|
|
51
|
+
* - Full SQL feature set (CTEs, window functions, etc.)
|
|
52
|
+
* - Native JSON/JSONB support
|
|
53
|
+
* - Robust replication and backup options
|
|
54
|
+
* - Battle-tested in production environments
|
|
55
|
+
* - Secure remote connections with SSL/TLS
|
|
56
|
+
*
|
|
57
|
+
* ## Limitations
|
|
58
|
+
* - Requires separate server process
|
|
59
|
+
* - Network latency for remote connections
|
|
60
|
+
* - Higher resource consumption than SQLite
|
|
61
|
+
* - Configuration complexity for optimal performance
|
|
62
|
+
*
|
|
63
|
+
* ## When to Use
|
|
64
|
+
* - Production web applications
|
|
65
|
+
* - Multi-user systems
|
|
66
|
+
* - When you need advanced SQL features
|
|
67
|
+
* - Cloud deployments (AWS RDS, Heroku, Supabase, etc.)
|
|
68
|
+
* - Remote database access
|
|
69
|
+
*
|
|
70
|
+
* ## Remote Connection Examples
|
|
71
|
+
*
|
|
72
|
+
* ### Connection String (Recommended)
|
|
73
|
+
* ```typescript
|
|
74
|
+
* const db = createPostgresAdapter({
|
|
75
|
+
* connectionString: 'postgresql://user:password@db.example.com:5432/mydb?sslmode=require'
|
|
76
|
+
* });
|
|
77
|
+
* ```
|
|
78
|
+
*
|
|
79
|
+
* ### Granular Configuration
|
|
80
|
+
* ```typescript
|
|
81
|
+
* const db = createPostgresAdapter({
|
|
82
|
+
* host: 'db.example.com',
|
|
83
|
+
* port: 5432,
|
|
84
|
+
* database: 'mydb',
|
|
85
|
+
* user: 'dbuser',
|
|
86
|
+
* password: 'secure_password',
|
|
87
|
+
* ssl: true, // Enable SSL for security
|
|
88
|
+
* max: 20, // Connection pool size
|
|
89
|
+
* statement_timeout: 30000 // 30 second timeout
|
|
90
|
+
* });
|
|
91
|
+
* ```
|
|
92
|
+
*
|
|
93
|
+
* ### Cloud Provider Examples
|
|
94
|
+
*
|
|
95
|
+
* #### AWS RDS
|
|
96
|
+
* ```typescript
|
|
97
|
+
* const db = createPostgresAdapter({
|
|
98
|
+
* host: 'mydb.abc123.us-east-1.rds.amazonaws.com',
|
|
99
|
+
* port: 5432,
|
|
100
|
+
* database: 'postgres',
|
|
101
|
+
* user: 'admin',
|
|
102
|
+
* password: process.env.RDS_PASSWORD,
|
|
103
|
+
* ssl: { rejectUnauthorized: true }
|
|
104
|
+
* });
|
|
105
|
+
* ```
|
|
106
|
+
*
|
|
107
|
+
* #### Heroku Postgres
|
|
108
|
+
* ```typescript
|
|
109
|
+
* const db = createPostgresAdapter({
|
|
110
|
+
* connectionString: process.env.DATABASE_URL,
|
|
111
|
+
* ssl: { rejectUnauthorized: false } // Heroku uses self-signed certs
|
|
112
|
+
* });
|
|
113
|
+
* ```
|
|
114
|
+
*
|
|
115
|
+
* #### Supabase
|
|
116
|
+
* ```typescript
|
|
117
|
+
* const db = createPostgresAdapter({
|
|
118
|
+
* connectionString: process.env.SUPABASE_DB_URL,
|
|
119
|
+
* ssl: true
|
|
120
|
+
* });
|
|
121
|
+
* ```
|
|
122
|
+
*
|
|
123
|
+
* #### DigitalOcean Managed Database
|
|
124
|
+
* ```typescript
|
|
125
|
+
* const db = createPostgresAdapter({
|
|
126
|
+
* host: 'db-postgresql-nyc3-12345.ondigitalocean.com',
|
|
127
|
+
* port: 25060,
|
|
128
|
+
* database: 'defaultdb',
|
|
129
|
+
* user: 'doadmin',
|
|
130
|
+
* password: process.env.DO_DB_PASSWORD,
|
|
131
|
+
* ssl: { rejectUnauthorized: true, ca: process.env.DO_CA_CERT }
|
|
132
|
+
* });
|
|
133
|
+
* ```
|
|
134
|
+
*
|
|
135
|
+
* ## Graceful Degradation
|
|
136
|
+
* - Automatic reconnection on connection loss
|
|
137
|
+
* - Connection pool handles transient failures
|
|
138
|
+
* - Falls back to SQLite if PostgreSQL unavailable
|
|
139
|
+
*/
|
|
140
|
+
export declare class PostgresAdapter implements StorageAdapter {
|
|
141
|
+
readonly kind = "postgres";
|
|
142
|
+
readonly capabilities: ReadonlySet<StorageCapability>;
|
|
143
|
+
private options;
|
|
144
|
+
private pool;
|
|
145
|
+
private transactionalClient;
|
|
146
|
+
constructor(options: PostgresAdapterOptions | string);
|
|
147
|
+
open(openOptions?: StorageOpenOptions): Promise<void>;
|
|
148
|
+
run(statement: string, parameters?: StorageParameters): Promise<StorageRunResult>;
|
|
149
|
+
get<T>(statement: string, parameters?: StorageParameters): Promise<T | null>;
|
|
150
|
+
all<T>(statement: string, parameters?: StorageParameters): Promise<T[]>;
|
|
151
|
+
exec(script: string): Promise<void>;
|
|
152
|
+
transaction<T>(fn: (trx: StorageAdapter) => Promise<T>): Promise<T>;
|
|
153
|
+
close(): Promise<void>;
|
|
154
|
+
private getExecutor;
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Create a PostgreSQL adapter with connection string.
|
|
158
|
+
* @param connectionString - PostgreSQL connection string
|
|
159
|
+
* @example
|
|
160
|
+
* ```typescript
|
|
161
|
+
* const db = createPostgresAdapter('postgresql://user:pass@localhost:5432/mydb');
|
|
162
|
+
* ```
|
|
163
|
+
*/
|
|
164
|
+
export declare function createPostgresAdapter(connectionString: string): StorageAdapter;
|
|
165
|
+
/**
|
|
166
|
+
* Create a PostgreSQL adapter with configuration options.
|
|
167
|
+
* @param options - PostgreSQL adapter configuration
|
|
168
|
+
* @example
|
|
169
|
+
* ```typescript
|
|
170
|
+
* const db = createPostgresAdapter({
|
|
171
|
+
* host: 'db.example.com',
|
|
172
|
+
* database: 'mydb',
|
|
173
|
+
* user: 'dbuser',
|
|
174
|
+
* password: 'secure_password',
|
|
175
|
+
* ssl: true
|
|
176
|
+
* });
|
|
177
|
+
* ```
|
|
178
|
+
*/
|
|
179
|
+
export declare function createPostgresAdapter(options: PostgresAdapterOptions): StorageAdapter;
|
|
180
|
+
//# sourceMappingURL=postgresAdapter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"postgresAdapter.d.ts","sourceRoot":"","sources":["../../src/adapters/postgresAdapter.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,cAAc,EAAE,iBAAiB,EAAE,kBAAkB,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,MAAM,UAAU,CAAC;AAG3H;;;GAGG;AACH,MAAM,WAAW,sBAAsB;IACrC,qFAAqF;IACrF,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,mDAAmD;IACnD,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,kCAAkC;IAClC,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,oBAAoB;IACpB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,kCAAkC;IAClC,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,kCAAkC;IAClC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,yDAAyD;IACzD,GAAG,CAAC,EAAE,OAAO,GAAG;QAAE,kBAAkB,CAAC,EAAE,OAAO,CAAC;QAAC,EAAE,CAAC,EAAE,MAAM,CAAC;QAAC,IAAI,CAAC,EAAE,MAAM,CAAC;QAAC,GAAG,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;IAC3F,yCAAyC;IACzC,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,qCAAqC;IACrC,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,0CAA0C;IAC1C,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,yDAAyD;IACzD,uBAAuB,CAAC,EAAE,MAAM,CAAC;IACjC,+CAA+C;IAC/C,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,+CAA+C;IAC/C,iBAAiB,CAAC,EAAE,MAA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;;;;GAaG;AACH,wBAAgB,qBAAqB,CAAC,OAAO,EAAE,sBAAsB,GAAG,cAAc,CAAC"}
|
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
import { Pool } from 'pg';
|
|
2
|
+
import { normaliseParameters } from '../utils/parameterUtils';
|
|
3
|
+
const isPositional = (statement) => statement.includes('?');
|
|
4
|
+
const buildNamedStatement = (statement, named) => {
|
|
5
|
+
const order = [];
|
|
6
|
+
const text = statement.replace(/@([A-Za-z0-9_]+)/g, (_, name) => {
|
|
7
|
+
order.push(name);
|
|
8
|
+
return `$${order.length}`;
|
|
9
|
+
});
|
|
10
|
+
const values = order.map((key) => named[key]);
|
|
11
|
+
return { text, values };
|
|
12
|
+
};
|
|
13
|
+
const buildPositionalStatement = (statement, positional) => {
|
|
14
|
+
let index = 0;
|
|
15
|
+
const text = statement.replace(/\?/g, () => {
|
|
16
|
+
index += 1;
|
|
17
|
+
return `$${index}`;
|
|
18
|
+
});
|
|
19
|
+
return { text, values: positional };
|
|
20
|
+
};
|
|
21
|
+
const prepareStatement = (statement, parameters) => {
|
|
22
|
+
const { named, positional } = normaliseParameters(parameters);
|
|
23
|
+
if (named) {
|
|
24
|
+
return buildNamedStatement(statement, named);
|
|
25
|
+
}
|
|
26
|
+
if (positional) {
|
|
27
|
+
return buildPositionalStatement(statement, positional);
|
|
28
|
+
}
|
|
29
|
+
if (isPositional(statement)) {
|
|
30
|
+
return buildPositionalStatement(statement, []);
|
|
31
|
+
}
|
|
32
|
+
return { text: statement, values: [] };
|
|
33
|
+
};
|
|
34
|
+
const splitStatements = (script) => script
|
|
35
|
+
.split(';')
|
|
36
|
+
.map((part) => part.trim())
|
|
37
|
+
.filter((part) => part.length > 0);
|
|
38
|
+
/**
|
|
39
|
+
* PostgreSQL adapter for production-grade SQL operations.
|
|
40
|
+
*
|
|
41
|
+
* ## Performance Characteristics
|
|
42
|
+
* - Connection pooling for efficient resource usage
|
|
43
|
+
* - Excellent concurrent access with MVCC
|
|
44
|
+
* - Optimized for complex queries and large datasets
|
|
45
|
+
* - ~10,000 queries/second with connection pooling
|
|
46
|
+
*
|
|
47
|
+
* ## Advantages
|
|
48
|
+
* - Full SQL feature set (CTEs, window functions, etc.)
|
|
49
|
+
* - Native JSON/JSONB support
|
|
50
|
+
* - Robust replication and backup options
|
|
51
|
+
* - Battle-tested in production environments
|
|
52
|
+
* - Secure remote connections with SSL/TLS
|
|
53
|
+
*
|
|
54
|
+
* ## Limitations
|
|
55
|
+
* - Requires separate server process
|
|
56
|
+
* - Network latency for remote connections
|
|
57
|
+
* - Higher resource consumption than SQLite
|
|
58
|
+
* - Configuration complexity for optimal performance
|
|
59
|
+
*
|
|
60
|
+
* ## When to Use
|
|
61
|
+
* - Production web applications
|
|
62
|
+
* - Multi-user systems
|
|
63
|
+
* - When you need advanced SQL features
|
|
64
|
+
* - Cloud deployments (AWS RDS, Heroku, Supabase, etc.)
|
|
65
|
+
* - Remote database access
|
|
66
|
+
*
|
|
67
|
+
* ## Remote Connection Examples
|
|
68
|
+
*
|
|
69
|
+
* ### Connection String (Recommended)
|
|
70
|
+
* ```typescript
|
|
71
|
+
* const db = createPostgresAdapter({
|
|
72
|
+
* connectionString: 'postgresql://user:password@db.example.com:5432/mydb?sslmode=require'
|
|
73
|
+
* });
|
|
74
|
+
* ```
|
|
75
|
+
*
|
|
76
|
+
* ### Granular Configuration
|
|
77
|
+
* ```typescript
|
|
78
|
+
* const db = createPostgresAdapter({
|
|
79
|
+
* host: 'db.example.com',
|
|
80
|
+
* port: 5432,
|
|
81
|
+
* database: 'mydb',
|
|
82
|
+
* user: 'dbuser',
|
|
83
|
+
* password: 'secure_password',
|
|
84
|
+
* ssl: true, // Enable SSL for security
|
|
85
|
+
* max: 20, // Connection pool size
|
|
86
|
+
* statement_timeout: 30000 // 30 second timeout
|
|
87
|
+
* });
|
|
88
|
+
* ```
|
|
89
|
+
*
|
|
90
|
+
* ### Cloud Provider Examples
|
|
91
|
+
*
|
|
92
|
+
* #### AWS RDS
|
|
93
|
+
* ```typescript
|
|
94
|
+
* const db = createPostgresAdapter({
|
|
95
|
+
* host: 'mydb.abc123.us-east-1.rds.amazonaws.com',
|
|
96
|
+
* port: 5432,
|
|
97
|
+
* database: 'postgres',
|
|
98
|
+
* user: 'admin',
|
|
99
|
+
* password: process.env.RDS_PASSWORD,
|
|
100
|
+
* ssl: { rejectUnauthorized: true }
|
|
101
|
+
* });
|
|
102
|
+
* ```
|
|
103
|
+
*
|
|
104
|
+
* #### Heroku Postgres
|
|
105
|
+
* ```typescript
|
|
106
|
+
* const db = createPostgresAdapter({
|
|
107
|
+
* connectionString: process.env.DATABASE_URL,
|
|
108
|
+
* ssl: { rejectUnauthorized: false } // Heroku uses self-signed certs
|
|
109
|
+
* });
|
|
110
|
+
* ```
|
|
111
|
+
*
|
|
112
|
+
* #### Supabase
|
|
113
|
+
* ```typescript
|
|
114
|
+
* const db = createPostgresAdapter({
|
|
115
|
+
* connectionString: process.env.SUPABASE_DB_URL,
|
|
116
|
+
* ssl: true
|
|
117
|
+
* });
|
|
118
|
+
* ```
|
|
119
|
+
*
|
|
120
|
+
* #### DigitalOcean Managed Database
|
|
121
|
+
* ```typescript
|
|
122
|
+
* const db = createPostgresAdapter({
|
|
123
|
+
* host: 'db-postgresql-nyc3-12345.ondigitalocean.com',
|
|
124
|
+
* port: 25060,
|
|
125
|
+
* database: 'defaultdb',
|
|
126
|
+
* user: 'doadmin',
|
|
127
|
+
* password: process.env.DO_DB_PASSWORD,
|
|
128
|
+
* ssl: { rejectUnauthorized: true, ca: process.env.DO_CA_CERT }
|
|
129
|
+
* });
|
|
130
|
+
* ```
|
|
131
|
+
*
|
|
132
|
+
* ## Graceful Degradation
|
|
133
|
+
* - Automatic reconnection on connection loss
|
|
134
|
+
* - Connection pool handles transient failures
|
|
135
|
+
* - Falls back to SQLite if PostgreSQL unavailable
|
|
136
|
+
*/
|
|
137
|
+
export class PostgresAdapter {
|
|
138
|
+
constructor(options) {
|
|
139
|
+
this.kind = 'postgres';
|
|
140
|
+
this.capabilities = new Set([
|
|
141
|
+
'transactions', // Full ACID transaction support
|
|
142
|
+
'locks', // Row-level and advisory locks
|
|
143
|
+
'persistence', // Data persisted to disk
|
|
144
|
+
'concurrent', // Excellent concurrent access
|
|
145
|
+
'json', // Native JSON/JSONB support
|
|
146
|
+
'arrays', // Native array data types
|
|
147
|
+
'prepared' // Prepared statements for security/performance
|
|
148
|
+
]);
|
|
149
|
+
this.pool = null;
|
|
150
|
+
this.transactionalClient = null;
|
|
151
|
+
// Support both string and object initialization
|
|
152
|
+
if (typeof options === 'string') {
|
|
153
|
+
this.options = { connectionString: options };
|
|
154
|
+
}
|
|
155
|
+
else {
|
|
156
|
+
this.options = options;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
async open(openOptions) {
|
|
160
|
+
if (this.pool) {
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
// Build pool configuration
|
|
164
|
+
const poolConfig = {};
|
|
165
|
+
// Use connection string if provided
|
|
166
|
+
if (this.options.connectionString || openOptions?.connectionString) {
|
|
167
|
+
poolConfig.connectionString = openOptions?.connectionString ?? this.options.connectionString;
|
|
168
|
+
}
|
|
169
|
+
else {
|
|
170
|
+
// Build from individual options
|
|
171
|
+
poolConfig.host = this.options.host ?? 'localhost';
|
|
172
|
+
poolConfig.port = this.options.port ?? 5432;
|
|
173
|
+
poolConfig.database = this.options.database;
|
|
174
|
+
poolConfig.user = this.options.user;
|
|
175
|
+
poolConfig.password = this.options.password;
|
|
176
|
+
}
|
|
177
|
+
// Pool configuration
|
|
178
|
+
if (this.options.max !== undefined)
|
|
179
|
+
poolConfig.max = this.options.max;
|
|
180
|
+
if (this.options.min !== undefined)
|
|
181
|
+
poolConfig.min = this.options.min;
|
|
182
|
+
if (this.options.idleTimeoutMillis !== undefined) {
|
|
183
|
+
poolConfig.idleTimeoutMillis = this.options.idleTimeoutMillis;
|
|
184
|
+
}
|
|
185
|
+
if (this.options.connectionTimeoutMillis !== undefined) {
|
|
186
|
+
poolConfig.connectionTimeoutMillis = this.options.connectionTimeoutMillis;
|
|
187
|
+
}
|
|
188
|
+
if (this.options.application_name !== undefined) {
|
|
189
|
+
poolConfig.application_name = this.options.application_name;
|
|
190
|
+
}
|
|
191
|
+
if (this.options.statement_timeout !== undefined) {
|
|
192
|
+
poolConfig.statement_timeout = this.options.statement_timeout;
|
|
193
|
+
}
|
|
194
|
+
if (this.options.query_timeout !== undefined) {
|
|
195
|
+
poolConfig.query_timeout = this.options.query_timeout;
|
|
196
|
+
}
|
|
197
|
+
// SSL configuration
|
|
198
|
+
if (this.options.ssl !== undefined) {
|
|
199
|
+
poolConfig.ssl = this.options.ssl;
|
|
200
|
+
}
|
|
201
|
+
this.pool = new Pool(poolConfig);
|
|
202
|
+
// Test connection
|
|
203
|
+
const client = await this.pool.connect();
|
|
204
|
+
client.release();
|
|
205
|
+
}
|
|
206
|
+
async run(statement, parameters) {
|
|
207
|
+
const executor = await this.getExecutor();
|
|
208
|
+
const { text, values } = prepareStatement(statement, parameters);
|
|
209
|
+
const result = await executor.query(text, values);
|
|
210
|
+
return { changes: result.rowCount ?? 0, lastInsertRowid: result.rows?.[0]?.id ?? null };
|
|
211
|
+
}
|
|
212
|
+
async get(statement, parameters) {
|
|
213
|
+
const executor = await this.getExecutor();
|
|
214
|
+
const { text, values } = prepareStatement(statement, parameters);
|
|
215
|
+
const result = await executor.query(text, values);
|
|
216
|
+
return result.rows?.[0] ?? null;
|
|
217
|
+
}
|
|
218
|
+
async all(statement, parameters) {
|
|
219
|
+
const executor = await this.getExecutor();
|
|
220
|
+
const { text, values } = prepareStatement(statement, parameters);
|
|
221
|
+
const result = await executor.query(text, values);
|
|
222
|
+
return result.rows ?? [];
|
|
223
|
+
}
|
|
224
|
+
async exec(script) {
|
|
225
|
+
const executor = await this.getExecutor();
|
|
226
|
+
const statements = splitStatements(script);
|
|
227
|
+
for (const text of statements) {
|
|
228
|
+
await executor.query(text);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
async transaction(fn) {
|
|
232
|
+
if (!this.pool) {
|
|
233
|
+
throw new Error('Postgres adapter not opened.');
|
|
234
|
+
}
|
|
235
|
+
const client = await this.pool.connect();
|
|
236
|
+
try {
|
|
237
|
+
await client.query('BEGIN');
|
|
238
|
+
this.transactionalClient = client;
|
|
239
|
+
const result = await fn(this);
|
|
240
|
+
await client.query('COMMIT');
|
|
241
|
+
return result;
|
|
242
|
+
}
|
|
243
|
+
catch (error) {
|
|
244
|
+
await client.query('ROLLBACK');
|
|
245
|
+
throw error;
|
|
246
|
+
}
|
|
247
|
+
finally {
|
|
248
|
+
this.transactionalClient = null;
|
|
249
|
+
client.release();
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
async close() {
|
|
253
|
+
if (this.pool) {
|
|
254
|
+
await this.pool.end();
|
|
255
|
+
this.pool = null;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
async getExecutor() {
|
|
259
|
+
if (this.transactionalClient) {
|
|
260
|
+
return this.transactionalClient;
|
|
261
|
+
}
|
|
262
|
+
if (!this.pool) {
|
|
263
|
+
throw new Error('Postgres adapter not opened.');
|
|
264
|
+
}
|
|
265
|
+
return this.pool;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
export function createPostgresAdapter(optionsOrString) {
|
|
269
|
+
return new PostgresAdapter(optionsOrString);
|
|
270
|
+
}
|
|
271
|
+
//# sourceMappingURL=postgresAdapter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"postgresAdapter.js","sourceRoot":"","sources":["../../src/adapters/postgresAdapter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAA0B,MAAM,IAAI,CAAC;AAElD,OAAO,EAAE,mBAAmB,EAAE,MAAM,yBAAyB,CAAC;AA0C9D,MAAM,YAAY,GAAG,CAAC,SAAiB,EAAW,EAAE,CAAC,SAAS,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC;AAE7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orageParameters, StorageRunResult } from '../types';
|
|
3
|
+
type SqlJsAdapterOptions = SqlJsConfig;
|
|
4
|
+
/**
|
|
5
|
+
* Storage adapter backed by sql.js (WebAssembly) for environments without native SQLite bindings.
|
|
6
|
+
*/
|
|
7
|
+
export declare class SqlJsAdapter implements StorageAdapter {
|
|
8
|
+
private readonly adapterOptions;
|
|
9
|
+
readonly kind = "sqljs";
|
|
10
|
+
readonly capabilities: ReadonlySet<StorageCapability>;
|
|
11
|
+
private SQL;
|
|
12
|
+
private db;
|
|
13
|
+
private filePath?;
|
|
14
|
+
constructor(adapterOptions?: SqlJsAdapterOptions);
|
|
15
|
+
open(options?: StorageOpenOptions): Promise<void>;
|
|
16
|
+
run(statement: string, parameters?: StorageParameters): Promise<StorageRunResult>;
|
|
17
|
+
get<T>(statement: string, parameters?: StorageParameters): Promise<T | null>;
|
|
18
|
+
all<T>(statement: string, parameters?: StorageParameters): Promise<T[]>;
|
|
19
|
+
exec(script: string): Promise<void>;
|
|
20
|
+
transaction<T>(fn: (trx: StorageAdapter) => Promise<T>): Promise<T>;
|
|
21
|
+
close(): Promise<void>;
|
|
22
|
+
private prepareInternal;
|
|
23
|
+
private ensureOpen;
|
|
24
|
+
private persistIfNeeded;
|
|
25
|
+
}
|
|
26
|
+
export declare const createSqlJsAdapter: (options?: SqlJsAdapterOptions) => StorageAdapter;
|
|
27
|
+
export {};
|
|
28
|
+
//# sourceMappingURL=sqlJsAdapter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sqlJsAdapter.d.ts","sourceRoot":"","sources":["../../src/adapters/sqlJsAdapter.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAe,WAAW,EAA6B,MAAM,QAAQ,CAAC;AAGlF,OAAO,KAAK,EAAE,cAAc,EAAE,iBAAiB,EAAE,kBAAkB,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,MAAM,UAAU,CAAC;AAG3H,KAAK,mBAAmB,GAAG,WAAW,CAA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