Skip to content

jsigler47/typeorm-upsert-testing

Repository files navigation

Nest Logo

A progressive Node.js framework for building efficient and scalable server-side applications.

NPM Version Package License NPM Downloads CircleCI Coverage Discord Backers on Open Collective Sponsors on Open Collective Support us

Description

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.

Testing steps

# 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"}'

Results

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.

Installation

$ npm install

Running the app

# dpenedencies
$ docker-compose up -d

# development
$ npm run start

# watch mode
$ npm run start:dev

# production mode
$ npm run start:prod

Test

# unit tests
$ npm run test

# e2e tests
$ npm run test:e2e

# test coverage
$ npm run test:cov

Support

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.

Stay in touch

License

Nest is MIT licensed.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published