@petradb/lucid 1.2.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.
@@ -0,0 +1,31 @@
1
+ /**
2
+ * PetraDB dialect for AdonisJS Lucid.
3
+ *
4
+ * Modeled after PgDialect since PetraDB is PostgreSQL-compatible,
5
+ * but uses PetraDB-specific SQL (SHOW TABLES, SHOW VIEWS, etc.).
6
+ */
7
+ export declare class PetraDBDialect {
8
+ client: any;
9
+ config: any;
10
+ name: string;
11
+ supportsAdvisoryLocks: boolean;
12
+ supportsViews: boolean;
13
+ supportsTypes: boolean;
14
+ supportsDomains: boolean;
15
+ supportsReturningStatement: boolean;
16
+ version: string | undefined;
17
+ dateTimeFormat: string;
18
+ constructor(client: any, config: any);
19
+ getAllTables(_schemas?: string[]): Promise<string[]>;
20
+ getAllViews(_schemas?: string[]): Promise<string[]>;
21
+ getAllTypes(_schemas?: string[]): Promise<string[]>;
22
+ getAllDomains(_schemas?: string[]): Promise<string[]>;
23
+ truncate(table: string, _cascade?: boolean): Promise<void>;
24
+ truncateAllTables(excludeTables?: string[], _schemas?: string[]): Promise<void>;
25
+ dropAllTables(_schemas?: string[]): Promise<void>;
26
+ dropAllViews(_schemas?: string[]): Promise<void>;
27
+ dropAllTypes(_schemas?: string[]): Promise<void>;
28
+ dropAllDomains(_schemas?: string[]): Promise<void>;
29
+ getAdvisoryLock(_key: number): Promise<boolean>;
30
+ releaseAdvisoryLock(_key: number): Promise<boolean>;
31
+ }
@@ -0,0 +1,83 @@
1
+ // @ts-nocheck — Lucid internals are untyped; we implement DialectContract at runtime.
2
+ /**
3
+ * PetraDB dialect for AdonisJS Lucid.
4
+ *
5
+ * Modeled after PgDialect since PetraDB is PostgreSQL-compatible,
6
+ * but uses PetraDB-specific SQL (SHOW TABLES, SHOW VIEWS, etc.).
7
+ */
8
+ export class PetraDBDialect {
9
+ client;
10
+ config;
11
+ name = "petradb";
12
+ supportsAdvisoryLocks = false;
13
+ supportsViews = true;
14
+ supportsTypes = true;
15
+ supportsDomains = false;
16
+ supportsReturningStatement = true;
17
+ version;
18
+ dateTimeFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZ";
19
+ constructor(client, config) {
20
+ this.client = client;
21
+ this.config = config;
22
+ }
23
+ async getAllTables(_schemas) {
24
+ const result = await this.client.rawQuery("SHOW TABLES");
25
+ const rows = result.rows || result;
26
+ return rows.map((r) => r.table_name || r.name);
27
+ }
28
+ async getAllViews(_schemas) {
29
+ const result = await this.client.rawQuery("SHOW VIEWS");
30
+ const rows = result.rows || result;
31
+ return rows.map((r) => r.view_name || r.name);
32
+ }
33
+ async getAllTypes(_schemas) {
34
+ const result = await this.client.rawQuery("SHOW TYPES");
35
+ const rows = result.rows || result;
36
+ return rows.map((r) => r.type_name || r.name);
37
+ }
38
+ async getAllDomains(_schemas) {
39
+ return [];
40
+ }
41
+ async truncate(table, _cascade) {
42
+ await this.client.rawQuery(`TRUNCATE TABLE "${table}"`);
43
+ }
44
+ async truncateAllTables(excludeTables, _schemas) {
45
+ const tables = await this.getAllTables();
46
+ const exclude = new Set(excludeTables || []);
47
+ for (const table of tables) {
48
+ if (!exclude.has(table)) {
49
+ await this.truncate(table);
50
+ }
51
+ }
52
+ }
53
+ async dropAllTables(_schemas) {
54
+ const tables = await this.getAllTables();
55
+ const ignore = this.config.wipe?.ignoreTables || [];
56
+ for (const table of tables) {
57
+ if (!ignore.includes(table)) {
58
+ await this.client.rawQuery(`DROP TABLE "${table}" CASCADE`);
59
+ }
60
+ }
61
+ }
62
+ async dropAllViews(_schemas) {
63
+ const views = await this.getAllViews();
64
+ for (const view of views) {
65
+ await this.client.rawQuery(`DROP VIEW "${view}"`);
66
+ }
67
+ }
68
+ async dropAllTypes(_schemas) {
69
+ const types = await this.getAllTypes();
70
+ for (const type of types) {
71
+ await this.client.rawQuery(`DROP TYPE "${type}"`);
72
+ }
73
+ }
74
+ async dropAllDomains(_schemas) {
75
+ // PetraDB doesn't support domains
76
+ }
77
+ async getAdvisoryLock(_key) {
78
+ return false;
79
+ }
80
+ async releaseAdvisoryLock(_key) {
81
+ return false;
82
+ }
83
+ }
@@ -0,0 +1,2 @@
1
+ import { PetraDBDialect } from "./dialect.js";
2
+ export { PetraDBDialect };
package/dist/index.js ADDED
@@ -0,0 +1,70 @@
1
+ // @ts-nocheck — Monkey-patches Lucid internals which are untyped.
2
+ import { createRequire } from "node:module";
3
+ import path from "node:path";
4
+ import { PetraDBDialect } from "./dialect.js";
5
+ const _require = createRequire(import.meta.url);
6
+ /**
7
+ * Resolve the actual filesystem path of @adonisjs/lucid's build directory.
8
+ * We use absolute paths to bypass the package's exports map, which doesn't
9
+ * expose internal modules we need to patch.
10
+ */
11
+ function lucidBuildDir() {
12
+ // require.resolve('@adonisjs/lucid') returns .../build/index.js
13
+ return path.dirname(_require.resolve("@adonisjs/lucid"));
14
+ }
15
+ /**
16
+ * Patch Lucid's client allowlist to accept 'petradb' as a valid client name.
17
+ *
18
+ * This must run before any Lucid connections are created. Import this module
19
+ * as a side-effect import in your AdonisJS app:
20
+ *
21
+ * import '@petradb/lucid'
22
+ */
23
+ function patch() {
24
+ const buildDir = lucidBuildDir();
25
+ // 1. Add PetraDBDialect to the dialect mapping
26
+ const dialects = _require(path.join(buildDir, "src/dialects/index.js"));
27
+ dialects.clientsToDialectsMapping["petradb"] = PetraDBDialect;
28
+ // 2. Update the clientsNames array (used for validation in Connection constructor)
29
+ if (!dialects.clientsNames.includes("petradb")) {
30
+ dialects.clientsNames.push("petradb");
31
+ }
32
+ // 3. Patch Connection to swap 'petradb' string for PetraDBClient class
33
+ // and skip knex-dynamic-connection patching (PetraDB is in-process like SQLite)
34
+ const connectionMod = _require(path.join(buildDir, "src/connection/index.js"));
35
+ const Connection = connectionMod.Connection;
36
+ const originalGetWriteConfig = Connection.prototype.getWriteConfig;
37
+ Connection.prototype.getWriteConfig = function () {
38
+ const config = originalGetWriteConfig.call(this);
39
+ if (config.client === "petradb") {
40
+ const PetraDBClient = _require("@petradb/knex").default;
41
+ return { ...config, client: PetraDBClient };
42
+ }
43
+ return config;
44
+ };
45
+ // PetraDB is an in-process engine — no read/write replicas, no dynamic
46
+ // connection patching needed. Skip patchKnex for petradb connections.
47
+ const originalSetupWriteConnection = Connection.prototype.setupWriteConnection;
48
+ Connection.prototype.setupWriteConnection = function () {
49
+ if (this.clientName === "petradb") {
50
+ const knexMod = _require("knex");
51
+ const { Logger: ConnectionLogger } = _require(path.join(buildDir, "src/connection/logger.js"));
52
+ this.client = knexMod.knex(Object.assign({ log: new ConnectionLogger(this.name, this.logger) }, this.getWriteConfig(), { debug: false }));
53
+ // Skip patchKnex — not needed for in-process engines
54
+ return;
55
+ }
56
+ originalSetupWriteConnection.call(this);
57
+ };
58
+ const originalSetupReadConnection = Connection.prototype.setupReadConnection;
59
+ Connection.prototype.setupReadConnection = function () {
60
+ if (this.clientName === "petradb") {
61
+ // In-process engine — read client is same as write client
62
+ this.readClient = this.client;
63
+ return;
64
+ }
65
+ originalSetupReadConnection.call(this);
66
+ };
67
+ }
68
+ // Run the patch immediately on import
69
+ patch();
70
+ export { PetraDBDialect };
package/package.json ADDED
@@ -0,0 +1,50 @@
1
+ {
2
+ "name": "@petradb/lucid",
3
+ "version": "1.2.0",
4
+ "description": "AdonisJS Lucid ORM driver for PetraDB",
5
+ "type": "module",
6
+ "exports": {
7
+ ".": {
8
+ "types": "./dist/index.d.ts",
9
+ "default": "./dist/index.js"
10
+ }
11
+ },
12
+ "main": "dist/index.js",
13
+ "types": "dist/index.d.ts",
14
+ "files": [
15
+ "dist/",
16
+ "README.md"
17
+ ],
18
+ "scripts": {
19
+ "build": "tsc",
20
+ "clean": "rm -rf dist"
21
+ },
22
+ "peerDependencies": {
23
+ "@adonisjs/lucid": ">=22.0.0",
24
+ "@petradb/engine": ">=1.2.0",
25
+ "@petradb/knex": ">=1.2.0"
26
+ },
27
+ "devDependencies": {
28
+ "@adonisjs/lucid": "^22.0.0",
29
+ "@petradb/engine": "file:../engine/npm",
30
+ "@petradb/knex": "file:../knex",
31
+ "knex": "^3.1.0",
32
+ "luxon": "^3.7.2",
33
+ "typescript": "^5.9.0"
34
+ },
35
+ "repository": {
36
+ "type": "git",
37
+ "url": "git+https://github.com/edadma/petradb.git"
38
+ },
39
+ "keywords": [
40
+ "adonisjs",
41
+ "lucid",
42
+ "petradb",
43
+ "SQL",
44
+ "database",
45
+ "orm"
46
+ ],
47
+ "author": "Edward A. Maxedon, Sr.",
48
+ "license": "ISC",
49
+ "homepage": "https://petradb.dev"
50
+ }