Skip to content

Instantly share code, notes, and snippets.

@alphamikle
alphamikle / isolate.dart
Last active Mar 29, 2021
Isolated dart code
View isolate.dart
void main(List<String> arguments, dynamic message) {
print('GITHUB: Arguments from your isolate: $arguments');
print('GITHUB: Message from your isolate: $message');
}
View tewtutin_27.sql
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"
View tewtutin_26.ts
// ...
@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
View tewtutin_25.sql
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
View tewtutin_24.sql
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"
View tewtutin_23.ts
@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
) {
View tewtutin_22.ts
@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);
});
}
View tewtutin_21.ts
// ...
@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);
});
}
View tewtutin_20.ts
@Controller()
@ApiTags('app')
export class AppController {
constructor(
private readonly appService: AppService,
private readonly connection: Connection, // <-- use this
) {
}
// ...
}
View tewtutin_19.ts
// 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
// ..