2
0
mirror of https://github.com/frappe/books.git synced 2024-11-10 07:40:55 +00:00
books/server/models.js
2018-01-23 13:30:29 +05:30

42 lines
1.5 KiB
JavaScript

const frappe = require('frappejs');
const walk = require('walk');
const path = require('path');
module.exports = {
init_models(models_path) {
const setup_model = (doctype, name, file_path) => {
// add to frappe.models.data
if (!frappe.models[doctype]) {
frappe.models[doctype] = {};
}
frappe.models.data[doctype][name] = require(file_path);
}
const setup_controller = (doctype, file_path) => {
let _module = require(file_path);
frappe.models.controllers[doctype] = _module[doctype];
if (_module[doctype + '_meta']) {
frappe.models.meta_classes[doctype] = _module[doctype + '_meta'];
}
}
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 (file_data.name.endsWith('.json')) {
setup_model(doctype, name, file_path)
}
if (doctype==='doctype' && file_data.name.endsWith('.js')) {
setup_controller(path.basename(basepath), file_path);
}
next();
}
}
});
}
}