NestJS, PortsgreSQL and TypeORM - Migrations not running properly - postgresql

When trying to run the TypeORM Migrations, either automatically in the application startup or manually via the TypeORM CLI, only the migrations table gets created (and it stays empty). The migration files themselves are not being executed.
Here is my tsconfig.json
{
"compilerOptions": {
"module": "commonjs",
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"target": "es2017",
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./",
"incremental": true
}
}
Here is my package.json
...
"typeorm": "node --require ts-node/register ./node_modules/typeorm/cli.js",
...
Here is my ormconfig.json
...
"entities": ["dist/**/*.entity{.ts,.js}"],
"synchronize": true,
"migrationsRun": true,
"migrations ": ["dist/migrations/*{.ts,.js}"],
"cli": {
"migrationsDir": "src/migrations"
}
...
The migration files are being created through the TypeORM CLI and they are to populate some tables (insert statements). They are not related to changes in the database schema.
Please, can anyone help me make it work?

That was a silly one!
I guess some times the simplest problems are the hardest to spot.
The problem was in the ormconfig.json file.
I removed this empty space ("migrations ":) and everything worked just fine.

you should have synchronized to false synchronize:false
And from the terminal run
npx typeorm migration:generate -n AnyNameYouWant
After that, you can run
npx typeorm migration:run
You may also have to run nest build before running these commands.

I think this one is to synchronize the database with the schema, and it's not a good idea.
"synchronize": true,

Related

Why is importing an icon named ImportExport breaking other dependencies?

I added a Material-UI icon to one of my react components like so:
import { ImportExport } from '#material-ui/icons';
Thereafter, on my production site (but not my localhost dev server), I started getting an error on pageload whereby my socket-io client library was unable to correctly import something in one of its internal files. It had this:
const encodePacket = require("./encodePacket");
Which was used like so:
encodePacket(packet, false, encodedPacket => {
This is where the error arises: TypeError: encodePacket is not a function. Inspecting it, I can see that encodePacket is indeed not a function. When printed in the console, it shows as {default: {…}, __esModule: true}.
On my localhost dev server, this isn't the case. It's a function as expected. Moreover, the import right next to it, for decodePacket, is still a function in both contexts, locally and in production.
More bizarrely, when I examine the encodePacket module in the console, it looks like this:
Mui is Material-UI, a UI lib I use in other parts of the application and from which the icon is sourced - obviously it has no connection to a network library's internal packet encoding logic.
By commenting out the ImportExport icon and its import, I found that the error goes away.
What on earth though? How could this happen?
I even tried renaming the icon, but that didn't work either~
import { ImportExport as ReverseSortIcon } from '#material-ui/icons';
Any suggestions? I'm using Parcel and this is my tsconfig that has worked for 2+ years uneventfully~
{
"compilerOptions": {
"outDir": "./dist/",
"sourceMap": true,
"strict": true,
"noImplicitReturns": true,
"noImplicitAny": true,
"module": "es6",
"moduleResolution": "node",
"target": "es5",
"allowJs": true,
"jsx": "react",
"esModuleInterop": true
},
"include": [
"./src/**/*"
]
}

Code Runner extension in vs-code does not give clean output, despite checking off "Show Execution Message" option

I recently started using Code Runner Extension for Vs-Code and despite checking off the "code-runner.showExecutionMessage": false it keeps printing the file path, and some unwanted code after which it does print output but it's not clean, looks messy and hard to read the output. Here's what it does.
PS [path] \VsCode\ProblemSolving> cd "[path] \VsCode\ProblemSolving"
PS [path] \VsCode\ProblemSolving> cd "[path] \VsCode\ProblemSolving\" ; if ($?)
{ g++ CodingCompetitionTools.cpp -o CodingCompetitionTools } ; if ($?) { .\CodingCompetitionTools }
This precedes the actual program output and I don't want to see this, just the clean program output.
Settiings.json for code-runner
"code-runner.defaultLanguage": "c++",
"code-runner.saveAllFilesBeforeRun": true,
"code-runner.saveFileBeforeRun": true,
"code-runner.fileDirectoryAsCwd": true,
"code-runner.showExecutionMessage": false,
"code-runner.clearPreviousOutput": true,
"code-runner.runInTerminal": false,
Refer this link for Vs-Code Marketplace extension
Could you please share your settings.json file?
I guess your setting looks like this?
{
"code-runner.showExecutionMessage": false
}
Maybe you have code-runner.runInTerminal set to true? If so, change it to false.
Just to debug this a little further, create a .vscode folder in the root of your current project (with a preceding dot) and inside of that folder a settings.json file with just:
{
"code-runner.defaultLanguage": "c++",
"code-runner.saveAllFilesBeforeRun": true,
"code-runner.saveFileBeforeRun": true,
"code-runner.fileDirectoryAsCwd": true,
"code-runner.showExecutionMessage": false,
"code-runner.clearPreviousOutput": true,
"code-runner.runInTerminal": false
}
See if this changes anything.
Apologies if these steps are already obvious to you, but better safe than sorry.

babel-node is compiling test and failing

I can build and the code works and correctly excludes the tests using and can run the code with node:
babel src -s -d dist --extensions ".js,.ts,.tsx" --ignore '**/*.test.js' --ignore '**/test/*'
But trying to use babel-node seems to include the tests regardless:
babel-node --extensions '.js,.ts,.tsx' --ignore='src/**/*.test.js' src/index.js
Depending on the ignore pattern I can get different errors but errors inside a test file. eg. src/entity/authentication/authentication.test.js which babel should be ignoring.
I've tried a number of patters:
**/*.test.js
src/**/*.js
/src/**/*.js
I'm sure it something simple that I'm missing.
My babel config if its helpful:
{
"presets": [
"#babel/preset-env",
["#babel/preset-typescript", {
"isTSX": true,
"allExtensions": true
}]
],
"plugins": [
"babel-plugin-transform-typescript-metadata",
["#babel/plugin-proposal-decorators", {"legacy": true}],
"#babel/plugin-proposal-class-properties",
"#babel/plugin-transform-runtime"
]
}
🤦‍♂️Turns out it was not a babel issue at all it was a typeorm issue. https://github.com/typeorm/typeorm/issues/1654
"entities": [
"src/entity/**/!(*.test)*.js"
]
Note to self: Always leave a project in working condition prior to taking a long break from it. :( Much time was wasted. 😞

Angular (8) application build once (with production config) and deploy to multiple environments

I have a situation where I’m trying to build my angular application with production config and deploy to multiple environments, say, ng build --configuration=production
The work flow here is when I build using the above command (ng build --configuration=production), the environment.ts file gets replaced with environment.prod.ts
The configurations I have in environment.prod.ts is as follows,
export const environment = {
production: true,
environment: 'Production',
_webApiHost: 'prodsomename.company.com/api/',
};
The configurations I have in environmrnt.test.ts is as follows,
export const environment = {
production: true,
environment: 'Test',
_webApiHost: 'testsomename.company.com/api/',
};
The setting I have on angular.json file is as follows,
"configurations": {
"production": {
"fileReplacements": [ {
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
} ],
"optimization": true,
"outputHashing": "all",
"sourceMap": false,
"extractCss": true,
"namedChunks": false,
"aot": true,
"extractLicenses": true,
"vendorChunk": false,
"buildOptimizer": true,
"budgets": [ {
"type": "initial",
"maximumWarning": "2mb",
"maximumError": "5mb"
} ]
},
"test": {
"fileReplacements": [ {
"replace": "src/assets/configs/environment.ts",
"with": "src/assets/configs/environment.test.ts"
} ],
"optimization": true,
"outputHashing": "all",
"sourceMap": false,
"extractCss": true,
"namedChunks": false,
"aot": true,
"extractLicenses": true,
"vendorChunk": false,
"buildOptimizer": true,
"budgets": [ {
"type": "initial",
"maximumWarning": "2mb",
"maximumError": "5mb"
} ]
}
}
If I build the solution for every environment separately and deploy to appropriate environment as below figure,
it works like charm, which mean the,
testApp communicates to _webApiHost: testsomename.company.com/api/ and
prodApp communicates to _webApiHost: prodsomename.company.com/api/
In the above case the artifact which is tested by QA is different from the artifact which is deployed to production, which is not the ideal way of pushing the code to production.
But my concern is I want to build the app only once and deploy it to multiple environments, where each environment will communicate to appropriate api, like below figure,
When I build it using the command ng build --configuration=production, the environment.ts file will have production configurations,
export const environment = {
production: true,
environment: 'Production',
_webApiHost: 'prodsomename.company.com/api/',
};
So if that artifact is deployed to test environment,
the testApp is trying to communicate with _webApiHost: 'prodsomename.company.com/api/, which is not right.
Here is the Azure DevOps build pipeline powershell script I use to build the solution.
Set-Location "$(Build.Repository.LocalPath)\Buffini.Web.UI\Angular"
Write-Host 'Angular Install Starting'
npm install -g #angular/cli#8.0.6 -Verbose
Write-Host 'Angular Install Finished'
Write-Host 'NPM Install Starting'
npm install -Verbose
Write-Host 'NPM Install Finished'
Write-Host 'NPM Update Starting'
npm update -Verbose
Write-Host 'NPM Update Finished'
Write-Host 'NPM Audit Starting'
npm audit fix -Verbose
Write-Host 'NPM Audit Finished'
Write-Host 'Angular Build Starting'
ng build --configuration=production --deleteOutputPath=true
Write-Host 'Angular Build Finished'
I have tried searching for a solution online but I couldn’t find any.
Please help me in resolving the issue. I’ll highly appreciate your time and help on this. Thanks in advance.
To replace app configurations in runtime time. You need to create config.json file which contains the dynamic configurations (eg. _webApiHost). You can check the example code in this blog to fetch the config.json.
In the you pipeline, you can add extension tasks to replace the config.json contents before deploying to different environment(eg. test, production).
In this way you only need to build your angular app once, and only need replace the config.json contents accordingly before deploying to different environment.
The available extensions you can check out. Magic Chunks task, or
RegEx Match & Replace Task. You can check this thread for the example to use these tasks.
I won't pretend this an answer, necessarily, but it's long enough of a thought to not fit in the comments area. Perhaps you will find it helpful. (Full disclosure: I'm not using Azure, but rather GitLab. So there would be some translation necessary, regardless, if you find this approach of use.)
Anyway, I was asking the same question a while back. After some digging, I found this link helpful
Using that guidance, I did the following:
First, I do a basic docker build. In that build I have various environment files "ready for the asking" in a folder. The configuration file that actually drives the app is at the root.
I then do another docker build, this one whose sole purpose is to take the first build and give it the desired configuration. (I do it this way because the first build is slow, but I'd like to push to production without rebuilding.)
Next I do the environment build that I want.
For staging, for example: In my GitLab build CI/CD pipeline yaml, I have a line like this....
docker build -t xxxxx --build-arg SERVE_CONFIGURATION=staging -f [A-Docker-File] .
The docker file is the same for all environments, but based upon the passed in argument, this docker build pulls a different file and slams it into the driver's seat. Since there are no secrets in an Angular deployment, it doesn't matter that there are extra (unused) configuration files lurking in the folder structure (though if one were motivated, one could easily delete them.)
Anyway, inside that 2nd docker build, I have...
...
FROM registry.gitlab.com/xxxxxxxxxxxx/compiled as default-config
FROM registry.gitlab.com/xxxxxxxxxxxx/compiled as final-config
COPY --from=default-config /usr/share/nginx/html/environments/environment.$SERVE_CONFIGURATION.js /usr/share/nginx/html/environment.js
So this docker image is nothing more than a build off the prior image, but with the desired environment file in its proper place.
Anyway, I've probably left out some details, but I'm not sure this will help you and will stop here.

Wintersmith: error Error loading plugin './node_modules/wintersmith-coffee/': Cannot find module './plugin'

I built a site with wintersmith in November of 2013. It's live at http://powma.com
I'm coming back to it, but it's not building :-{
I don't mind getting my hands dirty, but I don't know where to start. I'm getting this error:
error Error loading plugin './node_modules/wintersmith-coffee/': Cannot find module './plugin'
Any suggestions?
Thanks!
Mike
UPDATE
Hey, this is because the coffeescript wasn't getting compiled.
I installed it globally, but that didn't help.
$ sudo npm install -g coffee-script
I manually compiled it and moved to other errors. Any suggestions for what's missing?
$ coffee -c plugin.coffee
Here's my config.json:
{
"locals":
{ "url": "http://localhost:8080"
, "title": "Powma"
, "subTitle": "Linking you to technology"
, "motto": "We build exceptions sites and applications to connect people to products, services, and each other."
, "owner": "Michael Cole"
, "profilePicture": "/static/img/profile-professional.jpg"
, "inlineSpriteMaxBytes" : 10000
},
"views": "./views",
"plugins":
[ "./node_modules/wintersmith-coffee/"
, "./node_modules/wintersmith-stylus/"
],
"require": {
"moment": "moment",
"_": "underscore",
"typogr": "typogr"
},
"jade": {
"pretty": true
},
"markdown": {
"smartLists": true,
"smartypants": true
},
"paginator": {
"perPage": 3
}
}
And package.json:
{
"name": "Powma-com",
"version": "0.1.1",
"private": true,
"engines": {
"node": "0.10.17"
},
"dependencies": {
"moment": "2.0.x",
"underscore": "1.5.x",
"typogr": "0.5.x",
"wintersmith": "2.0.x",
"wintersmith-stylus": "git://github.com/MichaelJCole/wintersmith-stylus.git#master",
"wintersmith-coffee": "0.2.x",
"express": "3.4.x",
"sendgrid": "~0.3.0-rc.1.7",
"express-validator": "~0.8.0",
"underscore-express": "0.0.4"
}
}
This is a new dev laptop I'm working with so that may be part of the problem.
I worked around the issue, but didn't fix it. Do I really need to manually compile the coffeescript?
Thanks!
I solved this issue by explicitly specifying plugin.coffee in the config.json file.
{
...other stuff...
"plugins":
[ "./node_modules/wintersmith-coffee/plugin.coffee"
, "./node_modules/wintersmith-stylus/plugin.coffee"
],
...more stuff...
}
It looks like you're missing wintersmith-coffee in node_modules; make sure you have it installed locally with npm install wintersmith-coffee. You can also try removing it from config.json if you're not using it anywhere.
It would also be helpful to see both your config.json and package.json. Also make sure you run an npm install and npm update to make sure you have everything referenced in package.json installed and updated.
Update
Not having CoffeeScript installed could have been the issue. After installing that globally, I'm not sure if all of your shell sessions will pick up the command and use it without being restarted. With a new shell session, see if you can build the site. You can also try testing Wintersmith in isolation of your site. Try generating a sample site with wintersmith new somepath and see if you can run wintersmith build there. That would be a good start for narrowing down your issues between your site and your workstation setup.