Djonathan-IA Claude Opus 4.8 commited on
Commit
1f8cb1d
·
1 Parent(s): 9b7e1a8

Persistência do banco via Turso (libsql, API síncrona) — dados sobrevivem a reinícios

Browse files
Files changed (3) hide show
  1. package-lock.json +165 -0
  2. package.json +1 -0
  3. src/db.js +41 -8
package-lock.json CHANGED
@@ -17,6 +17,7 @@
17
  "ffprobe-static": "^3.1.0",
18
  "fluent-ffmpeg": "^2.1.3",
19
  "googleapis": "^144.0.0",
 
20
  "multer": "^1.4.5-lts.1",
21
  "node-cron": "^3.0.3",
22
  "sharp": "^0.33.5"
@@ -891,6 +892,123 @@
891
  "node": ">=12"
892
  }
893
  },
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
894
  "node_modules/@malept/cross-spawn-promise": {
895
  "version": "1.1.1",
896
  "resolved": "https://registry.npmjs.org/@malept/cross-spawn-promise/-/cross-spawn-promise-1.1.1.tgz",
@@ -994,6 +1112,12 @@
994
  "node": ">= 10.0.0"
995
  }
996
  },
 
 
 
 
 
 
997
  "node_modules/@pkgjs/parseargs": {
998
  "version": "0.11.0",
999
  "resolved": "https://registry.npmjs.org/@pkgjs/parseargs/-/parseargs-0.11.0.tgz",
@@ -4726,6 +4850,47 @@
4726
  "safe-buffer": "~5.1.0"
4727
  }
4728
  },
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4729
  "node_modules/lodash": {
4730
  "version": "4.18.1",
4731
  "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.18.1.tgz",
 
17
  "ffprobe-static": "^3.1.0",
18
  "fluent-ffmpeg": "^2.1.3",
19
  "googleapis": "^144.0.0",
20
+ "libsql": "^0.5.29",
21
  "multer": "^1.4.5-lts.1",
22
  "node-cron": "^3.0.3",
23
  "sharp": "^0.33.5"
 
892
  "node": ">=12"
893
  }
894
  },
895
+ "node_modules/@libsql/darwin-arm64": {
896
+ "version": "0.5.29",
897
+ "resolved": "https://registry.npmjs.org/@libsql/darwin-arm64/-/darwin-arm64-0.5.29.tgz",
898
+ "integrity": "sha512-K+2RIB1OGFPYQbfay48GakLhqf3ArcbHqPFu7EZiaUcRgFcdw8RoltsMyvbj5ix2fY0HV3Q3Ioa/ByvQdaSM0A==",
899
+ "cpu": [
900
+ "arm64"
901
+ ],
902
+ "license": "MIT",
903
+ "optional": true,
904
+ "os": [
905
+ "darwin"
906
+ ]
907
+ },
908
+ "node_modules/@libsql/darwin-x64": {
909
+ "version": "0.5.29",
910
+ "resolved": "https://registry.npmjs.org/@libsql/darwin-x64/-/darwin-x64-0.5.29.tgz",
911
+ "integrity": "sha512-OtT+KFHsKFy1R5FVadr8FJ2Bb1mghtXTyJkxv0trocq7NuHntSki1eUbxpO5ezJesDvBlqFjnWaYYY516QNLhQ==",
912
+ "cpu": [
913
+ "x64"
914
+ ],
915
+ "license": "MIT",
916
+ "optional": true,
917
+ "os": [
918
+ "darwin"
919
+ ]
920
+ },
921
+ "node_modules/@libsql/linux-arm-gnueabihf": {
922
+ "version": "0.5.29",
923
+ "resolved": "https://registry.npmjs.org/@libsql/linux-arm-gnueabihf/-/linux-arm-gnueabihf-0.5.29.tgz",
924
+ "integrity": "sha512-CD4n4zj7SJTHso4nf5cuMoWoMSS7asn5hHygsDuhRl8jjjCTT3yE+xdUvI4J7zsyb53VO5ISh4cwwOtf6k2UhQ==",
925
+ "cpu": [
926
+ "arm"
927
+ ],
928
+ "license": "MIT",
929
+ "optional": true,
930
+ "os": [
931
+ "linux"
932
+ ]
933
+ },
934
+ "node_modules/@libsql/linux-arm-musleabihf": {
935
+ "version": "0.5.29",
936
+ "resolved": "https://registry.npmjs.org/@libsql/linux-arm-musleabihf/-/linux-arm-musleabihf-0.5.29.tgz",
937
+ "integrity": "sha512-2Z9qBVpEJV7OeflzIR3+l5yAd4uTOLxklScYTwpZnkm2vDSGlC1PRlueLaufc4EFITkLKXK2MWBpexuNJfMVcg==",
938
+ "cpu": [
939
+ "arm"
940
+ ],
941
+ "license": "MIT",
942
+ "optional": true,
943
+ "os": [
944
+ "linux"
945
+ ]
946
+ },
947
+ "node_modules/@libsql/linux-arm64-gnu": {
948
+ "version": "0.5.29",
949
+ "resolved": "https://registry.npmjs.org/@libsql/linux-arm64-gnu/-/linux-arm64-gnu-0.5.29.tgz",
950
+ "integrity": "sha512-gURBqaiXIGGwFNEaUj8Ldk7Hps4STtG+31aEidCk5evMMdtsdfL3HPCpvys+ZF/tkOs2MWlRWoSq7SOuCE9k3w==",
951
+ "cpu": [
952
+ "arm64"
953
+ ],
954
+ "license": "MIT",
955
+ "optional": true,
956
+ "os": [
957
+ "linux"
958
+ ]
959
+ },
960
+ "node_modules/@libsql/linux-arm64-musl": {
961
+ "version": "0.5.29",
962
+ "resolved": "https://registry.npmjs.org/@libsql/linux-arm64-musl/-/linux-arm64-musl-0.5.29.tgz",
963
+ "integrity": "sha512-fwgYZ0H8mUkyVqXZHF3mT/92iIh1N94Owi/f66cPVNsk9BdGKq5gVpoKO+7UxaNzuEH1roJp2QEwsCZMvBLpqg==",
964
+ "cpu": [
965
+ "arm64"
966
+ ],
967
+ "license": "MIT",
968
+ "optional": true,
969
+ "os": [
970
+ "linux"
971
+ ]
972
+ },
973
+ "node_modules/@libsql/linux-x64-gnu": {
974
+ "version": "0.5.29",
975
+ "resolved": "https://registry.npmjs.org/@libsql/linux-x64-gnu/-/linux-x64-gnu-0.5.29.tgz",
976
+ "integrity": "sha512-y14V0vY0nmMC6G0pHeJcEarcnGU2H6cm21ZceRkacWHvQAEhAG0latQkCtoS2njFOXiYIg+JYPfAoWKbi82rkg==",
977
+ "cpu": [
978
+ "x64"
979
+ ],
980
+ "license": "MIT",
981
+ "optional": true,
982
+ "os": [
983
+ "linux"
984
+ ]
985
+ },
986
+ "node_modules/@libsql/linux-x64-musl": {
987
+ "version": "0.5.29",
988
+ "resolved": "https://registry.npmjs.org/@libsql/linux-x64-musl/-/linux-x64-musl-0.5.29.tgz",
989
+ "integrity": "sha512-gquqwA/39tH4pFl+J9n3SOMSymjX+6kZ3kWgY3b94nXFTwac9bnFNMffIomgvlFaC4ArVqMnOZD3nuJ3H3VO1w==",
990
+ "cpu": [
991
+ "x64"
992
+ ],
993
+ "license": "MIT",
994
+ "optional": true,
995
+ "os": [
996
+ "linux"
997
+ ]
998
+ },
999
+ "node_modules/@libsql/win32-x64-msvc": {
1000
+ "version": "0.5.29",
1001
+ "resolved": "https://registry.npmjs.org/@libsql/win32-x64-msvc/-/win32-x64-msvc-0.5.29.tgz",
1002
+ "integrity": "sha512-4/0CvEdhi6+KjMxMaVbFM2n2Z44escBRoEYpR+gZg64DdetzGnYm8mcNLcoySaDJZNaBd6wz5DNdgRmcI4hXcg==",
1003
+ "cpu": [
1004
+ "x64"
1005
+ ],
1006
+ "license": "MIT",
1007
+ "optional": true,
1008
+ "os": [
1009
+ "win32"
1010
+ ]
1011
+ },
1012
  "node_modules/@malept/cross-spawn-promise": {
1013
  "version": "1.1.1",
1014
  "resolved": "https://registry.npmjs.org/@malept/cross-spawn-promise/-/cross-spawn-promise-1.1.1.tgz",
 
1112
  "node": ">= 10.0.0"
1113
  }
1114
  },
1115
+ "node_modules/@neon-rs/load": {
1116
+ "version": "0.0.4",
1117
+ "resolved": "https://registry.npmjs.org/@neon-rs/load/-/load-0.0.4.tgz",
1118
+ "integrity": "sha512-kTPhdZyTQxB+2wpiRcFWrDcejc4JI6tkPuS7UZCG4l6Zvc5kU/gGQ/ozvHTh1XR5tS+UlfAfGuPajjzQjCiHCw==",
1119
+ "license": "MIT"
1120
+ },
1121
  "node_modules/@pkgjs/parseargs": {
1122
  "version": "0.11.0",
1123
  "resolved": "https://registry.npmjs.org/@pkgjs/parseargs/-/parseargs-0.11.0.tgz",
 
4850
  "safe-buffer": "~5.1.0"
4851
  }
4852
  },
4853
+ "node_modules/libsql": {
4854
+ "version": "0.5.29",
4855
+ "resolved": "https://registry.npmjs.org/libsql/-/libsql-0.5.29.tgz",
4856
+ "integrity": "sha512-8lMP8iMgiBzzoNbAPQ59qdVcj6UaE/Vnm+fiwX4doX4Narook0a4GPKWBEv+CR8a1OwbfkgL18uBfBjWdF0Fzg==",
4857
+ "cpu": [
4858
+ "x64",
4859
+ "arm64",
4860
+ "wasm32",
4861
+ "arm"
4862
+ ],
4863
+ "license": "MIT",
4864
+ "os": [
4865
+ "darwin",
4866
+ "linux",
4867
+ "win32"
4868
+ ],
4869
+ "dependencies": {
4870
+ "@neon-rs/load": "^0.0.4",
4871
+ "detect-libc": "2.0.2"
4872
+ },
4873
+ "optionalDependencies": {
4874
+ "@libsql/darwin-arm64": "0.5.29",
4875
+ "@libsql/darwin-x64": "0.5.29",
4876
+ "@libsql/linux-arm-gnueabihf": "0.5.29",
4877
+ "@libsql/linux-arm-musleabihf": "0.5.29",
4878
+ "@libsql/linux-arm64-gnu": "0.5.29",
4879
+ "@libsql/linux-arm64-musl": "0.5.29",
4880
+ "@libsql/linux-x64-gnu": "0.5.29",
4881
+ "@libsql/linux-x64-musl": "0.5.29",
4882
+ "@libsql/win32-x64-msvc": "0.5.29"
4883
+ }
4884
+ },
4885
+ "node_modules/libsql/node_modules/detect-libc": {
4886
+ "version": "2.0.2",
4887
+ "resolved": "https://registry.npmjs.org/detect-libc/-/detect-libc-2.0.2.tgz",
4888
+ "integrity": "sha512-UX6sGumvvqSaXgdKGUsgZWqcUyIXZ/vZTrlRT/iobiKhGL0zL4d3osHj3uqllWJK+i+sixDS/3COVEOFbupFyw==",
4889
+ "license": "Apache-2.0",
4890
+ "engines": {
4891
+ "node": ">=8"
4892
+ }
4893
+ },
4894
  "node_modules/lodash": {
4895
  "version": "4.18.1",
4896
  "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.18.1.tgz",
package.json CHANGED
@@ -46,6 +46,7 @@
46
  "ffprobe-static": "^3.1.0",
47
  "fluent-ffmpeg": "^2.1.3",
48
  "googleapis": "^144.0.0",
 
49
  "multer": "^1.4.5-lts.1",
50
  "node-cron": "^3.0.3",
51
  "sharp": "^0.33.5"
 
46
  "ffprobe-static": "^3.1.0",
47
  "fluent-ffmpeg": "^2.1.3",
48
  "googleapis": "^144.0.0",
49
+ "libsql": "^0.5.29",
50
  "multer": "^1.4.5-lts.1",
51
  "node-cron": "^3.0.3",
52
  "sharp": "^0.33.5"
src/db.js CHANGED
@@ -1,6 +1,12 @@
1
- // Banco de dados local em SQLite, usando o modulo nativo do Node (node:sqlite).
2
- // Sem compilacao nativa: funciona direto no Node 22.5+.
3
- import { DatabaseSync } from 'node:sqlite';
 
 
 
 
 
 
4
  import fs from 'node:fs';
5
  import path from 'node:path';
6
  import { DIRS } from './config.js';
@@ -8,8 +14,25 @@ import { DIRS } from './config.js';
8
  // Garante que as pastas de dados existem.
9
  for (const dir of Object.values(DIRS)) fs.mkdirSync(dir, { recursive: true });
10
 
11
- const db = new DatabaseSync(path.join(DIRS.data, 'dark.db'));
12
- db.exec('PRAGMA journal_mode = WAL; PRAGMA foreign_keys = ON;');
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
  db.exec(`
15
  CREATE TABLE IF NOT EXISTS workspaces (
@@ -149,10 +172,20 @@ for (const t of OWNED_TABLES) {
149
  }
150
  })();
151
 
 
 
 
 
 
 
 
152
  export default db;
153
 
154
  // ----- Helpers genericos -----
155
- export function all(sql, params = []) { return db.prepare(sql).all(...params); }
156
- export function get(sql, params = []) { return db.prepare(sql).get(...params); }
 
 
 
157
  export function run(sql, params = []) { return db.prepare(sql).run(...params); }
158
- export function insert(sql, params = []) { return db.prepare(sql).run(...params).lastInsertRowid; }
 
1
+ // Banco de dados SQLite via cliente libsql (API SÍNCRONA, compatível).
2
+ //
3
+ // Dois modos:
4
+ // - LOCAL (padrão): arquivo dark.db no disco. Simples, mas some se o disco reiniciar.
5
+ // - TURSO (nuvem, grátis): se TURSO_DATABASE_URL + TURSO_AUTH_TOKEN estiverem no
6
+ // ambiente, usa uma "réplica embutida" — as leituras são locais (rápidas) e as
7
+ // gravações vão para o banco na NUVEM. Assim, contas, campanhas e logins
8
+ // SOBREVIVEM a reinícios do servidor (na hospedagem grátis do Hugging Face).
9
+ import Database from 'libsql';
10
  import fs from 'node:fs';
11
  import path from 'node:path';
12
  import { DIRS } from './config.js';
 
14
  // Garante que as pastas de dados existem.
15
  for (const dir of Object.values(DIRS)) fs.mkdirSync(dir, { recursive: true });
16
 
17
+ const localDbPath = path.join(DIRS.data, 'dark.db');
18
+ const TURSO_URL = process.env.TURSO_DATABASE_URL;
19
+ const TURSO_TOKEN = process.env.TURSO_AUTH_TOKEN;
20
+ export const usingTurso = Boolean(TURSO_URL && TURSO_TOKEN);
21
+
22
+ let db;
23
+ if (usingTurso) {
24
+ // Réplica embutida: arquivo local sincronizado com o banco na nuvem (Turso).
25
+ db = new Database(localDbPath, { syncUrl: TURSO_URL, authToken: TURSO_TOKEN, syncInterval: 30 });
26
+ try { db.sync(); } catch (e) { console.warn('[db] sync inicial falhou (seguindo):', e.message || e); }
27
+ db.exec('PRAGMA foreign_keys = ON;');
28
+ console.log('[db] usando Turso (nuvem) — dados persistem entre reinícios.');
29
+ } else {
30
+ db = new Database(localDbPath);
31
+ db.exec('PRAGMA journal_mode = WAL; PRAGMA foreign_keys = ON;');
32
+ }
33
+
34
+ // Replica pra nuvem periodicamente (rede de segurança, além do write-through).
35
+ export function syncNow() { if (usingTurso) { try { db.sync(); } catch {} } }
36
 
37
  db.exec(`
38
  CREATE TABLE IF NOT EXISTS workspaces (
 
172
  }
173
  })();
174
 
175
+ // Depois das migrações, empurra o schema pra nuvem e liga a replicação periódica.
176
+ if (usingTurso) {
177
+ syncNow();
178
+ const t = setInterval(syncNow, 30000);
179
+ if (t.unref) t.unref();
180
+ }
181
+
182
  export default db;
183
 
184
  // ----- Helpers genericos -----
185
+ // O libsql adiciona um campo "_metadata" em cada linha; removemos pra não vazar
186
+ // pra API/cliente.
187
+ function clean(row) { if (row && row._metadata !== undefined) delete row._metadata; return row; }
188
+ export function all(sql, params = []) { return db.prepare(sql).all(...params).map(clean); }
189
+ export function get(sql, params = []) { return clean(db.prepare(sql).get(...params)); }
190
  export function run(sql, params = []) { return db.prepare(sql).run(...params); }
191
+ export function insert(sql, params = []) { return Number(db.prepare(sql).run(...params).lastInsertRowid); }