2
0
mirror of https://github.com/frappe/books.git synced 2024-09-20 11:29:00 +00:00

fix: dont cache imports

- prevent OOM crashes
This commit is contained in:
18alantom 2022-03-01 14:53:43 +05:30 committed by Alan
parent 1b951e1513
commit f0e5e294a1
3 changed files with 49 additions and 36 deletions

View File

@ -7,8 +7,6 @@ const {
DEFAULT_DISPLAY_PRECISION, DEFAULT_DISPLAY_PRECISION,
} = require('./utils/consts'); } = require('./utils/consts');
const { markRaw } = require('vue'); const { markRaw } = require('vue');
const { ipcRenderer } = require('electron');
const { IPC_ACTIONS } = require('@/messages');
module.exports = { module.exports = {
initializeAndRegister(customModels = {}, force = false) { initializeAndRegister(customModels = {}, force = false) {
@ -278,11 +276,13 @@ module.exports = {
return newDoc; return newDoc;
}, },
getNewDoc(doctype) { getNewDoc(doctype, cacheDoc = true) {
let doc = this.newDoc({ doctype: doctype }); let doc = this.newDoc({ doctype: doctype });
doc._notInserted = true; doc._notInserted = true;
doc.name = frappe.getRandomString(); doc.name = frappe.getRandomString();
if (cacheDoc) {
this.addToCache(doc); this.addToCache(doc);
}
return doc; return doc;
}, },

View File

@ -1,4 +1,4 @@
import { Field, FieldType } from '@/types/model'; import { Doc, Map, Field, FieldType } from '@/types/model';
import frappe from 'frappe'; import frappe from 'frappe';
import { isNameAutoSet } from 'frappe/model/naming'; import { isNameAutoSet } from 'frappe/model/naming';
import { parseCSV } from './csvParser'; import { parseCSV } from './csvParser';
@ -23,10 +23,6 @@ type Exclusion = {
[key: string]: string[]; [key: string]: string[];
}; };
type Map = {
[key: string]: unknown;
};
type ObjectMap = { type ObjectMap = {
[key: string]: Map; [key: string]: Map;
}; };
@ -381,22 +377,41 @@ export class Importer {
delete docObj[key]; delete docObj[key];
} }
const doc = frappe.getNewDoc(this.doctype); const doc: Doc = frappe.getNewDoc(this.doctype, false);
try { try {
await this.makeEntry(doc, docObj);
entriesMade += 1;
setLoadingStatus(true, entriesMade, docObjs.length);
} catch (err) {
setLoadingStatus(false, entriesMade, docObjs.length);
return this.handleError(doc, err as Error, status);
}
status.names.push(doc.name);
}
setLoadingStatus(false, entriesMade, docObjs.length);
status.success = true;
return status;
}
addRow() {
const emptyRow = Array(this.columnLabels.length).fill('');
this.parsedValues.push(emptyRow);
}
async makeEntry(doc: Doc, docObj: Map) {
await doc.set(docObj); await doc.set(docObj);
await doc.insert(); await doc.insert();
if (this.shouldSubmit) { if (this.shouldSubmit) {
await doc.submit(); await doc.submit();
} }
entriesMade += 1; }
setLoadingStatus(true, entriesMade, docObjs.length);
} catch (err) {
setLoadingStatus(false, entriesMade, docObjs.length);
const messages = [
frappe.t`Could not import ${this.doctype} ${doc.name}.`,
];
const message = (err as Error).message; handleError(doc: Doc, err: Error, status: Status): Status {
const messages = [frappe.t`Could not import ${this.doctype} ${doc.name}.`];
const message = err.message;
if (message?.includes('UNIQUE constraint failed')) { if (message?.includes('UNIQUE constraint failed')) {
messages.push(frappe.t`${doc.name} already exists.`); messages.push(frappe.t`${doc.name} already exists.`);
} else if (message) { } else if (message) {
@ -414,17 +429,4 @@ export class Importer {
status.message = messages.join(' '); status.message = messages.join(' ');
return status; return status;
} }
status.names.push(doc.name);
}
setLoadingStatus(false, entriesMade, docObjs.length);
status.success = true;
return status;
}
addRow() {
const emptyRow = Array(this.columnLabels.length).fill('');
this.parsedValues.push(emptyRow);
}
} }

View File

@ -1,3 +1,7 @@
export type Map = {
[key: string]: unknown;
};
export enum FieldType { export enum FieldType {
Data = 'Data', Data = 'Data',
Select = 'Select', Select = 'Select',
@ -27,3 +31,10 @@ export interface Field {
hidden?: number | Function; hidden?: number | Function;
options?: string[]; options?: string[];
} }
export interface Doc {
name: string;
set: (fieldname: Map | string, value?: unknown) => Promise<void>;
insert: () => Promise<void>;
submit: () => Promise<void>;
}