spinlud / sequelize-typescript-generator

70 stars 25 forks source link

Encrypt Error on Command Line (npx stg) #15

Closed robpearmain closed 4 years ago

robpearmain commented 4 years ago

$ npx stg -D mssql -h (azuredb).database.windows.net -p 1433 -d (database)-u (username) -x (password) --indices --case camel --out-dir models --clean

fails as it says encrypt needs to be true.

Please could you add a command line option to set encrypt to true or false, or an option to point to a config file

spinlud commented 4 years ago

Hi, have you tried the ssl option?

image

robpearmain commented 4 years ago

Hi

Unfortunately not, I get:

ConnectionError [SequelizeConnectionError]: Server requires encryption, set 'encrypt' config option to true.

I am using

$ npx stg -D mssql -h (azuredb).database.windows.net -p 1433 -d (database)-u (username) -x (password) --indices --case camel --out-dir models --clean --ssl

If I use node and a config and add the following to config:

dialectOptions: { options: { encrypt: true } }

I can make a connection, but not from the command line:

$ npx stg -D mssql -h (azuredb).database.windows.net -p 1433 -d (database)-u (username) -x (password) --indices --case camel --out-dir models --clean --ssl

The database is in Azure

spinlud commented 4 years ago

Please try the new version, there is an additional cli option --dialect-options you can use to pass native settings for the connection. Usage:

stg [...] --dialect-options encrypt=true foo=1 bar=hithere --other-options

Let me know if it works for you.

robpearmain commented 4 years ago

Hi

Thanks for looking at this

I am using tedious, so not sure if the setting is elsewhere (from manuals), so sorry if previous post was misleading.

The manual says tedious options are as follows:

{ options: { debug: { ... }, database: '(db name)', encrypt: true }, server: '(azure host).database.windows.net', userName: '(user)', password: '(pwd)' }

There is more info here: https://stackoverflow.com/questions/41819106/connecting-to-mssql-server-with-sequelize

So, testing with the new version.Still the same issue:

"sequelize": "5.22.3", "sequelize-typescript": "^1.1.0", "sequelize-typescript-generator": "^2.7.0",

Still getting

ConnectionError [SequelizeConnectionError]: Server requires encryption, set 'encrypt' config option to true.

$ npx stg --dialect-options encrypt=true -D mssql -h (azure host).database.windows.net -p 1433 -d (db) -u (username) -x (password) --indices --case camel --out-dir models --clean --ssl

Same with or without --ssl and --dialect-options

ConnectionError [SequelizeConnectionError]: Server requires encryption, set 'encrypt' config option to true. at /Users/(me)/source/sequelize/test/node_modules/sequelize/lib/dialects/mssql/connection-manager.js:144:17 at tryCatcher (/Users/(me)/source/sequelize/test/node_modules/bluebird/js/release/util.js:16:23)

$ node -v v12.18.3

spinlud commented 4 years ago

Using only ‘sequelize’ and ‘tedious’, can you share the code you use to connect to your database?

robpearmain commented 4 years ago

.env

DB_HOST=xxx.database.windows.net DB_USERNAME=xxx DB_PASSWORD=xxx DB_DATABASE=xxx

config.js:

require('dotenv').config(); // this is important! module.exports = { "development": { "username": process.env.DB_USERNAME, "password": process.env.DB_PASSWORD, "database": process.env.DB_DATABASE, "host": process.env.DB_HOST, "dialect": "mssql" } }

snippet from models/index.js

const Sequelize = require('sequelize');

const config = require(__dirname + "/../config/config.js")[env];

sequelize = new Sequelize(process.env[config.use_env_variable], config);

package.json snippet

sequelize": "^6.3.5", "swr": "^0.3.2", "tedious": "^9.2.1"

I used a lower version of Sequelize to test due to the issue > 6 with STG

spinlud commented 4 years ago

.env

DB_HOST=xxx.database.windows.net DB_USERNAME=xxx DB_PASSWORD=xxx DB_DATABASE=xxx

config.js:

require('dotenv').config(); // this is important! module.exports = { "development": { "username": process.env.DB_USERNAME, "password": process.env.DB_PASSWORD, "database": process.env.DB_DATABASE, "host": process.env.DB_HOST, "dialect": "mssql" } }

snippet from models/index.js

const Sequelize = require('sequelize');

const config = require(__dirname + "/../config/config.js")[env];

sequelize = new Sequelize(process.env[config.use_env_variable], config);

package.json snippet

sequelize": "^6.3.5", "swr": "^0.3.2", "tedious": "^9.2.1"

I used a lower version of Sequelize to test due to the issue > 6 with STG

Thanks but I don't see any encrypt option in your code. Btw I think the problem is shown here: image

You have to pass a nested options object inside the dialectOptions to make it work.

I've modified the argument --dialect-options, now you must pass a json string instead (so you can even use nested objects). Example:

npx stg [...] --dialect-options '{"options": {"encrypt": true}}' [...] --logs

There is also another argument --dialect-options-file where you can pass a path to a json file instead of providing the json inline:

npx stg [...] --dialect-options-file /path/to/dialectOptions.json [...] --logs

Argument --logs is useful for debugging.

Let me know if this solves your issue.

robpearmain commented 4 years ago

$ npx stg -D mssql -h (azure).database.windows.net -p 1433 -d (db) -u (username) -x (password) --indices --case camel --out-dir models --clean --dialect-options '{"options": {"encrypt": true}}'

Works a treat, thanks for all your hard work on this