I want to use a separate database for running tests. So I tried to configure TypeORM for multiple environments (dev and test) but it's not working. It only use the 'dev' configuration.
This is my npm scripts:
"scripts": {
"start": "NODE_ENV=dev node dist/index.js",
"test": "NODE_ENV=test mocha --reporter spec --compilers ts:ts-node/register 'test/**/*.test.ts'"
}
If I console.log(process.env.NODE_ENV) I get the correct results ("dev" / "test").
This is my ormconfig.json
[
{
"environment": "dev",
"name": "default",
"driver": {
"type": "mysql",
"host": "localhost",
"port": 3306,
"username": "root",
"password": "",
"database": "api"
},
"entities": [ "dist/model/*.js" ],
"autoSchemaSync": true
},
{
"environment": "test",
"name": "default",
"driver": {
"type": "mysql",
"host": "localhost",
"port": 3306,
"username": "root",
"password": "",
"database": "api_test"
},
"entities": [ "dist/model/*.js" ],
"autoSchemaSync": true
}
]
I connect with createConnection();. I manually created both databases api and api_test beforehand.
Why is TypeORM not using the "test" configuration when I set NODE_ENV=test?
You could change your ormconfig.json to a js file then do something similar to this:
require('dotenv/config');
const database = {
development: "dev-db",
production: 'prod-db',
test: 'test-db'
}
module.exports = {
type: 'postgres',
host: 'localhost',
port: 5432,
username: 'ur-username',
password: 'password',
database: database[process.env.NODE_ENV],
entities: ['dist/**/*.entity{.ts,.js}'],
synchronize: true,
migrationsTableName: 'migration',
migrations: ['migration/*.js'],
cli: {
migrationsDir: 'migration',
},
};
Then when running your tests, don't forget to set the appropriate environment. NODE_ENV=test should do.
I had a similar problem. I got this to work by using different 'name' fields for each connection. For my run-time connection, I kept name=default, and for my test connection I used name=test. So:
[
{
"environment": "dev",
"name": "default",
"driver": {
"type": "mysql",
"host": "localhost",
"port": 3306,
"username": "root",
"password": "",
"database": "api"
},
"entities": [ "dist/model/*.js" ],
"autoSchemaSync": true
},
{
"environment": "test",
"name": "test", //// CHANGED
"driver": {
"type": "mysql",
"host": "localhost",
"port": 3306,
"username": "root",
"password": "",
"database": "api_test"
},
"entities": [ "dist/model/*.js" ],
"autoSchemaSync": true
}
]
In my application, I would simply use createConnection(), which would automatically use the connection with name=default.
For my tests, I used typeorm's createConnections() (notice the s). This loads all connections. Once loaded, I would immediately after use getConnection('test'), which would get the test connection. My beforeAll in my tests looked like this in typescript:
beforeAll(async () => {
await createConnections();
getConnection('test');
});
In javascript, it would probably look something like:
beforeAll(() => {
createConnections().then(() => {
getConnection('test');
});
});
Then my tests started to pass. Hope that helps.
I had the same problem and I solved it by installing cross-env and the dotenv-flow package.
My typeorm script looked like this:
"typeorm": "cross-env NODE_ENV=development ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli -d src/core/database/ormconfig.ts",
And I created an ormconfig.ts file, where I define the database connection variables through process.env, and all I had to do was add the "require" related to "dotenv-flow".
My ormconfig.ts
import { DataSource } from "typeorm";
require('dotenv-flow').config();
export const AppDataSource = new DataSource({
type: 'postgres',
host: process.env.DB_HOST,
port: parseInt(process.env.DB_PORT) || 5432,
username: process.env.DB_USER,
password: process.env.DB_PASS,
database: process.env.DB_NAME,
entities: [
],
migrations: [
],
migrationsRun: false,
migrationsTableName: 'history'
});
This option is no longer available.
You can see the pull request that removed it from the TypeORM docs, in Aug, 2017, here:
https://github.com/typeorm/typeorm.github.io/pull/13/files
The functionality itself seems to have been removed in a commit with other changes to fix another (unrelated?) issue. It is not immediately clear what the intent was in removing it.
Related
This question already has answers here:
Dialect needs to be explicitly supplied as of v4.0.0
(19 answers)
Closed 3 years ago.
I am using node, express and sequelize where when i run sequelize db:migrate am getting the following issue,
Sequelize CLI [Node: 13.2.0, CLI: 5.5.1, ORM: 5.21.2]
Loaded configuration file "server/config/config.json".
ERROR: Dialect needs to be explicitly supplied as of v4.0.0
Config file for db authentication is,
{
"dev": {
"username": "user_1",
"password": "pass",
"database": "todos_dev",
"host": "127.0.0.1",
"dialect": "postgres",
"operatorsAliases": false
},
"test": {
"username": "root",
"password": null,
"database": "database_test",
"host": "127.0.0.1",
"dialect": "postgres",
"operatorsAliases": false,
"dialectOptions": {
"bigNumberStrings": true
}
},
"production": {
"username": "root",
"password": null,
"database": "database_production",
"host": "127.0.0.1",
"dialect": "postgres",
"operatorsAliases": false
}
}
sequlize is not able to get NODE_ENV means it is not able to decide which authentication detail it should use, you need to define that using
export NODE_ENV=development
or when you do config for DB connection you can check it as -
const env = process.env.NODE_ENV || 'development';
This will solve the issue.
I'm trying to deploy strapi on google cloud app engine (standard env) but I keep getting a 500 server error. I googled all over but no guides have been written on how to successfully deploy strapi on AE.
I tried the suggestions on this thread: https://github.com/strapi/strapi/issues/2146
So I have:
the gcp-build script to install dependencies
an entrypoint in app.yaml to start strapi
my database.json and server.json in strapi are updated
But it does not seem to work for me. I keep getting the following error:
I tried googling that error "app/invalid" but I can't seem to find anything about that.
I also gave the flex env a spin but that failed as well (without a proper error).
This is my app.yaml file:
runtime: nodejs10
instance_class: F2
service: admin
entrypoint: node_modules/strapi/bin/strapi.js
env_variables:
DATABASE_HOST: "host"
DATABASE_PORT: 27017
DATABASE_NAME: "db"
DATABASE_USERNAME: "name"
DATABASE_PASSWORD: "pw"
DATABASE_SRV: true
DATABASE_AUTHENTICATION_DATABASE: "admin"
DATABASE_SSL: true
NODE_ENV: "production"
PORT: 1337
This is my package.json (important parts):
...
"scripts": {
"strapi": "node_modules/strapi/bin/strapi.js",
"gcp-build": "node node_modules/strapi/lib/utils/post-install.js && cd admin && npm run setup"
}
...
"engines": {
"node": ">=10.0.0",
"npm": ">=6.0.0"
},
production database.json:
{
"defaultConnection": "default",
"connections": {
"default": {
"connector": "strapi-hook-mongoose",
"settings": {
"client": "mongo",
"host": "host",
"port": 27017,
"database": "db",
"username": "name",
"password": "pw",
"srv": true
},
"options": {
"authenticationDatabase": "admin",
"ssl": true
}
}
}
}
And finally this is my server.json file:
{
"host": "https://admin-dot-ootje-website.appspot.com",
"port": 1337,
"production": true,
"proxy": {
"enabled": false
},
"autoReload": {
"enabled": false
},
"cron": {
"enabled": false
},
"admin": {
"autoOpen": false
}
}
I would expect that this works from the getting started guides in AE but it would seem I'm missing something. Does someone know what that error means? Did someone manage to already deploy strapi on AE?
If I get it working I'd like to add it to strapi docs or medium post for other people to find it easier than I did :)
Thanks in advance!
I think your problem provide from server.json file.
This configuration works on my side :
{
"host": "localhost",
"port": "${process.env.PORT || 1337}",
"production": true,
"proxy": {
"enabled": false
},
"autoReload": {
"enabled": false
},
"cron": {
"enabled": false
},
"admin": {
"autoOpen": false
}
}
I'm working on a new configuration file in order to have the auth provider working... and some other features. But this first version is working for me.
I keep you update of my search if you need.
## Update 1 ##
I found the good configuration for GCP in production mode.
I share it if anyone else need it :
{
"host": "localhost",
"port": "${process.env.PORT || 1337}",
"production": true,
"proxy": {
"enabled": true,
"ssl": true,
"host": "[project-name].appspot.com",
"port": 443
},
"autoReload": {
"enabled": false
},
"cron": {
"enabled": false
},
"admin": {
"autoOpen": false
}
}
I use flexible environment on GCP, but this will work in standard mode I think.
Thanks,
I develop a Nodejs app with sequelize used. I have been using 9.6 and just installed 10.6 and also copied the database to the new version (so right now I have 2 copies of it, one for each server/version).
However, my app still uses the old one. I am not sure how to change that. Is it via code or should I just stop somehow the old server running and start the new one? I use pgAdmin by the way.
This is my config.json that sequelize uses:
{
"development": {
"username": "postgres",
"password": "364200",
"database": "userbase",
"host": "127.0.0.1",
"port": 5433, // was 5432
"dialect": "postgres"
},
"test": {
"username": "postgres",
"password": "364200",
"database": "userbase",
"host": "127.0.0.1",
"port": 5433, // was 5432
"dialect": "postgres"
},
"production": {
"username": "postgres",
"password": "364200",
"database": "userbase",
"host": "127.0.0.1",
"port": 5433, // was 5432
"dialect": "postgres"
}
}
index.js (sequelize)
sequelize = new Sequelize('userbase', 'postgres', '364200', {
host: '127.0.0.1',
port: '5433' // <--- ADD THIS HERE
dialect: 'postgres',
});
EDIT: Fixed it with #Chiller's help in comments
I exported NODE_ENV=local and defined my mongo datasource as follows in datasources.local.json
"mongo": {
"host": "localhost",
"port": 27017,
"url": "mongodb://localhost:27017/mydb",
"database": "mydb",
"password": "",
"name": "mongo",
"user": "",
"connector": "mongodb"
},
But the database name is getting created as 'test'.I also tried in datasources.json but it's still the same. I need the database name to be 'mydb'. Please help
What fixed the problem for me was to set the name of the object to "db" and not "mongo" or "mongods".
"db": {
"host": "localhost",
"port": 27017,
"url": "mongodb://localhost:27017/mydb",
"database": "mydb",
"password": "",
"name": "mongo",
"user": "",
"connector": "mongodb"
},
I just got started using strapi framework and I would like to use mLab as my mongoDB database, so I go to configure strapi and fill the following details:
{
"defaultConnection": "default",
"connections": {
"default": {
"connector": "strapi-mongoose",
"settings": {
"client": "mongo",
"host": "localhost",
"port": 27017,
"database": "development",
"username": "",
"password": ""
},
"options": {}
}
}
}
The details I get from mLab are:
mongodb://myUsername:myPassword#ds047891.mlab.com:41365/myDatabase
Here is my final config:
{
"defaultConnection": "default",
"connections": {
"default": {
"connector": "strapi-mongoose",
"settings": {
"client": "mongo",
"host": "ds047891.mlab.com",
"port": 41365,
"database": "myDatabase",
"username": "myUsername",
"password": "myPassword"
},
"options": {}
}
}
}
When I try to start strapi, I get the following error:
DEBUG (2748 on DESKTOP-HAL1ATE): Server wasn't able to start properly.
ERROR (2748 on DESKTOP-HAL1ATE): (hook:mongoose) takes too long to load
I think that I did not setup my configuration right, but I can't pinpoint where the problem is. I hope someone could, thanks.
I am Pierre, on of the creators of Strapi. I tried with the following configuration and it worked well:
{
"defaultConnection": "default",
"connections": {
"default": {
"connector": "strapi-mongoose",
"settings": {
"client": "mongo",
"host": "ds135777.mlab.com",
"port": "35777",
"database": "myDatabase",
"username": "myUsername",
"password": "myPassword"
},
"options": {}
}
}
}
Our configurations files look quiet similar.
What file did you updated (/config/environment/development/database.json or /config/environment/production/database.json)?
Are you sure you entered the correct username and password? Did you try to login to your MongoDB instance through the command line mongo ds135777.mlab.com:35777/myDatabase -u <dbuser> -p <dbpassword>?
UPDATE
In version >= 3 for mlab don't forget to specify authenticationDatabase
"options": {
"authenticationDatabase": "your_mlad_database_name",
"ssl": false
}