2
0
mirror of https://github.com/frappe/books.git synced 2024-11-10 07:40:55 +00:00
books/frappe/index.js
2018-01-09 19:10:56 +05:30

70 lines
1.4 KiB
JavaScript

module.exports = {
async init() {
if (this._initialized) return;
this.init_config();
this.init_errors();
this.init_globals();
this._initialized = true;
},
init_config() {
this.config = {
backend: 'sqlite',
port: 8000
};
},
init_errors() {
this.ValueError = class extends Error { };
},
init_globals() {
this.meta_cache = {};
},
get_meta(doctype) {
if (!this.meta_cache[doctype]) {
this.meta_cache[doctype] = new (this.models.get_meta_class(doctype))(this.models.get('DocType', doctype));
}
return this.meta_cache[doctype];
},
init_controller(doctype, module) {
doctype = this.slug(doctype);
this.models.controllers[doctype] = module[doctype];
this.models.meta_classes[doctype] = module[doctype + '_meta'];
},
async get_doc(data, name) {
if (typeof data==='string' && typeof name==='string') {
let controller_class = this.models.get_controller(data);
var doc = new controller_class({doctype:data, name: name});
await doc.load();
} else {
let controller_class = this.models.get_controller(data.doctype);
var doc = new controller_class(data);
}
return doc;
},
async insert(data) {
const doc = await this.get_doc(data);
return await doc.insert();
},
login(user='guest', user_key) {
this.session = new this._session.Session(user);
if (user && user_key) {
this.authenticate(user_key);
}
},
close() {
this.db.close();
if (this.server) {
this.server.close();
}
}
};