Datasources
uStart was designed to easily connect with multiple data sources.
Connection
Use ustart.connect()
method into config/datasources.js
script to specify your connections.
import { ustart } from "ustart";
ustart.connect(URI, options, ustartOptions);
Define URI as environmental variable inside the .env
file:
URI="[datasource]://user:pass@example.com:[port]/[dbname]"
That is the minimum you have to do in your code to establish a connection.
The second argument options
is passed as is to the underlying library: Sequelize or Mongoose.
The third argument ustartOptions
allows you to customize the behavior of the datasource managed by uStart class. At the moment the only option supported is enableMigration
that allows you to enable the migration for the datasource. Read more about it on Migrations section.
Relational Databases
uStart uses Sequelize v5 to connect with RDBMS. Sequelize is a promise-based Node.js ORM that supports 5 databases: Postgres, MySQL, MariaDB, SQLite and Microsoft SQL Server.
At the moment, uStart can use PostgreSQL and MariaDB databases. Support for MySQL, SQLite and Microsoft SQL Server will be added in follow updates.
To establish a connection, you have to install manually the driver for the database of your choice:
npm install --save pg pg-hstore
npm install --save mariadb
You don't have to install Sequelize, it is installed by default when you executed
npx ustart-cli init
.
Inside config/datasources.js
define your connections (you can use more than one):
import { ustart } from "ustart";
ustart.connect(process.env.PG_URI);
ustart.connect(process.env.MARIADB_URI);
Add the endpoints into .env
file:
PG_URI="postgres://user:pass@example.com:5432/dbname"
MARIADB_URI="mariadb://user:password@example.com:9821/database"
Note that
PG_URI
andMARIADB_URI
are just recommended names, use whatever you need.
More info about URI format on Sequelize dialects.
NoSQL
uStart uses Mongoose v5 to connect to MongoDB. Mongoose is installed by default and no extra driver is needed.
import { ustart } from "ustart";
ustart.connect(process.env.MONGO_URI, { useNewUrlParser: true });
in your .env
file:
MONGO_URI="mongodb://username:password@host:port/database?options..."
Note that
MONGO_URI
is just recommended name, use whatever.
More info about URI format on Mongoose connections.