node-eventstore icon indicating copy to clipboard operation
node-eventstore copied to clipboard

Proposal - require dependency directly from databases instead of use `Store.use`

Open jacklam718 opened this issue 7 years ago • 20 comments

Proposal

require dependency directly from databases instead of use Store.use

For example: aws = require('aws-sdk); instead of aws = Store.use('aws-sdk'); I propose this idea because I have some issues.

Background

I using AWS Lambda with serverless and serverless-webpack I want to use eventstore with dynamodb from lambda but I got some errors. btw, I've installed aws-sdk and works on my local.

1. Implementation for db \"dynamodb\" does not exist!.

my code:

import eventstore from 'eventstore';
eventstore({ type: 'dynamodb' });

then I tried pass the DynamoDB class to eventstore, but I got another error Cannot find module 'aws-sdk' from parent my code:

import eventstore from 'eventstore';
import DynamoDB from 'eventstore/lib/databases/dynamodb';
eventstore({ type: DynamoDB });

Some of my thoughts

About the issue 1 and 2. I think it's because it cannot find modules via require after webpack bundled. https://github.com/adrai/node-eventstore/blob/master/index.js#L42 https://github.com/adrai/node-eventstore/blob/master/lib/databases/dynamodb.js#L2 https://github.com/adrai/node-eventstore/blob/master/lib/base.js#L172

So, that's why I propose this idea.

jacklam718 avatar Feb 20 '18 03:02 jacklam718

I could submit pull request if you agree my proposal.

jacklam718 avatar Feb 20 '18 03:02 jacklam718

Unfortunately this module was not designed to be used with webpack... the strategy here is to have the database "drivers" in control of the user. Another idea could be to have an "own" postinstall script that installs the dependency in the appropriate directory...

adrai avatar Feb 20 '18 07:02 adrai

Ok. Thanks for the quick reply.

jacklam718 avatar Feb 20 '18 09:02 jacklam718

But why not just require('aws-sdk') from dynamodb.js directly? It can solve this issue. And, I think serverless and serverless-webpack quite famous maybe other people will have same issue...

jacklam718 avatar Feb 20 '18 13:02 jacklam718

because of this: https://github.com/adrai/node-eventstore/blob/master/lib/base.js#L174 I've seen this even for prod environments

adrai avatar Feb 20 '18 13:02 adrai

What about do something like this?

dynamodb.js
var util = require('util');
var Store = require('../base');
var _ = require('lodash');
var async = require('async');
var dbg = require('debug');
try {
  var aws = require(aws-sdk);
} catch (e) {
  // workaround when `npm link`'ed for development
  var aws = prequire(aws-sdk);
}

jacklam718 avatar Feb 20 '18 13:02 jacklam718

Because I don't want other people like me. having same issue and spend few hours to investigate 😛 Really appreciate your quick reply 😀

jacklam718 avatar Feb 20 '18 13:02 jacklam718

For sure this will work, but than every implementation needs to do this... that's why we have moved this to a dedicated function.

If that is a concern of several people, we can do that.

The problem is that all cqrs relevant modules are constructed that way.

I also do not understand why it's necessary to use webpack in the backend.

In the mean time I would suggest you to use your fork (if you really need webpack)

adrai avatar Feb 20 '18 13:02 adrai

Because AWS Lambda doesn't support ES6. And, serverless-webpack is official plugin 😶

jacklam718 avatar Feb 20 '18 13:02 jacklam718

I know... https://twitter.com/adrirai/status/965700927670931456 but in my opinion... (really just my opinion) I would never transpile something in backend just because it's new and everyone is doing it in frontend... just wait until the real runtime is ready and available (then it's even faster and you need less code)

adrai avatar Feb 20 '18 14:02 adrai

btw, finally I solved the issue by using babel because babel only do transpile.

jacklam718 avatar Feb 22 '18 02:02 jacklam718

I had a similar problem with eventstore and it got solved by using webpack-node-externals.

eugenio79 avatar Jan 29 '19 10:01 eugenio79

@adrai Our main reason for using webpack is that we write our code in TypeScript. Having strongly typed code is even more useful in the backend than in the frontend. Basically we have four alternatives now:

  1. Try out a different transpiler to get TypeScript working - which might not actually solve the problem.
  2. Find a way to get this library to work with webpack - would only make sense if this is also in your interest
  3. Use a different library for CQRS - would be a shame, haven't found another good one yet
  4. Drop TypeScript and typesafety - this for us is definitely out, type safety was a giant win for our code base.

What's your opinion?

dbartholomae avatar Jun 03 '19 13:06 dbartholomae

@dbartholomae My honest opinion is: I have a "certain" opinion about TypeScript... but this is another story... I will personally not maintain any typescript types or files here... but as said before... this is just my opinion... What do you think @nanov?

adrai avatar Jun 03 '19 13:06 adrai

Don't worry, not talking about adding typings in here ;) Just about making it possible to use the library with a transpiler :)

dbartholomae avatar Jun 03 '19 18:06 dbartholomae

Just my two cents, I don‘t really get the problem to use the library with your favorite transpirier, be it rollup, webpack or parcel. All of them have settings and options which allow this, is the dynamic import the only issue here?

nanov avatar Jun 04 '19 17:06 nanov

So far we have multiple days just trying to set this up with webpack and don't see a light at the end of the tunnel yet :-/

dbartholomae avatar Jun 04 '19 17:06 dbartholomae

If you are not set on webpack, I would definitely give parcel a shot. If you supply some repo, I could take a look.

The thing is that, I guess your problems come from the dynamic imports, as such i‘m not sure that the proposal from this issue will solve them.

nanov avatar Jun 04 '19 17:06 nanov

I just had an idea, which might work for you, if we provide the ability to supply the library with the options so the implementation wont have to require it by itself ( dynamically ), will that work for you?

nanov avatar Jun 10 '19 05:06 nanov

We are currently in the works of getting the first prototype live. As soon as we have it (I assume next week) we will share the code base and some ideas for how it could be improved for our usage case :)

dbartholomae avatar Jun 11 '19 19:06 dbartholomae