Skip to content

Open-source and local-first Slack and Notion alternative that puts you in control of your data

Data flow refactoring

Hakan Shehu5d8ab10ff162
diff --git a/desktop/package-lock.json b/desktop/package-lock.json
index bb57dc12..c6b9b250 100644
--- a/desktop/package-lock.json
+++ b/desktop/package-lock.json
@@ -21,6 +21,7 @@
"@radix-ui/react-toast": "^1.2.1",
"@radix-ui/react-tooltip": "^1.1.2",
"@react-oauth/google": "^0.12.1",
+ "@tanstack/react-query": "^5.51.23",
"@tiptap/core": "^2.5.8",
"@tiptap/extension-blockquote": "^2.5.8",
"@tiptap/extension-bold": "^2.5.8",
@@ -51,7 +52,7 @@
"@tiptap/pm": "^2.5.8",
"@tiptap/react": "^2.5.8",
"@tiptap/suggestion": "^2.5.8",
- "axios": "^1.7.2",
+ "axios": "^1.7.4",
"better-sqlite3": "^11.1.2",
"class-variance-authority": "^0.7.0",
"clsx": "^2.1.1",
@@ -64,16 +65,18 @@
"lowlight": "^3.1.0",
"mobx": "^6.13.1",
"mobx-react-lite": "^4.0.7",
+ "node-sql-parser": "^5.3.1",
"re-resizable": "^6.9.17",
"react": "^18.3.1",
"react-dom": "^18.3.1",
- "react-hook-form": "^7.52.1",
+ "react-hook-form": "^7.52.2",
"react-intersection-observer": "^9.13.0",
- "react-router-dom": "^6.26.0",
- "tailwind-merge": "^2.4.0",
+ "react-router-dom": "^6.26.1",
+ "tailwind-merge": "^2.5.2",
"tailwindcss-animate": "^1.0.7",
- "ts-pattern": "^5.2.0",
+ "ts-pattern": "^5.3.1",
"ulid": "^2.3.0",
+ "ws": "^8.18.0",
"zod": "^3.23.8"
},
"devDependencies": {
@@ -91,19 +94,20 @@
"@types/lodash": "^4.17.7",
"@types/react": "^18.3.3",
"@types/react-dom": "^18.3.0",
+ "@types/ws": "^8.5.12",
"@typescript-eslint/eslint-plugin": "^5.62.0",
"@typescript-eslint/parser": "^5.62.0",
- "autoprefixer": "^10.4.19",
- "electron": "31.3.0",
+ "autoprefixer": "^10.4.20",
+ "electron": "^31.4.0",
"eslint": "^8.57.0",
"eslint-plugin-import": "^2.29.1",
"postcss": "^8.4.40",
"prettier": "^3.3.3",
- "prettier-plugin-tailwindcss": "^0.6.5",
- "tailwindcss": "^3.4.7",
+ "prettier-plugin-tailwindcss": "^0.6.6",
+ "tailwindcss": "^3.4.10",
"ts-node": "^10.9.2",
- "typescript": "~4.5.4",
- "vite": "^5.3.5"
+ "typescript": "^5.5.4",
+ "vite": "^5.4.1"
}
},
"node_modules/@alloc/quick-lru": {
@@ -2442,9 +2446,9 @@
"license": "MIT"
},
"node_modules/@remix-run/router": {
- "version": "1.19.0",
- "resolved": "https://registry.npmjs.org/@remix-run/router/-/router-1.19.0.tgz",
- "integrity": "sha512-zDICCLKEwbVYTS6TjYaWtHXxkdoUvD/QXvyVZjGCsWz5vyH7aFeONlPffPdW+Y/t6KT0MgXb2Mfjun9YpWN1dA==",
+ "version": "1.19.1",
+ "resolved": "https://registry.npmjs.org/@remix-run/router/-/router-1.19.1.tgz",
+ "integrity": "sha512-S45oynt/WH19bHbIXjtli6QmwNYvaz+vtnubvNpNDvUOoA/OWh6j1OikIP3G+v5GHdxyC6EXoChG3HgYGEUfcg==",
"license": "MIT",
"engines": {
"node": ">=14.0.0"
@@ -2700,6 +2704,32 @@
"node": ">=10"
}
},
+ "node_modules/@tanstack/query-core": {
+ "version": "5.51.21",
+ "resolved": "https://registry.npmjs.org/@tanstack/query-core/-/query-core-5.51.21.tgz",
+ "integrity": "sha512-POQxm42IUp6n89kKWF4IZi18v3fxQWFRolvBA6phNVmA8psdfB1MvDnGacCJdS+EOX12w/CyHM62z//rHmYmvw==",
+ "license": "MIT",
+ "funding": {
+ "type": "github",
+ "url": "https://github.com/sponsors/tannerlinsley"
+ }
+ },
+ "node_modules/@tanstack/react-query": {
+ "version": "5.51.23",
+ "resolved": "https://registry.npmjs.org/@tanstack/react-query/-/react-query-5.51.23.tgz",
+ "integrity": "sha512-CfJCfX45nnVIZjQBRYYtvVMIsGgWLKLYC4xcUiYEey671n1alvTZoCBaU9B85O8mF/tx9LPyrI04A6Bs2THv4A==",
+ "license": "MIT",
+ "dependencies": {
+ "@tanstack/query-core": "5.51.21"
+ },
+ "funding": {
+ "type": "github",
+ "url": "https://github.com/sponsors/tannerlinsley"
+ },
+ "peerDependencies": {
+ "react": "^18.0.0"
+ }
+ },
"node_modules/@tiptap/core": {
"version": "2.5.8",
"resolved": "https://registry.npmjs.org/@tiptap/core/-/core-2.5.8.tgz",
@@ -3339,6 +3369,12 @@
"undici-types": "~6.11.1"
}
},
+ "node_modules/@types/pegjs": {
+ "version": "0.10.6",
+ "resolved": "https://registry.npmjs.org/@types/pegjs/-/pegjs-0.10.6.tgz",
+ "integrity": "sha512-eLYXDbZWXh2uxf+w8sXS8d6KSoXTswfps6fvCUuVAGN8eRpfe7h9eSRydxiSJvo9Bf+GzifsDOr9TMQlmJdmkw==",
+ "license": "MIT"
+ },
"node_modules/@types/prop-types": {
"version": "15.7.12",
"resolved": "https://registry.npmjs.org/@types/prop-types/-/prop-types-15.7.12.tgz",
@@ -3396,6 +3432,16 @@
"integrity": "sha512-zFDAD+tlpf2r4asuHEj0XH6pY6i0g5NeAHPn+15wk3BV6JA69eERFXC1gyGThDkVa1zCyKr5jox1+2LbV/AMLg==",
"license": "MIT"
},
+ "node_modules/@types/ws": {
+ "version": "8.5.12",
+ "resolved": "https://registry.npmjs.org/@types/ws/-/ws-8.5.12.tgz",
+ "integrity": "sha512-3tPRkv1EtkDpzlgyKyI8pGsGZAGPEaXeu0DOj5DI25Ja91bdAYddYHbADRYVrZMRbfW+1l5YwXVDKohDJNQxkQ==",
+ "dev": true,
+ "license": "MIT",
+ "dependencies": {
+ "@types/node": "*"
+ }
+ },
"node_modules/@types/yauzl": {
"version": "2.10.3",
"resolved": "https://registry.npmjs.org/@types/yauzl/-/yauzl-2.10.3.tgz",
@@ -4047,9 +4093,9 @@
}
},
"node_modules/autoprefixer": {
- "version": "10.4.19",
- "resolved": "https://registry.npmjs.org/autoprefixer/-/autoprefixer-10.4.19.tgz",
- "integrity": "sha512-BaENR2+zBZ8xXhM4pUaKUxlVdxZ0EZhjvbopwnXmxRUfqDmwSpC2lAi/QXvx7NRdPCo1WKEcEF6mV64si1z4Ew==",
+ "version": "10.4.20",
+ "resolved": "https://registry.npmjs.org/autoprefixer/-/autoprefixer-10.4.20.tgz",
+ "integrity": "sha512-XY25y5xSv/wEoqzDyXXME4AFfkZI0P23z6Fs3YgymDnKJkCGOnkL0iTxCa85UTqaSgfcqyf3UA6+c7wUvx/16g==",
"dev": true,
"funding": [
{
@@ -4067,11 +4113,11 @@
],
"license": "MIT",
"dependencies": {
- "browserslist": "^4.23.0",
- "caniuse-lite": "^1.0.30001599",
+ "browserslist": "^4.23.3",
+ "caniuse-lite": "^1.0.30001646",
"fraction.js": "^4.3.7",
"normalize-range": "^0.1.2",
- "picocolors": "^1.0.0",
+ "picocolors": "^1.0.1",
"postcss-value-parser": "^4.2.0"
},
"bin": {
@@ -4101,9 +4147,9 @@
}
},
"node_modules/axios": {
- "version": "1.7.2",
- "resolved": "https://registry.npmjs.org/axios/-/axios-1.7.2.tgz",
- "integrity": "sha512-2A8QhOMrbomlDuiLeK9XibIBzuHeRcqqNOHp0Cyp5EoJ1IFDh+XZH3A6BkXtv0K4gFGCI0Y4BM7B1wOEi0Rmgw==",
+ "version": "1.7.4",
+ "resolved": "https://registry.npmjs.org/axios/-/axios-1.7.4.tgz",
+ "integrity": "sha512-DukmaFRnY6AzAALSH4J2M3k6PkaC+MfaAGdEERRWcC9q3/TWQwLpHR8ZRLKTdQ3aBDL64EdluRDjJqKw+BPZEw==",
"license": "MIT",
"dependencies": {
"follow-redirects": "^1.15.6",
@@ -4148,6 +4194,15 @@
"prebuild-install": "^7.1.1"
}
},
+ "node_modules/big-integer": {
+ "version": "1.6.52",
+ "resolved": "https://registry.npmjs.org/big-integer/-/big-integer-1.6.52.tgz",
+ "integrity": "sha512-QxD8cf2eVqJOOz63z6JIN9BzvVs/dlySa5HGSBH5xtR8dPteIRQnBxxKqkNTiT6jbDTF6jAfrd4oMcND9RGbQg==",
+ "license": "Unlicense",
+ "engines": {
+ "node": ">=0.6"
+ }
+ },
"node_modules/binary-extensions": {
"version": "2.3.0",
"resolved": "https://registry.npmjs.org/binary-extensions/-/binary-extensions-2.3.0.tgz",
@@ -4261,9 +4316,9 @@
}
},
"node_modules/browserslist": {
- "version": "4.23.2",
- "resolved": "https://registry.npmjs.org/browserslist/-/browserslist-4.23.2.tgz",
- "integrity": "sha512-qkqSyistMYdxAcw+CzbZwlBy8AGmS/eEWs+sEV5TnLRGDOL+C5M2EnH6tlZyg0YoAxGJAFKh61En9BR941GnHA==",
+ "version": "4.23.3",
+ "resolved": "https://registry.npmjs.org/browserslist/-/browserslist-4.23.3.tgz",
+ "integrity": "sha512-btwCFJVjI4YWDNfau8RhZ+B1Q/VLoUITrm3RlP6y1tYGWIOa+InuYiRGXUBXo8nA1qKmHMyLB/iVQg5TT4eFoA==",
"dev": true,
"funding": [
{
@@ -4281,9 +4336,9 @@
],
"license": "MIT",
"dependencies": {
- "caniuse-lite": "^1.0.30001640",
- "electron-to-chromium": "^1.4.820",
- "node-releases": "^2.0.14",
+ "caniuse-lite": "^1.0.30001646",
+ "electron-to-chromium": "^1.5.4",
+ "node-releases": "^2.0.18",
"update-browserslist-db": "^1.1.0"
},
"bin": {
@@ -4487,9 +4542,9 @@
}
},
"node_modules/caniuse-lite": {
- "version": "1.0.30001643",
- "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001643.tgz",
- "integrity": "sha512-ERgWGNleEilSrHM6iUz/zJNSQTP8Mr21wDWpdgvRwcTXGAq6jMtOUPP4dqFPTdKqZ2wKTdtB+uucZ3MRpAUSmg==",
+ "version": "1.0.30001651",
+ "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001651.tgz",
+ "integrity": "sha512-9Cf+Xv1jJNe1xPZLGuUXLNkE1BoDkqRqYyFJ9TDYSqhduqA4hu4oR9HluGoWYQC/aj8WHjsGVV+bwkh0+tegRg==",
"dev": true,
"funding": [
{
@@ -5673,9 +5728,9 @@
"license": "MIT"
},
"node_modules/electron": {
- "version": "31.3.0",
- "resolved": "https://registry.npmjs.org/electron/-/electron-31.3.0.tgz",
- "integrity": "sha512-3LMRMmK4UK0A+jYSLGLYdfhc20TgY2v5jD3iGmhRZlDYj0gn7xBj/waRjlNalysZ0D2rgPvoes0wHuf5e/Bguw==",
+ "version": "31.4.0",
+ "resolved": "https://registry.npmjs.org/electron/-/electron-31.4.0.tgz",
+ "integrity": "sha512-YTwKoAA+nrJMlI1TTHnIXLYWoQLKnhbkz0qxZcI7Hadcy0UaFMFs9xzwvH2MnrRpVJy7RKo49kVGuvSdRl8zMA==",
"dev": true,
"hasInstallScript": true,
"license": "MIT",
@@ -6118,9 +6173,9 @@
"license": "MIT"
},
"node_modules/electron-to-chromium": {
- "version": "1.5.2",
- "resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.5.2.tgz",
- "integrity": "sha512-kc4r3U3V3WLaaZqThjYz/Y6z8tJe+7K0bbjUVo3i+LWIypVdMx5nXCkwRe6SWbY6ILqLdc1rKcKmr3HoH7wjSQ==",
+ "version": "1.5.11",
+ "resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.5.11.tgz",
+ "integrity": "sha512-R1CccCDYqndR25CaXFd6hp/u9RaaMcftMkphmvuepXr5b1vfLkRml6aWVeBhXJ7rbevHkKEMJtz8XqPf7ffmew==",
"dev": true,
"license": "ISC"
},
@@ -7092,6 +7147,28 @@
"express": "^4.0.0 || ^5.0.0-alpha.1"
}
},
+ "node_modules/express-ws/node_modules/ws": {
+ "version": "7.5.10",
+ "resolved": "https://registry.npmjs.org/ws/-/ws-7.5.10.tgz",
+ "integrity": "sha512-+dbF1tHwZpXcbOJdVOkzLDxZP1ailvSxM6ZweXTegylPny803bFhA+vqBYw4s31NSAk4S2Qz+AKXK9a4wkdjcQ==",
+ "dev": true,
+ "license": "MIT",
+ "engines": {
+ "node": ">=8.3.0"
+ },
+ "peerDependencies": {
+ "bufferutil": "^4.0.1",
+ "utf-8-validate": "^5.0.2"
+ },
+ "peerDependenciesMeta": {
+ "bufferutil": {
+ "optional": true
+ },
+ "utf-8-validate": {
+ "optional": true
+ }
+ }
+ },
"node_modules/express/node_modules/debug": {
"version": "2.6.9",
"resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz",
@@ -9609,6 +9686,19 @@
"dev": true,
"license": "MIT"
},
+ "node_modules/node-sql-parser": {
+ "version": "5.3.1",
+ "resolved": "https://registry.npmjs.org/node-sql-parser/-/node-sql-parser-5.3.1.tgz",
+ "integrity": "sha512-WBDgG1lMy3y67DroHKn/hjqPXs2vGDrQPVCFyHE0kXsFncPKdiiMV4H78+XuXeKSFnGYgLnsQ7xyJHH2qDCgXQ==",
+ "license": "Apache-2.0",
+ "dependencies": {
+ "@types/pegjs": "^0.10.0",
+ "big-integer": "^1.6.48"
+ },
+ "engines": {
+ "node": ">=8"
+ }
+ },
"node_modules/nopt": {
"version": "6.0.0",
"resolved": "https://registry.npmjs.org/nopt/-/nopt-6.0.0.tgz",
@@ -10344,9 +10434,9 @@
}
},
"node_modules/postcss": {
- "version": "8.4.40",
- "resolved": "https://registry.npmjs.org/postcss/-/postcss-8.4.40.tgz",
- "integrity": "sha512-YF2kKIUzAofPMpfH6hOi2cGnv/HrUlfucspc7pDyvv7kGdqXrfj8SCl/t8owkEgKEuu8ZcRjSOxFxVLqwChZ2Q==",
+ "version": "8.4.41",
+ "resolved": "https://registry.npmjs.org/postcss/-/postcss-8.4.41.tgz",
+ "integrity": "sha512-TesUflQ0WKZqAvg52PWL6kHgLKP6xB6heTOdoYM0Wt2UHyxNa4K25EZZMgKns3BH1RLVbZCREPpLY0rhnNoHVQ==",
"funding": [
{
"type": "opencollective",
@@ -10577,9 +10667,9 @@
}
},
"node_modules/prettier-plugin-tailwindcss": {
- "version": "0.6.5",
- "resolved": "https://registry.npmjs.org/prettier-plugin-tailwindcss/-/prettier-plugin-tailwindcss-0.6.5.tgz",
- "integrity": "sha512-axfeOArc/RiGHjOIy9HytehlC0ZLeMaqY09mm8YCkMzznKiDkwFzOpBvtuhuv3xG5qB73+Mj7OCe2j/L1ryfuQ==",
+ "version": "0.6.6",
+ "resolved": "https://registry.npmjs.org/prettier-plugin-tailwindcss/-/prettier-plugin-tailwindcss-0.6.6.tgz",
+ "integrity": "sha512-OPva5S7WAsPLEsOuOWXATi13QrCKACCiIonFgIR6V4lYv4QLp++UXVhZSzRbZxXGimkQtQT86CC6fQqTOybGng==",
"dev": true,
"license": "MIT",
"engines": {
@@ -10597,6 +10687,7 @@
"prettier-plugin-import-sort": "*",
"prettier-plugin-jsdoc": "*",
"prettier-plugin-marko": "*",
+ "prettier-plugin-multiline-arrays": "*",
"prettier-plugin-organize-attributes": "*",
"prettier-plugin-organize-imports": "*",
"prettier-plugin-sort-imports": "*",
@@ -10634,6 +10725,9 @@
"prettier-plugin-marko": {
"optional": true
},
+ "prettier-plugin-multiline-arrays": {
+ "optional": true
+ },
"prettier-plugin-organize-attributes": {
"optional": true
},
@@ -11060,12 +11154,12 @@
}
},
"node_modules/react-hook-form": {
- "version": "7.52.1",
- "resolved": "https://registry.npmjs.org/react-hook-form/-/react-hook-form-7.52.1.tgz",
- "integrity": "sha512-uNKIhaoICJ5KQALYZ4TOaOLElyM+xipord+Ha3crEFhTntdLvWZqVY49Wqd/0GiVCA/f9NjemLeiNPjG7Hpurg==",
+ "version": "7.52.2",
+ "resolved": "https://registry.npmjs.org/react-hook-form/-/react-hook-form-7.52.2.tgz",
+ "integrity": "sha512-pqfPEbERnxxiNMPd0bzmt1tuaPcVccywFDpyk2uV5xCIBphHV5T8SVnX9/o3kplPE1zzKt77+YIoq+EMwJp56A==",
"license": "MIT",
"engines": {
- "node": ">=12.22.0"
+ "node": ">=18.0.0"
},
"funding": {
"type": "opencollective",
@@ -11150,12 +11244,12 @@
"license": "0BSD"
},
"node_modules/react-router": {
- "version": "6.26.0",
- "resolved": "https://registry.npmjs.org/react-router/-/react-router-6.26.0.tgz",
- "integrity": "sha512-wVQq0/iFYd3iZ9H2l3N3k4PL8EEHcb0XlU2Na8nEwmiXgIUElEH6gaJDtUQxJ+JFzmIXaQjfdpcGWaM6IoQGxg==",
+ "version": "6.26.1",
+ "resolved": "https://registry.npmjs.org/react-router/-/react-router-6.26.1.tgz",
+ "integrity": "sha512-kIwJveZNwp7teQRI5QmwWo39A5bXRyqpH0COKKmPnyD2vBvDwgFXSqDUYtt1h+FEyfnE8eXr7oe0MxRzVwCcvQ==",
"license": "MIT",
"dependencies": {
- "@remix-run/router": "1.19.0"
+ "@remix-run/router": "1.19.1"
},
"engines": {
"node": ">=14.0.0"
@@ -11165,13 +11259,13 @@
}
},
"node_modules/react-router-dom": {
- "version": "6.26.0",
- "resolved": "https://registry.npmjs.org/react-router-dom/-/react-router-dom-6.26.0.tgz",
- "integrity": "sha512-RRGUIiDtLrkX3uYcFiCIxKFWMcWQGMojpYZfcstc63A1+sSnVgILGIm9gNUA6na3Fm1QuPGSBQH2EMbAZOnMsQ==",
+ "version": "6.26.1",
+ "resolved": "https://registry.npmjs.org/react-router-dom/-/react-router-dom-6.26.1.tgz",
+ "integrity": "sha512-veut7m41S1fLql4pLhxeSW3jlqs+4MtjRLj0xvuCEXsxusJCbs6I8yn9BxzzDX2XDgafrccY6hwjmd/bL54tFw==",
"license": "MIT",
"dependencies": {
- "@remix-run/router": "1.19.0",
- "react-router": "6.26.0"
+ "@remix-run/router": "1.19.1",
+ "react-router": "6.26.1"
},
"engines": {
"node": ">=14.0.0"
@@ -12511,9 +12605,9 @@
}
},
"node_modules/tailwind-merge": {
- "version": "2.4.0",
- "resolved": "https://registry.npmjs.org/tailwind-merge/-/tailwind-merge-2.4.0.tgz",
- "integrity": "sha512-49AwoOQNKdqKPd9CViyH5wJoSKsCDjUlzL8DxuGp3P1FsGY36NJDAa18jLZcaHAUUuTj+JB8IAo8zWgBNvBF7A==",
+ "version": "2.5.2",
+ "resolved": "https://registry.npmjs.org/tailwind-merge/-/tailwind-merge-2.5.2.tgz",
+ "integrity": "sha512-kjEBm+pvD+6eAwzJL2Bi+02/9LFLal1Gs61+QB7HvTfQQ0aXwC5LGT8PEt1gS0CWKktKe6ysPTAy3cBC5MeiIg==",
"license": "MIT",
"funding": {
"type": "github",
@@ -12521,9 +12615,9 @@
}
},
"node_modules/tailwindcss": {
- "version": "3.4.7",
- "resolved": "https://registry.npmjs.org/tailwindcss/-/tailwindcss-3.4.7.tgz",
- "integrity": "sha512-rxWZbe87YJb4OcSopb7up2Ba4U82BoiSGUdoDr3Ydrg9ckxFS/YWsvhN323GMcddgU65QRy7JndC7ahhInhvlQ==",
+ "version": "3.4.10",
+ "resolved": "https://registry.npmjs.org/tailwindcss/-/tailwindcss-3.4.10.tgz",
+ "integrity": "sha512-KWZkVPm7yJRhdu4SRSl9d4AK2wM3a50UsvgHZO7xY77NQr2V+fIrEuoDGQcbvswWvFGbS2f6e+jC/6WJm1Dl0w==",
"license": "MIT",
"dependencies": {
"@alloc/quick-lru": "^5.2.0",
@@ -12848,9 +12942,9 @@
}
},
"node_modules/ts-pattern": {
- "version": "5.2.0",
- "resolved": "https://registry.npmjs.org/ts-pattern/-/ts-pattern-5.2.0.tgz",
- "integrity": "sha512-aGaSpOlDcns7ZoeG/OMftWyQG1KqPVhgplhJxNCvyIXqWrumM5uIoOSarw/hmmi/T1PnuQ/uD8NaFHvLpHicDg==",
+ "version": "5.3.1",
+ "resolved": "https://registry.npmjs.org/ts-pattern/-/ts-pattern-5.3.1.tgz",
+ "integrity": "sha512-1RUMKa8jYQdNfmnK4jyzBK3/PS/tnjcZ1CW0v1vWDeYe5RBklc/nquw03MEoB66hVBm4BnlCfmOqDVxHyT1DpA==",
"license": "MIT"
},
"node_modules/tsconfig-paths": {
@@ -13019,9 +13113,9 @@
}
},
"node_modules/typescript": {
- "version": "4.5.5",
- "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.5.5.tgz",
- "integrity": "sha512-TCTIul70LyWe6IJWT8QSYeA54WQe8EjQFU4wY52Fasj5UKx88LNYKCgBEHcOMOrFF1rKGbD8v/xcNWVUq9SymA==",
+ "version": "5.5.4",
+ "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.5.4.tgz",
+ "integrity": "sha512-Mtq29sKDAEYP7aljRgtPOpTvOfbwRWlS6dPRzwjdE+C0R4brX/GUyhHSecbHMFLNBLcJIPt9nl9yG5TZ1weH+Q==",
"devOptional": true,
"license": "Apache-2.0",
"bin": {
@@ -13029,7 +13123,7 @@
"tsserver": "bin/tsserver"
},
"engines": {
- "node": ">=4.2.0"
+ "node": ">=14.17"
}
},
"node_modules/uc.micro": {
@@ -13280,14 +13374,14 @@
}
},
"node_modules/vite": {
- "version": "5.3.5",
- "resolved": "https://registry.npmjs.org/vite/-/vite-5.3.5.tgz",
- "integrity": "sha512-MdjglKR6AQXQb9JGiS7Rc2wC6uMjcm7Go/NHNO63EwiJXfuk9PgqiP/n5IDJCziMkfw9n4Ubp7lttNwz+8ZVKA==",
+ "version": "5.4.1",
+ "resolved": "https://registry.npmjs.org/vite/-/vite-5.4.1.tgz",
+ "integrity": "sha512-1oE6yuNXssjrZdblI9AfBbHCC41nnyoVoEZxQnID6yvQZAFBzxxkqoFLtHUMkYunL8hwOLEjgTuxpkRxvba3kA==",
"dev": true,
"license": "MIT",
"dependencies": {
"esbuild": "^0.21.3",
- "postcss": "^8.4.39",
+ "postcss": "^8.4.41",
"rollup": "^4.13.0"
},
"bin": {
@@ -13307,6 +13401,7 @@
"less": "*",
"lightningcss": "^1.21.0",
"sass": "*",
+ "sass-embedded": "*",
"stylus": "*",
"sugarss": "*",
"terser": "^5.4.0"
@@ -13324,6 +13419,9 @@
"sass": {
"optional": true
},
+ "sass-embedded": {
+ "optional": true
+ },
"stylus": {
"optional": true
},
@@ -13583,17 +13681,16 @@
"license": "ISC"
},
"node_modules/ws": {
- "version": "7.5.10",
- "resolved": "https://registry.npmjs.org/ws/-/ws-7.5.10.tgz",
- "integrity": "sha512-+dbF1tHwZpXcbOJdVOkzLDxZP1ailvSxM6ZweXTegylPny803bFhA+vqBYw4s31NSAk4S2Qz+AKXK9a4wkdjcQ==",
- "dev": true,
+ "version": "8.18.0",
+ "resolved": "https://registry.npmjs.org/ws/-/ws-8.18.0.tgz",
+ "integrity": "sha512-8VbfWfHLbbwu3+N6OKsOMpBdT4kXPDDB9cJk2bJ6mh9ucxdlnNvH1e+roYkKmN9Nxw2yjz7VzeO9oOz2zJ04Pw==",
"license": "MIT",
"engines": {
- "node": ">=8.3.0"
+ "node": ">=10.0.0"
},
"peerDependencies": {
"bufferutil": "^4.0.1",
- "utf-8-validate": "^5.0.2"
+ "utf-8-validate": ">=5.0.2"
},
"peerDependenciesMeta": {
"bufferutil": {
diff --git a/desktop/package.json b/desktop/package.json
index c957ed3e..8153ead2 100644
--- a/desktop/package.json
+++ b/desktop/package.json
@@ -27,19 +27,20 @@
"@types/lodash": "^4.17.7",
"@types/react": "^18.3.3",
"@types/react-dom": "^18.3.0",
+ "@types/ws": "^8.5.12",
"@typescript-eslint/eslint-plugin": "^5.62.0",
"@typescript-eslint/parser": "^5.62.0",
- "autoprefixer": "^10.4.19",
- "electron": "31.3.0",
+ "autoprefixer": "^10.4.20",
+ "electron": "^31.4.0",
"eslint": "^8.57.0",
"eslint-plugin-import": "^2.29.1",
"postcss": "^8.4.40",
"prettier": "^3.3.3",
- "prettier-plugin-tailwindcss": "^0.6.5",
- "tailwindcss": "^3.4.7",
+ "prettier-plugin-tailwindcss": "^0.6.6",
+ "tailwindcss": "^3.4.10",
"ts-node": "^10.9.2",
- "typescript": "~4.5.4",
- "vite": "^5.3.5"
+ "typescript": "^5.5.4",
+ "vite": "^5.4.1"
},
"keywords": [],
"author": {
@@ -60,6 +61,7 @@
"@radix-ui/react-toast": "^1.2.1",
"@radix-ui/react-tooltip": "^1.1.2",
"@react-oauth/google": "^0.12.1",
+ "@tanstack/react-query": "^5.51.23",
"@tiptap/core": "^2.5.8",
"@tiptap/extension-blockquote": "^2.5.8",
"@tiptap/extension-bold": "^2.5.8",
@@ -90,7 +92,7 @@
"@tiptap/pm": "^2.5.8",
"@tiptap/react": "^2.5.8",
"@tiptap/suggestion": "^2.5.8",
- "axios": "^1.7.2",
+ "axios": "^1.7.4",
"better-sqlite3": "^11.1.2",
"class-variance-authority": "^0.7.0",
"clsx": "^2.1.1",
@@ -103,16 +105,18 @@
"lowlight": "^3.1.0",
"mobx": "^6.13.1",
"mobx-react-lite": "^4.0.7",
+ "node-sql-parser": "^5.3.1",
"re-resizable": "^6.9.17",
"react": "^18.3.1",
"react-dom": "^18.3.1",
- "react-hook-form": "^7.52.1",
+ "react-hook-form": "^7.52.2",
"react-intersection-observer": "^9.13.0",
- "react-router-dom": "^6.26.0",
- "tailwind-merge": "^2.4.0",
+ "react-router-dom": "^6.26.1",
+ "tailwind-merge": "^2.5.2",
"tailwindcss-animate": "^1.0.7",
- "ts-pattern": "^5.2.0",
+ "ts-pattern": "^5.3.1",
"ulid": "^2.3.0",
+ "ws": "^8.18.0",
"zod": "^3.23.8"
}
}
diff --git a/desktop/src/components/accounts/account-logout.tsx b/desktop/src/components/accounts/account-logout.tsx
index 23cc8d81..9d39b494 100644
--- a/desktop/src/components/accounts/account-logout.tsx
+++ b/desktop/src/components/accounts/account-logout.tsx
@@ -41,7 +41,7 @@ export const AccountLogout = ({ id, onCancel }: AccountLogoutProps) => {
disabled={isDeleting}
onClick={async () => {
setIsDeleting(true);
- await window.globalDb.logout(id);
+ await window.neuron.logout(id);
window.location.href = '/';
}}
>
diff --git a/desktop/src/components/accounts/login.tsx b/desktop/src/components/accounts/login.tsx
index 5f697ecd..f5fdc40c 100644
--- a/desktop/src/components/accounts/login.tsx
+++ b/desktop/src/components/accounts/login.tsx
@@ -3,19 +3,48 @@ import { EmailLogin } from '@/components/accounts/email-login';
import { LoginOutput } from '@/types/accounts';
import { EmailRegister } from '@/components/accounts/email-register';
import { observer } from 'mobx-react-lite';
+import { useAppDatabase } from '@/contexts/app-database';
const serverUrl = 'http://localhost:3000';
export const Login = observer(() => {
+ const appDatabase = useAppDatabase();
const [showRegister, setShowRegister] = React.useState(false);
const handleLogin = async (output: LoginOutput) => {
- await window.globalDb.addAccount(output.account);
+ const insertAccountQuery = appDatabase.database
+ .insertInto('accounts')
+ .values({
+ id: output.account.id,
+ name: output.account.name,
+ avatar: output.account.avatar,
+ device_id: output.account.deviceId,
+ email: output.account.email,
+ token: output.account.token,
+ })
+ .compile();
+
+ await appDatabase.executeQuery(insertAccountQuery);
if (output.workspaces.length > 0) {
- for (const workspace of output.workspaces) {
- await window.globalDb.addWorkspace(workspace);
- }
+ const insertWorkspacesQuery = appDatabase.database
+ .insertInto('workspaces')
+ .values(
+ output.workspaces.map((workspace) => ({
+ id: workspace.id,
+ name: workspace.name,
+ account_id: output.account.id,
+ avatar: workspace.avatar,
+ role: workspace.role,
+ description: workspace.description,
+ synced_at: workspace.syncedAt,
+ user_id: workspace.userId,
+ version_id: workspace.versionId,
+ })),
+ )
+ .compile();
+
+ await appDatabase.executeQuery(insertWorkspacesQuery);
}
window.location.href = '/';
diff --git a/desktop/src/components/app.tsx b/desktop/src/components/app.tsx
index d5ceca5d..e677b265 100644
--- a/desktop/src/components/app.tsx
+++ b/desktop/src/components/app.tsx
@@ -4,42 +4,69 @@ import { AppLoading } from '@/components/app-loading';
import { AccountContext } from '@/contexts/account';
import Axios from 'axios';
import { AxiosContext } from '@/contexts/axios';
-import { useStore } from '@/contexts/store';
import { observer } from 'mobx-react-lite';
import { Outlet } from 'react-router-dom';
import { AccountLogout } from '@/components/accounts/account-logout';
+import { AppDatabaseContext } from '@/contexts/app-database';
+import {
+ Kysely,
+ SqliteAdapter,
+ DummyDriver,
+ SqliteIntrospector,
+ SqliteQueryCompiler,
+} from 'kysely';
+import { AppDatabaseSchema } from '@/data/schemas/app';
+import { useQuery } from '@tanstack/react-query';
const serverUrl = 'http://localhost:3000';
+const appDatabase = new Kysely<AppDatabaseSchema>({
+ dialect: {
+ createAdapter: () => new SqliteAdapter(),
+ createDriver: () => new DummyDriver(),
+ createIntrospector: (db) => new SqliteIntrospector(db),
+ createQueryCompiler: () => new SqliteQueryCompiler(),
+ },
+});
+
export const App = observer(() => {
- const store = useStore();
const [showLogout, setShowLogout] = React.useState(false);
- React.useEffect(() => {
- if (!store.loaded) {
- window.globalDb
- .init()
- .then(({ accounts, workspaces }) => {
- store.setAccounts(accounts);
- store.setWorkspaces(workspaces);
- store.setLoaded();
- })
- .catch((error) => {
- // Handle any errors if needed
- console.error('Error loading data: ', error);
- });
- }
- }, [store.loaded]);
+ const accountsQuery = useQuery({
+ queryKey: ['accounts'],
+ queryFn: async () => {
+ const query = appDatabase.selectFrom('accounts').selectAll().compile();
+ return await window.neuron.executeAppQuery(query);
+ },
+ });
+
+ const workspacesQuery = useQuery({
+ queryKey: ['workspaces'],
+ queryFn: async () => {
+ const query = appDatabase.selectFrom('workspaces').selectAll().compile();
+ return await window.neuron.executeAppQuery(query);
+ },
+ });
- if (!store.loaded) {
+ const isLoading = accountsQuery.isPending || workspacesQuery.isPending;
+ if (isLoading) {
return <AppLoading />;
}
- if (store.accounts.length == 0) {
- return <Login />;
+ if (accountsQuery.data.rows.length == 0) {
+ return (
+ <AppDatabaseContext.Provider
+ value={{
+ database: appDatabase,
+ executeQuery: (query) => window.neuron.executeAppQuery(query),
+ }}
+ >
+ <Login />
+ </AppDatabaseContext.Provider>
+ );
}
- const account = store.accounts[0];
+ const account = accountsQuery.data.rows[0];
const axios = Axios.create({
baseURL: serverUrl,
headers: {
@@ -48,25 +75,50 @@ export const App = observer(() => {
});
return (
- <AccountContext.Provider
+ <AppDatabaseContext.Provider
value={{
- ...account,
- logout: () => {
- setShowLogout(true);
- },
+ database: appDatabase,
+ executeQuery: (query) => window.neuron.executeAppQuery(query),
}}
>
- <AxiosContext.Provider value={axios}>
- <Outlet />
- </AxiosContext.Provider>
- {showLogout && (
- <AccountLogout
- id={account.id}
- onCancel={() => {
- setShowLogout(false);
- }}
- />
- )}
- </AccountContext.Provider>
+ <AccountContext.Provider
+ value={{
+ id: account.id,
+ name: account.name,
+ avatar: account.avatar,
+ token: account.token,
+ email: account.email,
+ deviceId: account.device_id,
+ workspaces: workspacesQuery.data?.rows.map((row) => {
+ return {
+ id: row.id,
+ name: row.name,
+ description: row.description,
+ avatar: row.avatar,
+ versionId: row.version_id,
+ accountId: row.account_id,
+ role: row.role,
+ userId: row.user_id,
+ syncedAt: row.synced_at,
+ };
+ }),
+ logout: () => {
+ setShowLogout(true);
+ },
+ }}
+ >
+ <AxiosContext.Provider value={axios}>
+ <Outlet />
+ </AxiosContext.Provider>
+ {showLogout && (
+ <AccountLogout
+ id={account.id}
+ onCancel={() => {
+ setShowLogout(false);
+ }}
+ />
+ )}
+ </AccountContext.Provider>
+ </AppDatabaseContext.Provider>
);
});
diff --git a/desktop/src/components/messages/conversation.tsx b/desktop/src/components/messages/conversation.tsx
index dc0f76aa..f278cc4d 100644
--- a/desktop/src/components/messages/conversation.tsx
+++ b/desktop/src/components/messages/conversation.tsx
@@ -9,7 +9,7 @@ interface ConversationProps {
}
export const Conversation = observer(({ nodeId }: ConversationProps) => {
- const { nodes, isLoading, createMessage } = useConversation(nodeId);
+ const { messages, isLoading, createMessage } = useConversation(nodeId);
if (isLoading) {
return <div>Loading...</div>;
@@ -17,7 +17,7 @@ export const Conversation = observer(({ nodeId }: ConversationProps) => {
return (
<React.Fragment>
- <MessageList conversationId={nodeId} nodes={nodes} />
+ <MessageList conversationId={nodeId} messages={messages} />
<MessageCreate nodeId={nodeId} onSubmit={createMessage} />
</React.Fragment>
);
diff --git a/desktop/src/components/messages/message-delete-button.tsx b/desktop/src/components/messages/message-delete-button.tsx
index d5ffe0ef..a72dacca 100644
--- a/desktop/src/components/messages/message-delete-button.tsx
+++ b/desktop/src/components/messages/message-delete-button.tsx
@@ -18,6 +18,7 @@ interface MessageDeleteButtonProps {
export const MessageDeleteButton = ({ id }: MessageDeleteButtonProps) => {
const [showDeleteModal, setShowDeleteModal] = React.useState(false);
+ const [isDeleting, setIsDeleting] = React.useState(false);
const workspace = useWorkspace();
return (
@@ -38,8 +39,17 @@ export const MessageDeleteButton = ({ id }: MessageDeleteButtonProps) => {
<AlertDialogCancel>Cancel</AlertDialogCancel>
<Button
variant="destructive"
+ disabled={isDeleting}
onClick={async () => {
- await workspace.deleteNode(id);
+ setIsDeleting(true);
+
+ const query = workspace.schema
+ .deleteFrom('nodes')
+ .where('id', '=', id)
+ .compile();
+
+ await workspace.executeQuery(query);
+ setIsDeleting(false);
setShowDeleteModal(false);
}}
>
diff --git a/desktop/src/components/messages/message-list.tsx b/desktop/src/components/messages/message-list.tsx
index bd88f1ba..643bca26 100644
--- a/desktop/src/components/messages/message-list.tsx
+++ b/desktop/src/components/messages/message-list.tsx
@@ -1,27 +1,15 @@
import React from 'react';
-import { Node } from '@/types/nodes';
import { ScrollArea } from '@/components/ui/scroll-area';
import { InView } from 'react-intersection-observer';
import { Message } from '@/components/messages/message';
+import { MessageNode } from '@/types/messages';
interface MessageListProps {
conversationId: string;
- nodes: Node[];
+ messages: MessageNode[];
}
-export const MessageList = ({ conversationId, nodes }: MessageListProps) => {
- const messages = nodes
- .filter((node) => node.type === 'message')
- .sort((a, b) => {
- if (a.id > b.id) {
- return 1;
- } else if (a.id < b.id) {
- return -1;
- } else {
- return 0;
- }
- });
-
+export const MessageList = ({ conversationId, messages }: MessageListProps) => {
const viewportRef = React.useRef<HTMLDivElement>(null);
const containerRef = React.useRef<HTMLDivElement>(null);
const observerRef = React.useRef<ResizeObserver | null>(null);
@@ -110,11 +98,7 @@ export const MessageList = ({ conversationId, nodes }: MessageListProps) => {
<div className="flex-grow border-t border-gray-100" />
</div>
)}
- <Message
- message={message}
- previousMessage={previousMessage}
- nodes={nodes}
- />
+ <Message message={message} previousMessage={previousMessage} />
</React.Fragment>
);
})}
diff --git a/desktop/src/components/messages/message.tsx b/desktop/src/components/messages/message.tsx
index a5f4db47..570213c3 100644
--- a/desktop/src/components/messages/message.tsx
+++ b/desktop/src/components/messages/message.tsx
@@ -1,6 +1,5 @@
import React from 'react';
import { observer } from 'mobx-react-lite';
-import { Node } from '@/types/nodes';
import { Avatar } from '@/components/ui/avatar';
import {
Tooltip,
@@ -12,21 +11,17 @@ import { InView } from 'react-intersection-observer';
import { MessageReactionPicker } from '@/components/messages/message-reaction-picker';
import { Icon } from '@/components/ui/icon';
import { MessageDeleteButton } from '@/components/messages/message-delete-button';
-import { MessageReference } from '@/components/messages/message-reference';
-import { MessageReactions } from '@/components/messages/message-reactions';
-import { useWorkspace } from '@/contexts/workspace';
-import { buildNodeWithChildren } from '@/lib/nodes';
import { NodeRenderer } from '@/editor/renderers/node';
+import { MessageNode } from '@/types/messages';
interface MessageProps {
- message: Node;
- previousMessage?: Node;
- nodes: Node[];
+ message: MessageNode;
+ previousMessage?: MessageNode;
}
export const Message = observer(
- ({ message, previousMessage, nodes }: MessageProps) => {
- const author = nodes.find((node) => node.id === message.createdBy) ?? {
+ ({ message, previousMessage }: MessageProps) => {
+ const author = message.author ?? {
attrs: {
id: message.createdBy,
name: 'Deleted user',
@@ -53,9 +48,8 @@ export const Message = observer(
const canEdit = true;
const canDelete = true;
const canReplyInThread = true;
- const nodeWithChildren = buildNodeWithChildren(message, nodes);
- if (!nodeWithChildren.children || nodeWithChildren.children.length === 0) {
+ if (!message.children || message.children.length === 0) {
return null;
}
@@ -139,7 +133,7 @@ export const Message = observer(
</ul>
{/*{data.messageReference && <MessageReference />}*/}
<div className="text-foreground">
- <NodeRenderer node={nodeWithChildren} keyPrefix={message.id} />
+ <NodeRenderer node={message} keyPrefix={message.id} />
</div>
{/*<MessageReactions*/}
{/* message={message}*/}
diff --git a/desktop/src/components/root.tsx b/desktop/src/components/root.tsx
index 1b4d3cfe..7485ac68 100644
--- a/desktop/src/components/root.tsx
+++ b/desktop/src/components/root.tsx
@@ -3,13 +3,12 @@ import { createRoot } from 'react-dom/client';
import { App } from '@/components/app';
import { TooltipProvider } from '@/components/ui/tooltip';
import { Toaster } from '@/components/ui/toaster';
-import { StoreContext } from '@/contexts/store';
-import { store } from '@/store';
import { WorkspaceCreate } from '@/components/workspaces/workspace-create';
import { Workspace } from '@/components/workspaces/workspace';
import { WorkspaceRedirect } from '@/components/workspaces/workspace-redirect';
import { createBrowserRouter, RouterProvider } from 'react-router-dom';
import { Container } from '@/components/workspaces/container';
+import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
const router = createBrowserRouter([
{
@@ -38,14 +37,22 @@ const router = createBrowserRouter([
},
]);
+const queryClient = new QueryClient({
+ defaultOptions: {
+ queries: {
+ retry: false,
+ },
+ },
+});
+
export const Root = () => {
return (
- <StoreContext.Provider value={store}>
+ <QueryClientProvider client={queryClient}>
<TooltipProvider>
<RouterProvider router={router} />
</TooltipProvider>
<Toaster />
- </StoreContext.Provider>
+ </QueryClientProvider>
);
};
diff --git a/desktop/src/components/spaces/space-create-dialog.tsx b/desktop/src/components/spaces/space-create-dialog.tsx
index 9c31e48c..c57eb555 100644
--- a/desktop/src/components/spaces/space-create-dialog.tsx
+++ b/desktop/src/components/spaces/space-create-dialog.tsx
@@ -23,7 +23,6 @@ import { Button } from '@/components/ui/button';
import { Spinner } from '@/components/ui/spinner';
import { useForm } from 'react-hook-form';
import { zodResolver } from '@hookform/resolvers/zod';
-import { CreateNodeInput, Node } from '@/types/nodes';
import { NeuronId } from '@/lib/id';
import { NodeTypes } from '@/lib/constants';
import { generateNodeIndex } from '@/lib/nodes';
@@ -60,39 +59,57 @@ export const SpaceCreateDialog = ({
const handleSubmit = async (values: z.infer<typeof formSchema>) => {
setIsPending(true);
- const spaceInput: CreateNodeInput = {
- id: NeuronId.generate(NeuronId.Type.Space),
- type: NodeTypes.Space,
- parentId: null,
- attrs: {
- name: values.name,
- description: values.description,
- },
- };
+ const spaceId = NeuronId.generate(NeuronId.Type.Space);
+ const query = workspace.schema
+ .insertInto('nodes')
+ .values([
+ {
+ id: spaceId,
+ type: NodeTypes.Space,
+ index: generateNodeIndex(null, null),
+ parent_id: null,
+ workspace_id: workspace.id,
+ created_at: new Date().toISOString(),
+ created_by: workspace.userId,
+ version_id: NeuronId.generate(NeuronId.Type.Version),
+ attrs: JSON.stringify({
+ name: values.name,
+ description: values.description,
+ }),
+ },
+ {
+ id: NeuronId.generate(NeuronId.Type.Page),
+ type: NodeTypes.Page,
+ index: generateNodeIndex(null, null),
+ parent_id: spaceId,
+ workspace_id: workspace.id,
+ created_at: new Date().toISOString(),
+ created_by: workspace.userId,
+ version_id: NeuronId.generate(NeuronId.Type.Version),
+ attrs: JSON.stringify({
+ name: 'Home',
+ }),
+ },
+ {
+ id: NeuronId.generate(NeuronId.Type.Channel),
+ type: NodeTypes.Channel,
+ index: generateNodeIndex(null, null),
+ parent_id: spaceId,
+ workspace_id: workspace.id,
+ created_at: new Date().toISOString(),
+ created_by: workspace.userId,
+ version_id: NeuronId.generate(NeuronId.Type.Version),
+ attrs: JSON.stringify({
+ name: 'Discussions',
+ }),
+ },
+ ])
+ .compile();
- const pageInput: CreateNodeInput = {
- id: NeuronId.generate(NeuronId.Type.Page),
- type: NodeTypes.Page,
- attrs: {
- name: 'Home',
- },
- index: generateNodeIndex(null, null),
- parentId: spaceInput.id,
- };
-
- const channelInput: CreateNodeInput = {
- id: NeuronId.generate(NeuronId.Type.Channel),
- type: NodeTypes.Channel,
- attrs: {
- name: 'Discussions',
- },
- index: generateNodeIndex(pageInput.index, null),
- parentId: spaceInput.id,
- };
-
- await workspace.createNodes([spaceInput, pageInput, channelInput]);
+ await workspace.executeQuery(query);
setIsPending(false);
onOpenChange(false);
+ form.reset();
};
return (
diff --git a/desktop/src/components/workspaces/container-header.tsx b/desktop/src/components/workspaces/container-header.tsx
index 7d7fc6f7..329b0d51 100644
--- a/desktop/src/components/workspaces/container-header.tsx
+++ b/desktop/src/components/workspaces/container-header.tsx
@@ -25,6 +25,10 @@ import { useWorkspace } from '@/contexts/workspace';
import { cn } from '@/lib/utils';
import { Input } from '@/components/ui/input';
import { debounce } from 'lodash';
+import { sql } from 'kysely';
+import { mapNode } from '@/lib/nodes';
+import { useQuery } from '@tanstack/react-query';
+import { NodesTableSchema } from '@/data/schemas/workspace';
interface BreadcrumbNodeProps {
node: Node;
@@ -57,12 +61,13 @@ export const BreadcrumbNodeEditor = observer(
const handleNameChange = React.useMemo(
() =>
debounce(async (newName: string) => {
- await workspace.updateNode({
- id: node.id,
- attrs: {
- name: newName,
- },
- });
+ const query = workspace.schema
+ .updateTable('nodes')
+ .set('attrs', sql`json_set(attrs, '$.name', ${newName})`)
+ .where('id', '=', node.id)
+ .compile();
+
+ await workspace.executeQuery(query);
}, 500),
[node.id],
);
@@ -85,26 +90,106 @@ export const BreadcrumbNodeEditor = observer(
interface ContainerHeaderProps {
node: Node;
- breadcrumbNodes: Node[];
}
-export const ContainerHeader = observer(
- ({ node, breadcrumbNodes }: ContainerHeaderProps) => {
- const workspace = useWorkspace();
+export const ContainerHeader = observer(({ node }: ContainerHeaderProps) => {
+ const workspace = useWorkspace();
+ const { data, isPending } = useQuery({
+ queryKey: [`breadcrumb:${node.id}`],
+ queryFn: async ({ queryKey }) => {
+ const query = sql<NodesTableSchema>`
+ WITH RECURSIVE breadcrumb AS (
+ SELECT *
+ FROM nodes
+ WHERE id = ${node.parentId}
+ UNION ALL
+ SELECT n.*
+ FROM nodes n
+ INNER JOIN breadcrumb b ON n.id = b.parent_id
+ )
+ SELECT * FROM breadcrumb;
+ `.compile(workspace.schema);
- const showEllipsis = breadcrumbNodes.length > 2;
- const firstNodes = showEllipsis
- ? breadcrumbNodes.slice(0, 1)
- : breadcrumbNodes;
- const ellipsisNodes = showEllipsis ? breadcrumbNodes.slice(1, -1) : [];
- const lastNodes = showEllipsis ? breadcrumbNodes.slice(-1) : [];
+ const queryId = queryKey[0];
+ return await workspace.executeQueryAndSubscribe(queryId, query);
+ },
+ });
- const isClickable = (node: Node) => node.type !== NodeTypes.Space;
+ if (isPending) {
+ return null;
+ }
- return (
- <Breadcrumb className="mx-1 flex h-12 items-center justify-between border-b-2 border-gray-100 p-2 text-foreground/80">
- <BreadcrumbList>
- {firstNodes.map((breadcrumbNode) => {
+ const breadcrumbNodes = data?.rows.map((row) => mapNode(row)) ?? [];
+ const showEllipsis = breadcrumbNodes.length > 2;
+ const firstNodes = showEllipsis
+ ? breadcrumbNodes.slice(0, 1)
+ : breadcrumbNodes;
+ const ellipsisNodes = showEllipsis ? breadcrumbNodes.slice(1, -1) : [];
+ const lastNodes = showEllipsis ? breadcrumbNodes.slice(-1) : [];
+
+ const isClickable = (node: Node) => node.type !== NodeTypes.Space;
+
+ return (
+ <Breadcrumb className="mx-1 flex h-12 items-center justify-between border-b-2 border-gray-100 p-2 text-foreground/80">
+ <BreadcrumbList>
+ {firstNodes.map((breadcrumbNode) => {
+ return (
+ <React.Fragment key={breadcrumbNode.id}>
+ <BreadcrumbItem
+ onClick={() => {
+ if (isClickable(breadcrumbNode)) {
+ workspace.navigateToNode(breadcrumbNode.id);
+ }
+ }}
+ >
+ <BreadcrumbNode
+ node={breadcrumbNode}
+ className={
+ isClickable(breadcrumbNode)
+ ? 'hover:cursor-pointer hover:text-foreground'
+ : ''
+ }
+ />
+ </BreadcrumbItem>
+ <BreadcrumbSeparator />
+ </React.Fragment>
+ );
+ })}
+ {showEllipsis && (
+ <BreadcrumbItem>
+ <DropdownMenu>
+ <DropdownMenuTrigger className="flex items-center gap-1">
+ <BreadcrumbEllipsis className="h-4 w-4" />
+ </DropdownMenuTrigger>
+ <DropdownMenuContent align="start">
+ {ellipsisNodes.map((breadcrumbNode) => {
+ return (
+ <DropdownMenuItem
+ key={breadcrumbNode.id}
+ onClick={() => {
+ if (isClickable(breadcrumbNode)) {
+ workspace.navigateToNode(breadcrumbNode.id);
+ }
+ }}
+ >
+ <BreadcrumbNode
+ node={breadcrumbNode}
+ className={
+ isClickable(breadcrumbNode)
+ ? 'hover:cursor-pointer hover:text-foreground'
+ : ''
+ }
+ />
+ </DropdownMenuItem>
+ );
+ })}
+ </DropdownMenuContent>
+ </DropdownMenu>
+ </BreadcrumbItem>
+ )}
+ {showEllipsis &&
+ lastNodes.length > 0 &&
+ lastNodes.map((breadcrumbNode) => {
return (
<React.Fragment key={breadcrumbNode.id}>
<BreadcrumbItem
@@ -127,75 +212,17 @@ export const ContainerHeader = observer(
</React.Fragment>
);
})}
- {showEllipsis && (
- <BreadcrumbItem>
- <DropdownMenu>
- <DropdownMenuTrigger className="flex items-center gap-1">
- <BreadcrumbEllipsis className="h-4 w-4" />
- </DropdownMenuTrigger>
- <DropdownMenuContent align="start">
- {ellipsisNodes.map((breadcrumbNode) => {
- return (
- <DropdownMenuItem
- key={breadcrumbNode.id}
- onClick={() => {
- if (isClickable(breadcrumbNode)) {
- workspace.navigateToNode(breadcrumbNode.id);
- }
- }}
- >
- <BreadcrumbNode
- node={breadcrumbNode}
- className={
- isClickable(breadcrumbNode)
- ? 'hover:cursor-pointer hover:text-foreground'
- : ''
- }
- />
- </DropdownMenuItem>
- );
- })}
- </DropdownMenuContent>
- </DropdownMenu>
- </BreadcrumbItem>
- )}
- {showEllipsis &&
- lastNodes.length > 0 &&
- lastNodes.map((breadcrumbNode) => {
- return (
- <React.Fragment key={breadcrumbNode.id}>
- <BreadcrumbItem
- onClick={() => {
- if (isClickable(breadcrumbNode)) {
- workspace.navigateToNode(breadcrumbNode.id);
- }
- }}
- >
- <BreadcrumbNode
- node={breadcrumbNode}
- className={
- isClickable(breadcrumbNode)
- ? 'hover:cursor-pointer hover:text-foreground'
- : ''
- }
- />
- </BreadcrumbItem>
- <BreadcrumbSeparator />
- </React.Fragment>
- );
- })}
- <BreadcrumbItem className="hover:cursor-pointer hover:text-foreground">
- <Popover>
- <PopoverTrigger>
- <BreadcrumbNode node={node} />
- </PopoverTrigger>
- <PopoverContent>
- <BreadcrumbNodeEditor node={node} />
- </PopoverContent>
- </Popover>
- </BreadcrumbItem>
- </BreadcrumbList>
- </Breadcrumb>
- );
- },
-);
+ <BreadcrumbItem className="hover:cursor-pointer hover:text-foreground">
+ <Popover>
+ <PopoverTrigger>
+ <BreadcrumbNode node={node} />
+ </PopoverTrigger>
+ <PopoverContent>
+ <BreadcrumbNodeEditor node={node} />
+ </PopoverContent>
+ </Popover>
+ </BreadcrumbItem>
+ </BreadcrumbList>
+ </Breadcrumb>
+ );
+});
diff --git a/desktop/src/components/workspaces/container.tsx b/desktop/src/components/workspaces/container.tsx
index d9f66ce5..126a4a60 100644
--- a/desktop/src/components/workspaces/container.tsx
+++ b/desktop/src/components/workspaces/container.tsx
@@ -1,24 +1,46 @@
import React from 'react';
import { observer } from 'mobx-react-lite';
import { match } from 'ts-pattern';
+import { useParams } from 'react-router-dom';
import { NodeTypes } from '@/lib/constants';
import { PageContainerNode } from '@/components/pages/page-container-node';
import { ChannelContainerNode } from '@/components/channels/channel-container-node';
import { ContainerHeader } from '@/components/workspaces/container-header';
-import { useParams } from 'react-router-dom';
-import { useContainer } from '@/hooks/use-container';
+import { Spinner } from '@/components/ui/spinner';
+import { mapNode } from '@/lib/nodes';
+import { useQuery } from '@tanstack/react-query';
+import { useWorkspace } from '@/contexts/workspace';
export const Container = observer(() => {
const { nodeId } = useParams<{ nodeId: string }>();
- const { node, breadcrumb } = useContainer(nodeId);
+ const workspace = useWorkspace();
+
+ const { data, isPending } = useQuery({
+ queryKey: [`node:${nodeId}`],
+ queryFn: async ({ queryKey }) => {
+ const query = workspace.schema
+ .selectFrom('nodes')
+ .selectAll()
+ .where('id', '=', nodeId)
+ .compile();
+
+ const queryId = queryKey[0];
+ return await workspace.executeQueryAndSubscribe(queryId, query);
+ },
+ });
+
+ if (isPending) {
+ return <Spinner />;
+ }
- if (!node) {
+ if (!data || data.rows.length === 0) {
return null;
}
+ const node = mapNode(data.rows[0]);
return (
<div className="flex h-full w-full flex-col">
- <ContainerHeader node={node} breadcrumbNodes={breadcrumb} />
+ <ContainerHeader node={node} />
{match(node.type)
.with(NodeTypes.Channel, () => <ChannelContainerNode node={node} />)
.with(NodeTypes.Page, () => <PageContainerNode node={node} />)
diff --git a/desktop/src/components/workspaces/sidebar-header.tsx b/desktop/src/components/workspaces/sidebar-header.tsx
index ee62065c..5450c8bc 100644
--- a/desktop/src/components/workspaces/sidebar-header.tsx
+++ b/desktop/src/components/workspaces/sidebar-header.tsx
@@ -7,17 +7,13 @@ import {
} from '@/components/ui/popover';
import { Icon } from '@/components/ui/icon';
import { Avatar } from '@/components/ui/avatar';
-import { useStore } from '@/contexts/store';
import { observer } from 'mobx-react-lite';
import { useAccount } from '@/contexts/account';
export const SidebarHeader = observer(() => {
- const store = useStore();
const workspace = useWorkspace();
const account = useAccount();
- const allWorkspaces = store.workspaces;
-
return (
<Popover>
<PopoverTrigger asChild>
@@ -49,7 +45,7 @@ export const SidebarHeader = observer(() => {
<hr className="-mx-1 my-1 h-px bg-muted" />
<h2 className="text-sm font-semibold">Workspaces</h2>
<ul className="flex flex-col gap-0.5">
- {allWorkspaces.map((w) => {
+ {account.workspaces.map((w) => {
return (
<li
key={w.id}
diff --git a/desktop/src/components/workspaces/sidebar.tsx b/desktop/src/components/workspaces/sidebar.tsx
index 186a76bb..3263f031 100644
--- a/desktop/src/components/workspaces/sidebar.tsx
+++ b/desktop/src/components/workspaces/sidebar.tsx
@@ -3,12 +3,13 @@ import { SidebarHeader } from '@/components/workspaces/sidebar-header';
import { cn } from '@/lib/utils';
import { Icon } from '@/components/ui/icon';
import { SidebarSpaces } from '@/components/workspaces/sidebar-spaces';
-import { useWorkspace } from '@/contexts/workspace';
-import { useEventBus } from '@/hooks/use-event-bus';
import { Node } from '@/types/nodes';
import { SidebarContext } from '@/contexts/sidebar';
-import { SidebarStore } from '@/store/sidebar';
import { observer } from 'mobx-react-lite';
+import { Spinner } from '@/components/ui/spinner';
+import { mapNode } from '@/lib/nodes';
+import { useQuery } from '@tanstack/react-query';
+import { useWorkspace } from '@/contexts/workspace';
interface LayoutItem {
name: string;
@@ -31,56 +32,40 @@ const layouts: LayoutItem[] = [
export const Sidebar = observer(() => {
const workspace = useWorkspace();
- const eventBus = useEventBus();
- const store = React.useMemo(() => new SidebarStore(), [workspace.id]);
-
- React.useEffect(() => {
- const fetchNodes = async () => {
- store.setIsLoading(true);
-
- const nodes = await workspace.getSidebarNodes();
- store.setNodes(nodes);
-
- store.setIsLoading(false);
- };
-
- fetchNodes();
-
- const subscriptionId = eventBus.subscribe((event) => {
- if (event.event === 'node_created') {
- const createdNode = event.payload as Node;
- if (store.getNode(createdNode.id) || !createdNode.parentId) {
- store.setNode(createdNode);
- } else {
- const parent = store.getNode(createdNode.parentId);
- if (parent) {
- store.setNode(createdNode);
- }
- }
- }
-
- if (event.event === 'node_updated') {
- const updatedNode = event.payload as Node;
- store.setNode(updatedNode);
- }
+ const { data, isPending } = useQuery({
+ queryKey: [`sidebar:${workspace.id}`],
+ queryFn: async ({ queryKey }) => {
+ const query = workspace.schema
+ .selectFrom('nodes')
+ .selectAll()
+ .where((qb) =>
+ qb.or([
+ qb.eb('type', '=', 'space'),
+ qb.eb(
+ 'parent_id',
+ 'in',
+ qb.selectFrom('nodes').select('id').where('type', '=', 'space'),
+ ),
+ ]),
+ )
+ .compile();
- if (event.event === 'node_deleted') {
- const deletedNodeId = event.payload as string;
- store.deleteNode(deletedNodeId);
- }
- });
+ const queryId = queryKey[0];
+ return await workspace.executeQueryAndSubscribe(queryId, query);
+ },
+ });
- return () => {
- eventBus.unsubscribe(subscriptionId);
- };
- }, [workspace.id]);
+ if (isPending) {
+ return <Spinner />;
+ }
const currentLayout = 'spaces';
+ const nodes: Node[] = data?.rows.map((row) => mapNode(row)) ?? [];
return (
<SidebarContext.Provider
value={{
- nodes: store.getNodes(),
+ nodes: nodes,
}}
>
<div className="grid h-full max-h-screen w-full grid-cols-[4rem_1fr] grid-rows-[auto_1fr_auto] border-r border-gray-200">
diff --git a/desktop/src/components/workspaces/workspace-create.tsx b/desktop/src/components/workspaces/workspace-create.tsx
index d136bc54..0cbda045 100644
--- a/desktop/src/components/workspaces/workspace-create.tsx
+++ b/desktop/src/components/workspaces/workspace-create.tsx
@@ -15,23 +15,31 @@ import { Spinner } from '@/components/ui/spinner';
import { Input } from '@/components/ui/input';
import { Textarea } from '@/components/ui/textarea';
import { toast } from '@/components/ui/use-toast';
-import {parseApiError} from "@/lib/axios";
-import {Workspace} from "@/types/workspaces";
-import {useAxios} from "@/contexts/axios";
-import {useStore} from "@/contexts/store";
-import {observer} from "mobx-react-lite";
+import { parseApiError } from '@/lib/axios';
+import { observer } from 'mobx-react-lite';
+import { useMutation } from '@tanstack/react-query';
+import { Workspace } from '@/types/workspaces';
+import { useAppDatabase } from '@/contexts/app-database';
+import { useAxios } from '@/contexts/axios';
const formSchema = z.object({
name: z.string().min(3, 'Name must be at least 3 characters long.'),
description: z.string(),
});
+type formSchemaType = z.infer<typeof formSchema>;
+
export const WorkspaceCreate = observer(() => {
- const store = useStore();
+ const appDatabase = useAppDatabase();
const axios = useAxios();
+ const { mutate, isPending } = useMutation({
+ mutationFn: async (input: formSchemaType) => {
+ const { data } = await axios.post<Workspace>('v1/workspaces', input);
+ return data;
+ },
+ });
- const [isPending, setIsPending] = React.useState(false);
- const form = useForm<z.infer<typeof formSchema>>({
+ const form = useForm<formSchemaType>({
resolver: zodResolver(formSchema),
defaultValues: {
name: '',
@@ -39,31 +47,38 @@ export const WorkspaceCreate = observer(() => {
},
});
- const handleSubmit = async (values: z.infer<typeof formSchema>) => {
- setIsPending(true);
- try {
- const { data } = await axios.post<Workspace>('v1/workspaces', values);
- if (data) {
- store.addWorkspace(data);
- await window.globalDb.addWorkspace(data);
- } else {
+ const handleSubmit = async (values: formSchemaType) => {
+ mutate(values, {
+ onSuccess: async (data) => {
+ const insertWorkspaceQuery = appDatabase.database
+ .insertInto('workspaces')
+ .values({
+ id: data.id,
+ account_id: data.accountId,
+ name: data.name,
+ description: data.description,
+ avatar: data.avatar,
+ role: data.role,
+ synced_at: new Date().toISOString(),
+ user_id: data.userId,
+ version_id: data.versionId,
+ })
+ .compile();
+
+ await appDatabase.executeQuery(insertWorkspaceQuery);
+
+ window.location.href = '/';
+ },
+ onError: (error) => {
+ const apiError = parseApiError(error);
toast({
title: 'Failed to create workspace',
- description: 'Invalid response from server',
+ description: apiError.message,
variant: 'destructive',
});
- }
- } catch (error) {
- const apiError = parseApiError(error);
- toast({
- title: 'Failed to create workspace',
- description: apiError.message,
- variant: 'destructive',
- });
- } finally {
- setIsPending(false);
- }
- }
+ },
+ });
+ };
return (
<div className="container flex flex-row justify-center">
@@ -82,20 +97,20 @@ export const WorkspaceCreate = observer(() => {
<FormField
control={form.control}
name="name"
- render={({field}) => (
+ render={({ field }) => (
<FormItem className="flex-1">
<FormLabel>Name *</FormLabel>
<FormControl>
<Input placeholder="Name" {...field} />
</FormControl>
- <FormMessage/>
+ <FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="description"
- render={({field}) => (
+ render={({ field }) => (
<FormItem>
<FormLabel>Description</FormLabel>
<FormControl>
@@ -104,14 +119,14 @@ export const WorkspaceCreate = observer(() => {
{...field}
/>
</FormControl>
- <FormMessage/>
+ <FormMessage />
</FormItem>
)}
/>
</div>
<div className="flex flex-row justify-end gap-2">
<Button type="submit" disabled={isPending}>
- {isPending && <Spinner className="mr-1"/>}
+ {isPending && <Spinner className="mr-1" />}
Create
</Button>
</div>
@@ -119,5 +134,5 @@ export const WorkspaceCreate = observer(() => {
</Form>
</div>
</div>
- )
-});
\ No newline at end of file
+ );
+});
diff --git a/desktop/src/components/workspaces/workspace-redirect.tsx b/desktop/src/components/workspaces/workspace-redirect.tsx
index 60b496ba..3fa163bf 100644
--- a/desktop/src/components/workspaces/workspace-redirect.tsx
+++ b/desktop/src/components/workspaces/workspace-redirect.tsx
@@ -1,21 +1,20 @@
import React from 'react';
-import { useStore } from '@/contexts/store';
import { useNavigate } from 'react-router-dom';
import { observer } from 'mobx-react-lite';
+import { useAccount } from '@/contexts/account';
export const WorkspaceRedirect = observer(() => {
- const store = useStore();
- const workspaces = store.workspaces;
+ const account = useAccount();
const navigate = useNavigate();
React.useEffect(() => {
- if (workspaces.length == 0) {
+ if (account.workspaces.length == 0) {
navigate('/create');
return;
}
- navigate(`/${workspaces[0].id}`);
- }, [workspaces, navigate]);
+ navigate(`/${account.workspaces[0].id}`);
+ }, [account.workspaces, navigate]);
return null;
});
diff --git a/desktop/src/components/workspaces/workspace.tsx b/desktop/src/components/workspaces/workspace.tsx
index d8a4f348..a80e744a 100644
--- a/desktop/src/components/workspaces/workspace.tsx
+++ b/desktop/src/components/workspaces/workspace.tsx
@@ -1,101 +1,120 @@
import React from 'react';
import { Sidebar } from '@/components/workspaces/sidebar';
import { WorkspaceContext } from '@/contexts/workspace';
-import { useStore } from '@/contexts/store';
import { observer } from 'mobx-react-lite';
import { Outlet, useNavigate, useParams } from 'react-router-dom';
+import {
+ DummyDriver,
+ Kysely,
+ SqliteAdapter,
+ SqliteIntrospector,
+ SqliteQueryCompiler,
+} from 'kysely';
+import { WorkspaceDatabaseSchema } from '@/data/schemas/workspace';
+import { useAccount } from '@/contexts/account';
+import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
+import { useEventBus } from '@/hooks/use-event-bus';
+
+const workspaceDatabase = new Kysely<WorkspaceDatabaseSchema>({
+ dialect: {
+ createAdapter: () => new SqliteAdapter(),
+ createDriver: () => new DummyDriver(),
+ createIntrospector: (db) => new SqliteIntrospector(db),
+ createQueryCompiler: () => new SqliteQueryCompiler(),
+ },
+});
export const Workspace = observer(() => {
const { workspaceId } = useParams<{ workspaceId: string }>();
- const store = useStore();
+ const account = useAccount();
const navigate = useNavigate();
+ const eventBus = useEventBus();
+ const workspace = account.workspaces.find((w) => w.id === workspaceId);
+
+ const queryClient = React.useMemo(() => {
+ return new QueryClient({
+ defaultOptions: {
+ queries: {
+ retry: false,
+ },
+ },
+ });
+ }, [workspaceId]);
+
+ React.useEffect(() => {
+ if (!workspace) {
+ return;
+ }
+
+ const id = eventBus.subscribe((event) => {
+ if (event.event === 'workspace_query_updated') {
+ const result = event.payload.result;
+ const queryKey = event.payload.queryId;
+
+ if (result && queryKey) {
+ queryClient.setQueryData([queryKey], result);
+ }
+ }
+ });
+
+ queryClient.getQueryCache().subscribe(async (event) => {
+ if (
+ event.type === 'removed' &&
+ event.query &&
+ event.query.queryKey &&
+ event.query.queryKey.length > 0
+ ) {
+ const queryKey = event.query.queryKey[0];
+ await window.neuron.unsubscribeWorkspaceQuery(
+ workspace.accountId,
+ workspace.id,
+ queryKey,
+ );
+ }
+ });
+
+ return () => {
+ eventBus.unsubscribe(id);
+ };
+ }, [workspace]);
- const workspace = store.getWorkspace(workspaceId);
if (!workspace) {
return <p>Workspace not found</p>;
}
return (
- <WorkspaceContext.Provider
- value={{
- ...workspace,
- createNode: async (input) => {
- await window.workspaceDb.createNode(
- workspace.accountId,
- workspace.id,
- input,
- );
- },
- createNodes: async (inputs) => {
- await window.workspaceDb.createNodes(
- workspace.accountId,
- workspace.id,
- inputs,
- );
- },
- updateNode: async (node) => {
- await window.workspaceDb.updateNode(
- workspace.accountId,
- workspace.id,
- node,
- );
- },
- deleteNode: async (nodeId) => {
- await window.workspaceDb.deleteNode(
- workspace.accountId,
- workspace.id,
- nodeId,
- );
- },
- deleteNodes: async (nodeIds) => {
- await window.workspaceDb.deleteNodes(
- workspace.accountId,
- workspace.id,
- nodeIds,
- );
- },
- getConversationNodes: async (conversationId, count, after) => {
- return await window.workspaceDb.getConversationNodes(
- workspace.accountId,
- workspace.id,
- conversationId,
- count,
- after,
- );
- },
- getDocumentNodes: async (documentId) => {
- return await window.workspaceDb.getDocumentNodes(
- workspace.accountId,
- workspace.id,
- documentId,
- );
- },
- getContainerNodes(containerId) {
- return window.workspaceDb.getContainerNodes(
- workspace.accountId,
- workspace.id,
- containerId,
- );
- },
- getSidebarNodes() {
- return window.workspaceDb.getSidebarNodes(
- workspace.accountId,
- workspace.id,
- );
- },
- navigateToNode(nodeId) {
- navigate(`/${workspaceId}/${nodeId}`);
- },
- }}
- >
- <div className="flex h-screen max-h-screen flex-row">
- <div className="w-96">
- <Sidebar />
+ <QueryClientProvider client={queryClient}>
+ <WorkspaceContext.Provider
+ value={{
+ ...workspace,
+ schema: workspaceDatabase,
+ executeQuery: (query) =>
+ window.neuron.executeWorkspaceQuery(
+ workspace.accountId,
+ workspace.id,
+ query,
+ ),
+ executeQueryAndSubscribe: (queryId, query) =>
+ window.neuron.executeWorkspaceQueryAndSubscribe(
+ workspace.accountId,
+ workspace.id,
+ queryId,
+ query,
+ ),
+ navigateToNode(nodeId) {
+ navigate(`/${workspaceId}/${nodeId}`);
+ },
+ }}
+ >
+ <div className="flex h-screen max-h-screen flex-row">
+ <div className="w-96">
+ <Sidebar />
+ </div>
+ <main className="min-w-128 h-full w-full flex-grow overflow-hidden bg-white">
+ <Outlet />
+ </main>
</div>
- <main className="min-w-128 h-full w-full flex-grow overflow-hidden bg-white">
- <Outlet />
- </main>
- </div>
- </WorkspaceContext.Provider>
+ </WorkspaceContext.Provider>
+ </QueryClientProvider>
);
});
diff --git a/desktop/src/contexts/account.ts b/desktop/src/contexts/account.ts
index a8c166d6..7e046874 100644
--- a/desktop/src/contexts/account.ts
+++ b/desktop/src/contexts/account.ts
@@ -1,7 +1,9 @@
import { createContext, useContext } from 'react';
import { Account } from '@/types/accounts';
+import { Workspace } from '@/types/workspaces';
interface AccountContext extends Account {
+ workspaces: Workspace[];
logout: () => void;
}
diff --git a/desktop/src/contexts/app-database.tsx b/desktop/src/contexts/app-database.tsx
new file mode 100644
index 00000000..b2e8621e
--- /dev/null
+++ b/desktop/src/contexts/app-database.tsx
@@ -0,0 +1,14 @@
+import React from 'react';
+import { CompiledQuery, Kysely, QueryResult } from 'kysely';
+import { AppDatabaseSchema } from '@/data/schemas/app';
+
+interface AppDatabaseContext {
+ database: Kysely<AppDatabaseSchema>;
+ executeQuery: <R>(query: CompiledQuery<R>) => Promise<QueryResult<R>>;
+}
+
+export const AppDatabaseContext = React.createContext<AppDatabaseContext>(
+ {} as AppDatabaseContext,
+);
+
+export const useAppDatabase = () => React.useContext(AppDatabaseContext)!;
diff --git a/desktop/src/contexts/store.ts b/desktop/src/contexts/store.ts
deleted file mode 100644
index 23040983..00000000
--- a/desktop/src/contexts/store.ts
+++ /dev/null
@@ -1,6 +0,0 @@
-import { createContext, useContext } from 'react';
-import { store, AppStore } from '@/store';
-
-export const StoreContext = createContext<AppStore>(store);
-
-export const useStore = () => useContext(StoreContext);
diff --git a/desktop/src/contexts/workspace.ts b/desktop/src/contexts/workspace.ts
index 6f21a794..56308df9 100644
--- a/desktop/src/contexts/workspace.ts
+++ b/desktop/src/contexts/workspace.ts
@@ -1,24 +1,16 @@
import { createContext, useContext } from 'react';
import { Workspace } from '@/types/workspaces';
-import { CreateNodeInput, Node, UpdateNodeInput } from '@/types/nodes';
+import { CompiledQuery, Kysely, QueryResult } from 'kysely';
+import { WorkspaceDatabaseSchema } from '@/data/schemas/workspace';
interface WorkspaceContext extends Workspace {
- createNode: (input: CreateNodeInput) => Promise<void>;
- createNodes: (inputs: CreateNodeInput[]) => Promise<void>;
- updateNode: (input: UpdateNodeInput) => Promise<void>;
- deleteNode: (nodeId: string) => Promise<void>;
- deleteNodes: (nodeIds: string[]) => Promise<void>;
-
+ schema: Kysely<WorkspaceDatabaseSchema>;
+ executeQuery: <R>(query: CompiledQuery<R>) => Promise<QueryResult<R>>;
+ executeQueryAndSubscribe: <R>(
+ queryId: string,
+ query: CompiledQuery<R>,
+ ) => Promise<QueryResult<R>>;
navigateToNode: (nodeId: string) => void;
-
- getSidebarNodes: () => Promise<Node[]>;
- getConversationNodes: (
- conversationId: string,
- count: number,
- after?: string | null,
- ) => Promise<Node[]>;
- getDocumentNodes: (documentId: string) => Promise<Node[]>;
- getContainerNodes: (containerId: string) => Promise<Node[]>;
}
export const WorkspaceContext = createContext<WorkspaceContext>(
diff --git a/desktop/src/data/account-manager.ts b/desktop/src/data/account-manager.ts
new file mode 100644
index 00000000..fa22dd10
--- /dev/null
+++ b/desktop/src/data/account-manager.ts
@@ -0,0 +1,104 @@
+import * as fs from 'node:fs';
+import { Account } from '@/types/accounts';
+import { Workspace } from '@/types/workspaces';
+import { WorkspaceManager } from '@/data/workspace-manager';
+import { SocketManager } from '@/data/socket-manager';
+
+export class AccountManager {
+ private readonly accountPath: string;
+ private readonly socket: SocketManager;
+ private readonly workspaces: Map<string, WorkspaceManager>;
+ private interval: NodeJS.Timeout;
+
+ constructor(account: Account, appPath: string, workspaces: Workspace[]) {
+ this.socket = new SocketManager(account);
+ this.workspaces = new Map<string, WorkspaceManager>();
+
+ this.accountPath = `${appPath}/${account.id}`;
+ if (!fs.existsSync(this.accountPath)) {
+ fs.mkdirSync(this.accountPath);
+ }
+
+ for (const workspace of workspaces) {
+ this.workspaces.set(
+ workspace.id,
+ new WorkspaceManager(account, workspace, this.accountPath),
+ );
+ }
+ }
+
+ public async init() {
+ for (const workspace of this.workspaces.values()) {
+ await workspace.init();
+ }
+
+ this.socket.init();
+ this.socket.on('transaction_ack', async (payload) => {
+ const transactionId = payload.id;
+ const workspaceId = payload.workspaceId;
+
+ if (!this.workspaces.has(workspaceId)) {
+ return;
+ }
+
+ const workspace = this.workspaces.get(workspaceId);
+ await workspace?.acknowledgeTransaction(transactionId);
+ });
+
+ this.startEventLoop();
+ }
+
+ public getWorkspace(workspaceId: string): WorkspaceManager | undefined {
+ return this.workspaces.get(workspaceId);
+ }
+
+ public async logout(): Promise<void> {
+ if (fs.existsSync(this.accountPath)) {
+ fs.rm(this.accountPath, { recursive: true, force: true }, (err) => {
+ if (err) {
+ console.error('Error deleting account directory: ', err);
+ }
+ });
+ }
+
+ if (this.socket) {
+ this.socket.close();
+ }
+
+ if (this.interval) {
+ clearInterval(this.interval);
+ }
+ }
+
+ private startEventLoop() {
+ this.interval = setInterval(async () => {
+ try {
+ await this.executeEventLoop();
+ } catch (error) {
+ console.error('Error in event loop: ', error);
+ }
+ }, 15);
+ }
+
+ private async executeEventLoop() {
+ this.socket.checkConnection();
+
+ if (!this.socket.isConnected()) {
+ return;
+ }
+
+ for (const workspace of this.workspaces.values()) {
+ const transactions = await workspace.getTransactions();
+ if (transactions.length === 0) {
+ continue;
+ }
+
+ for (const transaction of transactions) {
+ this.socket.send({
+ type: 'transaction',
+ payload: transaction,
+ });
+ }
+ }
+ }
+}
diff --git a/desktop/src/data/app-manager.ts b/desktop/src/data/app-manager.ts
new file mode 100644
index 00000000..2fd5f199
--- /dev/null
+++ b/desktop/src/data/app-manager.ts
@@ -0,0 +1,146 @@
+import { app } from 'electron';
+import {
+ CompiledQuery,
+ Kysely,
+ Migration,
+ Migrator,
+ QueryResult,
+ SqliteDialect,
+} from 'kysely';
+import { AccountManager } from '@/data/account-manager';
+import { AppDatabaseSchema } from '@/data/schemas/app';
+import { appDatabaseMigrations } from '@/data/migrations/app';
+import { Account } from '@/types/accounts';
+import { buildSqlite } from '@/data/utils';
+import { Workspace } from '@/types/workspaces';
+
+class AppManager {
+ private readonly accounts: Map<string, AccountManager>;
+ private readonly appPath: string;
+ private readonly database: Kysely<AppDatabaseSchema>;
+ private initPromise: Promise<void> | null = null;
+
+ constructor() {
+ this.accounts = new Map<string, AccountManager>();
+ this.appPath = app.getPath('userData');
+
+ const dialect = new SqliteDialect({
+ database: buildSqlite(`${this.appPath}/global.db`),
+ });
+
+ this.database = new Kysely<AppDatabaseSchema>({
+ dialect,
+ });
+ }
+
+ public async getAccount(
+ accountId: string,
+ ): Promise<AccountManager | undefined> {
+ await this.waitForInit();
+ return this.accounts.get(accountId);
+ }
+
+ public async init() {
+ if (!this.initPromise) {
+ this.initPromise = this.startInit();
+ }
+
+ await this.initPromise;
+ }
+
+ public async execute(
+ query: CompiledQuery<unknown>,
+ ): Promise<QueryResult<unknown>> {
+ await this.waitForInit();
+ return await this.database.executeQuery(query);
+ }
+
+ public async logout(accountId: string): Promise<void> {
+ await this.waitForInit();
+ const accountManager = this.accounts.get(accountId);
+ if (!accountManager) {
+ throw new Error(`Account not found: ${accountId}`);
+ }
+
+ await accountManager.logout();
+ this.accounts.delete(accountId);
+ }
+
+ private async waitForInit() {
+ if (!this.initPromise) {
+ this.initPromise = this.startInit();
+ }
+
+ await this.initPromise;
+ }
+
+ private async startInit() {
+ await this.migrate();
+
+ const accounts = await this.getAccounts();
+ const workspaces = await this.getWorkspaces();
+ for (const account of accounts) {
+ const accountWorkspaces = workspaces.filter(
+ (workspace) => workspace.accountId === account.id,
+ );
+ const accountManager = new AccountManager(
+ account,
+ this.appPath,
+ accountWorkspaces,
+ );
+ await accountManager.init();
+
+ this.accounts.set(account.id, accountManager);
+ }
+ }
+
+ private async migrate() {
+ const migrator = new Migrator({
+ db: this.database,
+ provider: {
+ getMigrations(): Promise<Record<string, Migration>> {
+ return Promise.resolve(appDatabaseMigrations);
+ },
+ },
+ });
+
+ await migrator.migrateToLatest();
+ }
+
+ private async getAccounts(): Promise<Account[]> {
+ const accounts = await this.database
+ .selectFrom('accounts')
+ .selectAll()
+ .execute();
+
+ return accounts.map((account) => ({
+ id: account.id,
+ name: account.name,
+ email: account.email,
+ avatar: account.avatar,
+ token: account.token,
+ deviceId: account.device_id,
+ }));
+ }
+
+ private async getWorkspaces(): Promise<Workspace[]> {
+ const workspaces = await this.database
+ .selectFrom('workspaces')
+ .selectAll()
+ .execute();
+
+ return workspaces.map((workspace) => ({
+ id: workspace.id,
+ accountId: workspace.account_id,
+ name: workspace.name,
+ description: workspace.description,
+ avatar: workspace.avatar,
+ versionId: workspace.version_id,
+ role: workspace.role,
+ userId: workspace.user_id,
+ syncedAt: workspace.synced_at ? new Date(workspace.synced_at) : null,
+ }));
+ }
+}
+
+export const appManager = new AppManager();
diff --git a/desktop/src/data/databases/workspace/migrations.ts b/desktop/src/data/databases/workspace/migrations.ts
deleted file mode 100644
index 387478a1..00000000
--- a/desktop/src/data/databases/workspace/migrations.ts
+++ /dev/null
@@ -1,33 +0,0 @@
-import { Migration } from 'kysely';
-
-const createNodesTable: Migration = {
- up: async (db) => {
- await db.schema
- .createTable('nodes')
- .addColumn('id', 'text', (col) => col.notNull().primaryKey())
- .addColumn('workspace_id', 'text', (col) => col.notNull())
- .addColumn('parent_id', 'text', (col) =>
- col.references('nodes.id').onDelete('cascade'),
- )
- .addColumn('type', 'text', (col) => col.notNull())
- .addColumn('index', 'text')
- .addColumn('attrs', 'text')
- .addColumn('content', 'text')
- .addColumn('created_at', 'text', (col) => col.notNull())
- .addColumn('updated_at', 'text')
- .addColumn('created_by', 'text', (col) => col.notNull())
- .addColumn('updated_by', 'text')
- .addColumn('version_id', 'text', (col) => col.notNull())
- .addColumn('server_created_at', 'text')
- .addColumn('server_updated_at', 'text')
- .addColumn('state', 'text')
- .execute();
- },
- down: async (db) => {
- await db.schema.dropTable('nodes').execute();
- },
-};
-
-export const workspaceDatabaseMigrations: Record<string, Migration> = {
- '202408011756_create_nodes_table': createNodesTable,
-};
diff --git a/desktop/src/data/databases/global/migrations.ts b/desktop/src/data/migrations/app.ts
similarity index 64%
rename from desktop/src/data/databases/global/migrations.ts
rename to desktop/src/data/migrations/app.ts
index cbf5b6cf..c9f6cf96 100644
--- a/desktop/src/data/databases/global/migrations.ts
+++ b/desktop/src/data/migrations/app.ts
@@ -24,13 +24,13 @@ const createWorkspacesTable: Migration = {
await db.schema
.createTable('workspaces')
.addColumn('id', 'text', (col) => col.notNull())
+ .addColumn('account_id', 'text', (col) => col.notNull())
.addColumn('name', 'text', (col) => col.notNull())
.addColumn('description', 'text')
.addColumn('avatar', 'text')
.addColumn('version_id', 'text', (col) => col.notNull())
- .addColumn('account_id', 'integer', (col) => col.notNull())
.addColumn('role', 'integer', (col) => col.notNull())
- .addColumn('user_node_id', 'text', (col) => col.notNull())
+ .addColumn('user_id', 'text', (col) => col.notNull())
.addColumn('synced_at', 'text')
.execute();
},
@@ -39,26 +39,7 @@ const createWorkspacesTable: Migration = {
},
};
-const createTransactionsTable: Migration = {
- up: async (db) => {
- await db.schema
- .createTable('transactions')
- .addColumn('id', 'text', (col) => col.notNull().primaryKey())
- .addColumn('workspace_id', 'text', (col) => col.notNull())
- .addColumn('account_id', 'text', (col) => col.notNull())
- .addColumn('user_id', 'text', (col) => col.notNull())
- .addColumn('type', 'text', (col) => col.notNull())
- .addColumn('input', 'text', (col) => col.notNull())
- .addColumn('created_at', 'text', (col) => col.notNull())
- .execute();
- },
- down: async (db) => {
- await db.schema.dropTable('transactions').execute();
- },
-};
-
-export const globalDatabaseMigrations: Record<string, Migration> = {
+export const appDatabaseMigrations: Record<string, Migration> = {
'202408011709_create_accounts_table': createAccountsTable,
'202408011712_create_workspaces_table': createWorkspacesTable,
- '202408011715_create_transactions_table': createTransactionsTable,
};
diff --git a/desktop/src/data/migrations/workspace.ts b/desktop/src/data/migrations/workspace.ts
new file mode 100644
index 00000000..f2d8c4e6
--- /dev/null
+++ b/desktop/src/data/migrations/workspace.ts
@@ -0,0 +1,190 @@
+import { Migration, sql } from 'kysely';
+
+const createNodesTable: Migration = {
+ up: async (db) => {
+ await db.schema
+ .createTable('nodes')
+ .addColumn('id', 'text', (col) => col.notNull().primaryKey())
+ .addColumn('workspace_id', 'text', (col) => col.notNull())
+ .addColumn('parent_id', 'text', (col) =>
+ col.references('nodes.id').onDelete('cascade'),
+ )
+ .addColumn('type', 'text', (col) => col.notNull())
+ .addColumn('index', 'text')
+ .addColumn('attrs', 'text')
+ .addColumn('content', 'text')
+ .addColumn('created_at', 'text', (col) => col.notNull())
+ .addColumn('updated_at', 'text')
+ .addColumn('created_by', 'text', (col) => col.notNull())
+ .addColumn('updated_by', 'text')
+ .addColumn('version_id', 'text', (col) => col.notNull())
+ .addColumn('server_created_at', 'text')
+ .addColumn('server_updated_at', 'text')
+ .addColumn('server_version_id', 'text')
+ .execute();
+ },
+ down: async (db) => {
+ await db.schema.dropTable('nodes').execute();
+ },
+};
+
+const createTransactionsTable: Migration = {
+ up: async (db) => {
+ await db.schema
+ .createTable('transactions')
+ .addColumn('id', 'text', (col) => col.notNull().primaryKey())
+ .addColumn('action', 'text', (col) => col.notNull())
+ .addColumn('table', 'text', (col) => col.notNull())
+ .addColumn('after', 'json')
+ .addColumn('before', 'json')
+ .addColumn('created_at', 'text', (col) => col.notNull())
+ .execute();
+ },
+ down: async (db) => {
+ await db.schema.dropTable('transactions').execute();
+ },
+};
+
+const createNodesInsertTrigger: Migration = {
+ up: async (db) => {
+ await sql`
+ CREATE TRIGGER after_insert_nodes
+ AFTER INSERT ON nodes
+ FOR EACH ROW
+ WHEN NEW.server_version_id is null
+ BEGIN
+ INSERT INTO transactions ('id', 'action', 'table', 'after', 'created_at')
+ VALUES (
+ transaction_id(),
+ 'insert',
+ 'nodes',
+ json_object(
+ 'id', NEW.'id',
+ 'workspace_id', NEW.'workspace_id',
+ 'parent_id', NEW.'parent_id',
+ 'type', NEW.'type',
+ 'index', NEW.'index',
+ 'attrs', NEW.'attrs',
+ 'content', NEW.'content',
+ 'created_at', NEW.'created_at',
+ 'updated_at', NEW.'updated_at',
+ 'created_by', NEW.'created_by',
+ 'updated_by', NEW.'updated_by',
+ 'version_id', NEW.'version_id',
+ 'server_created_at', NEW.'server_created_at',
+ 'server_updated_at', NEW.'server_updated_at',
+ 'server_version_id', NEW.'server_version_id'
+ ),
+ datetime('now')
+ );
+ END;
+ `.execute(db);
+ },
+ down: async (db) => {
+ await sql`DROP TRIGGER after_insert_nodes`.execute(db);
+ },
+};
+
+const createNodesUpdateTrigger: Migration = {
+ up: async (db) => {
+ await sql`
+ CREATE TRIGGER after_update_nodes
+ AFTER UPDATE ON nodes
+ FOR EACH ROW
+ WHEN NEW.version_id != NEW.server_version_id
+ BEGIN
+ INSERT INTO transactions ('id', 'action', 'table', 'before', 'after', 'created_at')
+ VALUES (
+ transaction_id(),
+ 'update',
+ 'nodes',
+ json_object(
+ 'id', OLD.'id',
+ 'workspace_id', OLD.'workspace_id',
+ 'parent_id', OLD.'parent_id',
+ 'type', OLD.'type',
+ 'index', OLD.'index',
+ 'attrs', OLD.'attrs',
+ 'content', OLD.'content',
+ 'created_at', OLD.'created_at',
+ 'updated_at', OLD.'updated_at',
+ 'created_by', OLD.'created_by',
+ 'updated_by', OLD.'updated_by',
+ 'version_id', OLD.'version_id',
+ 'server_created_at', OLD.'server_created_at',
+ 'server_updated_at', OLD.'server_updated_at',
+ 'server_version_id', OLD.'server_version_id'
+ ),
+ json_object(
+ 'id', NEW.'id',
+ 'workspace_id', NEW.'workspace_id',
+ 'parent_id', NEW.'parent_id',
+ 'type', NEW.'type',
+ 'index', NEW.'index',
+ 'attrs', NEW.'attrs',
+ 'content', NEW.'content',
+ 'created_at', NEW.'created_at',
+ 'updated_at', NEW.'updated_at',
+ 'created_by', NEW.'created_by',
+ 'updated_by', NEW.'updated_by',
+ 'version_id', NEW.'version_id',
+ 'server_created_at', NEW.'server_created_at',
+ 'server_updated_at', NEW.'server_updated_at',
+ 'server_version_id', NEW.'server_version_id'
+ ),
+ datetime('now')
+ );
+ END;
+ `.execute(db);
+ },
+ down: async (db) => {
+ await sql`DROP TRIGGER after_update_nodes`.execute(db);
+ },
+};
+
+const createDeleteNodesTrigger: Migration = {
+ up: async (db) => {
+ await sql`
+ CREATE TRIGGER after_delete_nodes
+ AFTER DELETE ON nodes
+ FOR EACH ROW
+ BEGIN
+ INSERT INTO transactions ('id', 'action', 'table', 'before', 'created_at')
+ VALUES (
+ transaction_id(),
+ 'delete',
+ 'nodes',
+ json_object(
+ 'id', OLD.'id',
+ 'workspace_id', OLD.'workspace_id',
+ 'parent_id', OLD.'parent_id',
+ 'type', OLD.'type',
+ 'index', OLD.'index',
+ 'attrs', OLD.'attrs',
+ 'content', OLD.'content',
+ 'created_at', OLD.'created_at',
+ 'updated_at', OLD.'updated_at',
+ 'created_by', OLD.'created_by',
+ 'updated_by', OLD.'updated_by',
+ 'version_id', OLD.'version_id',
+ 'server_created_at', OLD.'server_created_at',
+ 'server_updated_at', OLD.'server_updated_at',
+ 'server_version_id', OLD.'server_version_id'
+ ),
+ datetime('now')
+ );
+ END;
+ `.execute(db);
+ },
+ down: async (db) => {
+ await sql`DROP TRIGGER after_delete_nodes`.execute(db);
+ },
+};
+
+export const workspaceDatabaseMigrations: Record<string, Migration> = {
+ '202408011756_create_nodes_table': createNodesTable,
+ '202408011757_create_transactions_table': createTransactionsTable,
+ '202408011758_create_nodes_insert_trigger': createNodesInsertTrigger,
+ '202408011759_create_nodes_update_trigger': createNodesUpdateTrigger,
+ '202408011800_create_nodes_delete_trigger': createDeleteNodesTrigger,
+};
diff --git a/desktop/src/electron/database/global/schema.ts b/desktop/src/data/schemas/app.ts
similarity index 61%
rename from desktop/src/electron/database/global/schema.ts
rename to desktop/src/data/schemas/app.ts
index 902cb4af..302c7fed 100644
--- a/desktop/src/electron/database/global/schema.ts
+++ b/desktop/src/data/schemas/app.ts
@@ -9,29 +9,17 @@ export interface AccountsTableSchema {
export interface WorkspacesTableSchema {
id: string;
+ account_id: string;
name: string;
description: string | null;
avatar: string | null;
version_id: string;
- account_id: string;
role: number;
- user_node_id: string;
- synced_at: string | null;
-}
-
-export interface TransactionsTableSchema {
- id: string;
- workspace_id: string;
- account_id: string;
user_id: string;
- type: string;
- node_id: string;
- input: string;
- created_at: string;
+ synced_at: string | null;
}
-export interface GlobalDatabaseSchema {
+export interface AppDatabaseSchema {
accounts: AccountsTableSchema;
workspaces: WorkspacesTableSchema;
- transactions: TransactionsTableSchema;
}
diff --git a/desktop/src/electron/database/workspace/schema.ts b/desktop/src/data/schemas/workspace.ts
similarity index 55%
rename from desktop/src/electron/database/workspace/schema.ts
rename to desktop/src/data/schemas/workspace.ts
index c13033e2..c4e8f597 100644
--- a/desktop/src/electron/database/workspace/schema.ts
+++ b/desktop/src/data/schemas/workspace.ts
@@ -11,9 +11,21 @@ export interface NodesTableSchema {
created_by: string;
updated_by: string | null;
version_id: string;
- state: string | null;
+ server_created_at: string | null;
+ server_updated_at: string | null;
+ server_version_id: string | null;
+}
+
+export interface TransactionsTableSchema {
+ id: string;
+ action: string;
+ table: string;
+ after?: string | null;
+ before?: string | null;
+ created_at: string;
}
export interface WorkspaceDatabaseSchema {
nodes: NodesTableSchema;
+ transactions: TransactionsTableSchema;
}
diff --git a/desktop/src/data/socket-manager.ts b/desktop/src/data/socket-manager.ts
new file mode 100644
index 00000000..9bec8743
--- /dev/null
+++ b/desktop/src/data/socket-manager.ts
@@ -0,0 +1,95 @@
+import { WebSocket } from 'ws';
+import { Account } from '@/types/accounts';
+
+const SOCKET_URL = 'ws://localhost:3000';
+
+export type SocketMessage = {
+ type: string;
+ payload: any;
+};
+
+export class SocketManager {
+ private readonly account: Account;
+ private socket: WebSocket | null;
+ private closingCount: number;
+ private listeners: Map<string, ((payload: any) => void)[]>;
+
+ constructor(account: Account) {
+ this.account = account;
+ this.socket = null;
+ this.closingCount = 0;
+ this.listeners = new Map();
+ }
+
+ public init() {
+ this.closingCount = 0;
+ this.socket = new WebSocket(
+ `${SOCKET_URL}/v1/synapse?device_id=${this.account.deviceId}`,
+ );
+
+ this.socket.onmessage = (event) => {
+ let data: string;
+
+ if (typeof event.data === 'string') {
+ data = event.data;
+ } else if (event.data instanceof ArrayBuffer) {
+ data = new TextDecoder().decode(event.data);
+ } else {
+ console.error('Unsupported message data type:', typeof event.data);
+ return;
+ }
+
+ const message: SocketMessage = JSON.parse(data);
+ const listeners = this.listeners.get(message.type) || [];
+ for (const listener of listeners) {
+ listener(message.payload);
+ }
+ };
+
+ this.socket.onerror = (event) => {
+ console.error('Socket error:', event);
+ };
+ }
+
+ public isConnected(): boolean {
+ return this.socket !== null && this.socket.readyState === WebSocket.OPEN;
+ }
+
+ public close() {
+ if (this.socket) {
+ this.socket.close();
+ }
+ }
+
+ public checkConnection() {
+ if (this.isConnected()) {
+ return;
+ }
+
+ if (this.socket == null || this.socket.readyState === WebSocket.CLOSED) {
+ this.init();
+ return;
+ }
+
+ if (this.socket.readyState === WebSocket.CLOSING) {
+ this.closingCount++;
+
+ if (this.closingCount > 50) {
+ this.socket.terminate();
+ this.closingCount = 0;
+ }
+ }
+ }
+
+ public send(message: SocketMessage) {
+ if (this.isConnected()) {
+ this.socket?.send(JSON.stringify(message));
+ }
+ }
+
+ public on(type: string, listener: (payload: any) => void) {
+ const listeners = this.listeners.get(type) || [];
+ listeners.push(listener);
+ this.listeners.set(type, listeners);
+ }
+}
diff --git a/desktop/src/data/utils.ts b/desktop/src/data/utils.ts
new file mode 100644
index 00000000..516d9d18
--- /dev/null
+++ b/desktop/src/data/utils.ts
@@ -0,0 +1,96 @@
+import SQLite from 'better-sqlite3';
+import { Parser, AST, Select } from 'node-sql-parser';
+import { NeuronId } from '@/lib/id';
+import { QueryResult } from 'kysely';
+
+const parser = new Parser();
+const parseOptions = { database: 'Sqlite' };
+
+export const buildSqlite = (filename: string) => {
+ const database = new SQLite(filename);
+ database.pragma('journal_mode = WAL');
+ database.function('transaction_id', () =>
+ NeuronId.generate(NeuronId.Type.Transaction),
+ );
+
+ return database;
+};
+
+export const extractTablesFromSql = (sql: string): string[] => {
+ const tables: string[] = [];
+ const ast = parser.astify(sql, parseOptions);
+ if (Array.isArray(ast)) {
+ ast.forEach((astItem) => {
+ tables.push(...extractTablesFromAst(astItem));
+ });
+ } else {
+ tables.push(...extractTablesFromAst(ast));
+ }
+
+ return tables;
+};
+
+const extractTablesFromAst = (ast: AST): string[] => {
+ const tables: string[] = [];
+ if (ast.type === 'select') {
+ const selectAst = ast as Select;
+ selectAst.from.forEach((from) => {
+ const astFrom = from as any;
+ if (astFrom.table) {
+ tables.push(astFrom.table);
+ }
+ });
+
+ if (selectAst.with && selectAst.with.length > 0) {
+ selectAst.with.forEach((withItem) => {
+ const fromArray = withItem?.stmt?.ast?.from;
+ fromArray?.forEach((from) => {
+ const astFrom = from as any;
+ if (astFrom.table) {
+ tables.push(astFrom.table);
+ }
+ });
+ });
+ }
+ }
+ if (ast.type === 'insert') {
+ tables.push(ast.table[0].table);
+ } else if (ast.type === 'update' && ast.table) {
+ ast.table.forEach((from) => {
+ const astFrom = from as any;
+ if (astFrom.table) {
+ tables.push(astFrom.table);
+ }
+ });
+ } else if (ast.type === 'delete') {
+ tables.push(ast.table.table);
+ }
+
+ return tables;
+};
+
+export const resultHasChanged = <R>(
+ oldResult: QueryResult<R>,
+ newResult: QueryResult<R>,
+): boolean => {
+ if (oldResult.rows.length !== newResult.rows.length) {
+ return true;
+ }
+
+ for (let i = 0; i < oldResult.rows.length; i++) {
+ const oldRow = oldResult.rows[i];
+ const newRow = newResult.rows[i];
+
+ if (Object.keys(oldRow).length !== Object.keys(newRow).length) {
+ return true;
+ }
+
+ for (const key in oldRow) {
+ if (oldRow[key] !== newRow[key]) {
+ return true;
+ }
+ }
+ }
+
+ return false;
+};
diff --git a/desktop/src/data/workspace-manager.ts b/desktop/src/data/workspace-manager.ts
new file mode 100644
index 00000000..3ec55d4b
--- /dev/null
+++ b/desktop/src/data/workspace-manager.ts
@@ -0,0 +1,159 @@
+import * as fs from 'node:fs';
+import {
+ CompiledQuery,
+ Kysely,
+ Migration,
+ Migrator,
+ QueryResult,
+ SqliteDialect,
+} from 'kysely';
+import { Workspace } from '@/types/workspaces';
+import { WorkspaceDatabaseSchema } from '@/data/schemas/workspace';
+import { workspaceDatabaseMigrations } from '@/data/migrations/workspace';
+import { Account } from '@/types/accounts';
+import {
+ buildSqlite,
+ extractTablesFromSql,
+ resultHasChanged,
+} from '@/data/utils';
+import { SubscribedQueryData } from '@/types/databases';
+import {
+ Transaction,
+ TransactionAction,
+ TransactionTable,
+} from '@/types/transactions';
+import { eventBus } from '@/lib/event-bus';
+
+export class WorkspaceManager {
+ private readonly account: Account;
+ private readonly workspace: Workspace;
+ private readonly database: Kysely<WorkspaceDatabaseSchema>;
+ private readonly subscribers: Map<string, SubscribedQueryData<unknown>>;
+
+ private lastChangeAt: Date = new Date();
+
+ constructor(account: Account, workspace: Workspace, accountPath: string) {
+ this.account = account;
+ this.workspace = workspace;
+ this.subscribers = new Map();
+
+ const workspaceDir = `${accountPath}/${workspace.id}`;
+ if (!fs.existsSync(workspaceDir)) {
+ fs.mkdirSync(workspaceDir);
+ }
+
+ const dialect = new SqliteDialect({
+ database: buildSqlite(`${workspaceDir}/workspace.db`),
+ });
+
+ this.database = new Kysely<WorkspaceDatabaseSchema>({
+ dialect,
+ });
+ }
+
+ public async init() {
+ await this.migrate();
+ }
+
+ public async execute<T>(query: CompiledQuery<T>): Promise<QueryResult<T>> {
+ const result = await this.database.executeQuery(query);
+
+ if (result.numAffectedRows > 0) {
+ this.lastChangeAt = new Date();
+ const affectedTables = extractTablesFromSql(query.sql);
+
+ for (const [subscriberId, subscriberData] of this.subscribers) {
+ const hasAffectedTables = subscriberData.tables.some((table) =>
+ affectedTables.includes(table),
+ );
+
+ if (!hasAffectedTables) {
+ continue;
+ }
+
+ const newResult = await this.database.executeQuery(
+ subscriberData.query,
+ );
+
+ if (resultHasChanged(subscriberData.result, newResult)) {
+ eventBus.publish({
+ event: 'workspace_query_updated',
+ payload: {
+ queryId: subscriberId,
+ result: newResult,
+ },
+ });
+ }
+ }
+ }
+
+ return result;
+ }
+
+ public async executeAndSubscribe<T>(
+ queryId: string,
+ query: CompiledQuery<T>,
+ ): Promise<QueryResult<T>> {
+ const result = await this.database.executeQuery(query);
+ if (result.numAffectedRows > 0) {
+ throw new Error('Query should not have any side effects');
+ }
+
+ const selectedTables = extractTablesFromSql(query.sql);
+ const subscriberData: SubscribedQueryData<T> = {
+ query,
+ tables: selectedTables,
+ result: result,
+ };
+ this.subscribers.set(queryId, subscriberData);
+
+ return result;
+ }
+
+ public unsubscribe(subscriberId: string) {
+ this.subscribers.delete(subscriberId);
+ }
+
+ public async getTransactions(): Promise<Transaction[]> {
+ const transactions = await this.database
+ .selectFrom('transactions')
+ .selectAll()
+ .execute();
+
+ if (transactions.length === 0) {
+ return [];
+ }
+
+ return transactions.map((row) => {
+ return {
+ id: row.id,
+ workspaceId: this.workspace.id,
+ action: row.action as TransactionAction,
+ table: row.table as TransactionTable,
+ after: row.after,
+ before: row.before,
+ createdAt: row.created_at,
+ };
+ });
+ }
+
+ public async acknowledgeTransaction(transactionId: string) {
+ await this.database
+ .deleteFrom('transactions')
+ .where('id', '=', transactionId)
+ .execute();
+ }
+
+ private async migrate() {
+ const migrator = new Migrator({
+ db: this.database,
+ provider: {
+ getMigrations(): Promise<Record<string, Migration>> {
+ return Promise.resolve(workspaceDatabaseMigrations);
+ },
+ },
+ });
+
+ await migrator.migrateToLatest();
+ }
+}
diff --git a/desktop/src/electron/database/global/index.ts b/desktop/src/electron/database/global/index.ts
deleted file mode 100644
index 2095e6f6..00000000
--- a/desktop/src/electron/database/global/index.ts
+++ /dev/null
@@ -1,247 +0,0 @@
-import { app } from 'electron';
-import SQLite from 'better-sqlite3';
-import { Kysely, Migration, Migrator, SqliteDialect } from 'kysely';
-import { GlobalDatabaseSchema } from '@/electron/database/global/schema';
-import { Account } from '@/types/accounts';
-import { Workspace } from '@/types/workspaces';
-import { Transaction, TransactionType } from '@/types/transactions';
-import { globalDatabaseMigrations } from '@/electron/database/global/migrations';
-import { GlobalDatabaseData } from '@/types/global';
-import { WorkspaceDatabase } from '@/electron/database/workspace';
-import * as fs from 'node:fs';
-
-export class GlobalDatabase {
- appPath: string;
- database: Kysely<GlobalDatabaseSchema>;
- workspaceDatabases: Map<string, WorkspaceDatabase> = new Map();
-
- constructor() {
- this.appPath = app.getPath('userData');
- const dialect = new SqliteDialect({
- database: new SQLite(`${this.appPath}/global.db`),
- });
-
- this.database = new Kysely<GlobalDatabaseSchema>({
- dialect,
- });
- }
-
- init = async (): Promise<GlobalDatabaseData> => {
- await this.migrate();
-
- const accounts = await this.getAccounts();
- const workspaces = await this.getWorkspaces();
-
- for (const workspace of workspaces) {
- await this.initWorkspaceDatabase(workspace);
- }
-
- return {
- accounts,
- workspaces,
- };
- };
-
- migrate = async () => {
- const migrator = new Migrator({
- db: this.database,
- provider: {
- getMigrations(): Promise<Record<string, Migration>> {
- return Promise.resolve(globalDatabaseMigrations);
- },
- },
- });
-
- await migrator.migrateToLatest();
- };
-
- getWorkspaceDatabase = (
- accountId: string,
- workspaceId: string,
- ): WorkspaceDatabase => {
- const key = `${accountId}_${workspaceId}`;
- const workspaceDatabase = this.workspaceDatabases.get(key);
- if (!workspaceDatabase) {
- throw new Error(
- `Workspace database not found for workspace ID: ${workspaceId}`,
- );
- }
-
- return workspaceDatabase;
- };
-
- initWorkspaceDatabase = async (workspace: Workspace): Promise<void> => {
- const key = `${workspace.accountId}_${workspace.id}`;
- const accountPath = this.buildAccountDirectoryPath(workspace.accountId);
- if (!fs.existsSync(accountPath)) {
- fs.mkdirSync(accountPath);
- }
-
- const workspaceDatabasePath = this.buildWorkspaceDatabasePath(
- workspace.accountId,
- workspace.id,
- );
- const workspaceDatabase = new WorkspaceDatabase(
- workspace.accountId,
- workspace.id,
- workspace.userNodeId,
- this,
- workspaceDatabasePath,
- );
- await workspaceDatabase.migrate();
- this.workspaceDatabases.set(key, workspaceDatabase);
- };
-
- getAccounts = async (): Promise<Account[]> => {
- const accounts = await this.database
- .selectFrom('accounts')
- .selectAll()
- .execute();
-
- return accounts.map((account) => ({
- id: account.id,
- name: account.name,
- email: account.email,
- avatar: account.avatar,
- token: account.token,
- deviceId: account.device_id,
- }));
- };
-
- addAccount = async (account: Account) => {
- await this.database
- .insertInto('accounts')
- .values({
- id: account.id,
- name: account.name,
- avatar: account.avatar,
- email: account.email,
- token: account.token,
- device_id: account.deviceId,
- })
- .execute();
- };
-
- logout = async (accountId: string) => {
- await this.database
- .deleteFrom('accounts')
- .where('id', '=', accountId)
- .execute();
-
- await this.database
- .deleteFrom('workspaces')
- .where('account_id', '=', accountId)
- .execute();
-
- await this.database
- .deleteFrom('transactions')
- .where('account_id', '=', accountId)
- .execute();
-
- const accountDirectoryPath = this.buildAccountDirectoryPath(accountId);
- if (fs.existsSync(accountDirectoryPath)) {
- fs.rm(accountDirectoryPath, { recursive: true, force: true }, (err) => {
- if (err) {
- console.error('Error deleting account directory: ', err);
- }
- });
- }
- };
-
- getWorkspaces = async (): Promise<Workspace[]> => {
- const workspaces = await this.database
- .selectFrom('workspaces')
- .selectAll()
- .execute();
-
- return workspaces.map((workspace) => ({
- id: workspace.id,
- name: workspace.name,
- description: workspace.description,
- avatar: workspace.avatar,
- versionId: workspace.version_id,
- accountId: workspace.account_id,
- role: workspace.role,
- userNodeId: workspace.user_node_id,
- syncedAt: workspace.synced_at ? new Date(workspace.synced_at) : null,
- }));
- };
-
- addWorkspace = async (workspace: Workspace) => {
- await this.database
- .insertInto('workspaces')
- .values({
- id: workspace.id,
- name: workspace.name,
- description: workspace.description,
- avatar: workspace.avatar,
- version_id: workspace.versionId,
- account_id: workspace.accountId,
- role: workspace.role,
- user_node_id: workspace.userNodeId,
- synced_at: workspace.syncedAt?.toISOString(),
- })
- .execute();
-
- await this.initWorkspaceDatabase(workspace);
- };
-
- addTransaction = async (transaction: Transaction) => {
- await this.database
- .insertInto('transactions')
- .values({
- id: transaction.id,
- type: transaction.type,
- account_id: transaction.accountId,
- user_id: transaction.userId,
- input: transaction.input,
- created_at: transaction.createdAt.toISOString(),
- workspace_id: transaction.workspaceId,
- })
- .execute();
- };
-
- getTransactions = async (count: number): Promise<Transaction[]> => {
- const transactions = await this.database
- .selectFrom('transactions')
- .selectAll()
- .limit(count)
- .execute();
-
- return transactions.map((transaction) => ({
- id: transaction.id,
- workspaceId: transaction.workspace_id,
- accountId: transaction.account_id,
- userId: transaction.user_id,
- type: transaction.type as TransactionType,
- nodeId: transaction.node_id,
- input: transaction.input,
- createdAt: new Date(transaction.created_at),
- }));
- };
-
- deleteTransactions = async (ids: string[]) => {
- await this.database
- .deleteFrom('transactions')
- .where('id', 'in', ids)
- .execute();
- };
-
- updateWorkspaceSyncedAt = async (workspaceId: string, date: Date) => {
- await this.database
- .updateTable('workspaces')
- .set('synced_at', date.toISOString())
- .where('id', '=', workspaceId)
- .execute();
- };
-
- buildAccountDirectoryPath = (accountId: string) => {
- return `${this.appPath}/account_${accountId}`;
- };
-
- buildWorkspaceDatabasePath = (accountId: string, workspaceId: string) => {
- return `${this.buildAccountDirectoryPath(accountId)}/workspace_${workspaceId}.db`;
- };
-}
-
-export const globalDatabase = new GlobalDatabase();
diff --git a/desktop/src/electron/database/workspace/index.ts b/desktop/src/electron/database/workspace/index.ts
deleted file mode 100644
index 78b86a77..00000000
--- a/desktop/src/electron/database/workspace/index.ts
+++ /dev/null
@@ -1,520 +0,0 @@
-import SQLite from 'better-sqlite3';
-import { Kysely, Migration, Migrator, SqliteDialect } from 'kysely';
-import {
- NodesTableSchema,
- WorkspaceDatabaseSchema,
-} from '@/electron/database/workspace/schema';
-import { workspaceDatabaseMigrations } from '@/electron/database/workspace/migrations';
-import { GlobalDatabase } from '@/electron/database/global';
-import { CreateNodeInput, Node, UpdateNodeInput } from '@/types/nodes';
-import { NeuronId } from '@/lib/id';
-import { LeafNodeTypes, NodeTypes, RootNodeTypes } from '@/lib/constants';
-import { eventBus } from '@/lib/event-bus';
-import {
- CreateNodeTransactionInput,
- UpdateNodeTransactionInput,
-} from '@/types/transactions';
-
-export class WorkspaceDatabase {
- accountId: string;
- workspaceId: string;
- userId: string;
- database: Kysely<WorkspaceDatabaseSchema>;
- globalDatabase: GlobalDatabase;
-
- constructor(
- accountId: string,
- workspaceId: string,
- userId: string,
- globalDatabase: GlobalDatabase,
- path: string,
- ) {
- this.accountId = accountId;
- this.workspaceId = workspaceId;
- this.globalDatabase = globalDatabase;
- this.userId = userId;
-
- const dialect = new SqliteDialect({
- database: new SQLite(path),
- });
-
- this.database = new Kysely<WorkspaceDatabaseSchema>({
- dialect,
- });
- }
-
- getNodes = async (): Promise<Node[]> => {
- const nodes = await this.database.selectFrom('nodes').selectAll().execute();
-
- return nodes.map((node) => ({
- id: node.id,
- type: node.type,
- index: node.index,
- parentId: node.parent_id,
- workspaceId: node.workspace_id,
- attrs: JSON.parse(node.attrs),
- content: JSON.parse(node.content),
- createdAt: new Date(node.created_at),
- createdBy: node.created_by,
- updatedAt: node.updated_at ? new Date(node.updated_at) : null,
- updatedBy: node.updated_by,
- versionId: node.version_id,
- }));
- };
-
- createNode = async (input: CreateNodeInput) => {
- const insertedRow = await this.database
- .insertInto('nodes')
- .values({
- id: input.id,
- type: input.type,
- index: input.index,
- parent_id: input.parentId,
- workspace_id: this.workspaceId,
- created_at: new Date().toISOString(),
- created_by: this.userId,
- version_id: NeuronId.generate(NeuronId.Type.Version),
- attrs: input.attrs && JSON.stringify(input.attrs),
- content: input.content && JSON.stringify(input.content),
- })
- .returningAll()
- .executeTakeFirst();
-
- const transactionInput = this.mapToCreateNodeTransactionInput(insertedRow);
- await this.globalDatabase.addTransaction({
- id: NeuronId.generate(NeuronId.Type.Transaction),
- type: 'create_node',
- workspaceId: this.workspaceId,
- userId: this.userId,
- accountId: this.accountId,
- input: JSON.stringify(transactionInput),
- createdAt: new Date(),
- });
-
- const node = this.mapToNode(insertedRow);
- eventBus.publish({
- event: 'node_created',
- payload: node,
- });
- };
-
- createNodes = async (inputs: CreateNodeInput[]) => {
- const insertedRows = await this.database
- .insertInto('nodes')
- .values(
- inputs.map((input) => ({
- id: input.id,
- type: input.type,
- index: input.index,
- parent_id: input.parentId,
- workspace_id: this.workspaceId,
- created_at: new Date().toISOString(),
- created_by: this.userId,
- version_id: NeuronId.generate(NeuronId.Type.Version),
- attrs: input.attrs && JSON.stringify(input.attrs),
- content: input.content && JSON.stringify(input.content),
- })),
- )
- .returningAll()
- .execute();
-
- const transactionInputs = insertedRows.map((node) =>
- this.mapToCreateNodeTransactionInput(node),
- );
- await this.globalDatabase.addTransaction({
- id: NeuronId.generate(NeuronId.Type.Transaction),
- type: 'create_nodes',
- workspaceId: this.workspaceId,
- userId: this.userId,
- accountId: this.accountId,
- input: JSON.stringify(transactionInputs),
- createdAt: new Date(),
- });
-
- insertedRows.forEach((row) => {
- const node = this.mapToNode(row);
- eventBus.publish({
- event: 'node_created',
- payload: node,
- });
- });
- };
-
- updateNode = async (input: UpdateNodeInput) => {
- const row = await this.database
- .selectFrom('nodes')
- .selectAll()
- .where('id', '=', input.id)
- .executeTakeFirst();
-
- if (!row) {
- return;
- }
-
- let updateDefinition = this.database.updateTable('nodes').set({
- updated_at: new Date().toISOString(),
- updated_by: this.userId,
- version_id: NeuronId.generate(NeuronId.Type.Version),
- });
-
- if (input.attrs !== undefined) {
- const existingAttrs = row.attrs && JSON.parse(row.attrs);
- const updatedAttrs = {
- ...existingAttrs,
- ...input.attrs,
- };
-
- updateDefinition = updateDefinition.set(
- 'attrs',
- JSON.stringify(updatedAttrs),
- );
- }
-
- if (input.content !== undefined) {
- const content =
- input.content === null ? null : JSON.stringify(input.content);
- updateDefinition = updateDefinition.set('content', content);
- }
-
- if (input.index !== undefined) {
- updateDefinition = updateDefinition.set('index', input.index);
- }
-
- if (input.parentId !== undefined) {
- updateDefinition = updateDefinition.set('parent_id', input.parentId);
- }
-
- const updatedRow = await updateDefinition
- .where('id', '=', input.id)
- .returningAll()
- .executeTakeFirst();
-
- const transactionInput = this.mapToUpdateNodeTransactionInput(updatedRow);
- await this.globalDatabase.addTransaction({
- id: NeuronId.generate(NeuronId.Type.Transaction),
- type: 'update_node',
- workspaceId: updatedRow.workspace_id,
- accountId: this.accountId,
- userId: this.userId,
- input: JSON.stringify(transactionInput),
- createdAt: new Date(),
- });
-
- const node: Node = this.mapToNode(updatedRow);
- eventBus.publish({
- event: 'node_updated',
- payload: node,
- });
- };
-
- deleteNode = async (nodeId: string) => {
- const deletedRow = await this.database
- .deleteFrom('nodes')
- .where('id', '=', nodeId)
- .returningAll()
- .executeTakeFirst();
-
- if (!deletedRow) {
- return;
- }
-
- await this.globalDatabase.addTransaction({
- id: NeuronId.generate(NeuronId.Type.Transaction),
- type: 'delete_node',
- workspaceId: this.workspaceId,
- accountId: this.accountId,
- userId: this.userId,
- input: nodeId,
- createdAt: new Date(),
- });
-
- const node: Node = this.mapToNode(deletedRow);
- eventBus.publish({
- event: 'node_deleted',
- payload: node,
- });
- };
-
- deleteNodes = async (nodeIds: string[]) => {
- const deletedRows = await this.database
- .deleteFrom('nodes')
- .where('id', 'in', nodeIds)
- .returningAll()
- .execute();
-
- await this.globalDatabase.addTransaction({
- id: NeuronId.generate(NeuronId.Type.Transaction),
- type: 'delete_nodes',
- workspaceId: this.workspaceId,
- accountId: this.accountId,
- userId: this.userId,
- input: JSON.stringify(nodeIds),
- createdAt: new Date(),
- });
-
- const nodes = deletedRows.map((node) => this.mapToNode(node));
- nodes.forEach((node) => {
- eventBus.publish({
- event: 'node_deleted',
- payload: node,
- });
- });
- };
-
- syncNodes = async (nodes: Node[]) => {
- const nodeIds = nodes.map((node) => node.id);
- const existingNodes = await this.database
- .selectFrom('nodes')
- .selectAll()
- .where('id', 'in', nodeIds)
- .execute();
-
- for (const node of nodes) {
- const existingNode = existingNodes.find((n) => n.id === node.id);
- if (!existingNode) {
- await this.database
- .insertInto('nodes')
- .values({
- id: node.id,
- type: node.type,
- index: node.index,
- parent_id: node.parentId,
- workspace_id: this.workspaceId,
- created_at: new Date(node.createdAt).toISOString(),
- created_by: node.createdBy,
- version_id: node.versionId,
- attrs: JSON.stringify(node.attrs),
- content: JSON.stringify(node.content),
- updated_at: node.updatedAt
- ? new Date(node.updatedAt).toISOString()
- : null,
- updated_by: node.updatedBy,
- })
- .execute();
- } else if (
- existingNode.version_id != node.versionId &&
- new Date(existingNode.updated_at) <= node.updatedAt
- ) {
- await this.database
- .updateTable('nodes')
- .set({
- type: node.type,
- index: node.index,
- parent_id: node.parentId,
- updated_at: node.updatedAt.toISOString(),
- updated_by: node.updatedBy,
- version_id: node.versionId,
- attrs: JSON.stringify(node.attrs),
- content: JSON.stringify(node.content),
- })
- .where('id', '=', node.id)
- .executeTakeFirst();
- }
- }
- };
-
- getSidebarNodes: () => Promise<Node[]> = async () => {
- const spaceRows = await this.database
- .selectFrom('nodes')
- .selectAll()
- .where('type', '=', NodeTypes.Space)
- .execute();
-
- const spaceIds = spaceRows.map((space) => space.id);
- const childRows = await this.database
- .selectFrom('nodes')
- .selectAll()
- .where('parent_id', 'in', spaceIds)
- .execute();
-
- const rows = [...spaceRows, ...childRows];
- return rows.map((row) => this.mapToNode(row));
- };
-
- getConversationNodes = async (
- conversationId: string,
- count: number,
- after?: string | null,
- ): Promise<Node[]> => {
- const messageQuery = this.database
- .selectFrom('nodes')
- .selectAll()
- .where('type', '=', 'message')
- .where('parent_id', '=', conversationId);
-
- if (after) {
- messageQuery.where('id', '<', after);
- }
-
- const messageRows = await messageQuery
- .orderBy('created_at', 'desc')
- .limit(count)
- .execute();
-
- const authorIds = messageRows.map((message) => message.created_by);
- const authorRows = await this.database
- .selectFrom('nodes')
- .selectAll()
- .where('id', 'in', authorIds)
- .execute();
-
- const parentIds = messageRows.map((message) => message.id);
- const rows = [...messageRows, ...authorRows];
-
- while (parentIds.length > 0) {
- const childRows = await this.database
- .selectFrom('nodes')
- .selectAll()
- .where('parent_id', 'in', parentIds)
- .execute();
-
- rows.push(...childRows);
- parentIds.splice(0, parentIds.length);
-
- const newParentIds = childRows
- .filter(
- (row) =>
- !RootNodeTypes.includes(row.type) &&
- !LeafNodeTypes.includes(row.type),
- )
- .map((row) => row.id);
-
- parentIds.push(...newParentIds);
- }
-
- return rows.map((row) => this.mapToNode(row));
- };
-
- getDocumentNodes = async (documentId: string): Promise<Node[]> => {
- const rows = await this.database
- .selectFrom('nodes')
- .selectAll()
- .where('parent_id', '=', documentId)
- .execute();
-
- const parentIds = rows
- .filter(
- (row) =>
- !RootNodeTypes.includes(row.type) &&
- !LeafNodeTypes.includes(row.type),
- )
- .map((row) => row.id);
-
- while (parentIds.length > 0) {
- const childRows = await this.database
- .selectFrom('nodes')
- .selectAll()
- .where('parent_id', 'in', parentIds)
- .execute();
-
- rows.push(...childRows);
- parentIds.splice(0, parentIds.length);
-
- const childParentIds = childRows
- .filter(
- (row) =>
- !RootNodeTypes.includes(row.type) &&
- !LeafNodeTypes.includes(row.type),
- )
- .map((row) => row.id);
-
- parentIds.push(...childParentIds);
- }
-
- return rows.map((row) => this.mapToNode(row));
- };
-
- getContainerNodes = async (containerId: string): Promise<Node[]> => {
- const containerRow = await this.database
- .selectFrom('nodes')
- .selectAll()
- .where('id', '=', containerId)
- .executeTakeFirst();
-
- if (!containerRow) {
- return [];
- }
-
- const rows = [containerRow];
- let parentId = containerRow.parent_id;
-
- while (parentId) {
- const parentRow = await this.database
- .selectFrom('nodes')
- .selectAll()
- .where('id', '=', parentId)
- .executeTakeFirst();
-
- if (!parentRow) {
- break;
- }
-
- rows.push(parentRow);
- parentId = parentRow.parent_id;
- }
-
- return rows.map((row) => this.mapToNode(row));
- };
-
- migrate = async () => {
- const migrator = new Migrator({
- db: this.database,
- provider: {
- getMigrations(): Promise<Record<string, Migration>> {
- return Promise.resolve(workspaceDatabaseMigrations);
- },
- },
- });
-
- await migrator.migrateToLatest();
- };
-
- mapToNode = (node: NodesTableSchema): Node => {
- return {
- id: node.id,
- type: node.type,
- index: node.index,
- parentId: node.parent_id,
- workspaceId: node.workspace_id,
- attrs: node.attrs && JSON.parse(node.attrs),
- content: node.content && JSON.parse(node.content),
- createdAt: new Date(node.created_at),
- createdBy: node.created_by,
- updatedAt: node.updated_at ? new Date(node.updated_at) : null,
- updatedBy: node.updated_by,
- versionId: node.version_id,
- };
- };
-
- mapToCreateNodeTransactionInput = (
- node: NodesTableSchema,
- ): CreateNodeTransactionInput => {
- return {
- id: node.id,
- workspaceId: node.workspace_id,
- parentId: node.parent_id,
- type: node.type,
- index: node.index,
- attrs: node.attrs && JSON.parse(node.attrs),
- content: node.content && JSON.parse(node.content),
- createdAt: new Date(node.created_at),
- createdBy: node.created_by,
- versionId: node.version_id,
- };
- };
-
- mapToUpdateNodeTransactionInput = (
- node: NodesTableSchema,
- ): UpdateNodeTransactionInput => {
- return {
- id: node.id,
- parentId: node.parent_id,
- index: node.index,
- attrs: node.attrs && JSON.parse(node.attrs),
- content: node.content && JSON.parse(node.content),
- updatedAt: new Date(node.updated_at),
- updatedBy: node.updated_by,
- versionId: node.version_id,
- };
- };
-}
diff --git a/desktop/src/electron/event-loop.ts b/desktop/src/electron/event-loop.ts
deleted file mode 100644
index 0d4847b1..00000000
--- a/desktop/src/electron/event-loop.ts
+++ /dev/null
@@ -1,97 +0,0 @@
-import { globalDatabase } from '@/electron/database/global';
-import { Account } from '@/types/accounts';
-import { Transaction } from '@/types/transactions';
-import { Node } from '@/types/nodes';
-
-export const initEventLoop = () => {
- setInterval(async () => {
- const accounts = await globalDatabase.getAccounts();
- await sendTransactions(accounts);
- await syncWorkspaces(accounts);
- }, 10000);
-};
-
-const sendTransactions = async (accounts: Account[]) => {
- const transactions = await globalDatabase.getTransactions(20);
- const groupedAccountTransactions = transactions.reduce(
- (acc, transaction) => {
- const account = accounts.find((a) => a.id === transaction.accountId);
- if (!account) {
- return acc;
- }
-
- const accountTransactions = acc.find((a) => a.account === account);
- if (accountTransactions) {
- accountTransactions.transactions.push(transaction);
- } else {
- acc.push({
- account,
- transactions: [transaction],
- });
- }
-
- return acc;
- },
- [] as { account: Account; transactions: Transaction[] }[],
- );
-
- for (const accountTransactions of groupedAccountTransactions) {
- const transactionIds = accountTransactions.transactions.map((t) => t.id);
- const response = await fetch('http://localhost:3000/v1/transactions', {
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json',
- Authorization: `Bearer ${accountTransactions.account.token}`,
- },
- body: JSON.stringify({
- transactions: accountTransactions.transactions,
- }),
- });
-
- if (response.ok) {
- await globalDatabase.deleteTransactions(transactionIds);
- }
- }
-};
-
-const syncWorkspaces = async (accounts: Account[]) => {
- const workspaces = await globalDatabase.getWorkspaces();
- for (const workspace of workspaces) {
- const account = accounts.find((a) => a.id === workspace.accountId);
- if (!account) {
- continue;
- }
-
- const date = new Date();
- let url = `http://localhost:3000/v1/workspaces/${workspace.id}/nodes`;
- if (workspace.syncedAt) {
- url += `?from=${workspace.syncedAt.toISOString()}`;
- }
- const response = await fetch(url, {
- method: 'GET',
- headers: {
- 'Content-Type': 'application/json',
- Authorization: `Bearer ${account.token}`,
- },
- });
-
- if (response.ok) {
- await globalDatabase.updateWorkspaceSyncedAt(workspace.id, date);
- const { nodes }: { nodes: Node[] } = await response.json();
- if (nodes.length == 0) {
- return;
- }
-
- const workspaceDatabase = globalDatabase.getWorkspaceDatabase(
- workspace.accountId,
- workspace.id,
- );
-
- if (!workspaceDatabase) {
- continue;
- }
-
- await workspaceDatabase.syncNodes(nodes);
- }
- }
-};
diff --git a/desktop/src/hooks/use-container.tsx b/desktop/src/hooks/use-container.tsx
deleted file mode 100644
index 4c9343b5..00000000
--- a/desktop/src/hooks/use-container.tsx
+++ /dev/null
@@ -1,82 +0,0 @@
-import React from 'react';
-import { useWorkspace } from '@/contexts/workspace';
-import { Node } from '@/types/nodes';
-import { useEventBus } from './use-event-bus';
-import { ContainerStore } from '@/store/container';
-
-interface useContainerResult {
- node: Node;
- breadcrumb: Node[];
-}
-
-export const useContainer = (containerId: string): useContainerResult => {
- const workspace = useWorkspace();
- const eventBus = useEventBus();
- const store = React.useMemo(() => new ContainerStore(), [containerId]);
-
- React.useEffect(() => {
- const fetchNodes = async () => {
- store.setIsLoading(true);
-
- const nodes = await workspace.getContainerNodes(containerId);
- store.setNodes(nodes);
-
- store.setIsLoading(false);
- };
-
- fetchNodes();
-
- const subscriptionId = eventBus.subscribe((event) => {
- if (event.event === 'node_created') {
- const createdNode = event.payload as Node;
- if (
- store.getNode(createdNode.id) ||
- createdNode.parentId === containerId
- ) {
- store.setNode(createdNode);
- } else {
- const parent = store.getNode(createdNode.parentId);
- if (parent) {
- store.setNode(createdNode);
- }
- }
- }
-
- if (event.event === 'node_updated') {
- const updatedNode = event.payload as Node;
- store.setNode(updatedNode);
- }
-
- if (event.event === 'node_deleted') {
- const deletedNodeId = event.payload as string;
- store.deleteNode(deletedNodeId);
- }
- });
-
- return () => {
- eventBus.unsubscribe(subscriptionId);
- };
- }, [containerId]);
-
- const containerNode = store.getNode(containerId);
-
- return {
- node: containerNode,
- breadcrumb: buildBreadcrumb(store, containerNode),
- };
-};
-
-const buildBreadcrumb = (store: ContainerStore, node?: Node | null): Node[] => {
- if (!node) {
- return [];
- }
-
- const breadcrumb = [];
- let parent = store.getNode(node.parentId);
- while (parent) {
- breadcrumb.unshift(parent);
- parent = store.getNode(parent.parentId);
- }
-
- return breadcrumb;
-};
diff --git a/desktop/src/hooks/use-conversation.tsx b/desktop/src/hooks/use-conversation.tsx
index c422a92a..9036c71b 100644
--- a/desktop/src/hooks/use-conversation.tsx
+++ b/desktop/src/hooks/use-conversation.tsx
@@ -1,90 +1,160 @@
-import React from 'react';
-import { CreateNodeInput, Node } from '@/types/nodes';
+import { Node } from '@/types/nodes';
import { useWorkspace } from '@/contexts/workspace';
-import { ConversationStore } from '@/store/conversation';
import { JSONContent } from '@tiptap/core';
import { NeuronId } from '@/lib/id';
-import { LeafNodeTypes } from '@/lib/constants';
-import { generateNodeIndex } from '@/lib/nodes';
-import { useEventBus } from './use-event-bus';
+import { LeafNodeTypes, NodeTypes } from '@/lib/constants';
+import { buildNodeWithChildren, generateNodeIndex, mapNode } from '@/lib/nodes';
+import { useQuery } from '@tanstack/react-query';
+import { sql } from 'kysely';
+import { NodesTableSchema } from '@/data/schemas/workspace';
+import { hashCode } from '@/lib/utils';
+import { MessageNode } from '@/types/messages';
interface useConversationResult {
isLoading: boolean;
- nodes: Node[];
+ messages: MessageNode[];
hasMore: boolean;
loadMore: () => void;
isLoadingMore: boolean;
createMessage: (content: JSONContent) => void;
}
+interface CreateNodeInput {
+ id: string;
+ parentId: string;
+ type: string;
+ attrs: Record<string, any>;
+ content?: JSONContent[];
+ index?: string | null;
+}
+
export const useConversation = (
conversationId: string,
): useConversationResult => {
const workspace = useWorkspace();
- const eventBus = useEventBus();
- const store = React.useMemo(() => new ConversationStore(), [conversationId]);
-
- React.useEffect(() => {
- const fetchNodes = async () => {
- store.setIsLoading(true);
- const nodes = await workspace.getConversationNodes(
- conversationId,
- 10,
- null,
- );
- store.setNodes(nodes);
- store.setIsLoading(false);
- };
- fetchNodes();
-
- const subscriptionId = eventBus.subscribe((event) => {
- if (event.event === 'node_created') {
- const createdNode = event.payload as Node;
- if (
- store.getNode(createdNode.id) ||
- createdNode.parentId === conversationId
- ) {
- store.setNode(createdNode);
- } else {
- const parent = store.getNode(createdNode.parentId);
- if (parent) {
- store.setNode(createdNode);
- }
- }
- } else if (event.event === 'node_updated') {
- const updatedNode = event.payload as Node;
- if (store.getNode(updatedNode.id)) {
- store.setNode(updatedNode);
- }
- } else if (event.event === 'node_deleted') {
- const deletedNode = event.payload as Node;
- if (store.getNode(deletedNode.id)) {
- store.deleteNode(deletedNode.id);
- }
- }
- });
-
- return () => {
- eventBus.unsubscribe(subscriptionId);
- };
- }, [conversationId]);
+ const messagesQuery = useQuery({
+ queryKey: [`conversation:messages:${conversationId}`],
+ queryFn: async ({ queryKey }) => {
+ const query = sql<NodesTableSchema>`
+ WITH RECURSIVE conversation_hierarchy AS (
+ SELECT *
+ FROM nodes
+ WHERE parent_id = ${conversationId} AND type = ${NodeTypes.Message}
+
+ UNION ALL
+
+ SELECT child.*
+ FROM nodes child
+ INNER JOIN conversation_hierarchy parent ON child.parent_id = parent.id
+ )
+ SELECT *
+ FROM conversation_hierarchy;
+ `.compile(workspace.schema);
+
+ const queryId = queryKey[0];
+ return await workspace.executeQueryAndSubscribe(queryId, query);
+ },
+ });
+
+ const authorIds = [
+ ...new Set(
+ messagesQuery.data?.rows
+ .filter((row) => row.type === NodeTypes.Message)
+ .map((row) => row.created_by) ?? [],
+ ),
+ ];
+
+ const authorIdsHash = hashCode(authorIds.join(','));
+ const authorsQuery = useQuery({
+ queryKey: [`authors:${authorIdsHash}`],
+ enabled: authorIds.length > 0,
+ queryFn: async ({ queryKey }) => {
+ const query = workspace.schema
+ .selectFrom('nodes')
+ .selectAll()
+ .where('id', 'in', authorIds)
+ .compile();
+
+ const queryId = queryKey[0];
+ return await workspace.executeQueryAndSubscribe(queryId, query);
+ },
+ });
const createMessage = async (content: JSONContent) => {
const inputs = buildMessageCreateInputs(conversationId, content);
- await workspace.createNodes(inputs);
+ const createMessageQuery = workspace.schema
+ .insertInto('nodes')
+ .values(
+ inputs.map((input) => {
+ return {
+ id: input.id,
+ type: input.type,
+ index: input.index,
+ parent_id: input.parentId,
+ workspace_id: workspace.id,
+ created_at: new Date().toISOString(),
+ created_by: workspace.userId,
+ version_id: NeuronId.generate(NeuronId.Type.Version),
+ attrs: input.attrs && JSON.stringify(input.attrs),
+ content: input.content && JSON.stringify(input.content),
+ };
+ }),
+ )
+ .compile();
+
+ await workspace.executeQuery(createMessageQuery);
};
+ const conversationNodes =
+ messagesQuery.data?.rows.map((row) => mapNode(row)) ?? [];
+ const authorNodes = authorsQuery.data?.rows.map((row) => mapNode(row)) ?? [];
+ const messages = buildMessages(conversationNodes, authorNodes);
+
return {
- isLoading: store.isLoading,
- nodes: store.getNodes(),
- hasMore: store.hasMore,
+ isLoading: messagesQuery.isPending,
+ messages: messages,
+ hasMore: false,
loadMore: () => console.log('load more'),
- isLoadingMore: store.isLoadingMore,
+ isLoadingMore: false,
createMessage: createMessage,
};
};
+const buildMessages = (nodes: Node[], authors: Node[]): MessageNode[] => {
+ const messages: MessageNode[] = [];
+ const authorMap = new Map<string, Node>();
+
+ for (const author of authors) {
+ authorMap.set(author.id, author);
+ }
+
+ for (const node of nodes) {
+ if (node.type !== NodeTypes.Message) {
+ continue;
+ }
+
+ const messageNode = buildNodeWithChildren(node, nodes);
+ const author = authorMap.get(node.createdBy);
+ const message: MessageNode = {
+ ...messageNode,
+ author,
+ };
+
+ messages.push(message);
+ }
+
+ return messages.sort((a, b) => {
+ if (a.id > b.id) {
+ return 1;
+ } else if (a.id < b.id) {
+ return -1;
+ } else {
+ return 0;
+ }
+ });
+};
+
const buildMessageCreateInputs = (
conversationId: string,
content: JSONContent,
diff --git a/desktop/src/hooks/use-document.tsx b/desktop/src/hooks/use-document.tsx
index 019321eb..3576d7de 100644
--- a/desktop/src/hooks/use-document.tsx
+++ b/desktop/src/hooks/use-document.tsx
@@ -1,11 +1,6 @@
import React from 'react';
import { debounce, isEqual } from 'lodash';
-import {
- CreateNodeInput,
- Node,
- NodeBlock,
- UpdateNodeInput,
-} from '@/types/nodes';
+import { Node, NodeBlock } from '@/types/nodes';
import { useWorkspace } from '@/contexts/workspace';
import { JSONContent } from '@tiptap/core';
import { DocumentStore } from '@/store/document';
@@ -37,8 +32,8 @@ export const useDocument = (node: Node): useNodeResult => {
React.useEffect(() => {
const fetchNodes = async () => {
- const nodes = await workspace.getDocumentNodes(node.id);
- store.setNodes(nodes);
+ // const nodes = await workspace.getDocumentNodes(node.id);
+ // store.setNodes(nodes);
store.setIsLoaded(true);
};
@@ -77,62 +72,62 @@ export const useDocument = (node: Node): useNodeResult => {
const onUpdate = React.useMemo(
() =>
debounce(async (updatedContent: JSONContent) => {
- const childrenNodes = buildNodesFromEditor(
- updatedContent.content,
- node.id,
- );
-
- const nodesToCreate: CreateNodeInput[] = [];
- const nodesToUpdate: UpdateNodeInput[] = [];
- const nodesToDelete: string[] = [];
-
- const currentNodes = store.nodes;
- fillIndexesFromNodes(childrenNodes, currentNodes);
- validateIndexes(childrenNodes);
-
- const editorNodes = flattenNodesFromEditor(childrenNodes);
- for (const editorNode of editorNodes) {
- const existingNode = currentNodes[editorNode.id];
- if (!existingNode) {
- nodesToCreate.push({
- id: editorNode.id,
- type: editorNode.type,
- parentId: editorNode.parentId,
- index: editorNode.index,
- attrs: editorNode.attrs,
- content: editorNode.content,
- });
- } else if (hasChanged(existingNode, editorNode)) {
- nodesToUpdate.push({
- id: editorNode.id,
- parentId: editorNode.parentId,
- index: editorNode.index,
- attrs: editorNode.attrs,
- content: editorNode.content,
- });
- }
- }
-
- const currentNodeIds = Object.keys(currentNodes);
- for (const nodeId of currentNodeIds) {
- if (!editorNodes.find((node) => node.id === nodeId)) {
- nodesToDelete.push(nodeId);
- }
- }
-
- if (nodesToCreate.length > 0) {
- await workspace.createNodes(nodesToCreate);
- }
-
- if (nodesToUpdate.length > 0) {
- for (const nodeToUpdate of nodesToUpdate) {
- await workspace.updateNode(nodeToUpdate);
- }
- }
-
- if (nodesToDelete.length > 0) {
- await workspace.deleteNodes(nodesToDelete);
- }
+ // const childrenNodes = buildNodesFromEditor(
+ // updatedContent.content,
+ // node.id,
+ // );
+ //
+ // const nodesToCreate: CreateNodeInput[] = [];
+ // const nodesToUpdate: UpdateNodeInput[] = [];
+ // const nodesToDelete: string[] = [];
+ //
+ // const currentNodes = store.nodes;
+ // fillIndexesFromNodes(childrenNodes, currentNodes);
+ // validateIndexes(childrenNodes);
+ //
+ // const editorNodes = flattenNodesFromEditor(childrenNodes);
+ // for (const editorNode of editorNodes) {
+ // const existingNode = currentNodes[editorNode.id];
+ // if (!existingNode) {
+ // nodesToCreate.push({
+ // id: editorNode.id,
+ // type: editorNode.type,
+ // parentId: editorNode.parentId,
+ // index: editorNode.index,
+ // attrs: editorNode.attrs,
+ // content: editorNode.content,
+ // });
+ // } else if (hasChanged(existingNode, editorNode)) {
+ // nodesToUpdate.push({
+ // id: editorNode.id,
+ // parentId: editorNode.parentId,
+ // index: editorNode.index,
+ // attrs: editorNode.attrs,
+ // content: editorNode.content,
+ // });
+ // }
+ // }
+ //
+ // const currentNodeIds = Object.keys(currentNodes);
+ // for (const nodeId of currentNodeIds) {
+ // if (!editorNodes.find((node) => node.id === nodeId)) {
+ // nodesToDelete.push(nodeId);
+ // }
+ // }
+ //
+ // if (nodesToCreate.length > 0) {
+ // await workspace.createNodes(nodesToCreate);
+ // }
+ //
+ // if (nodesToUpdate.length > 0) {
+ // for (const nodeToUpdate of nodesToUpdate) {
+ // await workspace.updateNode(nodeToUpdate);
+ // }
+ // }
+ //
+ // if (nodesToDelete.length > 0) {
+ // await workspace.deleteNodes(nodesToDelete);
+ // }
}, 500),
[node.id],
);
diff --git a/desktop/src/lib/event-bus.ts b/desktop/src/lib/event-bus.ts
index 7455655e..f905ce14 100644
--- a/desktop/src/lib/event-bus.ts
+++ b/desktop/src/lib/event-bus.ts
@@ -9,7 +9,7 @@ export interface Event {
}
export interface EventBus {
- subscribe(callback: (payload: any) => void): string;
+ subscribe(callback: (event: Event) => void): string;
unsubscribe(subscriptionId: string): void;
publish(event: Event): void;
}
@@ -23,7 +23,7 @@ export class EventBusService {
}
public subscribe(callback: (event: Event) => void): string {
- const id = (this.id++).toLocaleString();
+ const id = (this.id++).toLocaleString();
this.subscriptions.set(id, {
callback,
id,
diff --git a/desktop/src/lib/nodes.ts b/desktop/src/lib/nodes.ts
index 2707965a..67d65f04 100644
--- a/desktop/src/lib/nodes.ts
+++ b/desktop/src/lib/nodes.ts
@@ -1,3 +1,4 @@
+import { NodesTableSchema } from '@/data/schemas/workspace';
import { Node, NodeWithChildren } from '@/types/nodes';
import { generateKeyBetween } from 'fractional-indexing-jittered';
@@ -20,4 +21,27 @@ export const generateNodeIndex = (before?: string | null, after?: string | null)
const upper = after === undefined ? null : after;
return generateKeyBetween(lower, upper);
+}
+
+export const mapNode = (row: NodesTableSchema): Node => {
+ return {
+ id: row.id,
+ type: row.type,
+ index: row.index,
+ parentId: row.parent_id,
+ workspaceId: row.workspace_id,
+ attrs: row.attrs && JSON.parse(row.attrs),
+ content: row.content && JSON.parse(row.content),
+ createdAt: new Date(row.created_at),
+ createdBy: row.created_by,
+ updatedAt: row.updated_at ? new Date(row.updated_at) : null,
+ updatedBy: row.updated_by,
+ versionId: row.version_id,
+ serverCreatedAt: row.server_created_at
+ ? new Date(row.server_created_at)
+ : null,
+ serverUpdatedAt: row.server_updated_at
+ ? new Date(row.server_updated_at)
+ : null,
+ };
}
\ No newline at end of file
diff --git a/desktop/src/main.ts b/desktop/src/main.ts
index fbb370b7..80fd5916 100644
--- a/desktop/src/main.ts
+++ b/desktop/src/main.ts
@@ -1,8 +1,8 @@
import { app, ipcMain, BrowserWindow } from 'electron';
import path from 'path';
-import { globalDatabase } from '@/electron/database/global';
-import { initEventLoop } from '@/electron/event-loop';
import { eventBus } from '@/lib/event-bus';
+import { appManager } from '@/data/app-manager';
+import { CompiledQuery } from 'kysely';
let subscriptionId: string | null = null;
@@ -66,109 +66,66 @@ app.on('before-quit', () => {
});
// inter process handlers
-ipcMain.handle('init', async () => globalDatabase.init());
-ipcMain.handle('get-accounts', async () => globalDatabase.getAccounts());
-ipcMain.handle('add-account', async (_, account) =>
- globalDatabase.addAccount(account),
-);
-ipcMain.handle('get-workspaces', async () => globalDatabase.getWorkspaces());
-ipcMain.handle('add-workspace', async (_, workspace) =>
- globalDatabase.addWorkspace(workspace),
-);
-ipcMain.handle('add-transaction', async (_, transaction) =>
- globalDatabase.addTransaction(transaction),
-);
-ipcMain.handle('logout', async (_, accountId) =>
- globalDatabase.logout(accountId),
-);
-
-ipcMain.handle('create-node', async (_, accountId, workspaceId, input) => {
- const workspaceDb = globalDatabase.getWorkspaceDatabase(
- accountId,
- workspaceId,
- );
- await workspaceDb.createNode(input);
-});
-
-ipcMain.handle('create-nodes', async (_, accountId, workspaceId, inputs) => {
- const workspaceDb = globalDatabase.getWorkspaceDatabase(
- accountId,
- workspaceId,
- );
- await workspaceDb.createNodes(inputs);
-});
-
-ipcMain.handle('get-nodes', async (_, accountId, workspaceId) => {
- const workspaceDb = globalDatabase.getWorkspaceDatabase(
- accountId,
- workspaceId,
- );
- return workspaceDb.getNodes();
-});
+ipcMain.handle('init', async () => appManager.init());
+ipcMain.handle('logout', async (_, accountId) => appManager.logout(accountId));
-ipcMain.handle('update-node', async (_, accountId, workspaceId, input) => {
- const workspaceDb = globalDatabase.getWorkspaceDatabase(
- accountId,
- workspaceId,
- );
- await workspaceDb.updateNode(input);
-});
-
-ipcMain.handle('delete-node', async (_, accountId, workspaceId, nodeId) => {
- const workspaceDb = globalDatabase.getWorkspaceDatabase(
- accountId,
- workspaceId,
- );
- await workspaceDb.deleteNode(nodeId);
-});
-
-ipcMain.handle('delete-nodes', async (_, accountId, workspaceId, nodeIds) => {
- const workspaceDb = globalDatabase.getWorkspaceDatabase(
- accountId,
- workspaceId,
- );
- await workspaceDb.deleteNodes(nodeIds);
-});
-
-ipcMain.handle('get-sidebar-nodes', async (_, accountId, workspaceId) => {
- const workspaceDb = globalDatabase.getWorkspaceDatabase(
- accountId,
- workspaceId,
- );
- return await workspaceDb.getSidebarNodes();
+ipcMain.handle('execute-app-query', async (_, query: CompiledQuery) => {
+ return await appManager.execute(query);
});
ipcMain.handle(
- 'get-conversation-nodes',
- async (_, accountId, workspaceId, conversationId, count, after) => {
- const workspaceDb = globalDatabase.getWorkspaceDatabase(
- accountId,
- workspaceId,
- );
- return await workspaceDb.getConversationNodes(conversationId, count, after);
+ 'execute-workspace-query',
+ async (_, accountId: string, workspaceId: string, query: CompiledQuery) => {
+ const accountManager = await appManager.getAccount(accountId);
+ if (!accountManager) {
+ throw new Error(`Account not found: ${accountId}`);
+ }
+
+ const workspaceManager = accountManager.getWorkspace(workspaceId);
+ if (!workspaceManager) {
+ throw new Error(`Workspace not found: ${workspaceId}`);
+ }
+
+ return await workspaceManager.execute(query);
},
);
ipcMain.handle(
- 'get-document-nodes',
- async (_, accountId, workspaceId, documentId) => {
- const workspaceDb = globalDatabase.getWorkspaceDatabase(
- accountId,
- workspaceId,
- );
- return await workspaceDb.getDocumentNodes(documentId);
+ 'execute-workspace-query-and-subscribe',
+ async (
+ _,
+ accountId: string,
+ workspaceId: string,
+ queryId: string,
+ query: CompiledQuery,
+ ) => {
+ const accountManager = await appManager.getAccount(accountId);
+ if (!accountManager) {
+ throw new Error(`Account not found: ${accountId}`);
+ }
+
+ const workspaceManager = accountManager.getWorkspace(workspaceId);
+ if (!workspaceManager) {
+ throw new Error(`Workspace not found: ${workspaceId}`);
+ }
+
+ return await workspaceManager.executeAndSubscribe(queryId, query);
},
);
ipcMain.handle(
- 'get-container-nodes',
- async (_, accountId, workspaceId, containerId) => {
- const workspaceDb = globalDatabase.getWorkspaceDatabase(
- accountId,
- workspaceId,
- );
- return await workspaceDb.getContainerNodes(containerId);
+ 'unsubscribe-workspace-query',
+ async (_, accountId: string, workspaceId: string, queryId: string) => {
+ const accountManager = await appManager.getAccount(accountId);
+ if (!accountManager) {
+ throw new Error(`Account not found: ${accountId}`);
+ }
+
+ const workspaceManager = accountManager.getWorkspace(workspaceId);
+ if (!workspaceManager) {
+ throw new Error(`Workspace not found: ${workspaceId}`);
+ }
+
+ workspaceManager.unsubscribe(queryId);
},
);
-
-initEventLoop();
diff --git a/desktop/src/preload.ts b/desktop/src/preload.ts
index be242dc9..14db054c 100644
--- a/desktop/src/preload.ts
+++ b/desktop/src/preload.ts
@@ -1,83 +1,55 @@
import { contextBridge, ipcRenderer } from 'electron';
-import { Account } from '@/types/accounts';
-import { Workspace } from '@/types/workspaces';
-import { Transaction } from '@/types/transactions';
-import { CreateNodeInput, UpdateNodeInput } from '@/types/nodes';
import { eventBus, Event } from '@/lib/event-bus';
+import { CompiledQuery, QueryResult } from 'kysely';
-contextBridge.exposeInMainWorld('globalDb', {
+contextBridge.exposeInMainWorld('neuron', {
init: () => ipcRenderer.invoke('init'),
- getAccounts: () => ipcRenderer.invoke('get-accounts'),
- addAccount: (account: Account) => ipcRenderer.invoke('add-account', account),
- getWorkspaces: () => ipcRenderer.invoke('get-workspaces'),
- addWorkspace: (workspace: Workspace) =>
- ipcRenderer.invoke('add-workspace', workspace),
- addTransaction: (transaction: Transaction) =>
- ipcRenderer.invoke('add-transaction', transaction),
logout: (accountId: string) => ipcRenderer.invoke('logout', accountId),
-});
+ executeAppQuery: <R>(query: CompiledQuery<R>): Promise<QueryResult<R>> =>
+ ipcRenderer.invoke('execute-app-query', query),
-contextBridge.exposeInMainWorld('workspaceDb', {
- createNode: (accountId: string, workspaceId: string, node: CreateNodeInput) =>
- ipcRenderer.invoke('create-node', accountId, workspaceId, node),
- createNodes: (
- accountId: string,
- workspaceId: string,
- nodes: CreateNodeInput[],
- ) => ipcRenderer.invoke('create-nodes', accountId, workspaceId, nodes),
- getNodes: (accountId: string, workspaceId: string) =>
- ipcRenderer.invoke('get-nodes', accountId, workspaceId),
- updateNode: (
+ executeAccountQuery: <R>(
accountId: string,
- workspaceId: string,
- input: UpdateNodeInput,
- ) => ipcRenderer.invoke('update-node', accountId, workspaceId, input),
- deleteNode: (accountId: string, workspaceId: string, nodeId: string) =>
- ipcRenderer.invoke('delete-node', accountId, workspaceId, nodeId),
- deleteNodes: (accountId: string, workspaceId: string, nodeIds: string[]) =>
- ipcRenderer.invoke('delete-nodes', accountId, workspaceId, nodeIds),
+ query: CompiledQuery<R>,
+ ): Promise<QueryResult<R>> =>
+ ipcRenderer.invoke('execute-account-query', accountId, query),
- getSidebarNodes: (accountId: string, workspaceId: string) =>
- ipcRenderer.invoke('get-sidebar-nodes', accountId, workspaceId),
-
- getConversationNodes: (
+ executeWorkspaceQuery: <R>(
accountId: string,
workspaceId: string,
- conversationId: string,
- count: number,
- after?: string | null,
- ) =>
+ query: CompiledQuery<R>,
+ ): Promise<QueryResult<R>> =>
ipcRenderer.invoke(
- 'get-conversation-nodes',
+ 'execute-workspace-query',
accountId,
workspaceId,
- conversationId,
- count,
- after,
+ query,
),
- getDocumentNodes: (
+ executeWorkspaceQueryAndSubscribe: <R>(
accountId: string,
workspaceId: string,
- documentId: string,
- ) =>
+ queryId: string,
+ query: CompiledQuery<R>,
+ ): Promise<QueryResult<R>> =>
ipcRenderer.invoke(
- 'get-document-nodes',
+ 'execute-workspace-query-and-subscribe',
accountId,
workspaceId,
- documentId,
+ queryId,
+ query,
),
- getContainerNodes: (
+ unsubscribeWorkspaceQuery: (
accountId: string,
workspaceId: string,
- containerId: string,
+ queryId: string,
) =>
ipcRenderer.invoke(
- 'get-container-nodes',
+ 'unsubscribe-workspace-query',
accountId,
workspaceId,
- containerId,
+ queryId,
),
});
diff --git a/desktop/src/store/container.ts b/desktop/src/store/container.ts
deleted file mode 100644
index 0cb5a13e..00000000
--- a/desktop/src/store/container.ts
+++ /dev/null
@@ -1,40 +0,0 @@
-import { Node } from '@/types/nodes';
-import { makeAutoObservable } from 'mobx';
-
-export class ContainerStore {
- public nodes: Record<string, Node>;
- public isLoading: boolean;
-
- constructor() {
- this.nodes = {};
- this.isLoading = false;
-
- makeAutoObservable(this);
- }
-
- public setNode(node: Node) {
- this.nodes[node.id] = node;
- }
-
- public setNodes(nodes: Node[]) {
- nodes.forEach((node) => {
- this.nodes[node.id] = node;
- });
- }
-
- public deleteNode(nodeId: string) {
- delete this.nodes[nodeId];
- }
-
- public setIsLoading(isLoading: boolean) {
- this.isLoading = isLoading;
- }
-
- public getNode(nodeId: string) {
- return this.nodes[nodeId];
- }
-
- public getNodes() {
- return Object.values(this.nodes);
- }
-}
\ No newline at end of file
diff --git a/desktop/src/store/conversation.ts b/desktop/src/store/conversation.ts
deleted file mode 100644
index f93c4898..00000000
--- a/desktop/src/store/conversation.ts
+++ /dev/null
@@ -1,52 +0,0 @@
-import { Node } from '@/types/nodes';
-import { makeAutoObservable } from 'mobx';
-
-export class ConversationStore {
- public nodes: Record<string, Node>;
- public isLoading: boolean;
- public hasMore: boolean;
- public isLoadingMore: boolean;
-
- constructor() {
- this.nodes = {};
- this.isLoading = false;
- this.hasMore = false;
- this.isLoadingMore = false;
-
- makeAutoObservable(this);
- }
-
- public setNode(node: Node) {
- this.nodes[node.id] = node;
- }
-
- public setNodes(nodes: Node[]) {
- nodes.forEach((node) => {
- this.nodes[node.id] = node;
- });
- }
-
- public deleteNode(nodeId: string) {
- delete this.nodes[nodeId];
- }
-
- public setIsLoading(isLoading: boolean) {
- this.isLoading = isLoading;
- }
-
- public setHasMore(hasMore: boolean) {
- this.hasMore = hasMore;
- }
-
- public setIsLoadingMore(isLoadingMore: boolean) {
- this.isLoadingMore = isLoadingMore;
- }
-
- public getNode(nodeId: string) {
- return this.nodes[nodeId];
- }
-
- public getNodes() {
- return Object.values(this.nodes);
- }
-}
\ No newline at end of file
diff --git a/desktop/src/store/sidebar.ts b/desktop/src/store/sidebar.ts
deleted file mode 100644
index c0eb7711..00000000
--- a/desktop/src/store/sidebar.ts
+++ /dev/null
@@ -1,40 +0,0 @@
-import { Node } from '@/types/nodes';
-import { makeAutoObservable } from 'mobx';
-
-export class SidebarStore {
- public nodes: Record<string, Node>;
- public isLoading: boolean;
-
- constructor() {
- this.nodes = {};
- this.isLoading = false;
-
- makeAutoObservable(this);
- }
-
- public setNode(node: Node) {
- this.nodes[node.id] = node;
- }
-
- public setNodes(nodes: Node[]) {
- nodes.forEach((node) => {
- this.nodes[node.id] = node;
- });
- }
-
- public deleteNode(nodeId: string) {
- delete this.nodes[nodeId];
- }
-
- public setIsLoading(isLoading: boolean) {
- this.isLoading = isLoading;
- }
-
- public getNode(nodeId: string) {
- return this.nodes[nodeId];
- }
-
- public getNodes() {
- return Object.values(this.nodes);
- }
-}
diff --git a/desktop/src/types/databases.ts b/desktop/src/types/databases.ts
new file mode 100644
index 00000000..b7c91e60
--- /dev/null
+++ b/desktop/src/types/databases.ts
@@ -0,0 +1,7 @@
+import { CompiledQuery, QueryResult } from 'kysely';
+
+export type SubscribedQueryData<T> = {
+ query: CompiledQuery<T>;
+ tables: string[];
+ result: QueryResult<T>;
+};
diff --git a/desktop/src/types/messages.ts b/desktop/src/types/messages.ts
new file mode 100644
index 00000000..49df0f72
--- /dev/null
+++ b/desktop/src/types/messages.ts
@@ -0,0 +1,5 @@
+import { Node, NodeWithChildren } from '@/types/nodes';
+
+export type MessageNode = NodeWithChildren & {
+ author?: Node | null;
+};
diff --git a/desktop/src/types/nodes.ts b/desktop/src/types/nodes.ts
index 0c5bb2db..1fe730a8 100644
--- a/desktop/src/types/nodes.ts
+++ b/desktop/src/types/nodes.ts
@@ -11,7 +11,8 @@ export type Node = {
updatedAt?: Date | null;
updatedBy?: string | null;
versionId: string;
- state?: string | null;
+ serverCreatedAt?: Date | null;
+ serverUpdatedAt?: Date | null;
};
export type NodeBlock = {
@@ -28,20 +29,3 @@ export type NodeBlockMark = {
export type NodeWithChildren = Node & {
children: NodeWithChildren[];
};
-
-export type CreateNodeInput = {
- id: string;
- type: string;
- parentId?: string | null;
- index?: string | null;
- attrs?: Record<string, any> | null;
- content?: NodeBlock[] | null;
-};
-
-export type UpdateNodeInput = {
- id: string;
- parentId?: string | null;
- index?: string | null;
- attrs?: Record<string, any> | null;
- content?: NodeBlock[] | null;
-};
diff --git a/desktop/src/types/transactions.ts b/desktop/src/types/transactions.ts
index 80264a46..f8162fbd 100644
--- a/desktop/src/types/transactions.ts
+++ b/desktop/src/types/transactions.ts
@@ -1,42 +1,13 @@
-import { NodeBlock } from '@/types/nodes';
+export type TransactionTable = 'nodes';
-export type TransactionType =
- | 'create_node'
- | 'create_nodes'
- | 'update_node'
- | 'delete_node'
- | 'delete_nodes';
+export type TransactionAction = 'insert' | 'update' | 'delete';
export type Transaction = {
id: string;
- type: TransactionType;
workspaceId: string;
- accountId: string;
- userId: string;
- input: string;
- createdAt: Date;
-};
-
-export type CreateNodeTransactionInput = {
- id: string;
- workspaceId: string;
- parentId?: string | null;
- type: string;
- index?: string | null;
- attrs?: Record<string, any> | null;
- content?: NodeBlock[] | null;
- createdAt: Date;
- createdBy: string;
- versionId: string;
-};
-
-export type UpdateNodeTransactionInput = {
- id: string;
- parentId?: string | null;
- index?: string | null;
- attrs?: Record<string, any> | null;
- content?: NodeBlock[] | null;
- updatedAt: Date;
- updatedBy: string;
- versionId: string;
+ action: TransactionAction;
+ table: TransactionTable;
+ after?: string | null;
+ before?: string | null;
+ createdAt: string;
};
diff --git a/desktop/src/types/window.d.ts b/desktop/src/types/window.d.ts
index 8c4341ef..736e43de 100644
--- a/desktop/src/types/window.d.ts
+++ b/desktop/src/types/window.d.ts
@@ -1,75 +1,35 @@
-import { Workspace } from '@/types/workspaces';
-import { Transaction } from '@/types/transactions';
-import { Account } from '@/types/accounts';
-import { CreateNodeInput, Node, UpdateNodeInput } from '@/types/nodes';
import { GlobalDatabaseData } from '@/types/global';
import { EventBus } from '@/lib/event-bus';
+import { CompiledQuery, QueryResult } from 'kysely';
-export interface GlobalDbApi {
+interface NeuronApi {
init: () => Promise<GlobalDatabaseData>;
- getAccounts: () => Promise<Account[]>;
- addAccount: (account: Account) => Promise<void>;
- getWorkspaces: () => Promise<Workspace[]>;
- addWorkspace: (workspace: Workspace) => Promise<void>;
- addTransaction: (transaction: Transaction) => Promise<void>;
logout: (accountId: string) => Promise<void>;
-}
+ executeAppQuery: (query: CompiledQuery<R>) => Promise<QueryResult<R>>;
-export interface WorkspaceDbApi {
- createNode: (
- accountId: string,
- workspaceId: string,
- input: CreateNodeInput,
- ) => Promise<void>;
- createNodes: (
- accountId: string,
- workspaceId: string,
- inputs: CreateNodeInput[],
- ) => Promise<void>;
- getNodes: (accountId: string, workspaceId: string) => Promise<Node[]>;
- updateNode: (
- accountId: string,
- workspaceId: string,
- input: UpdateNodeInput,
- ) => Promise<void>;
- deleteNode: (
+ executeWorkspaceQuery: (
accountId: string,
workspaceId: string,
- nodeId: string,
- ) => Promise<void>;
- deleteNodes: (
- accountId: string,
- workspaceId: string,
- nodeIds: string[],
- ) => Promise<void>;
-
- getSidebarNodes: (accountId: string, workspaceId: string) => Promise<Node[]>;
+ query: CompiledQuery<R>,
+ ) => Promise<QueryResult<R>>;
- getConversationNodes: (
+ executeWorkspaceQueryAndSubscribe: (
accountId: string,
workspaceId: string,
- conversationId: string,
- count: number,
- after?: string | null,
- ) => Promise<Node[]>;
+ queryId: string,
+ query: CompiledQuery<R>,
+ ) => Promise<QueryResult<R>>;
- getDocumentNodes: (
+ unsubscribeWorkspaceQuery: (
accountId: string,
workspaceId: string,
- documentId: string,
- ) => Promise<Node[]>;
-
- getContainerNodes: (
- accountId: string,
- workspaceId: string,
- containerId: string,
- ) => Promise<Node[]>;
+ queryId: string,
+ ) => Promise<void>;
}
declare global {
interface Window {
- globalDb: GlobalDbApi;
- workspaceDb: WorkspaceDbApi;
+ neuron: NeuronApi;
eventBus: EventBus;
}
}
diff --git a/desktop/src/types/workspaces.ts b/desktop/src/types/workspaces.ts
index 3190864b..2a11ab0f 100644
--- a/desktop/src/types/workspaces.ts
+++ b/desktop/src/types/workspaces.ts
@@ -13,6 +13,6 @@ export type Workspace = {
versionId: string;
accountId: string;
role: WorkspaceRole;
- userNodeId: string;
+ userId: string;
syncedAt?: Date | null;
};
diff --git a/server/prisma/migrations/20240808134858_init_db/migration.sql b/server/prisma/migrations/20240822171526_init_db/migration.sql
similarity index 97%
rename from server/prisma/migrations/20240808134858_init_db/migration.sql
rename to server/prisma/migrations/20240822171526_init_db/migration.sql
index fd217f17..7e42f109 100644
--- a/server/prisma/migrations/20240808134858_init_db/migration.sql
+++ b/server/prisma/migrations/20240822171526_init_db/migration.sql
@@ -34,7 +34,7 @@ CREATE TABLE "workspaces" (
CREATE TABLE "workspace_accounts" (
"workspace_id" VARCHAR(30) NOT NULL,
"account_id" VARCHAR(30) NOT NULL,
- "user_node_id" VARCHAR(30) NOT NULL,
+ "user_id" VARCHAR(30) NOT NULL,
"role" INTEGER NOT NULL,
"attrs" JSONB,
"created_at" TIMESTAMPTZ(6) NOT NULL,
@@ -63,7 +63,7 @@ CREATE TABLE "nodes" (
"version_id" VARCHAR(30) NOT NULL,
"server_created_at" TIMESTAMPTZ(6) NOT NULL,
"server_updated_at" TIMESTAMPTZ(6),
- "state" TEXT,
+ "server_version_id" VARCHAR(30) NOT NULL,
CONSTRAINT "nodes_pkey" PRIMARY KEY ("id")
);
diff --git a/server/prisma/schema.prisma b/server/prisma/schema.prisma
index 6734c09f..96d8b0a5 100644
--- a/server/prisma/schema.prisma
+++ b/server/prisma/schema.prisma
@@ -40,7 +40,7 @@ model workspaces {
model workspaceAccounts {
workspaceId String @db.VarChar(30) @map("workspace_id")
accountId String @db.VarChar(30) @map("account_id")
- userNodeId String @db.VarChar(30) @map("user_node_id")
+ userId String @db.VarChar(30) @map("user_id")
role Int
attrs Json?
createdAt DateTime @db.Timestamptz(6) @map("created_at")
@@ -69,7 +69,7 @@ model nodes {
versionId String @db.VarChar(30) @map("version_id")
serverCreatedAt DateTime @db.Timestamptz(6) @map("server_created_at")
serverUpdatedAt DateTime? @db.Timestamptz(6) @map("server_updated_at")
- state String? @db.Text()
+ serverVersionId String @db.VarChar(30) @map("server_version_id")
parent nodes? @relation("node_parent_child", fields: [parentId], references: [id], onDelete: Cascade)
children nodes[] @relation("node_parent_child")
diff --git a/server/src/api.ts b/server/src/api.ts
index 5ee73c43..d9b29bee 100644
--- a/server/src/api.ts
+++ b/server/src/api.ts
@@ -5,9 +5,10 @@ import { WebSocketServer } from 'ws';
import { accountsRouter } from '@/routes/accounts';
import { workspacesRouter } from '@/routes/workspaces';
-import { transactionsRouter } from '@/routes/transactions';
import { authMiddleware } from '@/middlewares/auth';
import { sockets } from '@/lib/sockets';
+import { SocketMessage } from '@/types/sockets';
+import { producer, TOPIC_NAMES } from '@/data/kafka';
export const initApi = () => {
const app = express();
@@ -22,16 +23,16 @@ export const initApi = () => {
app.use('/v1/accounts', accountsRouter);
app.use('/v1/workspaces', authMiddleware, workspacesRouter);
- app.use('/v1/transactions', authMiddleware, transactionsRouter);
const server = http.createServer(app);
const wss = new WebSocketServer({
server,
- path: '/synapse',
+ path: '/v1/synapse',
});
wss.on('connection', (socket, req) => {
+ console.log('New connection', req.url);
const deviceId = req.url?.split('device_id=')[1];
if (!deviceId) {
socket.close();
@@ -40,8 +41,28 @@ export const initApi = () => {
sockets.addSocket(deviceId, socket);
- socket.on('message', (message) => {
- console.log(`Received message: ${message}`);
+ socket.on('message', async (message) => {
+ const socketMessage: SocketMessage = JSON.parse(message.toString());
+ if (socketMessage.type === 'transaction') {
+ await producer.send({
+ topic: TOPIC_NAMES.TRANSACTIONS,
+ messages: [
+ {
+ key: socketMessage.payload.id,
+ value: JSON.stringify(socketMessage.payload),
+ },
+ ],
+ });
+
+ const ackMessage: SocketMessage = {
+ type: 'transaction_ack',
+ payload: {
+ id: socketMessage.payload.id,
+ workspaceId: socketMessage.payload.workspaceId,
+ },
+ };
+ socket.send(JSON.stringify(ackMessage));
+ }
});
socket.on('close', () => {
diff --git a/server/src/consumers/node-changes.ts b/server/src/consumers/node-changes.ts
index 0cb7e93c..360f85d9 100644
--- a/server/src/consumers/node-changes.ts
+++ b/server/src/consumers/node-changes.ts
@@ -72,7 +72,7 @@ const handleNodeChange = async (change: ChangeMessage<NodeChangeData>) => {
serverUpdatedAt: changeData.server_updated_at
? new Date(changeData.server_updated_at)
: null,
- state: changeData.state,
+ serverVersionId: changeData.server_version_id,
};
await prisma.updates.create({
diff --git a/server/src/consumers/transactions.ts b/server/src/consumers/transactions.ts
index 9b048e61..72c73959 100644
--- a/server/src/consumers/transactions.ts
+++ b/server/src/consumers/transactions.ts
@@ -1,9 +1,5 @@
import { kafka, TOPIC_NAMES, CONSUMER_IDS } from '@/data/kafka';
-import {
- CreateNodeTransactionInput,
- Transaction,
- UpdateNodeTransactionInput,
-} from '@/types/transactions';
+import { NodeTransactionData, Transaction } from '@/types/transactions';
import { prisma } from '@/data/prisma';
import { Prisma } from '@prisma/client';
@@ -17,45 +13,55 @@ export const initTransactionsConsumer = async () => {
eachMessage: async ({ message }) => {
if (message.value === null) return;
- const transaction: Transaction = JSON.parse(message.value.toString());
- await handleTransaction(transaction);
+ try {
+ const transaction: Transaction = JSON.parse(message.value.toString());
+ await handleTransaction(transaction);
+ } catch (error) {
+ console.error('Error processing transaction:', error);
+ }
},
});
};
const handleTransaction = async (transaction: Transaction) => {
- switch (transaction.type) {
- case 'create_node':
- await handleCreateNodeTransaction(transaction);
+ switch (transaction.table) {
+ case 'nodes':
+ await handleNodeTransaction(transaction);
break;
- case 'create_nodes':
- await handleCreateNodesTransaction(transaction);
+ }
+};
+
+const handleNodeTransaction = async (transaction: Transaction) => {
+ switch (transaction.action) {
+ case 'insert':
+ await handleInsertNodeTransaction(transaction);
break;
- case 'update_node':
+ case 'update':
await handleUpdateNodeTransaction(transaction);
break;
- case 'delete_node':
+ case 'delete':
await handleDeleteNodeTransaction(transaction);
break;
- case 'delete_nodes':
- await handleDeleteNodesTransaction(transaction);
- break;
}
};
-const handleCreateNodeTransaction = async (transaction: Transaction) => {
- const input = JSON.parse(transaction.input) as CreateNodeTransactionInput;
+const handleInsertNodeTransaction = async (transaction: Transaction) => {
+ if (!transaction.after) {
+ return;
+ }
+ const input = JSON.parse(transaction.after) as NodeTransactionData;
const data: Prisma.nodesUncheckedCreateInput = {
id: input.id,
- parentId: input.parentId,
- workspaceId: input.workspaceId,
+ parentId: input.parent_id,
+ workspaceId: input.workspace_id,
type: input.type,
index: input.index,
- createdAt: input.createdAt,
- createdBy: input.createdBy,
- versionId: input.versionId,
+ createdAt: input.created_at,
+ createdBy: input.created_by,
+ versionId: input.version_id,
serverCreatedAt: new Date(),
+ serverVersionId: input.version_id,
};
if (input.attrs !== undefined && input.attrs !== null) {
@@ -71,40 +77,12 @@ const handleCreateNodeTransaction = async (transaction: Transaction) => {
});
};
-const handleCreateNodesTransaction = async (transaction: Transaction) => {
- const nodes = JSON.parse(transaction.input) as CreateNodeTransactionInput[];
- const data: Prisma.nodesUncheckedCreateInput[] = nodes.map((node) => {
- const data: Prisma.nodesUncheckedCreateInput = {
- id: node.id,
- parentId: node.parentId,
- workspaceId: node.workspaceId,
- type: node.type,
- index: node.index,
- createdAt: node.createdAt,
- createdBy: node.createdBy,
- versionId: node.versionId,
- serverCreatedAt: new Date(),
- };
-
- if (node.attrs !== undefined && node.attrs !== null) {
- data.attrs = node.attrs;
- }
-
- if (node.content !== undefined && node.content !== null) {
- data.content = node.content;
- }
-
- return data;
- });
-
- await prisma.nodes.createMany({
- data: data,
- });
-};
-
const handleUpdateNodeTransaction = async (transaction: Transaction) => {
- const input = JSON.parse(transaction.input) as UpdateNodeTransactionInput;
+ if (!transaction.after) {
+ return;
+ }
+ const input = JSON.parse(transaction.after) as NodeTransactionData;
const existingNode = await prisma.nodes.findUnique({
where: {
id: input.id,
@@ -116,11 +94,12 @@ const handleUpdateNodeTransaction = async (transaction: Transaction) => {
}
const data: Prisma.nodesUncheckedUpdateInput = {
- parentId: input.parentId,
- updatedAt: input.updatedAt,
- updatedBy: input.updatedBy,
- versionId: input.versionId,
+ parentId: input.parent_id,
+ updatedAt: input.updated_at,
+ updatedBy: input.updated_by,
+ versionId: input.version_id,
serverUpdatedAt: new Date(),
+ serverVersionId: input.version_id,
};
if (input.attrs !== undefined) {
@@ -132,9 +111,10 @@ const handleUpdateNodeTransaction = async (transaction: Transaction) => {
? (existingNode.attrs as Record<string, any>)
: {};
+ const updatedAttrs = JSON.parse(input.attrs) as Record<string, any>;
const newAttrs = {
...existingAttrs,
- ...input.attrs,
+ ...updatedAttrs,
};
const hasAttrs = Object.keys(newAttrs).length > 0;
@@ -159,20 +139,14 @@ const handleUpdateNodeTransaction = async (transaction: Transaction) => {
};
const handleDeleteNodeTransaction = async (transaction: Transaction) => {
- await prisma.nodes.delete({
- where: {
- id: transaction.input,
- },
- });
-};
+ if (!transaction.before) {
+ return;
+ }
-const handleDeleteNodesTransaction = async (transaction: Transaction) => {
- const input = JSON.parse(transaction.input) as string[];
- await prisma.nodes.deleteMany({
+ const input = JSON.parse(transaction.before) as NodeTransactionData;
+ await prisma.nodes.delete({
where: {
- id: {
- in: input,
- },
+ id: input.id,
},
});
};
diff --git a/server/src/routes/accounts.ts b/server/src/routes/accounts.ts
index fbec8374..6681a393 100644
--- a/server/src/routes/accounts.ts
+++ b/server/src/routes/accounts.ts
@@ -215,7 +215,7 @@ const buildLoginOutput = async (
id: workspace.id,
name: workspace.name,
role: accountWorkspace.role,
- userNodeId: accountWorkspace.userNodeId,
+ userId: accountWorkspace.userId,
versionId: accountWorkspace.versionId,
accountId: accountWorkspace.accountId,
avatar: workspace.avatar,
diff --git a/server/src/routes/transactions.ts b/server/src/routes/transactions.ts
deleted file mode 100644
index aa4a7ceb..00000000
--- a/server/src/routes/transactions.ts
+++ /dev/null
@@ -1,27 +0,0 @@
-import { Request, Response, Router } from 'express';
-import { Transaction } from '@/types/transactions';
-import { producer, TOPIC_NAMES } from '@/data/kafka';
-
-export const transactionsRouter = Router();
-
-transactionsRouter.post('/', async (req: Request, res: Response) => {
- const transactions: Transaction[] = req.body.transactions;
-
- const messages = transactions.map((transaction) => ({
- key: transaction.id,
- value: JSON.stringify(transaction),
- }));
-
- await producer.sendBatch({
- topicMessages: [
- {
- topic: TOPIC_NAMES.TRANSACTIONS,
- messages: messages,
- },
- ],
- });
-
- res.status(200).json({
- appliedTransactionIds: transactions.map((transaction) => transaction.id),
- });
-});
diff --git a/server/src/routes/workspaces.ts b/server/src/routes/workspaces.ts
index 4071f4ec..971b39de 100644
--- a/server/src/routes/workspaces.ts
+++ b/server/src/routes/workspaces.ts
@@ -56,11 +56,12 @@ workspacesRouter.post('/', async (req: NeuronRequest, res: NeuronResponse) => {
versionId: NeuronId.generate(NeuronId.Type.Version),
};
- const userNodeId = NeuronId.generate(NeuronId.Type.User);
+ const userId = NeuronId.generate(NeuronId.Type.User);
+ const userVersionId = NeuronId.generate(NeuronId.Type.Version);
const workspaceAccount: WorkspaceAccount = {
accountId: req.accountId,
workspaceId: workspace.id,
- userNodeId: userNodeId,
+ userId: userId,
role: WorkspaceRole.Owner,
createdAt: new Date(),
createdBy: req.accountId,
@@ -74,18 +75,19 @@ workspacesRouter.post('/', async (req: NeuronRequest, res: NeuronResponse) => {
}),
prisma.nodes.create({
data: {
- id: userNodeId,
+ id: userId,
workspaceId: workspace.id,
type: 'user',
attrs: {
- id: account.id,
+ accountId: account.id,
name: account.name,
avatar: account.avatar,
},
createdAt: new Date(),
- createdBy: userNodeId,
- versionId: NeuronId.generate(NeuronId.Type.Version),
+ createdBy: userId,
+ versionId: userVersionId,
serverCreatedAt: new Date(),
+ serverVersionId: userVersionId,
},
}),
prisma.workspaceAccounts.create({
@@ -101,7 +103,7 @@ workspacesRouter.post('/', async (req: NeuronRequest, res: NeuronResponse) => {
versionId: workspace.versionId,
accountId: account.id,
role: workspaceAccount.role,
- userNodeId: userNodeId,
+ userId: userId,
};
return res.status(200).json(output);
@@ -173,7 +175,7 @@ workspacesRouter.put(
versionId: updatedWorkspace.versionId,
accountId: req.accountId,
role: workspaceAccount.role,
- userNodeId: workspaceAccount.userNodeId,
+ userId: workspaceAccount.userId,
};
return res.status(200).json(output);
@@ -276,7 +278,7 @@ workspacesRouter.get(':id', async (req: NeuronRequest, res: NeuronResponse) => {
versionId: workspace.versionId,
accountId: req.accountId,
role: workspaceAccount.role,
- userNodeId: workspaceAccount.userNodeId,
+ userId: workspaceAccount.userId,
};
return res.status(200).json(output);
@@ -324,7 +326,7 @@ workspacesRouter.get('/', async (req: NeuronRequest, res: NeuronResponse) => {
versionId: workspace.versionId,
accountId: req.accountId,
role: workspaceAccount.role,
- userNodeId: workspaceAccount.userNodeId,
+ userId: workspaceAccount.userId,
};
outputs.push(output);
@@ -353,7 +355,7 @@ workspacesRouter.get('/:id/nodes', async (req: Request, res: Response) => {
updatedBy: node.updatedBy,
serverCreatedAt: node.serverCreatedAt,
serverUpdatedAt: node.serverUpdatedAt,
- state: node.state,
+ serverVersionId: node.serverVersionId,
};
});
diff --git a/server/src/types/changes.ts b/server/src/types/changes.ts
index 77e0f478..637d5500 100644
--- a/server/src/types/changes.ts
+++ b/server/src/types/changes.ts
@@ -40,7 +40,7 @@ export type NodeChangeData = {
version_id: string;
server_created_at: string;
server_updated_at?: string | null;
- state: string | null;
+ server_version_id: string;
};
export type UpdateChangeData = {
diff --git a/server/src/types/nodes.ts b/server/src/types/nodes.ts
index ae55c6f7..d22347b0 100644
--- a/server/src/types/nodes.ts
+++ b/server/src/types/nodes.ts
@@ -11,7 +11,9 @@ export type Node = {
updatedAt?: Date | null;
updatedBy?: string | null;
versionId: string;
- state: string | null;
+ serverCreatedAt: Date;
+ serverUpdatedAt?: Date | null;
+ serverVersionId: string;
};
export type NodeBlock = {
diff --git a/server/src/types/sockets.ts b/server/src/types/sockets.ts
new file mode 100644
index 00000000..064945f1
--- /dev/null
+++ b/server/src/types/sockets.ts
@@ -0,0 +1,4 @@
+export type SocketMessage = {
+ type: string;
+ payload: any;
+};
diff --git a/server/src/types/transactions.ts b/server/src/types/transactions.ts
index 80264a46..40807216 100644
--- a/server/src/types/transactions.ts
+++ b/server/src/types/transactions.ts
@@ -1,42 +1,31 @@
-import { NodeBlock } from '@/types/nodes';
+export type TransactionTable = 'nodes';
-export type TransactionType =
- | 'create_node'
- | 'create_nodes'
- | 'update_node'
- | 'delete_node'
- | 'delete_nodes';
+export type TransactionAction = 'insert' | 'update' | 'delete';
export type Transaction = {
id: string;
- type: TransactionType;
workspaceId: string;
- accountId: string;
- userId: string;
- input: string;
- createdAt: Date;
+ action: TransactionAction;
+ table: TransactionTable;
+ after?: string | null;
+ before?: string | null;
+ createdAt: string;
};
-export type CreateNodeTransactionInput = {
+export type NodeTransactionData = {
id: string;
- workspaceId: string;
- parentId?: string | null;
+ workspace_id: string;
+ parent_id: string | null;
type: string;
- index?: string | null;
- attrs?: Record<string, any> | null;
- content?: NodeBlock[] | null;
- createdAt: Date;
- createdBy: string;
- versionId: string;
-};
-
-export type UpdateNodeTransactionInput = {
- id: string;
- parentId?: string | null;
- index?: string | null;
- attrs?: Record<string, any> | null;
- content?: NodeBlock[] | null;
- updatedAt: Date;
- updatedBy: string;
- versionId: string;
+ index: string | null;
+ attrs: string | null;
+ content: string | null;
+ created_at: string;
+ updated_at: string | null;
+ created_by: string;
+ updated_by: string | null;
+ version_id: string;
+ server_created_at: string;
+ server_updated_at: string | null;
+ server_version_id: string | null;
};
diff --git a/server/src/types/workspaces.ts b/server/src/types/workspaces.ts
index 72bfa757..4a55053b 100644
--- a/server/src/types/workspaces.ts
+++ b/server/src/types/workspaces.ts
@@ -31,7 +31,7 @@ export type Workspace = {
export type WorkspaceAccount = {
workspaceId: string;
accountId: string;
- userNodeId: string;
+ userId: string;
role: WorkspaceRole;
createdAt: Date;
createdBy: string;
@@ -55,5 +55,5 @@ export type WorkspaceOutput = {
versionId: string;
accountId: string;
role: WorkspaceRole;
- userNodeId: string;
+ userId: string;
};