2022-04-24 12:18:44 +05:30
|
|
|
import { Doc } from 'fyo/model/doc';
|
2022-04-19 11:29:36 +05:30
|
|
|
import { DocMap, ModelMap, SinglesMap } from 'fyo/model/types';
|
|
|
|
import { coreModels } from 'fyo/models';
|
2022-05-26 15:41:15 +05:30
|
|
|
import { NotFoundError, ValueError } from 'fyo/utils/errors';
|
2022-04-19 11:29:36 +05:30
|
|
|
import Observable from 'fyo/utils/observable';
|
2022-04-22 16:32:03 +05:30
|
|
|
import { Schema } from 'schemas/types';
|
2022-04-21 18:38:36 +05:30
|
|
|
import { getRandomString } from 'utils';
|
2022-04-19 11:29:36 +05:30
|
|
|
import { Fyo } from '..';
|
2022-10-13 17:10:28 +05:30
|
|
|
import { DocValueMap, RawValueMap } from './types';
|
2022-03-22 14:58:36 +05:30
|
|
|
|
|
|
|
export class DocHandler {
|
2022-04-19 11:29:36 +05:30
|
|
|
fyo: Fyo;
|
2022-04-28 00:09:34 +05:30
|
|
|
models: ModelMap = {};
|
2022-04-18 13:31:41 +05:30
|
|
|
singles: SinglesMap = {};
|
2022-05-03 23:18:50 +05:30
|
|
|
docs: Observable<DocMap | undefined> = new Observable();
|
2022-04-28 00:09:34 +05:30
|
|
|
observer: Observable<never> = new Observable();
|
2023-02-20 11:22:48 +05:30
|
|
|
#temporaryNameCounters: Record<string, number>;
|
2022-03-22 14:58:36 +05:30
|
|
|
|
2022-04-19 11:29:36 +05:30
|
|
|
constructor(fyo: Fyo) {
|
|
|
|
this.fyo = fyo;
|
2023-02-20 11:22:48 +05:30
|
|
|
this.#temporaryNameCounters = {};
|
2022-03-22 14:58:36 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
init() {
|
|
|
|
this.models = {};
|
2022-04-22 16:32:03 +05:30
|
|
|
this.singles = {};
|
2022-03-22 14:58:36 +05:30
|
|
|
this.docs = new Observable();
|
2022-04-28 00:09:34 +05:30
|
|
|
this.observer = new Observable();
|
2022-03-22 14:58:36 +05:30
|
|
|
}
|
|
|
|
|
2022-07-30 16:33:09 +05:30
|
|
|
async purgeCache() {
|
2022-04-22 16:32:03 +05:30
|
|
|
this.init();
|
|
|
|
}
|
|
|
|
|
2022-04-18 12:12:56 +05:30
|
|
|
registerModels(models: ModelMap, regionalModels: ModelMap = {}) {
|
2022-04-19 11:29:36 +05:30
|
|
|
for (const schemaName in this.fyo.db.schemaMap) {
|
2022-04-18 12:12:56 +05:30
|
|
|
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 14:58:36 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Doc Operations
|
|
|
|
*/
|
|
|
|
|
|
|
|
async getDoc(
|
2022-04-07 11:50:14 +05:30
|
|
|
schemaName: string,
|
2022-05-26 15:41:15 +05:30
|
|
|
name?: string,
|
2022-03-22 14:58:36 +05:30
|
|
|
options = { skipDocumentCache: false }
|
|
|
|
) {
|
2022-05-26 15:41:15 +05:30
|
|
|
if (name === undefined) {
|
|
|
|
name = schemaName;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (name === schemaName && !this.fyo.schemaMap[schemaName]?.isSingle) {
|
|
|
|
throw new ValueError(`${schemaName} is not a Single Schema`);
|
|
|
|
}
|
|
|
|
|
2022-04-07 11:50:14 +05:30
|
|
|
let doc: Doc | undefined;
|
2022-03-22 14:58:36 +05:30
|
|
|
if (!options?.skipDocumentCache) {
|
2022-05-03 23:18:50 +05:30
|
|
|
doc = this.#getFromCache(schemaName, name);
|
2022-03-22 14:58:36 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if (doc) {
|
|
|
|
return doc;
|
|
|
|
}
|
|
|
|
|
2022-10-08 00:11:50 +05:30
|
|
|
doc = this.getNewDoc(schemaName, { name }, false);
|
2022-03-22 14:58:36 +05:30
|
|
|
await doc.load();
|
2023-04-17 09:59:12 +05:30
|
|
|
this.#addToCache(doc);
|
2022-03-22 14:58:36 +05:30
|
|
|
|
|
|
|
return doc;
|
|
|
|
}
|
|
|
|
|
2022-04-22 16:32:03 +05:30
|
|
|
getNewDoc(
|
|
|
|
schemaName: string,
|
2022-10-13 17:10:28 +05:30
|
|
|
data: DocValueMap | RawValueMap = {},
|
2022-04-24 12:18:44 +05:30
|
|
|
cacheDoc: boolean = true,
|
2022-04-22 16:32:03 +05:30
|
|
|
schema?: Schema,
|
2022-10-13 17:10:28 +05:30
|
|
|
Model?: typeof Doc,
|
|
|
|
isRawValueMap: boolean = true
|
2022-04-22 16:32:03 +05:30
|
|
|
): Doc {
|
2022-04-22 17:31:04 +05:30
|
|
|
if (!this.models[schemaName] && Model) {
|
|
|
|
this.models[schemaName] = Model;
|
|
|
|
}
|
|
|
|
|
|
|
|
Model ??= this.models[schemaName];
|
2022-04-22 16:32:03 +05:30
|
|
|
schema ??= this.fyo.schemaMap[schemaName];
|
2022-04-24 12:18:44 +05:30
|
|
|
|
2022-04-07 11:50:14 +05:30
|
|
|
if (schema === undefined) {
|
2022-05-04 13:49:40 +05:30
|
|
|
throw new NotFoundError(`Schema not found for ${schemaName}`);
|
2022-04-07 11:50:14 +05:30
|
|
|
}
|
|
|
|
|
2022-10-13 17:10:28 +05:30
|
|
|
const doc = new Model!(schema, data, this.fyo, isRawValueMap);
|
2023-02-21 11:51:32 +05:30
|
|
|
doc.name ??= this.getTemporaryName(schema);
|
2022-04-24 12:18:44 +05:30
|
|
|
if (cacheDoc) {
|
2023-04-17 09:59:12 +05:30
|
|
|
this.#addToCache(doc);
|
2022-04-24 12:18:44 +05:30
|
|
|
}
|
|
|
|
|
2022-03-22 14:58:36 +05:30
|
|
|
return doc;
|
|
|
|
}
|
|
|
|
|
2023-03-20 13:16:53 +05:30
|
|
|
isTemporaryName(name: string, schema: Schema): boolean {
|
|
|
|
const label = schema.label ?? schema.name;
|
|
|
|
const template = this.fyo.t`New ${label} `;
|
|
|
|
return name.includes(template);
|
|
|
|
}
|
|
|
|
|
2023-02-21 11:51:32 +05:30
|
|
|
getTemporaryName(schema: Schema): string {
|
2023-02-20 11:22:48 +05:30
|
|
|
if (schema.naming === 'random') {
|
|
|
|
return getRandomString();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.#temporaryNameCounters[schema.name] ??= 1;
|
|
|
|
|
|
|
|
const idx = this.#temporaryNameCounters[schema.name];
|
|
|
|
this.#temporaryNameCounters[schema.name] = idx + 1;
|
2023-03-20 13:16:53 +05:30
|
|
|
const label = schema.label ?? schema.name;
|
2023-02-20 11:22:48 +05:30
|
|
|
|
2023-03-20 13:16:53 +05:30
|
|
|
return this.fyo.t`New ${label} ${String(idx).padStart(2, '0')}`;
|
2023-02-20 11:22:48 +05:30
|
|
|
}
|
|
|
|
|
2022-05-03 23:18:50 +05:30
|
|
|
/**
|
|
|
|
* Cache operations
|
|
|
|
*/
|
|
|
|
|
2023-04-17 09:59:12 +05:30
|
|
|
#addToCache(doc: Doc) {
|
2022-05-03 23:18:50 +05:30
|
|
|
if (!doc.name) {
|
2022-03-22 14:58:36 +05:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-05-03 23:18:50 +05:30
|
|
|
const name = doc.name;
|
|
|
|
const schemaName = doc.schemaName;
|
|
|
|
|
|
|
|
if (!this.docs[schemaName]) {
|
|
|
|
this.docs.set(schemaName, {});
|
2022-05-04 13:49:40 +05:30
|
|
|
this.#setCacheUpdationListeners(schemaName);
|
2022-05-03 23:18:50 +05:30
|
|
|
}
|
|
|
|
|
2023-04-17 09:59:12 +05:30
|
|
|
this.docs.get(schemaName)![name] = doc;
|
2022-05-03 23:18:50 +05:30
|
|
|
|
|
|
|
// singles available as first level objects too
|
|
|
|
if (schemaName === doc.name) {
|
|
|
|
this.singles[name] = doc;
|
2022-03-22 14:58:36 +05:30
|
|
|
}
|
2022-04-01 15:05:51 +05:30
|
|
|
|
2022-05-03 23:18:50 +05:30
|
|
|
// propagate change to `docs`
|
|
|
|
doc.on('change', (params: unknown) => {
|
|
|
|
this.docs!.trigger('change', params);
|
|
|
|
});
|
|
|
|
|
|
|
|
doc.on('afterSync', () => {
|
2023-04-14 13:27:44 +05:30
|
|
|
if (doc.name === name && this.#cacheHas(schemaName, name)) {
|
2022-05-03 23:18:50 +05:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-04-17 09:59:12 +05:30
|
|
|
this.removeFromCache(doc.schemaName, name);
|
|
|
|
this.#addToCache(doc);
|
2022-05-03 23:18:50 +05:30
|
|
|
});
|
2022-05-04 13:49:40 +05:30
|
|
|
}
|
2022-05-03 23:18:50 +05:30
|
|
|
|
2022-05-04 13:49:40 +05:30
|
|
|
#setCacheUpdationListeners(schemaName: string) {
|
|
|
|
this.fyo.db.observer.on(`delete:${schemaName}`, (name: string) => {
|
2023-04-17 09:59:12 +05:30
|
|
|
this.removeFromCache(schemaName, name);
|
2022-05-03 23:18:50 +05:30
|
|
|
});
|
2022-05-04 13:49:40 +05:30
|
|
|
|
|
|
|
this.fyo.db.observer.on(
|
|
|
|
`rename:${schemaName}`,
|
|
|
|
(names: { oldName: string; newName: string }) => {
|
|
|
|
const doc = this.#getFromCache(schemaName, names.oldName);
|
|
|
|
if (doc === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-04-17 09:59:12 +05:30
|
|
|
this.removeFromCache(schemaName, names.oldName);
|
|
|
|
this.#addToCache(doc);
|
2022-05-04 13:49:40 +05:30
|
|
|
}
|
|
|
|
);
|
2022-05-03 23:18:50 +05:30
|
|
|
}
|
|
|
|
|
2023-04-17 09:59:12 +05:30
|
|
|
removeFromCache(schemaName: string, name: string) {
|
2022-05-03 23:18:50 +05:30
|
|
|
const docMap = this.docs.get(schemaName);
|
|
|
|
delete docMap?.[name];
|
|
|
|
}
|
|
|
|
|
|
|
|
#getFromCache(schemaName: string, name: string): Doc | undefined {
|
|
|
|
const docMap = this.docs.get(schemaName);
|
|
|
|
return docMap?.[name];
|
2022-04-07 11:50:14 +05:30
|
|
|
}
|
2023-04-14 13:27:44 +05:30
|
|
|
|
|
|
|
#cacheHas(schemaName: string, name: string): boolean {
|
|
|
|
return !!this.#getFromCache(schemaName, name);
|
|
|
|
}
|
2022-03-22 14:58:36 +05:30
|
|
|
}
|