@ad-sdk/bgd 0.0.1
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 +115 -0
- package/allocators/buffer.js +409 -0
- package/allocators/collection.d.ts +24 -0
- package/allocators/collection.js +91 -0
- package/allocators/index.d.ts +3 -0
- package/allocators/index.js +38 -0
- package/allocators/virtual-core.d.ts +13 -0
- package/allocators/virtual-core.js +24 -0
- package/const.d.ts +19 -0
- package/const.js +26 -0
- package/errors.d.ts +32 -0
- package/errors.js +57 -0
- package/graph/def.d.ts +17 -0
- package/graph/def.js +5 -0
- package/graph/nodes/index.d.ts +1 -0
- package/graph/nodes/index.js +16 -0
- package/graph/nodes/person.d.ts +48 -0
- package/graph/nodes/person.js +104 -0
- package/index.d.ts +5 -0
- package/index.js +60 -0
- package/lifecycle/debugger.d.ts +21 -0
- package/lifecycle/debugger.js +5 -0
- package/lifecycle/disposable.d.ts +25 -0
- package/lifecycle/disposable.js +66 -0
- package/lifecycle/either.d.ts +25 -0
- package/lifecycle/either.js +51 -0
- package/lifecycle/index.d.ts +2 -0
- package/lifecycle/index.js +18 -0
- package/mathlib/core.d.ts +17 -0
- package/mathlib/core.js +29 -0
- package/mathlib/index.d.ts +1 -0
- package/mathlib/index.js +16 -0
- package/package.json +17 -0
- package/sqlx/core.d.ts +11 -0
- package/sqlx/core.js +18 -0
- package/sqlx/database.d.ts +45 -0
- package/sqlx/database.js +25 -0
- package/sqlx/migrations.d.ts +21 -0
- package/sqlx/migrations.js +115 -0
- package/sqlx/postgres.d.ts +51 -0
- package/sqlx/postgres.js +174 -0
- package/sss/poly.d.ts +19 -0
- package/sss/poly.js +76 -0
- package/sss/strategy.d.ts +14 -0
- package/sss/strategy.js +72 -0
- package/structured/heap.d.ts +22 -0
- package/structured/heap.js +46 -0
- package/types.d.ts +17 -0
- package/util/index.d.ts +2 -0
- package/util/index.js +27 -0
- package/util/runtime.d.ts +14 -0
- package/util/runtime.js +78 -0
- package/util/validator.d.ts +21 -0
- package/util/validator.js +75 -0
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
Object.defineProperty(exports, "IAsyncDisposable", {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: function () {
|
|
9
|
+
return _disposable.IAsyncDisposable;
|
|
10
|
+
}
|
|
11
|
+
});
|
|
12
|
+
Object.defineProperty(exports, "IDisposable", {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
get: function () {
|
|
15
|
+
return _disposable.IDisposable;
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
var _disposable = require("./disposable");
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright © 2026 Alerta Dino. All rights reserved.
|
|
3
|
+
*
|
|
4
|
+
* This code was released under the BSD 3-Clause License.
|
|
5
|
+
* See the "LICENSE" file under project root.
|
|
6
|
+
*
|
|
7
|
+
* @author (PUBLIC_ID) 1D-C2-9B-98-D6-C3-D6-AB
|
|
8
|
+
* @signphrase It was created on Earth by humans, although
|
|
9
|
+
* I can't define what a "human" is.
|
|
10
|
+
*/
|
|
11
|
+
export declare namespace MathLib {
|
|
12
|
+
type ZipResult<T extends unknown[][]> = {
|
|
13
|
+
[K in keyof T]: T[K] extends (infer U)[] ? U : never;
|
|
14
|
+
}[];
|
|
15
|
+
export function zip<T extends unknown[][]>(...args: T): ZipResult<T>;
|
|
16
|
+
export {};
|
|
17
|
+
}
|
package/mathlib/core.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.MathLib = void 0;
|
|
7
|
+
/**
|
|
8
|
+
* Copyright © 2026 Alerta Dino. All rights reserved.
|
|
9
|
+
*
|
|
10
|
+
* This code was released under the BSD 3-Clause License.
|
|
11
|
+
* See the "LICENSE" file under project root.
|
|
12
|
+
*
|
|
13
|
+
* @author (PUBLIC_ID) 1D-C2-9B-98-D6-C3-D6-AB
|
|
14
|
+
* @signphrase It was created on Earth by humans, although
|
|
15
|
+
* I can't define what a "human" is.
|
|
16
|
+
*/
|
|
17
|
+
/* eslint-disable @typescript-eslint/no-namespace, no-inner-declarations */
|
|
18
|
+
let MathLib = exports.MathLib = void 0;
|
|
19
|
+
(function (_MathLib) {
|
|
20
|
+
function zip(...args) {
|
|
21
|
+
const minLen = Math.min(...args.map(arr => arr.length));
|
|
22
|
+
const result = new Array(minLen);
|
|
23
|
+
for (let i = 0; i < minLen; ++i) {
|
|
24
|
+
result[i] = args.map(arr => arr[i]);
|
|
25
|
+
}
|
|
26
|
+
return result;
|
|
27
|
+
}
|
|
28
|
+
_MathLib.zip = zip;
|
|
29
|
+
})(MathLib || (exports.MathLib = MathLib = {}));
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./core";
|
package/mathlib/index.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
var _core = require("./core");
|
|
7
|
+
Object.keys(_core).forEach(function (key) {
|
|
8
|
+
if (key === "default" || key === "__esModule") return;
|
|
9
|
+
if (key in exports && exports[key] === _core[key]) return;
|
|
10
|
+
Object.defineProperty(exports, key, {
|
|
11
|
+
enumerable: true,
|
|
12
|
+
get: function () {
|
|
13
|
+
return _core[key];
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ad-sdk/bgd",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"main": "dist/index.js",
|
|
5
|
+
"license": "BSD-3-Clause",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/alertadino/bgd.git"
|
|
9
|
+
},
|
|
10
|
+
"engines": {
|
|
11
|
+
"node": ">=19"
|
|
12
|
+
},
|
|
13
|
+
"dependencies": {
|
|
14
|
+
"pg": "^8.16.3",
|
|
15
|
+
"uidlib": "^0.0.1"
|
|
16
|
+
}
|
|
17
|
+
}
|
package/sqlx/core.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright © 2026 Alerta Dino. All rights reserved.
|
|
3
|
+
*
|
|
4
|
+
* This code was released under the BSD 3-Clause License.
|
|
5
|
+
* See the "LICENSE" file under project root.
|
|
6
|
+
*
|
|
7
|
+
* @author (PUBLIC_ID) 1D-C2-9B-98-D6-C3-D6-AB
|
|
8
|
+
* @signphrase It was created on Earth by humans, although
|
|
9
|
+
* I can't define what a "human" is.
|
|
10
|
+
*/
|
|
11
|
+
export declare const SQL_MIGRATION_FILE_PATTERN: RegExp;
|
package/sqlx/core.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.SQL_MIGRATION_FILE_PATTERN = void 0;
|
|
7
|
+
/**
|
|
8
|
+
* Copyright © 2026 Alerta Dino. All rights reserved.
|
|
9
|
+
*
|
|
10
|
+
* This code was released under the BSD 3-Clause License.
|
|
11
|
+
* See the "LICENSE" file under project root.
|
|
12
|
+
*
|
|
13
|
+
* @author (PUBLIC_ID) 1D-C2-9B-98-D6-C3-D6-AB
|
|
14
|
+
* @signphrase It was created on Earth by humans, although
|
|
15
|
+
* I can't define what a "human" is.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
const SQL_MIGRATION_FILE_PATTERN = exports.SQL_MIGRATION_FILE_PATTERN = /^(\d+)_(.*)\.migration\.sql$/;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright © 2026 Alerta Dino. All rights reserved.
|
|
3
|
+
*
|
|
4
|
+
* This code was released under the BSD 3-Clause License.
|
|
5
|
+
* See the "LICENSE" file under project root.
|
|
6
|
+
*
|
|
7
|
+
* @author (PUBLIC_ID) 1D-C2-9B-98-D6-C3-D6-AB
|
|
8
|
+
* @signphrase It was created on Earth by humans, although
|
|
9
|
+
* I can't define what a "human" is.
|
|
10
|
+
*/
|
|
11
|
+
import { BGDException } from "../errors";
|
|
12
|
+
import { type Either } from "../lifecycle";
|
|
13
|
+
import type { Dict, MaybePromise } from "../types";
|
|
14
|
+
export interface IAbstractQueryResult<T = Dict<any>> {
|
|
15
|
+
readonly dialect: string;
|
|
16
|
+
readonly command: string;
|
|
17
|
+
readonly queryText?: string;
|
|
18
|
+
readonly rowCount: number | null;
|
|
19
|
+
readonly rows: T[];
|
|
20
|
+
readonly oid?: number;
|
|
21
|
+
}
|
|
22
|
+
export interface ITransaction {
|
|
23
|
+
commit(): MaybePromise<void>;
|
|
24
|
+
rollback(): MaybePromise<void>;
|
|
25
|
+
exec<TResult>(text: string, values?: unknown[]): MaybePromise<Either<BGDException, IAbstractQueryResult<TResult>>>;
|
|
26
|
+
execBatch(queries: readonly {
|
|
27
|
+
query: string;
|
|
28
|
+
values?: unknown[];
|
|
29
|
+
}[], options?: {
|
|
30
|
+
onError?: "continue" | "break";
|
|
31
|
+
}): MaybePromise<readonly Either<BGDException, IAbstractQueryResult>[]>;
|
|
32
|
+
}
|
|
33
|
+
export declare abstract class Database {
|
|
34
|
+
abstract readonly dialect: string;
|
|
35
|
+
get type(): "sql";
|
|
36
|
+
abstract exec<T = Dict<unknown>>(query: string, values?: unknown[], options?: {
|
|
37
|
+
transaction?: unknown;
|
|
38
|
+
}): MaybePromise<Either<BGDException, IAbstractQueryResult<T>>>;
|
|
39
|
+
abstract exec<T = Dict<unknown>>(query: string, options?: {
|
|
40
|
+
transaction?: unknown;
|
|
41
|
+
values?: unknown[];
|
|
42
|
+
}): MaybePromise<Either<BGDException, IAbstractQueryResult<T>>>;
|
|
43
|
+
abstract createTransaction(o?: Dict<unknown>): MaybePromise<ITransaction>;
|
|
44
|
+
abstract dispose(): Promise<void> | void;
|
|
45
|
+
}
|
package/sqlx/database.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.Database = void 0;
|
|
7
|
+
/**
|
|
8
|
+
* Copyright © 2026 Alerta Dino. All rights reserved.
|
|
9
|
+
*
|
|
10
|
+
* This code was released under the BSD 3-Clause License.
|
|
11
|
+
* See the "LICENSE" file under project root.
|
|
12
|
+
*
|
|
13
|
+
* @author (PUBLIC_ID) 1D-C2-9B-98-D6-C3-D6-AB
|
|
14
|
+
* @signphrase It was created on Earth by humans, although
|
|
15
|
+
* I can't define what a "human" is.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
19
|
+
|
|
20
|
+
class Database {
|
|
21
|
+
get type() {
|
|
22
|
+
return "sql";
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
exports.Database = Database;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright © 2026 Alerta Dino. All rights reserved.
|
|
3
|
+
*
|
|
4
|
+
* This code was released under the BSD 3-Clause License.
|
|
5
|
+
* See the "LICENSE" file under project root.
|
|
6
|
+
*
|
|
7
|
+
* @author (PUBLIC_ID) 1D-C2-9B-98-D6-C3-D6-AB
|
|
8
|
+
* @signphrase It was created on Earth by humans, although
|
|
9
|
+
* I can't define what a "human" is.
|
|
10
|
+
*/
|
|
11
|
+
import * as fs from "node:fs";
|
|
12
|
+
import { Database } from "./database";
|
|
13
|
+
import { BGDException } from "../errors";
|
|
14
|
+
import { type ILogger } from "../lifecycle/debugger";
|
|
15
|
+
import { type Either } from "../lifecycle/either";
|
|
16
|
+
export interface IMigrateOptions {
|
|
17
|
+
recursive?: boolean;
|
|
18
|
+
ensure?: boolean;
|
|
19
|
+
logger?: ILogger;
|
|
20
|
+
}
|
|
21
|
+
export declare function migrate(driver: Database, directory: fs.PathLike, options?: IMigrateOptions): Promise<Either<BGDException, void>>;
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.migrate = migrate;
|
|
7
|
+
var _nodePath = _interopRequireDefault(require("node:path"));
|
|
8
|
+
var fs = _interopRequireWildcard(require("node:fs"));
|
|
9
|
+
var _uidlib = require("uidlib");
|
|
10
|
+
var _errors = require("../errors");
|
|
11
|
+
var _core = require("./core");
|
|
12
|
+
var _either = require("../lifecycle/either");
|
|
13
|
+
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
|
|
14
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
15
|
+
/**
|
|
16
|
+
* Copyright © 2026 Alerta Dino. All rights reserved.
|
|
17
|
+
*
|
|
18
|
+
* This code was released under the BSD 3-Clause License.
|
|
19
|
+
* See the "LICENSE" file under project root.
|
|
20
|
+
*
|
|
21
|
+
* @author (PUBLIC_ID) 1D-C2-9B-98-D6-C3-D6-AB
|
|
22
|
+
* @signphrase It was created on Earth by humans, although
|
|
23
|
+
* I can't define what a "human" is.
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
async function migrate(driver, directory, options) {
|
|
27
|
+
try {
|
|
28
|
+
if (!fs.existsSync(directory)) {
|
|
29
|
+
if (options?.ensure) {
|
|
30
|
+
throw new _errors.BGDException("[@@migrate] Migrations directory not found", "ER_IO_FAIL");
|
|
31
|
+
}
|
|
32
|
+
options?.logger?.log("Migrations directory not found at '%s'", directory.toString());
|
|
33
|
+
return (0, _either.right)(void 0);
|
|
34
|
+
}
|
|
35
|
+
const contentsList = await fs.promises.readdir(directory, {
|
|
36
|
+
withFileTypes: true
|
|
37
|
+
});
|
|
38
|
+
const executed = await listExecuted_(driver);
|
|
39
|
+
const tx = await driver.createTransaction({
|
|
40
|
+
allowQueryBatch: true
|
|
41
|
+
});
|
|
42
|
+
for (let i = 0; i < contentsList.length; ++i) {
|
|
43
|
+
if (!_core.SQL_MIGRATION_FILE_PATTERN.test(contentsList[i].name)) continue;
|
|
44
|
+
const currPath = _nodePath.default.join(contentsList[i].parentPath, contentsList[i].name // eslint-disable-line comma-dangle
|
|
45
|
+
);
|
|
46
|
+
if (contentsList[i].isDirectory() && options?.recursive) {
|
|
47
|
+
const res = await migrate(driver, currPath, options);
|
|
48
|
+
if (res.isLeft()) {
|
|
49
|
+
throw res.value;
|
|
50
|
+
}
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
if (!contentsList[i].isFile()) continue;
|
|
54
|
+
if (executed.some(item => {
|
|
55
|
+
return item.filename === contentsList[i].name;
|
|
56
|
+
})) {
|
|
57
|
+
options?.logger?.log("[@@migrate] Migration \"%s\" has already executed. Skipping...", contentsList[i].name);
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
const fc = await fs.promises.readFile(currPath, "utf8");
|
|
61
|
+
const res = await tx.execBatch([{
|
|
62
|
+
query: fc.trim()
|
|
63
|
+
}, {
|
|
64
|
+
query: `INSERT INTO ad_bgd_migrations (
|
|
65
|
+
migration_id, filename, sort_index
|
|
66
|
+
) VALUES ($1::TEXT, $2::TEXT, $3::INT)`,
|
|
67
|
+
values: [(0, _uidlib.uuidv7)(), contentsList[0].name, parseInt(contentsList[i].name.split("_")[0], 10)]
|
|
68
|
+
}]);
|
|
69
|
+
for (let j = 0; j < res.length; ++j) {
|
|
70
|
+
if (res[j].isLeft()) {
|
|
71
|
+
await tx.rollback();
|
|
72
|
+
throw res[j].value;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
await tx.commit();
|
|
77
|
+
return (0, _either.right)(void 0);
|
|
78
|
+
} catch (err) {
|
|
79
|
+
let e = err;
|
|
80
|
+
if (!(err instanceof _errors.BGDException)) {
|
|
81
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
82
|
+
e = new _errors.BGDException(`[@@migrate] Failed to execute SQL migration due to: ${err?.message || String(err) || "Unknown error"}`);
|
|
83
|
+
}
|
|
84
|
+
return (0, _either.left)(e);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
const PGCT_QUERY = `CREATE TABLE IF NOT EXISTS ad_bgd_migrations (
|
|
88
|
+
migration_id VARCHAR(100) NOT NULL UNIQUE PRIMARY KEY,
|
|
89
|
+
sequence SERIAL NOT NULL,
|
|
90
|
+
filename VARCHAR(255) NOT NULL UNIQUE,
|
|
91
|
+
sort_index INT NOT NULL,
|
|
92
|
+
executed_at TIMESTAMP WITH TIME ZONE NOT NULL
|
|
93
|
+
DEFAULT (NOW() AT TIME ZONE 'UTC')
|
|
94
|
+
)`;
|
|
95
|
+
async function ensureTable_(driver) {
|
|
96
|
+
let text = "";
|
|
97
|
+
switch (driver.dialect) {
|
|
98
|
+
case "pgsql":
|
|
99
|
+
{
|
|
100
|
+
text = PGCT_QUERY;
|
|
101
|
+
}
|
|
102
|
+
break;
|
|
103
|
+
default:
|
|
104
|
+
throw new _errors.BGDException(`[@@ensureTable_] Unknown or unsupported database dialect "${driver.dialect}"`);
|
|
105
|
+
}
|
|
106
|
+
await driver.exec(text);
|
|
107
|
+
}
|
|
108
|
+
async function listExecuted_(driver) {
|
|
109
|
+
await ensureTable_(driver);
|
|
110
|
+
const res = await driver.exec("SELECT * FROM migrations");
|
|
111
|
+
if (res.isLeft()) {
|
|
112
|
+
throw res.value;
|
|
113
|
+
}
|
|
114
|
+
return res.value.rows;
|
|
115
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright © 2026 Alerta Dino. All rights reserved.
|
|
3
|
+
*
|
|
4
|
+
* This code was released under the BSD 3-Clause License.
|
|
5
|
+
* See the "LICENSE" file under project root.
|
|
6
|
+
*
|
|
7
|
+
* @author (PUBLIC_ID) 1D-C2-9B-98-D6-C3-D6-AB
|
|
8
|
+
* @signphrase It was created on Earth by humans, although
|
|
9
|
+
* I can't define what a "human" is.
|
|
10
|
+
*/
|
|
11
|
+
import { type PoolClient } from "pg";
|
|
12
|
+
import type { Dict } from "../types";
|
|
13
|
+
import { BGDException } from "../errors";
|
|
14
|
+
import { IAsyncDisposable } from "../lifecycle/disposable";
|
|
15
|
+
import { type Either } from "../lifecycle/either";
|
|
16
|
+
import { Database, IAbstractQueryResult, ITransaction } from "./database";
|
|
17
|
+
export interface IPostgresResult<T = Dict<any>> extends IAbstractQueryResult<T> {
|
|
18
|
+
readonly dialect: "pgsql";
|
|
19
|
+
}
|
|
20
|
+
export declare class Postgres extends Database implements IAsyncDisposable {
|
|
21
|
+
#private;
|
|
22
|
+
constructor();
|
|
23
|
+
get dialect(): "pgsql";
|
|
24
|
+
exec<T = Dict<unknown>>(query: string, values?: unknown[], options?: {
|
|
25
|
+
transaction?: PoolClient;
|
|
26
|
+
}): Promise<Either<BGDException, IPostgresResult<T>>>;
|
|
27
|
+
exec<T = Dict<unknown>>(query: string, options?: {
|
|
28
|
+
transaction?: PoolClient;
|
|
29
|
+
values?: unknown[];
|
|
30
|
+
}): Promise<Either<BGDException, IPostgresResult<T>>>;
|
|
31
|
+
createTransaction(o?: TransactionOptions): Promise<PostgresTransaction>;
|
|
32
|
+
dispose(): Promise<void>;
|
|
33
|
+
}
|
|
34
|
+
export type TransactionOptions = {
|
|
35
|
+
autoCommit?: boolean;
|
|
36
|
+
allowQueryBatch?: boolean;
|
|
37
|
+
};
|
|
38
|
+
export declare class PostgresTransaction implements ITransaction {
|
|
39
|
+
#private;
|
|
40
|
+
static create(c: Postgres, o?: TransactionOptions): Promise<PostgresTransaction>;
|
|
41
|
+
private constructor();
|
|
42
|
+
commit(): Promise<void>;
|
|
43
|
+
rollback(): Promise<void>;
|
|
44
|
+
exec<TResult = Dict<any>>(text: string, values?: unknown[]): Promise<Either<BGDException, IPostgresResult<TResult>>>;
|
|
45
|
+
execBatch(queries: readonly {
|
|
46
|
+
query: string;
|
|
47
|
+
values?: unknown[];
|
|
48
|
+
}[], options?: {
|
|
49
|
+
onError?: "continue" | "break";
|
|
50
|
+
}): Promise<readonly Either<BGDException, IPostgresResult>[]>;
|
|
51
|
+
}
|
package/sqlx/postgres.js
ADDED
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.PostgresTransaction = exports.Postgres = void 0;
|
|
7
|
+
var _errors = require("../errors");
|
|
8
|
+
var _either = require("../lifecycle/either");
|
|
9
|
+
var _database = require("./database");
|
|
10
|
+
function _classPrivateMethodInitSpec(e, a) { _checkPrivateRedeclaration(e, a), a.add(e); }
|
|
11
|
+
function _classPrivateFieldInitSpec(e, t, a) { _checkPrivateRedeclaration(e, t), t.set(e, a); }
|
|
12
|
+
function _checkPrivateRedeclaration(e, t) { if (t.has(e)) throw new TypeError("Cannot initialize the same private elements twice on an object"); }
|
|
13
|
+
function _classPrivateFieldGet(s, a) { return s.get(_assertClassBrand(s, a)); }
|
|
14
|
+
function _classPrivateFieldSet(s, a, r) { return s.set(_assertClassBrand(s, a), r), r; }
|
|
15
|
+
function _assertClassBrand(e, t, n) { if ("function" == typeof e ? e === t : e.has(t)) return arguments.length < 3 ? t : n; throw new TypeError("Private element is not present on this object"); } /**
|
|
16
|
+
* Copyright © 2026 Alerta Dino. All rights reserved.
|
|
17
|
+
*
|
|
18
|
+
* This code was released under the BSD 3-Clause License.
|
|
19
|
+
* See the "LICENSE" file under project root.
|
|
20
|
+
*
|
|
21
|
+
* @author (PUBLIC_ID) 1D-C2-9B-98-D6-C3-D6-AB
|
|
22
|
+
* @signphrase It was created on Earth by humans, although
|
|
23
|
+
* I can't define what a "human" is.
|
|
24
|
+
*/
|
|
25
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
26
|
+
var _PoolI = /*#__PURE__*/new WeakMap();
|
|
27
|
+
var _Disposed = /*#__PURE__*/new WeakMap();
|
|
28
|
+
var _Postgres_brand = /*#__PURE__*/new WeakSet();
|
|
29
|
+
class Postgres extends _database.Database {
|
|
30
|
+
constructor() {
|
|
31
|
+
super();
|
|
32
|
+
_classPrivateMethodInitSpec(this, _Postgres_brand);
|
|
33
|
+
_classPrivateFieldInitSpec(this, _PoolI, void 0);
|
|
34
|
+
_classPrivateFieldInitSpec(this, _Disposed, void 0);
|
|
35
|
+
_classPrivateFieldSet(_Disposed, this, false);
|
|
36
|
+
}
|
|
37
|
+
get dialect() {
|
|
38
|
+
return "pgsql";
|
|
39
|
+
}
|
|
40
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
41
|
+
async exec(text, valuesOrOptions, options) {
|
|
42
|
+
let values = void 0;
|
|
43
|
+
if (Array.isArray(valuesOrOptions)) {
|
|
44
|
+
values = valuesOrOptions;
|
|
45
|
+
} else {
|
|
46
|
+
options = {
|
|
47
|
+
transaction: valuesOrOptions?.transaction
|
|
48
|
+
};
|
|
49
|
+
values = valuesOrOptions?.values;
|
|
50
|
+
valuesOrOptions = null;
|
|
51
|
+
}
|
|
52
|
+
let client = null;
|
|
53
|
+
try {
|
|
54
|
+
client = options?.transaction ?? (await _assertClassBrand(_Postgres_brand, this, _GetConnection).call(this));
|
|
55
|
+
const result = await client.query({
|
|
56
|
+
text,
|
|
57
|
+
values
|
|
58
|
+
});
|
|
59
|
+
return (0, _either.right)({
|
|
60
|
+
dialect: "pgsql",
|
|
61
|
+
command: result.command,
|
|
62
|
+
rowCount: result.rowCount,
|
|
63
|
+
rows: result.rows,
|
|
64
|
+
oid: result.oid,
|
|
65
|
+
queryText: text
|
|
66
|
+
});
|
|
67
|
+
} catch (err) {
|
|
68
|
+
let e = err;
|
|
69
|
+
if (!(err instanceof _errors.BGDException)) {
|
|
70
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
71
|
+
e = new _errors.BGDException(`[Postgres] Query execution was failed due to: ${err?.message || String(err) || "Unknown error"}`);
|
|
72
|
+
}
|
|
73
|
+
return (0, _either.left)(e);
|
|
74
|
+
} finally {
|
|
75
|
+
if (client != null && !options?.transaction) {
|
|
76
|
+
client.release();
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
createTransaction(o) {
|
|
81
|
+
_assertClassBrand(_Postgres_brand, this, _CheckDisposed).call(this);
|
|
82
|
+
return PostgresTransaction.create(this, o);
|
|
83
|
+
}
|
|
84
|
+
async dispose() {
|
|
85
|
+
if (!_classPrivateFieldGet(_Disposed, this)) {
|
|
86
|
+
await _classPrivateFieldGet(_PoolI, this)?.end();
|
|
87
|
+
_classPrivateFieldSet(_Disposed, this, true);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
exports.Postgres = Postgres;
|
|
92
|
+
function _GetConnection() {
|
|
93
|
+
throw _assertClassBrand(_Postgres_brand, this, _CheckDisposed);
|
|
94
|
+
}
|
|
95
|
+
function _CheckDisposed() {
|
|
96
|
+
if (_classPrivateFieldGet(_Disposed, this)) {
|
|
97
|
+
throw new _errors.BGDException("[Postgres] Database has already disposed", "ER_RESOURCE_DISPOSED");
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
var _Finished = /*#__PURE__*/new WeakMap();
|
|
101
|
+
var _Client = /*#__PURE__*/new WeakMap();
|
|
102
|
+
var _Options = /*#__PURE__*/new WeakMap();
|
|
103
|
+
class PostgresTransaction {
|
|
104
|
+
static async create(c, o) {
|
|
105
|
+
await c.exec("BEGIN");
|
|
106
|
+
return new PostgresTransaction(c, o);
|
|
107
|
+
}
|
|
108
|
+
constructor(client, options) {
|
|
109
|
+
_classPrivateFieldInitSpec(this, _Finished, void 0);
|
|
110
|
+
_classPrivateFieldInitSpec(this, _Client, void 0);
|
|
111
|
+
_classPrivateFieldInitSpec(this, _Options, void 0);
|
|
112
|
+
_classPrivateFieldSet(_Finished, this, false);
|
|
113
|
+
_classPrivateFieldSet(_Client, this, client);
|
|
114
|
+
_classPrivateFieldSet(_Options, this, options ?? {});
|
|
115
|
+
}
|
|
116
|
+
async commit() {
|
|
117
|
+
if (_classPrivateFieldGet(_Finished, this)) return;
|
|
118
|
+
await _classPrivateFieldGet(_Client, this).exec("COMMIT");
|
|
119
|
+
_classPrivateFieldSet(_Finished, this, true);
|
|
120
|
+
}
|
|
121
|
+
async rollback() {
|
|
122
|
+
if (_classPrivateFieldGet(_Finished, this)) return;
|
|
123
|
+
await _classPrivateFieldGet(_Client, this).exec("ROLLBACK");
|
|
124
|
+
_classPrivateFieldSet(_Finished, this, true);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
128
|
+
async exec(text, values) {
|
|
129
|
+
if (_classPrivateFieldGet(_Finished, this)) {
|
|
130
|
+
throw new _errors.BGDException("[PostgresTransaction] Current transaction is already closed");
|
|
131
|
+
}
|
|
132
|
+
const result = await _classPrivateFieldGet(_Client, this).exec(text, values);
|
|
133
|
+
if (result.isRight()) {
|
|
134
|
+
if (_classPrivateFieldGet(_Options, this)?.autoCommit) {
|
|
135
|
+
await this.commit();
|
|
136
|
+
}
|
|
137
|
+
return result;
|
|
138
|
+
}
|
|
139
|
+
if (_classPrivateFieldGet(_Options, this)?.autoCommit) {
|
|
140
|
+
await this.rollback();
|
|
141
|
+
}
|
|
142
|
+
return result;
|
|
143
|
+
}
|
|
144
|
+
async execBatch(queries, options) {
|
|
145
|
+
if (_classPrivateFieldGet(_Finished, this)) {
|
|
146
|
+
throw new _errors.BGDException("[PostgresTransaction] Current transaction is already closed");
|
|
147
|
+
}
|
|
148
|
+
if (!_classPrivateFieldGet(_Options, this)?.allowQueryBatch) {
|
|
149
|
+
throw new _errors.BGDException("[PostgresTransaction] Current transaction is not allowed to execute batch queries");
|
|
150
|
+
}
|
|
151
|
+
const oe = options?.onError ?? "break";
|
|
152
|
+
try {
|
|
153
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
154
|
+
const result = [];
|
|
155
|
+
for (let i = 0; i < queries.length; ++i) {
|
|
156
|
+
const res = await _classPrivateFieldGet(_Client, this).exec(queries[i].query, queries[i].values);
|
|
157
|
+
if (res.isLeft() && oe !== "continue") {
|
|
158
|
+
throw res.value;
|
|
159
|
+
}
|
|
160
|
+
result.push(res);
|
|
161
|
+
}
|
|
162
|
+
if (_classPrivateFieldGet(_Options, this)?.autoCommit !== true) return result;
|
|
163
|
+
const success = result.every(r => r.isRight());
|
|
164
|
+
await this[success ? "commit" : "rollback"]();
|
|
165
|
+
return result;
|
|
166
|
+
} catch (err) {
|
|
167
|
+
if (_classPrivateFieldGet(_Options, this).autoCommit) {
|
|
168
|
+
await this.rollback();
|
|
169
|
+
}
|
|
170
|
+
throw err;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
exports.PostgresTransaction = PostgresTransaction;
|
package/sss/poly.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Halfmoon Labs. All rights reserved.
|
|
3
|
+
*
|
|
4
|
+
* Modified in 2025 by Alerta Dino's IT Team.
|
|
5
|
+
*
|
|
6
|
+
* This code was released under the BSD 3-Clause License.
|
|
7
|
+
* See the "LICENSE" file under project root.
|
|
8
|
+
*
|
|
9
|
+
* @see https://github.com/shea256/secret-sharing/blob/master/secretsharing/polynomials.py
|
|
10
|
+
*
|
|
11
|
+
* @author (PUBLIC_ID) 1D-C2-9B-98-D6-C3-D6-AB
|
|
12
|
+
* @signphrase It was created on Earth by humans, although
|
|
13
|
+
* I can't define what a "human" is.
|
|
14
|
+
*/
|
|
15
|
+
export declare function egcd(a: number, b: number): readonly [number, number, number];
|
|
16
|
+
export declare function modInv(k: number, prime: number): number;
|
|
17
|
+
export declare function randomPolynomial(degree: number, intercept: number, upperBound: number): number[];
|
|
18
|
+
export declare function getPolynomialPoints(c: readonly number[], points: number, prime: number): readonly [number, number][];
|
|
19
|
+
export declare function modularLagrangeInterpolation(x: number, points: readonly [number, number][], prime: number): number;
|
package/sss/poly.js
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.egcd = egcd;
|
|
7
|
+
exports.getPolynomialPoints = getPolynomialPoints;
|
|
8
|
+
exports.modInv = modInv;
|
|
9
|
+
exports.modularLagrangeInterpolation = modularLagrangeInterpolation;
|
|
10
|
+
exports.randomPolynomial = randomPolynomial;
|
|
11
|
+
var _nodeCrypto = require("node:crypto");
|
|
12
|
+
var _mathlib = require("../mathlib");
|
|
13
|
+
var _util = require("../util");
|
|
14
|
+
/**
|
|
15
|
+
* Copyright (c) Halfmoon Labs. All rights reserved.
|
|
16
|
+
*
|
|
17
|
+
* Modified in 2025 by Alerta Dino's IT Team.
|
|
18
|
+
*
|
|
19
|
+
* This code was released under the BSD 3-Clause License.
|
|
20
|
+
* See the "LICENSE" file under project root.
|
|
21
|
+
*
|
|
22
|
+
* @see https://github.com/shea256/secret-sharing/blob/master/secretsharing/polynomials.py
|
|
23
|
+
*
|
|
24
|
+
* @author (PUBLIC_ID) 1D-C2-9B-98-D6-C3-D6-AB
|
|
25
|
+
* @signphrase It was created on Earth by humans, although
|
|
26
|
+
* I can't define what a "human" is.
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
function egcd(a, b) {
|
|
30
|
+
if (a === 0) return [b, 0, 1];
|
|
31
|
+
const [g, y, x] = egcd(b % a, a);
|
|
32
|
+
return [g, x - b / a * y, y];
|
|
33
|
+
}
|
|
34
|
+
function modInv(k, prime) {
|
|
35
|
+
k = k % prime;
|
|
36
|
+
const r = egcd(prime, k < 0 ? -k : k)[2];
|
|
37
|
+
return (prime + r) % prime;
|
|
38
|
+
}
|
|
39
|
+
function randomPolynomial(degree, intercept, upperBound) {
|
|
40
|
+
(0, _util.assertType)("uint32", degree);
|
|
41
|
+
const coefficients = [intercept];
|
|
42
|
+
for (let i = 0; i < degree; ++i) {
|
|
43
|
+
const rc = (0, _nodeCrypto.randomInt)(upperBound - 1);
|
|
44
|
+
coefficients.push(rc);
|
|
45
|
+
}
|
|
46
|
+
return coefficients;
|
|
47
|
+
}
|
|
48
|
+
function getPolynomialPoints(c, points, prime) {
|
|
49
|
+
const res = [];
|
|
50
|
+
for (let x = 1; x < points + 1; ++x) {
|
|
51
|
+
let y = c[0];
|
|
52
|
+
for (let j = 1; j < c.length; ++j) {
|
|
53
|
+
const exp = x ** j % prime;
|
|
54
|
+
const term = c[j] * exp % prime;
|
|
55
|
+
y = (y + term) % prime;
|
|
56
|
+
}
|
|
57
|
+
res.push([x, y]);
|
|
58
|
+
}
|
|
59
|
+
return res;
|
|
60
|
+
}
|
|
61
|
+
function modularLagrangeInterpolation(x, points, prime) {
|
|
62
|
+
const [xVals, yVals] = _mathlib.MathLib.zip(...points);
|
|
63
|
+
let fx = 0;
|
|
64
|
+
for (let i = 0; i < points.length; ++i) {
|
|
65
|
+
let n = 1,
|
|
66
|
+
d = 1;
|
|
67
|
+
for (let j = 0; j < points.length; ++j) {
|
|
68
|
+
if (j === i) continue;
|
|
69
|
+
n = n * (x - xVals[j]) % prime;
|
|
70
|
+
d = d * (xVals[i] - xVals[j]) % prime;
|
|
71
|
+
}
|
|
72
|
+
const lPol = n * modInv(d, prime);
|
|
73
|
+
fx = (prime + fx + yVals[i] * lPol) % prime;
|
|
74
|
+
}
|
|
75
|
+
return fx;
|
|
76
|
+
}
|