2016-04-16 23:24:57 +00:00
|
|
|
import PouchDB from 'pouchdb';
|
|
|
|
import * as methods from '../constants/global';
|
2016-04-17 01:52:10 +00:00
|
|
|
import {generateId} from './util'
|
2016-04-18 04:39:15 +00:00
|
|
|
import pouchDBFind from 'pouchdb-find'
|
|
|
|
|
|
|
|
// Add plugins
|
|
|
|
PouchDB.plugin(pouchDBFind);
|
2016-04-16 23:24:57 +00:00
|
|
|
|
|
|
|
let db = new PouchDB('insomnia.db');
|
|
|
|
|
2016-04-17 20:35:35 +00:00
|
|
|
// For browser console debugging
|
2016-04-18 04:39:15 +00:00
|
|
|
global.db = db;
|
2016-04-16 23:24:57 +00:00
|
|
|
|
2016-04-17 20:35:35 +00:00
|
|
|
export let changes = db.changes({
|
2016-04-16 23:24:57 +00:00
|
|
|
since: 'now',
|
|
|
|
live: true,
|
|
|
|
include_docs: true,
|
|
|
|
return_docs: false
|
|
|
|
}).on('complete', function (info) {
|
|
|
|
console.log('complete', info);
|
|
|
|
}).on('error', function (err) {
|
|
|
|
console.log('error', err);
|
|
|
|
});
|
|
|
|
|
|
|
|
export function allDocs () {
|
|
|
|
return db.allDocs({include_docs: true});
|
|
|
|
}
|
|
|
|
|
2016-04-17 01:52:10 +00:00
|
|
|
export function get (id) {
|
|
|
|
return db.get(id);
|
|
|
|
}
|
|
|
|
|
2016-04-16 23:24:57 +00:00
|
|
|
export function update (doc, patch = {}) {
|
|
|
|
const updatedDoc = Object.assign(
|
|
|
|
{},
|
|
|
|
doc,
|
|
|
|
patch,
|
|
|
|
{modified: Date.now()}
|
|
|
|
);
|
2016-04-17 20:35:35 +00:00
|
|
|
|
2016-04-16 23:24:57 +00:00
|
|
|
return db.put(updatedDoc).catch(e => {
|
|
|
|
if (e.status === 409) {
|
|
|
|
console.warn('Retrying document update for', updatedDoc);
|
2016-04-17 01:52:10 +00:00
|
|
|
get(doc._id).then(dbDoc => {
|
2016-04-16 23:24:57 +00:00
|
|
|
update(dbDoc, patch);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function remove (doc) {
|
|
|
|
return update(doc, {_deleted: true});
|
|
|
|
}
|
|
|
|
|
2016-04-17 20:35:35 +00:00
|
|
|
// ~~~~~~~~~~~~~~~~~~~ //
|
|
|
|
// DEFAULT MODEL STUFF //
|
|
|
|
// ~~~~~~~~~~~~~~~~~~~ //
|
2016-04-16 23:24:57 +00:00
|
|
|
|
2016-04-18 04:39:15 +00:00
|
|
|
function modelCreate (type, idPrefix, defaults, patch = {}) {
|
2016-04-17 20:35:35 +00:00
|
|
|
const model = Object.assign(
|
|
|
|
defaults,
|
2016-04-16 23:24:57 +00:00
|
|
|
patch,
|
2016-04-18 04:39:15 +00:00
|
|
|
|
2016-04-16 23:24:57 +00:00
|
|
|
// Required Generated Fields
|
|
|
|
{
|
2016-04-17 20:35:35 +00:00
|
|
|
_id: generateId(idPrefix),
|
2016-04-16 23:24:57 +00:00
|
|
|
_rev: undefined,
|
2016-04-18 04:39:15 +00:00
|
|
|
type: type,
|
2016-04-16 23:24:57 +00:00
|
|
|
created: Date.now(),
|
|
|
|
modified: Date.now()
|
|
|
|
}
|
2016-04-17 01:52:10 +00:00
|
|
|
);
|
2016-04-18 04:39:15 +00:00
|
|
|
|
2016-04-17 20:35:35 +00:00
|
|
|
update(model);
|
2016-04-18 04:39:15 +00:00
|
|
|
|
2016-04-17 20:35:35 +00:00
|
|
|
return model;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ~~~~~~~ //
|
|
|
|
// REQUEST //
|
|
|
|
// ~~~~~~~ //
|
|
|
|
|
|
|
|
export function requestCreate (patch = {}) {
|
2016-04-18 04:39:15 +00:00
|
|
|
return modelCreate('Request', 'req', {
|
2016-04-17 20:35:35 +00:00
|
|
|
url: '',
|
|
|
|
name: 'New Request',
|
|
|
|
method: methods.METHOD_GET,
|
|
|
|
body: '',
|
|
|
|
params: [],
|
|
|
|
contentType: 'text/plain',
|
|
|
|
headers: [],
|
|
|
|
authentication: {},
|
|
|
|
parent: null
|
|
|
|
}, patch);
|
2016-04-16 23:24:57 +00:00
|
|
|
}
|
|
|
|
|
2016-04-17 20:35:35 +00:00
|
|
|
export function requestCopy (originalRequest) {
|
|
|
|
const name = `${originalRequest.name} (Copy)`;
|
|
|
|
return requestCreate(Object.assign({}, originalRequest, {name}));
|
2016-04-16 23:24:57 +00:00
|
|
|
}
|
|
|
|
|
2016-04-17 20:35:35 +00:00
|
|
|
|
2016-04-16 23:24:57 +00:00
|
|
|
// ~~~~~~~~~~~~~ //
|
|
|
|
// REQUEST GROUP //
|
|
|
|
// ~~~~~~~~~~~~~ //
|
|
|
|
|
|
|
|
export function requestGroupCreate (patch = {}) {
|
2016-04-18 04:39:15 +00:00
|
|
|
return modelCreate('RequestGroup', 'grp', {
|
2016-04-17 20:35:35 +00:00
|
|
|
collapsed: false,
|
|
|
|
name: 'New Request Group',
|
|
|
|
environment: {},
|
|
|
|
parent: null
|
|
|
|
}, patch);
|
|
|
|
}
|
2016-04-16 23:24:57 +00:00
|
|
|
|
|
|
|
|
2016-04-17 20:35:35 +00:00
|
|
|
// ~~~~~~~~~//
|
|
|
|
// RESPONSE //
|
|
|
|
// ~~~~~~~~~//
|
2016-04-16 23:24:57 +00:00
|
|
|
|
2016-04-17 20:35:35 +00:00
|
|
|
export function responseCreate (patch = {}) {
|
2016-04-18 04:39:15 +00:00
|
|
|
return modelCreate('Response', 'rsp', {
|
|
|
|
requestId: null,
|
|
|
|
statusCode: 0,
|
|
|
|
statusMessage: '',
|
|
|
|
contentType: 'text/plain',
|
|
|
|
bytes: 0,
|
|
|
|
millis: 0,
|
|
|
|
headers: {},
|
|
|
|
body: ''
|
2016-04-17 20:35:35 +00:00
|
|
|
}, patch);
|
|
|
|
}
|
2016-04-18 04:39:15 +00:00
|
|
|
|
|
|
|
db.createIndex({
|
|
|
|
index: {fields: ['requestId']}
|
|
|
|
}).catch(err => {
|
|
|
|
console.error('Failed to create index', err);
|
|
|
|
}).then(() => {
|
|
|
|
console.log('-- Indexes Updated --');
|
|
|
|
});
|
|
|
|
|
|
|
|
export function responseGetForRequest (request) {
|
|
|
|
return db.find({
|
|
|
|
selector: {
|
|
|
|
requestId: request._id
|
|
|
|
},
|
2016-04-20 02:14:53 +00:00
|
|
|
sort: [{requestId: 'desc'}],
|
2016-04-18 04:39:15 +00:00
|
|
|
limit: 1
|
|
|
|
})
|
|
|
|
}
|