New database function to get all tages. New database function to get like tags. Added those changes to electron IPC. Changed list to support all image formats possible. Added debounce lib for events. Working on new timechain tags view.
199 lines
4.8 KiB
JavaScript
199 lines
4.8 KiB
JavaScript
const { ipcMain } = require('electron')
|
|
const { app } = require('electron')
|
|
const path = require('path')
|
|
const fs = require('fs')
|
|
// const config = app.getPath('userData');
|
|
|
|
const Config = require('electron-config')
|
|
const config = new Config()
|
|
|
|
let DBPath = config.get('database.path')
|
|
if (!DBPath) {
|
|
DBPathDir = app.getPath('userData')
|
|
DBPath = path.join(DBPathDir, '/timechain.db')
|
|
config.set('database.path', DBPath)
|
|
}
|
|
|
|
const { TimeChainDataSqliteRecord, ConnectToDatabase, TimeChainDataSqliteFile, TimeChainDataSqliteTag, TimeChainDataSqliteTagLink } = require('./sqlite')
|
|
|
|
console.log(DBPath)
|
|
|
|
const DB = ConnectToDatabase(DBPath)
|
|
|
|
const DbRecord = new TimeChainDataSqliteRecord()
|
|
const DbFile = new TimeChainDataSqliteFile()
|
|
const DbTag = new TimeChainDataSqliteTag()
|
|
const DbTagLink = new TimeChainDataSqliteTagLink()
|
|
|
|
// ** Extra IPC Functions
|
|
|
|
ipcMain.handle('select-sqlite-file', (event, arg) => {
|
|
const { dialog } = require('electron')
|
|
const files = dialog.showOpenDialog({
|
|
filters: [{
|
|
name: 'Sqlite File',
|
|
extensions: ['db', 'db3']
|
|
}]
|
|
})
|
|
|
|
if (!files) return false
|
|
return files
|
|
})
|
|
|
|
ipcMain.handle('create-sqlite-file', (event, arg) => {
|
|
const { dialog } = require('electron')
|
|
const files = dialog.showSaveDialogSync({
|
|
filters: [{
|
|
name: 'Sqlite File',
|
|
extensions: ['db', 'db3']
|
|
}]
|
|
})
|
|
|
|
if (!files) return false
|
|
return files
|
|
})
|
|
|
|
/**
|
|
* Allows the UI to select another database file
|
|
*/
|
|
ipcMain.handle('timechain-database-open', (event, arg) => {
|
|
if (!arg.filename) return false
|
|
if (!fs.existsSync(arg.filename)) return false
|
|
config.set('database.path', arg.filename)
|
|
app.relaunch()
|
|
app.quit()
|
|
// DB = ConnectToDatabase(arg.filename)
|
|
return true
|
|
})
|
|
|
|
/**
|
|
* Allows the UI to create a new database instance
|
|
*/
|
|
ipcMain.handle('timechain-database-create', (event, arg) => {
|
|
if (!arg.filename) return false
|
|
console.log('database.path', arg.filename)
|
|
config.set('database.path', arg.filename)
|
|
app.relaunch()
|
|
app.quit()
|
|
// DB = ConnectToDatabase(arg.filename)
|
|
return true
|
|
})
|
|
|
|
// ** RECORD **
|
|
console.log('Register timechain-record')
|
|
ipcMain.handle('timechain-record', async (event, arg) => {
|
|
let res = null
|
|
|
|
switch (arg.func) {
|
|
case 'add':
|
|
res = await DbRecord.add(arg.uuid, arg.timestamp, arg.content, arg.mime, arg.hash)
|
|
break
|
|
case 'delete':
|
|
res = await DbRecord.delete(arg.uuid)
|
|
break
|
|
case 'update':
|
|
res = await DbRecord.update(arg.uuid, arg.content, arg.mime, arg.hash)
|
|
break
|
|
case 'find':
|
|
res = await DbRecord.find(arg.search, arg.sort, arg.limit, arg.offset)
|
|
break
|
|
case 'get':
|
|
res = await DbRecord.get(arg.uuid)
|
|
break
|
|
default:
|
|
res = new Error('unknown command')
|
|
}
|
|
|
|
return res
|
|
})
|
|
|
|
// ** FILE **
|
|
|
|
ipcMain.handle('timechain-file', async (event, arg) => {
|
|
let res = null
|
|
|
|
switch (arg.func) {
|
|
case 'add':
|
|
res = await DbFile.add(arg.uuid_record, arg.uuid, arg.timestamp, arg.content, arg.mime, arg.hash)
|
|
break
|
|
case 'update':
|
|
res = await DbFile.update(arg.uuid, arg.timestamp, arg.content, arg.mime, arg.hash)
|
|
break
|
|
case 'get-record':
|
|
res = await DbFile.getByRecord(arg.uuid_record)
|
|
break
|
|
case 'delete-record':
|
|
res = await DbFile.deleteRecord(arg.uuid_record)
|
|
break
|
|
case 'get':
|
|
res = await DbFile.get(arg.uuid)
|
|
break
|
|
case 'delete':
|
|
res = await DbFile.delete(arg.uuid)
|
|
break
|
|
default:
|
|
res = new Error('Unknow a command')
|
|
}
|
|
|
|
return res
|
|
})
|
|
|
|
// ** TAG **
|
|
|
|
ipcMain.handle('timechain-tag', async (event, arg) => {
|
|
let res = null
|
|
|
|
switch (arg.func) {
|
|
case 'add':
|
|
res = await DbTag.add(arg.tag)
|
|
break
|
|
case 'delete':
|
|
res = await DbTag.delete(arg.tag)
|
|
break
|
|
case 'has':
|
|
res = await DbTag.delete(arg.tag)
|
|
break
|
|
case 'all':
|
|
res = await DbTag.all()
|
|
break;
|
|
case 'like':
|
|
res = await DbTag.like(arg.str)
|
|
break;
|
|
default:
|
|
res = new Error('Command Unknown')
|
|
}
|
|
|
|
return res
|
|
})
|
|
|
|
// TAG LINK
|
|
|
|
ipcMain.handle('timechain-taglink', async (event, arg) => {
|
|
let res = null
|
|
|
|
switch (arg.func) {
|
|
case 'add':
|
|
res = await DbTagLink.add(arg.uuid, arg.tag)
|
|
break
|
|
case 'delete':
|
|
res = await DbTagLink.delete(arg.uuid, arg.tag)
|
|
break
|
|
case 'delete-tag':
|
|
res = await DbTagLink.deleteTag(arg.tag)
|
|
break
|
|
case 'delete-record':
|
|
res = await DbTagLink.deleteRecord(arg.uuid)
|
|
break
|
|
case 'get-records':
|
|
res = await DbTagLink.getRecords(atg.tag)
|
|
break
|
|
case 'get-tags':
|
|
res = await DbTagLink.getTags(atg.uuid)
|
|
break
|
|
default:
|
|
res = Error('Commande not known')
|
|
}
|
|
|
|
return res
|
|
})
|