@spfn/core 0.2.0-beta.64 → 0.2.0-beta.67
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 +243 -72
- package/dist/db/index.js.map +1 -1
- package/dist/server/index.d.ts +8 -0
- package/package.json +10 -8
package/dist/db/index.js
CHANGED
|
@@ -441,8 +441,8 @@ async function createWriteReadClients(writeUrl, readUrl, poolConfig, retryConfig
|
|
|
441
441
|
try {
|
|
442
442
|
readClient = await createDatabaseConnection(readUrl, readPoolConfig, retryConfig);
|
|
443
443
|
return {
|
|
444
|
-
write: drizzle(writeClient),
|
|
445
|
-
read: drizzle(readClient),
|
|
444
|
+
write: drizzle({ client: writeClient }),
|
|
445
|
+
read: drizzle({ client: readClient }),
|
|
446
446
|
writeClient,
|
|
447
447
|
readClient
|
|
448
448
|
};
|
|
@@ -458,8 +458,8 @@ async function createWriteReadClients(writeUrl, readUrl, poolConfig, retryConfig
|
|
|
458
458
|
}
|
|
459
459
|
);
|
|
460
460
|
return {
|
|
461
|
-
write: drizzle(writeClient),
|
|
462
|
-
read: drizzle(writeClient),
|
|
461
|
+
write: drizzle({ client: writeClient }),
|
|
462
|
+
read: drizzle({ client: writeClient }),
|
|
463
463
|
writeClient,
|
|
464
464
|
readClient: writeClient
|
|
465
465
|
};
|
|
@@ -467,7 +467,7 @@ async function createWriteReadClients(writeUrl, readUrl, poolConfig, retryConfig
|
|
|
467
467
|
}
|
|
468
468
|
async function createSingleClient(url, poolConfig, retryConfig) {
|
|
469
469
|
const client = await createDatabaseConnection(url, poolConfig, retryConfig);
|
|
470
|
-
const db = drizzle(client);
|
|
470
|
+
const db = drizzle({ client });
|
|
471
471
|
return {
|
|
472
472
|
write: db,
|
|
473
473
|
read: db,
|
|
@@ -519,6 +519,11 @@ async function createDatabaseFromEnv(options) {
|
|
|
519
519
|
throw new Error("No database pattern detected despite passing config check");
|
|
520
520
|
}
|
|
521
521
|
|
|
522
|
+
// src/db/manager/path-utils.ts
|
|
523
|
+
function toPosixPath(path) {
|
|
524
|
+
return path.split("\\").join("/");
|
|
525
|
+
}
|
|
526
|
+
|
|
522
527
|
// src/db/manager/global-state.ts
|
|
523
528
|
var getWriteInstance = () => globalThis.__SPFN_DB_WRITE__;
|
|
524
529
|
var setWriteInstance = (instance) => {
|
|
@@ -528,6 +533,10 @@ var getReadInstance = () => globalThis.__SPFN_DB_READ__;
|
|
|
528
533
|
var setReadInstance = (instance) => {
|
|
529
534
|
globalThis.__SPFN_DB_READ__ = instance;
|
|
530
535
|
};
|
|
536
|
+
var getDatabaseProvider = () => globalThis.__SPFN_DB_PROVIDER__;
|
|
537
|
+
var setDatabaseProviderInstance = (provider) => {
|
|
538
|
+
globalThis.__SPFN_DB_PROVIDER__ = provider;
|
|
539
|
+
};
|
|
531
540
|
var getWriteClient = () => globalThis.__SPFN_DB_WRITE_CLIENT__;
|
|
532
541
|
var setWriteClient = (client) => {
|
|
533
542
|
globalThis.__SPFN_DB_WRITE_CLIENT__ = client;
|
|
@@ -547,6 +556,18 @@ var getInitOptions = () => globalThis.__SPFN_DB_INIT_OPTIONS__;
|
|
|
547
556
|
var setInitOptions = (options) => {
|
|
548
557
|
globalThis.__SPFN_DB_INIT_OPTIONS__ = options;
|
|
549
558
|
};
|
|
559
|
+
var getInitPromise = () => globalThis.__SPFN_DB_INIT_PROMISE__;
|
|
560
|
+
var setInitPromise = (promise) => {
|
|
561
|
+
globalThis.__SPFN_DB_INIT_PROMISE__ = promise;
|
|
562
|
+
};
|
|
563
|
+
var getInitProvider = () => globalThis.__SPFN_DB_INIT_PROVIDER__;
|
|
564
|
+
var setInitProvider = (provider) => {
|
|
565
|
+
globalThis.__SPFN_DB_INIT_PROVIDER__ = provider;
|
|
566
|
+
};
|
|
567
|
+
var getClosePromise = () => globalThis.__SPFN_DB_CLOSE_PROMISE__;
|
|
568
|
+
var setClosePromise = (promise) => {
|
|
569
|
+
globalThis.__SPFN_DB_CLOSE_PROMISE__ = promise;
|
|
570
|
+
};
|
|
550
571
|
var getIsClosing = () => globalThis.__SPFN_DB_CLOSING__ === true;
|
|
551
572
|
var setIsClosing = (closing) => {
|
|
552
573
|
globalThis.__SPFN_DB_CLOSING__ = closing;
|
|
@@ -641,6 +662,10 @@ function startHealthCheck(config, options, getDatabase2) {
|
|
|
641
662
|
setHealthCheckInterval(interval);
|
|
642
663
|
}
|
|
643
664
|
async function triggerForceReconnect(reason) {
|
|
665
|
+
if (getDatabaseProvider()) {
|
|
666
|
+
dbLogger3.debug("Force reconnect skipped: database is externally provided", { reason });
|
|
667
|
+
return false;
|
|
668
|
+
}
|
|
644
669
|
if (!getWriteInstance()) {
|
|
645
670
|
dbLogger3.warn("Force reconnect skipped: database not initialized", { reason });
|
|
646
671
|
return false;
|
|
@@ -655,6 +680,10 @@ async function triggerForceReconnect(reason) {
|
|
|
655
680
|
return await attemptReconnection(config, options, reason);
|
|
656
681
|
}
|
|
657
682
|
async function attemptReconnection(config, options, reason) {
|
|
683
|
+
if (getDatabaseProvider()) {
|
|
684
|
+
dbLogger3.debug("Reconnection skipped: database is externally provided", { reason });
|
|
685
|
+
return false;
|
|
686
|
+
}
|
|
658
687
|
if (isReconnecting) {
|
|
659
688
|
dbLogger3.debug("Reconnection coalesced: attempt already in progress", { reason });
|
|
660
689
|
return false;
|
|
@@ -714,7 +743,6 @@ var STACK_TRACE_PATTERNS = {
|
|
|
714
743
|
withParens: /\((.+):(\d+):(\d+)\)/,
|
|
715
744
|
withoutParens: /at (.+):(\d+):(\d+)/
|
|
716
745
|
};
|
|
717
|
-
var initPromise = null;
|
|
718
746
|
async function cleanupDatabaseConnections(writeClient, readClient) {
|
|
719
747
|
const cleanupPromises = [];
|
|
720
748
|
if (writeClient) {
|
|
@@ -752,13 +780,40 @@ async function testDatabaseConnections(write, read) {
|
|
|
752
780
|
}
|
|
753
781
|
}
|
|
754
782
|
}
|
|
783
|
+
function validateDatabaseProvider(provider) {
|
|
784
|
+
if (typeof provider.kind !== "string" || !provider.kind.trim()) {
|
|
785
|
+
throw new Error("Database provider kind must be a non-empty string");
|
|
786
|
+
}
|
|
787
|
+
}
|
|
788
|
+
function registerDatabaseProvider(provider) {
|
|
789
|
+
setDatabaseProviderInstance(provider);
|
|
790
|
+
setWriteInstance(provider.write);
|
|
791
|
+
setReadInstance(provider.read ?? provider.write);
|
|
792
|
+
return {
|
|
793
|
+
write: provider.write,
|
|
794
|
+
read: provider.read ?? provider.write
|
|
795
|
+
};
|
|
796
|
+
}
|
|
797
|
+
async function closeDatabaseProvider(provider, context) {
|
|
798
|
+
if (!provider.close) {
|
|
799
|
+
return;
|
|
800
|
+
}
|
|
801
|
+
try {
|
|
802
|
+
await provider.close();
|
|
803
|
+
} catch (error) {
|
|
804
|
+
const closeError = error instanceof Error ? error : new Error(String(error));
|
|
805
|
+
dbLogger4.error(`Error closing ${provider.kind || "unknown"} database provider`, closeError, {
|
|
806
|
+
context
|
|
807
|
+
});
|
|
808
|
+
}
|
|
809
|
+
}
|
|
755
810
|
function getCallerInfo() {
|
|
756
811
|
try {
|
|
757
812
|
const stack = new Error().stack;
|
|
758
813
|
if (!stack) return void 0;
|
|
759
814
|
const lines = stack.split("\n");
|
|
760
815
|
for (let i = STACK_TRACE_SKIP_LINES; i < lines.length; i++) {
|
|
761
|
-
const line = lines[i];
|
|
816
|
+
const line = toPosixPath(lines[i]);
|
|
762
817
|
if (!line.includes("node_modules") && !line.includes("/db/manager/")) {
|
|
763
818
|
const match = line.match(STACK_TRACE_PATTERNS.withParens) || line.match(STACK_TRACE_PATTERNS.withoutParens);
|
|
764
819
|
if (match) {
|
|
@@ -811,24 +866,107 @@ function getDatabase(type) {
|
|
|
811
866
|
return writeInst;
|
|
812
867
|
}
|
|
813
868
|
function setDatabase(write, read) {
|
|
869
|
+
if (getIsClosing()) {
|
|
870
|
+
throw new Error("Cannot set database while closing");
|
|
871
|
+
}
|
|
872
|
+
if (getInitPromise()) {
|
|
873
|
+
throw new Error("Cannot set database while initialization is in progress");
|
|
874
|
+
}
|
|
875
|
+
if (getDatabaseProvider()) {
|
|
876
|
+
throw new Error(
|
|
877
|
+
"An external database provider is active. Call closeDatabase() before setting a database manually."
|
|
878
|
+
);
|
|
879
|
+
}
|
|
880
|
+
if (getWriteClient() || getReadClient()) {
|
|
881
|
+
throw new Error(
|
|
882
|
+
"Managed database connections are active. Call closeDatabase() before setting a database manually."
|
|
883
|
+
);
|
|
884
|
+
}
|
|
885
|
+
setDatabaseProviderInstance(void 0);
|
|
814
886
|
setWriteInstance(write);
|
|
815
887
|
setReadInstance(read ?? write);
|
|
816
888
|
}
|
|
889
|
+
function setDatabaseProvider(provider) {
|
|
890
|
+
validateDatabaseProvider(provider);
|
|
891
|
+
const activeProvider = getDatabaseProvider();
|
|
892
|
+
if (getIsClosing()) {
|
|
893
|
+
throw new Error("Cannot set database provider while closing");
|
|
894
|
+
}
|
|
895
|
+
if (getInitPromise()) {
|
|
896
|
+
throw new Error("Cannot set database provider while initialization is in progress");
|
|
897
|
+
}
|
|
898
|
+
if (activeProvider === provider) {
|
|
899
|
+
return {
|
|
900
|
+
write: provider.write,
|
|
901
|
+
read: provider.read ?? provider.write
|
|
902
|
+
};
|
|
903
|
+
}
|
|
904
|
+
if (getWriteInstance() || getReadInstance() || getWriteClient() || getReadClient() || activeProvider) {
|
|
905
|
+
throw new Error(
|
|
906
|
+
"Database already initialized. Call closeDatabase() before registering a different provider."
|
|
907
|
+
);
|
|
908
|
+
}
|
|
909
|
+
return registerDatabaseProvider(provider);
|
|
910
|
+
}
|
|
817
911
|
async function initDatabase(options) {
|
|
818
912
|
if (getIsClosing()) {
|
|
819
913
|
throw new Error("Cannot initialize database while closing");
|
|
820
914
|
}
|
|
821
915
|
const writeInst = getWriteInstance();
|
|
822
916
|
if (writeInst) {
|
|
917
|
+
const activeProvider = getDatabaseProvider();
|
|
918
|
+
if (!activeProvider && !getWriteClient()) {
|
|
919
|
+
throw new Error(
|
|
920
|
+
"Database was registered manually. Use getDatabase() directly or call closeDatabase() before initDatabase()."
|
|
921
|
+
);
|
|
922
|
+
}
|
|
923
|
+
if (options?.provider !== activeProvider) {
|
|
924
|
+
const detail = options?.provider ? "a different provider was supplied" : "the active external provider was not supplied";
|
|
925
|
+
throw new Error(`Database already initialized: ${detail}`);
|
|
926
|
+
}
|
|
823
927
|
dbLogger4.debug("Database already initialized");
|
|
824
|
-
return {
|
|
928
|
+
return {
|
|
929
|
+
write: writeInst,
|
|
930
|
+
read: getReadInstance()
|
|
931
|
+
};
|
|
825
932
|
}
|
|
826
|
-
|
|
933
|
+
const activeInitPromise = getInitPromise();
|
|
934
|
+
if (activeInitPromise) {
|
|
935
|
+
if (options?.provider !== getInitProvider()) {
|
|
936
|
+
throw new Error("Database initialization already in progress with a different provider");
|
|
937
|
+
}
|
|
827
938
|
dbLogger4.debug("Database initialization in progress, waiting...");
|
|
828
|
-
return await
|
|
939
|
+
return await activeInitPromise;
|
|
829
940
|
}
|
|
830
|
-
|
|
941
|
+
setInitProvider(options?.provider);
|
|
942
|
+
const currentInit = (async () => {
|
|
831
943
|
try {
|
|
944
|
+
if (options?.provider) {
|
|
945
|
+
const provider = options.provider;
|
|
946
|
+
try {
|
|
947
|
+
validateDatabaseProvider(provider);
|
|
948
|
+
await testDatabaseConnections(provider.write, provider.read);
|
|
949
|
+
if (getIsClosing()) {
|
|
950
|
+
throw new Error("Database closed during initialization");
|
|
951
|
+
}
|
|
952
|
+
const monConfig2 = buildMonitoringConfig(options.monitoring);
|
|
953
|
+
const result2 = registerDatabaseProvider(provider);
|
|
954
|
+
setInitOptions(options);
|
|
955
|
+
setMonitoringConfig(monConfig2);
|
|
956
|
+
dbLogger4.info("Database provider connected", {
|
|
957
|
+
kind: provider.kind,
|
|
958
|
+
hasReplica: !!(provider.read && provider.read !== provider.write)
|
|
959
|
+
});
|
|
960
|
+
return result2;
|
|
961
|
+
} catch (error) {
|
|
962
|
+
await closeDatabaseProvider(provider, "initialization_failed");
|
|
963
|
+
if (error instanceof Error && (error.message === "Database provider kind must be a non-empty string" || error.message === "Database closed during initialization")) {
|
|
964
|
+
throw error;
|
|
965
|
+
}
|
|
966
|
+
const message = error instanceof Error ? error.message : "Unknown error";
|
|
967
|
+
throw new Error(`Database connection test failed: ${message}`);
|
|
968
|
+
}
|
|
969
|
+
}
|
|
832
970
|
const result = await createDatabaseFromEnv(options);
|
|
833
971
|
try {
|
|
834
972
|
await testDatabaseConnections(result.write, result.read);
|
|
@@ -865,56 +1003,78 @@ async function initDatabase(options) {
|
|
|
865
1003
|
}
|
|
866
1004
|
return { write: getWriteInstance(), read: getReadInstance() };
|
|
867
1005
|
} finally {
|
|
868
|
-
|
|
1006
|
+
setInitPromise(void 0);
|
|
1007
|
+
setInitProvider(void 0);
|
|
869
1008
|
}
|
|
870
1009
|
})();
|
|
871
|
-
|
|
1010
|
+
setInitPromise(currentInit);
|
|
1011
|
+
return await currentInit;
|
|
872
1012
|
}
|
|
873
1013
|
async function closeDatabase() {
|
|
874
|
-
|
|
1014
|
+
const activeClosePromise = getClosePromise();
|
|
1015
|
+
if (activeClosePromise) {
|
|
875
1016
|
dbLogger4.debug("Database close already in progress");
|
|
876
|
-
return;
|
|
1017
|
+
return await activeClosePromise;
|
|
877
1018
|
}
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
1019
|
+
const currentClose = (async () => {
|
|
1020
|
+
setIsClosing(true);
|
|
1021
|
+
const activeInitPromise = getInitPromise();
|
|
1022
|
+
if (activeInitPromise) {
|
|
1023
|
+
dbLogger4.debug("Waiting for database initialization to complete before closing...");
|
|
1024
|
+
try {
|
|
1025
|
+
await activeInitPromise;
|
|
1026
|
+
} catch (_error) {
|
|
1027
|
+
dbLogger4.debug("Initialization failed during close, proceeding with cleanup");
|
|
1028
|
+
}
|
|
885
1029
|
}
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
return;
|
|
893
|
-
}
|
|
894
|
-
try {
|
|
895
|
-
stopHealthCheck();
|
|
896
|
-
const closePromises = [];
|
|
897
|
-
const writeC = getWriteClient();
|
|
898
|
-
if (writeC) {
|
|
899
|
-
closePromises.push(closeDatabaseClient(writeC, "write"));
|
|
1030
|
+
const writeInst = getWriteInstance();
|
|
1031
|
+
const readInst = getReadInstance();
|
|
1032
|
+
const provider = getDatabaseProvider();
|
|
1033
|
+
if (!writeInst && !readInst && !provider) {
|
|
1034
|
+
dbLogger4.debug("No database connections to close");
|
|
1035
|
+
return;
|
|
900
1036
|
}
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
closePromises
|
|
1037
|
+
try {
|
|
1038
|
+
stopHealthCheck();
|
|
1039
|
+
const closePromises = [];
|
|
1040
|
+
if (provider) {
|
|
1041
|
+
closePromises.push(closeDatabaseProvider(provider, "shutdown"));
|
|
1042
|
+
}
|
|
1043
|
+
const writeC = getWriteClient();
|
|
1044
|
+
if (writeC) {
|
|
1045
|
+
closePromises.push(closeDatabaseClient(writeC, "write"));
|
|
1046
|
+
}
|
|
1047
|
+
const readC = getReadClient();
|
|
1048
|
+
if (readC && readC !== writeC) {
|
|
1049
|
+
closePromises.push(closeDatabaseClient(readC, "read"));
|
|
1050
|
+
}
|
|
1051
|
+
await Promise.allSettled(closePromises);
|
|
1052
|
+
dbLogger4.info("All database connections closed");
|
|
1053
|
+
} finally {
|
|
1054
|
+
setWriteInstance(void 0);
|
|
1055
|
+
setReadInstance(void 0);
|
|
1056
|
+
setWriteClient(void 0);
|
|
1057
|
+
setReadClient(void 0);
|
|
1058
|
+
setDatabaseProviderInstance(void 0);
|
|
1059
|
+
setMonitoringConfig(void 0);
|
|
1060
|
+
setInitOptions(void 0);
|
|
904
1061
|
}
|
|
905
|
-
|
|
906
|
-
|
|
1062
|
+
})();
|
|
1063
|
+
setClosePromise(currentClose);
|
|
1064
|
+
try {
|
|
1065
|
+
await currentClose;
|
|
907
1066
|
} finally {
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
setMonitoringConfig(void 0);
|
|
913
|
-
setInitOptions(void 0);
|
|
914
|
-
setIsClosing(false);
|
|
1067
|
+
if (getClosePromise() === currentClose) {
|
|
1068
|
+
setClosePromise(void 0);
|
|
1069
|
+
setIsClosing(false);
|
|
1070
|
+
}
|
|
915
1071
|
}
|
|
916
1072
|
}
|
|
917
1073
|
async function forceReconnectDatabase(reason = "manual") {
|
|
1074
|
+
if (getDatabaseProvider()) {
|
|
1075
|
+
dbLogger4.debug("Force reconnect skipped: database is externally provided", { reason });
|
|
1076
|
+
return false;
|
|
1077
|
+
}
|
|
918
1078
|
return await triggerForceReconnect(reason);
|
|
919
1079
|
}
|
|
920
1080
|
function getDatabaseInfo() {
|
|
@@ -923,7 +1083,8 @@ function getDatabaseInfo() {
|
|
|
923
1083
|
return {
|
|
924
1084
|
hasWrite: !!writeInst,
|
|
925
1085
|
hasRead: !!readInst,
|
|
926
|
-
isReplica: !!(readInst && readInst !== writeInst)
|
|
1086
|
+
isReplica: !!(readInst && readInst !== writeInst),
|
|
1087
|
+
providerKind: getDatabaseProvider()?.kind
|
|
927
1088
|
};
|
|
928
1089
|
}
|
|
929
1090
|
var dbLogger5 = logger.child("@spfn/core:database");
|
|
@@ -1047,17 +1208,15 @@ var BARREL_FILE_PATTERNS = [
|
|
|
1047
1208
|
"/index.ts",
|
|
1048
1209
|
"/index.js",
|
|
1049
1210
|
"/index.mjs",
|
|
1050
|
-
"
|
|
1051
|
-
"
|
|
1052
|
-
"
|
|
1053
|
-
"
|
|
1054
|
-
"\\config.ts",
|
|
1055
|
-
"\\config.js",
|
|
1056
|
-
"\\config.mjs"
|
|
1211
|
+
"/config",
|
|
1212
|
+
"/config.ts",
|
|
1213
|
+
"/config.js",
|
|
1214
|
+
"/config.mjs"
|
|
1057
1215
|
];
|
|
1058
1216
|
var SUPPORTED_EXTENSIONS = [".ts", ".js", ".mjs"];
|
|
1059
1217
|
function isBarrelFile(filePath) {
|
|
1060
|
-
|
|
1218
|
+
const posixPath = toPosixPath(filePath);
|
|
1219
|
+
return BARREL_FILE_PATTERNS.some((pattern) => posixPath.endsWith(pattern));
|
|
1061
1220
|
}
|
|
1062
1221
|
function isAbsolutePath(path) {
|
|
1063
1222
|
if (path.startsWith("/")) return true;
|
|
@@ -1227,7 +1386,7 @@ function getDrizzleConfig(options = {}) {
|
|
|
1227
1386
|
if (options.packageFilter) {
|
|
1228
1387
|
const packageSchemas2 = options.disablePackageDiscovery ? [] : discoverPackageSchemas(options.cwd ?? process.cwd());
|
|
1229
1388
|
const filteredSchemas = packageSchemas2.filter(
|
|
1230
|
-
(schemaPath) => schemaPath.includes(`node_modules/${options.packageFilter}/`)
|
|
1389
|
+
(schemaPath) => toPosixPath(schemaPath).includes(`node_modules/${options.packageFilter}/`)
|
|
1231
1390
|
);
|
|
1232
1391
|
if (filteredSchemas.length === 0) {
|
|
1233
1392
|
throw new Error(
|
|
@@ -1411,7 +1570,10 @@ function runWithTransaction(tx, txId, callback) {
|
|
|
1411
1570
|
txLogger.debug("Root transaction context set", { txId, level: newLevel });
|
|
1412
1571
|
}
|
|
1413
1572
|
const afterCommitCallbacks = existingContext ? existingContext.afterCommitCallbacks : [];
|
|
1414
|
-
return asyncContext.run(
|
|
1573
|
+
return asyncContext.run(
|
|
1574
|
+
{ tx, txId, level: newLevel, afterCommitCallbacks },
|
|
1575
|
+
callback
|
|
1576
|
+
);
|
|
1415
1577
|
}
|
|
1416
1578
|
function onAfterCommit(callback) {
|
|
1417
1579
|
const context = getTransactionContext();
|
|
@@ -1528,14 +1690,15 @@ async function runInTransaction(callback, options = {}) {
|
|
|
1528
1690
|
let afterCommitCallbacks = [];
|
|
1529
1691
|
try {
|
|
1530
1692
|
const result = await writeDb.transaction(async (tx) => {
|
|
1693
|
+
const transaction = tx;
|
|
1531
1694
|
if (timeout > 0 && !isNested) {
|
|
1532
|
-
await
|
|
1695
|
+
await transaction.execute(sql.raw(`SET LOCAL statement_timeout = ${timeout}`));
|
|
1533
1696
|
}
|
|
1534
1697
|
if (idleTimeout > 0 && !isNested) {
|
|
1535
|
-
await
|
|
1698
|
+
await transaction.execute(sql.raw(`SET LOCAL idle_in_transaction_session_timeout = ${idleTimeout}`));
|
|
1536
1699
|
}
|
|
1537
|
-
return await runWithTransaction(
|
|
1538
|
-
const innerResult = await callback(
|
|
1700
|
+
return await runWithTransaction(transaction, txId, async () => {
|
|
1701
|
+
const innerResult = await callback(transaction);
|
|
1539
1702
|
if (!isNested) {
|
|
1540
1703
|
const ctx = getTransactionContext();
|
|
1541
1704
|
if (ctx) {
|
|
@@ -1698,7 +1861,8 @@ async function create(table, data) {
|
|
|
1698
1861
|
if (!db) {
|
|
1699
1862
|
throw new Error("Database not initialized. Call initDatabase() first.");
|
|
1700
1863
|
}
|
|
1701
|
-
const
|
|
1864
|
+
const results = await db.insert(table).values(data).returning();
|
|
1865
|
+
const result = results[0];
|
|
1702
1866
|
return result;
|
|
1703
1867
|
}
|
|
1704
1868
|
async function createMany(table, data) {
|
|
@@ -1714,10 +1878,11 @@ async function upsert(table, data, options) {
|
|
|
1714
1878
|
if (!db) {
|
|
1715
1879
|
throw new Error("Database not initialized. Call initDatabase() first.");
|
|
1716
1880
|
}
|
|
1717
|
-
const
|
|
1881
|
+
const results = await db.insert(table).values(data).onConflictDoUpdate({
|
|
1718
1882
|
target: options.target,
|
|
1719
1883
|
set: options.set || data
|
|
1720
1884
|
}).returning();
|
|
1885
|
+
const result = results[0];
|
|
1721
1886
|
return result;
|
|
1722
1887
|
}
|
|
1723
1888
|
async function updateOne(table, where, data) {
|
|
@@ -1729,7 +1894,8 @@ async function updateOne(table, where, data) {
|
|
|
1729
1894
|
if (!whereClause) {
|
|
1730
1895
|
throw new Error("updateOne requires at least one where condition");
|
|
1731
1896
|
}
|
|
1732
|
-
const
|
|
1897
|
+
const results = await db.update(table).set(data).where(whereClause).returning();
|
|
1898
|
+
const result = results[0];
|
|
1733
1899
|
return result ?? null;
|
|
1734
1900
|
}
|
|
1735
1901
|
async function updateMany(table, where, data) {
|
|
@@ -1753,7 +1919,8 @@ async function deleteOne(table, where) {
|
|
|
1753
1919
|
if (!whereClause) {
|
|
1754
1920
|
throw new Error("deleteOne requires at least one where condition");
|
|
1755
1921
|
}
|
|
1756
|
-
const
|
|
1922
|
+
const results = await db.delete(table).where(whereClause).returning();
|
|
1923
|
+
const result = results[0];
|
|
1757
1924
|
return result ?? null;
|
|
1758
1925
|
}
|
|
1759
1926
|
async function deleteMany(table, where) {
|
|
@@ -2004,7 +2171,8 @@ var BaseRepository = class {
|
|
|
2004
2171
|
* ```
|
|
2005
2172
|
*/
|
|
2006
2173
|
async _create(table, data) {
|
|
2007
|
-
const
|
|
2174
|
+
const results = await this.db.insert(table).values(data).returning();
|
|
2175
|
+
const result = results[0];
|
|
2008
2176
|
return result;
|
|
2009
2177
|
}
|
|
2010
2178
|
/**
|
|
@@ -2046,10 +2214,11 @@ var BaseRepository = class {
|
|
|
2046
2214
|
* ```
|
|
2047
2215
|
*/
|
|
2048
2216
|
async _upsert(table, data, options) {
|
|
2049
|
-
const
|
|
2217
|
+
const results = await this.db.insert(table).values(data).onConflictDoUpdate({
|
|
2050
2218
|
target: options.target,
|
|
2051
2219
|
set: options.set || data
|
|
2052
2220
|
}).returning();
|
|
2221
|
+
const result = results[0];
|
|
2053
2222
|
return result;
|
|
2054
2223
|
}
|
|
2055
2224
|
/**
|
|
@@ -2073,7 +2242,8 @@ var BaseRepository = class {
|
|
|
2073
2242
|
if (!whereClause) {
|
|
2074
2243
|
throw new Error("updateOne requires at least one where condition");
|
|
2075
2244
|
}
|
|
2076
|
-
const
|
|
2245
|
+
const results = await this.db.update(table).set(data).where(whereClause).returning();
|
|
2246
|
+
const result = results[0];
|
|
2077
2247
|
return result ?? null;
|
|
2078
2248
|
}
|
|
2079
2249
|
/**
|
|
@@ -2117,7 +2287,8 @@ var BaseRepository = class {
|
|
|
2117
2287
|
if (!whereClause) {
|
|
2118
2288
|
throw new Error("deleteOne requires at least one where condition");
|
|
2119
2289
|
}
|
|
2120
|
-
const
|
|
2290
|
+
const results = await this.db.delete(table).where(whereClause).returning();
|
|
2291
|
+
const result = results[0];
|
|
2121
2292
|
return result ?? null;
|
|
2122
2293
|
}
|
|
2123
2294
|
/**
|
|
@@ -2166,6 +2337,6 @@ var BaseRepository = class {
|
|
|
2166
2337
|
}
|
|
2167
2338
|
};
|
|
2168
2339
|
|
|
2169
|
-
export { BaseRepository, RepositoryError, Transactional, 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 };
|
|
2340
|
+
export { BaseRepository, RepositoryError, Transactional, 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 };
|
|
2170
2341
|
//# sourceMappingURL=index.js.map
|
|
2171
2342
|
//# sourceMappingURL=index.js.map
|