-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
多个mock模块如何公用一套数据? #47
Comments
由于插件是对 每个 对于 需要 实现对数据的操作并保存操作后的结果,建议使用 lowdb 之类的 nosql ,将数据保存到本地文件中,通过
import { join, dirname } from 'node:path'
import { fileURLToPath } from 'node:url'
import { Low } from 'lowdb'
import { JSONFile } from 'lowdb/node'
const __dirname = dirname(fileURLToPath(import.meta.url))
export function createDBHelper(filename: string) {
const file = join(__dirname, filename)
const adapter = new JSONFile(file)
const defaultData = { posts: [] }
const db = new Low(adapter, defaultData)
return db
}
import { defineConfig } from 'vite-plugin-mock-dev-server'
import { createDBHelper } from './db'
const db = createDBHelper('data.json')
export default defineConfig({
url: '/api/post',
body: async () => {
await db.read()
return db.data.posts
}
}) import { defineConfig } from 'vite-plugin-mock-dev-server'
import { createDBHelper } from './db'
const db = createDBHelper('data.json')
export default defineConfig({
url: '/api/post/delete',
body: async (query) => {
await db.read()
db.data.posts.splice(0, 1)
await db.write()
return { message: 'success' }
}
}) |
根据这类场景,目前我计划在 插件中 新增一个 |
好的,谢谢,目前先用 nosql 了 |
谢谢 更新太快了 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
做示例数据时需要多个*.mock.ts同时公用一套数据,但是用单例类模式也会在每个mock模块中重新生成数据,导致列表和编辑不是一条数据
The text was updated successfully, but these errors were encountered: