外键约束
This commit is contained in:
		@@ -18,8 +18,11 @@ export default defineEventHandler(async event => {
 | 
			
		||||
        if (query.archive) {
 | 
			
		||||
            query.archive = query.archive === 'true'
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return await event.context.db.Blog.findAll({ where: query, order }).then(list => {
 | 
			
		||||
        const include = [{
 | 
			
		||||
            model: event.context.db.User,
 | 
			
		||||
            attributes: ['id', 'name', 'avatar']
 | 
			
		||||
        }]
 | 
			
		||||
        return await event.context.db.Blog.findAll({ where: query, order, include }).then(list => {
 | 
			
		||||
            const regex = /<code\s+class="(.*)"\s*>([\s\S]*?)<\/code>/g;
 | 
			
		||||
            return list.map(item => ({
 | 
			
		||||
                ...item.dataValues,
 | 
			
		||||
 
 | 
			
		||||
@@ -12,7 +12,7 @@ export default defineNitroPlugin(nitro => {
 | 
			
		||||
    })
 | 
			
		||||
 | 
			
		||||
    // 定义数据模型
 | 
			
		||||
    const User = db.define('User', {
 | 
			
		||||
    const User = db.define('user', {
 | 
			
		||||
        id: { type: INTEGER, primaryKey: true, autoIncrement: true },
 | 
			
		||||
        name: { type: STRING, allowNull: false },
 | 
			
		||||
        avatar: { type: STRING, allowNull: true },
 | 
			
		||||
@@ -23,49 +23,53 @@ export default defineNitroPlugin(nitro => {
 | 
			
		||||
        updatedAt: { type: DATE, allowNull: false, defaultValue: new Date() }
 | 
			
		||||
    })
 | 
			
		||||
 | 
			
		||||
    const Session = db.define('Session', {
 | 
			
		||||
    const Session = db.define('session', {
 | 
			
		||||
        id: { type: STRING, primaryKey: true },
 | 
			
		||||
        user_id: { type: INTEGER, allowNull: false },
 | 
			
		||||
        user_id: { type: INTEGER, allowNull: false, references: { model: User, key: User.id } },
 | 
			
		||||
        ip: { type: STRING, allowNull: false },
 | 
			
		||||
        ua: { type: STRING, allowNull: false },
 | 
			
		||||
        createdAt: { type: DATE, allowNull: false, defaultValue: new Date() },
 | 
			
		||||
        updatedAt: { type: DATE, allowNull: false, defaultValue: new Date() }
 | 
			
		||||
    })
 | 
			
		||||
 | 
			
		||||
    const Avatar = db.define('Avatar', {
 | 
			
		||||
    const Avatar = db.define('avatar', {
 | 
			
		||||
        id: { type: INTEGER, primaryKey: true, autoIncrement: true },
 | 
			
		||||
        user_id: { type: INTEGER, allowNull: false },
 | 
			
		||||
        user_id: { type: INTEGER, allowNull: false, references: { model: User, key: User.id } },
 | 
			
		||||
        url: { type: STRING, allowNull: false },
 | 
			
		||||
        createdAt: { type: DATE, allowNull: false, defaultValue: new Date() },
 | 
			
		||||
        updatedAt: { type: DATE, allowNull: false, defaultValue: new Date() }
 | 
			
		||||
    })
 | 
			
		||||
 | 
			
		||||
    const Blog = db.define('Blog', {
 | 
			
		||||
    const Blog = db.define('blog', {
 | 
			
		||||
        id: { type: INTEGER, primaryKey: true, autoIncrement: true },
 | 
			
		||||
        name: { type: STRING, allowNull: false },
 | 
			
		||||
        content: { type: STRING, allowNull: false },
 | 
			
		||||
        user_id: { type: INTEGER, allowNull: false, defaultValue: 1 },
 | 
			
		||||
        user_id: { type: INTEGER, allowNull: false, references: { model: User, key: User.id } },
 | 
			
		||||
        archive: { type: BOOLEAN, defaultValue: false },
 | 
			
		||||
        createdAt: { type: DATE, allowNull: false, defaultValue: new Date() },
 | 
			
		||||
        updatedAt: { type: DATE, allowNull: false, defaultValue: new Date() }
 | 
			
		||||
    })
 | 
			
		||||
 | 
			
		||||
    const Gallery = db.define('Gallery', {
 | 
			
		||||
    const Gallery = db.define('gallery', {
 | 
			
		||||
        id: { type: INTEGER, primaryKey: true, autoIncrement: true },
 | 
			
		||||
        name: { type: STRING, allowNull: false },
 | 
			
		||||
        description: { type: STRING, defaultValue: '' },
 | 
			
		||||
        path: { type: STRING },
 | 
			
		||||
        width: { type: INTEGER },
 | 
			
		||||
        height: { type: INTEGER },
 | 
			
		||||
        format: { type: STRING },
 | 
			
		||||
        mimetype: { type: STRING },
 | 
			
		||||
        size: { type: INTEGER },
 | 
			
		||||
        user_id: { type: INTEGER },
 | 
			
		||||
        nsfw: { type: BOOLEAN, defaultValue: false },
 | 
			
		||||
        createdAt: { type: DATE, defaultValue: new Date() },
 | 
			
		||||
        updatedAt: { type: DATE, defaultValue: new Date() }
 | 
			
		||||
        description: { type: STRING, allowNull: false, defaultValue: '' },
 | 
			
		||||
        path: { type: STRING, allowNull: false },
 | 
			
		||||
        width: { type: INTEGER, allowNull: false },
 | 
			
		||||
        height: { type: INTEGER, allowNull: false },
 | 
			
		||||
        format: { type: STRING, allowNull: false },
 | 
			
		||||
        mimetype: { type: STRING, allowNull: false },
 | 
			
		||||
        size: { type: INTEGER, allowNull: false },
 | 
			
		||||
        user_id: { type: INTEGER, allowNull: false, references: { model: User, key: User.id } },
 | 
			
		||||
        nsfw: { type: BOOLEAN, allowNull: false, defaultValue: false },
 | 
			
		||||
        createdAt: { type: DATE, allowNull: false, defaultValue: new Date() },
 | 
			
		||||
        updatedAt: { type: DATE, allowNull: false, defaultValue: new Date() }
 | 
			
		||||
    })
 | 
			
		||||
 | 
			
		||||
    // 定义关联
 | 
			
		||||
    User.hasMany(Blog, { foreignKey: 'user_id' })   // 一个用户有多篇文章
 | 
			
		||||
    Blog.belongsTo(User, { foreignKey: 'user_id' }) // 一篇文章属于一个用户
 | 
			
		||||
 | 
			
		||||
    // 同步数据模型
 | 
			
		||||
    db.sync({ alter: true, logging: false })
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user