Skip to content

Backend: Implement Core Entities for Distribution, FeeConfig, User, and Wallet Modules #1

Description

@mubarak23

We need to implement the core database entities and models for four key modules in our backend system. These entities will serve as the foundation for the application's data layer and are perfect for contributors looking to get familiar with our codebase structure.

Tasks Breakdown

Phase 1: Entity Creation

Create Distribution.entity.ts with all specified fields and relationships
Create FeeConfig.entity.ts with fee calculation logic support
Create User.entity.ts with authentication and authorization fields
Create Wallet.entity.ts with multi-network support

Phase 2: Database Integration

Create corresponding database migrations
Add entities to the main database module
Set up proper indexing for performance optimization
Add database constraints and validations

Folder Structure

src/
├──component/
│   ├──v1
│        ├──Distribution
│            ├── Distribution.entity.ts
│        ├── Wallet
│            ├── Wallet.entity.ts
│        ├──FeeConfig
│            ├──FeeConfig.entity.ts
│        ├──User
│            ├──User.entity.ts

Source Definition (Drizzle Model)

const distributionModel = pgTable(
  "Distribution",
  {
    id: text().$default(generateUUID).primaryKey().notNull(),
    user_address: text("user_address").notNull(),
    transaction_hash: text("transaction_hash"),
    token_address: text("token_address").notNull(),
    token_symbol: text("token_symbol").notNull(),
    token_decimals: integer("token_decimals").notNull(),
    total_amount: numeric("total_amount", { precision: 65, scale: 30 }).notNull(),
    fee_amount: numeric("fee_amount", { precision: 65, scale: 30 }).notNull(),
    usd_rate: numeric("usd_rate", { precision: 65, scale: 30 }).default("0"),
    total_usd_amount: numeric("total_usd_amount", { precision: 65, scale: 30 }).default("0"),
    total_recipients: integer("total_recipients").notNull(),
    distribution_type: distribution_type("distribution_type").notNull(),
    chain_name: text("chain_name").default(""),
    status: distribution_status().default("pending").notNull(),
    block_number: bigint("block_number", { mode: "number" }),
    block_timestamp: timestamp("block_timestamp", { precision: 3, mode: "date" }),
    network: network().default("mainnet").notNull(),
    created_at: timestamp("created_at", { precision: 3, mode: "date" }).default(sql`CURRENT_TIMESTAMP`).notNull(),
    metadata: jsonb(),
  },
  (table) => [
    index("Distribution_created_at_idx").using("btree", table.created_at.asc().nullsLast().op("timestamp_ops")),
    index("Distribution_status_idx").using("btree", table.status.asc().nullsLast().op("enum_ops")),
    index("Distribution_transaction_hash_idx").using("btree", table.transaction_hash.asc().nullsLast().op("text_ops")),
    index("Distribution_user_address_idx").using("btree", table.user_address.asc().nullsLast().op("text_ops")),
  ]
);
export const feeConfigModel = pgTable(
  "FeeConfig",
  {
    id: text().$default(generateUUID).primaryKey().notNull(),
    network: network().notNull(),
    chainId: text("chain_id").notNull(),
    chainName: text("chain_name").notNull(),
    createdAt: timestamp("created_at").defaultNow(),
    updatedAt: timestamp("updated_at").defaultNow().notNull(),
    amount: numeric("amount", { precision: 65, scale: 30 }).notNull(),
  },
  (table) => ({
    uniqueNetwork: unique().on(table.chainName, table.network, table.chainId),
  })
);

export const userModel = pgTable(
  "User",
  {
    id: text().$default(generateUUID).primaryKey().notNull(),
    username: text().notNull(),
    email: text().notNull(),

    // renamed fields ↓
    created_at: timestamp({ precision: 3, mode: "date" })
      .default(sql`CURRENT_TIMESTAMP`)
      .notNull(),
    updated_at: timestamp({ precision: 3, mode: "date" })
      .default(sql`CURRENT_TIMESTAMP`)
      .notNull(),
  },
  (table) => [
    uniqueIndex("User_email_key").using(
      "btree",
      table.email.asc().nullsLast().op("text_ops")
    ),
  ]
);

🎯 Task

✅ Convert the above to use TypeORM decorators
✅ Use PostgreSQL types appropriately (numeric, jsonb, timestamp, enum, etc.)
✅ Ensure all fields match the original definitions (nullability, defaults, precision, etc.)
✅ Define appropriate @Index() decorators to match the existing indexes
✅ If enums like distribution_type, distribution_status, and network are used, define them using enum in TypeScript

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Backendonlydust-waveContribute to awesome OSS repos during OnlyDust's open source week

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions