@pixelbyte-software/pixcode 1.32.0 → 1.33.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,194 @@
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
3
+
4
+ /**
5
+ * Minimal JSON-backed store with atomic writes.
6
+ *
7
+ * Replaces the `better-sqlite3` auth/session/telegram DB from previous
8
+ * releases. The backing store is kilobytes, not gigabytes — a SQL engine
9
+ * was overkill and the native compile dragged install-time warnings +
10
+ * on-disk WAL files that confused users. This impl:
11
+ *
12
+ * - Loads the entire store into memory on first access (synchronous
13
+ * readFileSync) and keeps it cached; all subsequent reads hit the
14
+ * in-memory structure.
15
+ * - Every write flushes the whole document via write-to-tmp + rename
16
+ * so a crash mid-write can never truncate the file; either the
17
+ * old file survives or the new one is fully written.
18
+ * - All operations are synchronous and rely on Node's single-threaded
19
+ * JS execution for concurrency safety. No async queue, no locks.
20
+ *
21
+ * The schema is a flat object with one array per "table":
22
+ * { _version, _sequences: {<table>: nextId}, users: [...], ... }
23
+ *
24
+ * Callers use helpers like `insert`, `updateWhere`, `findWhere`,
25
+ * `deleteWhere` rather than composing queries — this is a key/value map
26
+ * with filter helpers, not a SQL engine.
27
+ */
28
+
29
+ const CURRENT_VERSION = 1;
30
+
31
+ // Tables the store manages — empty arrays on a fresh file.
32
+ const EMPTY_STORE = () => ({
33
+ _version: CURRENT_VERSION,
34
+ _sequences: {
35
+ users: 0,
36
+ api_keys: 0,
37
+ user_credentials: 0,
38
+ vapid_keys: 0,
39
+ push_subscriptions: 0,
40
+ },
41
+ users: [],
42
+ api_keys: [],
43
+ user_credentials: [],
44
+ user_notification_preferences: [], // each row: { user_id, preferences_json, updated_at }
45
+ vapid_keys: [],
46
+ push_subscriptions: [],
47
+ session_names: [], // each row: { session_id, provider, custom_name, created_at, updated_at }
48
+ app_config: [], // each row: { key, value, created_at }
49
+ telegram_config: [], // 0 or 1 row: { id=1, bot_token, bot_username, updated_at }
50
+ telegram_links: [], // each row: { user_id, chat_id, ... }
51
+ });
52
+
53
+ export class JsonStore {
54
+ constructor(filePath) {
55
+ this.filePath = filePath;
56
+ this.tmpPath = `${filePath}.tmp`;
57
+ this.data = null;
58
+ this._ensureLoaded();
59
+ }
60
+
61
+ _ensureLoaded() {
62
+ if (this.data) return;
63
+
64
+ // Ensure parent directory exists — matches the old better-sqlite3
65
+ // behavior where the directory was created during initialization.
66
+ const dir = path.dirname(this.filePath);
67
+ if (!fs.existsSync(dir)) {
68
+ fs.mkdirSync(dir, { recursive: true });
69
+ }
70
+
71
+ if (!fs.existsSync(this.filePath)) {
72
+ this.data = EMPTY_STORE();
73
+ this._flush();
74
+ return;
75
+ }
76
+
77
+ try {
78
+ const raw = fs.readFileSync(this.filePath, 'utf8');
79
+ const parsed = JSON.parse(raw);
80
+ // Fill missing keys from EMPTY_STORE so adding a new "table" in a
81
+ // later schema doesn't crash a fresh deploy reading an old file.
82
+ this.data = { ...EMPTY_STORE(), ...parsed };
83
+ // Ensure each well-known array key is actually an array — defends
84
+ // against a hand-edited file that set one to null or an object.
85
+ const empty = EMPTY_STORE();
86
+ for (const key of Object.keys(empty)) {
87
+ if (key === '_version' || key === '_sequences') continue;
88
+ if (!Array.isArray(this.data[key])) {
89
+ this.data[key] = [];
90
+ }
91
+ }
92
+ this.data._sequences = { ...empty._sequences, ...(parsed._sequences || {}) };
93
+ } catch (err) {
94
+ // Corrupted file — back up and start fresh. Never hide this; it's
95
+ // very likely user-visible (logins will reset).
96
+ const backup = `${this.filePath}.corrupt-${Date.now()}`;
97
+ console.error(`[JsonStore] Failed to read ${this.filePath}: ${err.message}. Backing up to ${backup}.`);
98
+ try { fs.renameSync(this.filePath, backup); } catch { /* noop */ }
99
+ this.data = EMPTY_STORE();
100
+ this._flush();
101
+ }
102
+ }
103
+
104
+ _flush() {
105
+ const serialized = JSON.stringify(this.data, null, 2);
106
+ fs.writeFileSync(this.tmpPath, serialized, 'utf8');
107
+ fs.renameSync(this.tmpPath, this.filePath);
108
+ }
109
+
110
+ // ---------- Raw access ----------
111
+ get raw() {
112
+ this._ensureLoaded();
113
+ return this.data;
114
+ }
115
+
116
+ save() { this._flush(); }
117
+
118
+ // ---------- Sequence (AUTOINCREMENT) ----------
119
+ nextId(table) {
120
+ this._ensureLoaded();
121
+ this.data._sequences[table] = (this.data._sequences[table] || 0) + 1;
122
+ return this.data._sequences[table];
123
+ }
124
+
125
+ // ---------- Query helpers ----------
126
+ findWhere(table, predicate) {
127
+ this._ensureLoaded();
128
+ return this.data[table].find(predicate) || null;
129
+ }
130
+
131
+ filterWhere(table, predicate) {
132
+ this._ensureLoaded();
133
+ return this.data[table].filter(predicate);
134
+ }
135
+
136
+ count(table, predicate) {
137
+ this._ensureLoaded();
138
+ if (!predicate) return this.data[table].length;
139
+ return this.data[table].filter(predicate).length;
140
+ }
141
+
142
+ // ---------- Mutation helpers ----------
143
+ insert(table, row, { autoId = true, sequenceKey } = {}) {
144
+ this._ensureLoaded();
145
+ const finalRow = { ...row };
146
+ if (autoId && finalRow.id === undefined) {
147
+ finalRow.id = this.nextId(sequenceKey || table);
148
+ }
149
+ this.data[table].push(finalRow);
150
+ this._flush();
151
+ return finalRow;
152
+ }
153
+
154
+ updateWhere(table, predicate, updater) {
155
+ this._ensureLoaded();
156
+ let changed = 0;
157
+ for (const row of this.data[table]) {
158
+ if (predicate(row)) {
159
+ const patch = typeof updater === 'function' ? updater(row) : updater;
160
+ Object.assign(row, patch);
161
+ changed += 1;
162
+ }
163
+ }
164
+ if (changed > 0) this._flush();
165
+ return changed;
166
+ }
167
+
168
+ upsertWhere(table, predicate, row) {
169
+ this._ensureLoaded();
170
+ const existing = this.data[table].find(predicate);
171
+ if (existing) {
172
+ Object.assign(existing, row);
173
+ this._flush();
174
+ return existing;
175
+ }
176
+ return this.insert(table, row);
177
+ }
178
+
179
+ deleteWhere(table, predicate) {
180
+ this._ensureLoaded();
181
+ const before = this.data[table].length;
182
+ this.data[table] = this.data[table].filter((row) => !predicate(row));
183
+ const removed = before - this.data[table].length;
184
+ if (removed > 0) this._flush();
185
+ return removed;
186
+ }
187
+ }
188
+
189
+ /**
190
+ * Helper — ISO timestamp matching the DATETIME format SQLite used to
191
+ * write. Stored as a plain ISO string; no one actually parses it beyond
192
+ * displaying "last login X hours ago".
193
+ */
194
+ export const nowIso = () => new Date().toISOString();
@@ -1,35 +1,36 @@
1
- import webPush from 'web-push';
2
- import { db } from '../database/db.js';
3
-
4
- let cachedKeys = null;
5
-
6
- function ensureVapidKeys() {
7
- if (cachedKeys) return cachedKeys;
8
-
9
- const row = db.prepare('SELECT public_key, private_key FROM vapid_keys ORDER BY id DESC LIMIT 1').get();
10
- if (row) {
11
- cachedKeys = { publicKey: row.public_key, privateKey: row.private_key };
12
- return cachedKeys;
13
- }
14
-
15
- const keys = webPush.generateVAPIDKeys();
16
- db.prepare('INSERT INTO vapid_keys (public_key, private_key) VALUES (?, ?)').run(keys.publicKey, keys.privateKey);
17
- cachedKeys = keys;
18
- return cachedKeys;
19
- }
20
-
21
- function getPublicKey() {
22
- return ensureVapidKeys().publicKey;
23
- }
24
-
25
- function configureWebPush() {
26
- const keys = ensureVapidKeys();
27
- webPush.setVapidDetails(
28
- 'mailto:noreply@pixcode.local',
29
- keys.publicKey,
30
- keys.privateKey
31
- );
32
- console.log('Web Push notifications configured');
33
- }
34
-
35
- export { ensureVapidKeys, getPublicKey, configureWebPush };
1
+ import webPush from 'web-push';
2
+
3
+ import { vapidKeysDb } from '../database/db.js';
4
+
5
+ let cachedKeys = null;
6
+
7
+ function ensureVapidKeys() {
8
+ if (cachedKeys) return cachedKeys;
9
+
10
+ const row = vapidKeysDb.getLatest();
11
+ if (row) {
12
+ cachedKeys = { publicKey: row.public_key, privateKey: row.private_key };
13
+ return cachedKeys;
14
+ }
15
+
16
+ const keys = webPush.generateVAPIDKeys();
17
+ vapidKeysDb.insert(keys.publicKey, keys.privateKey);
18
+ cachedKeys = keys;
19
+ return cachedKeys;
20
+ }
21
+
22
+ function getPublicKey() {
23
+ return ensureVapidKeys().publicKey;
24
+ }
25
+
26
+ function configureWebPush() {
27
+ const keys = ensureVapidKeys();
28
+ webPush.setVapidDetails(
29
+ 'mailto:noreply@pixcode.local',
30
+ keys.publicKey,
31
+ keys.privateKey
32
+ );
33
+ console.log('Web Push notifications configured');
34
+ }
35
+
36
+ export { ensureVapidKeys, getPublicKey, configureWebPush };
@@ -1,130 +0,0 @@
1
- export const APP_CONFIG_TABLE_SQL = `CREATE TABLE IF NOT EXISTS app_config (
2
- key TEXT PRIMARY KEY,
3
- value TEXT NOT NULL,
4
- created_at DATETIME DEFAULT CURRENT_TIMESTAMP
5
- );`;
6
- export const USER_NOTIFICATION_PREFERENCES_TABLE_SQL = `CREATE TABLE IF NOT EXISTS user_notification_preferences (
7
- user_id INTEGER PRIMARY KEY,
8
- preferences_json TEXT NOT NULL,
9
- updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
10
- FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
11
- );`;
12
- export const VAPID_KEYS_TABLE_SQL = `CREATE TABLE IF NOT EXISTS vapid_keys (
13
- id INTEGER PRIMARY KEY AUTOINCREMENT,
14
- public_key TEXT NOT NULL,
15
- private_key TEXT NOT NULL,
16
- created_at DATETIME DEFAULT CURRENT_TIMESTAMP
17
- );`;
18
- export const PUSH_SUBSCRIPTIONS_TABLE_SQL = `CREATE TABLE IF NOT EXISTS push_subscriptions (
19
- id INTEGER PRIMARY KEY AUTOINCREMENT,
20
- user_id INTEGER NOT NULL,
21
- endpoint TEXT NOT NULL UNIQUE,
22
- keys_p256dh TEXT NOT NULL,
23
- keys_auth TEXT NOT NULL,
24
- created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
25
- FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
26
- );`;
27
- export const SESSION_NAMES_TABLE_SQL = `CREATE TABLE IF NOT EXISTS session_names (
28
- id INTEGER PRIMARY KEY AUTOINCREMENT,
29
- session_id TEXT NOT NULL,
30
- provider TEXT NOT NULL DEFAULT 'claude',
31
- custom_name TEXT NOT NULL,
32
- created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
33
- updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
34
- UNIQUE(session_id, provider)
35
- );`;
36
- export const SESSION_NAMES_LOOKUP_INDEX_SQL = `CREATE INDEX IF NOT EXISTS idx_session_names_lookup ON session_names(session_id, provider);`;
37
- // Telegram integration: one global bot config row (id = 1), plus per-user links.
38
- // The singleton CHECK keeps callers from accidentally inserting a second config
39
- // — we only ever host one bot per Pixcode instance, and swapping tokens is an
40
- // UPDATE, not an INSERT.
41
- export const TELEGRAM_CONFIG_TABLE_SQL = `CREATE TABLE IF NOT EXISTS telegram_config (
42
- id INTEGER PRIMARY KEY CHECK (id = 1),
43
- bot_token TEXT NOT NULL,
44
- bot_username TEXT,
45
- updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
46
- );`;
47
- export const TELEGRAM_LINKS_TABLE_SQL = `CREATE TABLE IF NOT EXISTS telegram_links (
48
- user_id INTEGER PRIMARY KEY,
49
- chat_id TEXT,
50
- telegram_username TEXT,
51
- language TEXT DEFAULT 'en',
52
- pairing_code TEXT,
53
- pairing_code_expires_at DATETIME,
54
- verified_at DATETIME,
55
- notifications_enabled INTEGER NOT NULL DEFAULT 1,
56
- bridge_enabled INTEGER NOT NULL DEFAULT 1,
57
- updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
58
- FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
59
- );`;
60
- export const TELEGRAM_LINKS_CHAT_INDEX_SQL = `CREATE INDEX IF NOT EXISTS idx_telegram_links_chat ON telegram_links(chat_id);`;
61
- export const TELEGRAM_LINKS_CODE_INDEX_SQL = `CREATE INDEX IF NOT EXISTS idx_telegram_links_code ON telegram_links(pairing_code);`;
62
- export const DATABASE_SCHEMA_SQL = `PRAGMA foreign_keys = ON;
63
-
64
- CREATE TABLE IF NOT EXISTS users (
65
- id INTEGER PRIMARY KEY AUTOINCREMENT,
66
- username TEXT UNIQUE NOT NULL,
67
- password_hash TEXT NOT NULL,
68
- created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
69
- last_login DATETIME,
70
- is_active BOOLEAN DEFAULT 1,
71
- git_name TEXT,
72
- git_email TEXT,
73
- has_completed_onboarding BOOLEAN DEFAULT 0
74
- );
75
-
76
- CREATE INDEX IF NOT EXISTS idx_users_username ON users(username);
77
- CREATE INDEX IF NOT EXISTS idx_users_active ON users(is_active);
78
-
79
- CREATE TABLE IF NOT EXISTS api_keys (
80
- id INTEGER PRIMARY KEY AUTOINCREMENT,
81
- user_id INTEGER NOT NULL,
82
- key_name TEXT NOT NULL,
83
- api_key TEXT UNIQUE NOT NULL,
84
- created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
85
- last_used DATETIME,
86
- is_active BOOLEAN DEFAULT 1,
87
- FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
88
- );
89
-
90
- CREATE INDEX IF NOT EXISTS idx_api_keys_key ON api_keys(api_key);
91
- CREATE INDEX IF NOT EXISTS idx_api_keys_user_id ON api_keys(user_id);
92
- CREATE INDEX IF NOT EXISTS idx_api_keys_active ON api_keys(is_active);
93
-
94
- CREATE TABLE IF NOT EXISTS user_credentials (
95
- id INTEGER PRIMARY KEY AUTOINCREMENT,
96
- user_id INTEGER NOT NULL,
97
- credential_name TEXT NOT NULL,
98
- credential_type TEXT NOT NULL,
99
- credential_value TEXT NOT NULL,
100
- description TEXT,
101
- created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
102
- is_active BOOLEAN DEFAULT 1,
103
- FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
104
- );
105
-
106
- CREATE INDEX IF NOT EXISTS idx_user_credentials_user_id ON user_credentials(user_id);
107
- CREATE INDEX IF NOT EXISTS idx_user_credentials_type ON user_credentials(credential_type);
108
- CREATE INDEX IF NOT EXISTS idx_user_credentials_active ON user_credentials(is_active);
109
-
110
- ${USER_NOTIFICATION_PREFERENCES_TABLE_SQL}
111
-
112
- ${VAPID_KEYS_TABLE_SQL}
113
-
114
- ${PUSH_SUBSCRIPTIONS_TABLE_SQL}
115
-
116
- ${SESSION_NAMES_TABLE_SQL}
117
-
118
- ${SESSION_NAMES_LOOKUP_INDEX_SQL}
119
-
120
- ${APP_CONFIG_TABLE_SQL}
121
-
122
- ${TELEGRAM_CONFIG_TABLE_SQL}
123
-
124
- ${TELEGRAM_LINKS_TABLE_SQL}
125
-
126
- ${TELEGRAM_LINKS_CHAT_INDEX_SQL}
127
-
128
- ${TELEGRAM_LINKS_CODE_INDEX_SQL}
129
- `;
130
- //# sourceMappingURL=schema.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"schema.js","sourceRoot":"","sources":["../../../server/database/schema.js"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,oBAAoB,GAAG;;;;GAIjC,CAAC;AAEJ,MAAM,CAAC,MAAM,uCAAuC,GAAG;;;;;GAKpD,CAAC;AAEJ,MAAM,CAAC,MAAM,oBAAoB,GAAG;;;;;GAKjC,CAAC;AAEJ,MAAM,CAAC,MAAM,4BAA4B,GAAG;;;;;;;;GAQzC,CAAC;AAEJ,MAAM,CAAC,MAAM,uBAAuB,GAAG;;;;;;;;GAQpC,CAAC;AAEJ,MAAM,CAAC,MAAM,8BAA8B,GAAG,6FAA6F,CAAC;AAE5I,iFAAiF;AACjF,gFAAgF;AAChF,8EAA8E;AAC9E,yBAAyB;AACzB,MAAM,CAAC,MAAM,yBAAyB,GAAG;;;;;GAKtC,CAAC;AAEJ,MAAM,CAAC,MAAM,wBAAwB,GAAG;;;;;;;;;;;;GAYrC,CAAC;AAEJ,MAAM,CAAC,MAAM,6BAA6B,GAAG,gFAAgF,CAAC;AAC9H,MAAM,CAAC,MAAM,6BAA6B,GAAG,qFAAqF,CAAC;AAEnI,MAAM,CAAC,MAAM,mBAAmB,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAgDjC,uCAAuC;;EAEvC,oBAAoB;;EAEpB,4BAA4B;;EAE5B,uBAAuB;;EAEvB,8BAA8B;;EAE9B,oBAAoB;;EAEpB,yBAAyB;;EAEzB,wBAAwB;;EAExB,6BAA6B;;EAE7B,6BAA6B;CAC9B,CAAC"}
@@ -1,138 +0,0 @@
1
- export const APP_CONFIG_TABLE_SQL = `CREATE TABLE IF NOT EXISTS app_config (
2
- key TEXT PRIMARY KEY,
3
- value TEXT NOT NULL,
4
- created_at DATETIME DEFAULT CURRENT_TIMESTAMP
5
- );`;
6
-
7
- export const USER_NOTIFICATION_PREFERENCES_TABLE_SQL = `CREATE TABLE IF NOT EXISTS user_notification_preferences (
8
- user_id INTEGER PRIMARY KEY,
9
- preferences_json TEXT NOT NULL,
10
- updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
11
- FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
12
- );`;
13
-
14
- export const VAPID_KEYS_TABLE_SQL = `CREATE TABLE IF NOT EXISTS vapid_keys (
15
- id INTEGER PRIMARY KEY AUTOINCREMENT,
16
- public_key TEXT NOT NULL,
17
- private_key TEXT NOT NULL,
18
- created_at DATETIME DEFAULT CURRENT_TIMESTAMP
19
- );`;
20
-
21
- export const PUSH_SUBSCRIPTIONS_TABLE_SQL = `CREATE TABLE IF NOT EXISTS push_subscriptions (
22
- id INTEGER PRIMARY KEY AUTOINCREMENT,
23
- user_id INTEGER NOT NULL,
24
- endpoint TEXT NOT NULL UNIQUE,
25
- keys_p256dh TEXT NOT NULL,
26
- keys_auth TEXT NOT NULL,
27
- created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
28
- FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
29
- );`;
30
-
31
- export const SESSION_NAMES_TABLE_SQL = `CREATE TABLE IF NOT EXISTS session_names (
32
- id INTEGER PRIMARY KEY AUTOINCREMENT,
33
- session_id TEXT NOT NULL,
34
- provider TEXT NOT NULL DEFAULT 'claude',
35
- custom_name TEXT NOT NULL,
36
- created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
37
- updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
38
- UNIQUE(session_id, provider)
39
- );`;
40
-
41
- export const SESSION_NAMES_LOOKUP_INDEX_SQL = `CREATE INDEX IF NOT EXISTS idx_session_names_lookup ON session_names(session_id, provider);`;
42
-
43
- // Telegram integration: one global bot config row (id = 1), plus per-user links.
44
- // The singleton CHECK keeps callers from accidentally inserting a second config
45
- // — we only ever host one bot per Pixcode instance, and swapping tokens is an
46
- // UPDATE, not an INSERT.
47
- export const TELEGRAM_CONFIG_TABLE_SQL = `CREATE TABLE IF NOT EXISTS telegram_config (
48
- id INTEGER PRIMARY KEY CHECK (id = 1),
49
- bot_token TEXT NOT NULL,
50
- bot_username TEXT,
51
- updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
52
- );`;
53
-
54
- export const TELEGRAM_LINKS_TABLE_SQL = `CREATE TABLE IF NOT EXISTS telegram_links (
55
- user_id INTEGER PRIMARY KEY,
56
- chat_id TEXT,
57
- telegram_username TEXT,
58
- language TEXT DEFAULT 'en',
59
- pairing_code TEXT,
60
- pairing_code_expires_at DATETIME,
61
- verified_at DATETIME,
62
- notifications_enabled INTEGER NOT NULL DEFAULT 1,
63
- bridge_enabled INTEGER NOT NULL DEFAULT 1,
64
- updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
65
- FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
66
- );`;
67
-
68
- export const TELEGRAM_LINKS_CHAT_INDEX_SQL = `CREATE INDEX IF NOT EXISTS idx_telegram_links_chat ON telegram_links(chat_id);`;
69
- export const TELEGRAM_LINKS_CODE_INDEX_SQL = `CREATE INDEX IF NOT EXISTS idx_telegram_links_code ON telegram_links(pairing_code);`;
70
-
71
- export const DATABASE_SCHEMA_SQL = `PRAGMA foreign_keys = ON;
72
-
73
- CREATE TABLE IF NOT EXISTS users (
74
- id INTEGER PRIMARY KEY AUTOINCREMENT,
75
- username TEXT UNIQUE NOT NULL,
76
- password_hash TEXT NOT NULL,
77
- created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
78
- last_login DATETIME,
79
- is_active BOOLEAN DEFAULT 1,
80
- git_name TEXT,
81
- git_email TEXT,
82
- has_completed_onboarding BOOLEAN DEFAULT 0
83
- );
84
-
85
- CREATE INDEX IF NOT EXISTS idx_users_username ON users(username);
86
- CREATE INDEX IF NOT EXISTS idx_users_active ON users(is_active);
87
-
88
- CREATE TABLE IF NOT EXISTS api_keys (
89
- id INTEGER PRIMARY KEY AUTOINCREMENT,
90
- user_id INTEGER NOT NULL,
91
- key_name TEXT NOT NULL,
92
- api_key TEXT UNIQUE NOT NULL,
93
- created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
94
- last_used DATETIME,
95
- is_active BOOLEAN DEFAULT 1,
96
- FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
97
- );
98
-
99
- CREATE INDEX IF NOT EXISTS idx_api_keys_key ON api_keys(api_key);
100
- CREATE INDEX IF NOT EXISTS idx_api_keys_user_id ON api_keys(user_id);
101
- CREATE INDEX IF NOT EXISTS idx_api_keys_active ON api_keys(is_active);
102
-
103
- CREATE TABLE IF NOT EXISTS user_credentials (
104
- id INTEGER PRIMARY KEY AUTOINCREMENT,
105
- user_id INTEGER NOT NULL,
106
- credential_name TEXT NOT NULL,
107
- credential_type TEXT NOT NULL,
108
- credential_value TEXT NOT NULL,
109
- description TEXT,
110
- created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
111
- is_active BOOLEAN DEFAULT 1,
112
- FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
113
- );
114
-
115
- CREATE INDEX IF NOT EXISTS idx_user_credentials_user_id ON user_credentials(user_id);
116
- CREATE INDEX IF NOT EXISTS idx_user_credentials_type ON user_credentials(credential_type);
117
- CREATE INDEX IF NOT EXISTS idx_user_credentials_active ON user_credentials(is_active);
118
-
119
- ${USER_NOTIFICATION_PREFERENCES_TABLE_SQL}
120
-
121
- ${VAPID_KEYS_TABLE_SQL}
122
-
123
- ${PUSH_SUBSCRIPTIONS_TABLE_SQL}
124
-
125
- ${SESSION_NAMES_TABLE_SQL}
126
-
127
- ${SESSION_NAMES_LOOKUP_INDEX_SQL}
128
-
129
- ${APP_CONFIG_TABLE_SQL}
130
-
131
- ${TELEGRAM_CONFIG_TABLE_SQL}
132
-
133
- ${TELEGRAM_LINKS_TABLE_SQL}
134
-
135
- ${TELEGRAM_LINKS_CHAT_INDEX_SQL}
136
-
137
- ${TELEGRAM_LINKS_CODE_INDEX_SQL}
138
- `;