2
0
mirror of https://github.com/frappe/books.git synced 2024-12-23 03:19:01 +00:00

incr: add type converters

This commit is contained in:
18alantom 2022-03-31 17:13:20 +05:30
parent 4dbecad1cb
commit 1567dc762f
4 changed files with 90 additions and 11 deletions

View File

@ -1,11 +1,15 @@
import { DatabaseDemux } from '@/demux/db'; import { DatabaseDemux } from '@/demux/db';
import { Frappe } from 'frappe/core/frappe'; import { Frappe } from 'frappe/core/frappe';
import Money from 'pesa/dist/types/src/money';
import { getSchemas } from 'schemas';
import { FieldType, FieldTypeEnum, RawValue, SchemaMap } from 'schemas/types';
import { DatabaseBase, GetAllOptions } from 'utils/db/types'; import { DatabaseBase, GetAllOptions } from 'utils/db/types';
import { DocValueMap, RawValueMap } from './types'; import { DocValue, DocValueMap, RawValueMap, SingleValue } from './types';
export class DatabaseHandler extends DatabaseBase { export class DatabaseHandler extends DatabaseBase {
#frappe: Frappe; #frappe: Frappe;
#demux: DatabaseDemux; #demux: DatabaseDemux;
// #schemaMap: Readonly<SchemaMap>;
constructor(frappe: Frappe) { constructor(frappe: Frappe) {
super(); super();
@ -23,13 +27,14 @@ export class DatabaseHandler extends DatabaseBase {
init() { init() {
// do nothing // do nothing
// this.#schemaMap = getSchemas();
} }
async insert( async insert(
schemaName: string, schemaName: string,
docValueMap: DocValueMap docValueMap: DocValueMap
): Promise<DocValueMap> { ): Promise<DocValueMap> {
let rawValueMap = this.toRawValueMap( let rawValueMap = this.#toRawValueMap(
schemaName, schemaName,
docValueMap docValueMap
) as RawValueMap; ) as RawValueMap;
@ -38,7 +43,7 @@ export class DatabaseHandler extends DatabaseBase {
schemaName, schemaName,
rawValueMap rawValueMap
)) as RawValueMap; )) as RawValueMap;
return this.toDocValueMap(schemaName, rawValueMap) as DocValueMap; return this.#toDocValueMap(schemaName, rawValueMap) as DocValueMap;
} }
// Read // Read
@ -53,7 +58,7 @@ export class DatabaseHandler extends DatabaseBase {
name, name,
fields fields
)) as RawValueMap; )) as RawValueMap;
return this.toDocValueMap(schemaName, rawValueMap) as DocValueMap; return this.#toDocValueMap(schemaName, rawValueMap) as DocValueMap;
} }
async getAll( async getAll(
@ -66,13 +71,16 @@ export class DatabaseHandler extends DatabaseBase {
options options
)) as RawValueMap[]; )) as RawValueMap[];
return this.toDocValueMap(schemaName, rawValueMap) as DocValueMap[]; return this.#toDocValueMap(schemaName, rawValueMap) as DocValueMap[];
} }
async getSingleValues( async getSingleValues(
...fieldnames: ({ fieldname: string; parent?: string } | string)[] ...fieldnames: ({ fieldname: string; parent?: string } | string)[]
): Promise<{ fieldname: string; parent: string; value: unknown }[]> { ): Promise<SingleValue<DocValue>> {
await this.#demux.call('getSingleValues', ...fieldnames); const rawSingleValue = (await this.#demux.call(
'getSingleValues',
...fieldnames
)) as SingleValue<RawValue>;
} }
// Update // Update
@ -85,7 +93,7 @@ export class DatabaseHandler extends DatabaseBase {
} }
async update(schemaName: string, docValueMap: DocValueMap): Promise<void> { async update(schemaName: string, docValueMap: DocValueMap): Promise<void> {
const rawValueMap = this.toRawValueMap(schemaName, docValueMap); const rawValueMap = this.#toRawValueMap(schemaName, docValueMap);
await this.#demux.call('update', schemaName, rawValueMap); await this.#demux.call('update', schemaName, rawValueMap);
} }
@ -103,12 +111,60 @@ export class DatabaseHandler extends DatabaseBase {
return (await this.#demux.call('exists', schemaName, name)) as boolean; return (await this.#demux.call('exists', schemaName, name)) as boolean;
} }
toDocValueMap( #toDocValueMap(
schemaName: string, schemaName: string,
rawValueMap: RawValueMap | RawValueMap[] rawValueMap: RawValueMap | RawValueMap[]
): DocValueMap | DocValueMap[] {} ): DocValueMap | DocValueMap[] {}
toRawValueMap( #toRawValueMap(
schemaName: string, schemaName: string,
docValueMap: DocValueMap | DocValueMap[] docValueMap: DocValueMap | DocValueMap[]
): RawValueMap | RawValueMap[] {} ): RawValueMap | RawValueMap[] {}
#toDocValue(value: RawValue, fieldtype: FieldType): DocValue {
switch (fieldtype) {
case FieldTypeEnum.Currency:
return this.#frappe.pesa((value ?? 0) as string | number);
case FieldTypeEnum.Date:
return new Date(value as string);
case FieldTypeEnum.Datetime:
return new Date(value as string);
case FieldTypeEnum.Int:
return +(value as string | number);
case FieldTypeEnum.Float:
return +(value as string | number);
case FieldTypeEnum.Check:
return Boolean(value as number);
default:
return String(value);
}
}
#toRawValue(value: DocValue, fieldtype: FieldType): RawValue {
switch (fieldtype) {
case FieldTypeEnum.Currency:
return (value as Money).store;
case FieldTypeEnum.Date:
return (value as Date).toISOString().split('T')[0];
case FieldTypeEnum.Datetime:
return (value as Date).toISOString();
case FieldTypeEnum.Int: {
if (typeof value === 'string') {
return parseInt(value);
}
return Math.floor(value as number);
}
case FieldTypeEnum.Float: {
if (typeof value === 'string') {
return parseFloat(value);
}
return value as number;
}
case FieldTypeEnum.Check:
return Number(value);
default:
return String(value);
}
}
} }

View File

@ -5,3 +5,9 @@ export type DocValue = string | number | boolean | Date | Money;
export type DocValueMap = Record<string, DocValue | DocValueMap[]>; export type DocValueMap = Record<string, DocValue | DocValueMap[]>;
export type RawValueMap = Record<string, RawValue | RawValueMap[]>; export type RawValueMap = Record<string, RawValue | RawValueMap[]>;
export type SingleValue<T> = {
fieldname: string;
parent: string;
value: T;
}[];

View File

@ -12,15 +12,31 @@ const NAME_FIELD = {
readOnly: true, readOnly: true,
}; };
export function getSchemas(countryCode: string = '-'): SchemaMap { export function getSchemas(countryCode: string = '-'): Readonly<SchemaMap> {
const builtCoreSchemas = getCoreSchemas(); const builtCoreSchemas = getCoreSchemas();
const builtAppSchemas = getAppSchemas(countryCode); const builtAppSchemas = getAppSchemas(countryCode);
let schemaMap = Object.assign({}, builtAppSchemas, builtCoreSchemas); let schemaMap = Object.assign({}, builtAppSchemas, builtCoreSchemas);
schemaMap = addMetaFields(schemaMap); schemaMap = addMetaFields(schemaMap);
deepFreeze(schemaMap);
return schemaMap; return schemaMap;
} }
function deepFreeze(schemaMap: SchemaMap) {
Object.freeze(schemaMap);
for (const schemaName in schemaMap) {
Object.freeze(schemaMap[schemaName]);
for (const key in schemaMap[schemaName]) {
// @ts-ignore
Object.freeze(schemaMap[schemaName][key]);
}
for (const field of schemaMap[schemaName].fields ?? []) {
Object.freeze(field);
}
}
}
export function addMetaFields(schemaMap: SchemaMap): SchemaMap { export function addMetaFields(schemaMap: SchemaMap): SchemaMap {
const metaSchemaMap = getMapFromList(metaSchemas, 'name'); const metaSchemaMap = getMapFromList(metaSchemas, 'name');

View File

@ -42,6 +42,7 @@ export enum FieldTypeEnum {
Select = 'Select', Select = 'Select',
Link = 'Link', Link = 'Link',
Date = 'Date', Date = 'Date',
Datetime = 'Datetime',
Table = 'Table', Table = 'Table',
AutoComplete = 'AutoComplete', AutoComplete = 'AutoComplete',
Check = 'Check', Check = 'Check',