@guren/orm 1.0.0-rc.16 → 1.0.0-rc.18
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.js +63 -16
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,3 +1,20 @@
|
|
|
1
|
+
// src/instance-guard.ts
|
|
2
|
+
var INSTANCE_KEY = /* @__PURE__ */ Symbol.for("guren.orm.loaded");
|
|
3
|
+
var globalScope = globalThis;
|
|
4
|
+
var marker = globalScope[INSTANCE_KEY];
|
|
5
|
+
if (!marker) {
|
|
6
|
+
globalScope[INSTANCE_KEY] = { count: 1, warned: false };
|
|
7
|
+
} else {
|
|
8
|
+
marker.count += 1;
|
|
9
|
+
if (!marker.warned) {
|
|
10
|
+
marker.warned = true;
|
|
11
|
+
console.warn(
|
|
12
|
+
`[guren/orm] ${marker.count} copies of @guren/orm are loaded in this process. Adapter configuration and model state are NOT shared between copies, so database access will fail with "database has not been configured" for models imported through the extra copy.
|
|
13
|
+
[guren/orm] This usually means mixed @guren/* versions (check \`bun pm ls | grep @guren\`). Fix it by aligning all @guren/* packages to the same release, e.g. \`bunx guren upgrade\` or updating every @guren/* entry in package.json together, then reinstalling.`
|
|
14
|
+
);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
1
18
|
// src/adapters/drizzle-adapter.ts
|
|
2
19
|
import { and as and2, asc, count, desc, eq as eq2, inArray as inArray2, isNull as isNull2 } from "drizzle-orm";
|
|
3
20
|
|
|
@@ -2549,11 +2566,8 @@ function SoftDeletes(Base) {
|
|
|
2549
2566
|
}
|
|
2550
2567
|
|
|
2551
2568
|
// src/postgres.ts
|
|
2552
|
-
import { drizzle } from "drizzle-orm/postgres-js";
|
|
2553
|
-
import { migrate } from "drizzle-orm/postgres-js/migrator";
|
|
2554
2569
|
import { resolve as resolve3 } from "path";
|
|
2555
2570
|
import { fileURLToPath as fileURLToPath2 } from "url";
|
|
2556
|
-
import postgres from "postgres";
|
|
2557
2571
|
|
|
2558
2572
|
// src/migration-utils.ts
|
|
2559
2573
|
import { existsSync, readdirSync } from "fs";
|
|
@@ -2677,6 +2691,21 @@ function defineSeeder(handler) {
|
|
|
2677
2691
|
}
|
|
2678
2692
|
|
|
2679
2693
|
// src/postgres.ts
|
|
2694
|
+
async function loadPostgresModules() {
|
|
2695
|
+
try {
|
|
2696
|
+
const [{ drizzle }, { migrate }, postgresModule] = await Promise.all([
|
|
2697
|
+
import("drizzle-orm/postgres-js"),
|
|
2698
|
+
import("drizzle-orm/postgres-js/migrator"),
|
|
2699
|
+
import("postgres")
|
|
2700
|
+
]);
|
|
2701
|
+
return { drizzle, migrate, postgres: postgresModule.default };
|
|
2702
|
+
} catch (error) {
|
|
2703
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
2704
|
+
throw new Error(
|
|
2705
|
+
`createPostgresDatabase() requires the "postgres" package. Install it with \`bun add postgres\`. (${reason})`
|
|
2706
|
+
);
|
|
2707
|
+
}
|
|
2708
|
+
}
|
|
2680
2709
|
function createPostgresDatabase(options) {
|
|
2681
2710
|
const { migrationsFolder, connectionString, clientOptions, seedersFolder, relations } = options;
|
|
2682
2711
|
const resolvedMigrationsFolder = migrationsFolder instanceof URL ? fileURLToPath2(migrationsFolder) : resolve3(String(migrationsFolder));
|
|
@@ -2701,8 +2730,9 @@ function createPostgresDatabase(options) {
|
|
|
2701
2730
|
warnIgnoredFlatSqlMigrations(resolvedMigrationsFolder);
|
|
2702
2731
|
return;
|
|
2703
2732
|
}
|
|
2733
|
+
const { drizzle, migrate, postgres: postgresFactory } = await loadPostgresModules();
|
|
2704
2734
|
const url = resolveConnectionString();
|
|
2705
|
-
const migrationClient =
|
|
2735
|
+
const migrationClient = postgresFactory(url, {
|
|
2706
2736
|
max: 1,
|
|
2707
2737
|
...clientOptions
|
|
2708
2738
|
});
|
|
@@ -2726,8 +2756,9 @@ function createPostgresDatabase(options) {
|
|
|
2726
2756
|
}
|
|
2727
2757
|
const promise = (async () => {
|
|
2728
2758
|
await migrateOnce();
|
|
2759
|
+
const { drizzle, postgres: postgresFactory } = await loadPostgresModules();
|
|
2729
2760
|
const url = resolveConnectionString();
|
|
2730
|
-
client =
|
|
2761
|
+
client = postgresFactory(url, {
|
|
2731
2762
|
max: 1,
|
|
2732
2763
|
...clientOptions
|
|
2733
2764
|
});
|
|
@@ -2759,7 +2790,8 @@ function createPostgresDatabase(options) {
|
|
|
2759
2790
|
await runSeeders(db, resolvedSeedersFolder);
|
|
2760
2791
|
}
|
|
2761
2792
|
async function withAdminClient(callback) {
|
|
2762
|
-
const
|
|
2793
|
+
const { postgres: postgresFactory } = await loadPostgresModules();
|
|
2794
|
+
const adminClient = postgresFactory(resolveConnectionString(), {
|
|
2763
2795
|
max: 1,
|
|
2764
2796
|
...clientOptions
|
|
2765
2797
|
});
|
|
@@ -2805,10 +2837,22 @@ function createPostgresDatabase(options) {
|
|
|
2805
2837
|
}
|
|
2806
2838
|
|
|
2807
2839
|
// src/mysql.ts
|
|
2808
|
-
import { drizzle as drizzle2 } from "drizzle-orm/mysql2";
|
|
2809
|
-
import { migrate as migrate2 } from "drizzle-orm/mysql2/migrator";
|
|
2810
2840
|
import { resolve as resolve4 } from "path";
|
|
2811
2841
|
import { fileURLToPath as fileURLToPath3 } from "url";
|
|
2842
|
+
async function loadMySqlModules() {
|
|
2843
|
+
try {
|
|
2844
|
+
const [{ drizzle }, { migrate }] = await Promise.all([
|
|
2845
|
+
import("drizzle-orm/mysql2"),
|
|
2846
|
+
import("drizzle-orm/mysql2/migrator")
|
|
2847
|
+
]);
|
|
2848
|
+
return { drizzle, migrate };
|
|
2849
|
+
} catch (error) {
|
|
2850
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
2851
|
+
throw new Error(
|
|
2852
|
+
`createMySqlDatabase() requires the "mysql2" package. Install it with \`bun add mysql2\`. (${reason})`
|
|
2853
|
+
);
|
|
2854
|
+
}
|
|
2855
|
+
}
|
|
2812
2856
|
function createMySqlDatabase(options) {
|
|
2813
2857
|
const { migrationsFolder, connectionString, clientOptions, seedersFolder, relations } = options;
|
|
2814
2858
|
const resolvedMigrationsFolder = migrationsFolder instanceof URL ? fileURLToPath3(migrationsFolder) : resolve4(String(migrationsFolder));
|
|
@@ -2833,8 +2877,9 @@ function createMySqlDatabase(options) {
|
|
|
2833
2877
|
warnIgnoredFlatSqlMigrations(resolvedMigrationsFolder);
|
|
2834
2878
|
return;
|
|
2835
2879
|
}
|
|
2880
|
+
const { drizzle, migrate } = await loadMySqlModules();
|
|
2836
2881
|
const url = resolveConnectionString();
|
|
2837
|
-
const migrationDb =
|
|
2882
|
+
const migrationDb = drizzle({
|
|
2838
2883
|
connection: {
|
|
2839
2884
|
uri: url,
|
|
2840
2885
|
...clientOptions
|
|
@@ -2843,7 +2888,7 @@ function createMySqlDatabase(options) {
|
|
|
2843
2888
|
...relations ? { relations } : {}
|
|
2844
2889
|
});
|
|
2845
2890
|
try {
|
|
2846
|
-
await
|
|
2891
|
+
await migrate(migrationDb, { migrationsFolder: resolvedMigrationsFolder });
|
|
2847
2892
|
} finally {
|
|
2848
2893
|
await closeClient(migrationDb);
|
|
2849
2894
|
}
|
|
@@ -2861,8 +2906,9 @@ function createMySqlDatabase(options) {
|
|
|
2861
2906
|
}
|
|
2862
2907
|
const promise = (async () => {
|
|
2863
2908
|
await migrateOnce();
|
|
2909
|
+
const { drizzle } = await loadMySqlModules();
|
|
2864
2910
|
const url = resolveConnectionString();
|
|
2865
|
-
const db =
|
|
2911
|
+
const db = drizzle({
|
|
2866
2912
|
connection: {
|
|
2867
2913
|
uri: url,
|
|
2868
2914
|
...clientOptions
|
|
@@ -2899,7 +2945,8 @@ function createMySqlDatabase(options) {
|
|
|
2899
2945
|
await runSeeders(db, resolvedSeedersFolder);
|
|
2900
2946
|
}
|
|
2901
2947
|
async function withAdminDb(callback) {
|
|
2902
|
-
const
|
|
2948
|
+
const { drizzle } = await loadMySqlModules();
|
|
2949
|
+
const adminDb = drizzle({
|
|
2903
2950
|
connection: {
|
|
2904
2951
|
uri: resolveConnectionString(),
|
|
2905
2952
|
...clientOptions
|
|
@@ -3011,8 +3058,8 @@ function createSqliteDatabase(options) {
|
|
|
3011
3058
|
const sqlite = new Database(dbPath);
|
|
3012
3059
|
sqlite.exec("PRAGMA journal_mode = WAL;");
|
|
3013
3060
|
sqliteClient = sqlite;
|
|
3014
|
-
const { drizzle
|
|
3015
|
-
db =
|
|
3061
|
+
const { drizzle } = await import("drizzle-orm/bun-sqlite");
|
|
3062
|
+
db = drizzle({ client: sqlite, ...relations ? { relations } : {} });
|
|
3016
3063
|
return db;
|
|
3017
3064
|
}
|
|
3018
3065
|
async function migrateOnce() {
|
|
@@ -3023,8 +3070,8 @@ function createSqliteDatabase(options) {
|
|
|
3023
3070
|
return;
|
|
3024
3071
|
}
|
|
3025
3072
|
const database2 = await ensureDatabase2();
|
|
3026
|
-
const { migrate
|
|
3027
|
-
await
|
|
3073
|
+
const { migrate } = await import("drizzle-orm/bun-sqlite/migrator");
|
|
3074
|
+
await migrate(database2, { migrationsFolder: resolvedMigrationsFolder });
|
|
3028
3075
|
})();
|
|
3029
3076
|
migrationsPromise = migrationsPromise.catch((error) => {
|
|
3030
3077
|
migrationsPromise = void 0;
|