This commit is contained in:
29
plugins/cook/db/index.js
Normal file
29
plugins/cook/db/index.js
Normal file
@ -0,0 +1,29 @@
|
||||
import { createParser } from 'node-csv';
|
||||
import logger from '#root/utils/logger.js';
|
||||
|
||||
let recipeData;
|
||||
let loaded = false;
|
||||
|
||||
function parser() {
|
||||
csv = createParser();
|
||||
return new Promise((r, j) => {
|
||||
csv.parserFile('./recipe.csv', function (err, data) {
|
||||
if (err) {
|
||||
logger.err(err);
|
||||
return j();
|
||||
}
|
||||
logger('食谱加载成功');
|
||||
recipeData = data;
|
||||
loaded = true;
|
||||
r(recipeData);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export default async function getDb() {
|
||||
if (loaded) {
|
||||
return recipeData;
|
||||
} else {
|
||||
return await parser();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user