2
0
mirror of https://github.com/frappe/books.git synced 2024-11-10 07:40:55 +00:00
books/index.js

111 lines
2.7 KiB
JavaScript
Raw Normal View History

2018-01-12 12:25:07 +00:00
module.exports = {
async init() {
if (this._initialized) return;
this.init_config();
this.init_globals();
this._initialized = true;
},
init_config() {
this.config = {
backend: 'sqlite',
port: 8000
};
},
init_globals() {
this.meta_cache = {};
2018-01-24 11:52:05 +00:00
this.modules = {};
this.docs = {};
this.flags = {
cache_docs: false
}
},
add_to_cache(doc) {
if (!this.flags.cache_docs) return;
// add to `docs` cache
if (doc.doctype && doc.name) {
if (!this.docs[doc.doctype]) {
this.docs[doc.doctype] = {};
}
this.docs[doc.doctype][doc.name] = doc;
}
},
get_doc_from_cache(doctype, name) {
if (this.docs[doctype] && this.docs[doctype][name]) {
return this.docs[doctype][name];
}
2018-01-12 12:25:07 +00:00
},
get_meta(doctype) {
if (!this.meta_cache[doctype]) {
2018-01-24 11:52:05 +00:00
this.meta_cache[doctype] = new (this.get_meta_class(doctype))();
2018-01-12 12:25:07 +00:00
}
return this.meta_cache[doctype];
},
2018-01-24 11:52:05 +00:00
get_meta_class(doctype) {
2018-01-12 12:25:07 +00:00
doctype = this.slug(doctype);
2018-01-24 11:52:05 +00:00
if (this.modules[doctype] && this.modules[doctype].Meta) {
return this.modules[doctype].Meta;
} else {
return this.BaseMeta;
}
2018-01-12 12:25:07 +00:00
},
2018-02-01 11:07:36 +00:00
async get_doc(doctype, name) {
let doc = this.get_doc_from_cache(doctype, name);
if (!doc) {
let controller_class = this.get_controller_class(doctype);
doc = new controller_class({doctype:doctype, name: name});
await doc.load();
this.add_to_cache(doc);
2018-01-12 12:25:07 +00:00
}
return doc;
},
2018-02-01 11:07:36 +00:00
new_doc(data) {
let controller_class = this.get_controller_class(data.doctype);
return new controller_class(data);
},
2018-01-24 11:52:05 +00:00
get_controller_class(doctype) {
doctype = this.slug(doctype);
if (this.modules[doctype] && this.modules[doctype].Document) {
return this.modules[doctype].Document;
} else {
return this.BaseDocument;
}
},
async get_new_doc(doctype) {
2018-02-01 11:07:36 +00:00
let doc = frappe.new_doc({doctype: doctype});
doc.set_name();
doc.__not_inserted = true;
this.add_to_cache(doc);
return doc;
},
2018-01-12 12:25:07 +00:00
async insert(data) {
2018-02-01 11:07:36 +00:00
return await (this.new_doc(data)).insert();
2018-01-12 12:25:07 +00:00
},
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();
}
}
};