@sqlitecloud/drivers 0.0.34
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 +52 -0
- package/lib/connection.d.ts +65 -0
- package/lib/connection.js +264 -0
- package/lib/database.d.ts +154 -0
- package/lib/database.js +398 -0
- package/lib/index.d.ts +7 -0
- package/lib/index.js +22 -0
- package/lib/rowset.d.ts +36 -0
- package/lib/rowset.js +138 -0
- package/lib/sqlitecloud.v0.0.34.dev.js +597 -0
- package/lib/sqlitecloud.v0.0.34.js +1 -0
- package/lib/statement.d.ts +59 -0
- package/lib/statement.js +124 -0
- package/lib/transport-tls.d.ts +25 -0
- package/lib/transport-tls.js +458 -0
- package/lib/transport-ws.d.ts +24 -0
- package/lib/transport-ws.js +81 -0
- package/lib/types.d.ts +113 -0
- package/lib/types.js +38 -0
- package/lib/utilities.d.ts +23 -0
- package/lib/utilities.js +136 -0
- package/package.json +84 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 SQLiteCloud, Inc.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# sqlitecloud-js
|
|
2
|
+
|
|
3
|
+
[![npm package][npm-img]][npm-url]
|
|
4
|
+
[![Build Status][build-img]][build-url]
|
|
5
|
+
[![Downloads][downloads-img]][downloads-url]
|
|
6
|
+
[![Issues][issues-img]][issues-url]
|
|
7
|
+
[](https://codecov.io/gh/sqlitecloud/sqlitecloud-js)
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install sqlitecloud-js
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import { Database } from 'sqlitecloud-js'
|
|
19
|
+
|
|
20
|
+
let database = new Database('sqlitecloud://user:password@xxx.sqlite.cloud:8860/chinook.db')
|
|
21
|
+
|
|
22
|
+
let name = 'Breaking The Rules'
|
|
23
|
+
|
|
24
|
+
let results = await database.sql`SELECT * FROM tracks WHERE name = ${name}`
|
|
25
|
+
// => returns [{ AlbumId: 1, Name: 'Breaking The Rules', Composer: 'Angus Young... }]
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Use [Database.sql](https://sqlitecloud.github.io/sqlitecloud-js/classes/Database.html#sql) to execute prepared statements or plain SQL queries asynchronously. This method returns an array of rows for SELECT queries and supports the standard syntax for UPDATE, INSERT, and DELETE.
|
|
29
|
+
|
|
30
|
+
We aim for full compatibility with the established [sqlite3 API](https://www.npmjs.com/package/sqlite3), with the primary distinction being that our driver connects to SQLiteCloud databases. This allows you to migrate your [SQLite to the cloud](https://sqlitecloud.io) while continuing to use your existing codebase.
|
|
31
|
+
|
|
32
|
+
The package is developed entirely in TypeScript and is fully compatible with JavaScript. It doesn't require any native libraries. This makes it a straightforward and effective tool for managing cloud-based databases in a familiar SQLite environment.
|
|
33
|
+
|
|
34
|
+
## More
|
|
35
|
+
|
|
36
|
+
How do I deploy SQLite in the cloud?
|
|
37
|
+
[https://sqlitecloud.io](https://sqlitecloud.io)
|
|
38
|
+
|
|
39
|
+
How do I connect SQLite cloud with Javascript?
|
|
40
|
+
[https://sqlitecloud.github.io/sqlitecloud-js/](https://sqlitecloud.github.io/sqlitecloud-js/)
|
|
41
|
+
|
|
42
|
+
How can I contribute or suggest features?
|
|
43
|
+
[https://github.com/sqlitecloud/sqlitecloud-js/issues](https://github.com/sqlitecloud/sqlitecloud-js/issues)
|
|
44
|
+
|
|
45
|
+
[build-img]: https://github.com/sqlitecloud/sqlitecloud-js/actions/workflows/build-test-deploy.yml/badge.svg
|
|
46
|
+
[build-url]: https://github.com/sqlitecloud/sqlitecloud-js/actions/workflows/build-test-deploy.yml
|
|
47
|
+
[downloads-img]: https://img.shields.io/npm/dt/sqlitecloud-js
|
|
48
|
+
[downloads-url]: https://www.npmtrends.com/sqlitecloud-js
|
|
49
|
+
[npm-img]: https://img.shields.io/npm/v/sqlitecloud-js
|
|
50
|
+
[npm-url]: https://www.npmjs.com/package/sqlitecloud-js
|
|
51
|
+
[issues-img]: https://img.shields.io/github/issues/sqlitecloud/sqlitecloud-js
|
|
52
|
+
[issues-url]: https://github.com/sqlitecloud/sqlitecloud-js/issues
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* connection.ts - handles low level communication with sqlitecloud server
|
|
3
|
+
*/
|
|
4
|
+
import { SQLiteCloudConfig, ErrorCallback, ResultsCallback } from './types';
|
|
5
|
+
/** Default timeout value for queries */
|
|
6
|
+
export declare const DEFAULT_TIMEOUT: number;
|
|
7
|
+
/** Default tls connection port */
|
|
8
|
+
export declare const DEFAULT_PORT = 9960;
|
|
9
|
+
/**
|
|
10
|
+
* Base class for SQLiteCloudConnection handles basics and defines methods.
|
|
11
|
+
* Actual connection management and communication with the server in concrete classes.
|
|
12
|
+
*/
|
|
13
|
+
export declare class SQLiteCloudConnection {
|
|
14
|
+
/** Parse and validate provided connectionString or configuration */
|
|
15
|
+
constructor(config: SQLiteCloudConfig | string, callback?: ErrorCallback);
|
|
16
|
+
/** Configuration passed by client or extracted from connection string */
|
|
17
|
+
protected config: SQLiteCloudConfig;
|
|
18
|
+
/** Transport used to communicate with server */
|
|
19
|
+
protected transport?: ConnectionTransport;
|
|
20
|
+
/** Operations are serialized by waiting an any pending promises */
|
|
21
|
+
protected operations: OperationsQueue;
|
|
22
|
+
/** True if connection is open */
|
|
23
|
+
get connected(): boolean;
|
|
24
|
+
/** Connect will establish a tls or websocket transport to the server based on configuration and environment */
|
|
25
|
+
protected connect(callback?: ErrorCallback): this;
|
|
26
|
+
/** Validate configuration, apply defaults, throw if something is missing or misconfigured */
|
|
27
|
+
protected validateConfiguration(config: SQLiteCloudConfig): SQLiteCloudConfig;
|
|
28
|
+
/** Will log to console if verbose mode is enabled */
|
|
29
|
+
protected log(message: string, ...optionalParams: any[]): void;
|
|
30
|
+
/** Enable verbose logging for debug purposes */
|
|
31
|
+
verbose(): void;
|
|
32
|
+
/** Will enquee a command to be executed and callback with the resulting rowset/result/error */
|
|
33
|
+
sendCommands(commands: string, callback?: ResultsCallback): this;
|
|
34
|
+
/** Disconnect from server, release connection. */
|
|
35
|
+
close(): this;
|
|
36
|
+
}
|
|
37
|
+
type OperationCallback = (error: Error | null) => void;
|
|
38
|
+
type Operation = (done: OperationCallback) => void;
|
|
39
|
+
export declare class OperationsQueue {
|
|
40
|
+
private queue;
|
|
41
|
+
private isProcessing;
|
|
42
|
+
/** Add operations to the queue, process immediately if possible, else wait for previous operations to complete */
|
|
43
|
+
enqueue(operation: Operation): void;
|
|
44
|
+
/** Clear the queue */
|
|
45
|
+
clear(): void;
|
|
46
|
+
/** Process the next operation in the queue */
|
|
47
|
+
private processNext;
|
|
48
|
+
}
|
|
49
|
+
/** Messages going to the server are sometimes logged when error conditions occour and need to be stripped of user credentials */
|
|
50
|
+
export declare function anonimizeCommand(message: string): string;
|
|
51
|
+
/** Strip message code in error of user credentials */
|
|
52
|
+
export declare function anonimizeError(error: Error): Error;
|
|
53
|
+
/** Initialization commands sent to database when connection is established */
|
|
54
|
+
export declare function getInitializationCommands(config: SQLiteCloudConfig): string;
|
|
55
|
+
/** ConnectionTransport implements the underlying transport layer for the connection */
|
|
56
|
+
export interface ConnectionTransport {
|
|
57
|
+
/** True if connection is currently open */
|
|
58
|
+
get connected(): boolean;
|
|
59
|
+
connect(config: SQLiteCloudConfig, callback?: ErrorCallback): this;
|
|
60
|
+
/** Send a command, return the rowset/result or throw an error */
|
|
61
|
+
processCommands(commands: string, callback?: ResultsCallback): this;
|
|
62
|
+
/** Disconnect from server, release transport. */
|
|
63
|
+
close(): this;
|
|
64
|
+
}
|
|
65
|
+
export {};
|
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* connection.ts - handles low level communication with sqlitecloud server
|
|
4
|
+
*/
|
|
5
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
8
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
9
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
10
|
+
}
|
|
11
|
+
Object.defineProperty(o, k2, desc);
|
|
12
|
+
}) : (function(o, m, k, k2) {
|
|
13
|
+
if (k2 === undefined) k2 = k;
|
|
14
|
+
o[k2] = m[k];
|
|
15
|
+
}));
|
|
16
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
17
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
18
|
+
}) : function(o, v) {
|
|
19
|
+
o["default"] = v;
|
|
20
|
+
});
|
|
21
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
22
|
+
if (mod && mod.__esModule) return mod;
|
|
23
|
+
var result = {};
|
|
24
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
25
|
+
__setModuleDefault(result, mod);
|
|
26
|
+
return result;
|
|
27
|
+
};
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
exports.getInitializationCommands = exports.anonimizeError = exports.anonimizeCommand = exports.OperationsQueue = exports.SQLiteCloudConnection = exports.DEFAULT_PORT = exports.DEFAULT_TIMEOUT = void 0;
|
|
30
|
+
const types_1 = require("./types");
|
|
31
|
+
const utilities_1 = require("./utilities");
|
|
32
|
+
/** Default timeout value for queries */
|
|
33
|
+
exports.DEFAULT_TIMEOUT = 300 * 1000;
|
|
34
|
+
/** Default tls connection port */
|
|
35
|
+
exports.DEFAULT_PORT = 9960;
|
|
36
|
+
/**
|
|
37
|
+
* Base class for SQLiteCloudConnection handles basics and defines methods.
|
|
38
|
+
* Actual connection management and communication with the server in concrete classes.
|
|
39
|
+
*/
|
|
40
|
+
class SQLiteCloudConnection {
|
|
41
|
+
/** Parse and validate provided connectionString or configuration */
|
|
42
|
+
constructor(config, callback) {
|
|
43
|
+
/** Operations are serialized by waiting an any pending promises */
|
|
44
|
+
this.operations = new OperationsQueue();
|
|
45
|
+
if (typeof config === 'string') {
|
|
46
|
+
this.config = this.validateConfiguration({ connectionString: config });
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
this.config = this.validateConfiguration(config);
|
|
50
|
+
}
|
|
51
|
+
// connect transport layer to server
|
|
52
|
+
this.connect(callback);
|
|
53
|
+
}
|
|
54
|
+
//
|
|
55
|
+
// public properties
|
|
56
|
+
//
|
|
57
|
+
/** True if connection is open */
|
|
58
|
+
get connected() {
|
|
59
|
+
var _a;
|
|
60
|
+
return ((_a = this.transport) === null || _a === void 0 ? void 0 : _a.connected) || false;
|
|
61
|
+
}
|
|
62
|
+
/** Connect will establish a tls or websocket transport to the server based on configuration and environment */
|
|
63
|
+
connect(callback) {
|
|
64
|
+
this.operations.enqueue(done => {
|
|
65
|
+
var _a, _b;
|
|
66
|
+
// connect using websocket if tls is not supported or if explicitly requested
|
|
67
|
+
if (utilities_1.isBrowser || ((_a = this.config) === null || _a === void 0 ? void 0 : _a.useWebsocket) || ((_b = this.config) === null || _b === void 0 ? void 0 : _b.gatewayUrl)) {
|
|
68
|
+
// socket.io transport works in both node.js and browser environments and connects via SQLite Cloud Gateway
|
|
69
|
+
Promise.resolve().then(() => __importStar(require('./transport-ws'))).then(transport => {
|
|
70
|
+
this.transport = new transport.WebSocketTransport();
|
|
71
|
+
this.transport.connect(this.config, error => {
|
|
72
|
+
if (error) {
|
|
73
|
+
console.error(`SQLiteCloudConnection.connect - error while connecting WebSocketTransport: ${error.toString()}`, this.config, error);
|
|
74
|
+
this.close();
|
|
75
|
+
}
|
|
76
|
+
callback === null || callback === void 0 ? void 0 : callback.call(this, error || null);
|
|
77
|
+
done(error);
|
|
78
|
+
});
|
|
79
|
+
})
|
|
80
|
+
.catch(error => {
|
|
81
|
+
done(error);
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
// tls sockets work only in node.js environments
|
|
86
|
+
Promise.resolve().then(() => __importStar(require('./transport-tls'))).then(transport => {
|
|
87
|
+
this.transport = new transport.TlsSocketTransport();
|
|
88
|
+
this.transport.connect(this.config, error => {
|
|
89
|
+
if (error) {
|
|
90
|
+
console.error(`SQLiteCloudConnection.connect - error while connecting TlsSocketTransport: ${error.toString()}`, this.config, error);
|
|
91
|
+
this.close();
|
|
92
|
+
}
|
|
93
|
+
callback === null || callback === void 0 ? void 0 : callback.call(this, error || null);
|
|
94
|
+
done(error);
|
|
95
|
+
});
|
|
96
|
+
})
|
|
97
|
+
.catch(error => {
|
|
98
|
+
done(error);
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
return this;
|
|
103
|
+
}
|
|
104
|
+
//
|
|
105
|
+
// private methods
|
|
106
|
+
//
|
|
107
|
+
/** Validate configuration, apply defaults, throw if something is missing or misconfigured */
|
|
108
|
+
validateConfiguration(config) {
|
|
109
|
+
if (config.connectionString) {
|
|
110
|
+
config = Object.assign(Object.assign(Object.assign({}, config), (0, utilities_1.parseConnectionString)(config.connectionString)), { connectionString: config.connectionString // keep original connection string
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
// apply defaults where needed
|
|
114
|
+
config.port || (config.port = exports.DEFAULT_PORT);
|
|
115
|
+
config.timeout = config.timeout && config.timeout > 0 ? config.timeout : exports.DEFAULT_TIMEOUT;
|
|
116
|
+
config.clientId || (config.clientId = 'SQLiteCloud');
|
|
117
|
+
config.verbose = (0, utilities_1.parseBoolean)(config.verbose);
|
|
118
|
+
config.noBlob = (0, utilities_1.parseBoolean)(config.noBlob);
|
|
119
|
+
config.compression = (0, utilities_1.parseBoolean)(config.compression);
|
|
120
|
+
config.createDatabase = (0, utilities_1.parseBoolean)(config.createDatabase);
|
|
121
|
+
config.nonlinearizable = (0, utilities_1.parseBoolean)(config.nonlinearizable);
|
|
122
|
+
config.sqliteMode = (0, utilities_1.parseBoolean)(config.sqliteMode);
|
|
123
|
+
if (!config.username || !config.password || !config.host) {
|
|
124
|
+
console.error('SQLiteCloudConnection.validateConfiguration - missing arguments', config);
|
|
125
|
+
throw new types_1.SQLiteCloudError('The user, password and host arguments must be specified.', { errorCode: 'ERR_MISSING_ARGS' });
|
|
126
|
+
}
|
|
127
|
+
if (!config.connectionString) {
|
|
128
|
+
// build connection string from configuration, values are already validated
|
|
129
|
+
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
|
130
|
+
config.connectionString = `sqlitecloud://${config.username}:${config.password}@${config.host}:${config.port}/${config.database}`;
|
|
131
|
+
}
|
|
132
|
+
return config;
|
|
133
|
+
}
|
|
134
|
+
/** Will log to console if verbose mode is enabled */
|
|
135
|
+
log(message, ...optionalParams) {
|
|
136
|
+
if (this.config.verbose) {
|
|
137
|
+
message = anonimizeCommand(message);
|
|
138
|
+
console.log(`${new Date().toISOString()} ${this.config.clientId}: ${message}`, ...optionalParams);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
//
|
|
142
|
+
// public methods
|
|
143
|
+
//
|
|
144
|
+
/** Enable verbose logging for debug purposes */
|
|
145
|
+
verbose() {
|
|
146
|
+
this.config.verbose = true;
|
|
147
|
+
}
|
|
148
|
+
/** Will enquee a command to be executed and callback with the resulting rowset/result/error */
|
|
149
|
+
sendCommands(commands, callback) {
|
|
150
|
+
this.operations.enqueue(done => {
|
|
151
|
+
if (this.transport) {
|
|
152
|
+
this.transport.processCommands(commands, (error, result) => {
|
|
153
|
+
callback === null || callback === void 0 ? void 0 : callback.call(this, error, result);
|
|
154
|
+
done(error);
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
else {
|
|
158
|
+
const error = new types_1.SQLiteCloudError('Connection not established', { errorCode: 'ERR_CONNECTION_NOT_ESTABLISHED' });
|
|
159
|
+
callback === null || callback === void 0 ? void 0 : callback.call(this, error);
|
|
160
|
+
done(error);
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
return this;
|
|
164
|
+
}
|
|
165
|
+
/** Disconnect from server, release connection. */
|
|
166
|
+
close() {
|
|
167
|
+
var _a;
|
|
168
|
+
this.operations.clear();
|
|
169
|
+
(_a = this.transport) === null || _a === void 0 ? void 0 : _a.close();
|
|
170
|
+
this.transport = undefined;
|
|
171
|
+
return this;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
exports.SQLiteCloudConnection = SQLiteCloudConnection;
|
|
175
|
+
class OperationsQueue {
|
|
176
|
+
constructor() {
|
|
177
|
+
this.queue = [];
|
|
178
|
+
this.isProcessing = false;
|
|
179
|
+
}
|
|
180
|
+
/** Add operations to the queue, process immediately if possible, else wait for previous operations to complete */
|
|
181
|
+
enqueue(operation) {
|
|
182
|
+
this.queue.push(operation);
|
|
183
|
+
if (!this.isProcessing) {
|
|
184
|
+
this.processNext();
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
/** Clear the queue */
|
|
188
|
+
clear() {
|
|
189
|
+
this.queue = [];
|
|
190
|
+
this.isProcessing = false;
|
|
191
|
+
}
|
|
192
|
+
/** Process the next operation in the queue */
|
|
193
|
+
processNext() {
|
|
194
|
+
if (this.queue.length === 0) {
|
|
195
|
+
this.isProcessing = false;
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
this.isProcessing = true;
|
|
199
|
+
const operation = this.queue.shift();
|
|
200
|
+
operation === null || operation === void 0 ? void 0 : operation(() => {
|
|
201
|
+
// could receive (error) => { ...
|
|
202
|
+
// if (error) {
|
|
203
|
+
// console.warn('OperationQueue.processNext - error in operation', error)
|
|
204
|
+
// }
|
|
205
|
+
// process the next operation in the queue
|
|
206
|
+
this.processNext();
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
exports.OperationsQueue = OperationsQueue;
|
|
211
|
+
//
|
|
212
|
+
// utility functions
|
|
213
|
+
//
|
|
214
|
+
/** Messages going to the server are sometimes logged when error conditions occour and need to be stripped of user credentials */
|
|
215
|
+
function anonimizeCommand(message) {
|
|
216
|
+
// hide password in AUTH command if needed
|
|
217
|
+
message = message.replace(/USER \S+/, 'USER ******');
|
|
218
|
+
message = message.replace(/PASSWORD \S+?(?=;)/, 'PASSWORD ******');
|
|
219
|
+
message = message.replace(/HASH \S+?(?=;)/, 'HASH ******');
|
|
220
|
+
return message;
|
|
221
|
+
}
|
|
222
|
+
exports.anonimizeCommand = anonimizeCommand;
|
|
223
|
+
/** Strip message code in error of user credentials */
|
|
224
|
+
function anonimizeError(error) {
|
|
225
|
+
if (error === null || error === void 0 ? void 0 : error.message) {
|
|
226
|
+
error.message = anonimizeCommand(error.message);
|
|
227
|
+
}
|
|
228
|
+
return error;
|
|
229
|
+
}
|
|
230
|
+
exports.anonimizeError = anonimizeError;
|
|
231
|
+
/** Initialization commands sent to database when connection is established */
|
|
232
|
+
function getInitializationCommands(config) {
|
|
233
|
+
// first user authentication, then all other commands
|
|
234
|
+
let commands = `AUTH USER ${config.username || ''} ${config.passwordHashed ? 'HASH' : 'PASSWORD'} ${config.password || ''}; `;
|
|
235
|
+
if (config.database) {
|
|
236
|
+
if (config.createDatabase && !config.dbMemory) {
|
|
237
|
+
commands += `CREATE DATABASE ${config.database} IF NOT EXISTS; `;
|
|
238
|
+
}
|
|
239
|
+
commands += `USE DATABASE ${config.database}; `;
|
|
240
|
+
}
|
|
241
|
+
if (config.sqliteMode) {
|
|
242
|
+
commands += 'SET CLIENT KEY SQLITE TO 1; ';
|
|
243
|
+
}
|
|
244
|
+
if (config.compression) {
|
|
245
|
+
commands += 'SET CLIENT KEY COMPRESSION TO 1; ';
|
|
246
|
+
}
|
|
247
|
+
if (config.nonlinearizable) {
|
|
248
|
+
commands += 'SET CLIENT KEY NONLINEARIZABLE TO 1; ';
|
|
249
|
+
}
|
|
250
|
+
if (config.noBlob) {
|
|
251
|
+
commands += 'SET CLIENT KEY NOBLOB TO 1; ';
|
|
252
|
+
}
|
|
253
|
+
if (config.maxData) {
|
|
254
|
+
commands += `SET CLIENT KEY MAXDATA TO ${config.maxData}; `;
|
|
255
|
+
}
|
|
256
|
+
if (config.maxRows) {
|
|
257
|
+
commands += `SET CLIENT KEY MAXROWS TO ${config.maxRows}; `;
|
|
258
|
+
}
|
|
259
|
+
if (config.maxRowset) {
|
|
260
|
+
commands += `SET CLIENT KEY MAXROWSET TO ${config.maxRowset}; `;
|
|
261
|
+
}
|
|
262
|
+
return commands;
|
|
263
|
+
}
|
|
264
|
+
exports.getInitializationCommands = getInitializationCommands;
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { SQLiteCloudConfig, RowCountCallback } from './types';
|
|
2
|
+
import { Statement } from './statement';
|
|
3
|
+
import { ErrorCallback, ResultsCallback, RowCallback, RowsCallback } from './types';
|
|
4
|
+
import EventEmitter from 'eventemitter3';
|
|
5
|
+
/**
|
|
6
|
+
* Creating a Database object automatically opens a connection to the SQLite database.
|
|
7
|
+
* When the connection is established the Database object emits an open event and calls
|
|
8
|
+
* the optional provided callback. If the connection cannot be established an error event
|
|
9
|
+
* will be emitted and the optional callback is called with the error information.
|
|
10
|
+
*/
|
|
11
|
+
export declare class Database extends EventEmitter {
|
|
12
|
+
/** Create and initialize a database from a full configuration object, or connection string */
|
|
13
|
+
constructor(config: SQLiteCloudConfig | string, callback?: ErrorCallback);
|
|
14
|
+
constructor(config: SQLiteCloudConfig | string, mode?: number, callback?: ErrorCallback);
|
|
15
|
+
/** Configuration used to open database connections */
|
|
16
|
+
private config;
|
|
17
|
+
/** Database connections */
|
|
18
|
+
private connections;
|
|
19
|
+
/** Returns first available connection from connection pool */
|
|
20
|
+
private getConnection;
|
|
21
|
+
/** Handles an error by closing the connection, calling the callback and/or emitting an error event */
|
|
22
|
+
private handleError;
|
|
23
|
+
/**
|
|
24
|
+
* Some queries like inserts or updates processed via run or exec may generate
|
|
25
|
+
* an empty result (eg. no data was selected), but still have some metadata.
|
|
26
|
+
* For example the server may pass the id of the last row that was modified.
|
|
27
|
+
* In this case the callback results should be empty but the context may contain
|
|
28
|
+
* additional information like lastID, etc.
|
|
29
|
+
* @see https://github.com/TryGhost/node-sqlite3/wiki/API#runsql--param---callback
|
|
30
|
+
* @param results Results received from the server
|
|
31
|
+
* @returns A context object if one makes sense, otherwise undefined
|
|
32
|
+
*/
|
|
33
|
+
private processContext;
|
|
34
|
+
/** Emits given event with optional arguments on the next tick so callbacks can complete first */
|
|
35
|
+
private emitEvent;
|
|
36
|
+
/**
|
|
37
|
+
* Returns the configuration with which this database was opened.
|
|
38
|
+
* The configuration is readonly and cannot be changed as there may
|
|
39
|
+
* be multiple connections using the same configuration.
|
|
40
|
+
* @returns {SQLiteCloudConfig} A configuration object
|
|
41
|
+
*/
|
|
42
|
+
getConfiguration(): SQLiteCloudConfig;
|
|
43
|
+
/** Enable verbose mode */
|
|
44
|
+
verbose(): this;
|
|
45
|
+
/** Set a configuration option for the database */
|
|
46
|
+
configure(_option: string, _value: any): this;
|
|
47
|
+
/**
|
|
48
|
+
* Runs the SQL query with the specified parameters and calls the callback afterwards.
|
|
49
|
+
* The callback will contain the results passed back from the server, for example in the
|
|
50
|
+
* case of an update or insert, these would contain the number of rows modified, etc.
|
|
51
|
+
* It does not retrieve any result data. The function returns the Database object for
|
|
52
|
+
* which it was called to allow for function chaining.
|
|
53
|
+
*/
|
|
54
|
+
run<T>(sql: string, callback?: ResultsCallback<T>): this;
|
|
55
|
+
run<T>(sql: string, params: any, callback?: ResultsCallback<T>): this;
|
|
56
|
+
/**
|
|
57
|
+
* Runs the SQL query with the specified parameters and calls the callback with
|
|
58
|
+
* a subsequent result row. The function returns the Database object to allow for
|
|
59
|
+
* function chaining. The parameters are the same as the Database#run function,
|
|
60
|
+
* with the following differences: The signature of the callback is `function(err, row) {}`.
|
|
61
|
+
* If the result set is empty, the second parameter is undefined, otherwise it is an
|
|
62
|
+
* object containing the values for the first row. The property names correspond to
|
|
63
|
+
* the column names of the result set. It is impossible to access them by column index;
|
|
64
|
+
* the only supported way is by column name.
|
|
65
|
+
*/
|
|
66
|
+
get<T>(sql: string, callback?: RowCallback<T>): this;
|
|
67
|
+
get<T>(sql: string, params: any, callback?: RowCallback<T>): this;
|
|
68
|
+
/**
|
|
69
|
+
* Runs the SQL query with the specified parameters and calls the callback
|
|
70
|
+
* with all result rows afterwards. The function returns the Database object to
|
|
71
|
+
* allow for function chaining. The parameters are the same as the Database#run
|
|
72
|
+
* function, with the following differences: The signature of the callback is
|
|
73
|
+
* function(err, rows) {}. rows is an array. If the result set is empty, it will
|
|
74
|
+
* be an empty array, otherwise it will have an object for each result row which
|
|
75
|
+
* in turn contains the values of that row, like the Database#get function.
|
|
76
|
+
* Note that it first retrieves all result rows and stores them in memory.
|
|
77
|
+
* For queries that have potentially large result sets, use the Database#each
|
|
78
|
+
* function to retrieve all rows or Database#prepare followed by multiple Statement#get
|
|
79
|
+
* calls to retrieve a previously unknown amount of rows.
|
|
80
|
+
*/
|
|
81
|
+
all<T>(sql: string, callback?: RowsCallback<T>): this;
|
|
82
|
+
all<T>(sql: string, params: any, callback?: RowsCallback<T>): this;
|
|
83
|
+
/**
|
|
84
|
+
* Runs the SQL query with the specified parameters and calls the callback once for each result row.
|
|
85
|
+
* The function returns the Database object to allow for function chaining. The parameters are the
|
|
86
|
+
* same as the Database#run function, with the following differences: The signature of the callback
|
|
87
|
+
* is function(err, row) {}. If the result set succeeds but is empty, the callback is never called.
|
|
88
|
+
* In all other cases, the callback is called once for every retrieved row. The order of calls correspond
|
|
89
|
+
* exactly to the order of rows in the result set. After all row callbacks were called, the completion
|
|
90
|
+
* callback will be called if present. The first argument is an error object, and the second argument
|
|
91
|
+
* is the number of retrieved rows. If you specify only one function, it will be treated as row callback,
|
|
92
|
+
* if you specify two, the first (== second to last) function will be the row callback, the last function
|
|
93
|
+
* will be the completion callback. If you know that a query only returns a very limited number of rows,
|
|
94
|
+
* it might be more convenient to use Database#all to retrieve all rows at once. There is currently no
|
|
95
|
+
* way to abort execution.
|
|
96
|
+
*/
|
|
97
|
+
each<T>(sql: string, callback?: RowCallback<T>, complete?: RowCountCallback): this;
|
|
98
|
+
each<T>(sql: string, params: any, callback?: RowCallback<T>, complete?: RowCountCallback): this;
|
|
99
|
+
/**
|
|
100
|
+
* Prepares the SQL statement and optionally binds the specified parameters and
|
|
101
|
+
* calls the callback when done. The function returns a Statement object.
|
|
102
|
+
* When preparing was successful, the first and only argument to the callback
|
|
103
|
+
* is null, otherwise it is the error object. When bind parameters are supplied,
|
|
104
|
+
* they are bound to the prepared statement before calling the callback.
|
|
105
|
+
*/
|
|
106
|
+
prepare<T = any>(sql: string, ...params: any[]): Statement<T>;
|
|
107
|
+
/**
|
|
108
|
+
* Runs all SQL queries in the supplied string. No result rows are retrieved.
|
|
109
|
+
* The function returns the Database object to allow for function chaining.
|
|
110
|
+
* If a query fails, no subsequent statements will be executed (wrap it in a
|
|
111
|
+
* transaction if you want all or none to be executed). When all statements
|
|
112
|
+
* have been executed successfully, or when an error occurs, the callback
|
|
113
|
+
* function is called, with the first parameter being either null or an error
|
|
114
|
+
* object. When no callback is provided and an error occurs, an error event
|
|
115
|
+
* will be emitted on the database object.
|
|
116
|
+
*/
|
|
117
|
+
exec(sql: string, callback?: ErrorCallback): this;
|
|
118
|
+
/**
|
|
119
|
+
* If the optional callback is provided, this function will be called when the
|
|
120
|
+
* database was closed successfully or when an error occurred. The first argument
|
|
121
|
+
* is an error object. When it is null, closing succeeded. If no callback is provided
|
|
122
|
+
* and an error occurred, an error event with the error object as the only parameter
|
|
123
|
+
* will be emitted on the database object. If closing succeeded, a close event with no
|
|
124
|
+
* parameters is emitted, regardless of whether a callback was provided or not.
|
|
125
|
+
*/
|
|
126
|
+
close(callback?: ErrorCallback): void;
|
|
127
|
+
/**
|
|
128
|
+
* Loads a compiled SQLite extension into the database connection object.
|
|
129
|
+
* @param path Filename of the extension to load.
|
|
130
|
+
* @param callback If provided, this function will be called when the extension
|
|
131
|
+
* was loaded successfully or when an error occurred. The first argument is an
|
|
132
|
+
* error object. When it is null, loading succeeded. If no callback is provided
|
|
133
|
+
* and an error occurred, an error event with the error object as the only parameter
|
|
134
|
+
* will be emitted on the database object.
|
|
135
|
+
*/
|
|
136
|
+
loadExtension(_path: string, callback?: ErrorCallback): this;
|
|
137
|
+
/**
|
|
138
|
+
* Allows the user to interrupt long-running queries. Wrapper around
|
|
139
|
+
* sqlite3_interrupt and causes other data-fetching functions to be
|
|
140
|
+
* passed an err with code = sqlite3.INTERRUPT. The database must be
|
|
141
|
+
* open to use this function.
|
|
142
|
+
*/
|
|
143
|
+
interrupt(): void;
|
|
144
|
+
/**
|
|
145
|
+
* Sql is a promise based API for executing SQL statements. You can
|
|
146
|
+
* pass a simple string with a SQL statement or a template string
|
|
147
|
+
* using backticks and parameters in ${parameter} format. These parameters
|
|
148
|
+
* will be properly escaped and quoted like when using a prepared statement.
|
|
149
|
+
* @param sql A sql string or a template string in `backticks` format
|
|
150
|
+
* @returns An array of rows in case of selections or an object with
|
|
151
|
+
* metadata in case of insert, update, delete.
|
|
152
|
+
*/
|
|
153
|
+
sql(sql: TemplateStringsArray | string, ...values: any[]): Promise<any>;
|
|
154
|
+
}
|