2017-06-14 19:59:02 +00:00
|
|
|
import fs from 'fs-extra';
|
|
|
|
import { Database } from 'src/database.js';
|
|
|
|
import { DatabaseDriverNode } from 'src/database-driver-node.js';
|
|
|
|
import { BaseModel } from 'src/base-model.js';
|
2017-06-18 20:19:13 +00:00
|
|
|
import { Folder } from 'src/models/folder.js';
|
|
|
|
import { Note } from 'src/models/note.js';
|
|
|
|
import { BaseItem } from 'src/models/base-item.js';
|
2017-06-14 19:59:02 +00:00
|
|
|
import { Synchronizer } from 'src/synchronizer.js';
|
|
|
|
import { FileApi } from 'src/file-api.js';
|
|
|
|
import { FileApiDriverMemory } from 'src/file-api-driver-memory.js';
|
|
|
|
|
2017-06-18 20:19:13 +00:00
|
|
|
let databases_ = [];
|
|
|
|
let synchronizers_ = [];
|
2017-06-14 19:59:02 +00:00
|
|
|
let fileApi_ = null;
|
2017-06-18 20:19:13 +00:00
|
|
|
let currentClient_ = 1;
|
2017-06-14 19:59:02 +00:00
|
|
|
|
2017-06-18 20:19:13 +00:00
|
|
|
function sleep(n) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
setTimeout(() => {
|
|
|
|
resolve();
|
|
|
|
}, n * 1000);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function switchClient(id) {
|
|
|
|
currentClient_ = id;
|
|
|
|
BaseModel.db_ = databases_[id];
|
|
|
|
Folder.db_ = databases_[id];
|
|
|
|
Note.db_ = databases_[id];
|
|
|
|
BaseItem.db_ = databases_[id];
|
|
|
|
}
|
|
|
|
|
|
|
|
function clearDatabase(id = null) {
|
|
|
|
if (id === null) id = currentClient_;
|
|
|
|
|
|
|
|
let queries = [
|
|
|
|
'DELETE FROM changes',
|
|
|
|
'DELETE FROM notes',
|
|
|
|
'DELETE FROM folders',
|
|
|
|
'DELETE FROM item_sync_times',
|
|
|
|
];
|
|
|
|
|
|
|
|
return databases_[id].transactionExecBatch(queries);
|
|
|
|
}
|
|
|
|
|
|
|
|
function setupDatabase(id = null) {
|
|
|
|
if (id === null) id = currentClient_;
|
|
|
|
|
|
|
|
if (databases_[id]) {
|
|
|
|
return clearDatabase(id);
|
2017-06-14 19:59:02 +00:00
|
|
|
}
|
|
|
|
|
2017-06-18 20:19:13 +00:00
|
|
|
const filePath = __dirname + '/data/test-' + id + '.sqlite';
|
2017-06-14 19:59:02 +00:00
|
|
|
return fs.unlink(filePath).catch(() => {
|
|
|
|
// Don't care if the file doesn't exist
|
|
|
|
}).then(() => {
|
2017-06-18 20:19:13 +00:00
|
|
|
databases_[id] = new Database(new DatabaseDriverNode());
|
|
|
|
databases_[id].setDebugEnabled(false);
|
|
|
|
return databases_[id].open({ name: filePath }).then(() => {
|
|
|
|
BaseModel.db_ = databases_[id];
|
|
|
|
return setupDatabase(id);
|
2017-06-14 19:59:02 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-06-18 20:19:13 +00:00
|
|
|
async function setupDatabaseAndSynchronizer(id = null) {
|
|
|
|
if (id === null) id = currentClient_;
|
2017-06-17 23:49:52 +00:00
|
|
|
|
2017-06-18 20:19:13 +00:00
|
|
|
await setupDatabase(id);
|
|
|
|
|
|
|
|
if (!synchronizers_[id]) {
|
|
|
|
synchronizers_[id] = new Synchronizer(db(id), fileApi());
|
2017-06-17 23:49:52 +00:00
|
|
|
}
|
2017-06-18 20:19:13 +00:00
|
|
|
|
|
|
|
await fileApi().format();
|
2017-06-14 19:59:02 +00:00
|
|
|
}
|
|
|
|
|
2017-06-18 20:19:13 +00:00
|
|
|
function db(id = null) {
|
|
|
|
if (id === null) id = currentClient_;
|
|
|
|
return databases_[id];
|
2017-06-14 19:59:02 +00:00
|
|
|
}
|
|
|
|
|
2017-06-18 20:19:13 +00:00
|
|
|
function synchronizer(id = null) {
|
|
|
|
if (id === null) id = currentClient_;
|
|
|
|
console.info('SYNC', id);
|
|
|
|
return synchronizers_[id];
|
2017-06-14 19:59:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function fileApi() {
|
2017-06-18 20:19:13 +00:00
|
|
|
if (fileApi_) return fileApi_;
|
|
|
|
|
|
|
|
fileApi_ = new FileApi('/root', new FileApiDriverMemory());
|
2017-06-14 19:59:02 +00:00
|
|
|
return fileApi_;
|
|
|
|
}
|
|
|
|
|
2017-06-18 20:19:13 +00:00
|
|
|
export { setupDatabase, setupDatabaseAndSynchronizer, db, synchronizer, fileApi, sleep, clearDatabase, switchClient };
|