Skip to content

Commit

Permalink
feat: CommonDaoPatchByIdOptions.requireToExist
Browse files Browse the repository at this point in the history
  • Loading branch information
kirillgroshkov committed Aug 9, 2024
1 parent a3042ff commit 3bddbdb
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 41 deletions.
14 changes: 13 additions & 1 deletion src/commondao/common.dao.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ export interface CommonDaoCfg<BM extends BaseDBEntity, DBM extends BaseDBEntity

/**
* Defaults to false.
* If true - run patch operations (patch, patchById) in a Transaction.
* If true - run patch operations (patch, patchById, patchByIdOrCreate) in a Transaction.
*
* @experimental
*/
Expand Down Expand Up @@ -260,6 +260,18 @@ export interface CommonDaoSaveOptions<BM extends BaseDBEntity, DBM extends BaseD
skipIfEquals?: BM
}

export interface CommonDaoPatchByIdOptions<DBM extends BaseDBEntity>
extends CommonDaoSaveBatchOptions<DBM> {
/**
* Defaults to false.
* With false, if the row doesn't exist - it will be auto-created with `dao.create`.
* With true, if the row doesn't exist - it will throw an error.
*
* Use true when you expect the row to exist and it would be an error if it doesn't.
*/
requireToExist?: boolean
}

export interface CommonDaoPatchOptions<DBM extends BaseDBEntity>
extends CommonDaoSaveBatchOptions<DBM> {
/**
Expand Down
18 changes: 18 additions & 0 deletions src/commondao/common.dao.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
BaseDBEntity,
ErrorMode,
pExpectedError,
pExpectedErrorString,
pTry,
} from '@naturalcycles/js-lib'
import {
Expand Down Expand Up @@ -172,6 +173,23 @@ test('patchById', async () => {
`)
})

test('patchById requireToExist', async () => {
const id = '123456'
expect(
await pExpectedErrorString(
dao.patchById(
id,
{
k1: 'k111',
},
{
requireToExist: true,
},
),
),
).toMatchInlineSnapshot(`"AssertionError: TEST_TABLE.patchById(123456) is required, but missing"`)
})

test('patch', async () => {
const item: TestItemBM = await dao.save({
id: 'id1',
Expand Down
6 changes: 4 additions & 2 deletions src/commondao/common.dao.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import {
CommonDaoHooks,
CommonDaoLogLevel,
CommonDaoOptions,
CommonDaoPatchByIdOptions,
CommonDaoPatchOptions,
CommonDaoSaveBatchOptions,
CommonDaoSaveOptions,
Expand Down Expand Up @@ -635,7 +636,7 @@ export class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM> {
async patchById(
id: string,
patch: Partial<BM>,
opt: CommonDaoSaveBatchOptions<DBM> = {},
opt: CommonDaoPatchByIdOptions<DBM> = {},
): Promise<BM> {
if (this.cfg.patchInTransaction && !opt.tx) {
// patchInTransaction means that we should run this op in Transaction
Expand All @@ -655,6 +656,7 @@ export class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM> {
return patched
}
} else {
_assert(!opt.requireToExist, `${this.cfg.table}.patchById(${id}) is required, but missing`)
patched = this.create({ ...patch, id }, opt)
}

Expand All @@ -667,7 +669,7 @@ export class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM> {
async patchByIdInTransaction(
id: string,
patch: Partial<BM>,
opt?: CommonDaoSaveBatchOptions<DBM>,
opt?: CommonDaoPatchByIdOptions<DBM>,
): Promise<BM> {
return await this.runInTransaction(async daoTx => {
return await this.patchById(id, patch, { ...opt, tx: daoTx.tx })
Expand Down
76 changes: 38 additions & 38 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -281,14 +281,14 @@
integrity sha512-ooWCrlZP11i8GImSjTHYHLkvFDP48nS4+204nGb1RiX/WXYHmJA2III9/e2DWVabCESdW7hBAEzHRqUn9OUVvQ==

"@commitlint/cli@^19.0.0":
version "19.3.0"
resolved "https://registry.yarnpkg.com/@commitlint/cli/-/cli-19.3.0.tgz#44e6da9823a01f0cdcc43054bbefdd2c6c5ddf39"
integrity sha512-LgYWOwuDR7BSTQ9OLZ12m7F/qhNY+NpAyPBgo4YNMkACE7lGuUnuQq1yi9hz1KA4+3VqpOYl8H1rY/LYK43v7g==
version "19.4.0"
resolved "https://registry.yarnpkg.com/@commitlint/cli/-/cli-19.4.0.tgz#9f93d3ed07e531fcfa371015c8c87e0aa26d974f"
integrity sha512-sJX4J9UioVwZHq7JWM9tjT5bgWYaIN3rC4FP7YwfEwBYiIO+wMyRttRvQLNkow0vCdM0D67r9NEWU0Ui03I4Eg==
dependencies:
"@commitlint/format" "^19.3.0"
"@commitlint/lint" "^19.2.2"
"@commitlint/load" "^19.2.0"
"@commitlint/read" "^19.2.1"
"@commitlint/load" "^19.4.0"
"@commitlint/read" "^19.4.0"
"@commitlint/types" "^19.0.3"
execa "^8.0.1"
yargs "^17.0.0"
Expand Down Expand Up @@ -352,10 +352,10 @@
"@commitlint/rules" "^19.0.3"
"@commitlint/types" "^19.0.3"

"@commitlint/load@^19.2.0":
version "19.2.0"
resolved "https://registry.yarnpkg.com/@commitlint/load/-/load-19.2.0.tgz#3ca51fdead4f1e1e09c9c7df343306412b1ef295"
integrity sha512-XvxxLJTKqZojCxaBQ7u92qQLFMMZc4+p9qrIq/9kJDy8DOrEa7P1yx7Tjdc2u2JxIalqT4KOGraVgCE7eCYJyQ==
"@commitlint/load@^19.4.0":
version "19.4.0"
resolved "https://registry.yarnpkg.com/@commitlint/load/-/load-19.4.0.tgz#7df034e226e300fd577d3f63a72d790d5c821f53"
integrity sha512-I4lCWaEZYQJ1y+Y+gdvbGAx9pYPavqZAZ3/7/8BpWh+QjscAn8AjsUpLV2PycBsEx7gupq5gM4BViV9xwTIJuw==
dependencies:
"@commitlint/config-validator" "^19.0.3"
"@commitlint/execute-rule" "^19.0.0"
Expand All @@ -382,10 +382,10 @@
conventional-changelog-angular "^7.0.0"
conventional-commits-parser "^5.0.0"

"@commitlint/read@^19.2.1":
version "19.2.1"
resolved "https://registry.yarnpkg.com/@commitlint/read/-/read-19.2.1.tgz#7296b99c9a989e60e5927fff8388a1dd44299c2f"
integrity sha512-qETc4+PL0EUv7Q36lJbPG+NJiBOGg7SSC7B5BsPWOmei+Dyif80ErfWQ0qXoW9oCh7GTpTNRoaVhiI8RbhuaNw==
"@commitlint/read@^19.4.0":
version "19.4.0"
resolved "https://registry.yarnpkg.com/@commitlint/read/-/read-19.4.0.tgz#3866b1f9a272ef6a388986efa349d24228fc8b00"
integrity sha512-r95jLOEZzKDakXtnQub+zR3xjdnrl2XzerPwm7ch1/cc5JGq04tyaNpa6ty0CRCWdVrk4CZHhqHozb8yZwy2+g==
dependencies:
"@commitlint/top-level" "^19.0.0"
"@commitlint/types" "^19.0.3"
Expand Down Expand Up @@ -929,9 +929,9 @@
typescript "^5.0.2"

"@naturalcycles/dev-lib@^15.4.1":
version "15.4.1"
resolved "https://registry.yarnpkg.com/@naturalcycles/dev-lib/-/dev-lib-15.4.1.tgz#60d3c6ff43b9eba5e7c526025cf5d657ae4d0e1c"
integrity sha512-FEsaBI8ONbKomFJpkjkCgCDV7tmvi3IgI9mhJlIi4t5qlGvMgE/4syVk4Hp2cgCuoIQKO9zcpu9+mefXnC7GrA==
version "15.4.3"
resolved "https://registry.yarnpkg.com/@naturalcycles/dev-lib/-/dev-lib-15.4.3.tgz#b61b33a59bf08929f479f0e131737345012a8417"
integrity sha512-1VbHTJEPCAO/HvNjZDX1feDaj8Tit7u8QRa4rKXb0Swp0m7nzJm1DjtWFbE19wkgiR/zA/Pas0UAEQZn214Tlg==
dependencies:
"@commitlint/cli" "^19.0.0"
"@commitlint/config-conventional" "^19.0.0"
Expand All @@ -947,10 +947,10 @@
eslint-config-prettier "^9.0.0"
eslint-plugin-import "^2.22.1"
eslint-plugin-jest "^28.0.0"
eslint-plugin-jsdoc "^48.0.1"
eslint-plugin-jsdoc "^50.0.0"
eslint-plugin-simple-import-sort "^12.1.1"
eslint-plugin-unicorn "^55.0.0"
eslint-plugin-unused-imports "^4.0.0"
eslint-plugin-unused-imports "4.0.1"
eslint-plugin-vue "^9.0.0"
expect-type "^0.19.0"
globals "^15.8.0"
Expand All @@ -967,9 +967,9 @@
yargs "^17.0.0"

"@naturalcycles/js-lib@^14.0.0", "@naturalcycles/js-lib@^14.116.0", "@naturalcycles/js-lib@^14.244.0":
version "14.249.0"
resolved "https://registry.yarnpkg.com/@naturalcycles/js-lib/-/js-lib-14.249.0.tgz#0d98397aeb438caa71289df23319c5bbd681e616"
integrity sha512-0sTAaLPCnHFkXu1KCRgHmupJHA6brK2usSFp7OWQJEWZQkjhD66Fv8WJiKrLC/97z6K3xrX8S3B6pzOObWs/Ug==
version "14.251.0"
resolved "https://registry.yarnpkg.com/@naturalcycles/js-lib/-/js-lib-14.251.0.tgz#704f82a077c63795ea18aea968e37362c87a4f13"
integrity sha512-O8PH9vCc7UeMPvKQuoXU/Y62T+GDBRKePSgIOiW3pectYoB3PqSVTcNsUI2XqW17h0F2BoZX0dB4vaHcm+K6+w==
dependencies:
tslib "^2.0.0"
zod "^3.20.2"
Expand Down Expand Up @@ -1228,9 +1228,9 @@
"@types/yargs-parser" "*"

"@types/yargs@^17.0.8":
version "17.0.32"
resolved "https://registry.yarnpkg.com/@types/yargs/-/yargs-17.0.32.tgz#030774723a2f7faafebf645f4e5a48371dca6229"
integrity sha512-xQ67Yc/laOG5uMfX/093MRlGGCIBzZMarVa+gfNKJxWAIgykYpVGkBdbqEzGDDfCrVUj6Hiff4mTZ5BA6TmAog==
version "17.0.33"
resolved "https://registry.yarnpkg.com/@types/yargs/-/yargs-17.0.33.tgz#8c32303da83eec050a84b3c7ae7b9f922d13e32d"
integrity sha512-WpxBCKWPLr4xSsHgz511rFJAM+wS28w2zEO1QDNY5zM/S8ok70NNfztH0xwhqKyaK0OHCbN98LDAZuy1ctxDkA==
dependencies:
"@types/yargs-parser" "*"

Expand Down Expand Up @@ -1753,9 +1753,9 @@ camelcase@^6.2.0:
integrity sha512-Gmy6FhYlCY7uOElZUSbxo2UCDH8owEk996gkbrpsgGtrJLM3J7jGxl9Ic7Qwwj4ivOE5AWZWRMecDdF7hqGjFA==

caniuse-lite@^1.0.30001646:
version "1.0.30001649"
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001649.tgz#3ec700309ca0da2b0d3d5fb03c411b191761c992"
integrity sha512-fJegqZZ0ZX8HOWr6rcafGr72+xcgJKI9oWfDW5DrD7ExUtgZC7a7R7ZYmZqplh7XDocFdGeIFn7roAxhOeYrPQ==
version "1.0.30001651"
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001651.tgz#52de59529e8b02b1aedcaaf5c05d9e23c0c28138"
integrity sha512-9Cf+Xv1jJNe1xPZLGuUXLNkE1BoDkqRqYyFJ9TDYSqhduqA4hu4oR9HluGoWYQC/aj8WHjsGVV+bwkh0+tegRg==

chalk@^2.4.2:
version "2.4.2"
Expand Down Expand Up @@ -2168,9 +2168,9 @@ ejs@^3.1.10:
jake "^10.8.5"

electron-to-chromium@^1.5.4:
version "1.5.4"
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.5.4.tgz#cd477c830dd6fca41fbd5465c1ff6ce08ac22343"
integrity sha512-orzA81VqLyIGUEA77YkVA1D+N+nNfl2isJVjjmOyrlxuooZ19ynb+dOlaDTqd/idKRS9lDCSBmtzM+kyCsMnkA==
version "1.5.5"
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.5.5.tgz#03bfdf422bdd2c05ee2657efedde21264a1a566b"
integrity sha512-QR7/A7ZkMS8tZuoftC/jfqNkZLQO779SSW3YuZHP4eXpj3EffGLFcB/Xu9AAZQzLccTiCV+EmUo3ha4mQ9wnlA==

emittery@^0.13.1:
version "0.13.1"
Expand Down Expand Up @@ -2370,16 +2370,16 @@ eslint-plugin-import@^2.22.1:
tsconfig-paths "^3.15.0"

eslint-plugin-jest@^28.0.0:
version "28.7.0"
resolved "https://registry.yarnpkg.com/eslint-plugin-jest/-/eslint-plugin-jest-28.7.0.tgz#8ff262c26520242492f85f9268995bbf624758a4"
integrity sha512-fzPGN7awL2ftVRQh/bsCi+16ArUZWujZnD1b8EGJqy8nr4//7tZ3BIdc/9edcJBtB3hpci3GtdMNFVDwHU0Eag==
version "28.8.0"
resolved "https://registry.yarnpkg.com/eslint-plugin-jest/-/eslint-plugin-jest-28.8.0.tgz#54f597b5a3295ad04ec946baa245ad02b9b2bca0"
integrity sha512-Tubj1hooFxCl52G4qQu0edzV/+EZzPUeN8p2NnW5uu4fbDs+Yo7+qDVDc4/oG3FbCqEBmu/OC3LSsyiU22oghw==
dependencies:
"@typescript-eslint/utils" "^6.0.0 || ^7.0.0 || ^8.0.0"

eslint-plugin-jsdoc@^48.0.1:
version "48.11.0"
resolved "https://registry.yarnpkg.com/eslint-plugin-jsdoc/-/eslint-plugin-jsdoc-48.11.0.tgz#7c8dae6ce0d814aff54b87fdb808f02635691ade"
integrity sha512-d12JHJDPNo7IFwTOAItCeJY1hcqoIxE0lHA8infQByLilQ9xkqrRa6laWCnsuCrf+8rUnvxXY1XuTbibRBNylA==
eslint-plugin-jsdoc@^50.0.0:
version "50.0.0"
resolved "https://registry.yarnpkg.com/eslint-plugin-jsdoc/-/eslint-plugin-jsdoc-50.0.0.tgz#0d064e14e1a8a3624c0474359fc51325b38b0fc9"
integrity sha512-czyJ5F7/qY2LIhUD5Bl6q1CCZ8mjvfEA9HQN5nvIp/Pb8VLIlUNd+DMZdA2OKN74QQMS3pobC06hFqAOJyOv5Q==
dependencies:
"@es-joy/jsdoccomment" "~0.46.0"
are-docs-informative "^0.0.2"
Expand Down Expand Up @@ -2420,7 +2420,7 @@ eslint-plugin-unicorn@^55.0.0:
semver "^7.6.1"
strip-indent "^3.0.0"

eslint-plugin-unused-imports@^4.0.0:
[email protected].1:
version "4.0.1"
resolved "https://registry.yarnpkg.com/eslint-plugin-unused-imports/-/eslint-plugin-unused-imports-4.0.1.tgz#b78916ecb82ae6d0f54efb33879c6c7ecb52f080"
integrity sha512-rax76s05z64uQgG9YXsWFmXrgjkaK79AvfeAWiSxhPP6RVGxeRaj4+2u+wxxu/mDy2pmJoOy1QTOEALMia2xGQ==
Expand Down

0 comments on commit 3bddbdb

Please sign in to comment.