-
Notifications
You must be signed in to change notification settings - Fork 8
Basics
Depending on the database you want to use you need to place the driver jar file into the "jars" subdirectory of ringo-sqlstore.
Setting up Ringo SQLstore is done in a few lines of code. Require the Store module, initialize a ConnectionPool with the connection parameters as argument and pass it to the Store constructor:
var {Store} = require("ringo-sqlstore");
var connectionPool = module.singleton("connectionPool", function() {
return Store.initConnectionPool({
"url": "jdbc:h2:mem:test",
"driver": "org.h2.Driver"
});
});
var store = new Store(connectionPool);
The above example would operate on a H2 in-memory database, which is created when the first connection to the database is opened. An example for an existing MySQL database "test" would look like this:
var connectionPool = module.singleton("connectionPool", function() {
return Store.initConnectionPool({
"url": "jdbc:mysql://localhost/test",
"driver": "com.mysql.jdbc.Driver",
"username": "test",
"password": "test"
});
});
var store = new Store(connectionPool);
Within one application you can instantiate as many stores as needed, each operating on it's own database. Once the store is set up correctly, you should continue defining entities managed by this store instance.
The above examples instantiate the connection pool as a singleton, meaning there will only be one single instance of it, regardless how many threads an application uses. This is important: if Ringo SQLstore is used in a multithreaded application, it is viable to use a singleton for the connection pool, otherwise every new thread started in the application would create its own pool, and basically render the connection pool useless.
The same applies to the two caches SQLstore can utilize. Read more about them in the caches section
Under the hood Ringo SQLStore uses HikariCP for connection pooling. All options passed to Store.initConnectionPool()
are passed to Hikari. For more information about available configuration options see HikariCP documentation.