/ src / models / users.model.ts
users.model.ts
  1  // See http://docs.sequelizejs.com/en/latest/docs/models-definition/
  2  // for more of what you can do here.
  3  import {
  4  	DataTypes,
  5  	type Model,
  6  	type ModelStatic,
  7  	type Optional,
  8  	type Sequelize,
  9  } from "sequelize";
 10  
 11  import { UUID } from "sequelize";
 12  import { v4 as uuidv4 } from "uuid";
 13  
 14  import type { Application } from "../declarations";
 15  
 16  export interface UserAttributes {
 17  	id: string;
 18  	username: string;
 19  	email: string;
 20  	password?: string;
 21  	role: string;
 22  	createdAt?: string; // ISO date string
 23  	updatedAt?: string; // ISO date string
 24  }
 25  
 26  interface UserCreationAttributes extends Optional<UserAttributes, "id"> {}
 27  
 28  /**
 29   * @swagger
 30   * components:
 31   *   schemas:
 32   *     User:
 33   *       type: object
 34   *       properties:
 35   *         id:
 36   *           type: string
 37   *           format: uuid
 38   *         username:
 39   *           type: string
 40   *         email:
 41   *           type: string
 42   *           format: email
 43   *         password:
 44   *           type: string
 45   *         role:
 46   *           type: string
 47   *       required:
 48   *         - username
 49   *         - email
 50   *         - password
 51   *         - role
 52   */
 53  export default function (
 54  	app: Application,
 55  ): ModelStatic<Model<UserAttributes, UserCreationAttributes>> {
 56  	const sequelizeClient: Sequelize = app.get("sequelizeClient");
 57  	const users = sequelizeClient.define<
 58  		Model<UserAttributes, UserCreationAttributes>
 59  	>(
 60  		"users",
 61  		{
 62  			id: {
 63  				type: DataTypes.UUID,
 64  				defaultValue: uuidv4(),
 65  				primaryKey: true,
 66  				unique: true,
 67  				allowNull: false,
 68  			},
 69  			username: {
 70  				type: DataTypes.STRING,
 71  				allowNull: false,
 72  				unique: true,
 73  			},
 74  			email: {
 75  				type: DataTypes.STRING,
 76  				allowNull: false,
 77  				unique: true,
 78  			},
 79  			password: {
 80  				type: DataTypes.STRING,
 81  				allowNull: false,
 82  			},
 83  			role: {
 84  				type: DataTypes.STRING,
 85  				allowNull: false,
 86  			},
 87  		},
 88  		{
 89  			hooks: {
 90  				// biome-ignore lint/suspicious/noExplicitAny: <explanation>
 91  				beforeCount(options: any) {
 92  					options.raw = true;
 93  				},
 94  			},
 95  		},
 96  	);
 97  	// biome-ignore lint/suspicious/noExplicitAny: <explanation>
 98  	(users as any).associate = (models: any) => {
 99  		// Define associations here
100  		// See http://docs.sequelizejs.com/en/latest/docs/associations/
101  	};
102  
103  	return users;
104  }