Last active
May 24, 2025 16:32
-
-
Save mqnoy/c3a836047139e2c663163757996854b4 to your computer and use it in GitHub Desktop.
POC sequelize Model with base CRUD interface for repository
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
DestroyOptions, | |
UpdateOptions, | |
DataTypes, | |
Model, | |
Optional, | |
Sequelize, | |
WhereOptions, | |
} from 'sequelize' | |
export interface UserAttributes { | |
user_id: number | |
name: string | |
email: string | |
password: string | |
login_token: string | |
} | |
export type UserCreationAttributes = Optional<UserAttributes, 'user_id'> | |
export class Users extends Model<UserAttributes, UserCreationAttributes> implements UserAttributes { | |
declare user_id: number | |
declare name: string | |
declare email: string | |
declare password: string | |
declare login_token: string | |
static initModel(sequelize: Sequelize) { | |
Users.init( | |
{ | |
user_id: { | |
type: DataTypes.BIGINT, | |
primaryKey: true, | |
autoIncrement: true, | |
allowNull: false, | |
}, | |
name: { | |
type: DataTypes.STRING(255), | |
allowNull: false, | |
}, | |
email: { | |
type: DataTypes.STRING(255), | |
allowNull: false, | |
unique: true, | |
}, | |
password: { | |
type: DataTypes.STRING(255), | |
allowNull: false, | |
}, | |
login_token: { | |
type: DataTypes.TEXT, | |
allowNull: true, | |
}, | |
}, | |
{ | |
sequelize, | |
timestamps: false, | |
tableName: 'users', | |
} | |
) | |
} | |
} | |
interface SequelizeCrud<M, C> { | |
create(values: C): Promise<M> | |
read(where: WhereOptions<M>): Promise<M | null> | |
update(data: M, options: UpdateOptions<M>): Promise<number> | |
delete(options: DestroyOptions<M>): Promise<number> | |
} | |
interface UserRepositoryAbstract extends SequelizeCrud<Users, UserCreationAttributes> { | |
updateEmailByUserId(email: string, user_id: number): Promise<number> | |
} | |
class UserRepository implements UserRepositoryAbstract { | |
create = async (values: UserCreationAttributes): Promise<Users> => { | |
return await Users.create(values) | |
} | |
read = async (where: WhereOptions<Users>): Promise<Users | null> => { | |
return await Users.findOne({ where }) | |
} | |
update = async (data: Users, options: UpdateOptions<UserAttributes>): Promise<number> => { | |
const [affectedCount] = await Users.update(data, options) | |
return affectedCount | |
} | |
delete = async (options: DestroyOptions<Users>): Promise<number> => { | |
return await Users.destroy(options) | |
} | |
updateEmailByUserId = async (email: string, user_id: number): Promise<number> => { | |
const [affectedCount] = await Users.update({ email }, { where: { user_id } }) | |
return affectedCount | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use declare — it's the standard, safest approach in TypeScript + Sequelize v6+.