2
0
mirror of https://github.com/frappe/books.git synced 2024-11-12 16:36:27 +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,
} = require('./utils/consts');
const { markRaw } = require('vue');
const { ipcRenderer } = require('electron');
const { IPC_ACTIONS } = require('@/messages');
module.exports = {
initializeAndRegister(customModels = {}, force = false) {
@ -278,11 +276,13 @@ module.exports = {
return newDoc;
},
getNewDoc(doctype) {
getNewDoc(doctype, cacheDoc = true) {
let doc = this.newDoc({ doctype: doctype });
doc._notInserted = true;
doc.name = frappe.getRandomString();
this.addToCache(doc);
if (cacheDoc) {
this.addToCache(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 { isNameAutoSet } from 'frappe/model/naming';
import { parseCSV } from './csvParser';
@ -23,10 +23,6 @@ type Exclusion = {
[key: string]: string[];
};
type Map = {
[key: string]: unknown;
};
type ObjectMap = {
[key: string]: Map;
};
@ -381,38 +377,14 @@ export class Importer {
delete docObj[key];
}
const doc = frappe.getNewDoc(this.doctype);
const doc: Doc = frappe.getNewDoc(this.doctype, false);
try {
await doc.set(docObj);
await doc.insert();
if (this.shouldSubmit) {
await doc.submit();
}
await this.makeEntry(doc, docObj);
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;
if (message?.includes('UNIQUE constraint failed')) {
messages.push(frappe.t`${doc.name} already exists.`);
} else if (message) {
messages.push(message);
}
if (status.names.length) {
messages.push(
frappe.t`The following ${
status.names.length
} entries were created: ${status.names.join(', ')}`
);
}
status.message = messages.join(' ');
return status;
return this.handleError(doc, err as Error, status);
}
status.names.push(doc.name);
@ -427,4 +399,34 @@ export class Importer {
const emptyRow = Array(this.columnLabels.length).fill('');
this.parsedValues.push(emptyRow);
}
async makeEntry(doc: Doc, docObj: Map) {
await doc.set(docObj);
await doc.insert();
if (this.shouldSubmit) {
await doc.submit();
}
}
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')) {
messages.push(frappe.t`${doc.name} already exists.`);
} else if (message) {
messages.push(message);
}
if (status.names.length) {
messages.push(
frappe.t`The following ${
status.names.length
} entries were created: ${status.names.join(', ')}`
);
}
status.message = messages.join(' ');
return status;
}
}

View File

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