@guren/orm 2.2.0 → 2.2.1
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 +136 -185
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -2992,6 +2992,28 @@ function defineSeeder(handler) {
|
|
|
2992
2992
|
return handler;
|
|
2993
2993
|
}
|
|
2994
2994
|
|
|
2995
|
+
// src/single-flight.ts
|
|
2996
|
+
function singleFlight(factory) {
|
|
2997
|
+
let inFlight;
|
|
2998
|
+
function get() {
|
|
2999
|
+
if (inFlight) {
|
|
3000
|
+
return inFlight;
|
|
3001
|
+
}
|
|
3002
|
+
const attempt = factory().catch((error) => {
|
|
3003
|
+
if (inFlight === attempt) {
|
|
3004
|
+
inFlight = void 0;
|
|
3005
|
+
}
|
|
3006
|
+
throw error;
|
|
3007
|
+
});
|
|
3008
|
+
inFlight = attempt;
|
|
3009
|
+
return attempt;
|
|
3010
|
+
}
|
|
3011
|
+
function reset() {
|
|
3012
|
+
inFlight = void 0;
|
|
3013
|
+
}
|
|
3014
|
+
return { get, reset };
|
|
3015
|
+
}
|
|
3016
|
+
|
|
2995
3017
|
// src/postgres.ts
|
|
2996
3018
|
async function loadPostgresModules() {
|
|
2997
3019
|
try {
|
|
@@ -3012,8 +3034,6 @@ function createPostgresDatabase(options) {
|
|
|
3012
3034
|
const { migrationsFolder, connectionString, clientOptions, seedersFolder, relations } = options;
|
|
3013
3035
|
const resolvedMigrationsFolder = migrationsFolder instanceof URL ? fileURLToPath2(migrationsFolder) : resolve3(String(migrationsFolder));
|
|
3014
3036
|
const resolvedSeedersFolder = seedersFolder == null ? void 0 : seedersFolder instanceof URL ? fileURLToPath2(seedersFolder) : resolve3(String(seedersFolder));
|
|
3015
|
-
let migrationsPromise;
|
|
3016
|
-
let databasePromise;
|
|
3017
3037
|
let client;
|
|
3018
3038
|
let activeKey;
|
|
3019
3039
|
const callSite = new Error().stack;
|
|
@@ -3025,12 +3045,9 @@ function createPostgresDatabase(options) {
|
|
|
3025
3045
|
}
|
|
3026
3046
|
return resolved;
|
|
3027
3047
|
}
|
|
3028
|
-
|
|
3029
|
-
if (migrationsPromise) {
|
|
3030
|
-
return migrationsPromise;
|
|
3031
|
-
}
|
|
3048
|
+
const migrations = singleFlight(async () => {
|
|
3032
3049
|
let endpoint;
|
|
3033
|
-
|
|
3050
|
+
try {
|
|
3034
3051
|
if (!hasDrizzleMigrations(resolvedMigrationsFolder)) {
|
|
3035
3052
|
warnIgnoredFlatSqlMigrations(resolvedMigrationsFolder);
|
|
3036
3053
|
return;
|
|
@@ -3048,41 +3065,28 @@ function createPostgresDatabase(options) {
|
|
|
3048
3065
|
} finally {
|
|
3049
3066
|
await migrationClient.end({ timeout: 0 });
|
|
3050
3067
|
}
|
|
3051
|
-
}
|
|
3052
|
-
migrationsPromise = promise.catch((error) => {
|
|
3053
|
-
migrationsPromise = void 0;
|
|
3068
|
+
} catch (error) {
|
|
3054
3069
|
throw migrationFailure(error, endpoint);
|
|
3070
|
+
}
|
|
3071
|
+
});
|
|
3072
|
+
const database2 = singleFlight(async () => {
|
|
3073
|
+
await migrations.get();
|
|
3074
|
+
const { drizzle, postgres: postgresFactory } = await loadPostgresModules();
|
|
3075
|
+
const url = resolveConnectionString();
|
|
3076
|
+
const activeClient = postgresFactory(url, {
|
|
3077
|
+
max: 1,
|
|
3078
|
+
...clientOptions
|
|
3055
3079
|
});
|
|
3056
|
-
|
|
3057
|
-
|
|
3058
|
-
|
|
3059
|
-
|
|
3060
|
-
return databasePromise;
|
|
3080
|
+
client = activeClient;
|
|
3081
|
+
activeKey = hotReloadKey("postgres", callSite, url);
|
|
3082
|
+
if (activeKey) {
|
|
3083
|
+
await replaceActiveConnection(activeKey, closeDatabase);
|
|
3061
3084
|
}
|
|
3062
|
-
|
|
3063
|
-
|
|
3064
|
-
|
|
3065
|
-
const url = resolveConnectionString();
|
|
3066
|
-
const activeClient = postgresFactory(url, {
|
|
3067
|
-
max: 1,
|
|
3068
|
-
...clientOptions
|
|
3069
|
-
});
|
|
3070
|
-
client = activeClient;
|
|
3071
|
-
activeKey = hotReloadKey("postgres", callSite, url);
|
|
3072
|
-
if (activeKey) {
|
|
3073
|
-
await replaceActiveConnection(activeKey, closeDatabase);
|
|
3074
|
-
}
|
|
3075
|
-
return drizzle({
|
|
3076
|
-
client: activeClient,
|
|
3077
|
-
...relations ? { relations } : {}
|
|
3078
|
-
});
|
|
3079
|
-
})();
|
|
3080
|
-
databasePromise = promise.catch((error) => {
|
|
3081
|
-
databasePromise = void 0;
|
|
3082
|
-
throw error;
|
|
3085
|
+
return drizzle({
|
|
3086
|
+
client: activeClient,
|
|
3087
|
+
...relations ? { relations } : {}
|
|
3083
3088
|
});
|
|
3084
|
-
|
|
3085
|
-
}
|
|
3089
|
+
});
|
|
3086
3090
|
async function closeDatabase() {
|
|
3087
3091
|
if (!client) {
|
|
3088
3092
|
return;
|
|
@@ -3093,21 +3097,21 @@ function createPostgresDatabase(options) {
|
|
|
3093
3097
|
await client.end({ timeout: 0 });
|
|
3094
3098
|
} finally {
|
|
3095
3099
|
client = void 0;
|
|
3096
|
-
|
|
3100
|
+
database2.reset();
|
|
3097
3101
|
if (key) {
|
|
3098
3102
|
releaseActiveConnection(key, closeDatabase);
|
|
3099
3103
|
}
|
|
3100
3104
|
}
|
|
3101
3105
|
}
|
|
3102
3106
|
async function configureOrm() {
|
|
3103
|
-
const db = await
|
|
3107
|
+
const db = await database2.get();
|
|
3104
3108
|
DrizzleAdapter.configure(db);
|
|
3105
3109
|
}
|
|
3106
3110
|
async function seedDatabase() {
|
|
3107
3111
|
if (!resolvedSeedersFolder) {
|
|
3108
3112
|
throw new Error('No seeders folder configured. Provide "seedersFolder" when calling createPostgresDatabase().');
|
|
3109
3113
|
}
|
|
3110
|
-
const db = await
|
|
3114
|
+
const db = await database2.get();
|
|
3111
3115
|
try {
|
|
3112
3116
|
await runSeeders(db, resolvedSeedersFolder);
|
|
3113
3117
|
} catch (error) {
|
|
@@ -3135,7 +3139,7 @@ function createPostgresDatabase(options) {
|
|
|
3135
3139
|
await adminClient.unsafe("CREATE SCHEMA public");
|
|
3136
3140
|
await adminClient.unsafe("DROP SCHEMA IF EXISTS drizzle CASCADE");
|
|
3137
3141
|
});
|
|
3138
|
-
|
|
3142
|
+
migrations.reset();
|
|
3139
3143
|
}
|
|
3140
3144
|
async function migrationStatus() {
|
|
3141
3145
|
const localMigrations = listLocalMigrations(resolvedMigrationsFolder);
|
|
@@ -3155,8 +3159,8 @@ function createPostgresDatabase(options) {
|
|
|
3155
3159
|
return buildMigrationStatus(localMigrations, appliedRows);
|
|
3156
3160
|
}
|
|
3157
3161
|
return {
|
|
3158
|
-
getDatabase,
|
|
3159
|
-
migrateDatabase:
|
|
3162
|
+
getDatabase: database2.get,
|
|
3163
|
+
migrateDatabase: migrations.get,
|
|
3160
3164
|
closeDatabase,
|
|
3161
3165
|
configureOrm,
|
|
3162
3166
|
seedDatabase,
|
|
@@ -3187,8 +3191,6 @@ function createMySqlDatabase(options) {
|
|
|
3187
3191
|
const { migrationsFolder, connectionString, clientOptions, seedersFolder, relations } = options;
|
|
3188
3192
|
const resolvedMigrationsFolder = migrationsFolder instanceof URL ? fileURLToPath3(migrationsFolder) : resolve4(String(migrationsFolder));
|
|
3189
3193
|
const resolvedSeedersFolder = seedersFolder == null ? void 0 : seedersFolder instanceof URL ? fileURLToPath3(seedersFolder) : resolve4(String(seedersFolder));
|
|
3190
|
-
let migrationsPromise;
|
|
3191
|
-
let databasePromise;
|
|
3192
3194
|
let client;
|
|
3193
3195
|
let activeKey;
|
|
3194
3196
|
const callSite = new Error().stack;
|
|
@@ -3200,12 +3202,9 @@ function createMySqlDatabase(options) {
|
|
|
3200
3202
|
}
|
|
3201
3203
|
return resolved;
|
|
3202
3204
|
}
|
|
3203
|
-
|
|
3204
|
-
if (migrationsPromise) {
|
|
3205
|
-
return migrationsPromise;
|
|
3206
|
-
}
|
|
3205
|
+
const migrations = singleFlight(async () => {
|
|
3207
3206
|
let endpoint;
|
|
3208
|
-
|
|
3207
|
+
try {
|
|
3209
3208
|
if (!hasDrizzleMigrations(resolvedMigrationsFolder)) {
|
|
3210
3209
|
warnIgnoredFlatSqlMigrations(resolvedMigrationsFolder);
|
|
3211
3210
|
return;
|
|
@@ -3223,38 +3222,25 @@ function createMySqlDatabase(options) {
|
|
|
3223
3222
|
} finally {
|
|
3224
3223
|
await closePool(migrationClient);
|
|
3225
3224
|
}
|
|
3226
|
-
}
|
|
3227
|
-
migrationsPromise = promise.catch((error) => {
|
|
3228
|
-
migrationsPromise = void 0;
|
|
3225
|
+
} catch (error) {
|
|
3229
3226
|
throw migrationFailure(error, endpoint);
|
|
3230
|
-
});
|
|
3231
|
-
await migrationsPromise;
|
|
3232
|
-
}
|
|
3233
|
-
async function getDatabase() {
|
|
3234
|
-
if (databasePromise) {
|
|
3235
|
-
return databasePromise;
|
|
3236
3227
|
}
|
|
3237
|
-
|
|
3238
|
-
|
|
3239
|
-
|
|
3240
|
-
|
|
3241
|
-
|
|
3242
|
-
|
|
3243
|
-
|
|
3244
|
-
|
|
3245
|
-
|
|
3246
|
-
|
|
3247
|
-
|
|
3248
|
-
|
|
3249
|
-
|
|
3250
|
-
}
|
|
3251
|
-
})();
|
|
3252
|
-
databasePromise = promise.catch((error) => {
|
|
3253
|
-
databasePromise = void 0;
|
|
3254
|
-
throw error;
|
|
3228
|
+
});
|
|
3229
|
+
const database2 = singleFlight(async () => {
|
|
3230
|
+
await migrations.get();
|
|
3231
|
+
const { drizzle, createPool } = await loadMySqlModules();
|
|
3232
|
+
const url = resolveConnectionString();
|
|
3233
|
+
const activeClient = createPool({ uri: url, ...clientOptions });
|
|
3234
|
+
client = activeClient;
|
|
3235
|
+
activeKey = hotReloadKey("mysql", callSite, url);
|
|
3236
|
+
if (activeKey) {
|
|
3237
|
+
await replaceActiveConnection(activeKey, closeDatabase);
|
|
3238
|
+
}
|
|
3239
|
+
return drizzle({
|
|
3240
|
+
client: activeClient,
|
|
3241
|
+
...relations ? { relations } : {}
|
|
3255
3242
|
});
|
|
3256
|
-
|
|
3257
|
-
}
|
|
3243
|
+
});
|
|
3258
3244
|
async function closeDatabase() {
|
|
3259
3245
|
if (!client) {
|
|
3260
3246
|
return;
|
|
@@ -3265,21 +3251,21 @@ function createMySqlDatabase(options) {
|
|
|
3265
3251
|
await closePool(client);
|
|
3266
3252
|
} finally {
|
|
3267
3253
|
client = void 0;
|
|
3268
|
-
|
|
3254
|
+
database2.reset();
|
|
3269
3255
|
if (key) {
|
|
3270
3256
|
releaseActiveConnection(key, closeDatabase);
|
|
3271
3257
|
}
|
|
3272
3258
|
}
|
|
3273
3259
|
}
|
|
3274
3260
|
async function configureOrm() {
|
|
3275
|
-
const db = await
|
|
3261
|
+
const db = await database2.get();
|
|
3276
3262
|
DrizzleAdapter.configure(db);
|
|
3277
3263
|
}
|
|
3278
3264
|
async function seedDatabase() {
|
|
3279
3265
|
if (!resolvedSeedersFolder) {
|
|
3280
3266
|
throw new Error('No seeders folder configured. Provide "seedersFolder" when calling createMySqlDatabase().');
|
|
3281
3267
|
}
|
|
3282
|
-
const db = await
|
|
3268
|
+
const db = await database2.get();
|
|
3283
3269
|
try {
|
|
3284
3270
|
await runSeeders(db, resolvedSeedersFolder);
|
|
3285
3271
|
} catch (error) {
|
|
@@ -3319,7 +3305,7 @@ function createMySqlDatabase(options) {
|
|
|
3319
3305
|
await adminDb.execute(sql.raw("SET FOREIGN_KEY_CHECKS = 1"));
|
|
3320
3306
|
}
|
|
3321
3307
|
});
|
|
3322
|
-
|
|
3308
|
+
migrations.reset();
|
|
3323
3309
|
}
|
|
3324
3310
|
async function migrationStatus() {
|
|
3325
3311
|
const localMigrations = listLocalMigrations(resolvedMigrationsFolder);
|
|
@@ -3339,8 +3325,8 @@ function createMySqlDatabase(options) {
|
|
|
3339
3325
|
return buildMigrationStatus(localMigrations, appliedRows);
|
|
3340
3326
|
}
|
|
3341
3327
|
return {
|
|
3342
|
-
getDatabase,
|
|
3343
|
-
migrateDatabase:
|
|
3328
|
+
getDatabase: database2.get,
|
|
3329
|
+
migrateDatabase: migrations.get,
|
|
3344
3330
|
closeDatabase,
|
|
3345
3331
|
configureOrm,
|
|
3346
3332
|
seedDatabase,
|
|
@@ -3364,17 +3350,14 @@ function createSqliteDatabase(options) {
|
|
|
3364
3350
|
const { migrationsFolder, filename, seedersFolder, relations } = options;
|
|
3365
3351
|
const resolvedMigrationsFolder = migrationsFolder instanceof URL ? fileURLToPath4(migrationsFolder) : resolve5(String(migrationsFolder));
|
|
3366
3352
|
const resolvedSeedersFolder = seedersFolder == null ? void 0 : seedersFolder instanceof URL ? fileURLToPath4(seedersFolder) : resolve5(String(seedersFolder));
|
|
3367
|
-
let db;
|
|
3368
3353
|
let sqliteClient;
|
|
3369
|
-
let migrationsPromise;
|
|
3370
3354
|
let activeKey;
|
|
3371
3355
|
const callSite = new Error().stack;
|
|
3372
3356
|
function resolveFilename() {
|
|
3373
3357
|
const value = typeof filename === "function" ? filename() : filename;
|
|
3374
3358
|
return value ?? process.env.DATABASE_URL ?? "./data/guren.db";
|
|
3375
3359
|
}
|
|
3376
|
-
|
|
3377
|
-
if (db) return db;
|
|
3360
|
+
const database2 = singleFlight(async () => {
|
|
3378
3361
|
const dbPath = resolveFilename();
|
|
3379
3362
|
const { mkdirSync } = await import("fs");
|
|
3380
3363
|
const { dirname } = await import("path");
|
|
@@ -3383,58 +3366,53 @@ function createSqliteDatabase(options) {
|
|
|
3383
3366
|
} catch {
|
|
3384
3367
|
}
|
|
3385
3368
|
const { Database } = await import("bun:sqlite");
|
|
3369
|
+
const { drizzle } = await import("drizzle-orm/bun-sqlite");
|
|
3386
3370
|
const sqlite = new Database(dbPath);
|
|
3387
3371
|
sqlite.exec("PRAGMA journal_mode = WAL;");
|
|
3388
3372
|
sqliteClient = sqlite;
|
|
3389
|
-
const
|
|
3390
|
-
const database2 = drizzle({ client: sqlite, ...relations ? { relations } : {} });
|
|
3391
|
-
db = database2;
|
|
3373
|
+
const handle = drizzle({ client: sqlite, ...relations ? { relations } : {} });
|
|
3392
3374
|
activeKey = isInMemory(dbPath) ? void 0 : hotReloadKey("sqlite", callSite, resolve5(dbPath));
|
|
3393
3375
|
if (activeKey) {
|
|
3394
3376
|
await replaceActiveConnection(activeKey, closeDatabase);
|
|
3395
3377
|
}
|
|
3396
|
-
return
|
|
3397
|
-
}
|
|
3378
|
+
return handle;
|
|
3379
|
+
});
|
|
3398
3380
|
async function closeDatabase() {
|
|
3399
|
-
if (!
|
|
3381
|
+
if (!sqliteClient) return;
|
|
3400
3382
|
const key = activeKey;
|
|
3401
3383
|
activeKey = void 0;
|
|
3402
3384
|
try {
|
|
3403
|
-
sqliteClient
|
|
3385
|
+
sqliteClient.close();
|
|
3404
3386
|
} finally {
|
|
3405
|
-
db = void 0;
|
|
3406
3387
|
sqliteClient = void 0;
|
|
3388
|
+
database2.reset();
|
|
3407
3389
|
if (key) {
|
|
3408
3390
|
releaseActiveConnection(key, closeDatabase);
|
|
3409
3391
|
}
|
|
3410
3392
|
}
|
|
3411
3393
|
}
|
|
3412
|
-
|
|
3413
|
-
|
|
3414
|
-
migrationsPromise = (async () => {
|
|
3394
|
+
const migrations = singleFlight(async () => {
|
|
3395
|
+
try {
|
|
3415
3396
|
if (!hasDrizzleMigrations(resolvedMigrationsFolder)) {
|
|
3416
3397
|
warnIgnoredFlatSqlMigrations(resolvedMigrationsFolder);
|
|
3417
3398
|
return;
|
|
3418
3399
|
}
|
|
3419
|
-
const
|
|
3400
|
+
const db = await database2.get();
|
|
3420
3401
|
const { migrate } = await import("drizzle-orm/bun-sqlite/migrator");
|
|
3421
|
-
await migrate(
|
|
3422
|
-
}
|
|
3423
|
-
migrationsPromise = migrationsPromise.catch((error) => {
|
|
3424
|
-
migrationsPromise = void 0;
|
|
3402
|
+
await migrate(db, { migrationsFolder: resolvedMigrationsFolder });
|
|
3403
|
+
} catch (error) {
|
|
3425
3404
|
throw migrationFailure(error);
|
|
3426
|
-
}
|
|
3427
|
-
|
|
3428
|
-
}
|
|
3405
|
+
}
|
|
3406
|
+
});
|
|
3429
3407
|
return {
|
|
3430
3408
|
async getDatabase() {
|
|
3431
|
-
await
|
|
3432
|
-
return
|
|
3409
|
+
await migrations.get();
|
|
3410
|
+
return database2.get();
|
|
3433
3411
|
},
|
|
3434
|
-
migrateDatabase:
|
|
3412
|
+
migrateDatabase: migrations.get,
|
|
3435
3413
|
closeDatabase,
|
|
3436
3414
|
async resetDatabase() {
|
|
3437
|
-
await
|
|
3415
|
+
await database2.get();
|
|
3438
3416
|
if (!sqliteClient) return;
|
|
3439
3417
|
const objects = sqliteClient.query(
|
|
3440
3418
|
"SELECT name, type FROM main.sqlite_master WHERE type IN ('table', 'view') AND name NOT LIKE 'sqlite\\_%' ESCAPE '\\'"
|
|
@@ -3448,12 +3426,12 @@ function createSqliteDatabase(options) {
|
|
|
3448
3426
|
} finally {
|
|
3449
3427
|
sqliteClient.exec("PRAGMA foreign_keys = ON;");
|
|
3450
3428
|
}
|
|
3451
|
-
|
|
3429
|
+
migrations.reset();
|
|
3452
3430
|
},
|
|
3453
3431
|
async migrationStatus() {
|
|
3454
3432
|
const localMigrations = listLocalMigrations(resolvedMigrationsFolder);
|
|
3455
3433
|
if (localMigrations.length === 0) return [];
|
|
3456
|
-
await
|
|
3434
|
+
await database2.get();
|
|
3457
3435
|
let appliedRows = [];
|
|
3458
3436
|
try {
|
|
3459
3437
|
const rows = sqliteClient?.query("SELECT name, applied_at FROM __drizzle_migrations").all();
|
|
@@ -3463,17 +3441,17 @@ function createSqliteDatabase(options) {
|
|
|
3463
3441
|
return buildMigrationStatus(localMigrations, appliedRows);
|
|
3464
3442
|
},
|
|
3465
3443
|
async configureOrm() {
|
|
3466
|
-
const
|
|
3467
|
-
await
|
|
3468
|
-
DrizzleAdapter.configure(
|
|
3444
|
+
const db = await database2.get();
|
|
3445
|
+
await migrations.get();
|
|
3446
|
+
DrizzleAdapter.configure(db);
|
|
3469
3447
|
},
|
|
3470
3448
|
async seedDatabase() {
|
|
3471
3449
|
if (!resolvedSeedersFolder) {
|
|
3472
3450
|
throw new Error('No seeders folder configured. Provide "seedersFolder" when calling createSqliteDatabase().');
|
|
3473
3451
|
}
|
|
3474
|
-
const
|
|
3452
|
+
const db = await database2.get();
|
|
3475
3453
|
try {
|
|
3476
|
-
await runSeeders(
|
|
3454
|
+
await runSeeders(db, resolvedSeedersFolder);
|
|
3477
3455
|
} catch (error) {
|
|
3478
3456
|
throw seedFailure(error);
|
|
3479
3457
|
}
|
|
@@ -3487,37 +3465,28 @@ import { fileURLToPath as fileURLToPath5 } from "url";
|
|
|
3487
3465
|
function createD1Database(options) {
|
|
3488
3466
|
const { binding, migrationsFolder, relations } = options;
|
|
3489
3467
|
const migrationsHint = migrationsFolder == null ? "db/migrations" : migrationsFolder instanceof URL ? relative(process.cwd(), fileURLToPath5(migrationsFolder)) : migrationsFolder;
|
|
3490
|
-
|
|
3491
|
-
|
|
3492
|
-
if (
|
|
3493
|
-
|
|
3494
|
-
|
|
3495
|
-
|
|
3496
|
-
|
|
3497
|
-
|
|
3498
|
-
|
|
3499
|
-
|
|
3500
|
-
const { drizzle } = await import("drizzle-orm/d1");
|
|
3501
|
-
return drizzle(client, relations ? { relations } : void 0);
|
|
3502
|
-
})();
|
|
3503
|
-
databasePromise = promise.catch((error) => {
|
|
3504
|
-
databasePromise = void 0;
|
|
3505
|
-
throw error;
|
|
3506
|
-
});
|
|
3507
|
-
return databasePromise;
|
|
3508
|
-
}
|
|
3468
|
+
const databaseHandle = singleFlight(async () => {
|
|
3469
|
+
const client = binding();
|
|
3470
|
+
if (client == null) {
|
|
3471
|
+
throw new Error(
|
|
3472
|
+
'createD1Database: the "binding" resolver returned no D1 binding. On Workers this usually means it ran before the first request \u2014 defer access (e.g. binding: () => getWorkersEnv<Env>().DB) and check the d1_databases entry in wrangler.jsonc.'
|
|
3473
|
+
);
|
|
3474
|
+
}
|
|
3475
|
+
const { drizzle } = await import("drizzle-orm/d1");
|
|
3476
|
+
return drizzle(client, relations ? { relations } : void 0);
|
|
3477
|
+
});
|
|
3509
3478
|
return {
|
|
3510
|
-
getDatabase:
|
|
3479
|
+
getDatabase: databaseHandle.get,
|
|
3511
3480
|
async migrateDatabase() {
|
|
3512
3481
|
throw new Error(
|
|
3513
3482
|
`D1 migrations are not applied at runtime. Run \`wrangler d1 migrations apply <database>\` (local: add --local) with d1_databases[].migrations_dir pointing at ${migrationsHint}.`
|
|
3514
3483
|
);
|
|
3515
3484
|
},
|
|
3516
3485
|
async closeDatabase() {
|
|
3517
|
-
|
|
3486
|
+
databaseHandle.reset();
|
|
3518
3487
|
},
|
|
3519
3488
|
async configureOrm() {
|
|
3520
|
-
const database2 = await
|
|
3489
|
+
const database2 = await databaseHandle.get();
|
|
3521
3490
|
DrizzleAdapter.configure(database2);
|
|
3522
3491
|
},
|
|
3523
3492
|
async seedDatabase() {
|
|
@@ -3559,8 +3528,6 @@ function createAwsDataApiDatabase(options) {
|
|
|
3559
3528
|
const { migrationsFolder, database: database2, resourceArn, secretArn, clientOptions, migrateOnStart, seedersFolder, relations } = options;
|
|
3560
3529
|
const resolvedMigrationsFolder = migrationsFolder instanceof URL ? fileURLToPath6(migrationsFolder) : resolve6(String(migrationsFolder));
|
|
3561
3530
|
const resolvedSeedersFolder = seedersFolder == null ? void 0 : seedersFolder instanceof URL ? fileURLToPath6(seedersFolder) : resolve6(String(seedersFolder));
|
|
3562
|
-
let migrationsPromise;
|
|
3563
|
-
let databasePromise;
|
|
3564
3531
|
let client;
|
|
3565
3532
|
let activeKey;
|
|
3566
3533
|
const callSite = new Error().stack;
|
|
@@ -3591,49 +3558,33 @@ function createAwsDataApiDatabase(options) {
|
|
|
3591
3558
|
...relations ? { relations } : {}
|
|
3592
3559
|
};
|
|
3593
3560
|
}
|
|
3594
|
-
|
|
3595
|
-
|
|
3596
|
-
return migrationsPromise;
|
|
3597
|
-
}
|
|
3598
|
-
const promise = (async () => {
|
|
3561
|
+
const migrations = singleFlight(async () => {
|
|
3562
|
+
try {
|
|
3599
3563
|
if (!hasDrizzleMigrations(resolvedMigrationsFolder)) {
|
|
3600
3564
|
warnIgnoredFlatSqlMigrations(resolvedMigrationsFolder);
|
|
3601
3565
|
return;
|
|
3602
3566
|
}
|
|
3603
3567
|
const { migrate } = await loadAwsDataApiModules();
|
|
3604
3568
|
await withAdminDb((db) => migrate(db, { migrationsFolder: resolvedMigrationsFolder }));
|
|
3605
|
-
}
|
|
3606
|
-
migrationsPromise = promise.catch((error) => {
|
|
3607
|
-
migrationsPromise = void 0;
|
|
3569
|
+
} catch (error) {
|
|
3608
3570
|
const reason = error instanceof Error ? error.message : String(error);
|
|
3609
3571
|
throw new Error(`Failed to run database migrations: ${reason}`);
|
|
3610
|
-
}
|
|
3611
|
-
|
|
3612
|
-
|
|
3613
|
-
|
|
3614
|
-
|
|
3615
|
-
|
|
3616
|
-
}
|
|
3617
|
-
const
|
|
3618
|
-
|
|
3619
|
-
|
|
3620
|
-
|
|
3621
|
-
|
|
3622
|
-
|
|
3623
|
-
|
|
3624
|
-
|
|
3625
|
-
|
|
3626
|
-
if (activeKey) {
|
|
3627
|
-
await replaceActiveConnection(activeKey, closeDatabase);
|
|
3628
|
-
}
|
|
3629
|
-
return db;
|
|
3630
|
-
})();
|
|
3631
|
-
databasePromise = promise.catch((error) => {
|
|
3632
|
-
databasePromise = void 0;
|
|
3633
|
-
throw error;
|
|
3634
|
-
});
|
|
3635
|
-
return databasePromise;
|
|
3636
|
-
}
|
|
3572
|
+
}
|
|
3573
|
+
});
|
|
3574
|
+
const databaseHandle = singleFlight(async () => {
|
|
3575
|
+
if (migrateOnStart) {
|
|
3576
|
+
await migrations.get();
|
|
3577
|
+
}
|
|
3578
|
+
const { drizzle } = await loadAwsDataApiModules();
|
|
3579
|
+
const connection = resolveConnection();
|
|
3580
|
+
const db = drizzle(buildDrizzleConfig(connection));
|
|
3581
|
+
client = db.$client;
|
|
3582
|
+
activeKey = hotReloadKey("aws-data-api", callSite, `${connection.resourceArn}/${connection.database}`);
|
|
3583
|
+
if (activeKey) {
|
|
3584
|
+
await replaceActiveConnection(activeKey, closeDatabase);
|
|
3585
|
+
}
|
|
3586
|
+
return db;
|
|
3587
|
+
});
|
|
3637
3588
|
async function closeDatabase() {
|
|
3638
3589
|
if (!client) {
|
|
3639
3590
|
return;
|
|
@@ -3644,21 +3595,21 @@ function createAwsDataApiDatabase(options) {
|
|
|
3644
3595
|
client.destroy?.();
|
|
3645
3596
|
} finally {
|
|
3646
3597
|
client = void 0;
|
|
3647
|
-
|
|
3598
|
+
databaseHandle.reset();
|
|
3648
3599
|
if (key) {
|
|
3649
3600
|
releaseActiveConnection(key, closeDatabase);
|
|
3650
3601
|
}
|
|
3651
3602
|
}
|
|
3652
3603
|
}
|
|
3653
3604
|
async function configureOrm() {
|
|
3654
|
-
const db = await
|
|
3605
|
+
const db = await databaseHandle.get();
|
|
3655
3606
|
DrizzleAdapter.configure(db);
|
|
3656
3607
|
}
|
|
3657
3608
|
async function seedDatabase() {
|
|
3658
3609
|
if (!resolvedSeedersFolder) {
|
|
3659
3610
|
throw new Error('No seeders folder configured. Provide "seedersFolder" when calling createAwsDataApiDatabase().');
|
|
3660
3611
|
}
|
|
3661
|
-
const db = await
|
|
3612
|
+
const db = await databaseHandle.get();
|
|
3662
3613
|
await runSeeders(db, resolvedSeedersFolder);
|
|
3663
3614
|
}
|
|
3664
3615
|
async function withAdminDb(callback) {
|
|
@@ -3677,7 +3628,7 @@ function createAwsDataApiDatabase(options) {
|
|
|
3677
3628
|
await adminDb.execute(sql.raw("CREATE SCHEMA public"));
|
|
3678
3629
|
await adminDb.execute(sql.raw("DROP SCHEMA IF EXISTS drizzle CASCADE"));
|
|
3679
3630
|
});
|
|
3680
|
-
|
|
3631
|
+
migrations.reset();
|
|
3681
3632
|
}
|
|
3682
3633
|
async function migrationStatus() {
|
|
3683
3634
|
const localMigrations = listLocalMigrations(resolvedMigrationsFolder);
|
|
@@ -3700,8 +3651,8 @@ function createAwsDataApiDatabase(options) {
|
|
|
3700
3651
|
return buildMigrationStatus(localMigrations, appliedRows);
|
|
3701
3652
|
}
|
|
3702
3653
|
return {
|
|
3703
|
-
getDatabase,
|
|
3704
|
-
migrateDatabase:
|
|
3654
|
+
getDatabase: databaseHandle.get,
|
|
3655
|
+
migrateDatabase: migrations.get,
|
|
3705
3656
|
closeDatabase,
|
|
3706
3657
|
configureOrm,
|
|
3707
3658
|
seedDatabase,
|