2
0
mirror of https://github.com/frappe/books.git synced 2024-11-15 01:44:04 +00:00
books/fyo/core/docHandler.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

193 lines
4.3 KiB
TypeScript
Raw Normal View History

import { Doc } from 'fyo/model/doc';
import { DocMap, ModelMap, SinglesMap } from 'fyo/model/types';
import { coreModels } from 'fyo/models';
import Observable from 'fyo/utils/observable';
2022-04-22 11:02:03 +00:00
import { Schema } from 'schemas/types';
import { getRandomString } from 'utils';
import { Fyo } from '..';
import { DocValue, DocValueMap } from './types';
2022-03-22 09:28:36 +00:00
export class DocHandler {
fyo: Fyo;
models: ModelMap = {};
2022-04-18 08:01:41 +00:00
singles: SinglesMap = {};
docs: Observable<DocMap> = new Observable();
observer: Observable<never> = new Observable();
2022-03-22 09:28:36 +00:00
constructor(fyo: Fyo) {
this.fyo = fyo;
2022-03-22 09:28:36 +00:00
}
init() {
this.models = {};
2022-04-22 11:02:03 +00:00
this.singles = {};
2022-03-22 09:28:36 +00:00
this.docs = new Observable();
this.observer = new Observable();
2022-03-22 09:28:36 +00:00
}
2022-04-22 11:02:03 +00:00
purgeCache() {
this.init();
}
registerModels(models: ModelMap, regionalModels: ModelMap = {}) {
for (const schemaName in this.fyo.db.schemaMap) {
if (coreModels[schemaName] !== undefined) {
this.models[schemaName] = coreModels[schemaName];
} else if (regionalModels[schemaName] !== undefined) {
this.models[schemaName] = regionalModels[schemaName];
} else if (models[schemaName] !== undefined) {
this.models[schemaName] = models[schemaName];
} else {
this.models[schemaName] = Doc;
}
2022-03-22 09:28:36 +00:00
}
}
/**
* Cache operations
*/
addToCache(doc: Doc) {
if (!this.docs) {
return;
}
2022-03-22 09:28:36 +00:00
// add to `docs` cache
const name = doc.name;
const schemaName = doc.schemaName;
2022-03-22 09:28:36 +00:00
if (!name) {
2022-03-22 09:28:36 +00:00
return;
}
if (!this.docs[schemaName]) {
this.docs[schemaName] = {};
2022-03-22 09:28:36 +00:00
}
(this.docs[schemaName] as DocMap)[name] = doc;
2022-03-22 09:28:36 +00:00
// singles available as first level objects too
if (schemaName === doc.name) {
2022-03-22 09:28:36 +00:00
this.singles[name] = doc;
}
// propagate change to `docs`
2022-03-22 09:28:36 +00:00
doc.on('change', (params: unknown) => {
this.docs!.trigger('change', params);
});
}
removeFromCache(schemaName: string, name: string) {
const docMap = this.docs[schemaName] as DocMap | undefined;
delete docMap?.[name];
2022-03-22 09:28:36 +00:00
}
getFromCache(schemaName: string, name: string): Doc | undefined {
const docMap = this.docs[schemaName] as DocMap | undefined;
return docMap?.[name];
2022-03-22 09:28:36 +00:00
}
getCachedValue(
schemaName: string,
name: string,
fieldname: string
): DocValue | Doc[] | undefined {
const docMap = this.docs[schemaName] as DocMap;
const doc = docMap[name];
2022-03-22 09:28:36 +00:00
if (doc === undefined) {
return;
2022-03-22 09:28:36 +00:00
}
return doc.get(fieldname);
2022-03-22 09:28:36 +00:00
}
isDirty(schemaName: string, name: string): boolean {
const doc = (this.docs?.[schemaName] as DocMap)?.[name];
if (doc === undefined) {
return false;
2022-03-22 09:28:36 +00:00
}
return doc.dirty;
2022-03-22 09:28:36 +00:00
}
/**
* Doc Operations
*/
async getDoc(
schemaName: string,
2022-03-22 09:28:36 +00:00
name: string,
options = { skipDocumentCache: false }
) {
let doc: Doc | undefined;
2022-03-22 09:28:36 +00:00
if (!options?.skipDocumentCache) {
doc = this.getFromCache(schemaName, name);
2022-03-22 09:28:36 +00:00
}
if (doc) {
return doc;
}
doc = this.getNewDoc(schemaName, { name });
2022-03-22 09:28:36 +00:00
await doc.load();
this.addToCache(doc);
return doc;
}
async getSingle(schemaName: string) {
return await this.getDoc(schemaName, schemaName);
2022-03-22 09:28:36 +00:00
}
async getDuplicate(doc: Doc): Promise<Doc> {
const newDoc = await doc.duplicate(false);
delete newDoc.name;
2022-03-22 09:28:36 +00:00
return newDoc;
}
2022-04-22 11:02:03 +00:00
getNewDoc(
schemaName: string,
data: DocValueMap = {},
cacheDoc: boolean = true,
2022-04-22 11:02:03 +00:00
schema?: Schema,
Model?: typeof Doc
): Doc {
2022-04-22 12:01:04 +00:00
if (!this.models[schemaName] && Model) {
this.models[schemaName] = Model;
}
Model ??= this.models[schemaName];
2022-04-22 11:02:03 +00:00
schema ??= this.fyo.schemaMap[schemaName];
if (schema === undefined) {
throw new Error(`Schema not found for ${schemaName}`);
}
2022-04-22 12:01:04 +00:00
const doc = new Model!(schema, data, this.fyo);
doc.name ??= getRandomString();
if (cacheDoc) {
this.addToCache(doc);
}
2022-03-22 09:28:36 +00:00
return doc;
}
async syncDoc(schemaName: string, data: DocValueMap) {
const name = data.name as string | undefined;
if (name === undefined) {
2022-03-22 09:28:36 +00:00
return;
}
const docExists = await this.fyo.db.exists(schemaName, name);
if (!docExists) {
const doc = this.getNewDoc(schemaName, data);
await doc.sync();
return;
2022-03-22 09:28:36 +00:00
}
const doc = await this.getDoc(schemaName, name);
await doc.setMultiple(data);
await doc.sync();
}
2022-03-22 09:28:36 +00:00
}