@wild-framework/database 0.1.1 → 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/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/migrations.d.ts +71 -0
- package/dist/migrations.d.ts.map +1 -0
- package/dist/migrations.js +105 -0
- package/dist/migrations.js.map +1 -0
- package/dist/sql.d.ts +10 -0
- package/dist/sql.d.ts.map +1 -0
- package/dist/sql.js +53 -0
- package/dist/sql.js.map +1 -0
- package/package.json +4 -4
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
export { Database } from "./database.js";
|
|
2
2
|
export { DatabaseProvider } from "./provider.js";
|
|
3
3
|
export { InMemoryDatabaseDriver } from "./in-memory-driver.js";
|
|
4
|
+
export { MigrationRunner, loadMigrations } from "./migrations.js";
|
|
5
|
+
export { splitSqlStatements } from "./sql.js";
|
|
4
6
|
export type { DatabaseDriver, DatabaseStore, Row } from "./driver.js";
|
|
7
|
+
export type { SqlExecutor, Migration, MigrationStatus } from "./migrations.js";
|
|
5
8
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAC;AACjD,OAAO,EAAE,sBAAsB,EAAE,MAAM,uBAAuB,CAAC;AAC/D,YAAY,EAAE,cAAc,EAAE,aAAa,EAAE,GAAG,EAAE,MAAM,aAAa,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAC;AACjD,OAAO,EAAE,sBAAsB,EAAE,MAAM,uBAAuB,CAAC;AAC/D,OAAO,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAClE,OAAO,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAC;AAC9C,YAAY,EAAE,cAAc,EAAE,aAAa,EAAE,GAAG,EAAE,MAAM,aAAa,CAAC;AACtE,YAAY,EAAE,WAAW,EAAE,SAAS,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
export { Database } from "./database.js";
|
|
2
2
|
export { DatabaseProvider } from "./provider.js";
|
|
3
3
|
export { InMemoryDatabaseDriver } from "./in-memory-driver.js";
|
|
4
|
+
export { MigrationRunner, loadMigrations } from "./migrations.js";
|
|
5
|
+
export { splitSqlStatements } from "./sql.js";
|
|
4
6
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAC;AACjD,OAAO,EAAE,sBAAsB,EAAE,MAAM,uBAAuB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAC;AACjD,OAAO,EAAE,sBAAsB,EAAE,MAAM,uBAAuB,CAAC;AAC/D,OAAO,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAClE,OAAO,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAC"}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import type { Row } from "./driver.js";
|
|
2
|
+
/**
|
|
3
|
+
* Raw-SQL surface a migration runner needs.
|
|
4
|
+
*
|
|
5
|
+
* `exec` runs one or more `;`-separated statements; `all` runs a query and
|
|
6
|
+
* returns the rows.
|
|
7
|
+
*
|
|
8
|
+
* Async because network-backed drivers (PostgreSQL, MySQL) return promises;
|
|
9
|
+
* the SQLite driver simply resolves immediately.
|
|
10
|
+
*/
|
|
11
|
+
export interface SqlExecutor {
|
|
12
|
+
exec(sql: string): Promise<void>;
|
|
13
|
+
all<T = Row>(sql: string): Promise<T[]>;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* A single migration: a unique name plus the SQL to apply (and optionally
|
|
17
|
+
* revert).
|
|
18
|
+
*/
|
|
19
|
+
export interface Migration {
|
|
20
|
+
/** Unique, sortable name (e.g. `20261010_add_order_index`). */
|
|
21
|
+
name: string;
|
|
22
|
+
/** SQL statements to apply. */
|
|
23
|
+
up: string;
|
|
24
|
+
/** SQL statements to revert (used by rollback). */
|
|
25
|
+
down?: string;
|
|
26
|
+
}
|
|
27
|
+
/** Migration + whether it has been applied. */
|
|
28
|
+
export interface MigrationStatus {
|
|
29
|
+
name: string;
|
|
30
|
+
applied: boolean;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Applies and tracks SQL migrations against any `SqlExecutor`.
|
|
34
|
+
*
|
|
35
|
+
* Tracked in a `_wild_migrations` table (name + applied_at). Each migration
|
|
36
|
+
* is applied once, in name order. Optional `down` SQL enables rollback.
|
|
37
|
+
*
|
|
38
|
+
* Framework-agnostic: it only talks to `SqlExecutor`, so it works with the
|
|
39
|
+
* Drizzle driver today and any future SQL-backed driver.
|
|
40
|
+
*/
|
|
41
|
+
export declare class MigrationRunner {
|
|
42
|
+
private readonly executor;
|
|
43
|
+
constructor(executor: SqlExecutor);
|
|
44
|
+
/** Create the tracking table if it doesn't exist. */
|
|
45
|
+
ensureTable(): Promise<void>;
|
|
46
|
+
/** Names of already-applied migrations, oldest first. */
|
|
47
|
+
appliedNames(): Promise<string[]>;
|
|
48
|
+
/**
|
|
49
|
+
* Apply all pending migrations, in name order.
|
|
50
|
+
*
|
|
51
|
+
* @returns names of the migrations that were applied in this run
|
|
52
|
+
*/
|
|
53
|
+
migrate(migrations: Migration[]): Promise<string[]>;
|
|
54
|
+
/**
|
|
55
|
+
* Revert the most recent `steps` applied migrations (newest first).
|
|
56
|
+
*
|
|
57
|
+
* @returns names of the migrations that were reverted
|
|
58
|
+
*/
|
|
59
|
+
rollback(migrations: Migration[], steps?: number): Promise<string[]>;
|
|
60
|
+
/** Which migrations are applied vs pending, in name order. */
|
|
61
|
+
status(migrations: Migration[]): Promise<MigrationStatus[]>;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Load migrations from a directory of SQL files.
|
|
65
|
+
*
|
|
66
|
+
* Conventions:
|
|
67
|
+
* - `20261010_add_order_index.sql` → up SQL, name `20261010_add_order_index`
|
|
68
|
+
* - `20261010_add_order_index.down.sql` → optional rollback SQL
|
|
69
|
+
*/
|
|
70
|
+
export declare function loadMigrations(dir: string): Migration[];
|
|
71
|
+
//# sourceMappingURL=migrations.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"migrations.d.ts","sourceRoot":"","sources":["../src/migrations.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,aAAa,CAAC;AAEvC;;;;;;;;GAQG;AACH,MAAM,WAAW,WAAW;IACxB,IAAI,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IACjC,GAAG,CAAC,CAAC,GAAG,GAAG,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,EAAE,CAAC,CAAC;CAC3C;AAED;;;GAGG;AACH,MAAM,WAAW,SAAS;IACtB,+DAA+D;IAC/D,IAAI,EAAE,MAAM,CAAC;IACb,+BAA+B;IAC/B,EAAE,EAAE,MAAM,CAAC;IACX,mDAAmD;IACnD,IAAI,CAAC,EAAE,MAAM,CAAC;CACjB;AAED,+CAA+C;AAC/C,MAAM,WAAW,eAAe;IAC5B,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,OAAO,CAAC;CACpB;AAgBD;;;;;;;;GAQG;AACH,qBAAa,eAAe;IACZ,OAAO,CAAC,QAAQ,CAAC,QAAQ;gBAAR,QAAQ,EAAE,WAAW;IAElD,qDAAqD;IAC/C,WAAW,IAAI,OAAO,CAAC,IAAI,CAAC;IAIlC,yDAAyD;IACnD,YAAY,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC;IAQvC;;;;OAIG;IACG,OAAO,CAAC,UAAU,EAAE,SAAS,EAAE,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;IAiBzD;;;;OAIG;IACG,QAAQ,CAAC,UAAU,EAAE,SAAS,EAAE,EAAE,KAAK,SAAI,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;IAgBrE,8DAA8D;IACxD,MAAM,CAAC,UAAU,EAAE,SAAS,EAAE,GAAG,OAAO,CAAC,eAAe,EAAE,CAAC;CAOpE;AAMD;;;;;;GAMG;AACH,wBAAgB,cAAc,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,EAAE,CAYvD"}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { readdirSync, readFileSync, existsSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
const MIGRATIONS_TABLE = "_wild_migrations";
|
|
4
|
+
/**
|
|
5
|
+
* Dialect-neutral tracking table. `VARCHAR` (not `TEXT`) so MySQL accepts a
|
|
6
|
+
* PRIMARY KEY on the name column; SQLite and PostgreSQL treat it identically.
|
|
7
|
+
*/
|
|
8
|
+
const MIGRATIONS_DDL = `CREATE TABLE IF NOT EXISTS ${MIGRATIONS_TABLE} (name VARCHAR(255) PRIMARY KEY, applied_at VARCHAR(64) NOT NULL);`;
|
|
9
|
+
/** Escape a value for a single-quoted SQL literal. */
|
|
10
|
+
function quote(value) {
|
|
11
|
+
return `'${value.replace(/'/g, "''")}'`;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Applies and tracks SQL migrations against any `SqlExecutor`.
|
|
15
|
+
*
|
|
16
|
+
* Tracked in a `_wild_migrations` table (name + applied_at). Each migration
|
|
17
|
+
* is applied once, in name order. Optional `down` SQL enables rollback.
|
|
18
|
+
*
|
|
19
|
+
* Framework-agnostic: it only talks to `SqlExecutor`, so it works with the
|
|
20
|
+
* Drizzle driver today and any future SQL-backed driver.
|
|
21
|
+
*/
|
|
22
|
+
export class MigrationRunner {
|
|
23
|
+
executor;
|
|
24
|
+
constructor(executor) {
|
|
25
|
+
this.executor = executor;
|
|
26
|
+
}
|
|
27
|
+
/** Create the tracking table if it doesn't exist. */
|
|
28
|
+
async ensureTable() {
|
|
29
|
+
await this.executor.exec(MIGRATIONS_DDL);
|
|
30
|
+
}
|
|
31
|
+
/** Names of already-applied migrations, oldest first. */
|
|
32
|
+
async appliedNames() {
|
|
33
|
+
await this.ensureTable();
|
|
34
|
+
const rows = await this.executor.all(`SELECT name FROM ${MIGRATIONS_TABLE} ORDER BY name ASC`);
|
|
35
|
+
return rows.map((r) => r.name);
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Apply all pending migrations, in name order.
|
|
39
|
+
*
|
|
40
|
+
* @returns names of the migrations that were applied in this run
|
|
41
|
+
*/
|
|
42
|
+
async migrate(migrations) {
|
|
43
|
+
await this.ensureTable();
|
|
44
|
+
const applied = new Set(await this.appliedNames());
|
|
45
|
+
const ran = [];
|
|
46
|
+
for (const migration of sortMigrations(migrations)) {
|
|
47
|
+
if (applied.has(migration.name))
|
|
48
|
+
continue;
|
|
49
|
+
await this.executor.exec(migration.up);
|
|
50
|
+
await this.executor.exec(`INSERT INTO ${MIGRATIONS_TABLE} (name, applied_at) VALUES (${quote(migration.name)}, ${quote(new Date().toISOString())});`);
|
|
51
|
+
ran.push(migration.name);
|
|
52
|
+
}
|
|
53
|
+
return ran;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Revert the most recent `steps` applied migrations (newest first).
|
|
57
|
+
*
|
|
58
|
+
* @returns names of the migrations that were reverted
|
|
59
|
+
*/
|
|
60
|
+
async rollback(migrations, steps = 1) {
|
|
61
|
+
await this.ensureTable();
|
|
62
|
+
const byName = new Map(migrations.map((m) => [m.name, m]));
|
|
63
|
+
const reverted = [];
|
|
64
|
+
// appliedNames() is oldest-first → take the tail and reverse.
|
|
65
|
+
for (const name of (await this.appliedNames()).slice(-steps).reverse()) {
|
|
66
|
+
const migration = byName.get(name);
|
|
67
|
+
if (migration?.down)
|
|
68
|
+
await this.executor.exec(migration.down);
|
|
69
|
+
await this.executor.exec(`DELETE FROM ${MIGRATIONS_TABLE} WHERE name = ${quote(name)};`);
|
|
70
|
+
reverted.push(name);
|
|
71
|
+
}
|
|
72
|
+
return reverted;
|
|
73
|
+
}
|
|
74
|
+
/** Which migrations are applied vs pending, in name order. */
|
|
75
|
+
async status(migrations) {
|
|
76
|
+
const applied = new Set(await this.appliedNames());
|
|
77
|
+
return sortMigrations(migrations).map((m) => ({
|
|
78
|
+
name: m.name,
|
|
79
|
+
applied: applied.has(m.name),
|
|
80
|
+
}));
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
function sortMigrations(migrations) {
|
|
84
|
+
return [...migrations].sort((a, b) => a.name.localeCompare(b.name));
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Load migrations from a directory of SQL files.
|
|
88
|
+
*
|
|
89
|
+
* Conventions:
|
|
90
|
+
* - `20261010_add_order_index.sql` → up SQL, name `20261010_add_order_index`
|
|
91
|
+
* - `20261010_add_order_index.down.sql` → optional rollback SQL
|
|
92
|
+
*/
|
|
93
|
+
export function loadMigrations(dir) {
|
|
94
|
+
const files = readdirSync(dir)
|
|
95
|
+
.filter((f) => f.endsWith(".sql") && !f.endsWith(".down.sql"))
|
|
96
|
+
.sort();
|
|
97
|
+
return files.map((file) => {
|
|
98
|
+
const name = file.replace(/\.sql$/, "");
|
|
99
|
+
const up = readFileSync(join(dir, file), "utf8");
|
|
100
|
+
const downFile = join(dir, `${name}.down.sql`);
|
|
101
|
+
const down = existsSync(downFile) ? readFileSync(downFile, "utf8") : undefined;
|
|
102
|
+
return { name, up, down };
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
//# sourceMappingURL=migrations.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"migrations.js","sourceRoot":"","sources":["../src/migrations.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAChE,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAoCjC,MAAM,gBAAgB,GAAG,kBAAkB,CAAC;AAE5C;;;GAGG;AACH,MAAM,cAAc,GAChB,8BAA8B,gBAAgB,oEAAoE,CAAC;AAEvH,sDAAsD;AACtD,SAAS,KAAK,CAAC,KAAa;IACxB,OAAO,IAAI,KAAK,CAAC,OAAO,CAAC,IAAI,EAAE,IAAI,CAAC,GAAG,CAAC;AAC5C,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,OAAO,eAAe;IACK;IAA7B,YAA6B,QAAqB;QAArB,aAAQ,GAAR,QAAQ,CAAa;IAAG,CAAC;IAEtD,qDAAqD;IACrD,KAAK,CAAC,WAAW;QACb,MAAM,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,cAAc,CAAC,CAAC;IAC7C,CAAC;IAED,yDAAyD;IACzD,KAAK,CAAC,YAAY;QACd,MAAM,IAAI,CAAC,WAAW,EAAE,CAAC;QACzB,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,QAAQ,CAAC,GAAG,CAChC,oBAAoB,gBAAgB,oBAAoB,CAC3D,CAAC;QACF,OAAO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,CAAC,CAAC,QAAQ,CAAC,WAAW,CAAC,CAAC;SAC7D,IAAI,EAAE,CAAC;IAEZ,OAAO,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE;QACtB,MAAM,IAAI,GAAG,IAAI,CAAC,OAAO,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC;QACxC,MAAM,EAAE,GAAG,YAAY,CAAC,IAAI,CAAC,GAAG,EAAE,IAAI,CAAC,EAAE,MAAM,CAAC,CAAC;QACjD,MAAM,QAAQ,GAAG,IAAI,CAAC,GAAG,EAAE,GAAG,IAAI,WAAW,CAAC,CAAC;QAC/C,MAAM,IAAI,GAAG,UAAU,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,YAAY,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;QAC/E,OAAO,EAAE,IAAI,EAAE,EAAE,EAAE,IAAI,EAAE,CAAC;IAC9B,CAAC,CAAC,CAAC;AACP,CAAC"}
|
package/dist/sql.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Split a block of SQL into individual statements.
|
|
3
|
+
*
|
|
4
|
+
* Splits on `;` that appear outside single/double-quoted string literals and
|
|
5
|
+
* outside `--` line comments. Used by network-backed drivers (PostgreSQL,
|
|
6
|
+
* MySQL) whose clients can't execute multi-statement batches the way
|
|
7
|
+
* better-sqlite3's `exec()` does.
|
|
8
|
+
*/
|
|
9
|
+
export declare function splitSqlStatements(sql: string): string[];
|
|
10
|
+
//# sourceMappingURL=sql.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sql.d.ts","sourceRoot":"","sources":["../src/sql.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,wBAAgB,kBAAkB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,EAAE,CAiDxD"}
|
package/dist/sql.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Split a block of SQL into individual statements.
|
|
3
|
+
*
|
|
4
|
+
* Splits on `;` that appear outside single/double-quoted string literals and
|
|
5
|
+
* outside `--` line comments. Used by network-backed drivers (PostgreSQL,
|
|
6
|
+
* MySQL) whose clients can't execute multi-statement batches the way
|
|
7
|
+
* better-sqlite3's `exec()` does.
|
|
8
|
+
*/
|
|
9
|
+
export function splitSqlStatements(sql) {
|
|
10
|
+
const statements = [];
|
|
11
|
+
let current = "";
|
|
12
|
+
let inSingle = false;
|
|
13
|
+
let inDouble = false;
|
|
14
|
+
let inLineComment = false;
|
|
15
|
+
for (let i = 0; i < sql.length; i++) {
|
|
16
|
+
const ch = sql[i];
|
|
17
|
+
const next = sql[i + 1];
|
|
18
|
+
if (inLineComment) {
|
|
19
|
+
current += ch;
|
|
20
|
+
if (ch === "\n")
|
|
21
|
+
inLineComment = false;
|
|
22
|
+
continue;
|
|
23
|
+
}
|
|
24
|
+
if (!inSingle && !inDouble && ch === "-" && next === "-") {
|
|
25
|
+
inLineComment = true;
|
|
26
|
+
current += ch;
|
|
27
|
+
continue;
|
|
28
|
+
}
|
|
29
|
+
if (ch === "'" && !inDouble) {
|
|
30
|
+
inSingle = !inSingle;
|
|
31
|
+
current += ch;
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
if (ch === '"' && !inSingle) {
|
|
35
|
+
inDouble = !inDouble;
|
|
36
|
+
current += ch;
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
if (ch === ";" && !inSingle && !inDouble) {
|
|
40
|
+
const trimmed = current.trim();
|
|
41
|
+
if (trimmed)
|
|
42
|
+
statements.push(trimmed);
|
|
43
|
+
current = "";
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
current += ch;
|
|
47
|
+
}
|
|
48
|
+
const trimmed = current.trim();
|
|
49
|
+
if (trimmed)
|
|
50
|
+
statements.push(trimmed);
|
|
51
|
+
return statements;
|
|
52
|
+
}
|
|
53
|
+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C;QAED,IAAI,EAAE,KAAK,GAAG,IAAI,CAAC,QAAQ,IAAI,CAAC,QAAQ,EAAE,CAAC;YACvC,MAAM,OAAO,GAAG,OAAO,CAAC,IAAI,EAAE,CAAC;YAC/B,IAAI,OAAO;gBAAE,UAAU,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;YACtC,OAAO,GAAG,EAAE,CAAC;YACb,SAAS;QACb,CAAC;QAED,OAAO,IAAI,EAAE,CAAC;IAClB,CAAC;IAED,MAAM,OAAO,GAAG,OAAO,CAAC,IAAI,EAAE,CAAC;IAC/B,IAAI,OAAO;QAAE,UAAU,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;IAEtC,OAAO,UAAU,CAAC;AACtB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wild-framework/database",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
"author": "Sphah",
|
|
21
21
|
"repository": {
|
|
22
22
|
"type": "git",
|
|
23
|
-
"url": "git+https://github.com/
|
|
23
|
+
"url": "git+https://github.com/Khandasoftware/wild-framework.git"
|
|
24
24
|
},
|
|
25
25
|
"description": "Database connection abstraction for Wild Framework.",
|
|
26
26
|
"keywords": [
|
|
@@ -31,8 +31,8 @@
|
|
|
31
31
|
"orm"
|
|
32
32
|
],
|
|
33
33
|
"dependencies": {
|
|
34
|
-
"@wild-framework/core": "0.
|
|
35
|
-
"@wild-framework/container": "0.
|
|
34
|
+
"@wild-framework/core": "0.2.2",
|
|
35
|
+
"@wild-framework/container": "0.2.2"
|
|
36
36
|
},
|
|
37
37
|
"scripts": {
|
|
38
38
|
"build": "tsc",
|