typeORM v0.3.6 nest,js migration - migration

I have Typeorm loaded asynchronously
app.module.ts:
TypeOrmModule.forRootAsync({ inject: [ConfigService], useFactory: getTypeormConfig}), typeorm.config.ts: export const getTypeormConfig = async (config: ConfigService) => ({ type: 'postgres' as const, host: config.get<string>('TYPEORM_HOST'), port: config.get<number>('TYPEORM_PORT'), password: config.get<string>('TYPEORM_PASSWORD'), ....
It seems that the typeORM documentation is outdated, as they say:: https://wanago.io/2022/07/25/api-nestjs-database-migrations-typeorm/
I'm trying to follow this example. Created a separate second configuration for CLI migration typeorm-migration.config.ts at the root of the project:
import { BusinessLog } from 'src/yandex-ndd-api-client/entity/business-log.entity';
import { ClientStatus } from 'src/yandex-ndd-api-client/entity/client-status.entity';
import { Item } from 'src/yandex-ndd-api-client/entity/item.entity'; ...
export default new DataSource({
type: 'postgres' as const,
host: 'postgres', port: 5432, username: 'postgres', database: 'childrensworld',
subscribers: [TruckSubscriber, RequestSubscriber],
entities: [ Request, Item, BusinessLog, Place, ClientStatus, ....
I also wrote in package.json:
"typeorm": "ts-node ./node_modules/typeorm/cli", "typeorm:run-migrations": "npm run typeorm migration:run -- -d ./typeorm-migration.config.ts", "typeorm:generate-migration": "npm run typeorm -- -d ./typeorm-migration.config.ts migration:generate ./migrations/test_migration_name", "typeorm:create-migration": "npm run typeorm -- migration:create ./migrations/test_migration_name", "typeorm:revert-migration": "npm run typeorm -- -d ./typeorm-migration.config.ts migration:revert"
launching npm run typeorm:generate-migration --name=CreatePost as in the example and get:
Error during migration run: Error: Unable to open file: "E:\Programming\Nodejs\..........\typeorm-migration.config.ts". Cannot find module 'src/yandex-ndd-api-client/entity/business-log.entity' Require stack: - E:\Programming\Nodejs\LandPro\сhildsworld\Projects\tests\test_migrationTypeOrm\typeorm-migration.config.ts
As if it cannot read entities from typeorm-migration.config.ts The example says nothing about this. Maybe this config is for CLI migration(typeorm-migration.config.ts)do you need to connect somewhere else?

This is our typeorm cmd. You might need the -r tsconfig-paths/register.
typeorm": "ts-node --transpile-only -r tsconfig-paths/register ./node_modules/typeorm/cli.js --dataSource src/typeorm/typeorm.config.ts",

Related

TypeORM Migration: File must contain a TypeScript / JavaScript code and export a DataSource instance

When trying to autogenerate migrations I get the following error.
File must contain a TypeScript / JavaScript code and export a DataSource instance
This is the command that I am running:
typeorm migration:generate projects/core/migrations/user -d db_config.ts -o
And my db_config.ts file looks like this:
import { DataSource } from "typeorm";
const AppDataSource = new DataSource({
type: "postgres",
host: process.env.PGHOST,
port: 5432,
username: process.env.PGUSER,
password: process.env.PGPASSWORD,
database: process.env.PGDATABASE,
entities: ["./projects/**/entities/*.ts"],
migrations: ["./projects/**/migrations/**.js"],
synchronize: true,
logging: false,
});
export default AppDataSource
My current file structure looks like this:
back_end
-- projects
--- index.ts
--- db_config.ts
And my index.ts file looks like this:
import express from "express";
import { AppDataSource } from "./data-source";
import budget_app from "./projects/budget_app/routes";
export const app = express();
const port = 3000;
AppDataSource.initialize()
.then(() => {
console.log("Data Source has been initialized!");
})
.catch((err) => {
console.error("Error during Data Source initialization", err);
});
// export default AppDataSource;
app.get("/", (req, res) => {
res.send("Hello World!!!!");
});
app.use("/budget_app", budget_app);
app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});
I am also running this in a docker container along with my postgres database. I have confirmed that the connection works because if I do synchronize=true it will create the table just fine. I just can't create the migration.
So I'm confused and don't know where to go from here to fix the issue. Thanks for your help in advance!
I had trouble with migrations in typeorm, and finally found a solution that will work consistently.
For me, build and then using js datasource didn't work, So i provide my solution for those who steel have struggle with typeorm-migrations.
Here is my step by step solution:
create your datasource config in some file like datasource.config.ts,
mine is like this:
import * as mysqlDriver from 'mysql2';
import {DataSourceOptions} from 'typeorm';
import dotenv from 'dotenv';
dotenv.config();
export function getConfig() {
return {
driver: mysqlDriver,
type: 'mysql',
host: process.env.MYSQL_HOST,
port: parseInt(process.env.MYSQL_PORT, 10),
username: process.env.MYSQL_USER,
password: process.env.MYSQL_PASSWORD,
database: process.env.MYSQL_DB,
synchronize: false,
migrations: [__dirname + '/../../typeorm-migrations/*.{ts,js}'],
entities: [__dirname + '/../**/entity/*.{ts,js}'],
} as DataSourceOptions;
}
create a file with name like migration.config.ts
the implementation is like this:
const datasource = new DataSource(getConfig()); // config is one that is defined in datasource.config.ts file
datasource.initialize();
export default datasource;
now you can define your migration commands in package.json file
"migration:up": "./node_modules/.bin/ts-node ./node_modules/.bin/typeorm migration:run -d config/migration.config.ts",
"migration:down": "./node_modules/.bin/ts-node ./node_modules/.bin/typeorm migration:revert -d config/migration.config.ts"
with running yarn run migration:up you will be able to run your defined migrations in typeorm-migrations folder
I was running into the same issues (typeorm 0.3.4). I was just using npx typeorm migration:show -d ./src/data-source.ts and getting the same error as above (File must contain a TypeScript / JavaScript code and export a DataSource instance), while generating the migration file itself worked somehow, but not running/showing the migrations themselves.
My datasource looks like this
export const AppDataSource = new DataSource({
type: 'postgres',
url: process.env.DATABASE_URL,
logging: true,
entities: ['dist/entities/*.js'],
migrations: ['dist/migrations/*.js'],
});
because my tsc output lives in /dist. So based on the comments above I started using the datasource file that was generated from TypeScript and the error message changed:
npx typeorm migration:run -d ./dist/appDataSource.js
CannotExecuteNotConnectedError: Cannot execute operation on "default" connection because connection is not yet established.
So I looked into the database logs and realized it wanted to connect to postgres with the standard unix user, it wasn't honoring the connection strings in the datasource code. I had to supply all environment variables to the command as well and it worked:
DATABASE_URL=postgresql://postgres:postgres#localhost:5432/tsgraphqlserver npx typeorm migration:run -d ./dist/appDataSource.js
I had the same issue when using a .env file (if you don't have a .env file, this answer probably is irrelevant for you).
It seems, that the CLI does not pick environment variables from dotenv, so you have to load them yourself. E.g., using dotenv library, put this on top of your data-source file:
import * as dotenv from 'dotenv';
dotenv.config();
// export const AppDataSource = new DataSource()...
Alternatively, provide real environment variables when running the script:
PGHOST=... PGUSER=... PGDATABASE=... PGPASSWORD=... typeorm migration:generate ...
I am actually running into the same issue.
I was able to resolve it by using *.js instead of *.ts
Please try something like this:
tsc && typeorm migration:generate -d db_config.ts projects/core/migrations/user
My tsconfig.json looks like this.
{
"compilerOptions": {
"target": "esnext",
"module": "CommonJS",
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"outDir": "./build",
"removeComments": false,
"resolveJsonModule": true,
"esModuleInterop": true,
}
}
I recommend you open an issue on the typeorm github repo, I think it might be a bug.
Add the following to package.json scripts section:
"typeorm": "typeorm-ts-node-commonjs",
"migration:run": "ts-node ./node_modules/typeorm/cli.js migration:run -d ./src/data-source.ts",
"schema:sync": "npm run typeorm schema:sync -- -d src/data-source.ts",
"migration:show": "npm run typeorm migration:show -- -d src/data-source.ts",
"migration:generate": "npm run typeorm migration:generate -- -d src/data-source.ts",
"migration:create": "npm run typeorm migration:create"
You can then use npm run migration:create -- src/migration for example

Webdriver instances not created for custom protractor.conf file

I want to integrate my E2E suite in Travis, so I followed this article. As mentioned in the article I've created a custom protractor.ci.conf.js file of the Travis build. I've placed this file inside my e2e folder (path: e2e/protractor.ci.conf.js).
The only difference in my custom e2e/protractor.ci.conf.js and angular generated protractor.conf.js files is the value in args property displayed below.
e2e/protractor.ci.conf.js
chromeOptions: {
args: [
'--headless',
'window-size=1920,1080'
]
}
protractor.conf.js
const SpecReporter = require('jasmine-spec-reporter').SpecReporter;
exports.config = {
allScriptsTimeout: 11000,
specs: [
'./e2e/**/*.e2e-spec.ts'
],
capabilities: {
shardTestFiles: true,
maxInstances: 2,
'browserName': 'chrome',
chromeOptions: {
args: ['--start-maximized']
}
},
directConnect: true,
baseUrl: 'localhost:4000/',
framework: 'jasmine',
jasmineNodeOpts: {
showColors: true,
defaultTimeoutInterval: 300000,
print: function () {
}
},
useAllAngular2AppRoots: true,
onPrepare: function () {
jasmine.getEnv().addReporter(new SpecReporter());
require('ts-node').register({
project: 'e2e/tsconfig.json'
});
}
};
In my package.json file there are 2 scripts one for running tests locally and one on Travis.
Package.json (at the same level where protractor.conf.js is located)
"scripts": {
...
"test": "ng test --watch=false",
"pree2e": "webdriver-manager update",
"e2e": "concurrently --kill-others \"ng e2e --port=4000\" \"npm run _server:run\"",
"e2e:ci": "concurrently --kill-others \"ng e2e --port=4000 --protractor-config=e2e/protractor.ci.conf.js\" \"npm run _server:run\"",
"_server:run": "tsc -p ./server && concurrently \"tsc -w -p ./server\" \"nodemon dist/server/index.js\" ",
...
},
.travis.yml
branches:
only:
- staging
- prod
- functional-testing
script:
...
- if [[ $TRAVIS_COMMIT_MESSAGE == *"[skip e2e]"* ]]; then echo "skipping E2E test"; else npm run e2e:ci; fi
...
before_deploy:
- sed -i '/dist/d' .gitignore
- git add . && git commit -m "latest build"
- cd $TRAVIS_BUILD_DIR/dist
PROBLEM
When simply running npm run e2e, every test is working fine. But when I'm using npm run e2e:ci command scripts hangs and no instance of WebDriver runs.
I/launcher — Running 0 instances of WebDriver
is coming instead of 1 or 2 instances.
That's because since you made a new config file and apparently placed in the folder
/e2e instead of the default root folder.
The path to the test files in your case should also be updated.
So './e2e/**/*.e2e-spec.ts' will get changed to './**/*.e2e-spec.ts'
Since, currently the test is not able to find any files specified, it doesn't run any instances.

Configure webpack for vue such that backend express server is in ECMA2016

I wrote an app using the webpack-boilerplate for vue. The backend handling GET and POST requests is an express-server. IDE is visual studio code.
This is what I did:
$ vue init webpack app
$ cd app
$ npm install
$ npm install axios express body-parser node-sass sass-loader
$ npm install --save-dev concurrently nodemon
app/express/app.js looks like:
//import express from express
var express = require('express')
var app = express()
// sets port 8080 to default or unless otherwise specified in the environment
app.set('port', process.env.PORT || 8080)
var bodyParser = require('body-parser')
app.use(bodyParser.json())
app.use(express.static(`${__dirname}/../dist/`))
// Test: curl -X POST -H "Content-Type: application/json" -d '{"path": "bla/blub.txt"}' http://localhost:8081/api/save
app.post('/api/save', function (req, res) {
response = {
msg: 'okay',
data: Math.floor(Math.random() * 10)
};
res.end(JSON.stringify(response))
})
var server = app.listen(app.get('port'), function () {
var host = server.address().address
var port = server.address().port
console.log("Example app listening at http://%s:%s", host, port)
})
I modified the boilerplate code generated by vue init webpack such that app/components/HelloWorld.vue is:
<template>
<div>
<h1>{{ msg }}</h1>
</div>
</template>
<script>
import Axios from 'axios'
const baseUrl = "http://127.0.0.1:8080/api"
export default {
name: 'HelloWorld',
data () {
return {
msg: 'Welcome to Your Vue.js App'
}
},
async created() {
this.msg = await Axios.post(`${baseUrl}/save`, {"path": "bla/blub.txt"})
.then(response => new Promise(resolve => {
let msg = response.data.msg
resolve(msg)
}))
.catch(error => {
console.log(error)
Promise.reject(error)
})
}
}
</script>
<style lang="scss" scoped>
h1 {
font-weight: normal;
}
</style>
To start development in one command (npm start go) and allow hot reloading, I changed app/package.json (don't know where I copied that from):
"scripts": {
"dev": "webpack-dev-server --inline --progress --config build/webpack.dev.conf.js",
"apiserver": "PORT=8081 nodemon express/app.js",
"go": "concurrently --kill-others \"npm run dev\" \"npm run apiserver\"",
"start": "npm run dev",
"build": "node build/build.js"
},
To start the dev version of the app, I ran:
$ npm run dev # webpack-dev-server starts on port 8080, express on 8081
To avoid CORS-problems, webpack can be configured to proxy express requests. Change app/config/index.js:
proxyTable: {
'/api':{
target: 'http://localhost:8081',
changeOrigin: true,
}
},
One can now run npm run dev from app/ again, everything works fine.
Hence, over to production mode. I run
$ npm run build
$ node express/app.js
Everything runs fine.
(Over time, I added answers found by myself into this question... The original question was: "Configure webpack for vue frontend and express backend (scenario both production and development)")
My question is now:
How to change webpack babel setup such that the node-run file app.js uses ECMA2016 (such that import express from express can be used instead of require ...)?
Thanks for any help!

Heroku tests started failing overnight? (cannot configure sandbox)

I’m getting a strange error when my tests run on Heroku which wasn’t happening on Friday, everything is fine locally.
-----> Running test command `mix test`...
08:29:52.761 [info] Already up
08:29:53.389 [info] seeding in prod environment
08:29:53.389 [info] runing default seeds
All departments inserted
** (RuntimeError) cannot configure sandbox with pool DBConnection.Poolboy.
To use the SQL Sandbox, configure your repository pool as:
pool: Ecto.Adapters.SQL.Sandbox
(ecto) lib/ecto/adapters/sql/sandbox.ex:429: Ecto.Adapters.SQL.Sandbox.mode/2
(elixir) lib/code.ex:376: Code.require_file/2
(elixir) lib/enum.ex:675: Enum."-each/2-lists^foreach/1-0-"/2
(elixir) lib/enum.ex:675: Enum.each/2
(mix) lib/mix/tasks/test.ex:229: Mix.Tasks.Test.run/1
-----> test command `mix test` failed with exit status 1
No changes to test config in months…
# Configure your database
config :ev2, Ev2.Repo,
adapter: Ecto.Adapters.Postgres,
username: "postgres",
password: "postgres",
database: "ev2_timecards_test",
hostname: "localhost",
pool: Ecto.Adapters.SQL.Sandbox
app.json currently looks like this:
"environments": {
"test": {
"env": {
"IS_STAGING": {
"required": false
}
},
"addons":[
"heroku-postgresql:hobby-dev",
"heroku-redis:hobby-dev"
],
"scripts": {
"test-setup": "mix ecto.migrate",
"test": "mix test",
"postdeploy": "bash <(curl -s https://codecov.io/bash)"
}
}
}
Running with Phoenix 1.3.
I wonder if anyone is facing this on Monday morning or if anyone has any suggestions?
Thanks in advance!
Set the mix_env to test in your setup and test script:
"scripts": {
"test-setup": "MIX_ENV=test mix ecto.migrate",
"test": "MIX_ENV=test mix test"
}

PhantomJS timeout issue when running in headless mode in GitLab CI

I am trying to use GitLab CI to run some client-side unit test written using QUnit. Now to run the Qunit test I am using the grunt-contrib-qunit plugin. To run these tests in headless mode I am using this plugin which hosts it on a localhost server in a console and runs all unit tests. When running this project locally I am successfully able to run all the unit tests but when I checking in my code which kicks of the CI process, on GitLab, it fails on starting the phantomjs server and gives timeout error. I am also providing the jsbin link of the two text files which are basically the output of the unit test from my console. One file is of my local system and another is from the GitLab CI that runs on GitLab website when I check-in my code.
Local Console Output File Dump
Gitlab CI Output Dump
Adding my gitlab-ci.yaml file
image: node:4.2.2
before_script:
- dir
- cd sapui5_repo
- dir
- cd app-with-tests
build:
stage: build
script:
- npm i
- npm run test
cache:
policy: push
paths:
- node_modules
artifacts:
paths:
- built
Also adding my gruntfile if that helps
/* global module */
module.exports = function (grunt) {
grunt.initConfig({
qunit: {
all: {
options: {
timeout: 9000,
urls: [
"http://localhost:9000/webcontent/test/unit/unitTests.qunit.html"
]
}
},
//all: ["webcontent/test/unit/unitTests.qunit.html"],
options: {
timeout: 2000,
}
},
connect: {
options: {
//open: true,
},
first: {
options: {
port: 9000,
//livereload: 3500,
base: "./"
}
},
second: {
options: {
open: {
target: "http://localhost:9000/webcontent"
},
keepalive: true,
port: 9000,
livereload: 3501,
base: "./",
}
}
},
});
grunt.loadNpmTasks("grunt-contrib-connect");
grunt.loadNpmTasks("grunt-contrib-qunit");
grunt.registerTask("test", [
"connect:first", "qunit"
]);
grunt.registerTask("default", [
"connect:second"
]);
};