declapract-typescript-ehmpathy 0.31.7 → 0.31.9
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.
- package/dist/practices/package-json-order/best-practice/package.json.declapract.ts +2 -1
- package/dist/practices/persist-with-rds/bad-practices/with-connection-in-input/.declapract.readme.md +1 -0
- package/dist/practices/persist-with-rds/bad-practices/with-connection-in-input/src/utils/database/withDatabaseConnection.ts +3 -0
- package/dist/practices/persist-with-rds/best-practice/package.json +3 -3
- package/dist/practices/persist-with-rds/best-practice/src/utils/database/withDatabaseTransaction.ts +8 -7
- package/package.json +1 -1
- package/dist/practices/persist-with-rds/best-practice/src/utils/database/withDatabaseConnection.ts +0 -46
package/dist/practices/persist-with-rds/bad-practices/with-connection-in-input/.declapract.readme.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
withDatabaseConnection -> withDatabaseContext, per procedure-fns pattern
|
|
@@ -5,14 +5,14 @@
|
|
|
5
5
|
},
|
|
6
6
|
"devDependencies": {
|
|
7
7
|
"sql-code-generator": "@declapract{check.minVersion('0.9.3')}",
|
|
8
|
-
"sql-dao-generator": "@declapract{check.minVersion('0.
|
|
8
|
+
"sql-dao-generator": "@declapract{check.minVersion('0.4.0')}",
|
|
9
9
|
"sql-schema-control": "@declapract{check.minVersion('1.5.0')}",
|
|
10
|
-
"sql-schema-generator": "@declapract{check.minVersion('0.
|
|
10
|
+
"sql-schema-generator": "@declapract{check.minVersion('0.23.0')}",
|
|
11
11
|
"@types/yesql": "@declapract{check.minVersion('3.2.2')}",
|
|
12
12
|
"@types/pg": "@declapract{check.minVersion('8.6.1')}"
|
|
13
13
|
},
|
|
14
14
|
"scripts": {
|
|
15
|
-
"generate:dao": "npx sql-dao-generator generate && npm run fix:format",
|
|
15
|
+
"generate:dao:postgres": "npx sql-dao-generator generate && npm run fix:format",
|
|
16
16
|
"generate:schema": "npx sql-schema-generator generate -c codegen.sql.schema.yml && npm run fix:format",
|
|
17
17
|
"generate:types-from-sql": "npx sql-code-generator generate -c codegen.sql.types.yml && npm run fix:format",
|
|
18
18
|
"provision:docker:clear": "docker rm -f $(docker ps -a -f 'publish=7821' -q) 2>/dev/null || true && echo 'ensured port is available 👍'",
|
package/dist/practices/persist-with-rds/best-practice/src/utils/database/withDatabaseTransaction.ts
CHANGED
|
@@ -1,19 +1,20 @@
|
|
|
1
1
|
import { DatabaseConnection } from './getDatabaseConnection';
|
|
2
2
|
|
|
3
3
|
export const withDatabaseTransaction = <
|
|
4
|
-
P
|
|
4
|
+
P,
|
|
5
|
+
C extends { dbConnection: DatabaseConnection },
|
|
5
6
|
R,
|
|
6
7
|
>(
|
|
7
|
-
logic: (
|
|
8
|
+
logic: (input: P, context: C) => R | Promise<R>,
|
|
8
9
|
): typeof logic => {
|
|
9
|
-
return (async (
|
|
10
|
-
await
|
|
10
|
+
return (async (input: P, context: C) => {
|
|
11
|
+
await context.dbConnection.query({ sql: 'START TRANSACTION' }); // begin transaction
|
|
11
12
|
try {
|
|
12
|
-
const result = await logic(
|
|
13
|
-
await
|
|
13
|
+
const result = await logic(input, context); // run the request
|
|
14
|
+
await context.dbConnection.query({ sql: 'COMMIT' }); // commit if successful
|
|
14
15
|
return result;
|
|
15
16
|
} catch (error) {
|
|
16
|
-
await
|
|
17
|
+
await context.dbConnection.query({ sql: 'ROLLBACK' }); // rollback if not successful
|
|
17
18
|
throw error;
|
|
18
19
|
}
|
|
19
20
|
}) as typeof logic;
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "declapract-typescript-ehmpathy",
|
|
3
3
|
"author": "ehmpathy",
|
|
4
4
|
"description": "declapract best practices declarations for typescript",
|
|
5
|
-
"version": "0.31.
|
|
5
|
+
"version": "0.31.9",
|
|
6
6
|
"main": "src/index.js",
|
|
7
7
|
"repository": "ehmpathy/declapract-typescript-ehmpathy",
|
|
8
8
|
"homepage": "https://github.com/ehmpathy/declapract-typescript-ehmpathy",
|
package/dist/practices/persist-with-rds/best-practice/src/utils/database/withDatabaseConnection.ts
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import { DropFirst } from 'type-fns';
|
|
2
|
-
|
|
3
|
-
import {
|
|
4
|
-
DatabaseConnection,
|
|
5
|
-
getDatabaseConnection,
|
|
6
|
-
} from './getDatabaseConnection';
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* wraps the function to provide a managed database connection to it, if one was not already passed in
|
|
10
|
-
*
|
|
11
|
-
* note
|
|
12
|
-
* - manages the database connection it gives to the function by opening it and closing it once the function finishes
|
|
13
|
-
* - if a database connection was passed in as part of the arguments, it will instead use that one and will not close it once it finishes (useful for transactions)
|
|
14
|
-
*
|
|
15
|
-
* example usage:
|
|
16
|
-
* ```
|
|
17
|
-
* const findById = withDatabaseConnection(({ id, dbConnection }: { id: string; dbConnection: DatabaseConnection }) => {
|
|
18
|
-
* // do logic with dbConnection
|
|
19
|
-
* });
|
|
20
|
-
* // ...
|
|
21
|
-
* await findById({ id: 821 }); // note how we don't have to pass in the dbConnection
|
|
22
|
-
* ```
|
|
23
|
-
*/
|
|
24
|
-
export const withDatabaseConnection = <
|
|
25
|
-
P extends { dbConnection: DatabaseConnection },
|
|
26
|
-
R,
|
|
27
|
-
PR, // the rest of the parameters
|
|
28
|
-
>(
|
|
29
|
-
logic: (args0: P, ...argsRest: PR[]) => R | Promise<R>,
|
|
30
|
-
) => {
|
|
31
|
-
return async (
|
|
32
|
-
args0: Omit<P, 'dbConnection'> & { dbConnection?: DatabaseConnection },
|
|
33
|
-
...argsRest: DropFirst<Parameters<typeof logic>>
|
|
34
|
-
) => {
|
|
35
|
-
// open the db connection, if one was not given
|
|
36
|
-
const dbConnection = args0.dbConnection ?? (await getDatabaseConnection());
|
|
37
|
-
|
|
38
|
-
// try and run the logic with db connection
|
|
39
|
-
try {
|
|
40
|
-
return await logic({ ...args0, dbConnection } as P, ...argsRest); // as P because: https://github.com/microsoft/TypeScript/issues/35858
|
|
41
|
-
} finally {
|
|
42
|
-
// make sure to close the db connection, both when `logic` throws an error or succeeds
|
|
43
|
-
if (!args0.dbConnection) await dbConnection.end();
|
|
44
|
-
}
|
|
45
|
-
};
|
|
46
|
-
};
|