diff --git a/frappe/backends/helpers.ts b/frappe/backends/helpers.ts new file mode 100644 index 00000000..1d2d38a4 --- /dev/null +++ b/frappe/backends/helpers.ts @@ -0,0 +1,24 @@ +export const sqliteTypeMap = { + AutoComplete: 'text', + Currency: 'text', + Int: 'integer', + Float: 'float', + Percent: 'float', + Check: 'integer', + Code: 'text', + Date: 'text', + Datetime: 'text', + Time: 'text', + Text: 'text', + Data: 'text', + Link: 'text', + DynamicLink: 'text', + Password: 'text', + Select: 'text', + File: 'text', + Attach: 'text', + AttachImage: 'text', + Color: 'text', +}; + +export const validTypes = Object.keys(sqliteTypeMap); diff --git a/frappe/backends/sqlite.js b/frappe/backends/sqlite.js index 0d76cf5b..f9f8a58a 100644 --- a/frappe/backends/sqlite.js +++ b/frappe/backends/sqlite.js @@ -1,5 +1,6 @@ import frappe from 'frappe'; import Database from './database'; +import { sqliteTypeMap } from './helpers'; export default class SqliteDatabase extends Database { constructor({ dbPath }) { @@ -67,36 +68,7 @@ export default class SqliteDatabase extends Database { } initTypeMap() { - // prettier-ignore - this.typeMap = { - 'AutoComplete': 'text', - 'Currency': 'text', - 'Int': 'integer', - 'Float': 'float', - 'Percent': 'float', - 'Check': 'integer', - 'Small Text': 'text', - 'Long Text': 'text', - 'Code': 'text', - 'Text Editor': 'text', - 'Date': 'text', - 'Datetime': 'text', - 'Time': 'text', - 'Text': 'text', - 'Data': 'text', - 'Link': 'text', - 'DynamicLink': 'text', - 'Password': 'text', - 'Select': 'text', - 'Read Only': 'text', - 'File': 'text', - 'Attach': 'text', - 'AttachImage': 'text', - 'Signature': 'text', - 'Color': 'text', - 'Barcode': 'text', - 'Geolocation': 'text' - }; + this.typeMap = sqliteTypeMap; } getError(err) { diff --git a/frappe/common/index.js b/frappe/common/index.js deleted file mode 100644 index 269e7ffc..00000000 --- a/frappe/common/index.js +++ /dev/null @@ -1,7 +0,0 @@ -export default async function initLibs(frappe) { - const BaseMeta = await import('frappe/model/meta'); - const BaseDocument = await import('frappe/model/document'); - - frappe.BaseDocument = BaseDocument.default; - frappe.BaseMeta = BaseMeta.default; -} diff --git a/frappe/index.js b/frappe/index.js index 3db2be5c..5976dbb2 100644 --- a/frappe/index.js +++ b/frappe/index.js @@ -1,4 +1,3 @@ -import initLibs from 'frappe/common'; import { getMoneyMaker } from 'pesa'; import { markRaw } from 'vue'; import * as errors from './common/errors'; @@ -23,7 +22,10 @@ class Frappe { async initializeAndRegister(customModels = {}, force = false) { this.init(force); - await initLibs(this); + + this.Meta = (await import('frappe/model/meta')).default; + this.Document = (await import('frappe/model/document')).default; + const coreModels = await import('frappe/models'); this.registerModels(coreModels.default); this.registerModels(customModels); @@ -233,7 +235,7 @@ class Frappe { throw new Error(`${doctype} is not a registered doctype`); } - let metaClass = model.metaClass || this.BaseMeta; + let metaClass = model.metaClass || this.Meta; this.metaCache[doctype] = new metaClass(model); } @@ -257,7 +259,7 @@ class Frappe { getDocumentClass(doctype) { const meta = this.getMeta(doctype); - return meta.documentClass || this.BaseDocument; + return meta.documentClass || this.Document; } async getSingle(doctype) { @@ -292,7 +294,7 @@ class Frappe { } async newCustomDoc(fields) { - let doc = new this.BaseDocument({ isCustom: 1, fields }); + let doc = new this.Document({ isCustom: 1, fields }); doc._notInserted = true; doc.name = this.getRandomString(); this.addToCache(doc); @@ -300,7 +302,7 @@ class Frappe { } createMeta(fields) { - let meta = new this.BaseMeta({ isCustom: 1, fields }); + let meta = new this.Meta({ isCustom: 1, fields }); return meta; } diff --git a/frappe/model/meta.js b/frappe/model/meta.js index 5b3dc5c4..f3f0611b 100644 --- a/frappe/model/meta.js +++ b/frappe/model/meta.js @@ -1,9 +1,12 @@ import frappe from 'frappe'; +import { validTypes } from 'frappe/backends/helpers'; +import { ValueError } from 'frappe/common/errors'; import { indicators as indicatorColor } from '../../src/colors'; +import { t } from '../utils/translation'; import Document from './document'; import model from './index'; -export default class BaseMeta extends Document { +export default class Meta extends Document { constructor(data) { if (data.basedOn) { let config = frappe.models[data.basedOn]; @@ -33,7 +36,7 @@ export default class BaseMeta extends Document { if (!this.fields.find((df) => df.fieldname === 'name') && !this.isSingle) { this.fields = [ { - label: frappe.t`ID`, + label: t`ID`, fieldname: 'name', fieldtype: 'Data', required: 1, @@ -149,12 +152,12 @@ export default class BaseMeta extends Document { // fields validation this.fields.forEach((df, i) => { if (!df.fieldname) { - throw new frappe.errors.ValidationError( + throw new ValidationError( `DocType ${this.name}: "fieldname" is required for field at index ${i}` ); } if (!df.fieldtype) { - throw new frappe.errors.ValidationError( + throw new ValidationError( `DocType ${this.name}: "fieldtype" is required for field "${df.fieldname}"` ); } @@ -165,7 +168,7 @@ export default class BaseMeta extends Document { // standard fields for (let field of model.commonFields) { if ( - frappe.db.typeMap[field.fieldtype] && + validTypes.includes(field.fieldtype) && !doctypeFields.includes(field.fieldname) ) { _add(field); @@ -176,7 +179,7 @@ export default class BaseMeta extends Document { _add({ fieldtype: 'Check', fieldname: 'submitted', - label: frappe.t`Submitted`, + label: t`Submitted`, }); } @@ -184,7 +187,7 @@ export default class BaseMeta extends Document { // child fields for (let field of model.childFields) { if ( - frappe.db.typeMap[field.fieldtype] && + validTypes.includes(field.fieldtype) && !doctypeFields.includes(field.fieldname) ) { _add(field); @@ -194,7 +197,7 @@ export default class BaseMeta extends Document { // parent fields for (let field of model.parentFields) { if ( - frappe.db.typeMap[field.fieldtype] && + validTypes.includes(field.fieldtype) && !doctypeFields.includes(field.fieldname) ) { _add(field); @@ -206,7 +209,7 @@ export default class BaseMeta extends Document { // tree fields for (let field of model.treeFields) { if ( - frappe.db.typeMap[field.fieldtype] && + validTypes.includes(field.fieldtype) && !doctypeFields.includes(field.fieldname) ) { _add(field); @@ -216,7 +219,7 @@ export default class BaseMeta extends Document { // doctype fields for (let field of this.fields) { - let include = frappe.db.typeMap[field.fieldtype]; + const include = validTypes.includes(field.fieldtype); if (include) { _add(field); @@ -278,7 +281,7 @@ export default class BaseMeta extends Document { } if (!validValues.includes(value)) { - throw new frappe.errors.ValueError( + throw new ValueError( // prettier-ignore `DocType ${this.name}: Invalid value "${value}" for "${field.label}". Must be one of ${options.join(', ')}` ); diff --git a/frappe/models/doctype/NumberSeries/NumberSeriesDocument.js b/frappe/models/doctype/NumberSeries/NumberSeriesDocument.js index 7b83ab6c..09c6f73c 100644 --- a/frappe/models/doctype/NumberSeries/NumberSeriesDocument.js +++ b/frappe/models/doctype/NumberSeries/NumberSeriesDocument.js @@ -1,8 +1,8 @@ import { getPaddedName } from '@/utils'; import frappe from 'frappe'; -import BaseDocument from 'frappe/model/document'; +import Document from 'frappe/model/document'; -export default class NumberSeries extends BaseDocument { +export default class NumberSeries extends Document { validate() { if (!this.current) { this.current = this.start; diff --git a/frappe/utils/noop.js b/frappe/utils/noop.js deleted file mode 100644 index bd375ffa..00000000 --- a/frappe/utils/noop.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = function () { return function () {}; }; \ No newline at end of file diff --git a/models/doctype/Account/AccountDocument.js b/models/doctype/Account/AccountDocument.js index b16f1463..f2fa2abb 100644 --- a/models/doctype/Account/AccountDocument.js +++ b/models/doctype/Account/AccountDocument.js @@ -1,7 +1,7 @@ import frappe from 'frappe'; -import BaseDocument from 'frappe/model/document'; +import Document from 'frappe/model/document'; -export default class Account extends BaseDocument { +export default class Account extends Document { async validate() { if (!this.accountType && this.parentAccount) { this.accountType = await frappe.db.getValue( diff --git a/models/doctype/Event/EventDocument.js b/models/doctype/Event/EventDocument.js index 077fbad4..c6c06908 100644 --- a/models/doctype/Event/EventDocument.js +++ b/models/doctype/Event/EventDocument.js @@ -1,6 +1,6 @@ -import BaseDocument from 'frappe/model/document'; +import Document from 'frappe/model/document'; -export default class Event extends BaseDocument { +export default class Event extends Document { alertEvent() { alert(this.title); } diff --git a/models/doctype/GSTR3B/GSTR3BDocument.js b/models/doctype/GSTR3B/GSTR3BDocument.js index 31721db5..88cfec2c 100644 --- a/models/doctype/GSTR3B/GSTR3BDocument.js +++ b/models/doctype/GSTR3B/GSTR3BDocument.js @@ -1,8 +1,8 @@ import frappe from 'frappe'; -import BaseDocument from 'frappe/model/document'; +import Document from 'frappe/model/document'; import format from './GSTR3BFormat'; -export default class GSTR3B extends BaseDocument { +export default class GSTR3B extends Document { async getData() { const monthIndex = [ 'January', diff --git a/models/doctype/JournalEntry/JournalEntryServer.js b/models/doctype/JournalEntry/JournalEntryServer.js index c081e720..f10f6ce3 100644 --- a/models/doctype/JournalEntry/JournalEntryServer.js +++ b/models/doctype/JournalEntry/JournalEntryServer.js @@ -1,7 +1,7 @@ -import BaseDocument from 'frappe/model/document'; +import Document from 'frappe/model/document'; import LedgerPosting from '../../../accounting/ledgerPosting'; -export default class JournalEntryServer extends BaseDocument { +export default class JournalEntryServer extends Document { getPosting() { let entries = new LedgerPosting({ reference: this }); diff --git a/models/doctype/Party/PartyServer.js b/models/doctype/Party/PartyServer.js index 174811c5..6145ad88 100644 --- a/models/doctype/Party/PartyServer.js +++ b/models/doctype/Party/PartyServer.js @@ -1,7 +1,7 @@ import frappe from 'frappe'; -import BaseDocument from 'frappe/model/document'; +import Document from 'frappe/model/document'; -export default class PartyServer extends BaseDocument { +export default class PartyServer extends Document { beforeInsert() { if (this.customer && this.supplier) { frappe.call({ diff --git a/models/doctype/Payment/PaymentServer.js b/models/doctype/Payment/PaymentServer.js index 64be0a5c..4f4e6f19 100644 --- a/models/doctype/Payment/PaymentServer.js +++ b/models/doctype/Payment/PaymentServer.js @@ -1,8 +1,8 @@ import frappe from 'frappe'; -import BaseDocument from 'frappe/model/document'; +import Document from 'frappe/model/document'; import LedgerPosting from '../../../accounting/ledgerPosting'; -export default class PaymentServer extends BaseDocument { +export default class PaymentServer extends Document { async change({ changed }) { switch (changed) { case 'for': { diff --git a/models/doctype/Transaction/TransactionDocument.js b/models/doctype/Transaction/TransactionDocument.js index 6564e5c8..49faf74b 100644 --- a/models/doctype/Transaction/TransactionDocument.js +++ b/models/doctype/Transaction/TransactionDocument.js @@ -1,8 +1,8 @@ import frappe from 'frappe'; -import BaseDocument from 'frappe/model/document'; +import Document from 'frappe/model/document'; import { getExchangeRate } from '../../../accounting/exchangeRate'; -export default class TransactionDocument extends BaseDocument { +export default class TransactionDocument extends Document { async getExchangeRate() { if (!this.currency) return 1.0; diff --git a/src/initialization.js b/src/initialization.js index c536ac99..c49a2255 100644 --- a/src/initialization.js +++ b/src/initialization.js @@ -108,7 +108,7 @@ export async function connectToLocalDatabase(filePath) { export async function purgeCache(purgeAll = false) { const filterFunction = purgeAll ? () => true - : (d) => frappe.docs[d][d] instanceof frappe.BaseMeta; + : (d) => frappe.docs[d][d] instanceof frappe.Meta; Object.keys(frappe.docs) .filter(filterFunction) diff --git a/src/utils.js b/src/utils.js index 7ed1f4e6..677639c9 100644 --- a/src/utils.js +++ b/src/utils.js @@ -484,7 +484,7 @@ export function formatXLabels(label) { export function stringifyCircular( obj, ignoreCircular = false, - convertBaseDocument = false + convertDocument = false ) { const cacheKey = []; const cacheValue = []; @@ -504,7 +504,7 @@ export function stringifyCircular( cacheKey.push(key); cacheValue.push(value); - if (convertBaseDocument && value instanceof frappe.BaseDocument) { + if (convertDocument && value instanceof frappe.Document) { return value.getValidDict(); }