diff --git a/dlt-connector/schema.gql b/dlt-connector/schema.gql new file mode 100644 index 000000000..30a58b0f1 --- /dev/null +++ b/dlt-connector/schema.gql @@ -0,0 +1,32 @@ +# ----------------------------------------------- +# !!! THIS FILE WAS GENERATED BY TYPE-GRAPHQL !!! +# !!! DO NOT MODIFY THIS FILE BY YOURSELF !!! +# ----------------------------------------------- + +"""The `Decimal` scalar type to represent currency values""" +scalar Decimal + +type Mutation { + transmitTransaction(data: TransactionInput!): TransmitTransactionResult! +} + +type Query { + version: String! +} + +input TransactionInput { + amount: Decimal! + createdAt: Int! + type: TransactionType! +} + +"""Type of the transaction""" +enum TransactionType { + CREATION + RECEIVE + SEND +} + +type TransmitTransactionResult { + dltTransactionIdHex: String! +} \ No newline at end of file diff --git a/dlt-connector/src/controller/Community.test.ts b/dlt-connector/src/controller/Community.test.ts index ce0f62616..2afb9dc22 100644 --- a/dlt-connector/src/controller/Community.test.ts +++ b/dlt-connector/src/controller/Community.test.ts @@ -8,6 +8,7 @@ import { } from './Community' import { TestDB } from '@test/TestDB' import { getDataSource } from '@/typeorm/DataSource' +import { Community } from '@entity/Community' jest.mock('@typeorm/DataSource', () => ({ getDataSource: () => TestDB.instance.dbConnect, @@ -55,8 +56,15 @@ describe('controller/Community', () => { const communityDraft = new CommunityDraft() communityDraft.foreign = false communityDraft.createdAt = '2022-05-01T17:00:12.128Z' - communityDraft.uuid = '3d813cab-47fb-32ba-91df-831e1593ac29' - expect(await isExist(communityDraft)).toBe(false) + communityDraft.uuid = '3d813cbb-47fb-32ba-91df-831e1593ac29' + expect(await isExist(communityDraft)).toBe(true) + }) + + it('createdAt with ms precision', async () => { + const list = await getDataSource().manager.findOne(Community, { where: { foreign: false } }) + expect(list).toMatchObject({ + createdAt: new Date('2022-05-01T17:00:12.128Z'), + }) }) }) }) diff --git a/dlt-connector/src/graphql/resolver/CommunityResolver.test.ts b/dlt-connector/src/graphql/resolver/CommunityResolver.test.ts index f815ca0f5..adb754606 100644 --- a/dlt-connector/src/graphql/resolver/CommunityResolver.test.ts +++ b/dlt-connector/src/graphql/resolver/CommunityResolver.test.ts @@ -46,7 +46,7 @@ describe('graphql/resolver/CommunityResolver', () => { input: { uuid: '3d813cbb-37fb-42ba-91df-831e1593ac29', foreign: true, - createdAt: '2012-04-17T17:12:00Z', + createdAt: '2012-04-17T17:12:00.0012Z', }, }, }) @@ -63,7 +63,7 @@ describe('graphql/resolver/CommunityResolver', () => { input: { uuid: '3d823cad-37fb-41cd-91df-152e1593ac29', foreign: false, - createdAt: '2012-05-12T13:12:00Z', + createdAt: '2012-05-12T13:12:00.2917Z', }, }, }) @@ -80,7 +80,7 @@ describe('graphql/resolver/CommunityResolver', () => { input: { uuid: '3d823cad-37fb-41cd-91df-152e1593ac29', foreign: false, - createdAt: '2012-05-12T13:12:00Z', + createdAt: '2012-05-12T13:12:00.1271Z', }, }, }) diff --git a/dlt-database/entity/0001-init_db/Account.ts b/dlt-database/entity/0001-init_db/Account.ts index ce80367bd..38afba928 100644 --- a/dlt-database/entity/0001-init_db/Account.ts +++ b/dlt-database/entity/0001-init_db/Account.ts @@ -28,10 +28,15 @@ export class Account { @Column({ type: 'tinyint', unsigned: true }) type: number - @Column({ name: 'created_at', type: 'datetime', default: () => 'CURRENT_TIMESTAMP(3)' }) + @Column({ + name: 'created_at', + type: 'datetime', + precision: 3, + default: () => 'CURRENT_TIMESTAMP(3)', + }) createdAt: Date - @Column({ name: 'confirmed_at', type: 'datetime', nullable: true }) + @Column({ name: 'confirmed_at', type: 'datetime', precision: 3, nullable: true }) confirmedAt?: Date @Column({ @@ -46,7 +51,8 @@ export class Account { @Column({ name: 'balance_date', type: 'datetime', - default: () => 'CURRENT_TIMESTAMP(3)', + precision: 3, + default: () => 'CURRENT_TIMESTAMP()', }) balanceDate: Date diff --git a/dlt-database/entity/0001-init_db/AccountCommunity.ts b/dlt-database/entity/0001-init_db/AccountCommunity.ts index 2b9ad38f9..8aa3cc455 100644 --- a/dlt-database/entity/0001-init_db/AccountCommunity.ts +++ b/dlt-database/entity/0001-init_db/AccountCommunity.ts @@ -22,9 +22,9 @@ export class AccountCommunity { @Column({ name: 'community_id', type: 'int', unsigned: true }) communityId: number - @Column({ name: 'valid_from', type: 'datetime' }) + @Column({ name: 'valid_from', type: 'datetime', precision: 3 }) validFrom: Date - @Column({ name: 'valid_to', type: 'datetime', nullable: true }) + @Column({ name: 'valid_to', type: 'datetime', precision: 3, nullable: true }) validTo?: Date } diff --git a/dlt-database/entity/0001-init_db/Community.ts b/dlt-database/entity/0001-init_db/Community.ts index e44aa2f8f..e6f88f6cd 100644 --- a/dlt-database/entity/0001-init_db/Community.ts +++ b/dlt-database/entity/0001-init_db/Community.ts @@ -37,10 +37,15 @@ export class Community { @JoinColumn({ name: 'auf_account_id' }) aufAccount?: Account - @Column({ name: 'created_at', type: 'datetime', default: () => 'CURRENT_TIMESTAMP(3)' }) + @Column({ + name: 'created_at', + type: 'datetime', + precision: 3, + default: () => 'CURRENT_TIMESTAMP(3)', + }) createdAt: Date - @Column({ name: 'confirmed_at', type: 'datetime', nullable: true }) + @Column({ name: 'confirmed_at', type: 'datetime', precision: 3, nullable: true }) confirmedAt?: Date @OneToMany(() => AccountCommunity, (accountCommunity) => accountCommunity.community) diff --git a/dlt-database/entity/0001-init_db/ConfirmedTransaction.ts b/dlt-database/entity/0001-init_db/ConfirmedTransaction.ts index 5b1119229..ba9b8752d 100644 --- a/dlt-database/entity/0001-init_db/ConfirmedTransaction.ts +++ b/dlt-database/entity/0001-init_db/ConfirmedTransaction.ts @@ -44,6 +44,6 @@ export class ConfirmedTransaction { @Column({ name: 'iota_milestone', type: 'bigint' }) iotaMilestone: number - @Column({ name: 'confirmed_at', type: 'datetime' }) + @Column({ name: 'confirmed_at', type: 'datetime', precision: 3 }) confirmedAt: Date } diff --git a/dlt-database/entity/0001-init_db/TransactionRecipe.ts b/dlt-database/entity/0001-init_db/TransactionRecipe.ts index c4a89e5dd..da62ad0ad 100644 --- a/dlt-database/entity/0001-init_db/TransactionRecipe.ts +++ b/dlt-database/entity/0001-init_db/TransactionRecipe.ts @@ -55,7 +55,7 @@ export class TransactionRecipe { @Column({ type: 'tinyint' }) type: number - @Column({ name: 'created_at', type: 'datetime' }) + @Column({ name: 'created_at', type: 'datetime', precision: 3 }) createdAt: Date @Column({ name: 'body_bytes', type: 'blob' }) diff --git a/dlt-database/entity/0001-init_db/User.ts b/dlt-database/entity/0001-init_db/User.ts index a4452c68d..681a668e2 100644 --- a/dlt-database/entity/0001-init_db/User.ts +++ b/dlt-database/entity/0001-init_db/User.ts @@ -21,6 +21,7 @@ export class User extends BaseEntity { @Column({ name: 'created_at', type: 'datetime', + precision: 3, default: () => 'CURRENT_TIMESTAMP(3)', }) createdAt: Date @@ -28,6 +29,7 @@ export class User extends BaseEntity { @Column({ name: 'confirmed_at', type: 'datetime', + precision: 3, nullable: true, }) confirmedAt?: Date diff --git a/dlt-database/entity/0002-refactor_add_community/Account.ts b/dlt-database/entity/0002-refactor_add_community/Account.ts new file mode 100644 index 000000000..1e7373b6c --- /dev/null +++ b/dlt-database/entity/0002-refactor_add_community/Account.ts @@ -0,0 +1,70 @@ +import { Entity, PrimaryGeneratedColumn, Column, ManyToOne, JoinColumn, OneToMany } from 'typeorm' +import { User } from '../User' +import { TransactionRecipe } from '../TransactionRecipe' +import { ConfirmedTransaction } from '../ConfirmedTransaction' +import { DecimalTransformer } from '../../src/typeorm/DecimalTransformer' +import { Decimal } from 'decimal.js-light' +import { AccountCommunity } from '../AccountCommunity' + +@Entity('accounts') +export class Account { + @PrimaryGeneratedColumn('increment', { unsigned: true }) + id: number + + @ManyToOne(() => User, (user) => user.accounts) // Assuming you have a User entity with 'accounts' relation + @JoinColumn({ name: 'user_id' }) + user?: User + + // if user id is null, account belongs to community gmw or auf + @Column({ name: 'user_id', type: 'int', unsigned: true, nullable: true }) + userId?: number + + @Column({ name: 'derivation_index', type: 'int', unsigned: true }) + derivationIndex: number + + @Column({ name: 'derive2_pubkey', type: 'binary', length: 32, unique: true }) + derive2Pubkey: Buffer + + @Column({ type: 'tinyint', unsigned: true }) + type: number + + @Column({ + name: 'created_at', + type: 'datetime', + precision: 3, + default: () => 'CURRENT_TIMESTAMP(3)', + }) + createdAt: Date + + @Column({ name: 'confirmed_at', type: 'datetime', nullable: true }) + confirmedAt?: Date + + @Column({ + type: 'decimal', + precision: 40, + scale: 20, + default: 0, + transformer: DecimalTransformer, + }) + balance: Decimal + + @Column({ + name: 'balance_date', + type: 'datetime', + default: () => 'CURRENT_TIMESTAMP()', + }) + balanceDate: Date + + @OneToMany(() => AccountCommunity, (accountCommunity) => accountCommunity.account) + @JoinColumn({ name: 'account_id' }) + accountCommunities: AccountCommunity[] + + @OneToMany(() => TransactionRecipe, (recipe) => recipe.signingAccount) + transactionRecipesSigning?: TransactionRecipe[] + + @OneToMany(() => TransactionRecipe, (recipe) => recipe.recipientAccount) + transactionRecipesRecipient?: TransactionRecipe[] + + @OneToMany(() => ConfirmedTransaction, (transaction) => transaction.account) + confirmedTransactions?: ConfirmedTransaction[] +} diff --git a/dlt-database/entity/0002-refactor_add_community/AccountCommunity.ts b/dlt-database/entity/0002-refactor_add_community/AccountCommunity.ts new file mode 100644 index 000000000..2b9ad38f9 --- /dev/null +++ b/dlt-database/entity/0002-refactor_add_community/AccountCommunity.ts @@ -0,0 +1,30 @@ +import { Entity, PrimaryGeneratedColumn, Column, ManyToOne, JoinColumn } from 'typeorm' + +import { Account } from '../Account' +import { Community } from '../Community' + +@Entity('accounts_communities') +export class AccountCommunity { + @PrimaryGeneratedColumn('increment', { unsigned: true }) + id: number + + @ManyToOne(() => Account, (account) => account.accountCommunities) + @JoinColumn({ name: 'account_id' }) + account: Account + + @Column({ name: 'account_id', type: 'int', unsigned: true }) + accountId: number + + @ManyToOne(() => Community, (community) => community.accountCommunities) + @JoinColumn({ name: 'community_id' }) + community: Community + + @Column({ name: 'community_id', type: 'int', unsigned: true }) + communityId: number + + @Column({ name: 'valid_from', type: 'datetime' }) + validFrom: Date + + @Column({ name: 'valid_to', type: 'datetime', nullable: true }) + validTo?: Date +} diff --git a/dlt-database/entity/0002-refactor_add_community/Community.ts b/dlt-database/entity/0002-refactor_add_community/Community.ts index 7547a115f..6edd71a4e 100644 --- a/dlt-database/entity/0002-refactor_add_community/Community.ts +++ b/dlt-database/entity/0002-refactor_add_community/Community.ts @@ -37,7 +37,12 @@ export class Community { @JoinColumn({ name: 'auf_account_id' }) aufAccount?: Account - @Column({ name: 'created_at', type: 'datetime', default: () => 'CURRENT_TIMESTAMP(3)' }) + @Column({ + name: 'created_at', + type: 'datetime', + precision: 3, + default: () => 'CURRENT_TIMESTAMP(3)', + }) createdAt: Date @Column({ name: 'confirmed_at', type: 'datetime', nullable: true }) diff --git a/dlt-database/entity/0002-refactor_add_community/ConfirmedTransaction.ts b/dlt-database/entity/0002-refactor_add_community/ConfirmedTransaction.ts new file mode 100644 index 000000000..5b1119229 --- /dev/null +++ b/dlt-database/entity/0002-refactor_add_community/ConfirmedTransaction.ts @@ -0,0 +1,49 @@ +import { Entity, PrimaryGeneratedColumn, Column, ManyToOne, JoinColumn, OneToOne } from 'typeorm' +import { Decimal } from 'decimal.js-light' + +import { DecimalTransformer } from '../../src/typeorm/DecimalTransformer' +import { Account } from '../Account' +import { TransactionRecipe } from '../TransactionRecipe' + +@Entity('confirmed_transactions') +export class ConfirmedTransaction { + @PrimaryGeneratedColumn('increment', { unsigned: true, type: 'bigint' }) + id: number + + @OneToOne(() => TransactionRecipe, (recipe) => recipe.confirmedTransaction) + @JoinColumn({ name: 'transaction_recipe_id' }) + transactionRecipe: TransactionRecipe + + @Column({ name: 'transaction_recipe_id', type: 'int', unsigned: true }) + transactionRecipeId: number + + @Column({ type: 'bigint' }) + nr: number + + @Column({ type: 'binary', length: 48 }) + runningHash: Buffer + + @ManyToOne(() => Account, (account) => account.confirmedTransactions) + @JoinColumn({ name: 'account_id' }) + account: Account + + @Column({ name: 'account_id', type: 'int', unsigned: true }) + accountId: number + + @Column({ + name: 'account_balance', + type: 'decimal', + precision: 40, + scale: 20, + nullable: false, + default: 0, + transformer: DecimalTransformer, + }) + accountBalance: Decimal + + @Column({ name: 'iota_milestone', type: 'bigint' }) + iotaMilestone: number + + @Column({ name: 'confirmed_at', type: 'datetime' }) + confirmedAt: Date +} diff --git a/dlt-database/entity/0002-refactor_add_community/User.ts b/dlt-database/entity/0002-refactor_add_community/User.ts new file mode 100644 index 000000000..5387be058 --- /dev/null +++ b/dlt-database/entity/0002-refactor_add_community/User.ts @@ -0,0 +1,39 @@ +import { BaseEntity, Entity, PrimaryGeneratedColumn, Column, OneToMany, JoinColumn } from 'typeorm' + +import { Account } from '../Account' + +@Entity('users', { engine: 'InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci' }) +export class User extends BaseEntity { + @PrimaryGeneratedColumn('increment', { unsigned: true }) + id: number + + @Column({ + name: 'gradido_id', + length: 36, + nullable: true, + collation: 'utf8mb4_unicode_ci', + }) + gradidoID?: string + + @Column({ name: 'derive1_pubkey', type: 'binary', length: 32, unique: true }) + derive1Pubkey: Buffer + + @Column({ + name: 'created_at', + type: 'datetime', + precision: 3, + default: () => 'CURRENT_TIMESTAMP(3)', + }) + createdAt: Date + + @Column({ + name: 'confirmed_at', + type: 'datetime', + nullable: true, + }) + confirmedAt?: Date + + @OneToMany(() => Account, (account) => account.user) + @JoinColumn({ name: 'user_id' }) + accounts?: Account[] +} diff --git a/dlt-database/entity/Account.ts b/dlt-database/entity/Account.ts index c5d1722b1..ed1e92840 100644 --- a/dlt-database/entity/Account.ts +++ b/dlt-database/entity/Account.ts @@ -1 +1 @@ -export { Account } from './0001-init_db/Account' +export { Account } from './0002-refactor_add_community/Account' diff --git a/dlt-database/entity/AccountCommunity.ts b/dlt-database/entity/AccountCommunity.ts index 8bd78e073..985e7bfb9 100644 --- a/dlt-database/entity/AccountCommunity.ts +++ b/dlt-database/entity/AccountCommunity.ts @@ -1 +1 @@ -export { AccountCommunity } from './0001-init_db/AccountCommunity' +export { AccountCommunity } from './0002-refactor_add_community/AccountCommunity' diff --git a/dlt-database/entity/ConfirmedTransaction.ts b/dlt-database/entity/ConfirmedTransaction.ts index d91aad926..765e0b2e6 100644 --- a/dlt-database/entity/ConfirmedTransaction.ts +++ b/dlt-database/entity/ConfirmedTransaction.ts @@ -1 +1 @@ -export { ConfirmedTransaction } from './0001-init_db/ConfirmedTransaction' +export { ConfirmedTransaction } from './0002-refactor_add_community/ConfirmedTransaction' diff --git a/dlt-database/entity/User.ts b/dlt-database/entity/User.ts index c31bcc47f..3c803d783 100644 --- a/dlt-database/entity/User.ts +++ b/dlt-database/entity/User.ts @@ -1 +1 @@ -export { User } from './0001-init_db/User' +export { User } from './0002-refactor_add_community/User' diff --git a/dlt-database/migrations/0002-refactor_add_community.ts b/dlt-database/migrations/0002-refactor_add_community.ts index 90e7e179a..725954ea0 100644 --- a/dlt-database/migrations/0002-refactor_add_community.ts +++ b/dlt-database/migrations/0002-refactor_add_community.ts @@ -12,6 +12,25 @@ export async function upgrade(queryFn: (query: string, values?: any[]) => Promis await queryFn( `ALTER TABLE \`communities\` MODIFY COLUMN \`root_chaincode\` binary(32) NULL DEFAULT NULL;`, ) + await queryFn( + `ALTER TABLE \`communities\` MODIFY COLUMN \`confirmed_at\` datetime NULL DEFAULT NULL;`, + ) + await queryFn(`ALTER TABLE \`users\` MODIFY COLUMN \`confirmed_at\` datetime NULL DEFAULT NULL;`) + await queryFn( + `ALTER TABLE \`accounts\` MODIFY COLUMN \`confirmed_at\` datetime NULL DEFAULT NULL;`, + ) + await queryFn( + `ALTER TABLE \`accounts\` MODIFY COLUMN \`balance_date\` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP;`, + ) + await queryFn( + `ALTER TABLE \`accounts_communities\` MODIFY COLUMN \`valid_from\` datetime NOT NULL;`, + ) + await queryFn( + `ALTER TABLE \`accounts_communities\` MODIFY COLUMN \`valid_to\` datetime NULL DEFAULT NULL;`, + ) + await queryFn( + `ALTER TABLE \`confirmed_transactions\` MODIFY COLUMN \`confirmed_at\` datetime NOT NULL;`, + ) } export async function downgrade(queryFn: (query: string, values?: any[]) => Promise>) { @@ -22,4 +41,21 @@ export async function downgrade(queryFn: (query: string, values?: any[]) => Prom await queryFn( `ALTER TABLE \`communities\` MODIFY COLUMN \`root_chaincode\` binary(32) DEFAULT NULL;`, ) + await queryFn( + `ALTER TABLE \`communities\` MODIFY COLUMN \`confirmed_at\` datetime(3) DEFAULT NULL;`, + ) + await queryFn(`ALTER TABLE \`users\` MODIFY COLUMN \`confirmed_at\` datetime(3) DEFAULT NULL;`) + await queryFn(`ALTER TABLE \`accounts\` MODIFY COLUMN \`confirmed_at\` datetime(3) DEFAULT NULL;`) + await queryFn( + `ALTER TABLE \`accounts\` MODIFY COLUMN \`balance_date\` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3);`, + ) + await queryFn( + `ALTER TABLE \`accounts_communities\` MODIFY COLUMN \`valid_from\` datetime(3) NOT NULL;`, + ) + await queryFn( + `ALTER TABLE \`accounts_communities\` MODIFY COLUMN \`valid_to\` datetime(3) DEFAULT NULL;`, + ) + await queryFn( + `ALTER TABLE \`confirmed_transactions\` MODIFY COLUMN \`confirmed_at\` datetime(3) NOT NULL;`, + ) }