10000 Adapter factory? · Issue #94 · okv/east · GitHub
[go: up one dir, main page]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapter factory? #94

Open
justinmchase opened this issue May 19, 2022 · 3 comments
Open

Adapter factory? #94

justinmchase opened this issue May 19, 2022 · 3 comments

Comments

@justinmchase
Copy link

I am trying to use east programmatically, I already have a mongo client and connection, but I can't figure out how to just provide that connection to east already.

const { mongo } = services // already configured and connected

 await migrationManager.configure({
  adapter: new MongoAdapter(mongo) // err
})
@justinmchase
Copy link
Author
justinmchase commented May 19, 2022

I'm not sure how to solve it per se but basically it would be nice if the adapter could accept a string, a class or a factory function which gives you the instance rather than relying on you to construct the adapter.

For example something like this may work:

const { mongo } = services // already configured and connected

 await migrationManager.configure({
  adapter: undefined,
  adapterFactory: () => new MongoAdapter(mongo)
})

@okv
Copy link
Owner
okv commented May 25, 2022

Hi, that configure method's adapter parameter can already be a function or a path to a module that exposes adapter's constructor. In case of the function adapter's constructor is expected to be returned. There is no way to pass an instance of an adapter because East fully controls adapter's lifecycle (connects it, disconnects it, etc).

@justinmchase
Copy link
Author

There is no way to pass an instance of an adapter because East fully controls adapter's lifecycle (connects it, disconnects it, etc).

Except when run programmatically in another application. I really need to control the construction specifically in that case.

I have a workaround fortunately:

  const { services } = context
  const { mongo } = services // already configured and connected

  class DynamicAdapter extends Adapter<TContext> {
      private migrations: Collection<IMigration>    
      public async connect(): Promise<TContext> {
        this.migrations = mongo.getCollection<IMigration>('migrations') // references mongo instance in higher scope
        return await Promise.resolve(context)
      }
      // etc...
  }

  await migrationManager.configure({
    adapter: DynamicAdapter
  })

I need to do this because I need more than just a database connection for migrations, I need a full context of my application, connections to multiple services, etc. For example, if you update a particular database record you may need to also push a message into a stream or queue or call a 3rd party api.

So anyway, I have a workaround but its inellegant, I think it would be very helpful to make it possible to accept adapter instances for programmatic uses rather than require a constructor specifically.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants
0