put notes/d67f10aa-5d76-420a-ae73-d1ca3fabf611.json
Browse files
notes/d67f10aa-5d76-420a-ae73-d1ca3fabf611.json
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"id": "d67f10aa-5d76-420a-ae73-d1ca3fabf611",
|
3 |
+
"title": "twan",
|
4 |
+
"content": "const sqlite3 = require('sqlite3').verbose();\nconst path = require('path');\n\nclass Database {\n constructor() {\n this.db = null;\n this.init();\n }\n\n init() {\n const dbPath = path.join(__dirname, '../database/codes.db');\n this.db = new sqlite3.Database(dbPath, (err) => {\n if (err) {\n console.error('Error opening database:', err);\n } else {\n console.log('Connected to SQLite database');\n this.createTables();\n }\n });\n }\n\n createTables() {\n const createCodesTable = `\n CREATE TABLE IF NOT EXISTS codes (\n id INTEGER PRIMARY KEY AUTOINCREMENT,\n title TEXT NOT NULL DEFAULT 'Untitled',\n content TEXT NOT NULL,\n language TEXT NOT NULL DEFAULT 'javascript',\n created_at DATETIME DEFAULT CURRENT_TIMESTAMP,\n updated_at DATETIME DEFAULT CURRENT_TIMESTAMP\n )\n `;\n\n this.db.run(createCodesTable, (err) => {\n if (err) {\n console.error('Error creating codes table:', err);\n } else {\n console.log('Codes table ready');\n }\n });\n }\n\n // Create new code\n createCode(title, content, language) {\n return new Promise((resolve, reject) => {\n const sql = `\n INSERT INTO codes (title, content, language, created_at, updated_at)\n VALUES (?, ?, ?, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)\n `;\n \n this.db.run(sql, [title, content, language], function(err) {\n if (err) {\n reject(err);\n } else {\n resolve({ id: this.lastID });\n }\n });\n });\n }\n\n // Get code by ID\n getCode(id) {\n return new Promise((resolve, reject) => {\n const sql = 'SELECT * FROM codes WHERE id = ?';\n \n this.db.get(sql, [id], (err, row) => {\n if (err) {\n reject(err);\n } else {\n resolve(row);\n }\n });\n });\n }\n\n // Update code\n updateCode(id, title, content, language) {\n return new Promise((resolve, reject) => {\n const sql = `\n UPDATE codes \n SET title = ?, content = ?, language = ?, updated_at = CURRENT_TIMESTAMP\n WHERE id = ?\n `;\n \n this.db.run(sql, [title, content, language, id], function(err) {\n if (err) {\n reject(err);\n } else {\n resolve({ changes: this.changes });\n }\n });\n });\n }\n\n // Get all codes (for listing)\n getAllCodes() {\n return new Promise((resolve, reject) => {\n const sql = `\n SELECT id, title, language, created_at, updated_at,\n substr(content, 1, 100) as preview\n FROM codes \n ORDER BY updated_at DESC\n `;\n \n this.db.all(sql, [], (err, rows) => {\n if (err) {\n reject(err);\n } else {\n resolve(rows);\n }\n });\n });\n }\n\n close() {\n if (this.db) {\n this.db.close();\n }\n }\n}\n\nmodule.exports = Database;\n",
|
5 |
+
"language": "javascript",
|
6 |
+
"createdAt": 1755435485929,
|
7 |
+
"updatedAt": 1755435485929
|
8 |
+
}
|