@lokalise/prisma-utils 1.0.2 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,8 @@
1
+ import { PrismaClientKnownRequestError } from '@prisma/client/runtime/library';
2
+
3
+ /**
4
+ * Check if the error is a CockroachDB transaction retry error
5
+ *
6
+ * @param error
7
+ */
8
+ export declare const isCockroachDBRetryTransaction: (error: PrismaClientKnownRequestError) => boolean;
@@ -0,0 +1,8 @@
1
+ const o = "40001", r = (e) => {
2
+ const t = e.meta;
3
+ return t ? t.code === o : !1;
4
+ };
5
+ export {
6
+ r as isCockroachDBRetryTransaction
7
+ };
8
+ //# sourceMappingURL=cockroachdbError.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cockroachdbError.js","sources":["../src/cockroachdbError.ts"],"sourcesContent":["import type { PrismaClientKnownRequestError } from '@prisma/client/runtime/library'\n\n/**\n * https://www.cockroachlabs.com/docs/stable/transaction-retry-error-reference#:~:text=To%20indicate%20that%20a%20transaction,the%20string%20%22restart%20transaction%22%20.\n *\n * All transaction retry errors use the SQLSTATE error code 40001\n */\nconst COCKROACHDB_RETRY_TRANSACTION_CODE = '40001'\n\n/**\n * Check if the error is a CockroachDB transaction retry error\n *\n * @param error\n */\nexport const isCockroachDBRetryTransaction = (error: PrismaClientKnownRequestError): boolean => {\n\tconst meta = error.meta\n\tif (!meta) return false\n\n\treturn meta.code === COCKROACHDB_RETRY_TRANSACTION_CODE\n}\n"],"names":["COCKROACHDB_RETRY_TRANSACTION_CODE","isCockroachDBRetryTransaction","error","meta"],"mappings":"AAOA,MAAMA,IAAqC,SAO9BC,IAAgC,CAACC,MAAkD;AAC/F,QAAMC,IAAOD,EAAM;AACnB,SAAKC,IAEEA,EAAK,SAASH,IAFH;AAGnB;"}
package/dist/index.cjs CHANGED
@@ -1,2 +1,2 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const i=require("@lokalise/node-core"),o=r=>!!r&&i.isError(r)&&"code"in r&&typeof r.code=="string"&&r.code.startsWith("P"),a="P2025",R="P2034",c="P2002",I=async(r,n,t={retriesAllowed:3})=>{let e,s=0;for(;s<t.retriesAllowed&&(e=await _(r,n,t),!(e.result||!o(e.error)||e.error.code!==R));)s++;return e??{error:new Error("No transaction retry executed")}},_=async(r,n,t)=>{try{return{result:await r.$transaction(n,t)}}catch(e){return{error:e}}};exports.PRISMA_NOT_FOUND_ERROR=a;exports.PRISMA_SERIALIZATION_ERROR=R;exports.PRISMA_UNIQUE_CONSTRAINT_ERROR=c;exports.isPrismaClientKnownRequestError=o;exports.prismaTransaction=I;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const i=require("@lokalise/node-core"),s=r=>!!r&&i.isError(r)&&"code"in r&&typeof r.code=="string"&&r.code.startsWith("P"),R="P2025",c="P2034",a="P2002",u={COCKROACHDB:"CockroachDb"},O="40001",A=r=>{const e=r.meta;return e?e.code===O:!1},_=async(r,e,t={retriesAllowed:3})=>{let n,o=0;for(;o<t.retriesAllowed&&(n=await l(r,e,t),!(n.result||!E(n,t.DbDriver??"CockroachDb")));)o++;return n??{error:new Error("No transaction retry executed")}},l=async(r,e,t)=>{try{return{result:await r.$transaction(e,t)}}catch(n){return{error:n}}},E=(r,e)=>{if(s(r.error)){const t=r.error;if(t.code===c||e==="CockroachDb"&&A(t))return!0}return!1};exports.DbDriverEnum=u;exports.PRISMA_NOT_FOUND_ERROR=R;exports.PRISMA_SERIALIZATION_ERROR=c;exports.PRISMA_UNIQUE_CONSTRAINT_ERROR=a;exports.isPrismaClientKnownRequestError=s;exports.prismaTransaction=_;
2
2
  //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","sources":["../src/prismaError.ts","../src/prismaTransaction.ts"],"sourcesContent":["import { isError } from '@lokalise/node-core'\nimport type { PrismaClientKnownRequestError } from '@prisma/client/runtime/library'\n\n/**\n * What is checked?\n * \t1. error is defined and not null\n * \t2. error is an `Error`\n * \t3. error contains the field code which is a string\n * \t4. code starts by `P` ([doc](https://www.prisma.io/docs/reference/api-reference/error-reference#error-codes))\n */\nexport const isPrismaClientKnownRequestError = (\n\terror: unknown,\n): error is PrismaClientKnownRequestError =>\n\t!!error &&\n\tisError(error) &&\n\t'code' in error &&\n\ttypeof error.code === 'string' &&\n\terror.code.startsWith('P')\n\n/*\n * Prisma error code P2025 indicates that the operation failed because it depends on one or more\n * records that were required but not found\n */\nexport const PRISMA_NOT_FOUND_ERROR = 'P2025'\n\n/*\n * Prisma error code P2034 indicates a serialization error and that the transaction must be retried.\n * A different error code would indicate that an internal state error happened and that\n * the cluster itself is experiencing an issue which requires intervention\n */\nexport const PRISMA_SERIALIZATION_ERROR = 'P2034'\n\n/*\n * Prisma error code P2002 indicates that the operation failed because a unique constraint was\n * violated. This can happen if you try to create a record with a unique field that already exists\n */\nexport const PRISMA_UNIQUE_CONSTRAINT_ERROR = 'P2002'\n","import type { Either } from '@lokalise/node-core'\nimport type { PrismaClient, Prisma } from '@prisma/client'\nimport type * as runtime from '@prisma/client/runtime/library'\n\nimport { isPrismaClientKnownRequestError, PRISMA_SERIALIZATION_ERROR } from './prismaError'\n\nexport type PrismaTransactionOptions = {\n\tretriesAllowed: number\n\tmaxWait?: number\n\ttimeout?: number\n\tisolationLevel?: string\n}\n\nexport type PrismaTransactionBasicOptions = Pick<\n\tPrismaTransactionOptions,\n\t'retriesAllowed' | 'isolationLevel'\n>\n\nexport type PrismaTransactionClient<P> = Omit<P, runtime.ITXClientDenyList>\n\nexport type PrismaTransactionFn<T, P> = (prisma: PrismaTransactionClient<P>) => Promise<T>\n\ntype PrismaTransactionReturnType<T> = Either<\n\tunknown,\n\tT | runtime.Types.Utils.UnwrapTuple<Prisma.PrismaPromise<unknown>[]>\n>\n\n/**\n * Perform a Prisma DB transaction with automatic retries if needed.\n *\n * @template T | T extends Prisma.PrismaPromise<unknown>[]\n * @param {PrismaClient} prisma\n * @param {PrismaTransactionFn<T> | Prisma.PrismaPromise<unknown>[]} arg\t\t operation to perform into the transaction\n * @param {PrismaTransactionOptions | PrismaTransactionBasicOptions} options transaction configuration\n * @return {Promise<PrismaTransactionReturnType<T>>}\n */\nexport const prismaTransaction = (async <T, P extends PrismaClient>(\n\tprisma: P,\n\targ: PrismaTransactionFn<T, P>,\n\toptions: PrismaTransactionOptions = { retriesAllowed: 3 },\n): Promise<PrismaTransactionReturnType<T>> => {\n\tlet result: PrismaTransactionReturnType<T> | undefined = undefined\n\n\tlet retries = 0\n\twhile (retries < options.retriesAllowed) {\n\t\tresult = await executeTransactionTry(prisma, arg, options)\n\n\t\tif (\n\t\t\tresult.result ||\n\t\t\t!isPrismaClientKnownRequestError(result.error) ||\n\t\t\tresult.error.code !== PRISMA_SERIALIZATION_ERROR\n\t\t) {\n\t\t\tbreak\n\t\t}\n\n\t\tretries++\n\t}\n\n\treturn result ?? { error: new Error('No transaction retry executed') }\n}) as {\n\t<T, P extends PrismaClient>(\n\t\tprisma: P,\n\t\tfn: PrismaTransactionFn<T, P>,\n\t\toptions?: PrismaTransactionOptions,\n\t): Promise<Either<unknown, T>>\n\t<T extends Prisma.PrismaPromise<unknown>[], P extends PrismaClient>(\n\t\tprisma: P,\n\t\targs: [...T],\n\t\toptions?: PrismaTransactionBasicOptions,\n\t): Promise<Either<unknown, runtime.Types.Utils.UnwrapTuple<T>>>\n}\n\nconst executeTransactionTry = async <T, P extends PrismaClient>(\n\tprisma: P,\n\targ: PrismaTransactionFn<T, P>,\n\toptions: PrismaTransactionOptions,\n): Promise<PrismaTransactionReturnType<T>> => {\n\ttry {\n\t\treturn {\n\t\t\t// @ts-ignore\n\t\t\tresult: await prisma.$transaction<T>(arg, options),\n\t\t}\n\t} catch (error) {\n\t\treturn { error }\n\t}\n}\n"],"names":["isPrismaClientKnownRequestError","error","isError","PRISMA_NOT_FOUND_ERROR","PRISMA_SERIALIZATION_ERROR","PRISMA_UNIQUE_CONSTRAINT_ERROR","prismaTransaction","prisma","arg","options","result","retries","executeTransactionTry"],"mappings":"uHAUaA,EACZC,GAEA,CAAC,CAACA,GACFC,UAAQD,CAAK,GACb,SAAUA,GACV,OAAOA,EAAM,MAAS,UACtBA,EAAM,KAAK,WAAW,GAAG,EAMbE,EAAyB,QAOzBC,EAA6B,QAM7BC,EAAiC,QCAjCC,EAAqB,MACjCC,EACAC,EACAC,EAAoC,CAAE,eAAgB,KACT,CAC7C,IAAIC,EAEAC,EAAU,EACP,KAAAA,EAAUF,EAAQ,iBACxBC,EAAS,MAAME,EAAsBL,EAAQC,EAAKC,CAAO,EAGxD,EAAAC,EAAO,QACP,CAACV,EAAgCU,EAAO,KAAK,GAC7CA,EAAO,MAAM,OAASN,KAKvBO,IAGD,OAAOD,GAAU,CAAE,MAAO,IAAI,MAAM,+BAA+B,CAAE,CACtE,EAaME,EAAwB,MAC7BL,EACAC,EACAC,IAC6C,CACzC,GAAA,CACI,MAAA,CAEN,OAAQ,MAAMF,EAAO,aAAgBC,EAAKC,CAAO,CAAA,QAE1CR,EAAO,CACf,MAAO,CAAE,MAAAA,CAAM,CAChB,CACD"}
1
+ {"version":3,"file":"index.cjs","sources":["../src/prismaError.ts","../src/types.ts","../src/cockroachdbError.ts","../src/prismaTransaction.ts"],"sourcesContent":["import { isError } from '@lokalise/node-core'\nimport type { PrismaClientKnownRequestError } from '@prisma/client/runtime/library'\n\n/**\n * What is checked?\n * \t1. error is defined and not null\n * \t2. error is an `Error`\n * \t3. error contains the field code which is a string\n * \t4. code starts by `P` ([doc](https://www.prisma.io/docs/reference/api-reference/error-reference#error-codes))\n */\nexport const isPrismaClientKnownRequestError = (\n\terror: unknown,\n): error is PrismaClientKnownRequestError =>\n\t!!error &&\n\tisError(error) &&\n\t'code' in error &&\n\ttypeof error.code === 'string' &&\n\terror.code.startsWith('P')\n\n/*\n * Prisma error code P2025 indicates that the operation failed because it depends on one or more\n * records that were required but not found\n */\nexport const PRISMA_NOT_FOUND_ERROR = 'P2025'\n\n/*\n * Prisma error code P2034 indicates a serialization error and that the transaction must be retried.\n * A different error code would indicate that an internal state error happened and that\n * the cluster itself is experiencing an issue which requires intervention\n */\nexport const PRISMA_SERIALIZATION_ERROR = 'P2034'\n\n/*\n * Prisma error code P2002 indicates that the operation failed because a unique constraint was\n * violated. This can happen if you try to create a record with a unique field that already exists\n */\nexport const PRISMA_UNIQUE_CONSTRAINT_ERROR = 'P2002'\n","import type * as runtime from '@prisma/client/runtime/library'\n\ntype ObjectValues<T> = T[keyof T]\n\nexport const DbDriverEnum = {\n\tCOCKROACHDB: 'CockroachDb',\n} as const\nexport type DbDriver = ObjectValues<typeof DbDriverEnum>\n\nexport type PrismaTransactionOptions = {\n\tretriesAllowed: number\n\tmaxWait?: number\n\ttimeout?: number\n\tisolationLevel?: string\n\tDbDriver?: DbDriver\n}\n\nexport type PrismaTransactionBasicOptions = Pick<\n\tPrismaTransactionOptions,\n\t'retriesAllowed' | 'isolationLevel' | 'DbDriver'\n>\n\nexport type PrismaTransactionClient<P> = Omit<P, runtime.ITXClientDenyList>\n\nexport type PrismaTransactionFn<T, P> = (prisma: PrismaTransactionClient<P>) => Promise<T>\n","import type { PrismaClientKnownRequestError } from '@prisma/client/runtime/library'\n\n/**\n * https://www.cockroachlabs.com/docs/stable/transaction-retry-error-reference#:~:text=To%20indicate%20that%20a%20transaction,the%20string%20%22restart%20transaction%22%20.\n *\n * All transaction retry errors use the SQLSTATE error code 40001\n */\nconst COCKROACHDB_RETRY_TRANSACTION_CODE = '40001'\n\n/**\n * Check if the error is a CockroachDB transaction retry error\n *\n * @param error\n */\nexport const isCockroachDBRetryTransaction = (error: PrismaClientKnownRequestError): boolean => {\n\tconst meta = error.meta\n\tif (!meta) return false\n\n\treturn meta.code === COCKROACHDB_RETRY_TRANSACTION_CODE\n}\n","import type { Either } from '@lokalise/node-core'\nimport type { PrismaClient, Prisma } from '@prisma/client'\nimport type * as runtime from '@prisma/client/runtime/library'\n\nimport { isCockroachDBRetryTransaction } from './cockroachdbError'\nimport { isPrismaClientKnownRequestError, PRISMA_SERIALIZATION_ERROR } from './prismaError'\nimport type {\n\tDbDriver,\n\tPrismaTransactionBasicOptions,\n\tPrismaTransactionFn,\n\tPrismaTransactionOptions,\n} from './types'\n\ntype PrismaTransactionReturnType<T> = Either<\n\tunknown,\n\tT | runtime.Types.Utils.UnwrapTuple<Prisma.PrismaPromise<unknown>[]>\n>\n\n/**\n * Perform a Prisma DB transaction with automatic retries if needed.\n *\n * @template T | T extends Prisma.PrismaPromise<unknown>[]\n * @param {PrismaClient} prisma\n * @param {PrismaTransactionFn<T> | Prisma.PrismaPromise<unknown>[]} arg\t operation to perform into the transaction\n * @param {PrismaTransactionOptions | PrismaTransactionBasicOptions} options transaction configuration\n * @return {Promise<PrismaTransactionReturnType<T>>}\n */\nexport const prismaTransaction = (async <T, P extends PrismaClient>(\n\tprisma: P,\n\targ: PrismaTransactionFn<T, P>,\n\toptions: PrismaTransactionOptions = { retriesAllowed: 3 },\n): Promise<PrismaTransactionReturnType<T>> => {\n\tlet result: PrismaTransactionReturnType<T> | undefined = undefined\n\n\tlet retries = 0\n\twhile (retries < options.retriesAllowed) {\n\t\tresult = await executeTransactionTry(prisma, arg, options)\n\n\t\tif (result.result || !isRetryAllowed(result, options.DbDriver ?? 'CockroachDb')) {\n\t\t\tbreak\n\t\t}\n\n\t\tretries++\n\t}\n\n\treturn result ?? { error: new Error('No transaction retry executed') }\n}) as {\n\t<T, P extends PrismaClient>(\n\t\tprisma: P,\n\t\tfn: PrismaTransactionFn<T, P>,\n\t\toptions?: PrismaTransactionOptions,\n\t): Promise<Either<unknown, T>>\n\t<T extends Prisma.PrismaPromise<unknown>[], P extends PrismaClient>(\n\t\tprisma: P,\n\t\targs: [...T],\n\t\toptions?: PrismaTransactionBasicOptions,\n\t): Promise<Either<unknown, runtime.Types.Utils.UnwrapTuple<T>>>\n}\n\nconst executeTransactionTry = async <T, P extends PrismaClient>(\n\tprisma: P,\n\targ: PrismaTransactionFn<T, P>,\n\toptions: PrismaTransactionOptions,\n): Promise<PrismaTransactionReturnType<T>> => {\n\ttry {\n\t\treturn {\n\t\t\t// @ts-ignore\n\t\t\tresult: await prisma.$transaction<T>(arg, options),\n\t\t}\n\t} catch (error) {\n\t\treturn { error }\n\t}\n}\n\nconst isRetryAllowed = <T>(result: PrismaTransactionReturnType<T>, dbDriver: DbDriver): boolean => {\n\tif (isPrismaClientKnownRequestError(result.error)) {\n\t\tconst error = result.error\n\t\tif (error.code === PRISMA_SERIALIZATION_ERROR) return true\n\t\tif (dbDriver === 'CockroachDb' && isCockroachDBRetryTransaction(error)) return true\n\t}\n\n\treturn false\n}\n"],"names":["isPrismaClientKnownRequestError","error","isError","PRISMA_NOT_FOUND_ERROR","PRISMA_SERIALIZATION_ERROR","PRISMA_UNIQUE_CONSTRAINT_ERROR","DbDriverEnum","COCKROACHDB_RETRY_TRANSACTION_CODE","isCockroachDBRetryTransaction","meta","prismaTransaction","prisma","arg","options","result","retries","executeTransactionTry","isRetryAllowed","dbDriver"],"mappings":"uHAUaA,EACZC,GAEA,CAAC,CAACA,GACFC,UAAQD,CAAK,GACb,SAAUA,GACV,OAAOA,EAAM,MAAS,UACtBA,EAAM,KAAK,WAAW,GAAG,EAMbE,EAAyB,QAOzBC,EAA6B,QAM7BC,EAAiC,QChCjCC,EAAe,CAC3B,YAAa,aACd,ECCMC,EAAqC,QAO9BC,EAAiCP,GAAkD,CAC/F,MAAMQ,EAAOR,EAAM,KACnB,OAAKQ,EAEEA,EAAK,OAASF,EAFH,EAGnB,ECQaG,EAAqB,MACjCC,EACAC,EACAC,EAAoC,CAAE,eAAgB,KACT,CAC7C,IAAIC,EAEAC,EAAU,EACP,KAAAA,EAAUF,EAAQ,iBACxBC,EAAS,MAAME,EAAsBL,EAAQC,EAAKC,CAAO,EAErD,EAAAC,EAAO,QAAU,CAACG,EAAeH,EAAQD,EAAQ,UAAY,aAAa,KAI9EE,IAGD,OAAOD,GAAU,CAAE,MAAO,IAAI,MAAM,+BAA+B,CAAE,CACtE,EAaME,EAAwB,MAC7BL,EACAC,EACAC,IAC6C,CACzC,GAAA,CACI,MAAA,CAEN,OAAQ,MAAMF,EAAO,aAAgBC,EAAKC,CAAO,CAAA,QAE1CZ,EAAO,CACf,MAAO,CAAE,MAAAA,CAAM,CAChB,CACD,EAEMgB,EAAiB,CAAIH,EAAwCI,IAAgC,CAC9F,GAAAlB,EAAgCc,EAAO,KAAK,EAAG,CAClD,MAAMb,EAAQa,EAAO,MAEjB,GADAb,EAAM,OAASG,GACfc,IAAa,eAAiBV,EAA8BP,CAAK,EAAU,MAAA,EAChF,CAEO,MAAA,EACR"}
package/dist/index.d.ts CHANGED
@@ -1,3 +1,3 @@
1
1
  export * from './prismaError';
2
+ export * from './types';
2
3
  export { prismaTransaction } from './prismaTransaction';
3
- export type { PrismaTransactionOptions, PrismaTransactionBasicOptions } from './prismaTransaction';
package/dist/index.js CHANGED
@@ -1,10 +1,12 @@
1
- import { PRISMA_NOT_FOUND_ERROR as I, PRISMA_SERIALIZATION_ERROR as _, PRISMA_UNIQUE_CONSTRAINT_ERROR as o, isPrismaClientKnownRequestError as O } from "./prismaError.js";
2
- import { prismaTransaction as E } from "./prismaTransaction.js";
1
+ import { PRISMA_NOT_FOUND_ERROR as o, PRISMA_SERIALIZATION_ERROR as I, PRISMA_UNIQUE_CONSTRAINT_ERROR as _, isPrismaClientKnownRequestError as e } from "./prismaError.js";
2
+ import { DbDriverEnum as O } from "./types.js";
3
+ import { prismaTransaction as m } from "./prismaTransaction.js";
3
4
  export {
4
- I as PRISMA_NOT_FOUND_ERROR,
5
- _ as PRISMA_SERIALIZATION_ERROR,
6
- o as PRISMA_UNIQUE_CONSTRAINT_ERROR,
7
- O as isPrismaClientKnownRequestError,
8
- E as prismaTransaction
5
+ O as DbDriverEnum,
6
+ o as PRISMA_NOT_FOUND_ERROR,
7
+ I as PRISMA_SERIALIZATION_ERROR,
8
+ _ as PRISMA_UNIQUE_CONSTRAINT_ERROR,
9
+ e as isPrismaClientKnownRequestError,
10
+ m as prismaTransaction
9
11
  };
10
12
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;"}
1
+ {"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;"}
@@ -1,22 +1,14 @@
1
- import { PrismaClient, Prisma } from '@prisma/client';
2
1
  import { Either } from '@lokalise/node-core';
2
+ import { PrismaClient, Prisma } from '@prisma/client';
3
+ import { PrismaTransactionBasicOptions, PrismaTransactionFn, PrismaTransactionOptions } from './types';
3
4
 
4
5
  import type * as runtime from '@prisma/client/runtime/library';
5
- export type PrismaTransactionOptions = {
6
- retriesAllowed: number;
7
- maxWait?: number;
8
- timeout?: number;
9
- isolationLevel?: string;
10
- };
11
- export type PrismaTransactionBasicOptions = Pick<PrismaTransactionOptions, 'retriesAllowed' | 'isolationLevel'>;
12
- export type PrismaTransactionClient<P> = Omit<P, runtime.ITXClientDenyList>;
13
- export type PrismaTransactionFn<T, P> = (prisma: PrismaTransactionClient<P>) => Promise<T>;
14
6
  /**
15
7
  * Perform a Prisma DB transaction with automatic retries if needed.
16
8
  *
17
9
  * @template T | T extends Prisma.PrismaPromise<unknown>[]
18
10
  * @param {PrismaClient} prisma
19
- * @param {PrismaTransactionFn<T> | Prisma.PrismaPromise<unknown>[]} arg operation to perform into the transaction
11
+ * @param {PrismaTransactionFn<T> | Prisma.PrismaPromise<unknown>[]} arg operation to perform into the transaction
20
12
  * @param {PrismaTransactionOptions | PrismaTransactionBasicOptions} options transaction configuration
21
13
  * @return {Promise<PrismaTransactionReturnType<T>>}
22
14
  */
@@ -1,20 +1,28 @@
1
- import { isPrismaClientKnownRequestError as n, PRISMA_SERIALIZATION_ERROR as s } from "./prismaError.js";
2
- const l = async (t, o, e = { retriesAllowed: 3 }) => {
3
- let r, a = 0;
4
- for (; a < e.retriesAllowed && (r = await i(t, o, e), !(r.result || !n(r.error) || r.error.code !== s)); )
1
+ import { isCockroachDBRetryTransaction as n } from "./cockroachdbError.js";
2
+ import { isPrismaClientKnownRequestError as i, PRISMA_SERIALIZATION_ERROR as c } from "./prismaError.js";
3
+ const R = async (e, o, r = { retriesAllowed: 3 }) => {
4
+ let t, a = 0;
5
+ for (; a < r.retriesAllowed && (t = await s(e, o, r), !(t.result || !l(t, r.DbDriver ?? "CockroachDb"))); )
5
6
  a++;
6
- return r ?? { error: new Error("No transaction retry executed") };
7
- }, i = async (t, o, e) => {
7
+ return t ?? { error: new Error("No transaction retry executed") };
8
+ }, s = async (e, o, r) => {
8
9
  try {
9
10
  return {
10
11
  // @ts-ignore
11
- result: await t.$transaction(o, e)
12
+ result: await e.$transaction(o, r)
12
13
  };
13
- } catch (r) {
14
- return { error: r };
14
+ } catch (t) {
15
+ return { error: t };
15
16
  }
17
+ }, l = (e, o) => {
18
+ if (i(e.error)) {
19
+ const r = e.error;
20
+ if (r.code === c || o === "CockroachDb" && n(r))
21
+ return !0;
22
+ }
23
+ return !1;
16
24
  };
17
25
  export {
18
- l as prismaTransaction
26
+ R as prismaTransaction
19
27
  };
20
28
  //# sourceMappingURL=prismaTransaction.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"prismaTransaction.js","sources":["../src/prismaTransaction.ts"],"sourcesContent":["import type { Either } from '@lokalise/node-core'\nimport type { PrismaClient, Prisma } from '@prisma/client'\nimport type * as runtime from '@prisma/client/runtime/library'\n\nimport { isPrismaClientKnownRequestError, PRISMA_SERIALIZATION_ERROR } from './prismaError'\n\nexport type PrismaTransactionOptions = {\n\tretriesAllowed: number\n\tmaxWait?: number\n\ttimeout?: number\n\tisolationLevel?: string\n}\n\nexport type PrismaTransactionBasicOptions = Pick<\n\tPrismaTransactionOptions,\n\t'retriesAllowed' | 'isolationLevel'\n>\n\nexport type PrismaTransactionClient<P> = Omit<P, runtime.ITXClientDenyList>\n\nexport type PrismaTransactionFn<T, P> = (prisma: PrismaTransactionClient<P>) => Promise<T>\n\ntype PrismaTransactionReturnType<T> = Either<\n\tunknown,\n\tT | runtime.Types.Utils.UnwrapTuple<Prisma.PrismaPromise<unknown>[]>\n>\n\n/**\n * Perform a Prisma DB transaction with automatic retries if needed.\n *\n * @template T | T extends Prisma.PrismaPromise<unknown>[]\n * @param {PrismaClient} prisma\n * @param {PrismaTransactionFn<T> | Prisma.PrismaPromise<unknown>[]} arg\t\t operation to perform into the transaction\n * @param {PrismaTransactionOptions | PrismaTransactionBasicOptions} options transaction configuration\n * @return {Promise<PrismaTransactionReturnType<T>>}\n */\nexport const prismaTransaction = (async <T, P extends PrismaClient>(\n\tprisma: P,\n\targ: PrismaTransactionFn<T, P>,\n\toptions: PrismaTransactionOptions = { retriesAllowed: 3 },\n): Promise<PrismaTransactionReturnType<T>> => {\n\tlet result: PrismaTransactionReturnType<T> | undefined = undefined\n\n\tlet retries = 0\n\twhile (retries < options.retriesAllowed) {\n\t\tresult = await executeTransactionTry(prisma, arg, options)\n\n\t\tif (\n\t\t\tresult.result ||\n\t\t\t!isPrismaClientKnownRequestError(result.error) ||\n\t\t\tresult.error.code !== PRISMA_SERIALIZATION_ERROR\n\t\t) {\n\t\t\tbreak\n\t\t}\n\n\t\tretries++\n\t}\n\n\treturn result ?? { error: new Error('No transaction retry executed') }\n}) as {\n\t<T, P extends PrismaClient>(\n\t\tprisma: P,\n\t\tfn: PrismaTransactionFn<T, P>,\n\t\toptions?: PrismaTransactionOptions,\n\t): Promise<Either<unknown, T>>\n\t<T extends Prisma.PrismaPromise<unknown>[], P extends PrismaClient>(\n\t\tprisma: P,\n\t\targs: [...T],\n\t\toptions?: PrismaTransactionBasicOptions,\n\t): Promise<Either<unknown, runtime.Types.Utils.UnwrapTuple<T>>>\n}\n\nconst executeTransactionTry = async <T, P extends PrismaClient>(\n\tprisma: P,\n\targ: PrismaTransactionFn<T, P>,\n\toptions: PrismaTransactionOptions,\n): Promise<PrismaTransactionReturnType<T>> => {\n\ttry {\n\t\treturn {\n\t\t\t// @ts-ignore\n\t\t\tresult: await prisma.$transaction<T>(arg, options),\n\t\t}\n\t} catch (error) {\n\t\treturn { error }\n\t}\n}\n"],"names":["prismaTransaction","prisma","arg","options","result","retries","executeTransactionTry","isPrismaClientKnownRequestError","PRISMA_SERIALIZATION_ERROR","error"],"mappings":";AAoCa,MAAAA,IAAqB,OACjCC,GACAC,GACAC,IAAoC,EAAE,gBAAgB,QACT;AAC7C,MAAIC,GAEAC,IAAU;AACP,SAAAA,IAAUF,EAAQ,mBACxBC,IAAS,MAAME,EAAsBL,GAAQC,GAAKC,CAAO,GAGxD,EAAAC,EAAO,UACP,CAACG,EAAgCH,EAAO,KAAK,KAC7CA,EAAO,MAAM,SAASI;AAKvB,IAAAH;AAGD,SAAOD,KAAU,EAAE,OAAO,IAAI,MAAM,+BAA+B,EAAE;AACtE,GAaME,IAAwB,OAC7BL,GACAC,GACAC,MAC6C;AACzC,MAAA;AACI,WAAA;AAAA;AAAA,MAEN,QAAQ,MAAMF,EAAO,aAAgBC,GAAKC,CAAO;AAAA,IAAA;AAAA,WAE1CM,GAAO;AACf,WAAO,EAAE,OAAAA,EAAM;AAAA,EAChB;AACD;"}
1
+ {"version":3,"file":"prismaTransaction.js","sources":["../src/prismaTransaction.ts"],"sourcesContent":["import type { Either } from '@lokalise/node-core'\nimport type { PrismaClient, Prisma } from '@prisma/client'\nimport type * as runtime from '@prisma/client/runtime/library'\n\nimport { isCockroachDBRetryTransaction } from './cockroachdbError'\nimport { isPrismaClientKnownRequestError, PRISMA_SERIALIZATION_ERROR } from './prismaError'\nimport type {\n\tDbDriver,\n\tPrismaTransactionBasicOptions,\n\tPrismaTransactionFn,\n\tPrismaTransactionOptions,\n} from './types'\n\ntype PrismaTransactionReturnType<T> = Either<\n\tunknown,\n\tT | runtime.Types.Utils.UnwrapTuple<Prisma.PrismaPromise<unknown>[]>\n>\n\n/**\n * Perform a Prisma DB transaction with automatic retries if needed.\n *\n * @template T | T extends Prisma.PrismaPromise<unknown>[]\n * @param {PrismaClient} prisma\n * @param {PrismaTransactionFn<T> | Prisma.PrismaPromise<unknown>[]} arg\t operation to perform into the transaction\n * @param {PrismaTransactionOptions | PrismaTransactionBasicOptions} options transaction configuration\n * @return {Promise<PrismaTransactionReturnType<T>>}\n */\nexport const prismaTransaction = (async <T, P extends PrismaClient>(\n\tprisma: P,\n\targ: PrismaTransactionFn<T, P>,\n\toptions: PrismaTransactionOptions = { retriesAllowed: 3 },\n): Promise<PrismaTransactionReturnType<T>> => {\n\tlet result: PrismaTransactionReturnType<T> | undefined = undefined\n\n\tlet retries = 0\n\twhile (retries < options.retriesAllowed) {\n\t\tresult = await executeTransactionTry(prisma, arg, options)\n\n\t\tif (result.result || !isRetryAllowed(result, options.DbDriver ?? 'CockroachDb')) {\n\t\t\tbreak\n\t\t}\n\n\t\tretries++\n\t}\n\n\treturn result ?? { error: new Error('No transaction retry executed') }\n}) as {\n\t<T, P extends PrismaClient>(\n\t\tprisma: P,\n\t\tfn: PrismaTransactionFn<T, P>,\n\t\toptions?: PrismaTransactionOptions,\n\t): Promise<Either<unknown, T>>\n\t<T extends Prisma.PrismaPromise<unknown>[], P extends PrismaClient>(\n\t\tprisma: P,\n\t\targs: [...T],\n\t\toptions?: PrismaTransactionBasicOptions,\n\t): Promise<Either<unknown, runtime.Types.Utils.UnwrapTuple<T>>>\n}\n\nconst executeTransactionTry = async <T, P extends PrismaClient>(\n\tprisma: P,\n\targ: PrismaTransactionFn<T, P>,\n\toptions: PrismaTransactionOptions,\n): Promise<PrismaTransactionReturnType<T>> => {\n\ttry {\n\t\treturn {\n\t\t\t// @ts-ignore\n\t\t\tresult: await prisma.$transaction<T>(arg, options),\n\t\t}\n\t} catch (error) {\n\t\treturn { error }\n\t}\n}\n\nconst isRetryAllowed = <T>(result: PrismaTransactionReturnType<T>, dbDriver: DbDriver): boolean => {\n\tif (isPrismaClientKnownRequestError(result.error)) {\n\t\tconst error = result.error\n\t\tif (error.code === PRISMA_SERIALIZATION_ERROR) return true\n\t\tif (dbDriver === 'CockroachDb' && isCockroachDBRetryTransaction(error)) return true\n\t}\n\n\treturn false\n}\n"],"names":["prismaTransaction","prisma","arg","options","result","retries","executeTransactionTry","isRetryAllowed","error","dbDriver","isPrismaClientKnownRequestError","PRISMA_SERIALIZATION_ERROR","isCockroachDBRetryTransaction"],"mappings":";;AA2Ba,MAAAA,IAAqB,OACjCC,GACAC,GACAC,IAAoC,EAAE,gBAAgB,QACT;AAC7C,MAAIC,GAEAC,IAAU;AACP,SAAAA,IAAUF,EAAQ,mBACxBC,IAAS,MAAME,EAAsBL,GAAQC,GAAKC,CAAO,GAErD,EAAAC,EAAO,UAAU,CAACG,EAAeH,GAAQD,EAAQ,YAAY,aAAa;AAI9E,IAAAE;AAGD,SAAOD,KAAU,EAAE,OAAO,IAAI,MAAM,+BAA+B,EAAE;AACtE,GAaME,IAAwB,OAC7BL,GACAC,GACAC,MAC6C;AACzC,MAAA;AACI,WAAA;AAAA;AAAA,MAEN,QAAQ,MAAMF,EAAO,aAAgBC,GAAKC,CAAO;AAAA,IAAA;AAAA,WAE1CK,GAAO;AACf,WAAO,EAAE,OAAAA,EAAM;AAAA,EAChB;AACD,GAEMD,IAAiB,CAAIH,GAAwCK,MAAgC;AAC9F,MAAAC,EAAgCN,EAAO,KAAK,GAAG;AAClD,UAAMI,IAAQJ,EAAO;AAEjB,QADAI,EAAM,SAASG,KACfF,MAAa,iBAAiBG,EAA8BJ,CAAK;AAAU,aAAA;AAAA,EAChF;AAEO,SAAA;AACR;"}
@@ -0,0 +1,17 @@
1
+ import type * as runtime from '@prisma/client/runtime/library';
2
+ type ObjectValues<T> = T[keyof T];
3
+ export declare const DbDriverEnum: {
4
+ readonly COCKROACHDB: "CockroachDb";
5
+ };
6
+ export type DbDriver = ObjectValues<typeof DbDriverEnum>;
7
+ export type PrismaTransactionOptions = {
8
+ retriesAllowed: number;
9
+ maxWait?: number;
10
+ timeout?: number;
11
+ isolationLevel?: string;
12
+ DbDriver?: DbDriver;
13
+ };
14
+ export type PrismaTransactionBasicOptions = Pick<PrismaTransactionOptions, 'retriesAllowed' | 'isolationLevel' | 'DbDriver'>;
15
+ export type PrismaTransactionClient<P> = Omit<P, runtime.ITXClientDenyList>;
16
+ export type PrismaTransactionFn<T, P> = (prisma: PrismaTransactionClient<P>) => Promise<T>;
17
+ export {};
package/dist/types.js ADDED
@@ -0,0 +1,7 @@
1
+ const o = {
2
+ COCKROACHDB: "CockroachDb"
3
+ };
4
+ export {
5
+ o as DbDriverEnum
6
+ };
7
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sources":["../src/types.ts"],"sourcesContent":["import type * as runtime from '@prisma/client/runtime/library'\n\ntype ObjectValues<T> = T[keyof T]\n\nexport const DbDriverEnum = {\n\tCOCKROACHDB: 'CockroachDb',\n} as const\nexport type DbDriver = ObjectValues<typeof DbDriverEnum>\n\nexport type PrismaTransactionOptions = {\n\tretriesAllowed: number\n\tmaxWait?: number\n\ttimeout?: number\n\tisolationLevel?: string\n\tDbDriver?: DbDriver\n}\n\nexport type PrismaTransactionBasicOptions = Pick<\n\tPrismaTransactionOptions,\n\t'retriesAllowed' | 'isolationLevel' | 'DbDriver'\n>\n\nexport type PrismaTransactionClient<P> = Omit<P, runtime.ITXClientDenyList>\n\nexport type PrismaTransactionFn<T, P> = (prisma: PrismaTransactionClient<P>) => Promise<T>\n"],"names":["DbDriverEnum"],"mappings":"AAIO,MAAMA,IAAe;AAAA,EAC3B,aAAa;AACd;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lokalise/prisma-utils",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "type": "module",
5
5
  "license": "Apache-2.0",
6
6
  "files": [
@@ -60,8 +60,8 @@
60
60
  "prettier": "^3.2.5",
61
61
  "rimraf": "^5.0.1",
62
62
  "typescript": "5.4.5",
63
- "vite": "^5.2.10",
64
- "vitest": "^1.5.3"
63
+ "vite": "^5.2.11",
64
+ "vitest": "^1.6.0"
65
65
  },
66
66
  "prettier": "@lokalise/prettier-config"
67
67
  }