2016-07-16 02:06:10 +00:00
|
|
|
import electron from 'electron';
|
|
|
|
import * as fsPath from 'path';
|
|
|
|
import * as fs from 'fs';
|
2016-04-27 23:33:38 +00:00
|
|
|
|
2016-07-16 02:06:10 +00:00
|
|
|
import * as methods from '../lib/constants';
|
|
|
|
import {generateId} from './util';
|
|
|
|
import {PREVIEW_MODE_SOURCE} from '../lib/previewModes';
|
|
|
|
import {CONTENT_TYPE_TEXT} from '../lib/contentTypes';
|
|
|
|
import {DB_PERSIST_INTERVAL, DEFAULT_SIDEBAR_WIDTH} from '../lib/constants';
|
2016-04-18 04:39:15 +00:00
|
|
|
|
2016-04-26 07:29:24 +00:00
|
|
|
export const TYPE_WORKSPACE = 'Workspace';
|
|
|
|
export const TYPE_REQUEST_GROUP = 'RequestGroup';
|
|
|
|
export const TYPE_REQUEST = 'Request';
|
|
|
|
export const TYPE_RESPONSE = 'Response';
|
2016-04-27 03:17:05 +00:00
|
|
|
const TYPES = [
|
|
|
|
TYPE_WORKSPACE,
|
|
|
|
TYPE_REQUEST_GROUP,
|
|
|
|
TYPE_REQUEST,
|
|
|
|
TYPE_RESPONSE
|
|
|
|
];
|
2016-04-26 07:29:24 +00:00
|
|
|
|
2016-04-27 03:17:05 +00:00
|
|
|
let db = null;
|
2016-05-11 05:43:51 +00:00
|
|
|
global.db = db;
|
2016-04-16 23:24:57 +00:00
|
|
|
|
2016-04-28 00:04:29 +00:00
|
|
|
function getDBFilePath () {
|
2016-04-28 07:30:26 +00:00
|
|
|
const basePath = electron.remote.app.getPath('userData');
|
|
|
|
return fsPath.join(basePath, 'insomnia.db.json');
|
2016-04-28 00:04:29 +00:00
|
|
|
}
|
|
|
|
|
2016-04-27 03:17:05 +00:00
|
|
|
/**
|
|
|
|
* Initialize the database. This should be called once on app start.
|
|
|
|
* @returns {Promise}
|
|
|
|
*/
|
|
|
|
let initialized = false;
|
|
|
|
export function initDB () {
|
|
|
|
// Only init once
|
|
|
|
if (initialized) {
|
|
|
|
return new Promise(resolve => resolve());
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Promise(resolve => {
|
2016-04-28 00:04:29 +00:00
|
|
|
const dbPath = getDBFilePath();
|
2016-06-18 21:02:27 +00:00
|
|
|
|
|
|
|
db = {
|
|
|
|
created: Date.now(),
|
|
|
|
entities: {}
|
|
|
|
};
|
|
|
|
|
2016-05-11 05:43:51 +00:00
|
|
|
for (let i = 0; i < TYPES.length; i++) {
|
2016-06-18 21:02:27 +00:00
|
|
|
db.entities[TYPES[i]] = {};
|
2016-05-11 05:43:51 +00:00
|
|
|
}
|
2016-06-18 21:02:27 +00:00
|
|
|
|
2016-05-11 05:43:51 +00:00
|
|
|
fs.readFile(getDBFilePath(), 'utf8', (err, text) => {
|
|
|
|
if (!err) {
|
|
|
|
// TODO: Better error handling
|
|
|
|
console.log('-- Restored DB from file --');
|
2016-07-07 20:10:55 +00:00
|
|
|
try {
|
|
|
|
Object.assign(db, JSON.parse(text));
|
|
|
|
} catch (e) {
|
|
|
|
console.error('Failed to parse DB file', e);
|
|
|
|
}
|
2016-04-27 03:17:05 +00:00
|
|
|
}
|
2016-05-11 05:43:51 +00:00
|
|
|
|
|
|
|
// Add listeners to do persistence
|
|
|
|
let timeout = null;
|
|
|
|
onChange('DB_WRITER', () => {
|
|
|
|
clearTimeout(timeout);
|
2016-07-16 07:22:08 +00:00
|
|
|
timeout = setTimeout(persistDB, DB_PERSIST_INTERVAL);
|
2016-05-11 05:43:51 +00:00
|
|
|
});
|
2016-06-18 21:02:27 +00:00
|
|
|
|
2016-05-11 05:43:51 +00:00
|
|
|
// Done
|
|
|
|
|
|
|
|
initialized = true;
|
|
|
|
console.log(`-- Initialize DB at ${dbPath} --`);
|
|
|
|
resolve();
|
2016-04-27 03:17:05 +00:00
|
|
|
});
|
|
|
|
})
|
|
|
|
}
|
2016-04-16 23:24:57 +00:00
|
|
|
|
2016-07-16 07:22:08 +00:00
|
|
|
function persistDB () {
|
|
|
|
// First, write to a tmp file, then overwrite the old one. This
|
|
|
|
// prevents getting a corrupted
|
|
|
|
const filePath = getDBFilePath();
|
|
|
|
const tmpFilePath = `${filePath}.tmp`;
|
|
|
|
|
|
|
|
const start = Date.now();
|
|
|
|
const blob = JSON.stringify(db, null, '\t');
|
|
|
|
const bytes = Buffer.byteLength(blob, 'utf8');
|
|
|
|
const megabytes = Math.round(bytes / 1024 / 1024 * 10) / 10;
|
|
|
|
|
|
|
|
fs.writeFile(tmpFilePath, blob, err => {
|
|
|
|
if (err) {
|
|
|
|
console.error('Failed to write DB to file', err);
|
|
|
|
} else {
|
|
|
|
fs.renameSync(tmpFilePath, filePath);
|
|
|
|
|
|
|
|
console.log(`-- Persisted DB in ${Date.now() - start}ms (${megabytes}MB) --`);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-04-26 07:29:24 +00:00
|
|
|
let changeListeners = {};
|
|
|
|
|
|
|
|
export function onChange (id, callback) {
|
|
|
|
console.log(`-- Added DB Listener ${id} -- `);
|
|
|
|
changeListeners[id] = callback;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function offChange (id) {
|
|
|
|
console.log(`-- Removed DB Listener ${id} -- `);
|
|
|
|
delete changeListeners[id];
|
|
|
|
}
|
|
|
|
|
2016-04-27 03:17:05 +00:00
|
|
|
export function get (type, id) {
|
2016-06-18 21:02:27 +00:00
|
|
|
const doc = db.entities[type][id];
|
2016-04-27 03:17:05 +00:00
|
|
|
return new Promise(resolve => resolve(doc));
|
2016-04-26 07:29:24 +00:00
|
|
|
}
|
|
|
|
|
2016-05-11 05:43:51 +00:00
|
|
|
function all (type) {
|
|
|
|
let docs = [];
|
2016-06-18 21:02:27 +00:00
|
|
|
const ids = Object.keys(db.entities[type]);
|
2016-05-11 05:43:51 +00:00
|
|
|
for (let i = 0; i < ids.length; i++) {
|
2016-06-18 21:02:27 +00:00
|
|
|
docs.push(db.entities[type][ids[i]]);
|
2016-05-11 05:43:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return new Promise(resolve => resolve(docs));
|
|
|
|
}
|
|
|
|
|
|
|
|
function removeWhere (type, key, value) {
|
2016-06-18 21:02:27 +00:00
|
|
|
const ids = Object.keys(db.entities[type]);
|
2016-05-11 05:43:51 +00:00
|
|
|
let docs = [];
|
|
|
|
|
|
|
|
for (let i = 0; i < ids.length; i++) {
|
2016-06-18 21:02:27 +00:00
|
|
|
const doc = db.entities[type][ids[i]];
|
2016-05-11 05:43:51 +00:00
|
|
|
if (doc[key] === value) {
|
|
|
|
remove(doc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-27 03:17:05 +00:00
|
|
|
return new Promise(resolve => resolve(docs));
|
2016-04-16 23:24:57 +00:00
|
|
|
}
|
|
|
|
|
2016-04-27 03:17:05 +00:00
|
|
|
function insert (doc) {
|
2016-06-18 21:02:27 +00:00
|
|
|
db.entities[doc.type][doc._id] = doc;
|
|
|
|
|
2016-05-11 05:43:51 +00:00
|
|
|
Object.keys(changeListeners).map(k => changeListeners[k]('insert', doc));
|
|
|
|
return new Promise(resolve => resolve(doc));
|
2016-04-17 01:52:10 +00:00
|
|
|
}
|
|
|
|
|
2016-04-27 03:17:05 +00:00
|
|
|
function update (doc) {
|
2016-06-18 21:02:27 +00:00
|
|
|
db.entities[doc.type][doc._id] = doc;
|
|
|
|
|
2016-05-11 05:43:51 +00:00
|
|
|
Object.keys(changeListeners).map(k => changeListeners[k]('update', doc));
|
|
|
|
return new Promise(resolve => resolve(doc));
|
2016-04-27 03:17:05 +00:00
|
|
|
}
|
2016-04-26 07:29:24 +00:00
|
|
|
|
2016-04-27 03:17:05 +00:00
|
|
|
function remove (doc) {
|
2016-06-18 21:02:27 +00:00
|
|
|
delete db.entities[doc.type][doc._id];
|
2016-04-29 03:37:49 +00:00
|
|
|
|
|
|
|
// Also remove children
|
2016-05-11 05:43:51 +00:00
|
|
|
TYPES.map(type => removeWhere(type, 'parentId', doc._id));
|
2016-06-18 21:02:27 +00:00
|
|
|
|
2016-05-11 05:43:51 +00:00
|
|
|
Object.keys(changeListeners).map(k => changeListeners[k]('remove', doc));
|
2016-07-08 06:30:26 +00:00
|
|
|
new Promise(resolve => resolve(doc));
|
2016-04-16 23:24:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-04-17 20:35:35 +00:00
|
|
|
// ~~~~~~~~~~~~~~~~~~~ //
|
|
|
|
// DEFAULT MODEL STUFF //
|
|
|
|
// ~~~~~~~~~~~~~~~~~~~ //
|
2016-04-16 23:24:57 +00:00
|
|
|
|
2016-04-27 03:17:05 +00:00
|
|
|
function docUpdate (originalDoc, patch = {}) {
|
|
|
|
const doc = Object.assign(
|
|
|
|
{},
|
|
|
|
originalDoc,
|
|
|
|
patch,
|
|
|
|
{modified: Date.now()}
|
|
|
|
);
|
|
|
|
|
|
|
|
// Fake a promise
|
|
|
|
const finalDoc = update(doc);
|
|
|
|
return new Promise(resolve => resolve(finalDoc));
|
|
|
|
}
|
|
|
|
|
|
|
|
function docCreate (type, idPrefix, defaults, patch = {}) {
|
2016-04-26 07:29:24 +00:00
|
|
|
const baseDefaults = {
|
|
|
|
parentId: null
|
|
|
|
};
|
|
|
|
|
|
|
|
const doc = Object.assign(
|
|
|
|
baseDefaults,
|
2016-04-17 20:35:35 +00:00
|
|
|
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-27 03:17:05 +00:00
|
|
|
$loki: undefined,
|
|
|
|
meta: 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-27 03:17:05 +00:00
|
|
|
// Fake a promise
|
|
|
|
return insert(doc);
|
2016-04-17 20:35:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ~~~~~~~ //
|
|
|
|
// REQUEST //
|
|
|
|
// ~~~~~~~ //
|
|
|
|
|
2016-07-06 20:18:26 +00:00
|
|
|
export function requestCreateAndActivate (workspace, patch = {}) {
|
|
|
|
return requestCreate(patch).then(r => {
|
|
|
|
workspaceUpdate(workspace, {activeRequestId: r._id});
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export function requestCopyAndActivate (workspace, request) {
|
|
|
|
return requestCopy(request).then(r => {
|
|
|
|
workspaceUpdate(workspace, {activeRequestId: r._id});
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-04-17 20:35:35 +00:00
|
|
|
export function requestCreate (patch = {}) {
|
2016-04-27 03:17:05 +00:00
|
|
|
return docCreate(TYPE_REQUEST, 'req', {
|
2016-04-17 20:35:35 +00:00
|
|
|
url: '',
|
|
|
|
name: 'New Request',
|
|
|
|
method: methods.METHOD_GET,
|
2016-05-01 19:56:30 +00:00
|
|
|
previewMode: PREVIEW_MODE_SOURCE,
|
2016-06-19 19:20:05 +00:00
|
|
|
contentType: CONTENT_TYPE_TEXT,
|
2016-04-17 20:35:35 +00:00
|
|
|
body: '',
|
|
|
|
params: [],
|
|
|
|
headers: [],
|
2016-04-26 07:29:24 +00:00
|
|
|
authentication: {}
|
2016-04-17 20:35:35 +00:00
|
|
|
}, patch);
|
2016-04-16 23:24:57 +00:00
|
|
|
}
|
|
|
|
|
2016-04-29 08:15:37 +00:00
|
|
|
export function requestById (id) {
|
|
|
|
return get(TYPE_REQUEST, id);
|
|
|
|
}
|
|
|
|
|
2016-04-27 03:17:05 +00:00
|
|
|
export function requestUpdate (request, patch) {
|
|
|
|
return docUpdate(request, patch);
|
|
|
|
}
|
|
|
|
|
2016-04-26 07:29:24 +00:00
|
|
|
export function requestCopy (request) {
|
|
|
|
const name = `${request.name} (Copy)`;
|
|
|
|
return requestCreate(Object.assign({}, request, {name}));
|
2016-04-16 23:24:57 +00:00
|
|
|
}
|
|
|
|
|
2016-04-27 03:17:05 +00:00
|
|
|
export function requestRemove (request) {
|
|
|
|
return remove(request);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function requestAll () {
|
2016-05-11 05:43:51 +00:00
|
|
|
return all(TYPE_REQUEST);
|
2016-04-27 03:17:05 +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-27 03:17:05 +00:00
|
|
|
return docCreate(TYPE_REQUEST_GROUP, 'grp', {
|
2016-04-17 20:35:35 +00:00
|
|
|
collapsed: false,
|
|
|
|
name: 'New Request Group',
|
2016-04-26 07:29:24 +00:00
|
|
|
environment: {}
|
2016-04-17 20:35:35 +00:00
|
|
|
}, patch);
|
|
|
|
}
|
2016-04-16 23:24:57 +00:00
|
|
|
|
2016-04-27 03:17:05 +00:00
|
|
|
export function requestGroupUpdate (requestGroup, patch) {
|
|
|
|
return docUpdate(requestGroup, patch);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function requestGroupById (id) {
|
|
|
|
return get(TYPE_REQUEST_GROUP, id);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function requestGroupRemove (requestGroup) {
|
|
|
|
return remove(requestGroup);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function requestGroupAll () {
|
2016-05-11 05:43:51 +00:00
|
|
|
return all(TYPE_REQUEST_GROUP);
|
2016-04-27 03:17:05 +00:00
|
|
|
}
|
|
|
|
|
2016-04-23 06:08:52 +00:00
|
|
|
// ~~~~~~~~ //
|
2016-04-17 20:35:35 +00:00
|
|
|
// RESPONSE //
|
2016-04-23 06:08:52 +00:00
|
|
|
// ~~~~~~~~ //
|
2016-04-16 23:24:57 +00:00
|
|
|
|
2016-04-17 20:35:35 +00:00
|
|
|
export function responseCreate (patch = {}) {
|
2016-04-27 03:17:05 +00:00
|
|
|
return docCreate(TYPE_RESPONSE, 'res', {
|
2016-04-18 04:39:15 +00:00
|
|
|
statusCode: 0,
|
|
|
|
statusMessage: '',
|
|
|
|
contentType: 'text/plain',
|
2016-06-19 07:21:43 +00:00
|
|
|
url: '',
|
2016-04-18 04:39:15 +00:00
|
|
|
bytes: 0,
|
|
|
|
millis: 0,
|
2016-04-27 03:17:05 +00:00
|
|
|
headers: [],
|
2016-06-18 21:02:27 +00:00
|
|
|
body: '',
|
|
|
|
error: ''
|
2016-04-17 20:35:35 +00:00
|
|
|
}, patch);
|
|
|
|
}
|
2016-04-18 04:39:15 +00:00
|
|
|
|
2016-04-27 03:17:05 +00:00
|
|
|
export function responseAll () {
|
2016-05-11 05:43:51 +00:00
|
|
|
return all(TYPE_RESPONSE);
|
2016-04-27 03:17:05 +00:00
|
|
|
}
|
|
|
|
|
2016-04-23 06:08:52 +00:00
|
|
|
|
|
|
|
// ~~~~~~~~~ //
|
|
|
|
// WORKSPACE //
|
|
|
|
// ~~~~~~~~~ //
|
|
|
|
|
|
|
|
export function workspaceCreate (patch = {}) {
|
2016-04-27 03:17:05 +00:00
|
|
|
return docCreate(TYPE_WORKSPACE, 'wrk', {
|
2016-04-26 07:29:24 +00:00
|
|
|
name: 'New Workspace',
|
|
|
|
activeRequestId: null,
|
2016-07-06 20:18:26 +00:00
|
|
|
environments: [],
|
|
|
|
sidebarWidth: DEFAULT_SIDEBAR_WIDTH,
|
|
|
|
filter: ''
|
2016-04-23 06:08:52 +00:00
|
|
|
}, patch);
|
|
|
|
}
|
2016-04-26 07:29:24 +00:00
|
|
|
|
|
|
|
export function workspaceAll () {
|
2016-05-11 05:43:51 +00:00
|
|
|
return all(TYPE_WORKSPACE).then(workspaces => {
|
2016-04-27 03:17:05 +00:00
|
|
|
if (workspaces.length === 0) {
|
|
|
|
workspaceCreate({name: 'Insomnia'});
|
|
|
|
return workspaceAll();
|
2016-04-26 07:29:24 +00:00
|
|
|
} else {
|
2016-04-27 03:17:05 +00:00
|
|
|
return new Promise(resolve => resolve(workspaces))
|
2016-04-26 07:29:24 +00:00
|
|
|
}
|
2016-04-27 03:17:05 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function workspaceUpdate (workspace, patch) {
|
|
|
|
return docUpdate(workspace, patch);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function workspaceRemove (workspace) {
|
|
|
|
return remove(workspace);
|
2016-04-26 07:29:24 +00:00
|
|
|
}
|