Skip to content

Commit

Permalink
initChatBookDbExec
Browse files Browse the repository at this point in the history
  • Loading branch information
chatbookai committed Jun 1, 2024
1 parent e669341 commit 1b28d3f
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 104 deletions.
6 changes: 3 additions & 3 deletions express/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import isDev from 'electron-is-dev'
import settings from 'electron-settings';

import { server, getPort } from './src/app.js';
import { initChatBookSetting, initChatBookDbExec } from './src/utils/db.js';
import { initChatBookSetting, ChatBookDbInit } from './src/utils/db.js';

const PORT = getPort();

Expand All @@ -26,8 +26,8 @@ function createMainWindow() {
ipcMain.on('start-chatbook', async (event, data) => {
ChatBookSetting = await settings.get('chatbook');
console.log("ChatBookSetting main.js", ChatBookSetting)
await initChatBookSetting(ChatBookSetting);
await initChatBookDbExec();
initChatBookSetting(ChatBookSetting);
ChatBookDbInit();
mainWindow.loadURL('http://localhost:' + PORT);
});

Expand Down
14 changes: 11 additions & 3 deletions express/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import cron from 'node-cron';
import dotenv from 'dotenv';
import { dirname, join } from 'path';

import { initChatBookDbExec } from './utils/db.js';
import { ChatBookDbInit, initChatBookSetting } from './utils/db.js';
import { parseFilesAndWeb, vectorDdProcess } from './utils/llms.js';

//import { downloadVideoFromAPI } from './utils/Backup/stability.js';
Expand All @@ -32,9 +32,17 @@ app.use(cors());
app.use(bodyParser.json());
dotenv.config();


//Initial Database and Folder
initChatBookDbExec()

function isElectron() {
return typeof process !== 'undefined' && !!(process.versions).electron;
}

//Running in express, not in electron
if(isElectron() == false) {
initChatBookSetting({NodeStorageDirectory: './data'})
ChatBookDbInit()
}

//Schedule Task for Parse Upload Files
cron.schedule('*/3 * * * *', () => {
Expand Down
44 changes: 22 additions & 22 deletions express/src/utils/app.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@

import { initChatBookDb } from './db.js'
import { ChatBookDbPool } from './db.js'
import { filterString, log, getNanoid } from './utils.js'

export async function addApp(Params) {
const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb()
const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool()

try{
console.log("ParamsParamsParamsParamsParams", Params)
Expand Down Expand Up @@ -32,7 +32,7 @@ import { filterString, log, getNanoid } from './utils.js'
}

export async function editApp(Params) {
const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb()
const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool()

try{
Params._id = filterString(Params._id)
Expand Down Expand Up @@ -60,7 +60,7 @@ import { filterString, log, getNanoid } from './utils.js'
}

export async function editAppById(Params) {
const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb()
const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool()

try{
Params._id = filterString(Params._id)
Expand All @@ -87,7 +87,7 @@ import { filterString, log, getNanoid } from './utils.js'
}

export async function deleteApp(Params) {
const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb()
const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool()

try{
Params._id = filterString(Params.appId)
Expand All @@ -105,7 +105,7 @@ import { filterString, log, getNanoid } from './utils.js'
}

export async function deleteAppById(Params) {
const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb()
const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool()

try{
Params._id = filterString(Params._id)
Expand All @@ -123,7 +123,7 @@ import { filterString, log, getNanoid } from './utils.js'
}

export async function getApp(id, userId) {
const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb()
const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool()

const idFilter = filterString(id)

Expand All @@ -141,7 +141,7 @@ import { filterString, log, getNanoid } from './utils.js'
}

export async function getAppById(_id, id) {
const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb()
const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool()

const _idFilter = filterString(_id)
const idFilter = filterString(id)
Expand All @@ -160,7 +160,7 @@ import { filterString, log, getNanoid } from './utils.js'
}

export async function getAppByPublishId(id) {
const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb()
const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool()

const idFilter = filterString(id)

Expand All @@ -183,7 +183,7 @@ import { filterString, log, getNanoid } from './utils.js'
}

export async function getAppPage(pageid, pagesize, userId) {
const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb()
const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool()

const pageidFiler = Number(pageid) < 0 ? 0 : pageid;
const pagesizeFiler = Number(pagesize) < 5 ? 5 : pagesize;
Expand All @@ -207,7 +207,7 @@ import { filterString, log, getNanoid } from './utils.js'
}

export async function getAppPageAll(pageid, pagesize, data) {
const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb()
const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool()

const pageidFiler = Number(pageid) < 0 ? 0 : pageid;
const pagesizeFiler = Number(pagesize) < 5 ? 5 : pagesize;
Expand Down Expand Up @@ -251,7 +251,7 @@ import { filterString, log, getNanoid } from './utils.js'
}

export async function getChatlogPageByApp(appId, pageid, pagesize, userId) {
const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb()
const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool()

const appIdFileter = filterString(appId)
const pageidFiler = Number(pageid) < 0 ? 0 : pageid;
Expand All @@ -276,7 +276,7 @@ import { filterString, log, getNanoid } from './utils.js'
}

export async function getChatlogStaticPageByApp(appId, pageid, pagesize) {
const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb()
const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool()

const appIdFileter = filterString(appId)
const pageidFiler = Number(pageid) < 0 ? 0 : pageid;
Expand Down Expand Up @@ -314,7 +314,7 @@ import { filterString, log, getNanoid } from './utils.js'
}

export async function getPublishsPageByApp(appId, pageid, pagesize, userId) {
const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb()
const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool()

const appIdFileter = filterString(appId)
const pageidFiler = Number(pageid) < 0 ? 0 : pageid;
Expand All @@ -339,7 +339,7 @@ import { filterString, log, getNanoid } from './utils.js'
}

export async function getPublishsAll(pageid, pagesize) {
const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb()
const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool()

const pageidFiler = Number(pageid) < 0 ? 0 : pageid;
const pagesizeFiler = Number(pagesize) < 5 ? 5 : pagesize;
Expand All @@ -366,7 +366,7 @@ import { filterString, log, getNanoid } from './utils.js'
}

export async function addPublish(Params) {
const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb()
const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool()

try{
Params._id = getNanoid(32)
Expand Down Expand Up @@ -404,7 +404,7 @@ import { filterString, log, getNanoid } from './utils.js'
}

export async function editPublish(Params) {
const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb()
const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool()

try{
Params._id = filterString(Params._id)
Expand All @@ -431,7 +431,7 @@ import { filterString, log, getNanoid } from './utils.js'
}

export async function deletePublish(Params) {
const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb()
const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool()

try{
Params._id = filterString(Params._id)
Expand All @@ -451,7 +451,7 @@ import { filterString, log, getNanoid } from './utils.js'
}

export async function getPublish(id, userId) {
const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb()
const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool()

const idFilter = filterString(id)
const userIdFilter = Number(userId)
Expand All @@ -469,7 +469,7 @@ import { filterString, log, getNanoid } from './utils.js'
}

export async function getChatLogByAppIdAndUserId(appId, userId, pageid, pagesize) {
const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb()
const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool()

const appIdFiler = filterString(appId);
const userIdFiler = filterString(userId);
Expand All @@ -495,7 +495,7 @@ import { filterString, log, getNanoid } from './utils.js'
}

export async function deleteUserLogByAppId(appId, userId) {
const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb()
const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool()

const UpdateChatLog = db.prepare("update chatlog set current = 0 where appId = ? and userId = ?");
UpdateChatLog.run(appId, userId);
Expand All @@ -505,7 +505,7 @@ import { filterString, log, getNanoid } from './utils.js'
}

export async function deleteUserLogByChatlogId(appId, userId, id) {
const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb()
const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool()

const UpdateChatLog = db.prepare("delete from chatlog where appId = ? and userId = ? and _id = ?");
UpdateChatLog.run(appId, userId, id);
Expand Down
26 changes: 13 additions & 13 deletions express/src/utils/dataset.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@

import { initChatBookDb } from './db.js'
import { ChatBookDbPool } from './db.js'
import { filterString, log, getNanoid } from './utils.js'


export async function addDataset(Params) {
const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb()
const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool()

try{
Params._id = filterString(Params._id)
Expand All @@ -29,7 +29,7 @@ import { filterString, log, getNanoid } from './utils.js'
}

export async function editDataset(Params) {
const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb()
const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool()

try{
Params._id = filterString(Params._id)
Expand All @@ -54,7 +54,7 @@ import { filterString, log, getNanoid } from './utils.js'
}

export async function deleteDataset(Params) {
const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb()
const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool()

try{
Params._id = filterString(Params.datasetId)
Expand All @@ -72,7 +72,7 @@ import { filterString, log, getNanoid } from './utils.js'
}

export async function getDataset(id, userId) {
const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb()
const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool()

const idFilter = filterString(id)
const userIdFilter = Number(userId)
Expand All @@ -90,7 +90,7 @@ import { filterString, log, getNanoid } from './utils.js'
}

export async function getDatasetPage(pageid, pagesize, userId) {
const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb()
const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool()

const pageidFiler = Number(pageid) < 0 ? 0 : pageid;
const pagesizeFiler = Number(pagesize) < 5 ? 5 : pagesize;
Expand All @@ -114,7 +114,7 @@ import { filterString, log, getNanoid } from './utils.js'
}

export async function getCollectionPageByDataset(datasetId, pageid, pagesize, userId) {
const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb()
const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool()

const datasetIdFileter = filterString(datasetId)
const pageidFiler = Number(pageid) < 0 ? 0 : pageid;
Expand All @@ -139,7 +139,7 @@ import { filterString, log, getNanoid } from './utils.js'
}

export async function getCollectionAll(pageid, pagesize) {
const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb()
const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool()

const pageidFiler = Number(pageid) < 0 ? 0 : pageid;
const pagesizeFiler = Number(pagesize) < 5 ? 5 : pagesize;
Expand All @@ -166,7 +166,7 @@ import { filterString, log, getNanoid } from './utils.js'
}

export async function addCollection(Params) {
const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb()
const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool()

try{
Params._id = getNanoid(32)
Expand Down Expand Up @@ -202,7 +202,7 @@ import { filterString, log, getNanoid } from './utils.js'
}

export async function editCollection(Params) {
const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb()
const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool()

try{
Params._id = filterString(Params._id)
Expand All @@ -227,7 +227,7 @@ import { filterString, log, getNanoid } from './utils.js'
}

export async function uploadCollection(Params) {
const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb()
const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool()

try{
const type = filterString(Params.type)
Expand Down Expand Up @@ -282,7 +282,7 @@ import { filterString, log, getNanoid } from './utils.js'
}

export async function deleteCollection(Params) {
const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb()
const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool()

try{
Params._id = filterString(Params._id)
Expand All @@ -301,7 +301,7 @@ import { filterString, log, getNanoid } from './utils.js'
}

export async function getCollection(id, userId) {
const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb()
const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool()

const idFilter = filterString(id)
const userIdFilter = Number(userId)
Expand Down
Loading

0 comments on commit 1b28d3f

Please sign in to comment.