diff --git a/README.md b/README.md index 5392b83f..9ec2e1be 100644 --- a/README.md +++ b/README.md @@ -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+`. @@ -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 @@ -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, @@ -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", diff --git a/package-lock.json b/package-lock.json index 78b27d36..caf5af8d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "ah-sequelize-plugin", - "version": "2.2.4", + "version": "3.0.0", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -916,12 +916,6 @@ "@babel/types": "^7.3.0" } }, - "@types/bluebird": { - "version": "3.5.33", - "resolved": "https://registry.npmjs.org/@types/bluebird/-/bluebird-3.5.33.tgz", - "integrity": "sha512-ndEo1xvnYeHxm7I/5sF6tBvnsA4Tdi3zj1keRKRs12SP+2ye2A27NDJ1B6PqkfMbGAcT+mqQVqbZRIrhfOp5PQ==", - "dev": true - }, "@types/color-name": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/@types/color-name/-/color-name-1.1.1.tgz", @@ -2978,7 +2972,8 @@ "version": "1.3.0", "resolved": "https://registry.npmjs.org/growly/-/growly-1.3.0.tgz", "integrity": "sha1-8QdIy+dq+WS3yWyTxrzCivEgwIE=", - "dev": true + "dev": true, + "optional": true }, "har-schema": { "version": "2.0.0", @@ -3476,6 +3471,7 @@ "resolved": "https://registry.npmjs.org/is-wsl/-/is-wsl-2.2.0.tgz", "integrity": "sha512-fKzAra0rGJUUBwGBgNkHZuToZcn+TtXHpeCgmkMJMMYx1sQDYaCSyjJBSCa2nH1DGm7s3n1oBnohoVTBaN7Lww==", "dev": true, + "optional": true, "requires": { "is-docker": "^2.0.0" } @@ -4961,6 +4957,7 @@ "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-6.0.0.tgz", "integrity": "sha512-Jo6dJ04CmSjuznwJSS3pUeWmd/H0ffTlkXXgwZi+eq1UCmqQwCh+eLsYOYCwY991i2Fah4h1BEMCx4qThGbsiA==", "dev": true, + "optional": true, "requires": { "yallist": "^4.0.0" }, @@ -4969,7 +4966,8 @@ "version": "4.0.0", "resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz", "integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==", - "dev": true + "dev": true, + "optional": true } } }, @@ -5317,6 +5315,7 @@ "resolved": "https://registry.npmjs.org/node-notifier/-/node-notifier-8.0.1.tgz", "integrity": "sha512-BvEXF+UmsnAfYfoapKM9nGxnP+Wn7P91YfXmrKnfcYCx6VBeoN5Ez5Ogck6I8Bi5k4RlpqRYaw75pAwzX9OphA==", "dev": true, + "optional": true, "requires": { "growly": "^1.3.0", "is-wsl": "^2.2.0", @@ -5331,6 +5330,7 @@ "resolved": "https://registry.npmjs.org/semver/-/semver-7.3.4.tgz", "integrity": "sha512-tCfb2WLjqFAtXn4KEdxIhalnRtoKFN7nAwj0B3ZXCbQloV2tq5eDbcTmT68JJD3nRJq24/XgxtQKFIpQdtvmVw==", "dev": true, + "optional": true, "requires": { "lru-cache": "^6.0.0" } @@ -5339,13 +5339,15 @@ "version": "8.3.2", "resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz", "integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==", - "dev": true + "dev": true, + "optional": true }, "which": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz", "integrity": "sha512-BLI3Tl1TW3Pvl70l3yq3Y64i+awpwXqsGBYWkkqMtnbXgrMD+yj7rhW0kuEDxzJaYXGjEW5ogapKNMEKNMjibA==", "dev": true, + "optional": true, "requires": { "isexe": "^2.0.0" } @@ -6575,7 +6577,8 @@ "version": "0.1.1", "resolved": "https://registry.npmjs.org/shellwords/-/shellwords-0.1.1.tgz", "integrity": "sha512-vFwSUfQvqybiICwZY5+DAWIPLKsWO31Q91JSKl3UYv+K5c2QRPzn0qzec6QPu1Qc9eHYItiP3NdJqNVqetYAww==", - "dev": true + "dev": true, + "optional": true }, "shimmer": { "version": "1.2.1", diff --git a/package.json b/package.json index 4f3ed7b4..6ae10040 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", diff --git a/src/config/sequelize.ts b/src/config/sequelize.ts index 9aa40fb0..7a72bfd7 100644 --- a/src/config/sequelize.ts +++ b/src/config/sequelize.ts @@ -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,