commit working state
This commit is contained in:
@@ -4,7 +4,9 @@ import {
|
||||
HasManySetAssociationsMixin, HasManyAddAssociationsMixin, HasManyHasAssociationsMixin,
|
||||
HasManyRemoveAssociationMixin, HasManyRemoveAssociationsMixin, Model, ModelDefined, Optional,
|
||||
Sequelize, InferAttributes, InferCreationAttributes, CreationOptional, NonAttribute, ForeignKey, BelongsTo, BelongsToGetAssociationMixin, UUIDV4, UUID,
|
||||
VirtualDataType
|
||||
VirtualDataType,
|
||||
HasOneGetAssociationMixin,
|
||||
HasOneCreateAssociationMixin
|
||||
} from 'sequelize';
|
||||
|
||||
|
||||
@@ -13,11 +15,35 @@ const sequelize = new Sequelize({
|
||||
storage: 'db.sqlite'
|
||||
});
|
||||
|
||||
export class UserPerms extends Model<InferAttributes<UserPerms>, InferCreationAttributes<UserPerms>> {
|
||||
declare id: CreationOptional<number>;
|
||||
declare user:NonAttribute<User>;
|
||||
declare user_id:ForeignKey<User['id']>;
|
||||
declare isAdmin:CreationOptional<boolean>;
|
||||
}
|
||||
UserPerms.init({
|
||||
id: {
|
||||
allowNull: false,
|
||||
autoIncrement: true,
|
||||
type: DataTypes.INTEGER,
|
||||
primaryKey: true,
|
||||
unique: true,
|
||||
},
|
||||
isAdmin: {
|
||||
allowNull: false,
|
||||
defaultValue: false,
|
||||
type: DataTypes.BOOLEAN
|
||||
}
|
||||
},
|
||||
{
|
||||
tableName: 'Perms',
|
||||
sequelize // passing the `sequelize` instance is required
|
||||
});
|
||||
|
||||
|
||||
export class Auth extends Model<InferAttributes<Auth>, InferCreationAttributes<Auth>> {
|
||||
declare id: CreationOptional<number>;
|
||||
declare token: CreationOptional<string>;
|
||||
declare user:NonAttribute<User>;
|
||||
declare user_id:ForeignKey<User['id']>;
|
||||
declare getUser: BelongsToGetAssociationMixin<User>;
|
||||
declare static associations: {
|
||||
@@ -96,6 +122,13 @@ export class User extends Model<InferAttributes<User>, InferCreationAttributes<U
|
||||
declare password: string;
|
||||
declare getAuthTokens:HasManyGetAssociationsMixin<Auth>;
|
||||
declare getPosts:HasManyGetAssociationsMixin<Post>;
|
||||
declare getPerms:HasOneGetAssociationMixin<UserPerms>;
|
||||
declare createPerms:HasOneCreateAssociationMixin<UserPerms>;
|
||||
declare perms?:NonAttribute<UserPerms>
|
||||
declare static associations: {
|
||||
perms: Association<UserPerms,User>;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
User.init(
|
||||
@@ -114,20 +147,19 @@ User.init(
|
||||
password: {
|
||||
allowNull: false,
|
||||
type: DataTypes.STRING,
|
||||
}
|
||||
|
||||
},
|
||||
},
|
||||
{
|
||||
defaultScope: {
|
||||
attributes:{
|
||||
exclude:['password']
|
||||
}
|
||||
attributes: {
|
||||
exclude: ['password'],
|
||||
},
|
||||
include: [{ association: 'perms' }],
|
||||
},
|
||||
tableName: 'Users',
|
||||
sequelize // passing the `sequelize` instance is required
|
||||
}
|
||||
);
|
||||
User.scope
|
||||
|
||||
Auth.belongsTo(User, {
|
||||
foreignKey:'user_id',
|
||||
@@ -149,7 +181,19 @@ User.hasMany(Post, {
|
||||
foreignKey: 'user_id',
|
||||
as: 'posts' // this determines the name in `associations`!
|
||||
});
|
||||
UserPerms.belongsTo(User, {
|
||||
foreignKey:'user_id',
|
||||
targetKey:'id',
|
||||
as: 'user'
|
||||
})
|
||||
User.hasOne(UserPerms, {
|
||||
sourceKey: 'id',
|
||||
foreignKey: 'user_id',
|
||||
as: 'perms'
|
||||
})
|
||||
|
||||
|
||||
User.sync();
|
||||
Auth.sync();
|
||||
Post.sync();
|
||||
Post.sync();
|
||||
UserPerms.sync();
|
||||
Reference in New Issue
Block a user