Is there a way to create a global singleton and have it work with modules?
Use Case:
I have a secret store, and previously I was just initializing it in the root module and storing an instance in a way that I could reference it from anywhere.
// in the root module
@func() async foo(token: Secret) {
await Secrets.init(token)
return dag.container().with(doStuff())
}
// in another file
function doStuff() {
return container => container.withExec(['echo', Secrets.instance.get('name')])
}
That all worked fine when I was in the runtime of a single module.
Now I'm starting to split things up a bit and I'm running into issues.
@object
class Other {
@field() container: Container
constructor(container) {this.container = container)
@func() async foo() {
return this.container.with(doStuff())
}
}
@object()
class Root {
@func() async entry(token: Secret) {
await Secrets.init(token)
const container = dag.container().from('node:20').with(doStuff())
return new Other(container)
}
}
The global instance still works fine while it's executing the Root module, but when it starts executing Other it failes because Secrets.instance isn't set.
I could just change it so that I have to pass the instance into everything, but that gets tedius very fast since the majority of my with helpers and modules need access to Secrets.
I also ran into an issue when I passed the instance of Secrets to a different module as it was having trouble deserializing the object. (I used @object() on the Secrets class and marked its fields with @field() as well)
unexpected result value type string for object "MotionDaggerInfisical"
The other approach that I can think of would be to pass the token: Secret into every module and always just re-initialize it. My assumption would be that this would be the more "dagger", but I'm not quite sure.
Any suggestions?