Run knexfile.js from a db/ folder - node.js

I have installed the nodejs package globally.
After initializing via knex init and putting the file into a /db folder, commands such as knex migrate:latest won`t work anymore, because knex cannot find the file. Furthermore, knex creates the migrate and seed folder in my root directory
Any suggestions how to config knex that it always looks into the /db folder? Is there any way to do this via a node-script?
Thanks for your reply!

You can give your config file location on script
add this on package.json
"migrate": "yarn knex migrate:latest --knexfile src/config/knexfile.js",
My knexfile is in src/config.

Write your own client implementation which just uses migration API provided by knex.

Related

yarn start:dev not working, not changes the code - Nest.js

enter image description here
my code is returning the same value even if i change the code
I want to know what happened
Try the following:
Add the rimraf lib:
yarn add rimraf
Then create a new script in the scripts section of the package.json:
"prebuild":"rimraf dist"
I highly recommend you to remove dist file first and build and rerun your project.
rm -rf dist
npm run build
npm run start:dev
I'm not sure you faced the issue while adding a new model or updated the schema of orm module.
If yes, According to this link NestJS won't automatically copy your assets to the dist folder moreover as I described earlier is not compatible with glob static paths such as entities property in TypeOrmModule.

NodeJS Sequelize ORM not recognise in Windows 10 CMD

Please i need help. I use windows 10 OS and i am trying to use the Sequelize ORM dependency package/module in my NodeJS. I have installed the sequelize dependency and the sequelize-cli dependency package using npm and I confirmed a successful installation because i can find them in my package.json file.
The problem is everytime I try to run the sequelize command to create the model and other directories so that i can start using the ORM mapping in my NodeJS file the Windows 10 cmd returns that "sequelze is not recognized as an internal or external command". I know any command to be run in CMD would be added to the environment variable but sequelize is not a software that is installed but a module inside Node that is install through npm command.
How can i make Windows 10 recognize the sequelize command?
Please i will appreciate any help whatsoever.
It's better to install sequelize-cli globally to avoid such issues. This way it will be in C:\Users\<user>\AppData\Roaming\npm (which is added in PATH) as sequelize.cmd

How to run `prisma generate` in production?

I'm confused about how Prisma code generation in production works. The Prisma CLI is supposed to be installed in devDependencies, yet the npx prisma generate command needs to be available in production, since the generated code is necessary for the application. How can I resolve this? I tried running npm i --production and npx prisma generate, which led to the expected problem of npx trying to auto-install prisma and getting Prisma 1 instead of Prisma 2 and then expecting a prisma.yml file which doesn't exist.
There's no need to run the prisma generate command that is executed on installation of the #prisma/client.
EDIT:
https://www.prisma.io/docs/reference/tools-and-interfaces/prisma-client/generating-prisma-client
Prisma has various guides for installing on different environments.
For example, this one talks about installing on vercel.
https://www.prisma.io/docs/guides/deployment/deployment-guides/deploying-to-vercel
Postinstall hook
The package.json uses the postinstall hook script to run prisma generate. Typically this would go in the build step. Because Vercel caches node_modules after the dependencies are installed, the functions won't have access to the generated Prisma Client.
Generating the Prisma Client in postinstall ensures that the generated Prisma Client in node_modules/#prisma/client is available to the functions.

How to run Jest tests with Node (express.js) backend and create-react-app as frontend

I have the following folder structure:
Clearly React is using Jest in "Client" node_modules. If i try to install Jest for server node_modules (one lvl up), i will get error from React, saying i have duplicate packages up in the tree. How should i run tests for server with Jest? How should i install Jest for server? Or should i? Or i can use the same Jest react uses in "Client" folder? If so, how? What is the command? Really appreciate your help.
2 separate dependency trees, avoiding duplicate packages up in the tree would look like following:
server
├──> node_modules
├──> index.js
└──> package.json
client
├──> node_modules
├──> index.js
└──> package.json
.gitignore
README.md
Solved by using npm link client/node_modules/jest from project root .

How to use the Knex CLI

I have installed Knex in my Node project and all is wonderful and great... so far...
Now I dig deeper into Knex and am confronted with migrations. All the docs I found talk about running commands like "knex migrate:latest", etc. What I get as a result when I try to run such commands from the (Windows) command line is an error telling me that 'knex' is an unknown command.
I am not a npm and Nodes expert, just enough to get the basics running. When digging into the knex node package I find some configuration for a cli.js file under a 'bin' section in the 'package.json'. I do not understand these configurations, even reading the npm documentation about this 'bin' section does not make it clearer to me.
So here my question:
I am on Windows 10 and have installed a package like 'knex' local to my project. Knex comes with a cli. What do I need to do to call that cli from my console?
You can find client from node_modules/.bin/knex if you haven't installed knex globally (which I don't recommend).
When you install packages locally to some directory, all the "bin" executables are linked automatically under node_modules/.bin. If you use these scripts from package.json scripts, npm automatically adds node_modules/.bin to path, so inside package json you don't have to refer node_modules/.bin/knex but just knex is enough.
In your console, try to $ npx knex migrate:latest
It helped me
First type in "npx knex" to access options and commands available to the knex module. To be able to make use of the Knex cli that comes bundled with it, you then have to access the knex module from whatever path you intend creating the file from. For example, let's say I was in the migrations directory and the node_modules folder is one path higher, I would access the Knex module in it this way '../node_modules/.bin/knex migrate:make create-user-table.js' to be able to create a 'create-user-table.js', migration file. I hope I'm clear enough.
If you have knex installed in your project, you can make it available for use via your shell by adding a simple script to your package.json.
"scripts": {
"knex": "knex"
}
Now you can use the knex cli with npm run knex or, if you use yarn, yarn knex.
How to use Knex CLI
Unix Shell
Locally
npm i knex
NODE_ENV=development npx knex migrate:list
# or
export NODE_ENV=development && npx knex migrate:list
Globally
npm i knex -g
NODE_ENV=development knex migrate:list
# or
export NODE_ENV=development && knex migrate:list
Windows Shell
Locally
npm i knex
set NODE_ENV=development&& npx knex migrate:list
Globally
npm i knex -g
set NODE_ENV=development&& knex migrate:list
Consider using Git Bash Shell which behaves like a Unix shell in addition to including the Windows environment.

Resources