A progressive Node.js framework for building efficient and scalable server-side applications.
Hypothesis: Per typeorm's documentation, if the option skipUpdateIfNoValuesChanged
is passed to an upsert call then the update should be skipped if no row values will be changed.
# Start dependencies
docker compose up -d
# Start API
npm run build && npm run start
# or
npm run start:debug
# Open postgres logs
docker compose logs -f
# Perform updates
# Using script
pip3 install requests # Only needs to be done once
cd manual-testing
python3 upsert-test.py
# Using curl (just one dog at a time)
curl -X POST http://localhost:3000/dogs/upsert \
-H "Content-Type: application/json" \
-d '{"name": "Buddy", "age": 3, "breed": "Labrador", "owner": "Dave"}'
Initial test:
Screenshot and logs contained in manual-testing/results
Given the 4 API calls in the script with 2 of the calls being identical, typeorm generated 4 queries and the postgres logs show 4 updates. This is undexpected based on the hypothesis.
Final test: After stepping through the typeorm upsert functions it was determined that if the primary id of the object being updated is a number and the type of id generation is 'increment' then it is not passed in to the update.
The way the upsert works in this case requires the id to be present. Changing the id to a generated uuid string works as expected.
$ npm install
# dpenedencies
$ docker-compose up -d
# development
$ npm run start
# watch mode
$ npm run start:dev
# production mode
$ npm run start:prod
# unit tests
$ npm run test
# e2e tests
$ npm run test:e2e
# test coverage
$ npm run test:cov
Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please read more here.
- Author - Kamil Myśliwiec
- Website - https://nestjs.com
- Twitter - @nestframework
Nest is MIT licensed.