pmlopes / yoke

Yoke is a middleware framework for Vert.x
http://pmlopes.github.io/yoke/
Apache License 2.0
157 stars 44 forks source link

Yoke and MySQL #85

Closed majimboo closed 10 years ago

majimboo commented 10 years ago

How do I connect yoke with mysql. I found mod-mysql-gresql but I don't understand how to implement this into the code. Do you have nya example?

pmlopes commented 10 years ago

no, yoke has no dependency and/or preference about databases you can use the module you prefer all you need to do is get a reference to the eventbus and send a message, for example from your middleware:

eb.send("jdbc.module", "select * from users") { reply ->
  // do something...
}

Note this is a hypothetical piece of code you need to use the right message for the module you pick, as a shameless example look at my other project v-odm:

Here is some code from the unit tests, it shows the basic DB operations you can do:

https://github.com/pmlopes/v-odm/blob/master/src/test/java/com/jetdrone/odm/JdbcTest.java

so to put all together, create a middleware class:

class MyMiddleware extends Middleware {
    private EventBus eb;
    private JdbcPersons mapper;

    public MyMiddleware(EventBus eb) { this.eb = eb; mapper = new JdbcPersons(eb, "mysql.address");}

    public void handle(YokeRequest req) {
      // here DB stuff:
      mapper..count(new AsyncResultHandler < Number > () {
                                @Override
                                public void handle (AsyncResult<Number> count) {
                                    if (count.failed()) {
                                        fail(count.cause().getMessage());
                                        return;
                                    }

                                    Number c = count.result();

          ....

                                }
                            });
                        }