wasowasowaso
This commit is contained in:
+50
-15
@@ -1,22 +1,37 @@
|
||||
import { Sequelize, DataTypes, Optional, Model, UUIDV4 } from 'sequelize';
|
||||
// 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
|
||||
// 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 = sequelize.define<AuthModel>(
|
||||
'Auth',
|
||||
export const MAuth = AuthModel.init(
|
||||
{
|
||||
id: {
|
||||
allowNull: false,
|
||||
@@ -34,5 +49,25 @@ export const MAuth = sequelize.define<AuthModel>(
|
||||
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 }
|
||||
Reference in New Issue
Block a user