mirror of
https://github.com/zadam/trilium.git
synced 2025-11-17 18:50:41 +01:00
chore(monorepo/server): move server-side source code
This commit is contained in:
72
apps/server/src/routes/api/sql.ts
Normal file
72
apps/server/src/routes/api/sql.ts
Normal file
@@ -0,0 +1,72 @@
|
||||
"use strict";
|
||||
|
||||
import sql from "../../services/sql.js";
|
||||
import becca from "../../becca/becca.js";
|
||||
import type { Request } from "express";
|
||||
import ValidationError from "../../errors/validation_error.js";
|
||||
import { safeExtractMessageAndStackFromError } from "../../services/utils.js";
|
||||
|
||||
function getSchema() {
|
||||
const tableNames = sql.getColumn(/*sql*/`SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%' ORDER BY name`);
|
||||
const tables = [];
|
||||
|
||||
for (const tableName of tableNames) {
|
||||
tables.push({
|
||||
name: tableName,
|
||||
columns: sql.getRows(`PRAGMA table_info(${tableName})`)
|
||||
});
|
||||
}
|
||||
|
||||
return tables;
|
||||
}
|
||||
|
||||
function execute(req: Request) {
|
||||
const note = becca.getNoteOrThrow(req.params.noteId);
|
||||
|
||||
const content = note.getContent();
|
||||
if (typeof content !== "string") {
|
||||
throw new ValidationError("Invalid note type.");
|
||||
}
|
||||
|
||||
const queries = content.split("\n---");
|
||||
|
||||
try {
|
||||
const results = [];
|
||||
|
||||
for (let query of queries) {
|
||||
query = query.trim();
|
||||
|
||||
while (query.startsWith("-- ")) {
|
||||
// Query starts with one or more SQL comments, discard these before we execute.
|
||||
const pivot = query.indexOf("\n");
|
||||
query = pivot > 0 ? query.substr(pivot + 1).trim() : "";
|
||||
}
|
||||
|
||||
if (!query) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (query.toLowerCase().startsWith("select") || query.toLowerCase().startsWith("with")) {
|
||||
results.push(sql.getRows(query));
|
||||
} else {
|
||||
results.push(sql.execute(query));
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
results
|
||||
};
|
||||
} catch (e: unknown) {
|
||||
const [errMessage] = safeExtractMessageAndStackFromError(e);
|
||||
return {
|
||||
success: false,
|
||||
error: errMessage
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export default {
|
||||
getSchema,
|
||||
execute
|
||||
};
|
||||
Reference in New Issue
Block a user