@guren/orm 1.0.0-rc.17 → 1.0.0-rc.19
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 +14 -6
- package/dist/index.js +49 -23
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -195,11 +195,19 @@ declare class QueryBuilder<TRecord extends PlainObject = PlainObject, TResult ex
|
|
|
195
195
|
count(): Promise<number>;
|
|
196
196
|
/**
|
|
197
197
|
* Paginate the query results.
|
|
198
|
-
*
|
|
199
|
-
*
|
|
200
|
-
*
|
|
198
|
+
*
|
|
199
|
+
* Accepts either positional arguments or the same options object shape as
|
|
200
|
+
* `Model.paginate()` so the two APIs stay interchangeable:
|
|
201
|
+
*
|
|
202
|
+
* @example
|
|
203
|
+
* await Post.where('published', true).paginate(2, 10)
|
|
204
|
+
* await Post.where('published', true).paginate({ page: 2, perPage: 10 })
|
|
201
205
|
*/
|
|
202
206
|
paginate(page?: number, perPage?: number): Promise<PaginatedResult<TResult>>;
|
|
207
|
+
paginate(options: {
|
|
208
|
+
page?: number;
|
|
209
|
+
perPage?: number;
|
|
210
|
+
}): Promise<PaginatedResult<TResult>>;
|
|
203
211
|
/**
|
|
204
212
|
* Bulk update records matching the current conditions.
|
|
205
213
|
* @param data - Data to set on matching records
|
|
@@ -1189,9 +1197,9 @@ type RelationTypesFor<T extends typeof Model> = T extends {
|
|
|
1189
1197
|
type RelationKey<T extends typeof Model> = keyof RelationTypesFor<T> & string;
|
|
1190
1198
|
type RelationKeyOrString<T extends typeof Model> = RelationKey<T> extends never ? string : RelationKey<T>;
|
|
1191
1199
|
type RelationNameUnion<Names> = Names extends readonly (infer Items)[] ? Items : Names;
|
|
1192
|
-
type RelationTypePick<T extends typeof Model, Names> =
|
|
1193
|
-
[K in
|
|
1194
|
-
}
|
|
1200
|
+
type RelationTypePick<T extends typeof Model, Names> = {
|
|
1201
|
+
[K in RelationNameUnion<Names> & string & keyof RelationTypesFor<T>]: RelationTypesFor<T>[K];
|
|
1202
|
+
};
|
|
1195
1203
|
type RelationCountPick<Names> = {
|
|
1196
1204
|
[K in RelationNameUnion<Names> & string as `${K}Count`]: number;
|
|
1197
1205
|
};
|
package/dist/index.js
CHANGED
|
@@ -731,13 +731,9 @@ var QueryBuilder = class {
|
|
|
731
731
|
const results = await this.executeQuery();
|
|
732
732
|
return results.length;
|
|
733
733
|
}
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
* @param perPage - Records per page (default: 15)
|
|
738
|
-
* @returns Paginated result with data and metadata
|
|
739
|
-
*/
|
|
740
|
-
async paginate(page = 1, perPage = DEFAULT_PAGINATION_SIZE) {
|
|
734
|
+
async paginate(pageOrOptions = 1, perPageArg = DEFAULT_PAGINATION_SIZE) {
|
|
735
|
+
const page = typeof pageOrOptions === "object" ? pageOrOptions.page ?? 1 : pageOrOptions;
|
|
736
|
+
const perPage = typeof pageOrOptions === "object" ? pageOrOptions.perPage ?? DEFAULT_PAGINATION_SIZE : perPageArg;
|
|
741
737
|
const sanitizedPage = Number.isFinite(page) && page >= 1 ? Math.floor(page) : 1;
|
|
742
738
|
const sanitizedPerPage = Number.isFinite(perPage) && perPage >= 1 ? Math.floor(perPage) : DEFAULT_PAGINATION_SIZE;
|
|
743
739
|
const total = await this.count();
|
|
@@ -2566,11 +2562,8 @@ function SoftDeletes(Base) {
|
|
|
2566
2562
|
}
|
|
2567
2563
|
|
|
2568
2564
|
// src/postgres.ts
|
|
2569
|
-
import { drizzle } from "drizzle-orm/postgres-js";
|
|
2570
|
-
import { migrate } from "drizzle-orm/postgres-js/migrator";
|
|
2571
2565
|
import { resolve as resolve3 } from "path";
|
|
2572
2566
|
import { fileURLToPath as fileURLToPath2 } from "url";
|
|
2573
|
-
import postgres from "postgres";
|
|
2574
2567
|
|
|
2575
2568
|
// src/migration-utils.ts
|
|
2576
2569
|
import { existsSync, readdirSync } from "fs";
|
|
@@ -2694,6 +2687,21 @@ function defineSeeder(handler) {
|
|
|
2694
2687
|
}
|
|
2695
2688
|
|
|
2696
2689
|
// src/postgres.ts
|
|
2690
|
+
async function loadPostgresModules() {
|
|
2691
|
+
try {
|
|
2692
|
+
const [{ drizzle }, { migrate }, postgresModule] = await Promise.all([
|
|
2693
|
+
import("drizzle-orm/postgres-js"),
|
|
2694
|
+
import("drizzle-orm/postgres-js/migrator"),
|
|
2695
|
+
import("postgres")
|
|
2696
|
+
]);
|
|
2697
|
+
return { drizzle, migrate, postgres: postgresModule.default };
|
|
2698
|
+
} catch (error) {
|
|
2699
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
2700
|
+
throw new Error(
|
|
2701
|
+
`createPostgresDatabase() requires the "postgres" package. Install it with \`bun add postgres\`. (${reason})`
|
|
2702
|
+
);
|
|
2703
|
+
}
|
|
2704
|
+
}
|
|
2697
2705
|
function createPostgresDatabase(options) {
|
|
2698
2706
|
const { migrationsFolder, connectionString, clientOptions, seedersFolder, relations } = options;
|
|
2699
2707
|
const resolvedMigrationsFolder = migrationsFolder instanceof URL ? fileURLToPath2(migrationsFolder) : resolve3(String(migrationsFolder));
|
|
@@ -2718,8 +2726,9 @@ function createPostgresDatabase(options) {
|
|
|
2718
2726
|
warnIgnoredFlatSqlMigrations(resolvedMigrationsFolder);
|
|
2719
2727
|
return;
|
|
2720
2728
|
}
|
|
2729
|
+
const { drizzle, migrate, postgres: postgresFactory } = await loadPostgresModules();
|
|
2721
2730
|
const url = resolveConnectionString();
|
|
2722
|
-
const migrationClient =
|
|
2731
|
+
const migrationClient = postgresFactory(url, {
|
|
2723
2732
|
max: 1,
|
|
2724
2733
|
...clientOptions
|
|
2725
2734
|
});
|
|
@@ -2743,8 +2752,9 @@ function createPostgresDatabase(options) {
|
|
|
2743
2752
|
}
|
|
2744
2753
|
const promise = (async () => {
|
|
2745
2754
|
await migrateOnce();
|
|
2755
|
+
const { drizzle, postgres: postgresFactory } = await loadPostgresModules();
|
|
2746
2756
|
const url = resolveConnectionString();
|
|
2747
|
-
client =
|
|
2757
|
+
client = postgresFactory(url, {
|
|
2748
2758
|
max: 1,
|
|
2749
2759
|
...clientOptions
|
|
2750
2760
|
});
|
|
@@ -2776,7 +2786,8 @@ function createPostgresDatabase(options) {
|
|
|
2776
2786
|
await runSeeders(db, resolvedSeedersFolder);
|
|
2777
2787
|
}
|
|
2778
2788
|
async function withAdminClient(callback) {
|
|
2779
|
-
const
|
|
2789
|
+
const { postgres: postgresFactory } = await loadPostgresModules();
|
|
2790
|
+
const adminClient = postgresFactory(resolveConnectionString(), {
|
|
2780
2791
|
max: 1,
|
|
2781
2792
|
...clientOptions
|
|
2782
2793
|
});
|
|
@@ -2822,10 +2833,22 @@ function createPostgresDatabase(options) {
|
|
|
2822
2833
|
}
|
|
2823
2834
|
|
|
2824
2835
|
// src/mysql.ts
|
|
2825
|
-
import { drizzle as drizzle2 } from "drizzle-orm/mysql2";
|
|
2826
|
-
import { migrate as migrate2 } from "drizzle-orm/mysql2/migrator";
|
|
2827
2836
|
import { resolve as resolve4 } from "path";
|
|
2828
2837
|
import { fileURLToPath as fileURLToPath3 } from "url";
|
|
2838
|
+
async function loadMySqlModules() {
|
|
2839
|
+
try {
|
|
2840
|
+
const [{ drizzle }, { migrate }] = await Promise.all([
|
|
2841
|
+
import("drizzle-orm/mysql2"),
|
|
2842
|
+
import("drizzle-orm/mysql2/migrator")
|
|
2843
|
+
]);
|
|
2844
|
+
return { drizzle, migrate };
|
|
2845
|
+
} catch (error) {
|
|
2846
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
2847
|
+
throw new Error(
|
|
2848
|
+
`createMySqlDatabase() requires the "mysql2" package. Install it with \`bun add mysql2\`. (${reason})`
|
|
2849
|
+
);
|
|
2850
|
+
}
|
|
2851
|
+
}
|
|
2829
2852
|
function createMySqlDatabase(options) {
|
|
2830
2853
|
const { migrationsFolder, connectionString, clientOptions, seedersFolder, relations } = options;
|
|
2831
2854
|
const resolvedMigrationsFolder = migrationsFolder instanceof URL ? fileURLToPath3(migrationsFolder) : resolve4(String(migrationsFolder));
|
|
@@ -2850,8 +2873,9 @@ function createMySqlDatabase(options) {
|
|
|
2850
2873
|
warnIgnoredFlatSqlMigrations(resolvedMigrationsFolder);
|
|
2851
2874
|
return;
|
|
2852
2875
|
}
|
|
2876
|
+
const { drizzle, migrate } = await loadMySqlModules();
|
|
2853
2877
|
const url = resolveConnectionString();
|
|
2854
|
-
const migrationDb =
|
|
2878
|
+
const migrationDb = drizzle({
|
|
2855
2879
|
connection: {
|
|
2856
2880
|
uri: url,
|
|
2857
2881
|
...clientOptions
|
|
@@ -2860,7 +2884,7 @@ function createMySqlDatabase(options) {
|
|
|
2860
2884
|
...relations ? { relations } : {}
|
|
2861
2885
|
});
|
|
2862
2886
|
try {
|
|
2863
|
-
await
|
|
2887
|
+
await migrate(migrationDb, { migrationsFolder: resolvedMigrationsFolder });
|
|
2864
2888
|
} finally {
|
|
2865
2889
|
await closeClient(migrationDb);
|
|
2866
2890
|
}
|
|
@@ -2878,8 +2902,9 @@ function createMySqlDatabase(options) {
|
|
|
2878
2902
|
}
|
|
2879
2903
|
const promise = (async () => {
|
|
2880
2904
|
await migrateOnce();
|
|
2905
|
+
const { drizzle } = await loadMySqlModules();
|
|
2881
2906
|
const url = resolveConnectionString();
|
|
2882
|
-
const db =
|
|
2907
|
+
const db = drizzle({
|
|
2883
2908
|
connection: {
|
|
2884
2909
|
uri: url,
|
|
2885
2910
|
...clientOptions
|
|
@@ -2916,7 +2941,8 @@ function createMySqlDatabase(options) {
|
|
|
2916
2941
|
await runSeeders(db, resolvedSeedersFolder);
|
|
2917
2942
|
}
|
|
2918
2943
|
async function withAdminDb(callback) {
|
|
2919
|
-
const
|
|
2944
|
+
const { drizzle } = await loadMySqlModules();
|
|
2945
|
+
const adminDb = drizzle({
|
|
2920
2946
|
connection: {
|
|
2921
2947
|
uri: resolveConnectionString(),
|
|
2922
2948
|
...clientOptions
|
|
@@ -3028,8 +3054,8 @@ function createSqliteDatabase(options) {
|
|
|
3028
3054
|
const sqlite = new Database(dbPath);
|
|
3029
3055
|
sqlite.exec("PRAGMA journal_mode = WAL;");
|
|
3030
3056
|
sqliteClient = sqlite;
|
|
3031
|
-
const { drizzle
|
|
3032
|
-
db =
|
|
3057
|
+
const { drizzle } = await import("drizzle-orm/bun-sqlite");
|
|
3058
|
+
db = drizzle({ client: sqlite, ...relations ? { relations } : {} });
|
|
3033
3059
|
return db;
|
|
3034
3060
|
}
|
|
3035
3061
|
async function migrateOnce() {
|
|
@@ -3040,8 +3066,8 @@ function createSqliteDatabase(options) {
|
|
|
3040
3066
|
return;
|
|
3041
3067
|
}
|
|
3042
3068
|
const database2 = await ensureDatabase2();
|
|
3043
|
-
const { migrate
|
|
3044
|
-
await
|
|
3069
|
+
const { migrate } = await import("drizzle-orm/bun-sqlite/migrator");
|
|
3070
|
+
await migrate(database2, { migrationsFolder: resolvedMigrationsFolder });
|
|
3045
3071
|
})();
|
|
3046
3072
|
migrationsPromise = migrationsPromise.catch((error) => {
|
|
3047
3073
|
migrationsPromise = void 0;
|