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-01-12 12:25:07 +00:00
|
|
|
this.meta_cache = {};
|
2018-01-24 11:52:05 +00:00
|
|
|
this.modules = {};
|
2018-01-15 11:55:31 +00:00
|
|
|
this.docs = {};
|
|
|
|
this.flags = {
|
|
|
|
cache_docs: false
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-02-08 12:28:51 +00:00
|
|
|
addToCache(doc) {
|
2018-01-15 11:55:31 +00:00
|
|
|
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) {
|
2018-01-15 11:55:31 +00:00
|
|
|
if (this.docs[doctype] && this.docs[doctype][name]) {
|
|
|
|
return this.docs[doctype][name];
|
|
|
|
}
|
2018-01-12 12:25:07 +00:00
|
|
|
},
|
|
|
|
|
2018-02-08 06:46:38 +00:00
|
|
|
getMeta(doctype) {
|
2018-01-12 12:25:07 +00:00
|
|
|
if (!this.meta_cache[doctype]) {
|
2018-02-08 12:28:51 +00:00
|
|
|
this.meta_cache[doctype] = new (this.getMetaClass(doctype))();
|
2018-01-12 12:25:07 +00:00
|
|
|
}
|
|
|
|
return this.meta_cache[doctype];
|
|
|
|
},
|
|
|
|
|
2018-02-08 12:28:51 +00:00
|
|
|
getMetaClass(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-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) {
|
2018-02-08 12:28:51 +00:00
|
|
|
let controller_class = this.getControllerClass(doctype);
|
2018-02-01 11:07:36 +00:00
|
|
|
doc = new controller_class({doctype:doctype, name: name});
|
|
|
|
await doc.load();
|
2018-02-08 12:28:51 +00:00
|
|
|
this.addToCache(doc);
|
2018-01-12 12:25:07 +00:00
|
|
|
}
|
|
|
|
return doc;
|
|
|
|
},
|
|
|
|
|
2018-02-08 12:28:51 +00:00
|
|
|
newDoc(data) {
|
|
|
|
let controller_class = this.getControllerClass(data.doctype);
|
2018-02-01 11:07:36 +00:00
|
|
|
return new controller_class(data);
|
|
|
|
},
|
|
|
|
|
2018-02-08 12:28:51 +00:00
|
|
|
getControllerClass(doctype) {
|
2018-01-24 11:52:05 +00:00
|
|
|
doctype = this.slug(doctype);
|
|
|
|
if (this.modules[doctype] && this.modules[doctype].Document) {
|
|
|
|
return this.modules[doctype].Document;
|
|
|
|
} else {
|
|
|
|
return this.BaseDocument;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-02-08 12:28:51 +00:00
|
|
|
async getNewDoc(doctype) {
|
|
|
|
let doc = this.newDoc({doctype: doctype});
|
2018-02-08 11:45:32 +00:00
|
|
|
doc.setName();
|
2018-02-09 12:55:55 +00:00
|
|
|
doc._notInserted = true;
|
2018-02-08 12:28:51 +00:00
|
|
|
this.addToCache(doc);
|
2018-01-15 11:55:31 +00:00
|
|
|
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 = new this._session.Session(user);
|
|
|
|
if (user && user_key) {
|
|
|
|
this.authenticate(user_key);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
close() {
|
|
|
|
this.db.close();
|
|
|
|
|
|
|
|
if (this.server) {
|
|
|
|
this.server.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|