@duraxis/nestjs 0.4.0 → 0.5.0
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/CHANGELOG.md +14 -1
- package/README.md +9 -8
- package/dist/duraxis.interfaces.d.ts +2 -2
- package/dist/duraxis.module.d.ts +4 -7
- package/dist/index.cjs +130 -107
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +132 -109
- package/dist/index.js.map +1 -1
- package/dist/transactions/transactional.instrumentation.d.ts +0 -1
- package/package.json +6 -6
package/dist/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { Global, Inject, Injectable, Module
|
|
1
|
+
import { Global, Inject, Injectable, Module } from "@nestjs/common";
|
|
2
2
|
import { DiscoveryModule, DiscoveryService } from "@nestjs/core";
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
3
|
+
import { DuraxisORM, QueryBuilder, discoverEntities } from "@duraxis/orm";
|
|
4
|
+
import { DuraxisDatabase, isDuraxisConfiguration, normalizeDuraxisConnection, selectDuraxisConnection } from "@duraxis/core";
|
|
5
5
|
import { AsyncLocalStorage } from "node:async_hooks";
|
|
6
6
|
// src/duraxis.tokens.ts
|
|
7
7
|
var DEFAULT_DURAXIS_CONNECTION = "default";
|
|
@@ -659,72 +659,25 @@ var TransactionManager = class {
|
|
|
659
659
|
if (options.connectionName != null) normalizeDuraxisConnectionName(options.connectionName);
|
|
660
660
|
}
|
|
661
661
|
};
|
|
662
|
-
// src/
|
|
663
|
-
var
|
|
664
|
-
function
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
}
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
"onModuleInit",
|
|
680
|
-
"onApplicationBootstrap",
|
|
681
|
-
"onModuleDestroy",
|
|
682
|
-
"beforeApplicationShutdown",
|
|
683
|
-
"onApplicationShutdown"
|
|
684
|
-
]);
|
|
685
|
-
function injectTransactionManagers(prototype) {
|
|
686
|
-
if (injectablePrototypes.has(prototype)) return;
|
|
687
|
-
injectablePrototypes.add(prototype);
|
|
688
|
-
Object.defineProperty(prototype, MANAGERS, { set(managers) {
|
|
689
|
-
bindings.set(this, managers);
|
|
690
|
-
instrumentTransactionalMethods(this);
|
|
691
|
-
} });
|
|
692
|
-
Inject(TransactionManagerRegistry)(prototype, MANAGERS);
|
|
693
|
-
}
|
|
694
|
-
function setTransactionalDefaultConnection(instance, name) {
|
|
695
|
-
defaultConnections.set(instance, name);
|
|
696
|
-
}
|
|
697
|
-
function wrapTransactionalMethod(method, property) {
|
|
698
|
-
if (wrappers.has(method)) return method;
|
|
699
|
-
const wrapped = function(...arguments_) {
|
|
700
|
-
const methodName = `${this.constructor.name}.${String(property)}`;
|
|
701
|
-
const managers = bindings.get(this);
|
|
702
|
-
if (!managers) throw new TransactionalProviderNotReadyError(methodName);
|
|
703
|
-
const options = transactionalOptions(this, wrapped) ?? {};
|
|
704
|
-
const name = normalizeDuraxisConnectionName(options.connectionName ?? defaultConnections.get(this));
|
|
705
|
-
return managers.get(name).execute(options, methodName, () => Reflect.apply(method, this, arguments_));
|
|
662
|
+
// src/duraxis.module.ts
|
|
663
|
+
var _DuraxisModule;
|
|
664
|
+
function isSharedConfiguration(options) {
|
|
665
|
+
return isDuraxisConfiguration(options) || "location" in options || "baseDirectory" in options || "migrations" in options || typeof options.autoLoadEntities === "object" || typeof options.entities === "function" || typeof options.driver === "function" && options.driver.length > 0;
|
|
666
|
+
}
|
|
667
|
+
async function sharedConnectionOptions(config, discovery, name) {
|
|
668
|
+
const normalized = normalizeDuraxisConnection(config);
|
|
669
|
+
const entities = await discoverEntities(config);
|
|
670
|
+
const features = discoverFeatureEntities(discovery, name);
|
|
671
|
+
if (config.autoLoadEntities && features.length && (features.length !== entities.length || features.some((entity) => !entities.includes(entity)))) throw new Error(`Duraxis entity discovery differs from forFeature registrations for connection ${name}. Align feature registrations and configured directories.`);
|
|
672
|
+
const driver = config.driver;
|
|
673
|
+
return {
|
|
674
|
+
driver: typeof driver === "function" ? () => driver(normalized.context) : driver,
|
|
675
|
+
database: normalized.database,
|
|
676
|
+
schema: normalized.schema,
|
|
677
|
+
entities,
|
|
678
|
+
closeStorage: config.closeStorage ?? typeof driver === "function"
|
|
706
679
|
};
|
|
707
|
-
for (const key of Reflect.getOwnMetadataKeys(method)) Reflect.defineMetadata(key, Reflect.getOwnMetadata(key, method), wrapped);
|
|
708
|
-
wrappers.add(wrapped);
|
|
709
|
-
return wrapped;
|
|
710
680
|
}
|
|
711
|
-
function instrumentTransactionalMethods(target) {
|
|
712
|
-
const visited = /* @__PURE__ */ new Set();
|
|
713
|
-
for (let prototype = target; prototype && prototype != Object.prototype; prototype = Object.getPrototypeOf(prototype)) for (const property of Reflect.ownKeys(prototype)) {
|
|
714
|
-
if (visited.has(property)) continue;
|
|
715
|
-
visited.add(property);
|
|
716
|
-
if (property == "constructor" || LIFECYCLE_METHODS.has(property)) continue;
|
|
717
|
-
const descriptor = Object.getOwnPropertyDescriptor(prototype, property);
|
|
718
|
-
const method = descriptor?.value;
|
|
719
|
-
if (typeof method != "function" || !transactionalOptions(target, method)) continue;
|
|
720
|
-
if (wrappers.has(method)) continue;
|
|
721
|
-
Object.defineProperty(target, property, {
|
|
722
|
-
...descriptor,
|
|
723
|
-
value: wrapTransactionalMethod(method, property)
|
|
724
|
-
});
|
|
725
|
-
}
|
|
726
|
-
}
|
|
727
|
-
// src/duraxis.module.ts
|
|
728
681
|
function rootProviders(name, optionsProvider) {
|
|
729
682
|
const connectionToken = getDuraxisConnectionToken(name);
|
|
730
683
|
const ormToken = getDuraxisOrmToken(name);
|
|
@@ -794,25 +747,10 @@ function rootModule(name, providers, imports = []) {
|
|
|
794
747
|
]
|
|
795
748
|
};
|
|
796
749
|
}
|
|
797
|
-
function featureModule(entities,
|
|
750
|
+
function featureModule(entities, connectionName) {
|
|
798
751
|
const name = normalizeDuraxisConnectionName(connectionName);
|
|
799
752
|
const uniqueEntities = [...new Set(entities)];
|
|
800
|
-
const uniqueServices = [...new Set(services)];
|
|
801
753
|
const repositoryProviders = uniqueEntities.map((entity) => repositoryProvider(entity, name));
|
|
802
|
-
const serviceProviders = uniqueServices.flatMap((service) => {
|
|
803
|
-
return [{
|
|
804
|
-
provide: service,
|
|
805
|
-
useClass: service,
|
|
806
|
-
scope: Scope.DEFAULT
|
|
807
|
-
}, {
|
|
808
|
-
provide: Symbol(`${service.name} default transaction connection`),
|
|
809
|
-
inject: [service],
|
|
810
|
-
useFactory: (instance) => {
|
|
811
|
-
setTransactionalDefaultConnection(instance, name);
|
|
812
|
-
return name;
|
|
813
|
-
}
|
|
814
|
-
}];
|
|
815
|
-
});
|
|
816
754
|
let DuraxisFeatureEntitiesMarker = class DuraxisFeatureEntitiesMarker {};
|
|
817
755
|
DuraxisFeatureEntitiesMarker = __decorate([DuraxisFeatureEntities({
|
|
818
756
|
connectionName: name,
|
|
@@ -820,12 +758,8 @@ function featureModule(entities, services, connectionName) {
|
|
|
820
758
|
})], DuraxisFeatureEntitiesMarker);
|
|
821
759
|
return {
|
|
822
760
|
module: DuraxisModule,
|
|
823
|
-
providers: [
|
|
824
|
-
|
|
825
|
-
...repositoryProviders,
|
|
826
|
-
...serviceProviders
|
|
827
|
-
],
|
|
828
|
-
exports: [...repositoryProviders.map((provider) => provider.provide), ...uniqueServices]
|
|
761
|
+
providers: [DuraxisFeatureEntitiesMarker, ...repositoryProviders],
|
|
762
|
+
exports: [...repositoryProviders.map((provider) => provider.provide)]
|
|
829
763
|
};
|
|
830
764
|
}
|
|
831
765
|
function repositoryProvider(entity, connectionName) {
|
|
@@ -835,16 +769,41 @@ function repositoryProvider(entity, connectionName) {
|
|
|
835
769
|
useFactory: (orm) => orm.getRepository(entity)
|
|
836
770
|
};
|
|
837
771
|
}
|
|
838
|
-
var DuraxisModule = class DuraxisModule {
|
|
772
|
+
var DuraxisModule = _DuraxisModule = class DuraxisModule {
|
|
839
773
|
static forRoot(options) {
|
|
840
|
-
|
|
774
|
+
if ("connections" in options) {
|
|
775
|
+
selectDuraxisConnection(options);
|
|
776
|
+
return {
|
|
777
|
+
module: _DuraxisModule,
|
|
778
|
+
imports: Object.keys(options.connections).sort().map((name) => {
|
|
779
|
+
const selected = selectDuraxisConnection(options, name);
|
|
780
|
+
return _DuraxisModule.forRoot({
|
|
781
|
+
...selected,
|
|
782
|
+
baseDirectory: selected.baseDirectory ?? normalizeDuraxisConnection(selected).context.root
|
|
783
|
+
});
|
|
784
|
+
}),
|
|
785
|
+
exports: [_DuraxisModule]
|
|
786
|
+
};
|
|
787
|
+
}
|
|
788
|
+
if (isSharedConfiguration(options)) {
|
|
789
|
+
const config = options;
|
|
790
|
+
normalizeDuraxisConnection(config);
|
|
791
|
+
const name = normalizeDuraxisConnectionName(config.name);
|
|
792
|
+
return rootModule(name, rootProviders(name, {
|
|
793
|
+
provide: getDuraxisOptionsToken(name),
|
|
794
|
+
inject: [DiscoveryService],
|
|
795
|
+
useFactory: (discovery) => sharedConnectionOptions(config, discovery, name)
|
|
796
|
+
}));
|
|
797
|
+
}
|
|
798
|
+
const rootOptions = options;
|
|
799
|
+
const name = normalizeDuraxisConnectionName(rootOptions.name);
|
|
841
800
|
const connectionOptions = {
|
|
842
|
-
driver:
|
|
843
|
-
...
|
|
844
|
-
...
|
|
845
|
-
...
|
|
846
|
-
...
|
|
847
|
-
...
|
|
801
|
+
driver: rootOptions.driver,
|
|
802
|
+
...rootOptions.entities == null ? {} : { entities: rootOptions.entities },
|
|
803
|
+
...rootOptions.autoLoadEntities == null ? {} : { autoLoadEntities: rootOptions.autoLoadEntities },
|
|
804
|
+
...rootOptions.database == null ? {} : { database: rootOptions.database },
|
|
805
|
+
...rootOptions.schema == null ? {} : { schema: rootOptions.schema },
|
|
806
|
+
...rootOptions.closeStorage == null ? {} : { closeStorage: rootOptions.closeStorage }
|
|
848
807
|
};
|
|
849
808
|
return rootModule(name, rootProviders(name, {
|
|
850
809
|
provide: getDuraxisOptionsToken(name),
|
|
@@ -855,23 +814,26 @@ var DuraxisModule = class DuraxisModule {
|
|
|
855
814
|
const name = normalizeDuraxisConnectionName(options.name);
|
|
856
815
|
return rootModule(name, rootProviders(name, {
|
|
857
816
|
provide: getDuraxisOptionsToken(name),
|
|
858
|
-
inject: options.inject
|
|
859
|
-
useFactory:
|
|
817
|
+
inject: [DiscoveryService, ...options.inject ?? []],
|
|
818
|
+
useFactory: async (discovery, ...args) => {
|
|
819
|
+
const config = await options.useFactory(...args);
|
|
820
|
+
if (isSharedConfiguration(config)) {
|
|
821
|
+
const shared = config;
|
|
822
|
+
if (shared.name && shared.name !== name) throw new Error("Async configuration connection name must match forRootAsync name");
|
|
823
|
+
return sharedConnectionOptions({
|
|
824
|
+
...shared,
|
|
825
|
+
name
|
|
826
|
+
}, discovery, name);
|
|
827
|
+
}
|
|
828
|
+
return config;
|
|
829
|
+
}
|
|
860
830
|
}), options.imports);
|
|
861
831
|
}
|
|
862
832
|
static forFeature(entities, connectionName) {
|
|
863
|
-
return featureModule(entities,
|
|
864
|
-
}
|
|
865
|
-
/** @deprecated Use forFeature(entities, connectionName) and regular module providers. */
|
|
866
|
-
static forTransactionalFeature(entities, providers, connectionName) {
|
|
867
|
-
return featureModule(entities, providers, connectionName);
|
|
868
|
-
}
|
|
869
|
-
/** @deprecated Declare services in the consuming module's providers array. */
|
|
870
|
-
static forTransactionalProviders(providers, connectionName) {
|
|
871
|
-
return featureModule([], providers, connectionName);
|
|
833
|
+
return featureModule(entities, connectionName);
|
|
872
834
|
}
|
|
873
835
|
};
|
|
874
|
-
DuraxisModule = __decorate([Module({})], DuraxisModule);
|
|
836
|
+
DuraxisModule = _DuraxisModule = __decorate([Module({})], DuraxisModule);
|
|
875
837
|
// src/duraxis.decorators.ts
|
|
876
838
|
function InjectDuraxisRepository(entity, connectionName) {
|
|
877
839
|
return Inject(getDuraxisRepositoryToken(entity, connectionName));
|
|
@@ -885,6 +847,67 @@ function InjectDuraxisDatabase(connectionName) {
|
|
|
885
847
|
function InjectDuraxisTransactionManager(connectionName) {
|
|
886
848
|
return Inject(getDuraxisTransactionManagerToken(connectionName));
|
|
887
849
|
}
|
|
850
|
+
// src/transactions/transactional.metadata.ts
|
|
851
|
+
var TRANSACTIONAL_METADATA = Symbol.for("@duraxis/nestjs/transactional");
|
|
852
|
+
function transactionalOptions(instance, method) {
|
|
853
|
+
const classOptions = Reflect.getMetadata(TRANSACTIONAL_METADATA, instance.constructor);
|
|
854
|
+
const methodOptions = Reflect.getMetadata(TRANSACTIONAL_METADATA, method);
|
|
855
|
+
return !classOptions && !methodOptions ? void 0 : {
|
|
856
|
+
...classOptions,
|
|
857
|
+
...methodOptions
|
|
858
|
+
};
|
|
859
|
+
}
|
|
860
|
+
// src/transactions/transactional.instrumentation.ts
|
|
861
|
+
var MANAGERS = Symbol("Duraxis transaction managers");
|
|
862
|
+
var bindings = /* @__PURE__ */ new WeakMap();
|
|
863
|
+
var injectablePrototypes = /* @__PURE__ */ new WeakSet();
|
|
864
|
+
var wrappers = /* @__PURE__ */ new WeakSet();
|
|
865
|
+
var LIFECYCLE_METHODS = /* @__PURE__ */ new Set([
|
|
866
|
+
"onModuleInit",
|
|
867
|
+
"onApplicationBootstrap",
|
|
868
|
+
"onModuleDestroy",
|
|
869
|
+
"beforeApplicationShutdown",
|
|
870
|
+
"onApplicationShutdown"
|
|
871
|
+
]);
|
|
872
|
+
function injectTransactionManagers(prototype) {
|
|
873
|
+
if (injectablePrototypes.has(prototype)) return;
|
|
874
|
+
injectablePrototypes.add(prototype);
|
|
875
|
+
Object.defineProperty(prototype, MANAGERS, { set(managers) {
|
|
876
|
+
bindings.set(this, managers);
|
|
877
|
+
instrumentTransactionalMethods(this);
|
|
878
|
+
} });
|
|
879
|
+
Inject(TransactionManagerRegistry)(prototype, MANAGERS);
|
|
880
|
+
}
|
|
881
|
+
function wrapTransactionalMethod(method, property) {
|
|
882
|
+
if (wrappers.has(method)) return method;
|
|
883
|
+
const wrapped = function(...arguments_) {
|
|
884
|
+
const methodName = `${this.constructor.name}.${String(property)}`;
|
|
885
|
+
const managers = bindings.get(this);
|
|
886
|
+
if (!managers) throw new TransactionalProviderNotReadyError(methodName);
|
|
887
|
+
const options = transactionalOptions(this, wrapped) ?? {};
|
|
888
|
+
const name = normalizeDuraxisConnectionName(options.connectionName);
|
|
889
|
+
return managers.get(name).execute(options, methodName, () => Reflect.apply(method, this, arguments_));
|
|
890
|
+
};
|
|
891
|
+
for (const key of Reflect.getOwnMetadataKeys(method)) Reflect.defineMetadata(key, Reflect.getOwnMetadata(key, method), wrapped);
|
|
892
|
+
wrappers.add(wrapped);
|
|
893
|
+
return wrapped;
|
|
894
|
+
}
|
|
895
|
+
function instrumentTransactionalMethods(target) {
|
|
896
|
+
const visited = /* @__PURE__ */ new Set();
|
|
897
|
+
for (let prototype = target; prototype && prototype != Object.prototype; prototype = Object.getPrototypeOf(prototype)) for (const property of Reflect.ownKeys(prototype)) {
|
|
898
|
+
if (visited.has(property)) continue;
|
|
899
|
+
visited.add(property);
|
|
900
|
+
if (property == "constructor" || LIFECYCLE_METHODS.has(property)) continue;
|
|
901
|
+
const descriptor = Object.getOwnPropertyDescriptor(prototype, property);
|
|
902
|
+
const method = descriptor?.value;
|
|
903
|
+
if (typeof method != "function" || !transactionalOptions(target, method)) continue;
|
|
904
|
+
if (wrappers.has(method)) continue;
|
|
905
|
+
Object.defineProperty(target, property, {
|
|
906
|
+
...descriptor,
|
|
907
|
+
value: wrapTransactionalMethod(method, property)
|
|
908
|
+
});
|
|
909
|
+
}
|
|
910
|
+
}
|
|
888
911
|
// src/transactions/transactional.decorator.ts
|
|
889
912
|
function Transactional(options = {}) {
|
|
890
913
|
return (target, _propertyKey, descriptor) => {
|