@wild-framework/database-postgres 0.0.0-stage → 0.2.2
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/dist/ddl.d.ts +11 -0
- package/dist/ddl.d.ts.map +1 -0
- package/dist/ddl.js +61 -0
- package/dist/ddl.js.map +1 -0
- package/dist/driver.d.ts +49 -0
- package/dist/driver.d.ts.map +1 -0
- package/dist/driver.js +75 -0
- package/dist/driver.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -0
- package/dist/provider.d.ts +30 -0
- package/dist/provider.d.ts.map +1 -0
- package/dist/provider.js +53 -0
- package/dist/provider.js.map +1 -0
- package/package.json +46 -3
- package/README.md +0 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Wild Framework contributors
|
|
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/dist/ddl.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generate `CREATE TABLE IF NOT EXISTS` statements from a Drizzle schema
|
|
3
|
+
* object for PostgreSQL.
|
|
4
|
+
*
|
|
5
|
+
* Auto-incrementing ids use Drizzle's `serial`/`bigserial` types — their
|
|
6
|
+
* `getSQLType()` already returns `serial`/`bigserial`, so no extra keyword
|
|
7
|
+
* is needed. Covers scalar types, NOT NULL, PRIMARY KEY, UNIQUE, and literal
|
|
8
|
+
* DEFAULTs.
|
|
9
|
+
*/
|
|
10
|
+
export declare function generatePostgresDdl(schema: Record<string, unknown>): string[];
|
|
11
|
+
//# sourceMappingURL=ddl.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ddl.d.ts","sourceRoot":"","sources":["../src/ddl.ts"],"names":[],"mappings":"AAiBA;;;;;;;;GAQG;AACH,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,EAAE,CAwB7E"}
|
package/dist/ddl.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { getTableConfig } from "drizzle-orm/pg-core";
|
|
2
|
+
/**
|
|
3
|
+
* Generate `CREATE TABLE IF NOT EXISTS` statements from a Drizzle schema
|
|
4
|
+
* object for PostgreSQL.
|
|
5
|
+
*
|
|
6
|
+
* Auto-incrementing ids use Drizzle's `serial`/`bigserial` types — their
|
|
7
|
+
* `getSQLType()` already returns `serial`/`bigserial`, so no extra keyword
|
|
8
|
+
* is needed. Covers scalar types, NOT NULL, PRIMARY KEY, UNIQUE, and literal
|
|
9
|
+
* DEFAULTs.
|
|
10
|
+
*/
|
|
11
|
+
export function generatePostgresDdl(schema) {
|
|
12
|
+
const statements = [];
|
|
13
|
+
for (const table of Object.values(schema)) {
|
|
14
|
+
if (!table || typeof table !== "object")
|
|
15
|
+
continue;
|
|
16
|
+
let config;
|
|
17
|
+
try {
|
|
18
|
+
config = getTableConfig(table);
|
|
19
|
+
}
|
|
20
|
+
catch {
|
|
21
|
+
continue; // not a table (view, relation, …)
|
|
22
|
+
}
|
|
23
|
+
const columns = config.columns;
|
|
24
|
+
if (columns.length === 0)
|
|
25
|
+
continue;
|
|
26
|
+
const lines = columns.map((column) => ` ${columnSql(column)}`);
|
|
27
|
+
statements.push(`CREATE TABLE IF NOT EXISTS "${config.name}" (\n${lines.join(",\n")}\n);`);
|
|
28
|
+
}
|
|
29
|
+
return statements;
|
|
30
|
+
}
|
|
31
|
+
/** Render a single column definition for PostgreSQL. */
|
|
32
|
+
function columnSql(column) {
|
|
33
|
+
const parts = [`"${column.name}"`, column.getSQLType?.() ?? "text"];
|
|
34
|
+
if (column.primary)
|
|
35
|
+
parts.push("PRIMARY KEY");
|
|
36
|
+
if (column.notNull)
|
|
37
|
+
parts.push("NOT NULL");
|
|
38
|
+
if (column.isUnique)
|
|
39
|
+
parts.push("UNIQUE");
|
|
40
|
+
const def = literalDefault(column);
|
|
41
|
+
if (def !== undefined)
|
|
42
|
+
parts.push(`DEFAULT ${def}`);
|
|
43
|
+
return parts.join(" ");
|
|
44
|
+
}
|
|
45
|
+
/** Render a literal default; function/SQL-expression defaults are left out. */
|
|
46
|
+
function literalDefault(column) {
|
|
47
|
+
if (!column.hasDefault)
|
|
48
|
+
return undefined;
|
|
49
|
+
const value = column.default;
|
|
50
|
+
if (value === null)
|
|
51
|
+
return "NULL";
|
|
52
|
+
if (typeof value === "number")
|
|
53
|
+
return String(value);
|
|
54
|
+
if (typeof value === "boolean")
|
|
55
|
+
return value ? "true" : "false";
|
|
56
|
+
if (typeof value === "string")
|
|
57
|
+
return `'${value.replace(/'/g, "''")}'`;
|
|
58
|
+
// Non-literal (SQL expression, function, …) — leave to migrations.
|
|
59
|
+
return undefined;
|
|
60
|
+
}
|
|
61
|
+
//# sourceMappingURL=ddl.js.map
|
package/dist/ddl.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ddl.js","sourceRoot":"","sources":["../src/ddl.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAiBrD;;;;;;;;GAQG;AACH,MAAM,UAAU,mBAAmB,CAAC,MAA+B;IAC/D,MAAM,UAAU,GAAa,EAAE,CAAC;IAEhC,KAAK,MAAM,KAAK,IAAI,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE,CAAC;QACxC,IAAI,CAAC,KAAK,IAAI,OAAO,KAAK,KAAK,QAAQ;YAAE,SAAS;QAElD,IAAI,MAA+C,CAAC;QACpD,IAAI,CAAC;YACD,MAAM,GAAG,cAAc,CAAC,KAA6C,CAGpE,CAAC;QACN,CAAC;QAAC,MAAM,CAAC;YACL,SAAS,CAAC,kCAAkC;QAChD,CAAC;QAED,MAAM,OAAO,GAAG,MAAM,CAAC,OAAO,CAAC;QAC/B,IAAI,OAAO,CAAC,MAAM,KAAK,CAAC;YAAE,SAAS;QAEnC,MAAM,KAAK,GAAG,OAAO,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,KAAK,SAAS,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;QAChE,UAAU,CAAC,IAAI,CAAC,+BAA+B,MAAM,CAAC,IAAI,QAAQ,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;IAC/F,CAAC;IAED,OAAO,UAAU,CAAC;AACtB,CAAC;AAED,wDAAwD;AACxD,SAAS,SAAS,CAAC,MAAkB;IACjC,MAAM,KAAK,GAAa,CAAC,IAAI,MAAM,CAAC,IAAI,GAAG,EAAE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3
|
+
import pg from "pg";
|
|
4
|
+
/**
|
|
5
|
+
* Configuration for the PostgreSQL driver.
|
|
6
|
+
*/
|
|
7
|
+
export interface PostgresConfig<TSchema extends Record<string, unknown>> {
|
|
8
|
+
/** PostgreSQL connection string (e.g. `postgres://user:pass@host:5432/db`). */
|
|
9
|
+
connectionString?: string;
|
|
10
|
+
/** A pre-built pool — for advanced use or tests. */
|
|
11
|
+
pool?: pg.Pool;
|
|
12
|
+
/** Optional Drizzle schema object (tables). */
|
|
13
|
+
schema?: TSchema;
|
|
14
|
+
/**
|
|
15
|
+
* Create tables from the schema at connect time (dev-friendly).
|
|
16
|
+
* Set false to rely on migrations.
|
|
17
|
+
* Defaults to true.
|
|
18
|
+
*/
|
|
19
|
+
autoCreate?: boolean;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* PostgreSQL driver implementing `DatabaseDriver` and `SqlExecutor`.
|
|
23
|
+
*
|
|
24
|
+
* Backed by `pg` + Drizzle (`drizzle-orm/node-postgres`). The `db` getter
|
|
25
|
+
* returns the typed Drizzle instance for ORM queries; `exec`/`all` expose
|
|
26
|
+
* raw SQL for migrations.
|
|
27
|
+
*/
|
|
28
|
+
export declare class PostgresDriver<TSchema extends Record<string, unknown>> implements DatabaseDriver, SqlExecutor {
|
|
29
|
+
private config;
|
|
30
|
+
private pool;
|
|
31
|
+
private drizzleDb;
|
|
32
|
+
private connected;
|
|
33
|
+
constructor(config: PostgresConfig<TSchema>);
|
|
34
|
+
connect(): Promise<void>;
|
|
35
|
+
close(): Promise<void>;
|
|
36
|
+
isConnected(): boolean;
|
|
37
|
+
/** Run one or more `;`-separated SQL statements. */
|
|
38
|
+
exec(sql: string): Promise<void>;
|
|
39
|
+
/** Run a query and return its rows. */
|
|
40
|
+
all<T = Row>(sql: string): Promise<T[]>;
|
|
41
|
+
private requirePool;
|
|
42
|
+
/**
|
|
43
|
+
* The typed Drizzle instance for queries.
|
|
44
|
+
*
|
|
45
|
+
* Throws if the driver is not connected.
|
|
46
|
+
*/
|
|
47
|
+
get db(): NodePgDatabase<TSchema>;
|
|
48
|
+
}
|
|
49
|
+
//# sourceMappingURL=driver.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"driver.d.ts","sourceRoot":"","sources":["../src/driver.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,WAAW,EAAE,GAAG,EAAE,MAAM,0BAA0B,CAAC;AAEjF,OAAO,EAAW,KAAK,cAAc,EAAE,MAAM,2BAA2B,CAAC;AACzE,OAAO,EAAE,MAAM,IAAI,CAAC;AAGpB;;GAEG;AACH,MAAM,WAAW,cAAc,CAAC,OAAO,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;IACnE,+EAA+E;IAC/E,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,oDAAoD;IACpD,IAAI,CAAC,EAAE,EAAE,CAAC,IAAI,CAAC;IACf,+CAA+C;IAC/C,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB;;;;OAIG;IACH,UAAU,CAAC,EAAE,OAAO,CAAC;CACxB;AAED;;;;;;GAMG;AACH,qBAAa,cAAc,CAAC,OAAO,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAC/D,YAAW,cAAc,EAAE,WAAW;IAM1B,OAAO,CAAC,MAAM;IAJ1B,OAAO,CAAC,IAAI,CAAwB;IACpC,OAAO,CAAC,SAAS,CAAwC;IACzD,OAAO,CAAC,SAAS,CAAS;gBAEN,MAAM,EAAE,cAAc,CAAC,OAAO,CAAC;IAE7C,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAaxB,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAU5B,WAAW,IAAI,OAAO;IAQtB,oDAAoD;IAC9C,IAAI,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAOtC,uCAAuC;IACjC,GAAG,CAAC,CAAC,GAAG,GAAG,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,EAAE,CAAC;IAK7C,OAAO,CAAC,WAAW;IAOnB;;;;OAIG;IACH,IAAI,EAAE,IAAI,cAAc,CAAC,OAAO,CAAC,CAKhC;CACJ"}
|
package/dist/driver.js
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { splitSqlStatements } from "@wild-framework/database";
|
|
2
|
+
import { drizzle } from "drizzle-orm/node-postgres";
|
|
3
|
+
import pg from "pg";
|
|
4
|
+
import { generatePostgresDdl } from "./ddl.js";
|
|
5
|
+
/**
|
|
6
|
+
* PostgreSQL driver implementing `DatabaseDriver` and `SqlExecutor`.
|
|
7
|
+
*
|
|
8
|
+
* Backed by `pg` + Drizzle (`drizzle-orm/node-postgres`). The `db` getter
|
|
9
|
+
* returns the typed Drizzle instance for ORM queries; `exec`/`all` expose
|
|
10
|
+
* raw SQL for migrations.
|
|
11
|
+
*/
|
|
12
|
+
export class PostgresDriver {
|
|
13
|
+
config;
|
|
14
|
+
pool = null;
|
|
15
|
+
drizzleDb = null;
|
|
16
|
+
connected = false;
|
|
17
|
+
constructor(config) {
|
|
18
|
+
this.config = config;
|
|
19
|
+
}
|
|
20
|
+
async connect() {
|
|
21
|
+
this.pool = this.config.pool ?? new pg.Pool({ connectionString: this.config.connectionString });
|
|
22
|
+
this.drizzleDb = drizzle(this.pool, { schema: this.config.schema });
|
|
23
|
+
if (this.config.autoCreate !== false && this.config.schema) {
|
|
24
|
+
for (const statement of generatePostgresDdl(this.config.schema)) {
|
|
25
|
+
await this.exec(statement);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
this.connected = true;
|
|
29
|
+
}
|
|
30
|
+
async close() {
|
|
31
|
+
// Only end pools we created — an injected pool is the caller's.
|
|
32
|
+
if (this.pool && !this.config.pool) {
|
|
33
|
+
await this.pool.end();
|
|
34
|
+
}
|
|
35
|
+
this.pool = null;
|
|
36
|
+
this.drizzleDb = null;
|
|
37
|
+
this.connected = false;
|
|
38
|
+
}
|
|
39
|
+
isConnected() {
|
|
40
|
+
return this.connected;
|
|
41
|
+
}
|
|
42
|
+
// -------------------------------------------------------------------
|
|
43
|
+
// SqlExecutor — raw SQL for migrations and ad-hoc statements
|
|
44
|
+
// -------------------------------------------------------------------
|
|
45
|
+
/** Run one or more `;`-separated SQL statements. */
|
|
46
|
+
async exec(sql) {
|
|
47
|
+
const pool = this.requirePool();
|
|
48
|
+
for (const statement of splitSqlStatements(sql)) {
|
|
49
|
+
await pool.query(statement);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
/** Run a query and return its rows. */
|
|
53
|
+
async all(sql) {
|
|
54
|
+
const result = await this.requirePool().query(sql);
|
|
55
|
+
return result.rows;
|
|
56
|
+
}
|
|
57
|
+
requirePool() {
|
|
58
|
+
if (!this.pool) {
|
|
59
|
+
throw new Error("Database is not connected. Call connect() first.");
|
|
60
|
+
}
|
|
61
|
+
return this.pool;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* The typed Drizzle instance for queries.
|
|
65
|
+
*
|
|
66
|
+
* Throws if the driver is not connected.
|
|
67
|
+
*/
|
|
68
|
+
get db() {
|
|
69
|
+
if (!this.drizzleDb) {
|
|
70
|
+
throw new Error("Database is not connected. Call connect() first.");
|
|
71
|
+
}
|
|
72
|
+
return this.drizzleDb;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
//# sourceMappingURL=driver.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"driver.js","sourceRoot":"","sources":["../src/driver.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,kBAAkB,EAAE,MAAM,0BAA0B,CAAC;AAC9D,OAAO,EAAE,OAAO,EAAuB,MAAM,2BAA2B,CAAC;AACzE,OAAO,EAAE,MAAM,IAAI,CAAC;AACpB,OAAO,EAAE,mBAAmB,EAAE,MAAM,UAAU,CAAC;AAoB/C;;;;;;GAMG;AACH,MAAM,OAAO,cAAc;IAOH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C7D,sEAAsE;IAEtE,oDAAoD;IACpD,KAAK,CAAC,IAAI,CAAC,GAAW;QAClB,MAAM,IAAI,GAAG,IAAI,CAAC,WAAW,EAAE,CAAC;QAChC,KAAK,MAAM,SAAS,IAAI,kBAAkB,CAAC,GAAG,CAAC,EAAE,CAAC;YAC9C,MAAM,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC;QAChC,CAAC;IACL,CAAC;IAED,uCAAuC;IACvC,KAAK,CAAC,GAAG,CAAU,GAAW;QAC1B,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,WAAW,EAAE,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QACnD,OAAO,MAAM,CAAC,IAAW,CAAC;IAC9B,CAAC;IAEO,WAAW;QACf,IAAI,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC;YACb,MAAM,IAAI,KAAK,CAAC,kDAAkD,CAAC,CAAC;QACxE,CAAC;QACD,OAAO,IAAI,CAAC,IAAI,CAAC;IACrB,CAAC;IAED;;;;OAIG;IACH,IAAI,EAAE;QACF,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,CAAC;YAClB,MAAM,IAAI,KAAK,CAAC,kDAAkD,CAAC,CAAC;QACxE,CAAC;QACD,OAAO,IAAI,CAAC,SAAS,CAAC;IAC1B,CAAC;CACJ"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAC7C,YAAY,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAClD,OAAO,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAC;AACjD,OAAO,EAAE,mBAAmB,EAAE,MAAM,UAAU,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAE7C,OAAO,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAC;AACjD,OAAO,EAAE,mBAAmB,EAAE,MAAM,UAAU,CAAC"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { Provider, Application } from "@wild-framework/core";
|
|
2
|
+
import { type Migration } from "@wild-framework/database";
|
|
3
|
+
/**
|
|
4
|
+
* Registers a PostgreSQL-backed database with the application.
|
|
5
|
+
*
|
|
6
|
+
* Static configuration (no-arg constructor requirement):
|
|
7
|
+
*
|
|
8
|
+
* ```ts
|
|
9
|
+
* PostgresProvider.connectionString = process.env.DATABASE_URL!;
|
|
10
|
+
* PostgresProvider.schema = { users };
|
|
11
|
+
* PostgresProvider.migrations = loadMigrations("./migrations");
|
|
12
|
+
* ```
|
|
13
|
+
*
|
|
14
|
+
* Connects on boot, creates tables from the schema, applies pending
|
|
15
|
+
* migrations, and closes on shutdown.
|
|
16
|
+
*/
|
|
17
|
+
export declare class PostgresProvider implements Provider {
|
|
18
|
+
/** PostgreSQL connection string. */
|
|
19
|
+
static connectionString: string | undefined;
|
|
20
|
+
/** Drizzle schema object (tables). */
|
|
21
|
+
static schema: Record<string, unknown> | undefined;
|
|
22
|
+
/** Create tables from the schema at boot. Defaults to true. */
|
|
23
|
+
static autoCreate: boolean;
|
|
24
|
+
/** Migrations to apply (in order) after connecting. */
|
|
25
|
+
static migrations: Migration[];
|
|
26
|
+
register(app: Application): void;
|
|
27
|
+
boot(app: Application): Promise<void>;
|
|
28
|
+
shutdown(app: Application): Promise<void>;
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=provider.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../src/provider.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AAElE,OAAO,EAA6B,KAAK,SAAS,EAAE,MAAM,0BAA0B,CAAC;AAGrF;;;;;;;;;;;;;GAaG;AACH,qBAAa,gBAAiB,YAAW,QAAQ;IAC7C,oCAAoC;IACpC,MAAM,CAAC,gBAAgB,EAAE,MAAM,GAAG,SAAS,CAAC;IAC5C,sCAAsC;IACtC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,SAAS,CAAC;IACnD,+DAA+D;IAC/D,MAAM,CAAC,UAAU,UAAQ;IACzB,uDAAuD;IACvD,MAAM,CAAC,UAAU,EAAE,SAAS,EAAE,CAAM;IAEpC,QAAQ,CAAC,GAAG,EAAE,WAAW,GAAG,IAAI;IAa1B,IAAI,CAAC,GAAG,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC;IAarC,QAAQ,CAAC,GAAG,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC;CAIlD"}
|
package/dist/provider.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { Container } from "@wild-framework/container";
|
|
2
|
+
import { Database, MigrationRunner } from "@wild-framework/database";
|
|
3
|
+
import { PostgresDriver } from "./driver.js";
|
|
4
|
+
/**
|
|
5
|
+
* Registers a PostgreSQL-backed database with the application.
|
|
6
|
+
*
|
|
7
|
+
* Static configuration (no-arg constructor requirement):
|
|
8
|
+
*
|
|
9
|
+
* ```ts
|
|
10
|
+
* PostgresProvider.connectionString = process.env.DATABASE_URL!;
|
|
11
|
+
* PostgresProvider.schema = { users };
|
|
12
|
+
* PostgresProvider.migrations = loadMigrations("./migrations");
|
|
13
|
+
* ```
|
|
14
|
+
*
|
|
15
|
+
* Connects on boot, creates tables from the schema, applies pending
|
|
16
|
+
* migrations, and closes on shutdown.
|
|
17
|
+
*/
|
|
18
|
+
export class PostgresProvider {
|
|
19
|
+
/** PostgreSQL connection string. */
|
|
20
|
+
static connectionString;
|
|
21
|
+
/** Drizzle schema object (tables). */
|
|
22
|
+
static schema;
|
|
23
|
+
/** Create tables from the schema at boot. Defaults to true. */
|
|
24
|
+
static autoCreate = true;
|
|
25
|
+
/** Migrations to apply (in order) after connecting. */
|
|
26
|
+
static migrations = [];
|
|
27
|
+
register(app) {
|
|
28
|
+
const container = app.resolve(Container);
|
|
29
|
+
const driver = new PostgresDriver({
|
|
30
|
+
connectionString: PostgresProvider.connectionString,
|
|
31
|
+
schema: PostgresProvider.schema,
|
|
32
|
+
autoCreate: PostgresProvider.autoCreate,
|
|
33
|
+
});
|
|
34
|
+
const database = new Database(driver);
|
|
35
|
+
container.singleton(Database, () => database);
|
|
36
|
+
}
|
|
37
|
+
async boot(app) {
|
|
38
|
+
const database = app.resolve(Database);
|
|
39
|
+
await database.connect();
|
|
40
|
+
if (PostgresProvider.migrations.length > 0) {
|
|
41
|
+
const driver = database.driver;
|
|
42
|
+
const applied = await new MigrationRunner(driver).migrate(PostgresProvider.migrations);
|
|
43
|
+
if (applied.length > 0) {
|
|
44
|
+
console.log(`[postgres] applied ${applied.length} migration(s): ${applied.join(", ")}`);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
async shutdown(app) {
|
|
49
|
+
const database = app.resolve(Database);
|
|
50
|
+
await database.close();
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
//# sourceMappingURL=provider.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"provider.js","sourceRoot":"","sources":["../src/provider.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,2BAA2B,CAAC;AACtD,OAAO,EAAE,QAAQ,EAAE,eAAe,EAAkB,MAAM,0BAA0B,CAAC;AACrF,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAE7C;;;;;;;;;;;;;GAaG;AACH,MAAM,OAAO,gBAAgB;IACzB,oCAAoC;IACpC,MAAM,CAAC,gBAAgB,CAAqB;IAC5C,sCAAsC;IACtC,MAAM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AC,OAAO,CAAW,QAAQ,CAAC,CAAC;QACjD,MAAM,QAAQ,CAAC,KAAK,EAAE,CAAC;IAC3B,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,49 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wild-framework/database-postgres",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.2.2",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"types": "./dist/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"import": "./dist/index.js"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"dist"
|
|
15
|
+
],
|
|
16
|
+
"publishConfig": {
|
|
17
|
+
"access": "public"
|
|
18
|
+
},
|
|
19
|
+
"license": "MIT",
|
|
20
|
+
"author": "Sphah",
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "git+https://github.com/Khandasoftware/wild-framework.git"
|
|
24
|
+
},
|
|
25
|
+
"description": "PostgreSQL driver for Wild Framework's database package (Drizzle + node-postgres).",
|
|
26
|
+
"keywords": [
|
|
27
|
+
"wild",
|
|
28
|
+
"framework",
|
|
29
|
+
"drizzle",
|
|
30
|
+
"postgres",
|
|
31
|
+
"postgresql",
|
|
32
|
+
"pg"
|
|
33
|
+
],
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"drizzle-orm": "^0.38.0",
|
|
36
|
+
"pg": "^8.13.0",
|
|
37
|
+
"@wild-framework/core": "0.2.2",
|
|
38
|
+
"@wild-framework/container": "0.2.2",
|
|
39
|
+
"@wild-framework/database": "0.2.2"
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"@types/pg": "^8.11.0"
|
|
43
|
+
},
|
|
44
|
+
"scripts": {
|
|
45
|
+
"build": "tsc",
|
|
46
|
+
"clean": "rm -rf dist",
|
|
47
|
+
"test": "vitest run"
|
|
48
|
+
}
|
|
6
49
|
}
|
package/README.md
DELETED