-
Notifications
You must be signed in to change notification settings - Fork 960
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3630 from activepieces/chore/make-update-flow-pub…
…lished-version-a-transaction chore: use db transactions for "update published flow version" request
- Loading branch information
Showing
14 changed files
with
189 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { Repository, EntityManager, ObjectLiteral, EntitySchema } from 'typeorm' | ||
import { databaseConnection } from '../../database/database-connection' | ||
|
||
/** | ||
* If given an {@link EntityManager}, returns a {@link Repository} for the current transaction. | ||
* Otherwise, returns the {@link Repository} for the default connection. | ||
*/ | ||
type RepoGetter<T extends ObjectLiteral = ObjectLiteral> = (entityManager?: EntityManager) => Repository<T> | ||
|
||
const instances = new Map<EntitySchema, RepoGetter>() | ||
|
||
/** | ||
* Creates a {@link RepoGetter} for the given entity. | ||
* @param entity The entity to create a {@link RepoGetter} for. | ||
* @returns A {@link RepoGetter} for the given entity. | ||
*/ | ||
export const repoFactory = <T extends ObjectLiteral>(entity: EntitySchema<T>): RepoGetter<T> => { | ||
if (instances.has(entity)) { | ||
return instances.get(entity) as RepoGetter<T> | ||
} | ||
|
||
const newInstance: RepoGetter<T> = (entityManager?: EntityManager) => { | ||
return entityManager?.getRepository(entity) | ||
?? databaseConnection.getRepository(entity) | ||
} | ||
|
||
instances.set(entity, newInstance as RepoGetter) | ||
return newInstance | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { EntityManager } from 'typeorm' | ||
import { databaseConnection } from '../../database/database-connection' | ||
|
||
export const transaction = async <T>(operation: (entityManager: EntityManager) => Promise<T>): Promise<T> => { | ||
return databaseConnection.transaction(operation) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import { databaseConnection } from '../../database/database-connection' | ||
import { repoFactory } from '../../core/db/repo-factory' | ||
import { FlowEntity } from './flow.entity' | ||
|
||
export const flowRepo = databaseConnection.getRepository(FlowEntity) | ||
export const flowRepo = repoFactory(FlowEntity) |
Oops, something went wrong.