2020-04-09 17:53:48 +02:00
|
|
|
const couchdb = require("../../../../db");
|
|
|
|
|
|
|
|
const CLIENT_DB_ID = "client-testing";
|
2020-04-20 17:17:11 +02:00
|
|
|
const TEST_APP_ID = "test-app";
|
2020-04-09 17:53:48 +02:00
|
|
|
|
2020-04-10 12:18:15 +02:00
|
|
|
exports.destroyDatabase = couchdb.db.destroy;
|
|
|
|
|
2020-04-22 17:35:20 +02:00
|
|
|
exports.createModel = async (instanceId, model) => {
|
|
|
|
model = model || {
|
2020-04-13 12:47:53 +02:00
|
|
|
"name": "TestModel",
|
|
|
|
"type": "model",
|
|
|
|
"key": "name",
|
2020-04-20 17:17:11 +02:00
|
|
|
"schema": {
|
|
|
|
"name": { "type": "string" }
|
|
|
|
}
|
2020-04-13 12:47:53 +02:00
|
|
|
}
|
2020-04-13 18:04:51 +02:00
|
|
|
const db = couchdb.db.use(instanceId);
|
|
|
|
const response = await db.insert(model);
|
|
|
|
|
|
|
|
const designDoc = await db.get("_design/database");
|
|
|
|
designDoc.views = {
|
|
|
|
...designDoc.views,
|
|
|
|
[`all_${response.id}`]: {
|
2020-04-22 17:35:20 +02:00
|
|
|
map: `function(doc) {
|
|
|
|
if (doc.modelId === "${response.id}") {
|
|
|
|
emit(doc[doc.key], doc._id);
|
|
|
|
}
|
|
|
|
}`
|
2020-04-13 18:04:51 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
await db.insert(designDoc, designDoc._id);
|
|
|
|
|
2020-04-13 17:46:28 +02:00
|
|
|
return {
|
|
|
|
...response,
|
|
|
|
...model
|
|
|
|
};
|
2020-04-13 12:47:53 +02:00
|
|
|
}
|
|
|
|
|
2020-04-09 17:53:48 +02:00
|
|
|
exports.createClientDatabase = async () => {
|
|
|
|
await couchdb.db.create(CLIENT_DB_ID);
|
|
|
|
|
2020-04-20 17:17:11 +02:00
|
|
|
const db = couchdb.db.use(CLIENT_DB_ID);
|
|
|
|
|
|
|
|
await db.insert({
|
2020-04-09 17:53:48 +02:00
|
|
|
views: {
|
|
|
|
by_type: {
|
|
|
|
map: function(doc) {
|
|
|
|
emit([doc.type], doc._id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, '_design/client');
|
2020-04-20 17:17:11 +02:00
|
|
|
|
|
|
|
await db.insert({
|
|
|
|
_id: TEST_APP_ID,
|
|
|
|
type: "app",
|
|
|
|
instances: []
|
|
|
|
});
|
|
|
|
|
|
|
|
return CLIENT_DB_ID;
|
2020-04-09 17:53:48 +02:00
|
|
|
}
|
|
|
|
|
2020-04-20 17:17:11 +02:00
|
|
|
exports.destroyClientDatabase = async () => await couchdb.db.destroy(CLIENT_DB_ID);
|
|
|
|
|
2020-04-09 17:53:48 +02:00
|
|
|
exports.createInstanceDatabase = async instanceId => {
|
|
|
|
await couchdb.db.create(instanceId);
|
|
|
|
|
2020-04-20 17:17:11 +02:00
|
|
|
const db = couchdb.db.use(instanceId);
|
|
|
|
|
|
|
|
await db.insert({
|
|
|
|
metadata: {
|
|
|
|
clientId: CLIENT_DB_ID,
|
|
|
|
applicationId: TEST_APP_ID
|
|
|
|
},
|
2020-04-09 17:53:48 +02:00
|
|
|
views: {
|
|
|
|
by_type: {
|
|
|
|
map: function(doc) {
|
|
|
|
emit([doc.type], doc._id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, '_design/database');
|
2020-04-13 12:47:53 +02:00
|
|
|
|
|
|
|
return instanceId;
|
2020-04-10 17:37:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.insertDocument = async (databaseId, document) => {
|
|
|
|
const { id, ...documentFields } = document;
|
|
|
|
await couchdb.db.use(databaseId).insert(documentFields, id);
|
2020-04-22 17:35:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.createSchema = async (request, instanceId, schema) => {
|
|
|
|
for (let model of schema.models) {
|
|
|
|
await request.post(`/api/${instanceId}/models`).send(model)
|
|
|
|
}
|
|
|
|
for (let view of schema.views) {
|
|
|
|
await request.post(`/api/${instanceId}/views`).send(view)
|
|
|
|
}
|
|
|
|
}
|