@guren/orm 2.1.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.d.ts +8 -0
- package/dist/index.js +192 -202
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -935,6 +935,14 @@ declare abstract class Model<TRecord extends PlainObject = PlainObject> {
|
|
|
935
935
|
* @returns A fresh QueryBuilder instance with no scopes applied
|
|
936
936
|
*/
|
|
937
937
|
static newQueryWithoutScopes<T extends typeof Model>(this: T, queryOptions?: ModelQueryOptions): QueryBuilder<TRecordFor<T>>;
|
|
938
|
+
/**
|
|
939
|
+
* Whether this model carries a filter that every query must apply.
|
|
940
|
+
*
|
|
941
|
+
* The entry points that talk to the adapter directly check this to decide
|
|
942
|
+
* whether they can take the fast path; anything that returns a QueryBuilder
|
|
943
|
+
* goes through `newQuery()` unconditionally.
|
|
944
|
+
*/
|
|
945
|
+
protected static hasScopes(): boolean;
|
|
938
946
|
/**
|
|
939
947
|
* Apply a named query scope.
|
|
940
948
|
*
|
package/dist/index.js
CHANGED
|
@@ -866,6 +866,11 @@ var QueryBuilder = class {
|
|
|
866
866
|
}, { trx: this.options.trx });
|
|
867
867
|
}
|
|
868
868
|
const simpleWhere = this.toSimpleWhereClause();
|
|
869
|
+
if (simpleWhere === null && this.conditions.length > 0) {
|
|
870
|
+
throw new Error(
|
|
871
|
+
`${this.modelClass.name}: this query uses conditions the configured adapter cannot express (it implements neither findManyAdvanced nor countAdvanced). Running it would drop every condition, including any global scope, and return unfiltered rows.`
|
|
872
|
+
);
|
|
873
|
+
}
|
|
869
874
|
return this.adapter.findMany(this.table, {
|
|
870
875
|
where: simpleWhere ?? void 0,
|
|
871
876
|
orderBy: this.options.orderBy.length > 0 ? this.options.orderBy : void 0,
|
|
@@ -887,15 +892,18 @@ var QueryBuilder = class {
|
|
|
887
892
|
if (condition.type !== "simple") {
|
|
888
893
|
return null;
|
|
889
894
|
}
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
result[condition.field] = condition.value;
|
|
895
|
+
let value;
|
|
896
|
+
if (condition.operator === "=" || condition.operator === "in") {
|
|
897
|
+
value = condition.value;
|
|
894
898
|
} else if (condition.operator === "is null") {
|
|
895
|
-
|
|
899
|
+
value = null;
|
|
896
900
|
} else {
|
|
897
901
|
return null;
|
|
898
902
|
}
|
|
903
|
+
if (condition.field in result && !Object.is(result[condition.field], value)) {
|
|
904
|
+
return null;
|
|
905
|
+
}
|
|
906
|
+
result[condition.field] = value;
|
|
899
907
|
}
|
|
900
908
|
return result;
|
|
901
909
|
}
|
|
@@ -1445,8 +1453,7 @@ var Model = class _Model {
|
|
|
1445
1453
|
* const users = await User.all()
|
|
1446
1454
|
*/
|
|
1447
1455
|
static async all(queryOptions) {
|
|
1448
|
-
|
|
1449
|
-
if (this.defaultScope || hasGlobalScopes) {
|
|
1456
|
+
if (this.hasScopes()) {
|
|
1450
1457
|
return this.newQuery(queryOptions).get();
|
|
1451
1458
|
}
|
|
1452
1459
|
const table = this.resolveTable();
|
|
@@ -1468,7 +1475,7 @@ var Model = class _Model {
|
|
|
1468
1475
|
* const userByEmail = await User.find('john@example.com', 'email')
|
|
1469
1476
|
*/
|
|
1470
1477
|
static async find(id, key = "id", queryOptions) {
|
|
1471
|
-
if (this.
|
|
1478
|
+
if (this.hasScopes()) {
|
|
1472
1479
|
return this.newQuery(queryOptions).where(key, id).first();
|
|
1473
1480
|
}
|
|
1474
1481
|
const table = this.resolveTable();
|
|
@@ -1532,7 +1539,7 @@ var Model = class _Model {
|
|
|
1532
1539
|
* const admin = await User.first({ role: 'admin' })
|
|
1533
1540
|
*/
|
|
1534
1541
|
static async first(where, queryOptions) {
|
|
1535
|
-
if (this.
|
|
1542
|
+
if (this.hasScopes()) {
|
|
1536
1543
|
const builder = this.newQuery(queryOptions).limit(1);
|
|
1537
1544
|
if (where) {
|
|
1538
1545
|
builder.where(where);
|
|
@@ -1549,7 +1556,7 @@ var Model = class _Model {
|
|
|
1549
1556
|
return record;
|
|
1550
1557
|
}
|
|
1551
1558
|
static where(fieldOrConditions, operatorOrValue, value) {
|
|
1552
|
-
const builder =
|
|
1559
|
+
const builder = this.newQuery();
|
|
1553
1560
|
if (typeof fieldOrConditions === "object" && fieldOrConditions !== null) {
|
|
1554
1561
|
return builder.where(fieldOrConditions);
|
|
1555
1562
|
}
|
|
@@ -1566,14 +1573,14 @@ var Model = class _Model {
|
|
|
1566
1573
|
* @param field - Column to check for NULL
|
|
1567
1574
|
*/
|
|
1568
1575
|
static whereNull(field) {
|
|
1569
|
-
return
|
|
1576
|
+
return this.newQuery().whereNull(field);
|
|
1570
1577
|
}
|
|
1571
1578
|
/**
|
|
1572
1579
|
* Start a fluent query with a WHERE NOT NULL condition.
|
|
1573
1580
|
* @param field - Column to check for NOT NULL
|
|
1574
1581
|
*/
|
|
1575
1582
|
static whereNotNull(field) {
|
|
1576
|
-
return
|
|
1583
|
+
return this.newQuery().whereNotNull(field);
|
|
1577
1584
|
}
|
|
1578
1585
|
/**
|
|
1579
1586
|
* Start a fluent query with a WHERE IN condition.
|
|
@@ -1581,7 +1588,7 @@ var Model = class _Model {
|
|
|
1581
1588
|
* @param values - Array of values to match against
|
|
1582
1589
|
*/
|
|
1583
1590
|
static whereIn(field, values) {
|
|
1584
|
-
return
|
|
1591
|
+
return this.newQuery().whereIn(field, values);
|
|
1585
1592
|
}
|
|
1586
1593
|
/**
|
|
1587
1594
|
* Start a fluent query with a WHERE NOT IN condition.
|
|
@@ -1589,7 +1596,7 @@ var Model = class _Model {
|
|
|
1589
1596
|
* @param values - Array of values to exclude
|
|
1590
1597
|
*/
|
|
1591
1598
|
static whereNotIn(field, values) {
|
|
1592
|
-
return
|
|
1599
|
+
return this.newQuery().whereNotIn(field, values);
|
|
1593
1600
|
}
|
|
1594
1601
|
/**
|
|
1595
1602
|
* Start a fluent query with a typed field selection.
|
|
@@ -1599,7 +1606,7 @@ var Model = class _Model {
|
|
|
1599
1606
|
* const first = await User.select('id').first()
|
|
1600
1607
|
*/
|
|
1601
1608
|
static select(...fields) {
|
|
1602
|
-
return
|
|
1609
|
+
return this.newQuery().select(...fields);
|
|
1603
1610
|
}
|
|
1604
1611
|
/**
|
|
1605
1612
|
* Start a new QueryBuilder for fluent query construction.
|
|
@@ -1633,6 +1640,16 @@ var Model = class _Model {
|
|
|
1633
1640
|
static newQueryWithoutScopes(queryOptions) {
|
|
1634
1641
|
return new QueryBuilder(this, queryOptions);
|
|
1635
1642
|
}
|
|
1643
|
+
/**
|
|
1644
|
+
* Whether this model carries a filter that every query must apply.
|
|
1645
|
+
*
|
|
1646
|
+
* The entry points that talk to the adapter directly check this to decide
|
|
1647
|
+
* whether they can take the fast path; anything that returns a QueryBuilder
|
|
1648
|
+
* goes through `newQuery()` unconditionally.
|
|
1649
|
+
*/
|
|
1650
|
+
static hasScopes() {
|
|
1651
|
+
return Boolean(this.defaultScope) || Boolean(this.globalScopeRegistry && this.globalScopeRegistry.size > 0);
|
|
1652
|
+
}
|
|
1636
1653
|
/**
|
|
1637
1654
|
* Apply a named query scope.
|
|
1638
1655
|
*
|
|
@@ -1648,7 +1665,7 @@ var Model = class _Model {
|
|
|
1648
1665
|
if (!scopes || typeof scopes[name] !== "function") {
|
|
1649
1666
|
throw new Error(`${this.name}: unknown scope "${name}".`);
|
|
1650
1667
|
}
|
|
1651
|
-
const builder =
|
|
1668
|
+
const builder = this.newQuery();
|
|
1652
1669
|
return scopes[name](builder);
|
|
1653
1670
|
}
|
|
1654
1671
|
/**
|
|
@@ -1858,8 +1875,18 @@ var Model = class _Model {
|
|
|
1858
1875
|
* await User.orderBy('name', { status: 'active' })
|
|
1859
1876
|
*/
|
|
1860
1877
|
static async orderBy(order, where, queryOptions) {
|
|
1861
|
-
const table = this.resolveTable();
|
|
1862
1878
|
const orderBy = normalizeOrderBy(order);
|
|
1879
|
+
if (this.hasScopes()) {
|
|
1880
|
+
const builder = this.newQuery(queryOptions);
|
|
1881
|
+
if (where && Object.keys(where).length > 0) {
|
|
1882
|
+
builder.where(where);
|
|
1883
|
+
}
|
|
1884
|
+
for (const clause of orderBy) {
|
|
1885
|
+
builder.orderBy(clause.column, clause.direction);
|
|
1886
|
+
}
|
|
1887
|
+
return builder.get();
|
|
1888
|
+
}
|
|
1889
|
+
const table = this.resolveTable();
|
|
1863
1890
|
const options = { orderBy };
|
|
1864
1891
|
if (where && Object.keys(where).length > 0) {
|
|
1865
1892
|
options.where = where;
|
|
@@ -1887,6 +1914,18 @@ var Model = class _Model {
|
|
|
1887
1914
|
* })
|
|
1888
1915
|
*/
|
|
1889
1916
|
static async paginate(options = {}, queryOptions) {
|
|
1917
|
+
if (this.hasScopes()) {
|
|
1918
|
+
const builder = this.newQuery(queryOptions);
|
|
1919
|
+
if (options.where && Object.keys(options.where).length > 0) {
|
|
1920
|
+
builder.where(options.where);
|
|
1921
|
+
}
|
|
1922
|
+
if (options.orderBy) {
|
|
1923
|
+
for (const clause of normalizeOrderBy(options.orderBy)) {
|
|
1924
|
+
builder.orderBy(clause.column, clause.direction);
|
|
1925
|
+
}
|
|
1926
|
+
}
|
|
1927
|
+
return builder.paginate({ page: options.page, perPage: options.perPage });
|
|
1928
|
+
}
|
|
1890
1929
|
const table = this.resolveTable();
|
|
1891
1930
|
const adapter = this.getAdapter();
|
|
1892
1931
|
const requestedPage = typeof options.page === "number" ? options.page : 1;
|
|
@@ -2953,6 +2992,28 @@ function defineSeeder(handler) {
|
|
|
2953
2992
|
return handler;
|
|
2954
2993
|
}
|
|
2955
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
|
+
|
|
2956
3017
|
// src/postgres.ts
|
|
2957
3018
|
async function loadPostgresModules() {
|
|
2958
3019
|
try {
|
|
@@ -2973,8 +3034,6 @@ function createPostgresDatabase(options) {
|
|
|
2973
3034
|
const { migrationsFolder, connectionString, clientOptions, seedersFolder, relations } = options;
|
|
2974
3035
|
const resolvedMigrationsFolder = migrationsFolder instanceof URL ? fileURLToPath2(migrationsFolder) : resolve3(String(migrationsFolder));
|
|
2975
3036
|
const resolvedSeedersFolder = seedersFolder == null ? void 0 : seedersFolder instanceof URL ? fileURLToPath2(seedersFolder) : resolve3(String(seedersFolder));
|
|
2976
|
-
let migrationsPromise;
|
|
2977
|
-
let databasePromise;
|
|
2978
3037
|
let client;
|
|
2979
3038
|
let activeKey;
|
|
2980
3039
|
const callSite = new Error().stack;
|
|
@@ -2986,12 +3045,9 @@ function createPostgresDatabase(options) {
|
|
|
2986
3045
|
}
|
|
2987
3046
|
return resolved;
|
|
2988
3047
|
}
|
|
2989
|
-
|
|
2990
|
-
if (migrationsPromise) {
|
|
2991
|
-
return migrationsPromise;
|
|
2992
|
-
}
|
|
3048
|
+
const migrations = singleFlight(async () => {
|
|
2993
3049
|
let endpoint;
|
|
2994
|
-
|
|
3050
|
+
try {
|
|
2995
3051
|
if (!hasDrizzleMigrations(resolvedMigrationsFolder)) {
|
|
2996
3052
|
warnIgnoredFlatSqlMigrations(resolvedMigrationsFolder);
|
|
2997
3053
|
return;
|
|
@@ -3009,41 +3065,28 @@ function createPostgresDatabase(options) {
|
|
|
3009
3065
|
} finally {
|
|
3010
3066
|
await migrationClient.end({ timeout: 0 });
|
|
3011
3067
|
}
|
|
3012
|
-
}
|
|
3013
|
-
migrationsPromise = promise.catch((error) => {
|
|
3014
|
-
migrationsPromise = void 0;
|
|
3068
|
+
} catch (error) {
|
|
3015
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
|
|
3016
3079
|
});
|
|
3017
|
-
|
|
3018
|
-
|
|
3019
|
-
|
|
3020
|
-
|
|
3021
|
-
return databasePromise;
|
|
3080
|
+
client = activeClient;
|
|
3081
|
+
activeKey = hotReloadKey("postgres", callSite, url);
|
|
3082
|
+
if (activeKey) {
|
|
3083
|
+
await replaceActiveConnection(activeKey, closeDatabase);
|
|
3022
3084
|
}
|
|
3023
|
-
|
|
3024
|
-
|
|
3025
|
-
|
|
3026
|
-
const url = resolveConnectionString();
|
|
3027
|
-
const activeClient = postgresFactory(url, {
|
|
3028
|
-
max: 1,
|
|
3029
|
-
...clientOptions
|
|
3030
|
-
});
|
|
3031
|
-
client = activeClient;
|
|
3032
|
-
activeKey = hotReloadKey("postgres", callSite, url);
|
|
3033
|
-
if (activeKey) {
|
|
3034
|
-
await replaceActiveConnection(activeKey, closeDatabase);
|
|
3035
|
-
}
|
|
3036
|
-
return drizzle({
|
|
3037
|
-
client: activeClient,
|
|
3038
|
-
...relations ? { relations } : {}
|
|
3039
|
-
});
|
|
3040
|
-
})();
|
|
3041
|
-
databasePromise = promise.catch((error) => {
|
|
3042
|
-
databasePromise = void 0;
|
|
3043
|
-
throw error;
|
|
3085
|
+
return drizzle({
|
|
3086
|
+
client: activeClient,
|
|
3087
|
+
...relations ? { relations } : {}
|
|
3044
3088
|
});
|
|
3045
|
-
|
|
3046
|
-
}
|
|
3089
|
+
});
|
|
3047
3090
|
async function closeDatabase() {
|
|
3048
3091
|
if (!client) {
|
|
3049
3092
|
return;
|
|
@@ -3054,21 +3097,21 @@ function createPostgresDatabase(options) {
|
|
|
3054
3097
|
await client.end({ timeout: 0 });
|
|
3055
3098
|
} finally {
|
|
3056
3099
|
client = void 0;
|
|
3057
|
-
|
|
3100
|
+
database2.reset();
|
|
3058
3101
|
if (key) {
|
|
3059
3102
|
releaseActiveConnection(key, closeDatabase);
|
|
3060
3103
|
}
|
|
3061
3104
|
}
|
|
3062
3105
|
}
|
|
3063
3106
|
async function configureOrm() {
|
|
3064
|
-
const db = await
|
|
3107
|
+
const db = await database2.get();
|
|
3065
3108
|
DrizzleAdapter.configure(db);
|
|
3066
3109
|
}
|
|
3067
3110
|
async function seedDatabase() {
|
|
3068
3111
|
if (!resolvedSeedersFolder) {
|
|
3069
3112
|
throw new Error('No seeders folder configured. Provide "seedersFolder" when calling createPostgresDatabase().');
|
|
3070
3113
|
}
|
|
3071
|
-
const db = await
|
|
3114
|
+
const db = await database2.get();
|
|
3072
3115
|
try {
|
|
3073
3116
|
await runSeeders(db, resolvedSeedersFolder);
|
|
3074
3117
|
} catch (error) {
|
|
@@ -3096,7 +3139,7 @@ function createPostgresDatabase(options) {
|
|
|
3096
3139
|
await adminClient.unsafe("CREATE SCHEMA public");
|
|
3097
3140
|
await adminClient.unsafe("DROP SCHEMA IF EXISTS drizzle CASCADE");
|
|
3098
3141
|
});
|
|
3099
|
-
|
|
3142
|
+
migrations.reset();
|
|
3100
3143
|
}
|
|
3101
3144
|
async function migrationStatus() {
|
|
3102
3145
|
const localMigrations = listLocalMigrations(resolvedMigrationsFolder);
|
|
@@ -3116,8 +3159,8 @@ function createPostgresDatabase(options) {
|
|
|
3116
3159
|
return buildMigrationStatus(localMigrations, appliedRows);
|
|
3117
3160
|
}
|
|
3118
3161
|
return {
|
|
3119
|
-
getDatabase,
|
|
3120
|
-
migrateDatabase:
|
|
3162
|
+
getDatabase: database2.get,
|
|
3163
|
+
migrateDatabase: migrations.get,
|
|
3121
3164
|
closeDatabase,
|
|
3122
3165
|
configureOrm,
|
|
3123
3166
|
seedDatabase,
|
|
@@ -3148,8 +3191,6 @@ function createMySqlDatabase(options) {
|
|
|
3148
3191
|
const { migrationsFolder, connectionString, clientOptions, seedersFolder, relations } = options;
|
|
3149
3192
|
const resolvedMigrationsFolder = migrationsFolder instanceof URL ? fileURLToPath3(migrationsFolder) : resolve4(String(migrationsFolder));
|
|
3150
3193
|
const resolvedSeedersFolder = seedersFolder == null ? void 0 : seedersFolder instanceof URL ? fileURLToPath3(seedersFolder) : resolve4(String(seedersFolder));
|
|
3151
|
-
let migrationsPromise;
|
|
3152
|
-
let databasePromise;
|
|
3153
3194
|
let client;
|
|
3154
3195
|
let activeKey;
|
|
3155
3196
|
const callSite = new Error().stack;
|
|
@@ -3161,12 +3202,9 @@ function createMySqlDatabase(options) {
|
|
|
3161
3202
|
}
|
|
3162
3203
|
return resolved;
|
|
3163
3204
|
}
|
|
3164
|
-
|
|
3165
|
-
if (migrationsPromise) {
|
|
3166
|
-
return migrationsPromise;
|
|
3167
|
-
}
|
|
3205
|
+
const migrations = singleFlight(async () => {
|
|
3168
3206
|
let endpoint;
|
|
3169
|
-
|
|
3207
|
+
try {
|
|
3170
3208
|
if (!hasDrizzleMigrations(resolvedMigrationsFolder)) {
|
|
3171
3209
|
warnIgnoredFlatSqlMigrations(resolvedMigrationsFolder);
|
|
3172
3210
|
return;
|
|
@@ -3184,38 +3222,25 @@ function createMySqlDatabase(options) {
|
|
|
3184
3222
|
} finally {
|
|
3185
3223
|
await closePool(migrationClient);
|
|
3186
3224
|
}
|
|
3187
|
-
}
|
|
3188
|
-
migrationsPromise = promise.catch((error) => {
|
|
3189
|
-
migrationsPromise = void 0;
|
|
3225
|
+
} catch (error) {
|
|
3190
3226
|
throw migrationFailure(error, endpoint);
|
|
3191
|
-
});
|
|
3192
|
-
await migrationsPromise;
|
|
3193
|
-
}
|
|
3194
|
-
async function getDatabase() {
|
|
3195
|
-
if (databasePromise) {
|
|
3196
|
-
return databasePromise;
|
|
3197
3227
|
}
|
|
3198
|
-
|
|
3199
|
-
|
|
3200
|
-
|
|
3201
|
-
|
|
3202
|
-
|
|
3203
|
-
|
|
3204
|
-
|
|
3205
|
-
|
|
3206
|
-
|
|
3207
|
-
|
|
3208
|
-
|
|
3209
|
-
|
|
3210
|
-
|
|
3211
|
-
}
|
|
3212
|
-
})();
|
|
3213
|
-
databasePromise = promise.catch((error) => {
|
|
3214
|
-
databasePromise = void 0;
|
|
3215
|
-
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 } : {}
|
|
3216
3242
|
});
|
|
3217
|
-
|
|
3218
|
-
}
|
|
3243
|
+
});
|
|
3219
3244
|
async function closeDatabase() {
|
|
3220
3245
|
if (!client) {
|
|
3221
3246
|
return;
|
|
@@ -3226,21 +3251,21 @@ function createMySqlDatabase(options) {
|
|
|
3226
3251
|
await closePool(client);
|
|
3227
3252
|
} finally {
|
|
3228
3253
|
client = void 0;
|
|
3229
|
-
|
|
3254
|
+
database2.reset();
|
|
3230
3255
|
if (key) {
|
|
3231
3256
|
releaseActiveConnection(key, closeDatabase);
|
|
3232
3257
|
}
|
|
3233
3258
|
}
|
|
3234
3259
|
}
|
|
3235
3260
|
async function configureOrm() {
|
|
3236
|
-
const db = await
|
|
3261
|
+
const db = await database2.get();
|
|
3237
3262
|
DrizzleAdapter.configure(db);
|
|
3238
3263
|
}
|
|
3239
3264
|
async function seedDatabase() {
|
|
3240
3265
|
if (!resolvedSeedersFolder) {
|
|
3241
3266
|
throw new Error('No seeders folder configured. Provide "seedersFolder" when calling createMySqlDatabase().');
|
|
3242
3267
|
}
|
|
3243
|
-
const db = await
|
|
3268
|
+
const db = await database2.get();
|
|
3244
3269
|
try {
|
|
3245
3270
|
await runSeeders(db, resolvedSeedersFolder);
|
|
3246
3271
|
} catch (error) {
|
|
@@ -3280,7 +3305,7 @@ function createMySqlDatabase(options) {
|
|
|
3280
3305
|
await adminDb.execute(sql.raw("SET FOREIGN_KEY_CHECKS = 1"));
|
|
3281
3306
|
}
|
|
3282
3307
|
});
|
|
3283
|
-
|
|
3308
|
+
migrations.reset();
|
|
3284
3309
|
}
|
|
3285
3310
|
async function migrationStatus() {
|
|
3286
3311
|
const localMigrations = listLocalMigrations(resolvedMigrationsFolder);
|
|
@@ -3300,8 +3325,8 @@ function createMySqlDatabase(options) {
|
|
|
3300
3325
|
return buildMigrationStatus(localMigrations, appliedRows);
|
|
3301
3326
|
}
|
|
3302
3327
|
return {
|
|
3303
|
-
getDatabase,
|
|
3304
|
-
migrateDatabase:
|
|
3328
|
+
getDatabase: database2.get,
|
|
3329
|
+
migrateDatabase: migrations.get,
|
|
3305
3330
|
closeDatabase,
|
|
3306
3331
|
configureOrm,
|
|
3307
3332
|
seedDatabase,
|
|
@@ -3325,17 +3350,14 @@ function createSqliteDatabase(options) {
|
|
|
3325
3350
|
const { migrationsFolder, filename, seedersFolder, relations } = options;
|
|
3326
3351
|
const resolvedMigrationsFolder = migrationsFolder instanceof URL ? fileURLToPath4(migrationsFolder) : resolve5(String(migrationsFolder));
|
|
3327
3352
|
const resolvedSeedersFolder = seedersFolder == null ? void 0 : seedersFolder instanceof URL ? fileURLToPath4(seedersFolder) : resolve5(String(seedersFolder));
|
|
3328
|
-
let db;
|
|
3329
3353
|
let sqliteClient;
|
|
3330
|
-
let migrationsPromise;
|
|
3331
3354
|
let activeKey;
|
|
3332
3355
|
const callSite = new Error().stack;
|
|
3333
3356
|
function resolveFilename() {
|
|
3334
3357
|
const value = typeof filename === "function" ? filename() : filename;
|
|
3335
3358
|
return value ?? process.env.DATABASE_URL ?? "./data/guren.db";
|
|
3336
3359
|
}
|
|
3337
|
-
|
|
3338
|
-
if (db) return db;
|
|
3360
|
+
const database2 = singleFlight(async () => {
|
|
3339
3361
|
const dbPath = resolveFilename();
|
|
3340
3362
|
const { mkdirSync } = await import("fs");
|
|
3341
3363
|
const { dirname } = await import("path");
|
|
@@ -3344,58 +3366,53 @@ function createSqliteDatabase(options) {
|
|
|
3344
3366
|
} catch {
|
|
3345
3367
|
}
|
|
3346
3368
|
const { Database } = await import("bun:sqlite");
|
|
3369
|
+
const { drizzle } = await import("drizzle-orm/bun-sqlite");
|
|
3347
3370
|
const sqlite = new Database(dbPath);
|
|
3348
3371
|
sqlite.exec("PRAGMA journal_mode = WAL;");
|
|
3349
3372
|
sqliteClient = sqlite;
|
|
3350
|
-
const
|
|
3351
|
-
const database2 = drizzle({ client: sqlite, ...relations ? { relations } : {} });
|
|
3352
|
-
db = database2;
|
|
3373
|
+
const handle = drizzle({ client: sqlite, ...relations ? { relations } : {} });
|
|
3353
3374
|
activeKey = isInMemory(dbPath) ? void 0 : hotReloadKey("sqlite", callSite, resolve5(dbPath));
|
|
3354
3375
|
if (activeKey) {
|
|
3355
3376
|
await replaceActiveConnection(activeKey, closeDatabase);
|
|
3356
3377
|
}
|
|
3357
|
-
return
|
|
3358
|
-
}
|
|
3378
|
+
return handle;
|
|
3379
|
+
});
|
|
3359
3380
|
async function closeDatabase() {
|
|
3360
|
-
if (!
|
|
3381
|
+
if (!sqliteClient) return;
|
|
3361
3382
|
const key = activeKey;
|
|
3362
3383
|
activeKey = void 0;
|
|
3363
3384
|
try {
|
|
3364
|
-
sqliteClient
|
|
3385
|
+
sqliteClient.close();
|
|
3365
3386
|
} finally {
|
|
3366
|
-
db = void 0;
|
|
3367
3387
|
sqliteClient = void 0;
|
|
3388
|
+
database2.reset();
|
|
3368
3389
|
if (key) {
|
|
3369
3390
|
releaseActiveConnection(key, closeDatabase);
|
|
3370
3391
|
}
|
|
3371
3392
|
}
|
|
3372
3393
|
}
|
|
3373
|
-
|
|
3374
|
-
|
|
3375
|
-
migrationsPromise = (async () => {
|
|
3394
|
+
const migrations = singleFlight(async () => {
|
|
3395
|
+
try {
|
|
3376
3396
|
if (!hasDrizzleMigrations(resolvedMigrationsFolder)) {
|
|
3377
3397
|
warnIgnoredFlatSqlMigrations(resolvedMigrationsFolder);
|
|
3378
3398
|
return;
|
|
3379
3399
|
}
|
|
3380
|
-
const
|
|
3400
|
+
const db = await database2.get();
|
|
3381
3401
|
const { migrate } = await import("drizzle-orm/bun-sqlite/migrator");
|
|
3382
|
-
await migrate(
|
|
3383
|
-
}
|
|
3384
|
-
migrationsPromise = migrationsPromise.catch((error) => {
|
|
3385
|
-
migrationsPromise = void 0;
|
|
3402
|
+
await migrate(db, { migrationsFolder: resolvedMigrationsFolder });
|
|
3403
|
+
} catch (error) {
|
|
3386
3404
|
throw migrationFailure(error);
|
|
3387
|
-
}
|
|
3388
|
-
|
|
3389
|
-
}
|
|
3405
|
+
}
|
|
3406
|
+
});
|
|
3390
3407
|
return {
|
|
3391
3408
|
async getDatabase() {
|
|
3392
|
-
await
|
|
3393
|
-
return
|
|
3409
|
+
await migrations.get();
|
|
3410
|
+
return database2.get();
|
|
3394
3411
|
},
|
|
3395
|
-
migrateDatabase:
|
|
3412
|
+
migrateDatabase: migrations.get,
|
|
3396
3413
|
closeDatabase,
|
|
3397
3414
|
async resetDatabase() {
|
|
3398
|
-
await
|
|
3415
|
+
await database2.get();
|
|
3399
3416
|
if (!sqliteClient) return;
|
|
3400
3417
|
const objects = sqliteClient.query(
|
|
3401
3418
|
"SELECT name, type FROM main.sqlite_master WHERE type IN ('table', 'view') AND name NOT LIKE 'sqlite\\_%' ESCAPE '\\'"
|
|
@@ -3409,12 +3426,12 @@ function createSqliteDatabase(options) {
|
|
|
3409
3426
|
} finally {
|
|
3410
3427
|
sqliteClient.exec("PRAGMA foreign_keys = ON;");
|
|
3411
3428
|
}
|
|
3412
|
-
|
|
3429
|
+
migrations.reset();
|
|
3413
3430
|
},
|
|
3414
3431
|
async migrationStatus() {
|
|
3415
3432
|
const localMigrations = listLocalMigrations(resolvedMigrationsFolder);
|
|
3416
3433
|
if (localMigrations.length === 0) return [];
|
|
3417
|
-
await
|
|
3434
|
+
await database2.get();
|
|
3418
3435
|
let appliedRows = [];
|
|
3419
3436
|
try {
|
|
3420
3437
|
const rows = sqliteClient?.query("SELECT name, applied_at FROM __drizzle_migrations").all();
|
|
@@ -3424,17 +3441,17 @@ function createSqliteDatabase(options) {
|
|
|
3424
3441
|
return buildMigrationStatus(localMigrations, appliedRows);
|
|
3425
3442
|
},
|
|
3426
3443
|
async configureOrm() {
|
|
3427
|
-
const
|
|
3428
|
-
await
|
|
3429
|
-
DrizzleAdapter.configure(
|
|
3444
|
+
const db = await database2.get();
|
|
3445
|
+
await migrations.get();
|
|
3446
|
+
DrizzleAdapter.configure(db);
|
|
3430
3447
|
},
|
|
3431
3448
|
async seedDatabase() {
|
|
3432
3449
|
if (!resolvedSeedersFolder) {
|
|
3433
3450
|
throw new Error('No seeders folder configured. Provide "seedersFolder" when calling createSqliteDatabase().');
|
|
3434
3451
|
}
|
|
3435
|
-
const
|
|
3452
|
+
const db = await database2.get();
|
|
3436
3453
|
try {
|
|
3437
|
-
await runSeeders(
|
|
3454
|
+
await runSeeders(db, resolvedSeedersFolder);
|
|
3438
3455
|
} catch (error) {
|
|
3439
3456
|
throw seedFailure(error);
|
|
3440
3457
|
}
|
|
@@ -3448,37 +3465,28 @@ import { fileURLToPath as fileURLToPath5 } from "url";
|
|
|
3448
3465
|
function createD1Database(options) {
|
|
3449
3466
|
const { binding, migrationsFolder, relations } = options;
|
|
3450
3467
|
const migrationsHint = migrationsFolder == null ? "db/migrations" : migrationsFolder instanceof URL ? relative(process.cwd(), fileURLToPath5(migrationsFolder)) : migrationsFolder;
|
|
3451
|
-
|
|
3452
|
-
|
|
3453
|
-
if (
|
|
3454
|
-
|
|
3455
|
-
|
|
3456
|
-
|
|
3457
|
-
|
|
3458
|
-
|
|
3459
|
-
|
|
3460
|
-
|
|
3461
|
-
const { drizzle } = await import("drizzle-orm/d1");
|
|
3462
|
-
return drizzle(client, relations ? { relations } : void 0);
|
|
3463
|
-
})();
|
|
3464
|
-
databasePromise = promise.catch((error) => {
|
|
3465
|
-
databasePromise = void 0;
|
|
3466
|
-
throw error;
|
|
3467
|
-
});
|
|
3468
|
-
return databasePromise;
|
|
3469
|
-
}
|
|
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
|
+
});
|
|
3470
3478
|
return {
|
|
3471
|
-
getDatabase:
|
|
3479
|
+
getDatabase: databaseHandle.get,
|
|
3472
3480
|
async migrateDatabase() {
|
|
3473
3481
|
throw new Error(
|
|
3474
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}.`
|
|
3475
3483
|
);
|
|
3476
3484
|
},
|
|
3477
3485
|
async closeDatabase() {
|
|
3478
|
-
|
|
3486
|
+
databaseHandle.reset();
|
|
3479
3487
|
},
|
|
3480
3488
|
async configureOrm() {
|
|
3481
|
-
const database2 = await
|
|
3489
|
+
const database2 = await databaseHandle.get();
|
|
3482
3490
|
DrizzleAdapter.configure(database2);
|
|
3483
3491
|
},
|
|
3484
3492
|
async seedDatabase() {
|
|
@@ -3520,8 +3528,6 @@ function createAwsDataApiDatabase(options) {
|
|
|
3520
3528
|
const { migrationsFolder, database: database2, resourceArn, secretArn, clientOptions, migrateOnStart, seedersFolder, relations } = options;
|
|
3521
3529
|
const resolvedMigrationsFolder = migrationsFolder instanceof URL ? fileURLToPath6(migrationsFolder) : resolve6(String(migrationsFolder));
|
|
3522
3530
|
const resolvedSeedersFolder = seedersFolder == null ? void 0 : seedersFolder instanceof URL ? fileURLToPath6(seedersFolder) : resolve6(String(seedersFolder));
|
|
3523
|
-
let migrationsPromise;
|
|
3524
|
-
let databasePromise;
|
|
3525
3531
|
let client;
|
|
3526
3532
|
let activeKey;
|
|
3527
3533
|
const callSite = new Error().stack;
|
|
@@ -3552,49 +3558,33 @@ function createAwsDataApiDatabase(options) {
|
|
|
3552
3558
|
...relations ? { relations } : {}
|
|
3553
3559
|
};
|
|
3554
3560
|
}
|
|
3555
|
-
|
|
3556
|
-
|
|
3557
|
-
return migrationsPromise;
|
|
3558
|
-
}
|
|
3559
|
-
const promise = (async () => {
|
|
3561
|
+
const migrations = singleFlight(async () => {
|
|
3562
|
+
try {
|
|
3560
3563
|
if (!hasDrizzleMigrations(resolvedMigrationsFolder)) {
|
|
3561
3564
|
warnIgnoredFlatSqlMigrations(resolvedMigrationsFolder);
|
|
3562
3565
|
return;
|
|
3563
3566
|
}
|
|
3564
3567
|
const { migrate } = await loadAwsDataApiModules();
|
|
3565
3568
|
await withAdminDb((db) => migrate(db, { migrationsFolder: resolvedMigrationsFolder }));
|
|
3566
|
-
}
|
|
3567
|
-
migrationsPromise = promise.catch((error) => {
|
|
3568
|
-
migrationsPromise = void 0;
|
|
3569
|
+
} catch (error) {
|
|
3569
3570
|
const reason = error instanceof Error ? error.message : String(error);
|
|
3570
3571
|
throw new Error(`Failed to run database migrations: ${reason}`);
|
|
3571
|
-
}
|
|
3572
|
-
|
|
3573
|
-
|
|
3574
|
-
|
|
3575
|
-
|
|
3576
|
-
|
|
3577
|
-
}
|
|
3578
|
-
const
|
|
3579
|
-
|
|
3580
|
-
|
|
3581
|
-
|
|
3582
|
-
|
|
3583
|
-
|
|
3584
|
-
|
|
3585
|
-
|
|
3586
|
-
|
|
3587
|
-
if (activeKey) {
|
|
3588
|
-
await replaceActiveConnection(activeKey, closeDatabase);
|
|
3589
|
-
}
|
|
3590
|
-
return db;
|
|
3591
|
-
})();
|
|
3592
|
-
databasePromise = promise.catch((error) => {
|
|
3593
|
-
databasePromise = void 0;
|
|
3594
|
-
throw error;
|
|
3595
|
-
});
|
|
3596
|
-
return databasePromise;
|
|
3597
|
-
}
|
|
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
|
+
});
|
|
3598
3588
|
async function closeDatabase() {
|
|
3599
3589
|
if (!client) {
|
|
3600
3590
|
return;
|
|
@@ -3605,21 +3595,21 @@ function createAwsDataApiDatabase(options) {
|
|
|
3605
3595
|
client.destroy?.();
|
|
3606
3596
|
} finally {
|
|
3607
3597
|
client = void 0;
|
|
3608
|
-
|
|
3598
|
+
databaseHandle.reset();
|
|
3609
3599
|
if (key) {
|
|
3610
3600
|
releaseActiveConnection(key, closeDatabase);
|
|
3611
3601
|
}
|
|
3612
3602
|
}
|
|
3613
3603
|
}
|
|
3614
3604
|
async function configureOrm() {
|
|
3615
|
-
const db = await
|
|
3605
|
+
const db = await databaseHandle.get();
|
|
3616
3606
|
DrizzleAdapter.configure(db);
|
|
3617
3607
|
}
|
|
3618
3608
|
async function seedDatabase() {
|
|
3619
3609
|
if (!resolvedSeedersFolder) {
|
|
3620
3610
|
throw new Error('No seeders folder configured. Provide "seedersFolder" when calling createAwsDataApiDatabase().');
|
|
3621
3611
|
}
|
|
3622
|
-
const db = await
|
|
3612
|
+
const db = await databaseHandle.get();
|
|
3623
3613
|
await runSeeders(db, resolvedSeedersFolder);
|
|
3624
3614
|
}
|
|
3625
3615
|
async function withAdminDb(callback) {
|
|
@@ -3638,7 +3628,7 @@ function createAwsDataApiDatabase(options) {
|
|
|
3638
3628
|
await adminDb.execute(sql.raw("CREATE SCHEMA public"));
|
|
3639
3629
|
await adminDb.execute(sql.raw("DROP SCHEMA IF EXISTS drizzle CASCADE"));
|
|
3640
3630
|
});
|
|
3641
|
-
|
|
3631
|
+
migrations.reset();
|
|
3642
3632
|
}
|
|
3643
3633
|
async function migrationStatus() {
|
|
3644
3634
|
const localMigrations = listLocalMigrations(resolvedMigrationsFolder);
|
|
@@ -3661,8 +3651,8 @@ function createAwsDataApiDatabase(options) {
|
|
|
3661
3651
|
return buildMigrationStatus(localMigrations, appliedRows);
|
|
3662
3652
|
}
|
|
3663
3653
|
return {
|
|
3664
|
-
getDatabase,
|
|
3665
|
-
migrateDatabase:
|
|
3654
|
+
getDatabase: databaseHandle.get,
|
|
3655
|
+
migrateDatabase: migrations.get,
|
|
3666
3656
|
closeDatabase,
|
|
3667
3657
|
configureOrm,
|
|
3668
3658
|
seedDatabase,
|