greenlock-store-sequelize.js/README.md

144 lines
3.5 KiB
Markdown

# [greenlock-store-sequelize](https://git.rootprojects.org/root/greenlock-store-sequelize.js) | A [Root](https://rootprojects.org) project
> A database-driven Greenlock storage plugin with wildcard support.
## Features
* Many [Supported SQL Databases](http://docs.sequelizejs.com/manual/getting-started.html)
* [x] PostgreSQL (**best**)
* [x] SQLite3 (**easiest**)
* [x] Microsoft SQL Server (mssql)
* [x] MySQL, MariaDB
* Works on all platforms
* [x] Mac, Linux, VPS
* [x] AWS, Heroku, Akkeris, Docker
* [x] Windows
## Usage
To use, provide this Greenlock storage plugin as the `store` option when you
invoke `create`:
```js
Greenlock.create({
store: require('greenlock-store-sequelize')
...
});
```
## Configuration
* SQLite3 (default)
* Database URLs / Connection Strings
* Environment variables
* Table Prefixes
### SQLite3 (default)
SQLite3 is the default database, however, since it has a large number of dependencies
and may require a native module to be built, you must explicitly install
[sqlite3](https://www.npmjs.com/package/sqlite3):
```bash
npm install --save sqlite3
```
The default db file will be written wherever Greenlock's `configDir` is set to,
which is probably `~/acme` or `~/letsencrypt`.
```bash
~/acme/db.sqlite3
```
If you wish to set special options you may do so by passing a pre-configured `Sequelize` instance:
```js
var Sequelize = require('sequelize');
var db = new Sequelize({ dialect: 'sqlite', storage: '/Users/me/acme/db.sqlite3' });
Greenlock.create({
store: require('greenlock-store-sequelize').create({ db: db })
...
});
```
### Database URL Connection String
You may use database URLs (also known as 'connection strings') to initialize sequelize:
```js
var Sequelize = require('sequelize');
var db = new Sequelize('postgres://user:pass@hostname:port/database');
Greenlock.create({
store: require('greenlock-store-sequelize').create({ db: db })
...
});
```
If you need to use **custom options**, just instantiate sequelize directly:
```js
var dbUrl = 'postgres://user:pass@hostname:port/database';
Greenlock.create({
store: require('greenlock-store-sequelize').create({ storeDatabaseUrl: dbUrl })
...
});
```
For more information, see the [Sequelize Getting Started](http://docs.sequelizejs.com/manual/getting-started.html) docs.
### ENVs (i.e. for Docker, Heroku, Akkeris)
If your database connection string is in an environment variable,
you would use the usual standard for your platform.
For example, if you're using Heroku, Akkeris, or Docker you're
database connection string is probably `DATABASE_URL`, so you'd do something like this:
```js
var Sequelize = require('sequelize');
var databaseUrl = process.env['DATABASE_URL'];
var db = new Sequelize(databaseUrl);
Greenlock.create({
store: require('greenlock-store-sequelize').create({ db: db })
...
});
```
### Table Prefixes
The default table names are as follows:
* Keypair
* Domain
* Certificate
* Chain
If you'd like to add a table name prefix or define a specific schema within the database (PostgreSQL, SQL Server),
you can do so like this:
```js
var Sequelize = require('sequelize');
var databaseUrl = process.env['DATABASE_URL'];
var db = new Sequelize(databaseUrl, {
define: {
hooks: {
beforeDefine: function (name, attrs) {
console.log(name);
console.log(attrs);
attrs.tableName = 'MyPrefix' + attrs.tableName;
//attrs.schema = 'public';
}
}
}
});
Greenlock.create({
store: require('greenlock-store-sequelize').create({ db: db })
...
});
```