2018-01-01 12:17:51 +00:00
|
|
|
const path = require('path');
|
2018-01-02 10:10:32 +00:00
|
|
|
const bodyParser = require('body-parser');
|
2018-01-03 16:38:17 +00:00
|
|
|
const express = require('express');
|
2018-01-01 12:17:51 +00:00
|
|
|
|
2018-01-01 09:27:59 +00:00
|
|
|
module.exports = {
|
2018-01-03 06:49:59 +00:00
|
|
|
async init() {
|
2018-01-01 09:27:59 +00:00
|
|
|
if (this._initialized) return;
|
|
|
|
this.init_config();
|
|
|
|
this.init_errors();
|
|
|
|
this.init_globals();
|
|
|
|
this.init_libs();
|
|
|
|
this._initialized = true;
|
2018-01-03 06:49:59 +00:00
|
|
|
|
|
|
|
// login as guest
|
|
|
|
this.login();
|
2018-01-01 09:27:59 +00:00
|
|
|
},
|
2018-01-02 10:10:32 +00:00
|
|
|
|
2018-01-01 09:27:59 +00:00
|
|
|
init_config() {
|
2018-01-02 10:10:32 +00:00
|
|
|
this.config = {
|
2018-01-03 06:49:59 +00:00
|
|
|
backend: 'sqlite'
|
2018-01-02 10:10:32 +00:00
|
|
|
};
|
2018-01-01 09:27:59 +00:00
|
|
|
},
|
2018-01-02 10:10:32 +00:00
|
|
|
|
2018-01-01 09:27:59 +00:00
|
|
|
init_errors() {
|
|
|
|
this.ValueError = class extends Error { };
|
|
|
|
},
|
2018-01-02 10:10:32 +00:00
|
|
|
|
2018-01-01 09:27:59 +00:00
|
|
|
init_globals() {
|
|
|
|
this.meta_cache = {};
|
|
|
|
},
|
2018-01-02 10:10:32 +00:00
|
|
|
|
2018-01-01 09:27:59 +00:00
|
|
|
init_libs() {
|
|
|
|
this.utils = require('./utils');
|
|
|
|
Object.assign(this, this.utils);
|
|
|
|
let models = require('./model/models');
|
|
|
|
this.models = new models.Models();
|
|
|
|
this.model = require('./model');
|
|
|
|
this.document = require('./model/document');
|
|
|
|
this.meta = require('./model/meta');
|
|
|
|
this.session_lib = require('./session');
|
2018-01-02 10:10:32 +00:00
|
|
|
this.rest_server = require('./rest_server');
|
|
|
|
},
|
|
|
|
|
|
|
|
init_app(app) {
|
|
|
|
this.app = app;
|
|
|
|
this.init_middleware();
|
|
|
|
this.rest_server.setup(this.app);
|
|
|
|
},
|
|
|
|
|
|
|
|
init_middleware() {
|
|
|
|
this.app.use(bodyParser.json());
|
|
|
|
this.app.use(bodyParser.urlencoded({ extended: true }));
|
2018-01-03 16:38:17 +00:00
|
|
|
this.app.use(express.static('frappe/client'));
|
2018-01-02 10:10:32 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
async start(port=8000) {
|
|
|
|
await this.db.migrate();
|
|
|
|
this.server = this.app.listen(port);
|
2018-01-01 09:27:59 +00:00
|
|
|
},
|
2018-01-02 10:10:32 +00:00
|
|
|
|
2018-01-03 06:49:59 +00:00
|
|
|
async init_db(db_type, options) {
|
|
|
|
var Database;
|
|
|
|
switch (db_type) {
|
|
|
|
case 'sqlite':
|
|
|
|
Database = require('./backends/sqlite').Database;
|
|
|
|
break;
|
|
|
|
case 'rest':
|
|
|
|
Database = require('./backends/rest_client').Database;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Error(`${db_type} is not a supported database type`);
|
|
|
|
}
|
|
|
|
this.db = new Database(options);
|
|
|
|
await this.db.connect();
|
2018-01-01 09:27:59 +00:00
|
|
|
},
|
2018-01-02 10:10:32 +00:00
|
|
|
|
2018-01-01 09:27:59 +00:00
|
|
|
get_meta(doctype) {
|
|
|
|
if (!this.meta_cache[doctype]) {
|
|
|
|
this.meta_cache[doctype] = new this.meta.Meta(this.models.get('DocType', doctype));
|
|
|
|
}
|
|
|
|
return this.meta_cache[doctype];
|
|
|
|
},
|
2018-01-02 10:10:32 +00:00
|
|
|
|
|
|
|
async get_doc(data, name) {
|
2018-01-01 09:27:59 +00:00
|
|
|
if (typeof data==='string' && typeof name==='string') {
|
|
|
|
let controller_class = this.models.get_controller(data);
|
|
|
|
var doc = new controller_class({doctype:data, name: name});
|
2018-01-02 10:10:32 +00:00
|
|
|
await doc.load();
|
2018-01-01 09:27:59 +00:00
|
|
|
} else {
|
|
|
|
let controller_class = this.models.get_controller(data.doctype);
|
|
|
|
var doc = new controller_class(data);
|
|
|
|
}
|
|
|
|
return doc;
|
|
|
|
},
|
2018-01-02 10:10:32 +00:00
|
|
|
|
|
|
|
async insert(data) {
|
|
|
|
const doc = await this.get_doc(data);
|
|
|
|
return await doc.insert();
|
|
|
|
},
|
|
|
|
|
2018-01-03 06:49:59 +00:00
|
|
|
login(user='guest', user_key) {
|
2018-01-01 09:27:59 +00:00
|
|
|
this.session = new this.session_lib.Session(user);
|
|
|
|
if (user && user_key) {
|
|
|
|
this.login(user_key);
|
|
|
|
}
|
2018-01-02 10:10:32 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
close() {
|
|
|
|
this.db.close();
|
2018-01-03 06:49:59 +00:00
|
|
|
|
|
|
|
if (this.server) {
|
|
|
|
this.server.close();
|
|
|
|
}
|
2018-01-01 09:27:59 +00:00
|
|
|
}
|
|
|
|
};
|