insomnia/app/database/index.js

152 lines
2.9 KiB
JavaScript
Raw Normal View History

2016-04-16 23:24:57 +00:00
import PouchDB from 'pouchdb';
import * as methods from '../constants/global';
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');
// For browser console debugging
2016-04-18 04:39:15 +00:00
global.db = db;
2016-04-16 23:24:57 +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});
}
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-16 23:24:57 +00:00
return db.put(updatedDoc).catch(e => {
if (e.status === 409) {
console.warn('Retrying document update for', updatedDoc);
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});
}
// ~~~~~~~~~~~~~~~~~~~ //
// 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 = {}) {
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
{
_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-18 04:39:15 +00:00
update(model);
2016-04-18 04:39:15 +00:00
return model;
}
// ~~~~~~~ //
// REQUEST //
// ~~~~~~~ //
export function requestCreate (patch = {}) {
2016-04-18 04:39:15 +00:00
return modelCreate('Request', 'req', {
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
}
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-16 23:24:57 +00:00
// ~~~~~~~~~~~~~ //
// REQUEST GROUP //
// ~~~~~~~~~~~~~ //
export function requestGroupCreate (patch = {}) {
2016-04-18 04:39:15 +00:00
return modelCreate('RequestGroup', 'grp', {
collapsed: false,
name: 'New Request Group',
environment: {},
parent: null
}, patch);
}
2016-04-16 23:24:57 +00:00
// ~~~~~~~~~//
// RESPONSE //
// ~~~~~~~~~//
2016-04-16 23:24:57 +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: ''
}, 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
},
sort: [{requestId: 'desc'}],
2016-04-18 04:39:15 +00:00
limit: 1
})
}