DEV Community

Jason
Jason

Posted on

Several issues regarding typescript+sequelize

The existing code is as follows:
// # Account.model.ts

import { DataTypes, Model, Optional } from 'sequelize'
import connection from '../connection'

interface AccountAttributes {
  id?: string
  platformId: string
  accountId: string
  nickname?: string
  followerCount: number
  followingCount: number
  likesCount: number
  worksCount: number
  beLikeCount: number
  createdAt?: Date
  updatedAt?: Date
  deletedAt?: Date
}

export interface AccountInput extends Optional<AccountAttributes, 'id'> {}

export interface AccountOuput extends Required<AccountAttributes> {}

class Account extends Model<AccountAttributes> implements AccountAttributes {
  public id!: string
  public platformId!: string
  public accountId!: string
  public nickname!: string
  public followerCount!: number
  public followingCount!: number
  public likesCount!: number
  public worksCount!: number
  public beLikeCount!: number

  public readonly createdAt!: Date
  public readonly updatedAt!: Date
  public readonly deletedAt!: Date
}

Account.init(
  {
    id: {
      type: DataTypes.UUID,
      allowNull: false,
      primaryKey: true,
      defaultValue: DataTypes.UUIDV4,
    },
    platformId: {
      type: DataTypes.UUID,
      allowNull: false,
    },
    accountId: {
      type: DataTypes.STRING,
      allowNull: false,
    },
    nickname: DataTypes.STRING,
    followerCount: DataTypes.INTEGER.UNSIGNED,
    followingCount: DataTypes.INTEGER.UNSIGNED,
    likesCount: DataTypes.INTEGER.UNSIGNED,
    worksCount: DataTypes.INTEGER.UNSIGNED,
    beLikeCount: DataTypes.INTEGER.UNSIGNED,
  },
  {
    sequelize: connection,
    modelName: 'Account',
  },
)

export default Account
Enter fullscreen mode Exit fullscreen mode

// #Account.repository.ts

import Account, {
  AccountInput,
  AccountOuput,
} from 'src/database/models/account'
import { GetAllFilters } from '../types/filter.types'
import { Op } from 'sequelize'
import { PagedResult } from 'src/types'

export const create = async (payload: AccountInput): Promise<AccountOuput> => {
  const entity = await Account.create(payload)
  return entity
}

export const update = async (
  id: string,
  payload: Partial<AccountInput>,
): Promise<AccountOuput> => {
  const entity = await Account.findByPk(id)
  if (!entity) {
    throw new Error('not found', { cause: 404 })
  }
  const updatedEntity = await entity.update(payload)
  return updatedEntity
}
Enter fullscreen mode Exit fullscreen mode

// #Account.service.ts

import { PagedResult } from 'src/types'
import * as accountRepository from '../repositories/account.repository'
import { AccountInput, AccountOuput } from 'src/database/models/account'

export const create = (payload: AccountInput): Promise<AccountOuput> => {
  return accountRepository.create(payload)
}

export const update = (
  id: string,
  payload: Partial<AccountInput>,
): Promise<AccountOuput> => {
  return accountRepository.update(id, payload)
}
Enter fullscreen mode Exit fullscreen mode

// #Account.controller.ts

import { Request, Response } from 'express'
import { asyncHandler, getPaginationParams, responseHandler } from 'src/helpers'
import * as service from '../services/account.service'
import { AccountInput } from 'src/database/models/account'

interface AccountCreationDto {
  platformId: string
  accountId: string
  nickname?: string
  followerCount: number
  followingCount: number
  likesCount: number
  worksCount: number
  beLikeCount: number
}

export const create = asyncHandler(async (req: Request, res: Response) => {
  try {
    const payload = req.body as AccountCreationDto
    const result = service.create(payload)
    return res.status(201).json(responseHandler(true, 201, 'success', result))
  } catch (error) {
    console.log(error)
    return res
      .status(500)
      .json(responseHandler(false, 500, (error as any)?.message, null))
  }
})
Enter fullscreen mode Exit fullscreen mode

Question:
Why do I need to define AccountCreationDto when I have already defined AccountInput?

Top comments (0)