@ad-sdk/bgd 0.0.4 → 0.0.6
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/allocators/buffer.d.ts +12 -0
- package/allocators/buffer.js +22 -0
- package/package.json +1 -1
- package/sqlx/core.d.ts +25 -0
- package/sqlx/migrations.js +1 -1
- package/sqlx/postgres.d.ts +2 -1
- package/sqlx/postgres.js +55 -12
package/allocators/buffer.d.ts
CHANGED
|
@@ -101,6 +101,18 @@ export declare class MemoryBuffer<T extends ArrayBuffer | SharedArrayBuffer = Ar
|
|
|
101
101
|
* invalidate the disposal logic resulting in a memory leak.
|
|
102
102
|
*/
|
|
103
103
|
valueOf(secure: false): Buffer | null;
|
|
104
|
+
/**
|
|
105
|
+
* **ATTENTION:** If you use this function make sure that
|
|
106
|
+
* will not create an external reference of the buffer, or it'll
|
|
107
|
+
* invalidate the disposal logic resulting in a memory leak.
|
|
108
|
+
*/
|
|
109
|
+
$ref(secure?: true): T;
|
|
110
|
+
/**
|
|
111
|
+
* **ATTENTION:** If you use this function make sure that
|
|
112
|
+
* will not create an external reference of the buffer, or it'll
|
|
113
|
+
* invalidate the disposal logic resulting in a memory leak.
|
|
114
|
+
*/
|
|
115
|
+
$ref(secure: false): T | null;
|
|
104
116
|
/**
|
|
105
117
|
* Clear current buffer and free allocated memory.
|
|
106
118
|
*
|
package/allocators/buffer.js
CHANGED
|
@@ -360,6 +360,28 @@ class MemoryBuffer {
|
|
|
360
360
|
return _classPrivateFieldGet(_BufRef, this);
|
|
361
361
|
}
|
|
362
362
|
|
|
363
|
+
/**
|
|
364
|
+
* **ATTENTION:** If you use this function make sure that
|
|
365
|
+
* will not create an external reference of the buffer, or it'll
|
|
366
|
+
* invalidate the disposal logic resulting in a memory leak.
|
|
367
|
+
*/
|
|
368
|
+
|
|
369
|
+
/**
|
|
370
|
+
* **ATTENTION:** If you use this function make sure that
|
|
371
|
+
* will not create an external reference of the buffer, or it'll
|
|
372
|
+
* invalidate the disposal logic resulting in a memory leak.
|
|
373
|
+
*/
|
|
374
|
+
|
|
375
|
+
$ref(secure) {
|
|
376
|
+
if (_classPrivateFieldGet(_BufRef, this) == null) {
|
|
377
|
+
if (secure !== false) {
|
|
378
|
+
_assertClassBrand(_MemoryBuffer_brand, this, _CheckDisposed).call(this);
|
|
379
|
+
}
|
|
380
|
+
return null;
|
|
381
|
+
}
|
|
382
|
+
return _classPrivateFieldGet(_BufRef, this).buffer;
|
|
383
|
+
}
|
|
384
|
+
|
|
363
385
|
/**
|
|
364
386
|
* Clear current buffer and free allocated memory.
|
|
365
387
|
*
|
package/package.json
CHANGED
package/sqlx/core.d.ts
CHANGED
|
@@ -8,4 +8,29 @@
|
|
|
8
8
|
* @signphrase It was created on Earth by humans, although
|
|
9
9
|
* I can't define what a "human" is.
|
|
10
10
|
*/
|
|
11
|
+
import { ConnectionOptions } from "node:tls";
|
|
12
|
+
import { BGDException } from "../errors";
|
|
13
|
+
import { type Either } from "../lifecycle";
|
|
11
14
|
export declare const SQL_MIGRATION_FILE_PATTERN: RegExp;
|
|
15
|
+
export type ConnectionProps = string | {
|
|
16
|
+
get(): Promise<Either<unknown, string>> | string;
|
|
17
|
+
} | {
|
|
18
|
+
host: string;
|
|
19
|
+
port?: number;
|
|
20
|
+
username: string;
|
|
21
|
+
password: string | {
|
|
22
|
+
get(): Promise<Either<unknown, string>> | Either<unknown, string>;
|
|
23
|
+
};
|
|
24
|
+
database: string;
|
|
25
|
+
};
|
|
26
|
+
export interface DatabaseOptions {
|
|
27
|
+
ssl?: boolean | ConnectionOptions;
|
|
28
|
+
connectionTimeout?: number;
|
|
29
|
+
onError?: (err: BGDException) => unknown;
|
|
30
|
+
verbose?: boolean;
|
|
31
|
+
replication?: {
|
|
32
|
+
master: ConnectionProps;
|
|
33
|
+
slaves: readonly ConnectionProps[];
|
|
34
|
+
replicationMode?: "slave" | "master";
|
|
35
|
+
} | false;
|
|
36
|
+
}
|
package/sqlx/migrations.js
CHANGED
|
@@ -61,7 +61,7 @@ async function migrate(driver, directory, options) {
|
|
|
61
61
|
const res = await tx.execBatch([{
|
|
62
62
|
query: fc.trim()
|
|
63
63
|
}, {
|
|
64
|
-
query: `INSERT INTO
|
|
64
|
+
query: `INSERT INTO bgd_migrations (
|
|
65
65
|
migration_id, filename, sort_index
|
|
66
66
|
) VALUES ($1::TEXT, $2::TEXT, $3::INT)`,
|
|
67
67
|
values: [(0, _uidlib.uuidv7)(), contentsList[0].name, parseInt(contentsList[i].name.split("_")[0], 10)]
|
package/sqlx/postgres.d.ts
CHANGED
|
@@ -13,13 +13,14 @@ import type { Dict } from "../types";
|
|
|
13
13
|
import { BGDException } from "../errors";
|
|
14
14
|
import { IAsyncDisposable } from "../lifecycle/disposable";
|
|
15
15
|
import { type Either } from "../lifecycle/either";
|
|
16
|
+
import type { ConnectionProps, DatabaseOptions } from "./core";
|
|
16
17
|
import { Database, IAbstractQueryResult, ITransaction } from "./database";
|
|
17
18
|
export interface IPostgresResult<T = Dict<any>> extends IAbstractQueryResult<T> {
|
|
18
19
|
readonly dialect: "pgsql";
|
|
19
20
|
}
|
|
20
21
|
export declare class Postgres extends Database implements IAsyncDisposable {
|
|
21
22
|
#private;
|
|
22
|
-
constructor();
|
|
23
|
+
constructor(conn: ConnectionProps, opts?: DatabaseOptions);
|
|
23
24
|
get dialect(): "pgsql";
|
|
24
25
|
exec<T = Dict<unknown>>(query: string, values?: unknown[], options?: {
|
|
25
26
|
transaction?: PoolClient;
|
package/sqlx/postgres.js
CHANGED
|
@@ -4,6 +4,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.PostgresTransaction = exports.Postgres = void 0;
|
|
7
|
+
var _pg = require("pg");
|
|
7
8
|
var _errors = require("../errors");
|
|
8
9
|
var _either = require("../lifecycle/either");
|
|
9
10
|
var _database = require("./database");
|
|
@@ -25,14 +26,22 @@ function _assertClassBrand(e, t, n) { if ("function" == typeof e ? e === t : e.h
|
|
|
25
26
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
26
27
|
var _PoolI = /*#__PURE__*/new WeakMap();
|
|
27
28
|
var _Disposed = /*#__PURE__*/new WeakMap();
|
|
29
|
+
var _Options = /*#__PURE__*/new WeakMap();
|
|
30
|
+
var _Conn = /*#__PURE__*/new WeakMap();
|
|
28
31
|
var _Postgres_brand = /*#__PURE__*/new WeakSet();
|
|
29
32
|
class Postgres extends _database.Database {
|
|
30
|
-
constructor() {
|
|
33
|
+
constructor(conn, opts) {
|
|
31
34
|
super();
|
|
32
35
|
_classPrivateMethodInitSpec(this, _Postgres_brand);
|
|
33
36
|
_classPrivateFieldInitSpec(this, _PoolI, void 0);
|
|
34
37
|
_classPrivateFieldInitSpec(this, _Disposed, void 0);
|
|
38
|
+
_classPrivateFieldInitSpec(this, _Options, void 0);
|
|
39
|
+
_classPrivateFieldInitSpec(this, _Conn, void 0);
|
|
35
40
|
_classPrivateFieldSet(_Disposed, this, false);
|
|
41
|
+
_classPrivateFieldSet(_Options, this, opts ?? {});
|
|
42
|
+
delete _classPrivateFieldGet(_Options, this).replication; // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
43
|
+
|
|
44
|
+
_classPrivateFieldSet(_Conn, this, conn);
|
|
36
45
|
}
|
|
37
46
|
get dialect() {
|
|
38
47
|
return "pgsql";
|
|
@@ -83,14 +92,48 @@ class Postgres extends _database.Database {
|
|
|
83
92
|
}
|
|
84
93
|
async dispose() {
|
|
85
94
|
if (!_classPrivateFieldGet(_Disposed, this)) {
|
|
86
|
-
|
|
95
|
+
try {
|
|
96
|
+
await _classPrivateFieldGet(_PoolI, this)?.end();
|
|
97
|
+
} catch (err) {
|
|
98
|
+
console.error(err);
|
|
99
|
+
}
|
|
87
100
|
_classPrivateFieldSet(_Disposed, this, true);
|
|
88
101
|
}
|
|
89
102
|
}
|
|
90
103
|
}
|
|
91
104
|
exports.Postgres = Postgres;
|
|
92
|
-
function _GetConnection() {
|
|
93
|
-
|
|
105
|
+
async function _GetConnection() {
|
|
106
|
+
if (_classPrivateFieldGet(_PoolI, this) == null) {
|
|
107
|
+
if (!_classPrivateFieldGet(_Conn, this)) {
|
|
108
|
+
throw new _errors.BGDException("[Postgres] Missing connection info for database");
|
|
109
|
+
}
|
|
110
|
+
if (typeof _classPrivateFieldGet(_Conn, this) === "object" && "get" in _classPrivateFieldGet(_Options, this)) {
|
|
111
|
+
throw new _errors.BGDException("[Postgres] Use of secure getters aren't supported yet");
|
|
112
|
+
}
|
|
113
|
+
const config = {
|
|
114
|
+
ssl: _classPrivateFieldGet(_Options, this)?.ssl,
|
|
115
|
+
allowExitOnIdle: true,
|
|
116
|
+
idleTimeoutMillis: 30000,
|
|
117
|
+
connectionTimeoutMillis: _classPrivateFieldGet(_Options, this)?.connectionTimeout ?? 2000
|
|
118
|
+
};
|
|
119
|
+
if (typeof _classPrivateFieldGet(_Conn, this) === "string") {
|
|
120
|
+
config.connectionString = _classPrivateFieldGet(_Conn, this);
|
|
121
|
+
} else if (typeof _classPrivateFieldGet(_Conn, this) === "object" && "host" in _classPrivateFieldGet(_Conn, this)) {
|
|
122
|
+
if (typeof _classPrivateFieldGet(_Conn, this).password === "object") {
|
|
123
|
+
throw new _errors.BGDException("[Postgres] Use of secure getters aren't supported yet");
|
|
124
|
+
}
|
|
125
|
+
config.host = _classPrivateFieldGet(_Conn, this).host;
|
|
126
|
+
config.port = _classPrivateFieldGet(_Conn, this).port;
|
|
127
|
+
config.password = _classPrivateFieldGet(_Conn, this).password;
|
|
128
|
+
config.database = _classPrivateFieldGet(_Conn, this).database;
|
|
129
|
+
config.user = _classPrivateFieldGet(_Conn, this).username;
|
|
130
|
+
} else {
|
|
131
|
+
throw new _errors.BGDException("[Postgres] Something is wrong with connection info");
|
|
132
|
+
}
|
|
133
|
+
_classPrivateFieldSet(_Conn, this, null);
|
|
134
|
+
_classPrivateFieldSet(_PoolI, this, new _pg.Pool(config));
|
|
135
|
+
}
|
|
136
|
+
return await _classPrivateFieldGet(_PoolI, this).connect();
|
|
94
137
|
}
|
|
95
138
|
function _CheckDisposed() {
|
|
96
139
|
if (_classPrivateFieldGet(_Disposed, this)) {
|
|
@@ -99,7 +142,7 @@ function _CheckDisposed() {
|
|
|
99
142
|
}
|
|
100
143
|
var _Finished = /*#__PURE__*/new WeakMap();
|
|
101
144
|
var _Client = /*#__PURE__*/new WeakMap();
|
|
102
|
-
var
|
|
145
|
+
var _Options2 = /*#__PURE__*/new WeakMap();
|
|
103
146
|
class PostgresTransaction {
|
|
104
147
|
static async create(c, o) {
|
|
105
148
|
await c.exec("BEGIN");
|
|
@@ -108,10 +151,10 @@ class PostgresTransaction {
|
|
|
108
151
|
constructor(client, options) {
|
|
109
152
|
_classPrivateFieldInitSpec(this, _Finished, void 0);
|
|
110
153
|
_classPrivateFieldInitSpec(this, _Client, void 0);
|
|
111
|
-
_classPrivateFieldInitSpec(this,
|
|
154
|
+
_classPrivateFieldInitSpec(this, _Options2, void 0);
|
|
112
155
|
_classPrivateFieldSet(_Finished, this, false);
|
|
113
156
|
_classPrivateFieldSet(_Client, this, client);
|
|
114
|
-
_classPrivateFieldSet(
|
|
157
|
+
_classPrivateFieldSet(_Options2, this, options ?? {});
|
|
115
158
|
}
|
|
116
159
|
async commit() {
|
|
117
160
|
if (_classPrivateFieldGet(_Finished, this)) return;
|
|
@@ -131,12 +174,12 @@ class PostgresTransaction {
|
|
|
131
174
|
}
|
|
132
175
|
const result = await _classPrivateFieldGet(_Client, this).exec(text, values);
|
|
133
176
|
if (result.isRight()) {
|
|
134
|
-
if (_classPrivateFieldGet(
|
|
177
|
+
if (_classPrivateFieldGet(_Options2, this)?.autoCommit) {
|
|
135
178
|
await this.commit();
|
|
136
179
|
}
|
|
137
180
|
return result;
|
|
138
181
|
}
|
|
139
|
-
if (_classPrivateFieldGet(
|
|
182
|
+
if (_classPrivateFieldGet(_Options2, this)?.autoCommit) {
|
|
140
183
|
await this.rollback();
|
|
141
184
|
}
|
|
142
185
|
return result;
|
|
@@ -145,7 +188,7 @@ class PostgresTransaction {
|
|
|
145
188
|
if (_classPrivateFieldGet(_Finished, this)) {
|
|
146
189
|
throw new _errors.BGDException("[PostgresTransaction] Current transaction is already closed");
|
|
147
190
|
}
|
|
148
|
-
if (!_classPrivateFieldGet(
|
|
191
|
+
if (!_classPrivateFieldGet(_Options2, this)?.allowQueryBatch) {
|
|
149
192
|
throw new _errors.BGDException("[PostgresTransaction] Current transaction is not allowed to execute batch queries");
|
|
150
193
|
}
|
|
151
194
|
const oe = options?.onError ?? "break";
|
|
@@ -159,12 +202,12 @@ class PostgresTransaction {
|
|
|
159
202
|
}
|
|
160
203
|
result.push(res);
|
|
161
204
|
}
|
|
162
|
-
if (_classPrivateFieldGet(
|
|
205
|
+
if (_classPrivateFieldGet(_Options2, this)?.autoCommit !== true) return result;
|
|
163
206
|
const success = result.every(r => r.isRight());
|
|
164
207
|
await this[success ? "commit" : "rollback"]();
|
|
165
208
|
return result;
|
|
166
209
|
} catch (err) {
|
|
167
|
-
if (_classPrivateFieldGet(
|
|
210
|
+
if (_classPrivateFieldGet(_Options2, this).autoCommit) {
|
|
168
211
|
await this.rollback();
|
|
169
212
|
}
|
|
170
213
|
throw err;
|