2
0
mirror of https://github.com/frappe/books.git synced 2024-09-20 11:29:00 +00:00
books/index.js

139 lines
3.4 KiB
JavaScript
Raw Normal View History

2018-01-12 12:25:07 +00:00
module.exports = {
async init() {
if (this._initialized) return;
2018-02-08 12:28:51 +00:00
this.initConfig();
this.initGlobals();
2018-01-12 12:25:07 +00:00
this._initialized = true;
},
2018-02-08 12:28:51 +00:00
initConfig() {
2018-01-12 12:25:07 +00:00
this.config = {
backend: 'sqlite',
port: 8000
};
},
2018-02-08 12:28:51 +00:00
initGlobals() {
2018-02-14 12:50:56 +00:00
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;
},
2018-02-08 12:28:51 +00:00
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;
}
},
2018-02-08 12:28:51 +00:00
getDocFromCache(doctype, name) {
if (this.docs[doctype] && this.docs[doctype][name]) {
return this.docs[doctype][name];
}
2018-01-12 12:25:07 +00:00
},
getMeta(doctype) {
2018-02-14 12:50:56 +00:00
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);
2018-01-12 12:25:07 +00:00
}
return this.metaCache[doctype];
2018-01-12 12:25:07 +00:00
},
2018-02-08 12:28:51 +00:00
async getDoc(doctype, name) {
let doc = this.getDocFromCache(doctype, name);
2018-02-01 11:07:36 +00:00
if (!doc) {
doc = new (this.getDocumentClass(doctype))({doctype:doctype, name: name});
2018-02-01 11:07:36 +00:00
await doc.load();
2018-02-08 12:28:51 +00:00
this.addToCache(doc);
2018-01-12 12:25:07 +00:00
}
return doc;
},
getDocumentClass(doctype) {
const meta = this.getMeta(doctype);
return meta.documentClass || this.BaseDocument;
},
2018-02-12 12:01:31 +00:00
async getSingle(doctype) {
return await this.getDoc(doctype, doctype);
},
2018-02-14 12:50:56 +00:00
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') {
2018-02-19 16:41:10 +00:00
newDoc[field.fieldname] = (doc[field.fieldname] || []).map(d => {
let newd = Object.assign({}, d);
newd.name = '';
return newd;
});
2018-02-14 12:50:56 +00:00
} else {
newDoc[field.fieldname] = doc[field.fieldname];
}
}
return newDoc;
},
2018-02-08 12:28:51 +00:00
newDoc(data) {
let doc = new (this.getDocumentClass(data.doctype))(data);
2018-02-12 12:01:31 +00:00
doc.setDefaults();
return doc;
2018-02-01 11:07:36 +00:00
},
2018-02-08 12:28:51 +00:00
async getNewDoc(doctype) {
let doc = this.newDoc({doctype: doctype});
2018-02-09 12:55:55 +00:00
doc._notInserted = true;
2018-02-12 12:01:31 +00:00
doc.name = this.getRandomName();
2018-02-08 12:28:51 +00:00
this.addToCache(doc);
return doc;
},
2018-01-12 12:25:07 +00:00
async insert(data) {
2018-02-08 12:28:51 +00:00
return await (this.newDoc(data)).insert();
2018-01-12 12:25:07 +00:00
},
login(user='guest', user_key) {
this.session = {user: user};
2018-01-12 12:25:07 +00:00
},
close() {
this.db.close();
if (this.server) {
this.server.close();
}
}
};