-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
233 lines (195 loc) · 6.59 KB
/
main.js
File metadata and controls
233 lines (195 loc) · 6.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import lbug from './lib/index.js';
lbug.setWorkerPath('./lib/lbug_wasm_worker.js');
const terminal = document.getElementById('terminal');
const input = document.getElementById('command-input');
const statusEl = document.getElementById('status');
let db = null;
let conn = null;
let commandHistory = [];
let historyIndex = -1;
function print(text, className = '') {
const line = document.createElement('div');
line.className = `output-line ${className}`;
line.textContent = text;
terminal.appendChild(line);
terminal.scrollTop = terminal.scrollHeight;
}
function printTable(rows) {
if (!rows || rows.length === 0) {
print('(empty result)', 'info');
return;
}
const line = document.createElement('div');
line.className = 'output-line result';
let html = '<table>';
const headers = Object.keys(rows[0]);
html += '<thead><tr>';
for (const h of headers) {
html += `<th>${h}</th>`;
}
html += '</tr></thead><tbody>';
for (const row of rows) {
html += '<tr>';
for (const h of headers) {
const val = row[h];
html += `<td>${val === null ? 'NULL' : val}</td>`;
}
html += '</tr>';
}
html += '</tbody></table>';
line.innerHTML = html;
terminal.appendChild(line);
terminal.scrollTop = terminal.scrollHeight;
}
async function initDB() {
try {
print('Initializing Ladybug database...', 'info');
db = new lbug.Database(':memory:');
conn = new lbug.Connection(db);
const version = await lbug.getVersion();
const storageVersion = await lbug.getStorageVersion();
print(`Ladybug v${version} initialized`, 'success');
print(`Storage version: ${storageVersion}`, 'info');
print('', 'info');
print('Welcome to the Ladybug Shell!', 'success');
print('Type "help" for available commands.', 'info');
print('', 'info');
statusEl.textContent = 'Ready';
statusEl.className = 'status ready';
printExample();
} catch (err) {
print(`Failed to initialize: ${err.message}`, 'error');
statusEl.textContent = 'Error';
statusEl.className = 'status error';
}
}
function printExample() {
print('=== Strongly Typed Graph (Recommended) ===', 'info');
print('CREATE NODE TABLE User(name STRING, age INT64, PRIMARY KEY(name));', 'info');
print('CREATE NODE TABLE City(name STRING, population INT64, PRIMARY KEY(name));', 'info');
print('CREATE REL TABLE livesIn(FROM User TO City, MANY_ONE);', 'info');
print('CREATE (u:User {name: "Alice", age: 30}) -[:livesIn]-> (c:City {name: "NYC", population: 8000000});', 'info');
print('MATCH (u:User)-[:livesIn]->(c:City) RETURN u.name, c.name;', 'info');
print('', 'info');
print('=== Open Type Graph (Schema-less) ===', 'info');
print('create graph mygraph any;', 'info');
print('use graph mygraph;', 'info');
print('CREATE (u:User {name: "Alice", age: 30}) -[:livesIn]-> (c:City {name: "NYC", population: 8000000});', 'info');
print('MATCH (u:User)-[:livesIn]->(c:City) RETURN u.name, c.name;', 'info');
}
async function executeCommand(cmd) {
const trimmed = cmd.trim();
if (!trimmed) return;
const statements = trimmed.split(';').map(s => s.trim()).filter(s => s);
for (const statement of statements) {
commandHistory.push(statement);
historyIndex = commandHistory.length;
print(`lbug> ${statement}`, 'info');
if (statement.toLowerCase() === 'help') {
printHelp();
continue;
}
if (statement.toLowerCase() === 'clear') {
terminal.innerHTML = '';
continue;
}
if (statement.toLowerCase() === ':schema') {
await showSchema();
continue;
}
if (statement.toLowerCase() === 'exit') {
print('Goodbye!', 'success');
await lbug.close();
continue;
}
if (!conn) {
print('Database not initialized', 'error');
continue;
}
try {
const result = await conn.query(statement);
let hasResults = false;
const rows = [];
while (result.hasNext()) {
hasResults = true;
const row = await result.getNext();
rows.push(row);
}
if (hasResults && rows.length > 0) {
printTable(rows);
} else {
print('OK', 'success');
}
await result.close();
} catch (err) {
print(`Error: ${err.message}`, 'error');
}
}
}
function printHelp() {
print('Available commands:', 'info');
print(' help - Show this help message', 'info');
print(' clear - Clear the terminal', 'info');
print(' :schema - Show current schema', 'info');
print(' exit - Close the database and exit', 'info');
print('', 'info');
print('Strongly Typed (Recommended):', 'info');
print(' CREATE NODE TABLE User(name STRING, age INT64, PRIMARY KEY(name));', 'info');
print(' CREATE NODE TABLE City(name STRING, population INT64, PRIMARY KEY(name));', 'info');
print(' CREATE REL TABLE livesIn(FROM User TO City, MANY_ONE);', 'info');
print(' CREATE (u:User {name: "Alice", age: 30}) -[:livesIn]-> (c:City {name: "NYC"});', 'info');
print(' MATCH (u:User)-[:livesIn]->(c:City) RETURN u.name, c.name;', 'info');
print('', 'info');
print('Open Type Graph (Schema-less):', 'info');
print(' create graph mygraph any;', 'info');
print(' use graph mygraph;', 'info');
print(' CREATE (u:User {name: "Alice"}) -[:livesIn]-> (c:City {name: "NYC"});', 'info');
print(' MATCH (u)-[:livesIn]->(c) RETURN u.name, c.name;', 'info');
}
async function showSchema() {
if (!conn) {
print('Database not initialized', 'error');
return;
}
try {
const result = await conn.query("CALL show_tables() RETURN *;");
const rows = await result.getAllObjects();
await result.close();
if (rows.length === 0) {
print('No tables found', 'info');
} else {
printTable(rows);
}
} catch (err) {
print(`Error: ${err.message}`, 'error');
}
}
input.addEventListener('keydown', async (e) => {
if (e.key === 'Enter' && !e.shiftKey) {
e.preventDefault();
const cmd = input.value;
input.value = '';
input.style.height = 'auto';
await executeCommand(cmd);
} else if (e.key === 'ArrowUp') {
e.preventDefault();
if (historyIndex > 0) {
historyIndex--;
input.value = commandHistory[historyIndex] || '';
}
} else if (e.key === 'ArrowDown') {
e.preventDefault();
if (historyIndex < commandHistory.length - 1) {
historyIndex++;
input.value = commandHistory[historyIndex] || '';
} else {
historyIndex = commandHistory.length;
input.value = '';
}
}
});
input.addEventListener('input', () => {
input.style.height = 'auto';
input.style.height = input.scrollHeight + 'px';
});
initDB();