2
0
mirror of https://github.com/frappe/books.git synced 2024-12-23 11:29:03 +00:00
books/index.js
2018-02-19 22:11:10 +05:30

139 lines
3.4 KiB
JavaScript

module.exports = {
async init() {
if (this._initialized) return;
this.initConfig();
this.initGlobals();
this._initialized = true;
},
initConfig() {
this.config = {
backend: 'sqlite',
port: 8000
};
},
initGlobals() {
this.metaCache = {};
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;
// 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;
}
},
getDocFromCache(doctype, name) {
if (this.docs[doctype] && this.docs[doctype][name]) {
return this.docs[doctype][name];
}
},
getMeta(doctype) {
if (!this.metaCache[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];
},
async getDoc(doctype, name) {
let doc = this.getDocFromCache(doctype, name);
if (!doc) {
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);
},
async getDuplicate(doc) {
const newDoc = await this.getNewDoc(doc.doctype);
for (let field of this.getMeta(doc.doctype).getValidFields()) {
if (field.fieldname === 'name') continue;
if (field.fieldtype === 'Table') {
newDoc[field.fieldname] = (doc[field.fieldname] || []).map(d => {
let newd = Object.assign({}, d);
newd.name = '';
return newd;
});
} else {
newDoc[field.fieldname] = doc[field.fieldname];
}
}
return newDoc;
},
newDoc(data) {
let doc = new (this.getDocumentClass(data.doctype))(data);
doc.setDefaults();
return doc;
},
async getNewDoc(doctype) {
let doc = this.newDoc({doctype: doctype});
doc._notInserted = true;
doc.name = this.getRandomName();
this.addToCache(doc);
return doc;
},
async insert(data) {
return await (this.newDoc(data)).insert();
},
login(user='guest', user_key) {
this.session = {user: user};
},
close() {
this.db.close();
if (this.server) {
this.server.close();
}
}
};