2016-10-21 17:20:36 +00:00
|
|
|
import electron from 'electron';
|
|
|
|
import NeDB from 'nedb';
|
|
|
|
import fsPath from 'path';
|
|
|
|
import crypto from 'crypto';
|
2016-11-10 02:40:53 +00:00
|
|
|
import * as util from '../common/misc';
|
2016-10-21 17:20:36 +00:00
|
|
|
|
2016-10-21 21:51:41 +00:00
|
|
|
const TYPE_RESOURCE = 'Resource';
|
2016-10-24 23:30:37 +00:00
|
|
|
const TYPE_CONFIG = 'Config';
|
|
|
|
|
|
|
|
export const SYNC_MODE_OFF = 'paused';
|
|
|
|
export const SYNC_MODE_ON = 'active';
|
|
|
|
|
2016-10-28 21:27:05 +00:00
|
|
|
export function allActiveResources (resourceGroupId = null) {
|
|
|
|
if (resourceGroupId) {
|
|
|
|
return findActiveResources({resourceGroupId});
|
|
|
|
} else {
|
|
|
|
return findActiveResources({});
|
|
|
|
}
|
2016-10-24 23:30:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function activeResourcesForResourceGroup (resourceGroupId) {
|
|
|
|
return findActiveResources({resourceGroupId});
|
|
|
|
}
|
2016-10-21 21:51:41 +00:00
|
|
|
|
2016-10-21 17:20:36 +00:00
|
|
|
export function allResources () {
|
2016-10-24 23:30:37 +00:00
|
|
|
return findResources({});
|
2016-10-21 17:20:36 +00:00
|
|
|
}
|
|
|
|
|
2016-10-24 23:30:37 +00:00
|
|
|
export async function findResources (query) {
|
2016-11-07 20:24:38 +00:00
|
|
|
return _execDB(TYPE_RESOURCE, 'find', query);
|
2016-10-21 17:20:36 +00:00
|
|
|
}
|
|
|
|
|
2016-10-24 23:30:37 +00:00
|
|
|
export async function findActiveResources (query) {
|
|
|
|
const configs = await findActiveConfigs();
|
|
|
|
const resourceGroupIds = configs.map(c => c.resourceGroupId);
|
|
|
|
return findResources(Object.assign({resourceGroupId: {$in: resourceGroupIds}}, query));
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function findActiveDirtyResources () {
|
|
|
|
return findActiveResources({dirty: true});
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function findActiveDirtyResourcesForResourceGroup (resourceGroupId) {
|
|
|
|
return findActiveResources({dirty: true, resourceGroupId});
|
|
|
|
}
|
|
|
|
|
2016-11-16 17:18:39 +00:00
|
|
|
export async function findDirtyResourcesForResourceGroup (resourceGroupId) {
|
|
|
|
return findResources({dirty: true, resourceGroupId});
|
2016-11-07 20:24:38 +00:00
|
|
|
}
|
|
|
|
|
2016-10-24 23:30:37 +00:00
|
|
|
export async function findResourcesForResourceGroup (resourceGroupId) {
|
|
|
|
return findResources({resourceGroupId});
|
2016-10-21 17:20:36 +00:00
|
|
|
}
|
|
|
|
|
2016-11-07 23:36:40 +00:00
|
|
|
export async function getResourceByDocId (id, resourceGroupId = null) {
|
|
|
|
let query;
|
|
|
|
if (resourceGroupId) {
|
|
|
|
query = {id, resourceGroupId};
|
|
|
|
} else {
|
|
|
|
query = {id};
|
|
|
|
}
|
|
|
|
|
|
|
|
const rawDocs = await _execDB(TYPE_RESOURCE, 'find', query);
|
2016-10-21 20:00:31 +00:00
|
|
|
return rawDocs.length >= 1 ? rawDocs[0] : null;
|
2016-10-21 17:20:36 +00:00
|
|
|
}
|
|
|
|
|
2016-10-24 23:30:37 +00:00
|
|
|
export async function insertResource (resource) {
|
2016-10-21 20:00:31 +00:00
|
|
|
const h = crypto.createHash('md5');
|
|
|
|
h.update(resource.resourceGroupId);
|
|
|
|
h.update(resource.id);
|
|
|
|
const newResource = Object.assign({}, resource, {_id: `rs_${h.digest('hex')}`});
|
2016-11-07 20:24:38 +00:00
|
|
|
await _execDB(TYPE_RESOURCE, 'insert', newResource);
|
2016-10-24 23:30:37 +00:00
|
|
|
return newResource;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function updateResource (resource, ...patches) {
|
|
|
|
const newDoc = Object.assign(resource, ...patches);
|
2016-11-07 20:24:38 +00:00
|
|
|
await _execDB(TYPE_RESOURCE, 'update', {_id: resource._id}, newDoc);
|
2016-10-24 23:30:37 +00:00
|
|
|
return newDoc
|
2016-10-21 17:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function removeResource (resource) {
|
2016-11-07 20:24:38 +00:00
|
|
|
return _execDB(TYPE_RESOURCE, 'remove', {_id: resource._id});
|
2016-10-21 21:51:41 +00:00
|
|
|
}
|
|
|
|
|
2016-10-24 23:30:37 +00:00
|
|
|
// ~~~~~~ //
|
|
|
|
// Config //
|
|
|
|
// ~~~~~~ //
|
|
|
|
|
|
|
|
export function findConfigs (query) {
|
2016-11-07 20:24:38 +00:00
|
|
|
return _execDB(TYPE_CONFIG, 'find', query)
|
2016-10-24 23:30:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function allConfigs () {
|
|
|
|
return findConfigs({})
|
|
|
|
}
|
|
|
|
|
2016-10-28 17:51:08 +00:00
|
|
|
export function findInactiveConfigs (excludedResourceGroupId = null) {
|
|
|
|
if (excludedResourceGroupId) {
|
2016-11-07 20:24:38 +00:00
|
|
|
return findConfigs({
|
|
|
|
syncMode: SYNC_MODE_OFF,
|
|
|
|
$not: {excludedResourceGroupId}
|
|
|
|
})
|
2016-10-24 23:30:37 +00:00
|
|
|
} else {
|
|
|
|
return findConfigs({syncMode: SYNC_MODE_OFF})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function findActiveConfigs (resourceGroupId = null) {
|
|
|
|
if (resourceGroupId) {
|
|
|
|
return findConfigs({syncMode: SYNC_MODE_ON, resourceGroupId})
|
|
|
|
} else {
|
|
|
|
return findConfigs({syncMode: SYNC_MODE_ON})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getConfig (resourceGroupId) {
|
2016-11-07 20:24:38 +00:00
|
|
|
const rawDocs = await _execDB(TYPE_CONFIG, 'find', {resourceGroupId});
|
2016-10-24 23:30:37 +00:00
|
|
|
return rawDocs.length >= 1 ? _initConfig(rawDocs[0]) : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function updateConfig (config, ...patches) {
|
|
|
|
const doc = _initConfig(Object.assign(config, ...patches));
|
2016-11-07 20:24:38 +00:00
|
|
|
await _execDB(TYPE_CONFIG, 'update', {_id: doc._id}, doc);
|
2016-10-24 23:30:37 +00:00
|
|
|
return doc;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function removeConfig (config) {
|
2016-11-07 20:24:38 +00:00
|
|
|
return _execDB(TYPE_CONFIG, 'remove', {_id: config._id});
|
2016-10-24 23:30:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function insertConfig (config) {
|
|
|
|
const doc = _initConfig(config);
|
2016-11-07 20:24:38 +00:00
|
|
|
await _execDB(TYPE_CONFIG, 'insert', doc);
|
2016-10-24 23:30:37 +00:00
|
|
|
return doc;
|
|
|
|
}
|
|
|
|
|
|
|
|
function _initConfig (data) {
|
|
|
|
return Object.assign({
|
|
|
|
_id: util.generateId('scf'),
|
2016-11-12 08:38:55 +00:00
|
|
|
syncMode: SYNC_MODE_ON,
|
2016-10-24 23:30:37 +00:00
|
|
|
resourceGroupId: null
|
|
|
|
}, data);
|
2016-10-21 17:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ~~~~~~~ //
|
|
|
|
// Helpers //
|
|
|
|
// ~~~~~~~ //
|
|
|
|
|
|
|
|
let _database = null;
|
2016-10-21 21:51:41 +00:00
|
|
|
function _getDB (type) {
|
2016-10-21 17:20:36 +00:00
|
|
|
if (!_database) {
|
|
|
|
const basePath = electron.remote.app.getPath('userData');
|
2016-10-21 21:51:41 +00:00
|
|
|
_database = {};
|
|
|
|
|
|
|
|
// NOTE: Do not EVER change this. EVER!
|
|
|
|
const resourcePath = fsPath.join(basePath, 'sync/Resource.db');
|
2016-10-24 23:30:37 +00:00
|
|
|
const configPath = fsPath.join(basePath, 'sync/Config.db');
|
2016-10-21 17:20:36 +00:00
|
|
|
|
|
|
|
// Fill in the defaults
|
2016-10-21 21:51:41 +00:00
|
|
|
_database['Resource'] = new NeDB({filename: resourcePath, autoload: true});
|
2016-10-24 23:30:37 +00:00
|
|
|
_database['Config'] = new NeDB({
|
|
|
|
filename: configPath,
|
|
|
|
autoload: true
|
|
|
|
});
|
2016-10-21 17:20:36 +00:00
|
|
|
|
|
|
|
// Done
|
2016-10-21 21:51:41 +00:00
|
|
|
console.log(`-- Initialize Sync DB at ${basePath} --`);
|
2016-10-21 17:20:36 +00:00
|
|
|
}
|
|
|
|
|
2016-10-21 21:51:41 +00:00
|
|
|
return _database[type];
|
2016-10-21 17:20:36 +00:00
|
|
|
}
|
2016-10-21 20:00:31 +00:00
|
|
|
|
2016-11-07 20:24:38 +00:00
|
|
|
function _execDB (type, fnName, ...args) {
|
2016-10-21 20:00:31 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2016-11-07 20:24:38 +00:00
|
|
|
_getDB(type)[fnName](...args, (err, data) => {
|
|
|
|
err ? reject(err) : resolve(data);
|
2016-10-21 20:00:31 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|