From 2422e33ec09378353e9090657bd7d2e1f146e473 Mon Sep 17 00:00:00 2001 From: Rushabh Mehta Date: Fri, 16 Feb 2018 18:43:46 +0530 Subject: [PATCH] refactor models are now js and better file naming for models --- .gitignore | 2 +- app.js | 2 +- backends/database.js | 28 ++++----- backends/mysql.js | 16 ++--- backends/rest_client.js | 14 ++--- backends/sqlite.js | 20 +++--- client/desk/index.js | 2 +- client/desk/listpage.js | 2 +- client/index.js | 4 +- client/style/style.scss | 7 ++- client/view/controls/base.js | 2 +- client/view/controls/link.js | 3 +- client/view/controls/table.js | 8 ++- client/view/form.js | 11 ++-- client/view/index.js | 19 ++---- client/view/list.js | 2 +- common/index.js | 10 +-- index.js | 61 ++++++++++--------- model/document.js | 25 +++++--- model/index.js | 4 +- model/meta.js | 19 +++--- .../NumberSeries.js} | 5 +- .../NumberSeries/NumberSeriesDocument.js | 15 +++++ .../SingleValue.js} | 4 +- .../SystemSettings.js} | 4 +- .../{todo/todo_client.js => ToDo/ToDoList.js} | 8 +-- .../user_role.json => UserRole/UserRole.js} | 4 +- models/doctype/number_series/number_series.js | 27 -------- models/doctype/role/role.js | 29 +++++---- models/doctype/role/role.json | 15 ----- models/doctype/session/session.js | 36 ++++++----- models/doctype/session/session.json | 21 ------- models/doctype/single_value/single_value.js | 16 ----- .../system_settings/system_settings.js | 16 ----- models/doctype/todo/todo.js | 53 ++++++++++------ models/doctype/todo/todo.json | 34 ----------- models/doctype/user/user.js | 44 ++++++++----- models/doctype/user/user.json | 30 --------- models/doctype/user_role/user_role.js | 16 ----- models/index.js | 10 +++ server/index.js | 30 ++++----- server/init_models.js | 23 ------- session.js | 17 ------ tests/helpers.js | 11 ++-- tests/test_controller.js | 2 +- tests/test_database.js | 2 +- tests/test_document.js | 6 +- tests/test_meta.js | 2 +- tests/test_models.js | 4 +- tests/test_mysql_backend.js | 20 +++--- tests/test_number_series.js | 13 ++-- tests/test_router.js | 8 +-- tests/test_server.js | 2 +- tests/test_single.js | 8 +-- 54 files changed, 320 insertions(+), 476 deletions(-) rename models/doctype/{number_series/number_series.json => NumberSeries/NumberSeries.js} (74%) create mode 100644 models/doctype/NumberSeries/NumberSeriesDocument.js rename models/doctype/{single_value/single_value.json => SingleValue/SingleValue.js} (90%) rename models/doctype/{system_settings/system_settings.json => SystemSettings/SystemSettings.js} (86%) rename models/doctype/{todo/todo_client.js => ToDo/ToDoList.js} (62%) rename models/doctype/{user_role/user_role.json => UserRole/UserRole.js} (82%) delete mode 100644 models/doctype/number_series/number_series.js delete mode 100644 models/doctype/role/role.json delete mode 100644 models/doctype/session/session.json delete mode 100644 models/doctype/single_value/single_value.js delete mode 100644 models/doctype/system_settings/system_settings.js delete mode 100644 models/doctype/todo/todo.json delete mode 100644 models/doctype/user/user.json delete mode 100644 models/doctype/user_role/user_role.js create mode 100644 models/index.js delete mode 100644 server/init_models.js delete mode 100644 session.js diff --git a/.gitignore b/.gitignore index 30197abd..5485f60b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,7 @@ node_modules .DS_Store Thumbs.db -test.db +*test.db *.log .cache diff --git a/app.js b/app.js index 922e05a5..a6c592c3 100644 --- a/app.js +++ b/app.js @@ -2,5 +2,5 @@ const server = require('frappejs/server'); server.start({ backend: 'sqllite', - connection_params: {dbPath: 'test.db'} + connectionParams: {dbPath: 'test.db'} }); \ No newline at end of file diff --git a/backends/database.js b/backends/database.js index 55760b33..2a259ecd 100644 --- a/backends/database.js +++ b/backends/database.js @@ -14,16 +14,14 @@ module.exports = class Database { } async migrate() { - for (let doctype in frappe.modules) { + for (let doctype in frappe.models) { // check if controller module - if (frappe.modules[doctype].Meta) { - let meta = frappe.getMeta(doctype); - if (!meta.isSingle) { - if (await this.tableExists(doctype)) { - await this.alterTable(doctype); - } else { - await this.createTable(doctype); - } + let meta = frappe.getMeta(doctype); + if (!meta.isSingle) { + if (await this.tableExists(doctype)) { + await this.alterTable(doctype); + } else { + await this.createTable(doctype); } } } @@ -53,7 +51,7 @@ module.exports = class Database { } getColumnDefinition(df) { - return `${df.fieldname} ${this.type_map[df.fieldtype]} ${df.required && !df.default ? "not null" : ""} ${df.default ? `default ${df.default}` : ""}` + // return `${df.fieldname} ${this.type_map[df.fieldtype]} ${ ? "PRIMARY KEY" : ""} ${df.required && !df.default ? "NOT NULL" : ""} ${df.default ? `DEFAULT ${df.default}` : ""}` } async alterTable(doctype) { @@ -89,7 +87,7 @@ module.exports = class Database { doc.name = doctype; } else { if (!name) { - throw frappe.errors.ValueError('name is mandatory'); + throw new frappe.errors.ValueError('name is mandatory'); } doc = await this.getOne(doctype, name, fields); } @@ -113,7 +111,7 @@ module.exports = class Database { async getSingle(doctype) { let values = await this.getAll({ - doctype: 'Single Value', + doctype: 'SingleValue', fields: ['fieldname', 'value'], filters: { parent: doctype }, order_by: 'fieldname', @@ -217,7 +215,7 @@ module.exports = class Database { let value = doc[field.fieldname]; if (value) { let singleValue = frappe.newDoc({ - doctype: 'Single Value', + doctype: 'SingleValue', parent: doctype, fieldname: field.fieldname, value: value @@ -228,7 +226,7 @@ module.exports = class Database { } async deleteSingleValues(name) { - // await frappe.db.run('delete from single_value where parent=?', name) + // await frappe.db.run('delete from SingleValue where parent=?', name) } prepareChild(parenttype, parent, child, field, idx) { @@ -275,7 +273,7 @@ module.exports = class Database { // delete children let tableFields = frappe.getMeta(doctype).getTableFields(); for (let field of tableFields) { - await this.deleteChildren(frappe.slug(field.childtype), name); + await this.deleteChildren(field.childtype, name); } } diff --git a/backends/mysql.js b/backends/mysql.js index fdca05f6..9f032e44 100644 --- a/backends/mysql.js +++ b/backends/mysql.js @@ -45,7 +45,7 @@ module.exports = class mysqlDatabase extends Database{ } async runCreateTableQuery(doctype, columns, values){ - const query = `CREATE TABLE IF NOT EXISTS ${frappe.slug(doctype)} ( + const query = `CREATE TABLE IF NOT EXISTS ${doctype} ( ${columns.join(", ")})`; return await this.run(query, values); @@ -62,7 +62,7 @@ module.exports = class mysqlDatabase extends Database{ async runAlterTableQuery(doctype) { - await this.run(`ALTER TABLE ${frappe.slug(doctype)} ADD COLUMN ${this.get_column_definition(df)}`, values); + await this.run(`ALTER TABLE ${doctype} ADD COLUMN ${this.get_column_definition(df)}`, values); } getOne(doctype, name, fields = '*') { @@ -70,7 +70,7 @@ module.exports = class mysqlDatabase extends Database{ fields = this.prepareFields(fields); return new Promise((resolve, reject) => { - this.conn.get(`select ${fields} from ${frappe.slug(doctype)} + this.conn.get(`select ${fields} from ${doctype} where name = ?`, name, (err, row) => { resolve(row || {}); @@ -86,7 +86,7 @@ module.exports = class mysqlDatabase extends Database{ doc.name = frappe.getRandomName(); } - return await this.run(`insert into ${frappe.slug(doctype)} + return await this.run(`insert into ${doctype} (${fields.map(field => field.fieldname).join(", ")}) values (${placeholders})`, this.getFormattedValues(fields, doc)); } @@ -99,19 +99,19 @@ module.exports = class mysqlDatabase extends Database{ // additional name for where clause values.push(doc.name); - return await this.run(`update ${frappe.slug(doctype)} + return await this.run(`update ${doctype} set ${assigns.join(", ")} where name=?`, values); } async runDeleteOtherChildren(field, added) { - await this.run(`delete from ${frappe.slug(field.childtype)} + await this.run(`delete from ${field.childtype} where parent = ? and name not in (${added.slice(1).map(d => '?').join(', ')})`, added); } async deleteOne(doctype, name) { - return await this.run(`delete from ${frappe.slug(doctype)} where name=?`, name); + return await this.run(`delete from ${doctype} where name=?`, name); } async deleteChildren(parenttype, parent) { @@ -127,7 +127,7 @@ module.exports = class mysqlDatabase extends Database{ let conditions = this.getFilterConditions(filters); this.conn.all(`select ${fields.join(", ")} - from ${frappe.slug(doctype)} + from ${doctype} ${conditions.conditions ? "where" : ""} ${conditions.conditions} ${order_by ? ("order by " + order_by) : ""} ${order_by ? (order || "asc") : ""} ${limit ? ("limit " + limit) : ""} ${start ? ("offset " + start) : ""}`, conditions.values, diff --git a/backends/rest_client.js b/backends/rest_client.js index 755cd230..0caf72df 100644 --- a/backends/rest_client.js +++ b/backends/rest_client.js @@ -15,7 +15,7 @@ module.exports = class RESTClient { async insert(doctype, doc) { doc.doctype = doctype; - let url = this.getURL('/api/resource', frappe.slug(doctype)); + let url = this.getURL('/api/resource', doctype); return await this.fetch(url, { method: 'POST', body: JSON.stringify(doc) @@ -23,7 +23,7 @@ module.exports = class RESTClient { } async get(doctype, name) { - let url = this.getURL('/api/resource', frappe.slug(doctype), name); + let url = this.getURL('/api/resource', doctype, name); return await this.fetch(url, { method: 'GET', headers: this.getHeaders() @@ -31,7 +31,7 @@ module.exports = class RESTClient { } async getAll({ doctype, fields, filters, start, limit, sort_by, order }) { - let url = this.getURL('/api/resource', frappe.slug(doctype)); + let url = this.getURL('/api/resource', doctype); url = url + "?" + this.getQueryString({ fields: JSON.stringify(fields), @@ -49,7 +49,7 @@ module.exports = class RESTClient { async update(doctype, doc) { doc.doctype = doctype; - let url = this.getURL('/api/resource', frappe.slug(doctype), doc.name); + let url = this.getURL('/api/resource', doctype, doc.name); return await this.fetch(url, { method: 'PUT', @@ -58,7 +58,7 @@ module.exports = class RESTClient { } async delete(doctype, name) { - let url = this.getURL('/api/resource', frappe.slug(doctype), name); + let url = this.getURL('/api/resource', doctype, name); return await this.fetch(url, { method: 'DELETE', @@ -66,7 +66,7 @@ module.exports = class RESTClient { } async deleteMany(doctype, names) { - let url = this.getURL('/api/resource', frappe.slug(doctype)); + let url = this.getURL('/api/resource', doctype); return await this.fetch(url, { method: 'DELETE', @@ -79,7 +79,7 @@ module.exports = class RESTClient { } async getValue(doctype, name, fieldname) { - let url = this.getURL('/api/resource', frappe.slug(doctype), name, fieldname); + let url = this.getURL('/api/resource', doctype, name, fieldname); return (await this.fetch(url, { method: 'GET', diff --git a/backends/sqlite.js b/backends/sqlite.js index f07c84e6..32a0dcb9 100644 --- a/backends/sqlite.js +++ b/backends/sqlite.js @@ -29,14 +29,14 @@ module.exports = class sqliteDatabase extends Database { } async runCreateTableQuery(doctype, columns, values) { - const query = `CREATE TABLE IF NOT EXISTS ${frappe.slug(doctype)} ( + const query = `CREATE TABLE IF NOT EXISTS ${doctype} ( ${columns.join(", ")})`; return await this.run(query, values); } getColumnDefinition(df) { - return `${df.fieldname} ${this.type_map[df.fieldtype]} ${df.required && !df.default ? "not null" : ""} ${df.default ? `default ${df.default}` : ""}` + return `${df.fieldname} ${this.type_map[df.fieldtype]} ${ df.fieldname==="name" ? "PRIMARY KEY" : ""} ${df.required && !df.default ? "NOT NULL" : ""} ${df.default ? `DEFAULT ${df.default}` : ""}` } async getTableColumns(doctype) { @@ -44,13 +44,13 @@ module.exports = class sqliteDatabase extends Database { } async runAlterTableQuery(doctype, field, values) { - await this.run(`ALTER TABLE ${frappe.slug(doctype)} ADD COLUMN ${this.getColumnDefinition(field)}`, values); + await this.run(`ALTER TABLE ${doctype} ADD COLUMN ${this.getColumnDefinition(field)}`, values); } getOne(doctype, name, fields = '*') { fields = this.prepareFields(fields); return new Promise((resolve, reject) => { - this.conn.get(`select ${fields} from ${frappe.slug(doctype)} + this.conn.get(`select ${fields} from ${doctype} where name = ?`, name, (err, row) => { resolve(row || {}); @@ -66,7 +66,7 @@ module.exports = class sqliteDatabase extends Database { doc.name = frappe.getRandomName(); } - return await this.run(`insert into ${frappe.slug(doctype)} + return await this.run(`insert into ${doctype} (${fields.map(field => field.fieldname).join(", ")}) values (${placeholders})`, this.getFormattedValues(fields, doc)); } @@ -79,21 +79,21 @@ module.exports = class sqliteDatabase extends Database { // additional name for where clause values.push(doc.name); - return await this.run(`update ${frappe.slug(doctype)} + return await this.run(`update ${doctype} set ${assigns.join(", ")} where name=?`, values); } async runDeleteOtherChildren(field, added) { // delete other children // `delete from doctype where parent = ? and name not in (?, ?, ?)}` - await this.run(`delete from ${frappe.slug(field.childtype)} + await this.run(`delete from ${field.childtype} where parent = ? and name not in (${added.slice(1).map(d => '?').join(', ')})`, added); } async deleteOne(doctype, name) { - return await this.run(`delete from ${frappe.slug(doctype)} where name=?`, name); + return await this.run(`delete from ${doctype} where name=?`, name); } async deleteChildren(parenttype, parent) { @@ -101,7 +101,7 @@ module.exports = class sqliteDatabase extends Database { } async deleteSingleValues(name) { - await frappe.db.run('delete from single_value where parent=?', name) + await frappe.db.run('delete from SingleValue where parent=?', name) } getAll({ doctype, fields, filters, start, limit, order_by = 'modified', order = 'desc' } = {}) { @@ -111,7 +111,7 @@ module.exports = class sqliteDatabase extends Database { return new Promise((resolve, reject) => { let conditions = this.getFilterConditions(filters); let query = `select ${fields.join(", ")} - from ${frappe.slug(doctype)} + from ${doctype} ${conditions.conditions ? "where" : ""} ${conditions.conditions} ${order_by ? ("order by " + order_by) : ""} ${order_by ? (order || "asc") : ""} ${limit ? ("limit " + limit) : ""} ${start ? ("offset " + start) : ""}`; diff --git a/client/desk/index.js b/client/desk/index.js index 3bc17800..864817a0 100644 --- a/client/desk/index.js +++ b/client/desk/index.js @@ -4,7 +4,7 @@ const Router = require('frappejs/common/router'); const Page = require('frappejs/client/view/page'); const FormPage = require('frappejs/client/desk/formpage'); const ListPage = require('frappejs/client/desk/listpage'); -const Navbar = require('./navbar'); +// const Navbar = require('./navbar'); const DeskMenu = require('./menu'); const FormModal = require('frappejs/client/desk/formmodal'); diff --git a/client/desk/listpage.js b/client/desk/listpage.js index 308a9893..688bbd37 100644 --- a/client/desk/listpage.js +++ b/client/desk/listpage.js @@ -15,7 +15,7 @@ module.exports = class ListPage extends Page { hasRoute: hasRoute }); - this.list = new (view.getList_class(doctype))({ + this.list = new (view.geListClass(doctype))({ doctype: doctype, parent: this.body, page: this diff --git a/client/index.js b/client/index.js index 98608f85..2306e4e5 100644 --- a/client/index.js +++ b/client/index.js @@ -8,7 +8,9 @@ module.exports = { async start({server, columns = 2}) { window.frappe = frappe; frappe.init(); - common.init_libs(frappe); + frappe.registerLibs(common); + frappe.registerModels(require('frappejs/models')); + frappe.registerModels(require('../models')); frappe.fetch = window.fetch.bind(); frappe.db = await new RESTClient({server: server}); diff --git a/client/style/style.scss b/client/style/style.scss index c71ac402..cd66d916 100644 --- a/client/style/style.scss +++ b/client/style/style.scss @@ -153,10 +153,15 @@ mark { .data-table { .body-scrollable { border-bottom: 0px !important; + + tr:first-child { + .data-table-col { + border-top: 0px !important; + } + } } thead td { - border-bottom: 0px !important; background-color: $gray-200 !important; } diff --git a/client/view/controls/base.js b/client/view/controls/base.js index b8c46c9f..2b0cc63e 100644 --- a/client/view/controls/base.js +++ b/client/view/controls/base.js @@ -140,7 +140,7 @@ class BaseControl { if (this.parentControl) { // its a child this.doc[this.fieldname] = value; - await this.parentControl.doc.set(this.fieldname, this.parentControl.getInputValue()); + await this.parentControl.doc.applyChange(this.fieldname); } else { // parent await this.doc.set(this.fieldname, value); diff --git a/client/view/controls/link.js b/client/view/controls/link.js index 3d2d49d0..cc54e73f 100644 --- a/client/view/controls/link.js +++ b/client/view/controls/link.js @@ -11,7 +11,6 @@ class LinkControl extends BaseControl { setupAwesomplete() { this.awesomplete = new Awesomplete(this.input, { - autoFirst: true, minChars: 0, maxItems: 99, filter: () => true, @@ -23,7 +22,7 @@ class LinkControl extends BaseControl { // action to add new item list.push({ - label:frappe._('+ New {0}', this.target), + label: frappe._('+ New {0}', this.label), value: '__newItem', }); diff --git a/client/view/controls/table.js b/client/view/controls/table.js index a972d8dd..e61684fc 100644 --- a/client/view/controls/table.js +++ b/client/view/controls/table.js @@ -55,7 +55,11 @@ class TableControl extends BaseControl { setInputValue(value) { this.datatable.refresh(this.getTableData(value)); - this.datatable.setDimensions(); + this.refreshToolbar(); + } + + refreshToolbar() { + this.wrapper.querySelector('.table-toolbar').classList.toggle('hide', this.disabled ? true : false); } getTableData(value) { @@ -124,7 +128,7 @@ class TableControl extends BaseControl { field: field, content: field.label, width: 120, - editable: field.disabled ? false : true, + editable: (this.disabled || field.disabled) ? false : true, sortable: false, resizable: true, dropdown: false, diff --git a/client/view/form.js b/client/view/form.js index f9439e20..657d0eb4 100644 --- a/client/view/form.js +++ b/client/view/form.js @@ -71,7 +71,7 @@ module.exports = class BaseForm extends Observable { if (this.meta.settings && this.actions.includes('settings')) { let menu = this.container.getDropdown(frappe._('Menu')); menu.addItem(frappe._('Settings...'), () => { - frappe.desk.showFormModal(frappe.slug(this.meta.settings), this.meta.settings); + frappe.desk.showFormModal(this.meta.settings, this.meta.settings); }); } @@ -99,13 +99,14 @@ module.exports = class BaseForm extends Observable { } setTitle() { - const doctype = this.doc.meta.name; + const doctypeLabel = this.doc.meta.label || this.doc.meta.name; + if (this.doc.meta.isSingle || !this.doc.meta.showTitle) { - this.container.setTitle(doctype); + this.container.setTitle(doctypeLabel); } else if (this.doc._notInserted) { - this.container.setTitle(frappe._('New {0}', doctype)); + this.container.setTitle(frappe._('New {0}', doctypeLabel)); } else { - this.container.setTitle(`${doctype} ${this.doc.name}`); + this.container.setTitle(`${doctypeLabel} ${this.doc.name}`); } } diff --git a/client/view/index.js b/client/view/index.js index e78490d5..32b898ac 100644 --- a/client/view/index.js +++ b/client/view/index.js @@ -1,23 +1,12 @@ const BaseList = require('frappejs/client/view/list'); const BaseForm = require('frappejs/client/view/form'); +const frappe = require('frappejs'); module.exports = { getFormClass(doctype) { - return this.get_view_class(doctype, 'Form', BaseForm); + return (frappe.views['Form'] && frappe.views['Form'][doctype]) || BaseForm; }, - getList_class(doctype) { - return this.get_view_class(doctype, 'List', BaseList); - }, - get_view_class(doctype, class_name, default_class) { - let client_module = this.get_client_module(doctype); - if (client_module && client_module[class_name]) { - return client_module[class_name]; - } else { - return default_class; - } - }, - - get_client_module(doctype) { - return frappe.modules[`${doctype}_client`]; + geListClass(doctype) { + return (frappe.views['List'] && frappe.views['List'][doctype]) || BaseList; } } \ No newline at end of file diff --git a/client/view/list.js b/client/view/list.js index 8b59799f..7782bb53 100644 --- a/client/view/list.js +++ b/client/view/list.js @@ -77,7 +77,7 @@ module.exports = class BaseList { makeToolbar() { this.makeSearch(); this.btnNew = this.page.addButton(frappe._('New'), 'btn-primary', async () => { - await frappe.router.setRoute('new', frappe.slug(this.doctype)); + await frappe.router.setRoute('new', this.doctype); }) this.btnDelete = this.page.addButton(frappe._('Delete'), 'btn-secondary hide', async () => { await frappe.db.deleteMany(this.doctype, this.getCheckedRowNames()); diff --git a/common/index.js b/common/index.js index 8d9a7fc4..6c7436e4 100644 --- a/common/index.js +++ b/common/index.js @@ -1,17 +1,17 @@ const utils = require('../utils'); const number_format = require('../utils/number_format'); const format = require('../utils/format'); -const model = require('../model'); -const _session = require('../session'); const errors = require('./errors'); +const BaseDocument = require('frappejs/model/document'); +const BaseMeta = require('frappejs/model/meta'); module.exports = { - init_libs(frappe) { + initLibs(frappe) { Object.assign(frappe, utils); Object.assign(frappe, number_format); Object.assign(frappe, format); - frappe.model = model; - frappe._session = _session; frappe.errors = errors; + frappe.BaseDocument = BaseDocument; + frappe.BaseMeta = BaseMeta; } } \ No newline at end of file diff --git a/index.js b/index.js index 9eb7b26e..dd2076b6 100644 --- a/index.js +++ b/index.js @@ -1,3 +1,4 @@ + module.exports = { async init() { if (this._initialized) return; @@ -15,13 +16,29 @@ module.exports = { initGlobals() { this.metaCache = {}; - this.modules = {}; + this.models = {}; + this.forms = {}; + this.views = {}; this.docs = {}; this.flags = { cache_docs: false } }, + registerLibs(common) { + // add standard libs and utils to frappe + common.initLibs(this); + }, + + registerModels(models) { + Object.assign(this.models, models); + }, + + registerView(view, doctype, module) { + if (!this.views[view]) this.views[view] = {}; + this.views[view][doctype] = module; + }, + addToCache(doc) { if (!this.flags.cache_docs) return; @@ -42,31 +59,32 @@ module.exports = { getMeta(doctype) { if (!this.metaCache[doctype]) { - this.metaCache[doctype] = new (this.getMetaClass(doctype))(); + let model = this.models[doctype]; + if (!model) { + throw `${doctype} is not a registered doctype`; + } + let metaClass = model.metaClass || this.BaseMeta; + this.metaCache[doctype] = new metaClass(model); } - return this.metaCache[doctype]; - }, - getMetaClass(doctype) { - doctype = this.slug(doctype); - if (this.modules[doctype] && this.modules[doctype].Meta) { - return this.modules[doctype].Meta; - } else { - return this.BaseMeta; - } + return this.metaCache[doctype]; }, async getDoc(doctype, name) { let doc = this.getDocFromCache(doctype, name); if (!doc) { - let controllerClass = this.getControllerClass(doctype); - doc = new controllerClass({doctype:doctype, name: name}); + doc = new (this.getDocumentClass(doctype))({doctype:doctype, name: name}); await doc.load(); this.addToCache(doc); } return doc; }, + getDocumentClass(doctype) { + const meta = this.getMeta(doctype); + return meta.documentClass || this.BaseDocument; + }, + async getSingle(doctype) { return await this.getDoc(doctype, doctype); }, @@ -85,21 +103,11 @@ module.exports = { }, newDoc(data) { - let controllerClass = this.getControllerClass(data.doctype); - let doc = new controllerClass(data); + let doc = new (this.getDocumentClass(data.doctype))(data); doc.setDefaults(); return doc; }, - getControllerClass(doctype) { - doctype = this.slug(doctype); - if (this.modules[doctype] && this.modules[doctype].Document) { - return this.modules[doctype].Document; - } else { - return this.BaseDocument; - } - }, - async getNewDoc(doctype) { let doc = this.newDoc({doctype: doctype}); doc._notInserted = true; @@ -113,10 +121,7 @@ module.exports = { }, login(user='guest', user_key) { - this.session = new this._session.Session(user); - if (user && user_key) { - this.authenticate(user_key); - } + this.session = {user: user}; }, close() { diff --git a/model/document.js b/model/document.js index 92e60501..030d633b 100644 --- a/model/document.js +++ b/model/document.js @@ -1,5 +1,6 @@ const frappe = require('frappejs'); const Observable = require('frappejs/utils/observable'); +const model = require('./index'); module.exports = class BaseDocument extends Observable { constructor(data) { @@ -34,12 +35,16 @@ module.exports = class BaseDocument extends Observable { // set value and trigger change async set(fieldname, value) { this[fieldname] = await this.validateField(fieldname, value); - if (await this.applyFormulae()) { + await this.applyChange(fieldname); + } + + async applyChange(fieldname) { + if (await this.applyFormula()) { // multiple changes await this.trigger('change', { doc: this }); } else { // no other change, trigger control refresh - await this.trigger('change', { doc: this, fieldname: fieldname, value: value }); + await this.trigger('change', { doc: this, fieldname: fieldname }); } } @@ -55,9 +60,9 @@ module.exports = class BaseDocument extends Observable { } if (this.meta.settings) { - const number_series = (await this.getSettings()).number_series; - if(number_series) { - this.name = await frappe.model.getSeriesNext(number_series); + const numberSeries = (await this.getSettings()).numberSeries; + if(numberSeries) { + this.name = await model.getSeriesNext(numberSeries); } } @@ -165,8 +170,8 @@ module.exports = class BaseDocument extends Observable { } } - async applyFormulae() { - if (!this.meta.hasFormulae()) { + async applyFormula() { + if (!this.meta.hasFormula()) { return false; } @@ -180,7 +185,7 @@ module.exports = class BaseDocument extends Observable { // for each row for (let row of this[tablefield.fieldname]) { for (let field of formulaFields) { - row[field.fieldname] = await eval(field.formula); + row[field.fieldname] = await field.formula(row, doc); } } } @@ -188,7 +193,7 @@ module.exports = class BaseDocument extends Observable { // parent for (let field of this.meta.getFormulaFields()) { - doc[field.fieldname] = eval(field.formula); + doc[field.fieldname] = await field.formula(doc); } return true; @@ -200,7 +205,7 @@ module.exports = class BaseDocument extends Observable { this.setStandardValues(); this.setKeywords(); this.setChildIdx(); - await this.applyFormulae(); + await this.applyFormula(); await this.trigger('validate'); } diff --git a/model/index.js b/model/index.js index 223487e6..8bd01a39 100644 --- a/model/index.js +++ b/model/index.js @@ -4,12 +4,12 @@ module.exports = { async getSeriesNext(prefix) { let series; try { - series = await frappe.getDoc('Number Series', prefix); + series = await frappe.getDoc('NumberSeries', prefix); } catch (e) { if (!e.status_code || e.status_code !== 404) { throw e; } - series = frappe.newDoc({doctype: 'Number Series', name: prefix, current: 0}); + series = frappe.newDoc({doctype: 'NumberSeries', name: prefix, current: 0}); await series.insert(); } let next = await series.next() diff --git a/model/meta.js b/model/meta.js index 27881fca..a77dd93d 100644 --- a/model/meta.js +++ b/model/meta.js @@ -1,5 +1,6 @@ const BaseDocument = require('./document'); const frappe = require('frappejs'); +const model = require('./index') module.exports = class BaseMeta extends BaseDocument { constructor(data) { @@ -36,21 +37,21 @@ module.exports = class BaseMeta extends BaseDocument { return this._formulaFields; } - hasFormulae() { - if (this._hasFormulae===undefined) { - this._hasFormulae = false; + hasFormula() { + if (this._hasFormula===undefined) { + this._hasFormula = false; if (this.getFormulaFields().length) { - this._hasFormulae = true; + this._hasFormula = true; } else { for (let tablefield of this.getTableFields()) { if (frappe.getMeta(tablefield.childtype).getFormulaFields().length) { - this._hasFormulae = true; + this._hasFormula = true; break; } } } } - return this._hasFormulae; + return this._hasFormula; } async set(fieldname, value) { @@ -76,7 +77,7 @@ module.exports = class BaseMeta extends BaseDocument { const doctype_fields = this.fields.map((field) => field.fieldname); // standard fields - for (let field of frappe.model.common_fields) { + for (let field of model.common_fields) { if (frappe.db.type_map[field.fieldtype] && !doctype_fields.includes(field.fieldname)) { _add(field); } @@ -84,14 +85,14 @@ module.exports = class BaseMeta extends BaseDocument { if (this.isChild) { // child fields - for (let field of frappe.model.child_fields) { + for (let field of model.child_fields) { if (frappe.db.type_map[field.fieldtype] && !doctype_fields.includes(field.fieldname)) { _add(field); } } } else { // parent fields - for (let field of frappe.model.parent_fields) { + for (let field of model.parent_fields) { if (frappe.db.type_map[field.fieldtype] && !doctype_fields.includes(field.fieldname)) { _add(field); } diff --git a/models/doctype/number_series/number_series.json b/models/doctype/NumberSeries/NumberSeries.js similarity index 74% rename from models/doctype/number_series/number_series.json rename to models/doctype/NumberSeries/NumberSeries.js index 9465d44f..8d326a6a 100644 --- a/models/doctype/number_series/number_series.json +++ b/models/doctype/NumberSeries/NumberSeries.js @@ -1,5 +1,6 @@ -{ - "name": "Number Series", +module.exports = { + "name": "NumberSeries", + "documentClass": require('./NumberSeriesDocument.js'), "doctype": "DocType", "isSingle": 0, "isChild": 0, diff --git a/models/doctype/NumberSeries/NumberSeriesDocument.js b/models/doctype/NumberSeries/NumberSeriesDocument.js new file mode 100644 index 00000000..8c35f35c --- /dev/null +++ b/models/doctype/NumberSeries/NumberSeriesDocument.js @@ -0,0 +1,15 @@ +const BaseDocument = require('frappejs/model/document'); + +module.exports = class NumberSeries extends BaseDocument { + validate() { + if (this.current===null || this.current===undefined) { + this.current = 0; + } + } + async next() { + this.validate(); + this.current++; + await this.update(); + return this.current; + } +} \ No newline at end of file diff --git a/models/doctype/single_value/single_value.json b/models/doctype/SingleValue/SingleValue.js similarity index 90% rename from models/doctype/single_value/single_value.json rename to models/doctype/SingleValue/SingleValue.js index 9ae1e6a0..68b4b35a 100644 --- a/models/doctype/single_value/single_value.json +++ b/models/doctype/SingleValue/SingleValue.js @@ -1,5 +1,5 @@ -{ - "name": "Single Value", +module.exports = { + "name": "SingleValue", "doctype": "DocType", "isSingle": 0, "isChild": 0, diff --git a/models/doctype/system_settings/system_settings.json b/models/doctype/SystemSettings/SystemSettings.js similarity index 86% rename from models/doctype/system_settings/system_settings.json rename to models/doctype/SystemSettings/SystemSettings.js index 440d6ff3..3b07b440 100644 --- a/models/doctype/system_settings/system_settings.json +++ b/models/doctype/SystemSettings/SystemSettings.js @@ -1,5 +1,5 @@ -{ - "name": "System Settings", +module.exports = { + "name": "SystemSettings", "doctype": "DocType", "isSingle": 1, "isChild": 0, diff --git a/models/doctype/todo/todo_client.js b/models/doctype/ToDo/ToDoList.js similarity index 62% rename from models/doctype/todo/todo_client.js rename to models/doctype/ToDo/ToDoList.js index 4f7f205a..9deea299 100644 --- a/models/doctype/todo/todo_client.js +++ b/models/doctype/ToDo/ToDoList.js @@ -1,15 +1,11 @@ const BaseList = require('frappejs/client/view/list'); -class ToDoList extends BaseList { +module.exports = class ToDoList extends BaseList { getFields() { return ['name', 'subject', 'status']; } getRowHTML(data) { let symbol = data.status=="Closed" ? "✔" : ""; - return `${symbol} ${data.subject}`; + return `${symbol} ${data.subject}`; } } - -module.exports = { - List: ToDoList -} \ No newline at end of file diff --git a/models/doctype/user_role/user_role.json b/models/doctype/UserRole/UserRole.js similarity index 82% rename from models/doctype/user_role/user_role.json rename to models/doctype/UserRole/UserRole.js index 9c9420c6..0cbe4f74 100644 --- a/models/doctype/user_role/user_role.json +++ b/models/doctype/UserRole/UserRole.js @@ -1,5 +1,5 @@ -{ - "name": "User Role", +module.exports = { + "name": "UserRole", "doctype": "DocType", "isSingle": 0, "isChild": 1, diff --git a/models/doctype/number_series/number_series.js b/models/doctype/number_series/number_series.js deleted file mode 100644 index 6aa1aea7..00000000 --- a/models/doctype/number_series/number_series.js +++ /dev/null @@ -1,27 +0,0 @@ -const BaseMeta = require('frappejs/model/meta'); -const BaseDocument = require('frappejs/model/document'); - -class NumberSeriesMeta extends BaseMeta { - setupMeta() { - Object.assign(this, require('./number_series.json')); - } -} - -class NumberSeries extends BaseDocument { - validate() { - if (this.current===null || this.current===undefined) { - this.current = 0; - } - } - async next() { - this.validate(); - this.current++; - await this.update(); - return this.current; - } -} - -module.exports = { - Document: NumberSeries, - Meta: NumberSeriesMeta -}; \ No newline at end of file diff --git a/models/doctype/role/role.js b/models/doctype/role/role.js index 85d44904..80037d71 100644 --- a/models/doctype/role/role.js +++ b/models/doctype/role/role.js @@ -1,16 +1,15 @@ -const BaseMeta = require('frappejs/model/meta'); -const BaseDocument = require('frappejs/model/document'); - -class RoleMeta extends BaseMeta { - setupMeta() { - Object.assign(this, require('./role.json')); - } -} - -class Role extends BaseDocument { -} - module.exports = { - Document: Role, - Meta: RoleMeta -}; \ No newline at end of file + "name": "Role", + "doctype": "DocType", + "isSingle": 0, + "isChild": 0, + "keywordFields": [], + "fields": [ + { + "fieldname": "name", + "label": "Name", + "fieldtype": "Data", + "required": 1 + } + ] +} \ No newline at end of file diff --git a/models/doctype/role/role.json b/models/doctype/role/role.json deleted file mode 100644 index 68d31502..00000000 --- a/models/doctype/role/role.json +++ /dev/null @@ -1,15 +0,0 @@ -{ - "name": "Role", - "doctype": "DocType", - "isSingle": 0, - "isChild": 0, - "keywordFields": [], - "fields": [ - { - "fieldname": "name", - "label": "Name", - "fieldtype": "Data", - "required": 1 - } - ] -} \ No newline at end of file diff --git a/models/doctype/session/session.js b/models/doctype/session/session.js index 9e224871..0f59e262 100644 --- a/models/doctype/session/session.js +++ b/models/doctype/session/session.js @@ -1,17 +1,21 @@ -const BaseMeta = require('frappejs/model/meta'); -const BaseDocument = require('frappejs/model/document'); - -class SessionMeta extends BaseMeta { - setupMeta() { - Object.assign(this, require('./session.json')); - } -} - -class Session extends BaseDocument { - -} - module.exports = { - Document: Session, - Meta: SessionMeta -}; \ No newline at end of file + "name": "Session", + "doctype": "DocType", + "isSingle": 0, + "isChild": 0, + "keywordFields": [], + "fields": [ + { + "fieldname": "username", + "label": "Username", + "fieldtype": "Data", + "required": 1 + }, + { + "fieldname": "password", + "label": "Password", + "fieldtype": "Password", + "required": 1 + } + ] +} \ No newline at end of file diff --git a/models/doctype/session/session.json b/models/doctype/session/session.json deleted file mode 100644 index 6e8e5480..00000000 --- a/models/doctype/session/session.json +++ /dev/null @@ -1,21 +0,0 @@ -{ - "name": "Session", - "doctype": "DocType", - "isSingle": 0, - "isChild": 0, - "keywordFields": [], - "fields": [ - { - "fieldname": "username", - "label": "Username", - "fieldtype": "Data", - "required": 1 - }, - { - "fieldname": "password", - "label": "Password", - "fieldtype": "Password", - "required": 1 - } - ] -} \ No newline at end of file diff --git a/models/doctype/single_value/single_value.js b/models/doctype/single_value/single_value.js deleted file mode 100644 index 194b1b3f..00000000 --- a/models/doctype/single_value/single_value.js +++ /dev/null @@ -1,16 +0,0 @@ -const BaseMeta = require('frappejs/model/meta'); -const BaseDocument = require('frappejs/model/document'); - -class SingleValueMeta extends BaseMeta { - setupMeta() { - Object.assign(this, require('./single_value.json')); - } -} - -class SingleValue extends BaseDocument { -} - -module.exports = { - Document: SingleValue, - Meta: SingleValueMeta -}; \ No newline at end of file diff --git a/models/doctype/system_settings/system_settings.js b/models/doctype/system_settings/system_settings.js deleted file mode 100644 index 7013d491..00000000 --- a/models/doctype/system_settings/system_settings.js +++ /dev/null @@ -1,16 +0,0 @@ -const BaseMeta = require('frappejs/model/meta'); -const BaseDocument = require('frappejs/model/document'); - -class SystemSettingsMeta extends BaseMeta { - setupMeta() { - Object.assign(this, require('./system_settings.json')); - } -} - -class SystemSettings extends BaseDocument { -} - -module.exports = { - Document: SystemSettings, - Meta: SystemSettingsMeta -}; \ No newline at end of file diff --git a/models/doctype/todo/todo.js b/models/doctype/todo/todo.js index 4a4f6116..ae68dab5 100644 --- a/models/doctype/todo/todo.js +++ b/models/doctype/todo/todo.js @@ -1,21 +1,34 @@ -const BaseMeta = require('frappejs/model/meta'); -const BaseDocument = require('frappejs/model/document'); - -class ToDoMeta extends BaseMeta { - setupMeta() { - Object.assign(this, require('./todo.json')); - } -} - -class ToDo extends BaseDocument { - validate() { - if (!this.status) { - this.status = 'Open'; - } - } -} - module.exports = { - Document: ToDo, - Meta: ToDoMeta -}; + "autoname": "random", + "name": "ToDo", + "doctype": "DocType", + "isSingle": 0, + "keywordFields": [ + "subject", + "description" + ], + "fields": [ + { + "fieldname": "subject", + "label": "Subject", + "fieldtype": "Data", + "required": 1 + }, + { + "fieldname": "status", + "label": "Status", + "fieldtype": "Select", + "options": [ + "Open", + "Closed" + ], + "default": "Open", + "required": 1 + }, + { + "fieldname": "description", + "label": "Description", + "fieldtype": "Text" + } + ] +} \ No newline at end of file diff --git a/models/doctype/todo/todo.json b/models/doctype/todo/todo.json deleted file mode 100644 index 7ba60ef1..00000000 --- a/models/doctype/todo/todo.json +++ /dev/null @@ -1,34 +0,0 @@ -{ - "autoname": "hash", - "name": "ToDo", - "doctype": "DocType", - "isSingle": 0, - "keywordFields": [ - "subject", - "description" - ], - "fields": [ - { - "fieldname": "subject", - "label": "Subject", - "fieldtype": "Data", - "required": 1 - }, - { - "fieldname": "status", - "label": "Status", - "fieldtype": "Select", - "options": [ - "Open", - "Closed" - ], - "default": "Open", - "required": 1 - }, - { - "fieldname": "description", - "label": "Description", - "fieldtype": "Text" - } - ] -} \ No newline at end of file diff --git a/models/doctype/user/user.js b/models/doctype/user/user.js index 8ce243b1..22ddbec7 100644 --- a/models/doctype/user/user.js +++ b/models/doctype/user/user.js @@ -1,16 +1,30 @@ -const BaseMeta = require('frappejs/model/meta'); -const BaseDocument = require('frappejs/model/document'); - -class UserMeta extends BaseMeta { - setupMeta() { - Object.assign(this, require('./user.json')); - } -} - -class User extends BaseDocument { -} - module.exports = { - Document: User, - Meta: UserMeta -}; \ No newline at end of file + "name": "User", + "doctype": "DocType", + "isSingle": 0, + "isChild": 0, + "keywordFields": [ + "name", + "full_name" + ], + "fields": [ + { + "fieldname": "name", + "label": "Name", + "fieldtype": "Data", + "required": 1 + }, + { + "fieldname": "full_name", + "label": "Full Name", + "fieldtype": "Data", + "required": 1 + }, + { + "fieldname": "roles", + "label": "Roles", + "fieldtype": "Table", + "childtype": "UserRole" + } + ] +} \ No newline at end of file diff --git a/models/doctype/user/user.json b/models/doctype/user/user.json deleted file mode 100644 index f158ed33..00000000 --- a/models/doctype/user/user.json +++ /dev/null @@ -1,30 +0,0 @@ -{ - "name": "User", - "doctype": "DocType", - "isSingle": 0, - "isChild": 0, - "keywordFields": [ - "name", - "full_name" - ], - "fields": [ - { - "fieldname": "name", - "label": "Name", - "fieldtype": "Data", - "required": 1 - }, - { - "fieldname": "full_name", - "label": "Full Name", - "fieldtype": "Data", - "required": 1 - }, - { - "fieldname": "roles", - "label": "Roles", - "fieldtype": "Table", - "childtype": "User Role" - } - ] -} \ No newline at end of file diff --git a/models/doctype/user_role/user_role.js b/models/doctype/user_role/user_role.js deleted file mode 100644 index 69348ca8..00000000 --- a/models/doctype/user_role/user_role.js +++ /dev/null @@ -1,16 +0,0 @@ -const BaseMeta = require('frappejs/model/meta'); -const BaseDocument = require('frappejs/model/document'); - -class UserRoleMeta extends BaseMeta { - setupMeta() { - Object.assign(this, require('./user_role.json')); - } -} - -class UserRole extends BaseDocument { -} - -module.exports = { - Document: UserRole, - Meta: UserRoleMeta -}; \ No newline at end of file diff --git a/models/index.js b/models/index.js new file mode 100644 index 00000000..0d30f253 --- /dev/null +++ b/models/index.js @@ -0,0 +1,10 @@ +module.exports = { + NumberSeries: require('./doctype/NumberSeries/NumberSeries.js'), + Role: require('./doctype/Role/Role.js'), + Session: require('./doctype/Session/Session.js'), + SingleValue: require('./doctype/SingleValue/SingleValue.js'), + SystemSettings: require('./doctype/SystemSettings/SystemSettings.js'), + ToDo: require('./doctype/ToDo/ToDo.js'), + User: require('./doctype/User/User.js'), + UserRole: require('./doctype/UserRole/UserRole.js') +} diff --git a/server/index.js b/server/index.js index 7b0bb805..1840cc7a 100644 --- a/server/index.js +++ b/server/index.js @@ -1,24 +1,25 @@ const backends = {}; backends.sqlite = require('frappejs/backends/sqlite'); -backends.mysql = require('frappejs/backends/mysql'); +//backends.mysql = require('frappejs/backends/mysql'); const express = require('express'); const app = express(); const frappe = require('frappejs'); -const rest_api = require('./rest_api') -const init_models = require('frappejs/server/init_models'); +const rest_api = require('./rest_api'); +const frappeModels = require('frappejs/models'); const common = require('frappejs/common'); const bodyParser = require('body-parser'); -const path = require('path'); module.exports = { - async start({backend, connection_params, models_path}) { + async start({backend, connectionParams, models}) { await this.init(); - this.init_models(models_path); + if (models) { + frappe.registerModels(models); + } // database - await this.init_db({backend:backend, connection_params:connection_params}); + await this.initDb({backend:backend, connectionParams:connectionParams}); // app app.use(bodyParser.json()); @@ -33,22 +34,15 @@ module.exports = { frappe.server = app.listen(frappe.config.port); }, - init_models(models_path) { - // import frappe modules - init_models(path.join(path.dirname(require.resolve('frappejs')), 'models')); - - // import modules from the app - init_models(models_path); - }, - async init() { await frappe.init(); - common.init_libs(frappe); + frappe.registerModels(frappeModels); + frappe.registerLibs(common); await frappe.login(); }, - async init_db({backend, connection_params}) { - frappe.db = await new backends[backend](connection_params); + async initDb({backend, connectionParams}) { + frappe.db = await new backends[backend](connectionParams); await frappe.db.connect(); await frappe.db.migrate(); }, diff --git a/server/init_models.js b/server/init_models.js deleted file mode 100644 index 519f1b0f..00000000 --- a/server/init_models.js +++ /dev/null @@ -1,23 +0,0 @@ -const frappe = require('frappejs'); -const walk = require('walk'); -const path = require('path'); - -module.exports = function(models_path) { - if (!models_path) { - return; - } - - walk.walkSync(models_path, { - listeners: { - file: (basepath, file_data, next) => { - const doctype = path.basename(path.dirname(basepath)); - const name = path.basename(basepath); - const file_path = path.resolve(basepath, file_data.name); - if (doctype==='doctype' && file_data.name.endsWith('.js')) { - frappe.modules[file_data.name.slice(0, -3)] = require(file_path); - } - next(); - } - } - }); -} diff --git a/session.js b/session.js deleted file mode 100644 index 7e70bee0..00000000 --- a/session.js +++ /dev/null @@ -1,17 +0,0 @@ -const frappe = require('frappejs'); - -class Session { - constructor(user, user_key) { - this.user = user || 'guest'; - if (this.user !== 'guest') { - this.login(user_key); - } - } - - login(user_key) { - // could be password, sessionid, otp - } - -} - -module.exports = { Session: Session }; \ No newline at end of file diff --git a/tests/helpers.js b/tests/helpers.js index e793ec0b..05f21777 100644 --- a/tests/helpers.js +++ b/tests/helpers.js @@ -1,12 +1,15 @@ const server = require('frappejs/server'); +const frappe = require('frappejs'); module.exports = { - async init_sqlite() { + async initSqlite({dbPath = '_test.db', models} = {}) { server.init(); - server.init_models(); - server.init_db({ + if (models) { + frappe.registerModels(models); + } + await server.initDb({ backend: 'sqlite', - connection_params: {dbPath: 'test.db'} + connectionParams: {dbPath: dbPath}, }); } } \ No newline at end of file diff --git a/tests/test_controller.js b/tests/test_controller.js index 1b3455e5..ea6d0e7e 100644 --- a/tests/test_controller.js +++ b/tests/test_controller.js @@ -4,7 +4,7 @@ const helpers = require('./helpers'); describe('Controller', () => { before(async function() { - await helpers.init_sqlite(); + await helpers.initSqlite(); }); it('should call controller method', async () => { diff --git a/tests/test_database.js b/tests/test_database.js index f2e8f056..251e7c18 100644 --- a/tests/test_database.js +++ b/tests/test_database.js @@ -4,7 +4,7 @@ const helpers = require('./helpers'); describe('Database', () => { before(async function() { - await helpers.init_sqlite(); + await helpers.initSqlite(); }); it('should insert and get values', async () => { diff --git a/tests/test_document.js b/tests/test_document.js index 27e4e529..869112c1 100644 --- a/tests/test_document.js +++ b/tests/test_document.js @@ -4,7 +4,7 @@ const helpers = require('./helpers'); describe('Document', () => { before(async function() { - await helpers.init_sqlite(); + await helpers.initSqlite(); }); it('should insert a doc', async () => { @@ -68,8 +68,8 @@ describe('Document', () => { }); it('should add, fetch and delete documents with children', async() => { - await frappe.newDoc({doctype: 'Role', name: 'Test Role'}).insert(); - await frappe.newDoc({doctype: 'Role', name: 'Test Role 1'}).insert(); + await frappe.insert({doctype: 'Role', name: 'Test Role'}); + await frappe.insert({doctype: 'Role', name: 'Test Role 1'}); let user = frappe.newDoc({ doctype: 'User', diff --git a/tests/test_meta.js b/tests/test_meta.js index 161e6081..e725ae98 100644 --- a/tests/test_meta.js +++ b/tests/test_meta.js @@ -4,7 +4,7 @@ const helpers = require('./helpers'); describe('Meta', () => { before(async function() { - await helpers.init_sqlite(); + await helpers.initSqlite(); }); it('should get init from json file', () => { diff --git a/tests/test_models.js b/tests/test_models.js index 99f33472..6d910056 100644 --- a/tests/test_models.js +++ b/tests/test_models.js @@ -4,11 +4,11 @@ const helpers = require('./helpers'); describe('Models', () => { before(async function() { - await helpers.init_sqlite(); + await helpers.initSqlite(); }); it('should get todo json', () => { - let todo = frappe.getMeta('todo'); + let todo = frappe.getMeta('ToDo'); assert.equal(todo.isSingle, 0); }); }); \ No newline at end of file diff --git a/tests/test_mysql_backend.js b/tests/test_mysql_backend.js index 63d001f9..ce62b131 100644 --- a/tests/test_mysql_backend.js +++ b/tests/test_mysql_backend.js @@ -1,10 +1,10 @@ -const server = require('frappejs/server'); -server.start({ - backend: 'mysql', - connection_params: { - host : "test_frappejs", - username : "test_frappejs", - password : "test_frappejs", - db_name : "test_frappejs" - } -}); +// const server = require('frappejs/server'); +// server.start({ +// backend: 'mysql', +// connectionParams: { +// host : "test_frappejs", +// username : "test_frappejs", +// password : "test_frappejs", +// db_name : "test_frappejs" +// } +// }); diff --git a/tests/test_number_series.js b/tests/test_number_series.js index 8eb8ace2..c032d96d 100644 --- a/tests/test_number_series.js +++ b/tests/test_number_series.js @@ -1,16 +1,17 @@ const assert = require('assert'); const frappe = require('frappejs'); const helpers = require('./helpers'); +const model = require('frappejs/model') -describe('Number Series', () => { +describe('NumberSeries', () => { before(async function() { - await helpers.init_sqlite(); + await helpers.initSqlite(); }); it('should start a series and get next value', async () => { - frappe.db.delete('Number Series', 'test-series-') - assert.equal(await frappe.model.getSeriesNext('test-series-'), 'test-series-1'); - assert.equal(await frappe.model.getSeriesNext('test-series-'), 'test-series-2'); - assert.equal(await frappe.model.getSeriesNext('test-series-'), 'test-series-3'); + frappe.db.delete('NumberSeries', 'test-series-') + assert.equal(await model.getSeriesNext('test-series-'), 'test-series-1'); + assert.equal(await model.getSeriesNext('test-series-'), 'test-series-2'); + assert.equal(await model.getSeriesNext('test-series-'), 'test-series-3'); }); }); \ No newline at end of file diff --git a/tests/test_router.js b/tests/test_router.js index a893c867..d333892f 100644 --- a/tests/test_router.js +++ b/tests/test_router.js @@ -17,12 +17,12 @@ describe('Router', () => { let router = new Router(); router.add('/edit/:doctype', 'catch'); router.add('/edit/:doctype/:name', 'all'); - router.add('/edit/todo/:name', 'todo'); - router.add('/edit/todo/mytest', 'static'); + router.add('/edit/ToDo/:name', 'ToDo'); + router.add('/edit/ToDo/mytest', 'static'); - assert.equal(router.match('/edit/todo/test').handler, 'todo'); + assert.equal(router.match('/edit/ToDo/test').handler, 'ToDo'); assert.equal(router.match('/edit/user/test').handler, 'all'); - assert.equal(router.match('/edit/todo/mytest').handler, 'static'); + assert.equal(router.match('/edit/ToDo/mytest').handler, 'static'); assert.equal(router.match('/edit/user').handler, 'catch'); }); diff --git a/tests/test_server.js b/tests/test_server.js index 86fea74d..ad6261a0 100644 --- a/tests/test_server.js +++ b/tests/test_server.js @@ -3,6 +3,6 @@ const server = require('frappejs/server'); if (require.main === module) { server.start({ backend: 'sqlite', - connection_params: {dbPath: 'test.db'} + connectionParams: {dbPath: 'test.db'} }); } \ No newline at end of file diff --git a/tests/test_single.js b/tests/test_single.js index 19a8d01a..0f1f162b 100644 --- a/tests/test_single.js +++ b/tests/test_single.js @@ -4,21 +4,21 @@ const helpers = require('./helpers'); describe('Single Documents', () => { before(async function() { - await helpers.init_sqlite(); + await helpers.initSqlite(); }); it('should set a single value', async () => { - let systemSettings = await frappe.getSingle('System Settings'); + let systemSettings = await frappe.getSingle('SystemSettings'); systemSettings.dateFormat = 'dd/mm/yyyy'; await systemSettings.update(); - systemSettings = await frappe.getSingle('System Settings'); + systemSettings = await frappe.getSingle('SystemSettings'); assert.equal(systemSettings.dateFormat, 'dd/mm/yyyy'); systemSettings.dateFormat = 'mm/dd/yyyy'; await systemSettings.update(); - systemSettings = await frappe.getSingle('System Settings'); + systemSettings = await frappe.getSingle('SystemSettings'); assert.equal(systemSettings.dateFormat, 'mm/dd/yyyy'); }); }); \ No newline at end of file