73 lines
2.3 KiB
TypeScript
73 lines
2.3 KiB
TypeScript
// import { Sequelize, DataTypes, Optional, Model, UUIDV4 } from 'sequelize';
|
|
import {
|
|
Association, DataTypes, HasManyAddAssociationMixin, HasManyCountAssociationsMixin,
|
|
HasManyCreateAssociationMixin, HasManyGetAssociationsMixin, HasManyHasAssociationMixin,
|
|
HasManySetAssociationsMixin, HasManyAddAssociationsMixin, HasManyHasAssociationsMixin,
|
|
HasManyRemoveAssociationMixin, HasManyRemoveAssociationsMixin, Model, ModelDefined, Optional,
|
|
Sequelize, InferAttributes, InferCreationAttributes, CreationOptional, NonAttribute, ForeignKey, BelongsTo, BelongsToGetAssociationMixin, UUIDV4, UUID
|
|
} from 'sequelize';
|
|
|
|
import { MUser, UserModel } from '@/model/sequelize/User';
|
|
const sequelize = new Sequelize({
|
|
dialect: 'sqlite',
|
|
storage: 'db.sqlite'
|
|
});
|
|
// interface AuthAttributes{
|
|
// id: number;
|
|
// token: string;
|
|
// user_id: number;
|
|
// };
|
|
// interface AuthCreationAttributes extends Optional<Optional<AuthAttributes, 'id'>,'token'> {};
|
|
// class AuthModel extends Model<AuthAttributes, AuthCreationAttributes>{
|
|
// createdAt?: Date;
|
|
// updatedAt?: Date;
|
|
// token: any;
|
|
// // declare title
|
|
// }
|
|
class AuthModel extends Model<InferAttributes<AuthModel, { omit: 'user' }>, InferCreationAttributes<AuthModel, { omit: 'user' }>> {
|
|
declare id: CreationOptional<number>;
|
|
declare token: CreationOptional<string>;
|
|
declare user_id: number;
|
|
declare getUser: BelongsToGetAssociationMixin<UserModel>;
|
|
declare user:NonAttribute<UserModel>;
|
|
}
|
|
export const MAuth = AuthModel.init(
|
|
{
|
|
id: {
|
|
allowNull: false,
|
|
autoIncrement: true,
|
|
type: DataTypes.INTEGER,
|
|
primaryKey: true,
|
|
unique: true,
|
|
},
|
|
token: {
|
|
type: DataTypes.UUID,
|
|
defaultValue: UUIDV4
|
|
},
|
|
user_id: {
|
|
type: DataTypes.INTEGER,
|
|
key: "User"
|
|
}
|
|
// date: DataTypes.DATE,
|
|
},
|
|
{
|
|
tableName: 'Auths',
|
|
sequelize // passing the `sequelize` instance is required
|
|
}
|
|
);
|
|
|
|
MAuth.belongsTo(MUser, {
|
|
foreignKey:'user_id',
|
|
targetKey:'id',
|
|
as: 'user'
|
|
})
|
|
|
|
MUser.hasMany(MAuth, {
|
|
sourceKey: 'id',
|
|
foreignKey: 'user_id',
|
|
as: 'authtokens' // this determines the name in `associations`!
|
|
});
|
|
// MAuth.belongsTo(MUser, { targetKey: 'id' });
|
|
// MAuth.sync();
|
|
|
|
export { AuthModel } |