0.0.7 • Published 2 years ago

alpha-nestjsx-crud v0.0.7

Weekly downloads
-
License
-
Repository
-
Last release
2 years ago

Alpha-NestJSX-Crud

Usage:

Let's define a model like this

import { Optional } from 'sequelize';
import {
  BeforeSave,
  BelongsToMany,
  Column,
  DataType,
  HasMany,
  Model,
  Table,
} from 'sequelize-typescript';

export interface UserAttributes {
  id: number;
  firstName: string;
  lastName: string;
  password: string;
  email: string;
  name: string;
}
export type UserCreationAttributes = Optional<UserAttributes, 'id'>;
@Table()
export class User
  extends Model<UserAttributes, UserCreationAttributes>
  implements UserAttributes
{
  @Column({
    autoIncrement: true,
    primaryKey: true,
    type: DataType.INTEGER,
  })
  id: number;

  @Column({
    type: DataType.STRING,
    allowNull: false,
  })
  firstName: string;
  @Column({
    type: DataType.STRING,
    allowNull: false,
  })
  lastName: string;

  @Column({
    type: DataType.VIRTUAL(DataType.STRING),
    get(): string {
      return `${this.getDataValue('firstName') || ''} ${
        this.getDataValue('lastName') || ''
      }`;
    },
  })
  name: string;

  @Column({
    type: DataType.CITEXT,
    unique: true,
    allowNull: false,
  })
  email: string;
}

Then you can implement AlphaSequelizeCrudService like the following:

@Injectable()
export class UserService
  extends AlphaSequelizeCrudService<User> {

  constructor(
    @InjectModel(User) private readonly userModel: typeof User
  ) {
    super(userModel);
  }
}