@spfn/core 0.2.0-beta.64 → 0.2.0-beta.66
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/README.md +10 -8
- package/dist/db/index.d.ts +115 -77
- package/dist/db/index.js +230 -62
- package/dist/db/index.js.map +1 -1
- package/dist/server/index.d.ts +8 -0
- package/package.json +10 -8
package/README.md
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
# @spfn/core — Type-safe Next.js + Hono backend framework (route DSL → RPC proxy → typed client)
|
|
2
2
|
|
|
3
3
|
`@spfn/core` is the backend runtime for SPFN: a tRPC-style **route DSL** (TypeBox-validated,
|
|
4
|
-
end-to-end typed), a Drizzle
|
|
4
|
+
end-to-end typed), a PostgreSQL Drizzle **data layer** (postgres.js by default, injectable
|
|
5
|
+
providers such as PGlite), an HTTP **server** entry point, and a
|
|
5
6
|
Next.js **RPC proxy + typed client** that wires a browser/RSC app to that backend with
|
|
6
7
|
compile-time-only type inference.
|
|
7
8
|
|
|
@@ -13,23 +14,24 @@ end-to-end flow. Follow the links for API detail.
|
|
|
13
14
|
## Install
|
|
14
15
|
|
|
15
16
|
```bash
|
|
16
|
-
pnpm add @spfn/core
|
|
17
|
+
pnpm add @spfn/core drizzle-orm@1.0.0-rc.4
|
|
17
18
|
# peer (optional): next ^15 || ^16
|
|
18
19
|
# optional deps: ioredis (cache), ws (websocket events); pg-boss ships as a direct dep for jobs
|
|
19
20
|
```
|
|
20
21
|
|
|
21
22
|
Node `>=18.18.0`. ESM-only.
|
|
22
23
|
|
|
23
|
-
> **Apps that define entities must declare the Postgres driver directly:** add
|
|
24
|
-
> `postgres
|
|
25
|
-
>
|
|
26
|
-
> the app doesn't pin the same
|
|
27
|
-
> `BaseRepository` generics collapse to `unknown`, and RPC
|
|
24
|
+
> **Apps that define entities must declare Drizzle and the Postgres driver directly:** add
|
|
25
|
+
> `drizzle-orm@1.0.0-rc.4`, `postgres`, and `pg` to your app's `dependencies`.
|
|
26
|
+
> `@spfn/core` consumes Drizzle as a peer, and Drizzle branches its type resolution on
|
|
27
|
+
> optional driver peers. If the app doesn't pin the same ORM and drivers, pnpm can resolve
|
|
28
|
+
> a second Drizzle instance, `BaseRepository` generics collapse to `unknown`, and RPC
|
|
29
|
+
> responses lose their types.
|
|
28
30
|
> `spfn create` adds these automatically; declare them by hand only when wiring SPFN into
|
|
29
31
|
> an existing app.
|
|
30
32
|
>
|
|
31
33
|
> ```bash
|
|
32
|
-
> pnpm add postgres pg
|
|
34
|
+
> pnpm add drizzle-orm@1.0.0-rc.4 postgres pg
|
|
33
35
|
> ```
|
|
34
36
|
|
|
35
37
|
## Modules
|
package/dist/db/index.d.ts
CHANGED
|
@@ -1,12 +1,48 @@
|
|
|
1
1
|
import { PostgresJsDatabase } from 'drizzle-orm/postgres-js';
|
|
2
2
|
import postgres, { Sql } from 'postgres';
|
|
3
|
-
import * as drizzle_orm from 'drizzle-orm';
|
|
4
|
-
import { SQL } from 'drizzle-orm';
|
|
5
3
|
import * as drizzle_orm_pg_core from 'drizzle-orm/pg-core';
|
|
6
|
-
import { PgColumn, PgTable } from 'drizzle-orm/pg-core';
|
|
4
|
+
import { PgAsyncDatabase, PgAsyncTransaction, PgColumn, PgTable } from 'drizzle-orm/pg-core';
|
|
5
|
+
import * as drizzle_orm from 'drizzle-orm';
|
|
6
|
+
import { InferSelectModel, SQL, InferInsertModel, AnyRelations, EmptyRelations } from 'drizzle-orm';
|
|
7
7
|
import * as hono_types from 'hono/types';
|
|
8
8
|
import { DatabaseError } from '@spfn/core/errors';
|
|
9
9
|
|
|
10
|
+
/**
|
|
11
|
+
* Database Manager Types
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* DB connection type
|
|
16
|
+
*/
|
|
17
|
+
type DbConnectionType = 'read' | 'write';
|
|
18
|
+
/**
|
|
19
|
+
* Common base for PostgreSQL Drizzle drivers.
|
|
20
|
+
*
|
|
21
|
+
* Both the built-in postgres.js database and externally supplied drivers such
|
|
22
|
+
* as PGlite extend this class.
|
|
23
|
+
*/
|
|
24
|
+
type DrizzleDatabase = PgAsyncDatabase<any, any>;
|
|
25
|
+
/** Default database type used by the environment-backed postgres.js path. */
|
|
26
|
+
type DefaultDatabase = PostgresJsDatabase;
|
|
27
|
+
/** Resolve the transaction type belonging to a PostgreSQL Drizzle database. */
|
|
28
|
+
type DatabaseTransaction<TDatabase extends DrizzleDatabase> = TDatabase extends PgAsyncDatabase<infer TQueryResult, infer TRelations> ? PgAsyncTransaction<TQueryResult, TRelations> : never;
|
|
29
|
+
/**
|
|
30
|
+
* An externally owned PostgreSQL Drizzle database.
|
|
31
|
+
*
|
|
32
|
+
* The provider owns connection creation. SPFN calls `close` at most once when
|
|
33
|
+
* `closeDatabase()` or server shutdown releases the registered provider.
|
|
34
|
+
*/
|
|
35
|
+
interface DatabaseProvider<TDatabase extends DrizzleDatabase = DrizzleDatabase> {
|
|
36
|
+
/** Primary database for writes (and reads when no replica is supplied). */
|
|
37
|
+
write: TDatabase;
|
|
38
|
+
/** Optional read replica. Falls back to `write`. */
|
|
39
|
+
read?: TDatabase;
|
|
40
|
+
/** Driver identifier used for diagnostics, for example `pglite`. */
|
|
41
|
+
kind: string;
|
|
42
|
+
/** Release provider-owned resources. */
|
|
43
|
+
close?: () => void | Promise<void>;
|
|
44
|
+
}
|
|
45
|
+
|
|
10
46
|
/**
|
|
11
47
|
* Database Configuration
|
|
12
48
|
*
|
|
@@ -23,11 +59,11 @@ import { DatabaseError } from '@spfn/core/errors';
|
|
|
23
59
|
* - src/server/core/db/index.ts (main exports)
|
|
24
60
|
*/
|
|
25
61
|
|
|
26
|
-
interface DatabaseClients {
|
|
62
|
+
interface DatabaseClients<TDatabase extends DrizzleDatabase = PostgresJsDatabase> {
|
|
27
63
|
/** Primary database for writes (or both read/write if no replica) */
|
|
28
|
-
write?:
|
|
64
|
+
write?: TDatabase;
|
|
29
65
|
/** Replica database for reads (optional, falls back to write) */
|
|
30
|
-
read?:
|
|
66
|
+
read?: TDatabase;
|
|
31
67
|
/** Raw postgres client for write operations (for cleanup) */
|
|
32
68
|
writeClient?: Sql;
|
|
33
69
|
/** Raw postgres client for read operations (for cleanup) */
|
|
@@ -71,6 +107,17 @@ interface DatabaseOptions {
|
|
|
71
107
|
*/
|
|
72
108
|
monitoring?: Partial<MonitoringConfig>;
|
|
73
109
|
}
|
|
110
|
+
/** Database initialization options, including an optional external provider. */
|
|
111
|
+
interface DatabaseInitOptions<TDatabase extends DrizzleDatabase = PostgresJsDatabase> extends DatabaseOptions {
|
|
112
|
+
/**
|
|
113
|
+
* Externally owned PostgreSQL Drizzle provider.
|
|
114
|
+
*
|
|
115
|
+
* When set, environment-based postgres.js initialization, health checks,
|
|
116
|
+
* and automatic reconnect are skipped. The provider's `close` callback is
|
|
117
|
+
* used during shutdown.
|
|
118
|
+
*/
|
|
119
|
+
provider?: DatabaseProvider<TDatabase>;
|
|
120
|
+
}
|
|
74
121
|
/**
|
|
75
122
|
* Connection pool configuration
|
|
76
123
|
*
|
|
@@ -149,15 +196,6 @@ interface RetryConfig {
|
|
|
149
196
|
*/
|
|
150
197
|
declare function createDatabaseFromEnv(options?: DatabaseOptions): Promise<DatabaseClients>;
|
|
151
198
|
|
|
152
|
-
/**
|
|
153
|
-
* Database Manager Types
|
|
154
|
-
*/
|
|
155
|
-
|
|
156
|
-
/**
|
|
157
|
-
* DB connection type
|
|
158
|
-
*/
|
|
159
|
-
type DbConnectionType = 'read' | 'write';
|
|
160
|
-
|
|
161
199
|
/**
|
|
162
200
|
* Global Database instance manager
|
|
163
201
|
* Provides singleton access to database across all modules
|
|
@@ -184,7 +222,7 @@ type DbConnectionType = 'read' | 'write';
|
|
|
184
222
|
* const posts = await dbRead.select().from(postsTable);
|
|
185
223
|
* ```
|
|
186
224
|
*/
|
|
187
|
-
declare function getDatabase(type?: DbConnectionType):
|
|
225
|
+
declare function getDatabase<TDatabase extends DrizzleDatabase = DefaultDatabase>(type?: DbConnectionType): TDatabase;
|
|
188
226
|
/**
|
|
189
227
|
* Set global database instances (for testing or manual configuration)
|
|
190
228
|
*
|
|
@@ -214,9 +252,17 @@ declare function getDatabase(type?: DbConnectionType): PostgresJsDatabase<Record
|
|
|
214
252
|
* setDatabase(undefined, undefined);
|
|
215
253
|
* ```
|
|
216
254
|
*/
|
|
217
|
-
declare function setDatabase(write:
|
|
255
|
+
declare function setDatabase<TDatabase extends DrizzleDatabase = DefaultDatabase>(write: TDatabase | undefined, read?: TDatabase | undefined): void;
|
|
256
|
+
/**
|
|
257
|
+
* Register an externally owned PostgreSQL Drizzle provider.
|
|
258
|
+
*
|
|
259
|
+
* This is the synchronous/manual counterpart to `initDatabase({ provider })`.
|
|
260
|
+
* It performs no connection test. Use `closeDatabase()` to invoke the
|
|
261
|
+
* provider's close callback and clear the global instances.
|
|
262
|
+
*/
|
|
263
|
+
declare function setDatabaseProvider<TDatabase extends DrizzleDatabase>(provider: DatabaseProvider<TDatabase>): DatabaseClients<TDatabase>;
|
|
218
264
|
/**
|
|
219
|
-
* Initialize database from environment variables
|
|
265
|
+
* Initialize a database provider or create postgres.js clients from environment variables
|
|
220
266
|
* Automatically called by server startup
|
|
221
267
|
*
|
|
222
268
|
* Supported environment variables:
|
|
@@ -261,14 +307,11 @@ declare function setDatabase(write: PostgresJsDatabase<Record<string, unknown>>
|
|
|
261
307
|
* });
|
|
262
308
|
* ```
|
|
263
309
|
*/
|
|
264
|
-
declare function initDatabase(options?:
|
|
265
|
-
write?: PostgresJsDatabase<Record<string, unknown>>;
|
|
266
|
-
read?: PostgresJsDatabase<Record<string, unknown>>;
|
|
267
|
-
}>;
|
|
310
|
+
declare function initDatabase<TDatabase extends DrizzleDatabase = DefaultDatabase>(options?: DatabaseInitOptions<TDatabase>): Promise<DatabaseClients<TDatabase>>;
|
|
268
311
|
/**
|
|
269
|
-
* Close
|
|
312
|
+
* Close the active database provider or postgres.js connections and clean up
|
|
270
313
|
*
|
|
271
|
-
*
|
|
314
|
+
* Invokes an external provider's close callback, or closes postgres.js pools with timeout.
|
|
272
315
|
* Should be called during graceful shutdown or after tests.
|
|
273
316
|
*
|
|
274
317
|
* @example
|
|
@@ -351,6 +394,7 @@ declare function getDatabaseInfo(): {
|
|
|
351
394
|
hasWrite: boolean;
|
|
352
395
|
hasRead: boolean;
|
|
353
396
|
isReplica: boolean;
|
|
397
|
+
providerKind?: string;
|
|
354
398
|
};
|
|
355
399
|
|
|
356
400
|
/**
|
|
@@ -549,7 +593,7 @@ declare function generateDrizzleConfigFile(options?: DrizzleConfigOptions): stri
|
|
|
549
593
|
* });
|
|
550
594
|
* ```
|
|
551
595
|
*/
|
|
552
|
-
declare function id():
|
|
596
|
+
declare function id(): drizzle_orm_pg_core.SetIsPrimaryKey<drizzle_orm_pg_core.PgBigSerial53Builder>;
|
|
553
597
|
/**
|
|
554
598
|
* Standard timestamp fields (createdAt, updatedAt)
|
|
555
599
|
*
|
|
@@ -576,8 +620,8 @@ declare function id(): drizzle_orm.IsPrimaryKey<drizzle_orm.NotNull<drizzle_orm_
|
|
|
576
620
|
* ```
|
|
577
621
|
*/
|
|
578
622
|
declare function timestamps(): {
|
|
579
|
-
createdAt:
|
|
580
|
-
updatedAt:
|
|
623
|
+
createdAt: drizzle_orm_pg_core.SetNotNull<drizzle_orm_pg_core.SetHasDefault<drizzle_orm_pg_core.PgTimestampBuilder>>;
|
|
624
|
+
updatedAt: drizzle_orm_pg_core.SetNotNull<drizzle_orm_pg_core.SetHasDefault<drizzle_orm_pg_core.PgTimestampBuilder>>;
|
|
581
625
|
};
|
|
582
626
|
/**
|
|
583
627
|
* Foreign key reference to another table
|
|
@@ -602,7 +646,7 @@ declare function timestamps(): {
|
|
|
602
646
|
*/
|
|
603
647
|
declare function foreignKey<T extends PgColumn>(name: string, reference: () => T, options?: {
|
|
604
648
|
onDelete?: 'cascade' | 'set null' | 'restrict' | 'no action';
|
|
605
|
-
}):
|
|
649
|
+
}): drizzle_orm_pg_core.SetNotNull<drizzle_orm_pg_core.PgBigInt53Builder>;
|
|
606
650
|
/**
|
|
607
651
|
* Optional foreign key reference (nullable)
|
|
608
652
|
*
|
|
@@ -622,7 +666,7 @@ declare function foreignKey<T extends PgColumn>(name: string, reference: () => T
|
|
|
622
666
|
*/
|
|
623
667
|
declare function optionalForeignKey<T extends PgColumn>(name: string, reference: () => T, options?: {
|
|
624
668
|
onDelete?: 'cascade' | 'set null' | 'restrict' | 'no action';
|
|
625
|
-
}): drizzle_orm_pg_core.
|
|
669
|
+
}): drizzle_orm_pg_core.PgBigInt53Builder;
|
|
626
670
|
/**
|
|
627
671
|
* UUID primary key
|
|
628
672
|
*
|
|
@@ -640,7 +684,7 @@ declare function optionalForeignKey<T extends PgColumn>(name: string, reference:
|
|
|
640
684
|
* });
|
|
641
685
|
* ```
|
|
642
686
|
*/
|
|
643
|
-
declare function uuid():
|
|
687
|
+
declare function uuid(): drizzle_orm_pg_core.SetIsPrimaryKey<drizzle_orm_pg_core.SetHasDefault<drizzle_orm_pg_core.PgUUIDBuilder>>;
|
|
644
688
|
/**
|
|
645
689
|
* Audit fields for tracking record creators and updaters
|
|
646
690
|
*
|
|
@@ -666,8 +710,8 @@ declare function uuid(): drizzle_orm.IsPrimaryKey<drizzle_orm.NotNull<drizzle_or
|
|
|
666
710
|
* ```
|
|
667
711
|
*/
|
|
668
712
|
declare function auditFields(): {
|
|
669
|
-
createdBy: drizzle_orm_pg_core.
|
|
670
|
-
updatedBy: drizzle_orm_pg_core.
|
|
713
|
+
createdBy: drizzle_orm_pg_core.PgTextBuilder<[string, ...string[]]>;
|
|
714
|
+
updatedBy: drizzle_orm_pg_core.PgTextBuilder<[string, ...string[]]>;
|
|
671
715
|
};
|
|
672
716
|
/**
|
|
673
717
|
* Publishing fields for content management
|
|
@@ -698,8 +742,8 @@ declare function auditFields(): {
|
|
|
698
742
|
* ```
|
|
699
743
|
*/
|
|
700
744
|
declare function publishingFields(): {
|
|
701
|
-
publishedAt: drizzle_orm_pg_core.
|
|
702
|
-
publishedBy: drizzle_orm_pg_core.
|
|
745
|
+
publishedAt: drizzle_orm_pg_core.PgTimestampBuilder;
|
|
746
|
+
publishedBy: drizzle_orm_pg_core.PgTextBuilder<[string, ...string[]]>;
|
|
703
747
|
};
|
|
704
748
|
/**
|
|
705
749
|
* Custom verification timestamp field
|
|
@@ -728,7 +772,7 @@ declare function publishingFields(): {
|
|
|
728
772
|
* ```
|
|
729
773
|
*/
|
|
730
774
|
declare function verificationTimestamp(fieldName: string): {
|
|
731
|
-
[x: string]: drizzle_orm_pg_core.
|
|
775
|
+
[x: string]: drizzle_orm_pg_core.PgTimestampBuilder;
|
|
732
776
|
};
|
|
733
777
|
/**
|
|
734
778
|
* Soft delete fields
|
|
@@ -762,8 +806,8 @@ declare function verificationTimestamp(fieldName: string): {
|
|
|
762
806
|
* ```
|
|
763
807
|
*/
|
|
764
808
|
declare function softDelete(): {
|
|
765
|
-
deletedAt: drizzle_orm_pg_core.
|
|
766
|
-
deletedBy: drizzle_orm_pg_core.
|
|
809
|
+
deletedAt: drizzle_orm_pg_core.PgTimestampBuilder;
|
|
810
|
+
deletedBy: drizzle_orm_pg_core.PgTextBuilder<[string, ...string[]]>;
|
|
767
811
|
};
|
|
768
812
|
/**
|
|
769
813
|
* UTC timestamp field
|
|
@@ -795,7 +839,7 @@ declare function softDelete(): {
|
|
|
795
839
|
* });
|
|
796
840
|
* ```
|
|
797
841
|
*/
|
|
798
|
-
declare function utcTimestamp(fieldName: string, mode?: 'date' | 'string'): drizzle_orm_pg_core.
|
|
842
|
+
declare function utcTimestamp(fieldName: string, mode?: 'date' | 'string'): drizzle_orm_pg_core.PgTimestampBuilder;
|
|
799
843
|
/**
|
|
800
844
|
* Type-safe enum text field
|
|
801
845
|
*
|
|
@@ -829,7 +873,7 @@ declare function utcTimestamp(fieldName: string, mode?: 'date' | 'string'): driz
|
|
|
829
873
|
* });
|
|
830
874
|
* ```
|
|
831
875
|
*/
|
|
832
|
-
declare function enumText<T extends readonly [string, ...string[]]>(fieldName: string, values: T): drizzle_orm_pg_core.
|
|
876
|
+
declare function enumText<T extends readonly [string, ...string[]]>(fieldName: string, values: T): drizzle_orm_pg_core.PgTextBuilder<drizzle_orm.Writable<T & [string, ...string[]]>>;
|
|
833
877
|
/**
|
|
834
878
|
* Type-safe JSONB field
|
|
835
879
|
*
|
|
@@ -872,7 +916,7 @@ declare function enumText<T extends readonly [string, ...string[]]>(fieldName: s
|
|
|
872
916
|
* tags: typedJsonb<string[]>('tags').notNull(),
|
|
873
917
|
* ```
|
|
874
918
|
*/
|
|
875
|
-
declare function typedJsonb<T>(fieldName: string):
|
|
919
|
+
declare function typedJsonb<T>(fieldName: string): drizzle_orm_pg_core.Set$Type<drizzle_orm_pg_core.PgJsonbBuilder, T>;
|
|
876
920
|
|
|
877
921
|
/**
|
|
878
922
|
* Database Schema Helper
|
|
@@ -949,10 +993,9 @@ declare function getSchemaInfo(packageName: string): {
|
|
|
949
993
|
*/
|
|
950
994
|
|
|
951
995
|
/**
|
|
952
|
-
* Transaction database
|
|
953
|
-
* Uses Record<string, unknown> to accept any schema shape
|
|
996
|
+
* Transaction type belonging to a PostgreSQL Drizzle database.
|
|
954
997
|
*/
|
|
955
|
-
type TransactionDB =
|
|
998
|
+
type TransactionDB<TDatabase extends DrizzleDatabase = DefaultDatabase> = DatabaseTransaction<TDatabase>;
|
|
956
999
|
/**
|
|
957
1000
|
* afterCommit callback type
|
|
958
1001
|
*/
|
|
@@ -960,9 +1003,9 @@ type AfterCommitCallback = () => void | Promise<void>;
|
|
|
960
1003
|
/**
|
|
961
1004
|
* Transaction context stored in AsyncLocalStorage
|
|
962
1005
|
*/
|
|
963
|
-
type TransactionContext = {
|
|
1006
|
+
type TransactionContext<TDatabase extends DrizzleDatabase = DrizzleDatabase> = {
|
|
964
1007
|
/** The actual Drizzle transaction object */
|
|
965
|
-
tx: TransactionDB
|
|
1008
|
+
tx: TransactionDB<TDatabase>;
|
|
966
1009
|
/** Unique transaction ID for logging and tracing */
|
|
967
1010
|
txId: string;
|
|
968
1011
|
level: number;
|
|
@@ -974,13 +1017,13 @@ type TransactionContext = {
|
|
|
974
1017
|
*
|
|
975
1018
|
* @returns TransactionContext if available, null otherwise
|
|
976
1019
|
*/
|
|
977
|
-
declare function getTransactionContext(): TransactionContext | null;
|
|
1020
|
+
declare function getTransactionContext<TDatabase extends DrizzleDatabase = DefaultDatabase>(): TransactionContext<TDatabase> | null;
|
|
978
1021
|
/**
|
|
979
1022
|
* Get current transaction from AsyncLocalStorage
|
|
980
1023
|
*
|
|
981
1024
|
* @returns Transaction if available, null otherwise
|
|
982
1025
|
*/
|
|
983
|
-
declare function getTransaction(): TransactionDB | null;
|
|
1026
|
+
declare function getTransaction<TDatabase extends DrizzleDatabase = DefaultDatabase>(): TransactionDB<TDatabase> | null;
|
|
984
1027
|
/**
|
|
985
1028
|
* Run a function within a transaction context
|
|
986
1029
|
*
|
|
@@ -992,7 +1035,7 @@ declare function getTransaction(): TransactionDB | null;
|
|
|
992
1035
|
* @param callback - Function to run within transaction context
|
|
993
1036
|
* @returns Result of the callback
|
|
994
1037
|
*/
|
|
995
|
-
declare function runWithTransaction<T>(tx: TransactionDB
|
|
1038
|
+
declare function runWithTransaction<T, TDatabase extends DrizzleDatabase = DefaultDatabase>(tx: TransactionDB<TDatabase>, txId: string, // Add txId parameter
|
|
996
1039
|
callback: () => Promise<T>): Promise<T>;
|
|
997
1040
|
/**
|
|
998
1041
|
* Register a callback to run after the current transaction commits
|
|
@@ -1193,7 +1236,7 @@ interface RunInTransactionOptions {
|
|
|
1193
1236
|
* @throws TransactionError if database not initialized or timeout exceeded
|
|
1194
1237
|
* @throws Any error thrown by callback function
|
|
1195
1238
|
*/
|
|
1196
|
-
declare function runInTransaction<T>(callback: (tx: TransactionDB) => Promise<T>, options?: RunInTransactionOptions): Promise<T>;
|
|
1239
|
+
declare function runInTransaction<T, TDatabase extends DrizzleDatabase = DefaultDatabase>(callback: (tx: TransactionDB<TDatabase>) => Promise<T>, options?: RunInTransactionOptions): Promise<T>;
|
|
1197
1240
|
|
|
1198
1241
|
/**
|
|
1199
1242
|
* PostgreSQL Error Conversion Utilities
|
|
@@ -1247,14 +1290,6 @@ declare function fromPostgresError(error: any): DatabaseError;
|
|
|
1247
1290
|
* ```
|
|
1248
1291
|
*/
|
|
1249
1292
|
|
|
1250
|
-
/**
|
|
1251
|
-
* Infer SELECT model from PgTable
|
|
1252
|
-
*/
|
|
1253
|
-
type InferSelectModel<T extends PgTable> = T['$inferSelect'];
|
|
1254
|
-
/**
|
|
1255
|
-
* Infer INSERT model from PgTable
|
|
1256
|
-
*/
|
|
1257
|
-
type InferInsertModel<T extends PgTable> = T['$inferInsert'];
|
|
1258
1293
|
/**
|
|
1259
1294
|
* Object-based where condition (AND only, equality only)
|
|
1260
1295
|
*/
|
|
@@ -1519,10 +1554,10 @@ declare function count<T extends PgTable>(table: T, where?: WhereObject<InferSel
|
|
|
1519
1554
|
*
|
|
1520
1555
|
* @example With Custom Schema Type
|
|
1521
1556
|
* ```typescript
|
|
1522
|
-
* import type {
|
|
1557
|
+
* import type { AppRelations } from './relations';
|
|
1523
1558
|
*
|
|
1524
|
-
* export class UserRepository extends BaseRepository<
|
|
1525
|
-
* // Now this.db and this.readDb
|
|
1559
|
+
* export class UserRepository extends BaseRepository<AppRelations> {
|
|
1560
|
+
* // Now this.db and this.readDb preserve the app's Drizzle relations
|
|
1526
1561
|
* }
|
|
1527
1562
|
* ```
|
|
1528
1563
|
*
|
|
@@ -1542,6 +1577,8 @@ declare function count<T extends PgTable>(table: T, where?: WhereObject<InferSel
|
|
|
1542
1577
|
* ```
|
|
1543
1578
|
*/
|
|
1544
1579
|
|
|
1580
|
+
/** Database surface available to repositories inside and outside transactions. */
|
|
1581
|
+
type RepositoryDatabase<TDatabase extends DrizzleDatabase> = TDatabase extends PgAsyncDatabase<infer TQueryResult, infer TRelations> ? PgAsyncDatabase<TQueryResult, TRelations> : never;
|
|
1545
1582
|
/**
|
|
1546
1583
|
* Enhanced error class that includes repository context
|
|
1547
1584
|
*/
|
|
@@ -1563,9 +1600,10 @@ declare class RepositoryError extends Error {
|
|
|
1563
1600
|
* - Inside transaction: Uses transaction DB
|
|
1564
1601
|
* - Outside transaction: Uses global DB instance (with read/write separation)
|
|
1565
1602
|
*
|
|
1566
|
-
* @template
|
|
1603
|
+
* @template TRelations - Drizzle relations type (defaults to no relations)
|
|
1604
|
+
* @template TDatabase - PostgreSQL Drizzle driver type (defaults to postgres.js)
|
|
1567
1605
|
*/
|
|
1568
|
-
declare abstract class BaseRepository<
|
|
1606
|
+
declare abstract class BaseRepository<TRelations extends AnyRelations = EmptyRelations, TDatabase extends DrizzleDatabase = PostgresJsDatabase<TRelations>> {
|
|
1569
1607
|
/**
|
|
1570
1608
|
* Write database instance
|
|
1571
1609
|
*
|
|
@@ -1582,7 +1620,7 @@ declare abstract class BaseRepository<TSchema extends Record<string, unknown> =
|
|
|
1582
1620
|
* }
|
|
1583
1621
|
* ```
|
|
1584
1622
|
*/
|
|
1585
|
-
protected get db():
|
|
1623
|
+
protected get db(): RepositoryDatabase<TDatabase>;
|
|
1586
1624
|
/**
|
|
1587
1625
|
* Read database instance
|
|
1588
1626
|
*
|
|
@@ -1603,7 +1641,7 @@ declare abstract class BaseRepository<TSchema extends Record<string, unknown> =
|
|
|
1603
1641
|
* }
|
|
1604
1642
|
* ```
|
|
1605
1643
|
*/
|
|
1606
|
-
protected get readDb():
|
|
1644
|
+
protected get readDb(): RepositoryDatabase<TDatabase>;
|
|
1607
1645
|
/**
|
|
1608
1646
|
* Wrap query execution with repository context
|
|
1609
1647
|
*
|
|
@@ -1649,7 +1687,7 @@ declare abstract class BaseRepository<TSchema extends Record<string, unknown> =
|
|
|
1649
1687
|
* const user = await this._findOne(users, eq(users.id, 1));
|
|
1650
1688
|
* ```
|
|
1651
1689
|
*/
|
|
1652
|
-
protected _findOne<T extends PgTable>(table: T, where: Record<string, any> | SQL | undefined): Promise<T
|
|
1690
|
+
protected _findOne<T extends PgTable>(table: T, where: Record<string, any> | SQL | undefined): Promise<InferSelectModel<T> | null>;
|
|
1653
1691
|
/**
|
|
1654
1692
|
* Find multiple records
|
|
1655
1693
|
*
|
|
@@ -1671,7 +1709,7 @@ declare abstract class BaseRepository<TSchema extends Record<string, unknown> =
|
|
|
1671
1709
|
orderBy?: SQL | SQL[];
|
|
1672
1710
|
limit?: number;
|
|
1673
1711
|
offset?: number;
|
|
1674
|
-
}): Promise<T[
|
|
1712
|
+
}): Promise<InferSelectModel<T>[]>;
|
|
1675
1713
|
/**
|
|
1676
1714
|
* Keyset (cursor) pagination — O(limit) instead of OFFSET's O(offset).
|
|
1677
1715
|
*
|
|
@@ -1697,7 +1735,7 @@ declare abstract class BaseRepository<TSchema extends Record<string, unknown> =
|
|
|
1697
1735
|
after?: string | number | bigint | Date;
|
|
1698
1736
|
order?: 'asc' | 'desc';
|
|
1699
1737
|
where?: Record<string, any> | SQL | undefined;
|
|
1700
|
-
}): Promise<T[
|
|
1738
|
+
}): Promise<InferSelectModel<T>[]>;
|
|
1701
1739
|
/**
|
|
1702
1740
|
* Create a new record
|
|
1703
1741
|
*
|
|
@@ -1713,7 +1751,7 @@ declare abstract class BaseRepository<TSchema extends Record<string, unknown> =
|
|
|
1713
1751
|
* });
|
|
1714
1752
|
* ```
|
|
1715
1753
|
*/
|
|
1716
|
-
protected _create<T extends PgTable>(table: T, data: T
|
|
1754
|
+
protected _create<T extends PgTable>(table: T, data: InferInsertModel<T>): Promise<InferSelectModel<T>>;
|
|
1717
1755
|
/**
|
|
1718
1756
|
* Create multiple records
|
|
1719
1757
|
*
|
|
@@ -1729,7 +1767,7 @@ declare abstract class BaseRepository<TSchema extends Record<string, unknown> =
|
|
|
1729
1767
|
* ]);
|
|
1730
1768
|
* ```
|
|
1731
1769
|
*/
|
|
1732
|
-
protected _createMany<T extends PgTable>(table: T, data: T[
|
|
1770
|
+
protected _createMany<T extends PgTable>(table: T, data: InferInsertModel<T>[]): Promise<InferSelectModel<T>[]>;
|
|
1733
1771
|
/**
|
|
1734
1772
|
* Upsert a record (INSERT or UPDATE on conflict)
|
|
1735
1773
|
*
|
|
@@ -1749,10 +1787,10 @@ declare abstract class BaseRepository<TSchema extends Record<string, unknown> =
|
|
|
1749
1787
|
* });
|
|
1750
1788
|
* ```
|
|
1751
1789
|
*/
|
|
1752
|
-
protected _upsert<T extends PgTable>(table: T, data: T
|
|
1790
|
+
protected _upsert<T extends PgTable>(table: T, data: InferInsertModel<T>, options: {
|
|
1753
1791
|
target: PgColumn[];
|
|
1754
|
-
set?: Partial<T
|
|
1755
|
-
}): Promise<T
|
|
1792
|
+
set?: Partial<InferInsertModel<T>> | Record<string, SQL | any>;
|
|
1793
|
+
}): Promise<InferSelectModel<T>>;
|
|
1756
1794
|
/**
|
|
1757
1795
|
* Update a single record
|
|
1758
1796
|
*
|
|
@@ -1769,7 +1807,7 @@ declare abstract class BaseRepository<TSchema extends Record<string, unknown> =
|
|
|
1769
1807
|
* );
|
|
1770
1808
|
* ```
|
|
1771
1809
|
*/
|
|
1772
|
-
protected _updateOne<T extends PgTable>(table: T, where: Record<string, any> | SQL | undefined, data: Partial<T
|
|
1810
|
+
protected _updateOne<T extends PgTable>(table: T, where: Record<string, any> | SQL | undefined, data: Partial<InferInsertModel<T>>): Promise<InferSelectModel<T> | null>;
|
|
1773
1811
|
/**
|
|
1774
1812
|
* Update multiple records
|
|
1775
1813
|
*
|
|
@@ -1786,7 +1824,7 @@ declare abstract class BaseRepository<TSchema extends Record<string, unknown> =
|
|
|
1786
1824
|
* );
|
|
1787
1825
|
* ```
|
|
1788
1826
|
*/
|
|
1789
|
-
protected _updateMany<T extends PgTable>(table: T, where: Record<string, any> | SQL | undefined, data: Partial<T
|
|
1827
|
+
protected _updateMany<T extends PgTable>(table: T, where: Record<string, any> | SQL | undefined, data: Partial<InferInsertModel<T>>): Promise<InferSelectModel<T>[]>;
|
|
1790
1828
|
/**
|
|
1791
1829
|
* Delete a single record
|
|
1792
1830
|
*
|
|
@@ -1799,7 +1837,7 @@ declare abstract class BaseRepository<TSchema extends Record<string, unknown> =
|
|
|
1799
1837
|
* const user = await this._deleteOne(users, { id: 1 });
|
|
1800
1838
|
* ```
|
|
1801
1839
|
*/
|
|
1802
|
-
protected _deleteOne<T extends PgTable>(table: T, where: Record<string, any> | SQL | undefined): Promise<T
|
|
1840
|
+
protected _deleteOne<T extends PgTable>(table: T, where: Record<string, any> | SQL | undefined): Promise<InferSelectModel<T> | null>;
|
|
1803
1841
|
/**
|
|
1804
1842
|
* Delete multiple records
|
|
1805
1843
|
*
|
|
@@ -1812,7 +1850,7 @@ declare abstract class BaseRepository<TSchema extends Record<string, unknown> =
|
|
|
1812
1850
|
* const users = await this._deleteMany(users, { verified: false });
|
|
1813
1851
|
* ```
|
|
1814
1852
|
*/
|
|
1815
|
-
protected _deleteMany<T extends PgTable>(table: T, where: Record<string, any> | SQL | undefined): Promise<T[
|
|
1853
|
+
protected _deleteMany<T extends PgTable>(table: T, where: Record<string, any> | SQL | undefined): Promise<InferSelectModel<T>[]>;
|
|
1816
1854
|
/**
|
|
1817
1855
|
* Count records
|
|
1818
1856
|
*
|
|
@@ -1829,4 +1867,4 @@ declare abstract class BaseRepository<TSchema extends Record<string, unknown> =
|
|
|
1829
1867
|
protected _count<T extends PgTable>(table: T, where?: Record<string, any> | SQL | undefined): Promise<number>;
|
|
1830
1868
|
}
|
|
1831
1869
|
|
|
1832
|
-
export { type AfterCommitCallback, BaseRepository, type DatabaseClients, type DrizzleConfigOptions, type PoolConfig, RepositoryError, type RetryConfig, type RunInTransactionOptions, type TransactionContext, type TransactionDB, Transactional, type TransactionalOptions, auditFields, checkConnection, closeDatabase, count, create, createDatabaseConnection, createDatabaseFromEnv, createMany, createSchema, deleteMany, deleteOne, detectDialect, enumText, findMany, findOne, forceReconnectDatabase, foreignKey, fromPostgresError, generateDrizzleConfigFile, getDatabase, getDatabaseInfo, getDrizzleConfig, getSchemaInfo, getTransaction, getTransactionContext, id, initDatabase, isConnectionLevelError, onAfterCommit, optionalForeignKey, packageNameToSchema, publishingFields, reportDatabaseError, resetConnectionErrorCounter, runInTransaction, runWithTransaction, setDatabase, softDelete, timestamps, typedJsonb, updateMany, updateOne, upsert, utcTimestamp, uuid, verificationTimestamp };
|
|
1870
|
+
export { type AfterCommitCallback, BaseRepository, type DatabaseClients, type DatabaseInitOptions, type DatabaseOptions, type DatabaseProvider, type DatabaseTransaction, type DefaultDatabase, type DrizzleConfigOptions, type DrizzleDatabase, type PoolConfig, type RepositoryDatabase, RepositoryError, type RetryConfig, type RunInTransactionOptions, type TransactionContext, type TransactionDB, Transactional, type TransactionalOptions, auditFields, checkConnection, closeDatabase, count, create, createDatabaseConnection, createDatabaseFromEnv, createMany, createSchema, deleteMany, deleteOne, detectDialect, enumText, findMany, findOne, forceReconnectDatabase, foreignKey, fromPostgresError, generateDrizzleConfigFile, getDatabase, getDatabaseInfo, getDrizzleConfig, getSchemaInfo, getTransaction, getTransactionContext, id, initDatabase, isConnectionLevelError, onAfterCommit, optionalForeignKey, packageNameToSchema, publishingFields, reportDatabaseError, resetConnectionErrorCounter, runInTransaction, runWithTransaction, setDatabase, setDatabaseProvider, softDelete, timestamps, typedJsonb, updateMany, updateOne, upsert, utcTimestamp, uuid, verificationTimestamp };
|