@wild-framework/database-auto 0.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.
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.
@@ -0,0 +1,11 @@
1
+ /** Which concrete driver a `DATABASE_URL` maps to. */
2
+ export type DatabaseScheme = "postgres" | "mysql" | "sqlite";
3
+ /**
4
+ * Classify a connection URL by scheme.
5
+ *
6
+ * postgres://… / postgresql://… → "postgres"
7
+ * mysql://… → "mysql"
8
+ * anything else / undefined → "sqlite"
9
+ */
10
+ export declare function classifyDatabaseUrl(url: string | undefined): DatabaseScheme;
11
+ //# sourceMappingURL=classify.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"classify.d.ts","sourceRoot":"","sources":["../src/classify.ts"],"names":[],"mappings":"AAAA,sDAAsD;AACtD,MAAM,MAAM,cAAc,GAAG,UAAU,GAAG,OAAO,GAAG,QAAQ,CAAC;AAE7D;;;;;;GAMG;AACH,wBAAgB,mBAAmB,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,GAAG,cAAc,CAK3E"}
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Classify a connection URL by scheme.
3
+ *
4
+ * postgres://… / postgresql://… → "postgres"
5
+ * mysql://… → "mysql"
6
+ * anything else / undefined → "sqlite"
7
+ */
8
+ export function classifyDatabaseUrl(url) {
9
+ if (!url)
10
+ return "sqlite";
11
+ if (url.startsWith("postgres://") || url.startsWith("postgresql://"))
12
+ return "postgres";
13
+ if (url.startsWith("mysql://"))
14
+ return "mysql";
15
+ return "sqlite";
16
+ }
17
+ //# sourceMappingURL=classify.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"classify.js","sourceRoot":"","sources":["../src/classify.ts"],"names":[],"mappings":"AAGA;;;;;;GAMG;AACH,MAAM,UAAU,mBAAmB,CAAC,GAAuB;IACvD,IAAI,CAAC,GAAG;QAAE,OAAO,QAAQ,CAAC;IAC1B,IAAI,GAAG,CAAC,UAAU,CAAC,aAAa,CAAC,IAAI,GAAG,CAAC,UAAU,CAAC,eAAe,CAAC;QAAE,OAAO,UAAU,CAAC;IACxF,IAAI,GAAG,CAAC,UAAU,CAAC,UAAU,CAAC;QAAE,OAAO,OAAO,CAAC;IAC/C,OAAO,QAAQ,CAAC;AACpB,CAAC"}
@@ -0,0 +1,4 @@
1
+ export { DatabaseProvider } from "./provider.js";
2
+ export { classifyDatabaseUrl } from "./classify.js";
3
+ export type { DatabaseScheme } from "./classify.js";
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAC;AACjD,OAAO,EAAE,mBAAmB,EAAE,MAAM,eAAe,CAAC;AACpD,YAAY,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,3 @@
1
+ export { DatabaseProvider } from "./provider.js";
2
+ export { classifyDatabaseUrl } from "./classify.js";
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAC;AACjD,OAAO,EAAE,mBAAmB,EAAE,MAAM,eAAe,CAAC"}
@@ -0,0 +1,41 @@
1
+ import type { Provider, Application } from "@wild-framework/core";
2
+ import { type Migration } from "@wild-framework/database";
3
+ /**
4
+ * Unified, env-driven database provider.
5
+ *
6
+ * Selects a concrete driver from the `DATABASE_URL` scheme and binds a
7
+ * `Database` into the container. This is the "one provider to rule them all"
8
+ * — register it and set `DATABASE_URL` to swap backends without touching
9
+ * app code.
10
+ *
11
+ * DATABASE_URL=postgres://… → PostgresDriver
12
+ * DATABASE_URL=mysql://… → MysqlDriver
13
+ * (unset) → DrizzleDriver (SQLite, `DB_FILE` or filename)
14
+ *
15
+ * Static configuration (no-arg constructor requirement):
16
+ *
17
+ * ```ts
18
+ * DatabaseProvider.schema = schema; // dialect-appropriate tables
19
+ * DatabaseProvider.migrations = loadMigrations("./migrations");
20
+ * ```
21
+ *
22
+ * IMPORTANT: Drizzle schemas are dialect-specific (`sqliteTable` vs `pgTable`
23
+ * vs `mysqlTable`). Pass a schema written in the dialect you intend to run.
24
+ *
25
+ * The three explicit providers (`DrizzleProvider`, `PostgresProvider`,
26
+ * `MysqlProvider`) remain available when you want to be explicit.
27
+ */
28
+ export declare class DatabaseProvider implements Provider {
29
+ /** Drizzle schema object (tables) for the target dialect. */
30
+ static schema: Record<string, unknown> | undefined;
31
+ /** Migrations to apply after connecting. */
32
+ static migrations: Migration[];
33
+ /** SQLite file path (used when DATABASE_URL is unset). Defaults to "shop.db". */
34
+ static filename: string;
35
+ /** Create tables from the schema at boot. Defaults to true. */
36
+ static autoCreate: boolean;
37
+ register(app: Application): void;
38
+ boot(app: Application): Promise<void>;
39
+ shutdown(app: Application): Promise<void>;
40
+ }
41
+ //# 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,EAGH,KAAK,SAAS,EAEjB,MAAM,0BAA0B,CAAC;AAMlC;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,qBAAa,gBAAiB,YAAW,QAAQ;IAC7C,6DAA6D;IAC7D,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,SAAS,CAAC;IACnD,4CAA4C;IAC5C,MAAM,CAAC,UAAU,EAAE,SAAS,EAAE,CAAM;IACpC,iFAAiF;IACjF,MAAM,CAAC,QAAQ,SAAa;IAC5B,+DAA+D;IAC/D,MAAM,CAAC,UAAU,UAAQ;IAEzB,QAAQ,CAAC,GAAG,EAAE,WAAW,GAAG,IAAI;IA4B1B,IAAI,CAAC,GAAG,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC;IAarC,QAAQ,CAAC,GAAG,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC;CAIlD"}
@@ -0,0 +1,81 @@
1
+ import { Container } from "@wild-framework/container";
2
+ import { Database, MigrationRunner, } from "@wild-framework/database";
3
+ import { DrizzleDriver } from "@wild-framework/database-drizzle";
4
+ import { PostgresDriver } from "@wild-framework/database-postgres";
5
+ import { MysqlDriver } from "@wild-framework/database-mysql";
6
+ import { classifyDatabaseUrl } from "./classify.js";
7
+ /**
8
+ * Unified, env-driven database provider.
9
+ *
10
+ * Selects a concrete driver from the `DATABASE_URL` scheme and binds a
11
+ * `Database` into the container. This is the "one provider to rule them all"
12
+ * — register it and set `DATABASE_URL` to swap backends without touching
13
+ * app code.
14
+ *
15
+ * DATABASE_URL=postgres://… → PostgresDriver
16
+ * DATABASE_URL=mysql://… → MysqlDriver
17
+ * (unset) → DrizzleDriver (SQLite, `DB_FILE` or filename)
18
+ *
19
+ * Static configuration (no-arg constructor requirement):
20
+ *
21
+ * ```ts
22
+ * DatabaseProvider.schema = schema; // dialect-appropriate tables
23
+ * DatabaseProvider.migrations = loadMigrations("./migrations");
24
+ * ```
25
+ *
26
+ * IMPORTANT: Drizzle schemas are dialect-specific (`sqliteTable` vs `pgTable`
27
+ * vs `mysqlTable`). Pass a schema written in the dialect you intend to run.
28
+ *
29
+ * The three explicit providers (`DrizzleProvider`, `PostgresProvider`,
30
+ * `MysqlProvider`) remain available when you want to be explicit.
31
+ */
32
+ export class DatabaseProvider {
33
+ /** Drizzle schema object (tables) for the target dialect. */
34
+ static schema;
35
+ /** Migrations to apply after connecting. */
36
+ static migrations = [];
37
+ /** SQLite file path (used when DATABASE_URL is unset). Defaults to "shop.db". */
38
+ static filename = "shop.db";
39
+ /** Create tables from the schema at boot. Defaults to true. */
40
+ static autoCreate = true;
41
+ register(app) {
42
+ const container = app.resolve(Container);
43
+ const url = process.env.DATABASE_URL;
44
+ const scheme = classifyDatabaseUrl(url);
45
+ const driver = scheme === "postgres"
46
+ ? new PostgresDriver({
47
+ connectionString: url,
48
+ schema: DatabaseProvider.schema,
49
+ autoCreate: DatabaseProvider.autoCreate,
50
+ })
51
+ : scheme === "mysql"
52
+ ? new MysqlDriver({
53
+ uri: url,
54
+ schema: DatabaseProvider.schema,
55
+ autoCreate: DatabaseProvider.autoCreate,
56
+ })
57
+ : new DrizzleDriver({
58
+ filename: process.env.DB_FILE ?? DatabaseProvider.filename,
59
+ schema: DatabaseProvider.schema,
60
+ autoCreate: DatabaseProvider.autoCreate,
61
+ });
62
+ const database = new Database(driver);
63
+ container.singleton(Database, () => database);
64
+ }
65
+ async boot(app) {
66
+ const database = app.resolve(Database);
67
+ await database.connect();
68
+ if (DatabaseProvider.migrations.length > 0) {
69
+ const driver = database.driver;
70
+ const applied = await new MigrationRunner(driver).migrate(DatabaseProvider.migrations);
71
+ if (applied.length > 0) {
72
+ console.log(`[database] applied ${applied.length} migration(s): ${applied.join(", ")}`);
73
+ }
74
+ }
75
+ }
76
+ async shutdown(app) {
77
+ const database = app.resolve(Database);
78
+ await database.close();
79
+ }
80
+ }
81
+ //# 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,EACH,QAAQ,EACR,eAAe,GAGlB,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAE,aAAa,EAAE,MAAM,kCAAkC,CAAC;AACjE,OAAO,EAAE,cAAc,EAAE,MAAM,mCAAmC,CAAC;AACnE,OAAO,EAAE,WAAW,EAAE,MAAM,gCAAgC,CAAC;AAC7D,OAAO,EAAE,mBAAmB,EAAE,MAAM,eAAe,CAAC;AAEpD;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,MAAM,OAAO,gBAAgB;IACzB,6DAA6D;IAC7D,MAAM,CAAC,MAAM,CAAsC;IACnD,4CAA4C;IAC5C,MAAM,CAAC,UAAU,GAAgB,EAAE,CAAC;IACpC,iFAAiF;IACjF,MAAM,CAAC,QAAQ,GAAG,SAAS,CAAC;IAC5B,+DAA+D;IAC/D,MAAM,CAAC,UAAU,GAAG,IAAI,CAAC;IAEzB,QAAQ,CAAC,GAAgB;QACrB,MAAM,SAAS,GAAG,GAAG,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;QACzC,MAAM,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC,YAAY,CAAC;QACrC,MAAM,MAAM,GAAG,mBAAmB,CAAC,GAAG,CAAC,CAAC;QAExC,MAAM,MAAM,GACR,MAAM,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OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;YAC5F,CAAC;QACL,CAAC;IACL,CAAC;IAED,KAAK,CAAC,QAAQ,CAAC,GAAgB;QAC3B,MAAM,QAAQ,GAAG,GAAG,CAAC,OAAO,CAAW,QAAQ,CAAC,CAAC;QACjD,MAAM,QAAQ,CAAC,KAAK,EAAE,CAAC;IAC3B,CAAC"}
package/package.json ADDED
@@ -0,0 +1,52 @@
1
+ {
2
+ "name": "@wild-framework/database-auto",
3
+ "version": "0.2.0",
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": "Env-driven database provider — auto-selects SQLite, Postgres, or MySQL from DATABASE_URL.",
26
+ "keywords": [
27
+ "wild",
28
+ "framework",
29
+ "database",
30
+ "drizzle",
31
+ "sqlite",
32
+ "postgres",
33
+ "mysql",
34
+ "auto"
35
+ ],
36
+ "dependencies": {
37
+ "@wild-framework/container": "0.2.0",
38
+ "@wild-framework/database-postgres": "0.2.0",
39
+ "@wild-framework/database-mysql": "0.2.0",
40
+ "@wild-framework/database": "0.2.0",
41
+ "@wild-framework/database-drizzle": "0.2.0",
42
+ "@wild-framework/core": "0.2.0"
43
+ },
44
+ "devDependencies": {
45
+ "drizzle-orm": "^0.38.0"
46
+ },
47
+ "scripts": {
48
+ "build": "tsc",
49
+ "clean": "rm -rf dist",
50
+ "test": "vitest run"
51
+ }
52
+ }