2016-04-27 23:33:38 +00:00
|
|
|
import * as fsPath from 'path'
|
|
|
|
import electron from 'electron'
|
2016-04-29 08:15:37 +00:00
|
|
|
import Loki from 'lokijs'
|
2016-04-27 23:33:38 +00:00
|
|
|
|
2016-04-28 07:41:31 +00:00
|
|
|
import * as methods from '../lib/constants'
|
2016-04-17 01:52:10 +00:00
|
|
|
import {generateId} from './util'
|
2016-04-29 04:57:03 +00:00
|
|
|
import {CONTENT_TYPE_TEXT} from '../lib/contentTypes'
|
|
|
|
import {PREVIEW_MODE_FRIENDLY} from '../lib/previewModes'
|
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-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();
|
|
|
|
db = new Loki(dbPath, {
|
2016-04-27 03:17:05 +00:00
|
|
|
autoload: true,
|
|
|
|
autosave: true,
|
2016-04-29 08:15:37 +00:00
|
|
|
autosaveInterval: 300, // TODO: do a final save on close
|
2016-04-27 03:17:05 +00:00
|
|
|
persistenceMethod: 'fs',
|
|
|
|
autoloadCallback () {
|
|
|
|
TYPES.map(type => {
|
|
|
|
let collection = db.getCollection(type);
|
|
|
|
if (!collection) {
|
|
|
|
collection = db.addCollection(type, {
|
2016-04-29 08:15:37 +00:00
|
|
|
indices: ['_id'],
|
2016-04-27 03:17:05 +00:00
|
|
|
asyncListeners: false,
|
2016-04-29 08:15:37 +00:00
|
|
|
disableChangesApi: true, // Don't need this yet
|
|
|
|
clone: true, // Clone objects on save
|
|
|
|
transactional: true
|
2016-04-27 03:17:05 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
collection.ensureUniqueIndex('_id');
|
|
|
|
|
|
|
|
console.log(`-- Initialize DB Collection ${type} --`)
|
|
|
|
}
|
|
|
|
|
|
|
|
collection.on('update', doc => {
|
|
|
|
Object.keys(changeListeners).map(k => changeListeners[k]('update', doc));
|
|
|
|
});
|
|
|
|
|
|
|
|
collection.on('insert', doc => {
|
|
|
|
Object.keys(changeListeners).map(k => changeListeners[k]('insert', doc));
|
|
|
|
});
|
|
|
|
|
|
|
|
collection.on('delete', doc => {
|
|
|
|
Object.keys(changeListeners).map(k => changeListeners[k]('delete', doc));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
resolve();
|
|
|
|
initialized = true;
|
2016-04-29 08:15:37 +00:00
|
|
|
console.log(`-- Initialize DB at ${dbPath} --`);
|
2016-04-27 03:17:05 +00:00
|
|
|
}
|
|
|
|
});
|
2016-04-29 08:15:37 +00:00
|
|
|
global.db = db;
|
2016-04-27 03:17:05 +00:00
|
|
|
})
|
|
|
|
}
|
2016-04-16 23:24:57 +00:00
|
|
|
|
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) {
|
|
|
|
const doc = db.getCollection(type).by('_id', id);
|
|
|
|
return new Promise(resolve => resolve(doc));
|
2016-04-26 07:29:24 +00:00
|
|
|
}
|
|
|
|
|
2016-04-27 03:17:05 +00:00
|
|
|
function find (type, query) {
|
|
|
|
const docs = db.getCollection(type).find(query);
|
|
|
|
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) {
|
|
|
|
const newDoc = db.getCollection(doc.type).insert(doc);
|
|
|
|
return new Promise(resolve => resolve(newDoc));
|
2016-04-17 01:52:10 +00:00
|
|
|
}
|
|
|
|
|
2016-04-27 03:17:05 +00:00
|
|
|
function update (doc) {
|
2016-04-29 08:15:37 +00:00
|
|
|
// NOTE: LokiJS only holds references to objects in its DB. This means we need to fetch the
|
|
|
|
// old reference and update it because that's the only way.
|
|
|
|
return get(doc.type, doc._id).then(oldDoc => {
|
|
|
|
Object.assign(oldDoc, doc);
|
|
|
|
db.getCollection(doc.type).update(oldDoc);
|
|
|
|
return new Promise(resolve => resolve(oldDoc));
|
|
|
|
});
|
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-04-29 03:37:49 +00:00
|
|
|
db.getCollection(doc.type).remove(doc);
|
|
|
|
|
|
|
|
// Also remove children
|
|
|
|
TYPES.map(type => db.getCollection(type).removeWhere({parentId: doc._id}));
|
|
|
|
|
|
|
|
new Promise(resolve => resolve());
|
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 //
|
|
|
|
// ~~~~~~~ //
|
|
|
|
|
|
|
|
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-04-26 07:29:24 +00:00
|
|
|
activated: Date.now(),
|
2016-04-29 04:57:03 +00:00
|
|
|
previewMode: PREVIEW_MODE_FRIENDLY,
|
2016-04-30 05:01:57 +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 () {
|
|
|
|
return find(TYPE_REQUEST, {});
|
|
|
|
}
|
|
|
|
|
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 () {
|
|
|
|
return find(TYPE_REQUEST_GROUP, {});
|
|
|
|
}
|
|
|
|
|
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',
|
|
|
|
bytes: 0,
|
|
|
|
millis: 0,
|
2016-04-27 03:17:05 +00:00
|
|
|
headers: [],
|
2016-04-18 04:39:15 +00:00
|
|
|
body: ''
|
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 () {
|
|
|
|
return find(TYPE_RESPONSE, {});
|
|
|
|
}
|
|
|
|
|
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-04-23 06:08:52 +00:00
|
|
|
environments: []
|
|
|
|
}, patch);
|
|
|
|
}
|
2016-04-26 07:29:24 +00:00
|
|
|
|
|
|
|
export function workspaceAll () {
|
2016-04-27 03:17:05 +00:00
|
|
|
return find(TYPE_WORKSPACE, {}).then(workspaces => {
|
|
|
|
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
|
|
|
}
|