@resin/pinejs
Version:
Pine.js is a sophisticated rules-driven API engine that enables you to define rules in a structured subset of English. Those rules are used in order for Pine.js to generate a database schema and the associated [OData](http://www.odata.org/) API. This make
328 lines (318 loc) • 11 kB
JavaScript
// Generated by CoffeeScript 1.12.7
(function() {
var Promise, isServerOnAir, permissions, serverIsOnAir, uiModel,
hasProp = {}.hasOwnProperty;
Promise = require('bluebird');
permissions = require('../sbvr-api/permissions');
uiModel = 'Vocabulary: ui\n\nTerm: text\n Concept type: Text (Type)\nTerm: name\n Concept type: Short Text (Type)\nTerm: textarea\n --Database id Field: name\n Reference Scheme: text\nFact type: textarea is disabled\nFact type: textarea has name\n Necessity: Each textarea has exactly 1 name\n Necessity: Each name is of exactly 1 textarea\nFact type: textarea has text\n Necessity: Each textarea has exactly 1 text';
isServerOnAir = (function() {
var promise, resolve;
resolve = null;
promise = new Promise(function(_resolve) {
return resolve = _resolve;
});
return function(value) {
if (value != null) {
if (promise.isPending()) {
resolve(value);
resolve = null;
} else {
promise = Promise.fulfilled(value);
}
}
return promise;
};
})();
serverIsOnAir = function(req, res, next) {
return isServerOnAir().then(function(onAir) {
if (onAir) {
return next();
} else {
return next('route');
}
});
};
exports.config = {
models: [
{
modelName: 'ui',
modelText: uiModel,
apiRoot: 'ui',
customServerCode: exports
}
]
};
exports.setup = function(app, sbvrUtils, db) {
var devApi, setupModels, uiApi;
uiApi = sbvrUtils.api.ui;
devApi = sbvrUtils.api.dev;
setupModels = function(tx) {
var uiApiTx;
uiApiTx = uiApi.clone({
passthrough: {
tx: tx,
req: permissions.root
}
});
return uiApiTx.get({
resource: 'textarea',
options: {
$select: 'id',
$filter: {
name: 'model_area'
}
}
}).then(function(result) {
if (result.length === 0) {
return uiApiTx.post({
resource: 'textarea',
body: {
name: 'model_area',
text: ' '
}
});
}
}).then(function() {
return devApi.get({
resource: 'model',
passthrough: {
tx: tx,
req: permissions.rootRead
},
options: {
$select: ['is_of__vocabulary', 'model_value'],
$filter: {
model_type: 'se',
is_of__vocabulary: 'data'
}
}
});
}).then(function(result) {
var instance;
if (result.length === 0) {
throw new Error('No SE data model found');
}
instance = result[0];
return sbvrUtils.executeModel(tx, {
apiRoot: instance.is_of__vocabulary,
modelText: instance.model_value
});
}).then(function() {
return isServerOnAir(true);
})["catch"](function(err) {
return isServerOnAir(false);
});
};
app.get('/onAir', function(req, res, next) {
return isServerOnAir().then(function(onAir) {
return res.json(onAir);
});
});
app.post('/update', permissions.checkPermissionsMiddleware('all'), serverIsOnAir, function(req, res, next) {
return res.sendStatus(404);
});
app.post('/execute', permissions.checkPermissionsMiddleware('all'), function(req, res, next) {
return uiApi.get({
resource: 'textarea',
passthrough: {
req: permissions.rootRead
},
options: {
$select: 'text',
$filter: {
name: 'model_area'
}
}
}).then(function(result) {
var modelText;
if (result.length === 0) {
throw new Error('Could not find the model to execute');
}
modelText = result[0].text;
return db.transaction(function(tx) {
return sbvrUtils.executeModel(tx, {
apiRoot: 'data',
modelText: modelText
}).then(function() {
return uiApi.patch({
resource: 'textarea',
passthrough: {
tx: tx,
req: permissions.root
},
options: {
$filter: {
name: 'model_area'
}
},
body: {
is_disabled: true
}
});
});
});
}).then(function() {
isServerOnAir(true);
return res.sendStatus(200);
})["catch"](function(err) {
isServerOnAir(false);
return res.status(404).json(err);
});
});
app.post('/validate', permissions.checkPermissionsMiddleware('get'), function(req, res, next) {
return sbvrUtils.runRule('data', req.body.rule).then(function(results) {
return res.json(results);
})["catch"](function(err) {
console.log('Error validating', err);
return res.sendStatus(404);
});
});
app["delete"]('/cleardb', permissions.checkPermissionsMiddleware('delete'), function(req, res, next) {
return db.transaction(function(tx) {
return tx.tableList().then(function(result) {
return Promise.all(result.rows.map(function(table) {
return tx.dropTable(table.name);
}));
}).then(function() {
return sbvrUtils.executeStandardModels(tx);
}).then(function() {
console.warn('DEL /cleardb is very destructive and should really be followed by a full restart/reload.');
return sbvrUtils.executeModels(tx, exports.config.models);
}).then(function() {
return setupModels(tx);
});
}).then(function() {
return res.sendStatus(200);
})["catch"](function(err) {
console.error('Error clearing db', err, err.stack);
return res.sendStatus(503);
});
});
app.put('/importdb', permissions.checkPermissionsMiddleware('set'), function(req, res, next) {
var queries;
queries = req.body.split(';');
return db.transaction(function(tx) {
return Promise.reduce(queries, function(result, query) {
query = query.trim();
if (query.length > 0) {
return tx.executeSql(query)["catch"](function(err) {
throw [query, err];
});
}
}, null);
}).then(function() {
return res.sendStatus(200);
})["catch"](function(err) {
console.error('Error importing db', err, err.stack);
return res.sendStatus(404);
});
});
app.get('/exportdb', permissions.checkPermissionsMiddleware('get'), function(req, res, next) {
return db.transaction(function(tx) {
return tx.tableList("name NOT LIKE '%_buk'").then(function(result) {
var exported;
exported = '';
return Promise.all(result.rows.map(function(table) {
var tableName;
tableName = table.name;
exported += 'DROP TABLE IF EXISTS "' + tableName + '";\n';
exported += table.sql + ';\n';
return tx.executeSql('SELECT * FROM "' + tableName + '";').then(function(result) {
var insQuery;
insQuery = '';
result.rows.forEach(function(currRow) {
var notFirst, propName, valQuery;
notFirst = false;
insQuery += 'INSERT INTO "' + tableName + '" (';
valQuery = '';
for (propName in currRow) {
if (!hasProp.call(currRow, propName)) continue;
if (notFirst) {
insQuery += ',';
valQuery += ',';
} else {
notFirst = true;
}
insQuery += '"' + propName + '"';
valQuery += "'" + currRow[propName] + "'";
}
return insQuery += ') values (' + valQuery + ');\n';
});
return exported += insQuery;
});
}))["return"](exported);
});
}).then(function(exported) {
return res.json(exported);
})["catch"](function(err) {
console.error('Error exporting db', err, err.stack);
return res.sendStatus(503);
});
});
app.post('/backupdb', permissions.checkPermissionsMiddleware('all'), serverIsOnAir, function(req, res, next) {
return db.transaction(function(tx) {
return tx.tableList("name NOT LIKE '%_buk'").then(function(result) {
return Promise.all(result.rows.map(function(currRow) {
var tableName;
tableName = currRow.name;
return tx.dropTable(tableName + '_buk', true).then(function() {
return tx.executeSql('ALTER TABLE "' + tableName + '" RENAME TO "' + tableName + '_buk";');
});
}));
});
}).then(function() {
return res.sendStatus(200);
})["catch"](function(err) {
console.error('Error backing up db', err, err.stack);
return res.sendStatus(404);
});
});
app.post('/restoredb', permissions.checkPermissionsMiddleware('all'), serverIsOnAir, function(req, res, next) {
return db.transaction(function(tx) {
return tx.tableList("name LIKE '%_buk'").then(function(result) {
return Promise.all(result.rows.map(function(currRow) {
var tableName;
tableName = currRow.name;
return tx.dropTable(tableName.slice(0, -4), true).then(function() {
return tx.executeSql('ALTER TABLE "' + tableName + '" RENAME TO "' + tableName.slice(0, -4) + '";');
});
}));
});
}).then(function() {
return res.sendStatus(200);
})["catch"](function(err) {
console.error('Error restoring db', err, err.stack);
return res.sendStatus(404);
});
});
app.all('/data/*', serverIsOnAir, sbvrUtils.handleODataRequest);
app.get('/Auth/*', serverIsOnAir, sbvrUtils.handleODataRequest);
app.merge('/ui/*', sbvrUtils.handleODataRequest);
app.patch('/ui/*', sbvrUtils.handleODataRequest);
app["delete"]('/', serverIsOnAir, function(req, res, next) {
return Promise.all([
uiApi.patch({
resource: 'textarea',
passthrough: {
req: permissions.root
},
options: {
$filter: {
name: 'model_area'
}
},
body: {
text: '',
name: 'model_area',
is_disabled: false
}
}), sbvrUtils.deleteModel('data')
]).then(function() {
isServerOnAir(false);
return res.sendStatus(200);
});
});
return db.transaction(setupModels);
};
}).call(this);
//# sourceMappingURL=SBVRServer.js.map