diff --git a/database/entity/0027-decimal_types/Transaction.ts b/database/entity/0027-decimal_types/Transaction.ts new file mode 100644 index 000000000..af1f68556 --- /dev/null +++ b/database/entity/0027-decimal_types/Transaction.ts @@ -0,0 +1,146 @@ +import Decimal from 'decimal.js-light' +import { BaseEntity, Entity, PrimaryGeneratedColumn, Column } from 'typeorm' +import { DecimalTransformer } from '../../src/typeorm/DecimalTransformer' + +@Entity('transactions') +export class Transaction extends BaseEntity { + @PrimaryGeneratedColumn('increment', { unsigned: true }) + id: number + + @Column({ name: 'user_id', unsigned: true, nullable: false }) + userId: number + + @Column({ unsigned: true, nullable: true, default: null }) + previous: number + + @Column({ name: 'type_id', unsigned: true, nullable: false }) + typeId: number + + @Column({ + name: 'dec_amount', + type: 'decimal', + precision: 40, + scale: 20, + nullable: false, + transformer: DecimalTransformer, + }) + decAmount: Decimal + + @Column({ + name: 'dec_balance', + type: 'decimal', + precision: 40, + scale: 20, + nullable: false, + transformer: DecimalTransformer, + }) + decBalance: Decimal + + @Column({ + name: 'balance_date', + type: 'datetime', + default: () => 'CURRENT_TIMESTAMP', + nullable: false, + }) + balanceDate: Date + + @Column({ + name: 'dec_decay', + type: 'decimal', + precision: 40, + scale: 20, + nullable: false, + transformer: DecimalTransformer, + }) + decDecay: Decimal + + @Column({ + name: 'decay_start', + type: 'datetime', + nullable: true, + default: null, + }) + decayStart: Date | null + + @Column({ type: 'bigint', nullable: false }) + amount: BigInt + + @Column({ length: 255, nullable: false, collation: 'utf8mb4_unicode_ci' }) + memo: string + + @Column({ + name: 'send_sender_final_balance', + type: 'bigint', + nullable: true, + default: null, + }) + sendSenderFinalBalance: BigInt | null + + @Column({ name: 'balance', type: 'bigint', default: 0 }) + balance: BigInt + + @Column({ name: 'creation_date', type: 'timestamp', nullable: true, default: null }) + creationDate: Date + + @Column({ + name: 'linked_user_id', + type: 'int', + unsigned: true, + nullable: true, + default: null, + }) + linkedUserId?: number | null + + @Column({ + name: 'linked_transaction_id', + type: 'int', + unsigned: true, + nullable: true, + default: null, + }) + linkedTransactionId?: number | null + + @Column({ + name: 'temp_dec_send_sender_final_balance', + type: 'decimal', + precision: 40, + scale: 20, + nullable: true, + default: null, + transformer: DecimalTransformer, + }) + tempDecSendSenderFinalBalance: Decimal + + @Column({ + name: 'temp_dec_diff_send_sender_final_balance', + type: 'decimal', + precision: 40, + scale: 20, + nullable: true, + default: null, + transformer: DecimalTransformer, + }) + tempDecDiffSendSenderFinalBalance: Decimal + + @Column({ + name: 'temp_dec_old_balance', + type: 'decimal', + precision: 40, + scale: 20, + nullable: true, + default: null, + transformer: DecimalTransformer, + }) + tempDecOldBalance: Decimal + + @Column({ + name: 'temp_dec_diff_balance', + type: 'decimal', + precision: 40, + scale: 20, + nullable: true, + default: null, + transformer: DecimalTransformer, + }) + tempDecDiffBalance: Decimal +} diff --git a/database/entity/Transaction.ts b/database/entity/Transaction.ts index 8d46ef6da..d652989e5 100644 --- a/database/entity/Transaction.ts +++ b/database/entity/Transaction.ts @@ -1 +1 @@ -export { Transaction } from './0026-clean_transaction_table/Transaction' +export { Transaction } from './0027-decimal_types/Transaction' diff --git a/database/src/typeorm/DecimalTransformer.ts b/database/src/typeorm/DecimalTransformer.ts new file mode 100644 index 000000000..88a3539ad --- /dev/null +++ b/database/src/typeorm/DecimalTransformer.ts @@ -0,0 +1,14 @@ +import Decimal from 'decimal.js-light' +import { ValueTransformer } from 'typeorm' + +export const DecimalTransformer: ValueTransformer = { + /** + * Used to marshal Decimal when writing to the database. + */ + to: (decimal: Decimal | null): string | null => (decimal ? decimal.toString() : null), + + /** + * Used to unmarshal Decimal when reading from the database. + */ + from: (decimal: string | null): Decimal | null => (decimal ? new Decimal(decimal) : null), +}