mirror of
https://github.com/frappe/books.git
synced 2025-01-10 18:24:40 +00:00
fix: dont cache imports
- prevent OOM crashes
This commit is contained in:
parent
1b951e1513
commit
f0e5e294a1
@ -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();
|
||||||
this.addToCache(doc);
|
if (cacheDoc) {
|
||||||
|
this.addToCache(doc);
|
||||||
|
}
|
||||||
return doc;
|
return doc;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -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,38 +377,14 @@ 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 doc.set(docObj);
|
await this.makeEntry(doc, docObj);
|
||||||
await doc.insert();
|
|
||||||
if (this.shouldSubmit) {
|
|
||||||
await doc.submit();
|
|
||||||
}
|
|
||||||
entriesMade += 1;
|
entriesMade += 1;
|
||||||
setLoadingStatus(true, entriesMade, docObjs.length);
|
setLoadingStatus(true, entriesMade, docObjs.length);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
setLoadingStatus(false, entriesMade, docObjs.length);
|
setLoadingStatus(false, entriesMade, docObjs.length);
|
||||||
const messages = [
|
return this.handleError(doc, err as Error, status);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
status.names.push(doc.name);
|
status.names.push(doc.name);
|
||||||
@ -427,4 +399,34 @@ export class Importer {
|
|||||||
const emptyRow = Array(this.columnLabels.length).fill('');
|
const emptyRow = Array(this.columnLabels.length).fill('');
|
||||||
this.parsedValues.push(emptyRow);
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -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>;
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user