Skip to content

Commit

Permalink
v3 (#255)
Browse files Browse the repository at this point in the history
  • Loading branch information
evantahler authored Jan 18, 2021
1 parent 6018bf2 commit 58f4fc6
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 76 deletions.
55 changes: 25 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ This plugin connects [Sequelize](https://sequelize.org/) and [Actionhero](https:

## Notes

- Versions `3.0.0+` of this package are only compatible with Actionhero versions `24.0.0+`, sequelize v6+ and sequelize-typescript v2+. Node.js v10+ is required.
- Versions `2.0.0+` of this package are only compatible with Actionhero versions `21.0.0+`.
- Versions `1.0.0+` of this package are only compatible with Actionhero versions `18.0.0+`.

Expand All @@ -17,9 +18,8 @@ For versions compatible with ActionHero versions prior to `18.0.0`, use version
## Setup

1. Install this plugin: `npm install ah-sequelize-plugin --save`
2. Add sequelize packages: `npm install sequelize@5 sequelize-typescript --save`
> !! Note that `sequelize-typescript` [currently requires sequelzie v5](https://github.com/actionhero/ah-sequelize-plugin/issues/172)
3. Add types and reflexive addons: `npm install @types/bluebird @types/validator reflect-metadata --save`
2. Add sequelize packages: `npm install sequelize@6 sequelize-typescript@2 --save`
3. Add types and reflexive addons: `npm install @types/validator reflect-metadata --save`
4. Add plugin to your project's `./src/config/plugins.ts`:

```ts
Expand Down Expand Up @@ -71,37 +71,34 @@ A `./src/config/sequelize.ts` will need to be created for your project. The exam

```javascript
import { URL } from "url";
import * as path from "path";
import { join } from "path";

export const DEFAULT = {
sequelize: (config) => {
let dialect = "postgres";
let host = "127.0.0.1";
let port = "5432";
let database = "actionhero";
let username = undefined;
let password = undefined;
let host = process.env.DB_HOST || "127.0.0.1";
let port = process.env.DB_PORT || "5432";
let database = `app_${process.env.NODE_ENV || "development"}`;
let username =
process.env.DB_USER || process.env.CI ? "postgres" : undefined;
let password = process.env.DB_PASS || undefined;

// if your environment provides database information via a single JDBC-style URL like mysql://username:password@hostname:port/default_schema
if (process.env.DATABASE_URL) {
const parsed = new URL(process.env.DATABASE_URL);
if (parsed.username) {
username = parsed.username;
}
if (parsed.password) {
password = parsed.password;
}
if (parsed.hostname) {
host = parsed.hostname;
}
if (parsed.port) {
port = parsed.port;
}
if (parsed.pathname) {
database = parsed.pathname.substring(1);
}
const connectionURL =
process.env.DATABASE_URL || process.env.MYSQL_URL || process.env.PG_URL;

if (connectionURL) {
const parsed = new URL(connectionURL);
if (parsed.protocol) dialect = parsed.protocol.slice(0, -1);
if (parsed.username) username = parsed.username;
if (parsed.password) password = parsed.password;
if (parsed.hostname) host = parsed.hostname;
if (parsed.port) port = parsed.port;
if (parsed.pathname) database = parsed.pathname.substring(1);
}

if (dialect === "postgresql") dialect = "postgres";

return {
autoMigrate: true,
logging: false,
Expand All @@ -111,15 +108,13 @@ export const DEFAULT = {
host: host,
username: username,
password: password,
models: [path.join(__dirname, "..", "models")],
migrations: [path.join(__dirname, "..", "migrations")],
models: [join(__dirname, "..", "models")],
migrations: [join(__dirname, "..", "migrations")],
// you can also pass "dialectOptions", for example if you need `{ssl: true}` for Postgres
};
},
};

module.exports.DEFAULT = DEFAULT;

// for the sequelize CLI tool
module.exports.development = DEFAULT.sequelize({
env: "development",
Expand Down
25 changes: 14 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 13 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name": "ah-sequelize-plugin",
"license": "Apache-2.0",
"description": "Use Sequelize in ActionHero",
"version": "2.2.4",
"version": "3.0.0",
"homepage": "http://actionherojs.com",
"repository": {
"type": "git",
Expand All @@ -26,26 +26,25 @@
"umzug": "v3.0.0-beta.5"
},
"devDependencies": {
"@types/bluebird": "^3.5.32",
"@types/jest": "^26.0.14",
"@types/node": "^14.11.8",
"@types/validator": "^13.1.0",
"@types/jest": "^26.0.20",
"@types/node": "^14.14.21",
"@types/validator": "^13.1.3",
"actionhero": "^25.0.1",
"bcrypt": "^5.0.0",
"jest": "^26.5.3",
"pg": "^8.4.1",
"prettier": "^2.1.2",
"jest": "^26.6.3",
"pg": "^8.5.1",
"prettier": "^2.2.1",
"reflect-metadata": "^0.1.13",
"sequelize": "^5.21.5",
"sequelize-typescript": "^1.0.0",
"ts-jest": "^26.4.1",
"ts-node-dev": "^1.0.0-pre.63",
"typescript": "^4.0.3"
"ts-jest": "^26.4.4",
"ts-node-dev": "^1.1.1",
"typescript": "^4.1.3"
},
"peerDependencies": {
"sequelize": ">=5.21.11",
"sequelize-typescript": ">=1.1.0",
"actionhero": ">=22.1.1"
"sequelize": ">=6.0.0",
"sequelize-typescript": ">=2.0.0",
"actionhero": ">=22.0.0"
},
"scripts": {
"dev": "ts-node-dev --transpile-only src/server.ts",
Expand Down
28 changes: 7 additions & 21 deletions src/config/sequelize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,15 @@ export const DEFAULT = {

if (connectionURL) {
const parsed = new URL(connectionURL);
if (parsed.protocol) {
dialect = parsed.protocol.slice(0, -1);
}
if (parsed.username) {
username = parsed.username;
}
if (parsed.password) {
password = parsed.password;
}
if (parsed.hostname) {
host = parsed.hostname;
}
if (parsed.port) {
port = parsed.port;
}
if (parsed.pathname) {
database = parsed.pathname.substring(1);
}
if (parsed.protocol) dialect = parsed.protocol.slice(0, -1);
if (parsed.username) username = parsed.username;
if (parsed.password) password = parsed.password;
if (parsed.hostname) host = parsed.hostname;
if (parsed.port) port = parsed.port;
if (parsed.pathname) database = parsed.pathname.substring(1);
}

if (dialect === "postgresql") {
dialect = "postgres";
}
if (dialect === "postgresql") dialect = "postgres";

return {
autoMigrate: true,
Expand Down

0 comments on commit 58f4fc6

Please sign in to comment.