Skip to content

Instantly share code, notes, and snippets.

View alphamikle's full-sized avatar

Mike Alfa alphamikle

View GitHub Profile
@alphamikle
alphamikle / isolate.dart
Last active October 8, 2021 12:29
Isolated dart code
void main(List<String> arguments, dynamic message) {
print('GITHUB: Arguments from your isolate: $arguments');
print('GITHUB: Message from your isolate: $message');
// V3
}
START TRANSACTION
// ...
SELECT "Purse"."id" AS "Purse_id", "Purse"."balance" AS "Purse_balance", "Purse"."userId" AS "Purse_userId"
FROM "purse" "Purse"
WHERE "Purse"."id" IN ($1)
UPDATE "purse"
SET "balance" = $2
WHERE "id" IN ($1)
SELECT "Purse"."id" AS "Purse_id", "Purse"."balance" AS "Purse_balance", "Purse"."userId" AS "Purse_userId"
FROM "purse" "Purse"
// ...
@Post('remittance-with-transaction-and-fee')
@ApiResponse({
type: RemittanceResultDto,
})
async makeRemittanceWithTransactionAndFee(@Body() remittanceDto: RemittanceDto) {
return this.connection.transaction(async manager => {
const transactionAppService = this.appService.withTransaction(manager); // <-- this is interesting new thing
const result = await transactionAppService.makeRemittance(remittanceDto.userIdFrom, remittanceDto.userIdTo, remittanceDto.sum, remittanceDto.withError);
result.fromBalance -= 1; // <-- transfer fee
START TRANSACTION
// ...
SELECT "Purse"."id" AS "Purse_id", "Purse"."balance" AS "Purse_balance", "Purse"."userId" AS "Purse_userId"
FROM "purse" "Purse"
WHERE "Purse"."id" IN ($1)
UPDATE "purse"
SET "balance" = $2
WHERE "id" IN ($1)
ROLLBACK
START TRANSACTION
// ...
SELECT "Purse"."id" AS "Purse_id", "Purse"."balance" AS "Purse_balance", "Purse"."userId" AS "Purse_userId"
FROM "purse" "Purse"
WHERE "Purse"."id" IN ($1)
UPDATE "purse"
SET "balance" = $2
WHERE "id" IN ($1)
SELECT "Purse"."id" AS "Purse_id", "Purse"."balance" AS "Purse_balance", "Purse"."userId" AS "Purse_userId"
FROM "purse" "Purse"
@Injectable()
export class AppService extends TransactionFor<AppService> /* <-- step 1 */ {
constructor(
@InjectRepository(User)
private readonly userRepository: Repository<User>,
@InjectRepository(Purse)
private readonly purseRepository: Repository<Purse>,
private readonly appServiceV2: AppServiceV2,
moduleRef: ModuleRef, // <-- step 2
) {
@Post('remittance-with-typeorm-transaction')
@ApiResponse({
type: RemittanceResultDto,
})
async makeRemittanceWithTypeOrmTransaction(@Body() remittanceDto: RemittanceDto) {
return await this.connection.transaction(transactionManager => {
return this.appService.makeRemittanceWithTypeOrmV1(transactionManager, remittanceDto.userIdFrom, remittanceDto.userIdTo, remittanceDto.sum, remittanceDto.withError);
});
}
// ...
@Post('remittance-with-transaction')
@ApiResponse({
type: RemittanceResultDto,
})
async makeRemittanceWithTransaction(@Body() remittanceDto: RemittanceDto) {
return await this.connection.transaction(transactionManager => {
return this.appService.withTransaction(transactionManager)/* <-- this is interesting new thing*/.makeRemittance(remittanceDto.userIdFrom, remittanceDto.userIdTo, remittanceDto.sum, remittanceDto.withError);
});
}
@Controller()
@ApiTags('app')
export class AppController {
constructor(
private readonly appService: AppService,
private readonly connection: Connection, // <-- use this
) {
}
// ...
}
// app.service.ts
@Transaction()
async makeRemittanceWithTypeOrmV2(fromId: number, toId: number, sum: number, withError: boolean, @TransactionManager() transactionEntityManager: EntityManager = null) {
// ...
await this.appServiceV2.savePurseInTransactionV2(fromPurse, transactionEntityManager);
// ...
}
// app.service-v2.ts
// ..