budibase/packages/server/middleware/routes/neo/tests/couchTestUtils.js

88 lines
1.8 KiB
JavaScript
Raw Normal View History

2020-04-09 17:53:48 +02:00
const couchdb = require("../../../../db");
const CLIENT_DB_ID = "client-testing";
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-13 12:47:53 +02:00
exports.createModel = async instanceId => {
const model = {
"name": "TestModel",
"type": "model",
"key": "name",
"schema": {
"name": { "type": "string" }
}
2020-04-13 12:47:53 +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}`]: {
map: function(doc) {
emit([doc.modelId], doc._id);
}
}
};
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);
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');
await db.insert({
_id: TEST_APP_ID,
type: "app",
instances: []
});
return CLIENT_DB_ID;
2020-04-09 17:53:48 +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);
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-09 17:53:48 +02:00
}