数据迁移
This commit is contained in:
@@ -13,7 +13,8 @@ export default defineEventHandler(async event => {
|
||||
|
||||
if (event.node.req.method === 'GET') {
|
||||
const query = getQuery(event)
|
||||
return await event.context.db.Blog.findAll({ where: query }).then(list => {
|
||||
const order = [['id', 'DESC']]
|
||||
return await event.context.db.Blog.findAll({ where: query, order }).then(list => {
|
||||
const regex = /<code\s+class="(.*)"\s*>([\s\S]*?)<\/code>/g;
|
||||
return list.map(item => ({
|
||||
...item.dataValues,
|
||||
@@ -35,7 +36,7 @@ export default defineEventHandler(async event => {
|
||||
}
|
||||
const regex = /<code\s+class="(.*)"\s*>([\s\S]*?)<\/code>/g;
|
||||
return await event.context.db.Blog.create({
|
||||
name: lexer(content).find(item => item.type === 'heading' && item.depth === 1) ?? '',
|
||||
name: lexer(content).find(item => item.type === 'heading' && item.depth === 1)?.text ?? '',
|
||||
content,
|
||||
user_id: event.context.user.id
|
||||
}).then(data => ({
|
||||
|
||||
27
server/api/blog/update.js
Normal file
27
server/api/blog/update.js
Normal file
@@ -0,0 +1,27 @@
|
||||
export default defineEventHandler(async event => {
|
||||
// 转移数据
|
||||
async function mover_data() {
|
||||
const { Blog } = event.context.db
|
||||
const keys = await useStorage('blog').getKeys();
|
||||
const all = await Promise.all(keys.map(async item => {
|
||||
return await useStorage('blog').getItem(item);
|
||||
}))
|
||||
all.sort((a, b) => new Date(a.createdAt) - new Date(b.createdAt))
|
||||
for (let item of all) {
|
||||
const { content, createdAt, updatedAt } = item
|
||||
const blog = await Blog.findOne({ where: { content } })
|
||||
if (!blog) {
|
||||
const res = await Blog.create({
|
||||
user_id: 1,
|
||||
name: lexer(content).find(i => i.type === 'heading' && i.depth === 1)?.text ?? '',
|
||||
content,
|
||||
createdAt: new Date(createdAt),
|
||||
updatedAt: new Date(updatedAt)
|
||||
})
|
||||
console.log(res.dataValues)
|
||||
}
|
||||
}
|
||||
return { success: true, message: '数据转移完成' }
|
||||
}
|
||||
return await mover_data()
|
||||
})
|
||||
@@ -1,4 +1,5 @@
|
||||
import sequelize from 'sequelize'
|
||||
import { lexer, marked } from 'marked'
|
||||
|
||||
export default defineNitroPlugin(nitro => {
|
||||
console.log('database plugin')
|
||||
@@ -53,14 +54,14 @@ export default defineNitroPlugin(nitro => {
|
||||
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 },
|
||||
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() }
|
||||
})
|
||||
@@ -68,23 +69,6 @@ export default defineNitroPlugin(nitro => {
|
||||
// 同步数据模型
|
||||
db.sync({ alter: true, logging: false })
|
||||
|
||||
//// 转移数据
|
||||
//useStorage('blog').getKeys().then(keys => keys.forEach(item => {
|
||||
// useStorage('blog').getItem(item).then(async data => {
|
||||
// if (!data.title) return
|
||||
// const rest = await Blog.findOne({ where: { title: data.title } })
|
||||
// if (rest) return console.log(rest)
|
||||
// const resx = await Blog.create({
|
||||
// title: data.title || '',
|
||||
// user_id: 0,
|
||||
// content: data.content,
|
||||
// createdAt: data.createdAt,
|
||||
// updatedAt: data.updatedAt
|
||||
// })
|
||||
// console.log(resx)
|
||||
// })
|
||||
//}))
|
||||
|
||||
// 挂载到上下文
|
||||
nitro.hooks.hook('request', event => {
|
||||
event.context.db = { User, Blog, Gallery, Avatar, Session }
|
||||
|
||||
Reference in New Issue
Block a user