insomnia/app/database/index.js

328 lines
7.1 KiB
JavaScript
Raw Normal View History

2016-04-27 23:33:38 +00:00
import electron from 'electron'
2016-05-11 05:43:51 +00:00
import * as fsPath from 'path'
import * as fs from 'fs'
2016-04-27 23:33:38 +00:00
2016-04-28 07:41:31 +00:00
import * as methods from '../lib/constants'
import {generateId} from './util'
2016-05-01 19:56:30 +00:00
import {PREVIEW_MODE_SOURCE} from '../lib/previewModes'
2016-06-19 19:20:05 +00:00
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
export const TYPE_WORKSPACE = 'Workspace';
export const TYPE_REQUEST_GROUP = 'RequestGroup';
export const TYPE_REQUEST = 'Request';
export const TYPE_RESPONSE = 'Response';
const TYPES = [
TYPE_WORKSPACE,
TYPE_REQUEST_GROUP,
TYPE_REQUEST,
TYPE_RESPONSE
];
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
}
/**
* 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 --');
Object.assign(db, JSON.parse(text));
}
2016-05-11 05:43:51 +00:00
// Add listeners to do persistence
2016-06-18 21:02:27 +00:00
2016-05-11 05:43:51 +00:00
let timeout = null;
onChange('DB_WRITER', () => {
clearTimeout(timeout);
timeout = setTimeout(() => {
fs.writeFile(getDBFilePath(), JSON.stringify(db, null, 2), err => {
if (err) {
console.error('Failed to write DB to file', err);
} else {
console.log('-- Persisted DB --');
}
})
}, DB_PERSIST_INTERVAL)
});
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-16 23:24:57 +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];
}
export function get (type, id) {
2016-06-18 21:02:27 +00:00
const doc = db.entities[type][id];
return new Promise(resolve => resolve(doc));
}
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);
}
}
return new Promise(resolve => resolve(docs));
2016-04-16 23:24:57 +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));
}
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));
}
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-04-29 03:37:49 +00:00
new Promise(resolve => resolve());
2016-04-16 23:24:57 +00:00
}
// ~~~~~~~~~~~~~~~~~~~ //
// DEFAULT MODEL STUFF //
// ~~~~~~~~~~~~~~~~~~~ //
2016-04-16 23:24:57 +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 = {}) {
const baseDefaults = {
parentId: null
};
const doc = Object.assign(
baseDefaults,
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),
$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-18 04:39:15 +00:00
// Fake a promise
return insert(doc);
}
// ~~~~~~~ //
// REQUEST //
// ~~~~~~~ //
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});
})
}
export function requestCreate (patch = {}) {
return docCreate(TYPE_REQUEST, 'req', {
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,
body: '',
params: [],
headers: [],
authentication: {}
}, 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);
}
export function requestUpdate (request, patch) {
return docUpdate(request, patch);
}
export function requestCopy (request) {
const name = `${request.name} (Copy)`;
return requestCreate(Object.assign({}, request, {name}));
2016-04-16 23:24:57 +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-16 23:24:57 +00:00
// ~~~~~~~~~~~~~ //
// REQUEST GROUP //
// ~~~~~~~~~~~~~ //
export function requestGroupCreate (patch = {}) {
return docCreate(TYPE_REQUEST_GROUP, 'grp', {
collapsed: false,
name: 'New Request Group',
environment: {}
}, patch);
}
2016-04-16 23:24:57 +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-23 06:08:52 +00:00
// ~~~~~~~~ //
// RESPONSE //
2016-04-23 06:08:52 +00:00
// ~~~~~~~~ //
2016-04-16 23:24:57 +00:00
export function responseCreate (patch = {}) {
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,
headers: [],
2016-06-18 21:02:27 +00:00
body: '',
error: ''
}, patch);
}
2016-04-18 04:39:15 +00:00
export function responseAll () {
2016-05-11 05:43:51 +00:00
return all(TYPE_RESPONSE);
}
2016-04-23 06:08:52 +00:00
// ~~~~~~~~~ //
// WORKSPACE //
// ~~~~~~~~~ //
export function workspaceCreate (patch = {}) {
return docCreate(TYPE_WORKSPACE, 'wrk', {
name: 'New Workspace',
activeRequestId: null,
environments: [],
sidebarWidth: DEFAULT_SIDEBAR_WIDTH,
filter: ''
2016-04-23 06:08:52 +00:00
}, patch);
}
export function workspaceAll () {
2016-05-11 05:43:51 +00:00
return all(TYPE_WORKSPACE).then(workspaces => {
if (workspaces.length === 0) {
workspaceCreate({name: 'Insomnia'});
return workspaceAll();
} else {
return new Promise(resolve => resolve(workspaces))
}
});
}
export function workspaceUpdate (workspace, patch) {
return docUpdate(workspace, patch);
}
export function workspaceRemove (workspace) {
return remove(workspace);
}