DbCreator.js 954 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. const InpxParser = require('./InpxParser');
  2. class DbCreator {
  3. constructor(config) {
  4. this.config = config;
  5. }
  6. async run(db, callback) {
  7. const config = this.config;
  8. //book
  9. await db.create({
  10. table: 'book'
  11. });
  12. //парсинг
  13. const parser = new InpxParser();
  14. const readFileCallback = async(readState) => {
  15. callback(readState);
  16. };
  17. let recsLoaded = 0;
  18. let id = 0;
  19. const parsedCallback = async(chunk) => {
  20. for (const rec of chunk) {
  21. rec.id = ++id;
  22. }
  23. await db.insert({table: 'book', rows: chunk});
  24. recsLoaded += chunk.length;
  25. callback({recsLoaded});
  26. };
  27. await parser.parse(config.inpxFile, readFileCallback, parsedCallback);
  28. //поисковые таблицы
  29. }
  30. }
  31. module.exports = DbCreator;