2019-04-20 22:02:45 +00:00
|
|
|
# [greenlock-store-sequelize](https://git.rootprojects.org/root/greenlock-store-sequelize.js) | A [Root](https://rootprojects.org) project
|
2019-04-08 13:01:22 +00:00
|
|
|
|
|
|
|
> A database-driven Greenlock storage plugin with wildcard support.
|
|
|
|
|
2019-05-09 06:35:02 +00:00
|
|
|
## 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
|
|
|
|
|
2019-04-08 13:01:22 +00:00
|
|
|
## Usage
|
|
|
|
|
2019-05-09 06:35:02 +00:00
|
|
|
To use, provide this Greenlock storage plugin as the `store` option when you
|
|
|
|
invoke `create`:
|
2019-04-08 13:01:22 +00:00
|
|
|
|
|
|
|
```js
|
2019-05-09 06:35:02 +00:00
|
|
|
Greenlock.create({
|
|
|
|
store: require('greenlock-store-sequelize')
|
|
|
|
...
|
2019-04-08 13:01:22 +00:00
|
|
|
});
|
|
|
|
```
|
|
|
|
|
2019-04-10 14:17:15 +00:00
|
|
|
## Configuration
|
2019-04-08 13:01:22 +00:00
|
|
|
|
2019-05-09 07:11:10 +00:00
|
|
|
<details><summary>SQLite3 (default)</summary>
|
2019-04-08 13:01:22 +00:00
|
|
|
|
2019-05-09 06:35:02 +00:00
|
|
|
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):
|
2019-04-08 13:01:22 +00:00
|
|
|
|
2019-05-09 06:35:02 +00:00
|
|
|
```bash
|
|
|
|
npm install --save sqlite3
|
|
|
|
```
|
2019-04-10 14:11:38 +00:00
|
|
|
|
2019-05-09 06:35:02 +00:00
|
|
|
The default db file will be written wherever Greenlock's `configDir` is set to,
|
|
|
|
which is probably `~/acme` or `~/letsencrypt`.
|
2019-04-10 14:11:38 +00:00
|
|
|
|
2019-05-09 06:35:02 +00:00
|
|
|
```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 })
|
|
|
|
...
|
2019-04-10 14:11:38 +00:00
|
|
|
});
|
|
|
|
```
|
2019-05-09 07:11:10 +00:00
|
|
|
</details>
|
2019-04-10 14:11:38 +00:00
|
|
|
|
2019-05-09 07:25:32 +00:00
|
|
|
<details><summary>PostgreSQL, SQL Server, and lesser databases...</summary>
|
|
|
|
|
|
|
|
The general format of a DATABASE_URL is something like this:
|
|
|
|
|
|
|
|
> `schema://user:pass@server:port/service?option=foo`
|
|
|
|
|
|
|
|
For example:
|
|
|
|
|
|
|
|
> `postgres://aj:secret123@127.0.0.1:5432/greenlock`
|
|
|
|
|
|
|
|
For each database the exact format may be slightly different:
|
|
|
|
|
|
|
|
* `postgres://user:pass@hostname:port/database?option=foo`
|
2019-05-09 07:26:24 +00:00
|
|
|
* `sqlserver://user:pass@datasource:port/instance/catalog?database=dbname` (mssql)
|
2019-05-09 07:25:32 +00:00
|
|
|
* `mysql://user:pass@hostname:port/database?option=foo`
|
|
|
|
* `mariadb://user:pass@hostname:port/database?option=foo`
|
|
|
|
|
|
|
|
There's also a way to specify objects instead of using the standard connection strings.
|
|
|
|
|
|
|
|
See the next section for more information.
|
|
|
|
</details>
|
|
|
|
|
2019-05-09 07:11:10 +00:00
|
|
|
<details><summary>Database URLs / Connection Strings</summary>
|
2019-05-09 06:35:02 +00:00
|
|
|
You may use database URLs (also known as 'connection strings') to initialize sequelize:
|
|
|
|
|
|
|
|
```js
|
2019-05-09 07:07:16 +00:00
|
|
|
var dbUrl = 'postgres://user:pass@hostname:port/database';
|
2019-05-09 06:35:02 +00:00
|
|
|
|
|
|
|
Greenlock.create({
|
2019-05-09 07:07:16 +00:00
|
|
|
store: require('greenlock-store-sequelize').create({ storeDatabaseUrl: dbUrl })
|
2019-05-09 06:35:02 +00:00
|
|
|
...
|
|
|
|
});
|
|
|
|
```
|
|
|
|
|
|
|
|
If you need to use **custom options**, just instantiate sequelize directly:
|
|
|
|
|
|
|
|
```js
|
2019-05-09 07:07:16 +00:00
|
|
|
var Sequelize = require('sequelize');
|
|
|
|
var db = new Sequelize('postgres://user:pass@hostname:port/database');
|
2019-05-09 06:35:02 +00:00
|
|
|
|
|
|
|
Greenlock.create({
|
2019-05-09 07:07:16 +00:00
|
|
|
store: require('greenlock-store-sequelize').create({ db: db })
|
2019-05-09 06:35:02 +00:00
|
|
|
...
|
|
|
|
});
|
|
|
|
```
|
|
|
|
|
2019-05-09 07:25:32 +00:00
|
|
|
See the [Sequelize Getting Started](http://docs.sequelizejs.com/manual/getting-started.html) docs for more info
|
|
|
|
on database options for sequelize.
|
2019-05-09 07:11:10 +00:00
|
|
|
</details>
|
2019-05-09 06:35:02 +00:00
|
|
|
|
2019-05-09 07:11:10 +00:00
|
|
|
<details><summary>Environment variables (AWS, Docker, Heroku, Akkeris)</summary>
|
2019-05-09 06:35:02 +00:00
|
|
|
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 })
|
|
|
|
...
|
2019-04-10 14:11:38 +00:00
|
|
|
});
|
|
|
|
```
|
2019-05-09 07:11:10 +00:00
|
|
|
</details>
|
2019-04-08 13:01:22 +00:00
|
|
|
|
2019-05-09 07:11:10 +00:00
|
|
|
<details><summary>Table Prefixes</summary>
|
2019-05-09 06:35:02 +00:00
|
|
|
The default table names are as follows:
|
2019-04-10 14:17:15 +00:00
|
|
|
|
2019-05-09 06:35:02 +00:00
|
|
|
* Keypair
|
|
|
|
* Domain
|
|
|
|
* Certificate
|
|
|
|
* Chain
|
2019-04-10 14:17:15 +00:00
|
|
|
|
2019-05-09 06:35:02 +00:00
|
|
|
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, {
|
2019-05-09 07:04:08 +00:00
|
|
|
hooks: {
|
|
|
|
beforeDefine: function (columns, model) {
|
|
|
|
model.tableName = 'MyPrefix' + model.name.plural;
|
|
|
|
//model.schema = 'public';
|
2019-05-09 06:35:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2019-04-08 13:01:22 +00:00
|
|
|
|
2019-05-09 06:35:02 +00:00
|
|
|
Greenlock.create({
|
|
|
|
store: require('greenlock-store-sequelize').create({ db: db })
|
|
|
|
...
|
2019-04-10 14:17:15 +00:00
|
|
|
});
|
|
|
|
```
|
2019-05-09 07:11:10 +00:00
|
|
|
</details>
|
2019-05-09 06:48:57 +00:00
|
|
|
|
|
|
|
## Table Structure
|
|
|
|
|
|
|
|
This is the table structure that's created.
|
|
|
|
|
|
|
|
```sql
|
|
|
|
CREATE TABLE `Keypairs` (
|
|
|
|
`id` INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
|
|
`xid` VARCHAR(255) UNIQUE,
|
|
|
|
`content` TEXT,
|
|
|
|
`createdAt` DATETIME NOT NULL,
|
|
|
|
`updatedAt` DATETIME NOT NULL);
|
|
|
|
|
|
|
|
CREATE TABLE `Domains` (
|
|
|
|
`id` INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
|
|
`subject` VARCHAR(255) UNIQUE,
|
|
|
|
`altnames` TEXT,
|
|
|
|
`createdAt` DATETIME NOT NULL,
|
|
|
|
`updatedAt` DATETIME NOT NULL);
|
|
|
|
|
|
|
|
CREATE TABLE `Certificates` (
|
|
|
|
`id` INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
|
|
`subject` VARCHAR(255) UNIQUE,
|
|
|
|
`cert` TEXT,
|
|
|
|
`issuedAt` DATETIME,
|
|
|
|
`expiresAt` DATETIME,
|
|
|
|
`altnames` TEXT,
|
|
|
|
`chain` TEXT,
|
|
|
|
`createdAt` DATETIME NOT NULL,
|
|
|
|
`updatedAt` DATETIME NOT NULL);
|
|
|
|
|
|
|
|
CREATE TABLE `Chains` (
|
|
|
|
`id` INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
|
|
`xid` VARCHAR(255) UNIQUE,
|
|
|
|
`content` TEXT,
|
|
|
|
`createdAt` DATETIME NOT NULL,
|
|
|
|
`updatedAt` DATETIME NOT NULL,
|
|
|
|
`CertificateId` INTEGER REFERENCES
|
|
|
|
`Certificates` (`id`) ON DELETE SET NULL ON UPDATE CASCADE);
|
|
|
|
```
|