|
@@ -0,0 +1,64 @@
|
|
|
+var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
|
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
|
+ return new (P || (P = Promise))(function (resolve, reject) {
|
|
|
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
|
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
|
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
|
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
|
+ });
|
|
|
+};
|
|
|
+import Surreal, { RecordId } from 'surrealdb';
|
|
|
+const appContainer = document.getElementById('app');
|
|
|
+const db = new Surreal();
|
|
|
+const auth = {
|
|
|
+ username: 'root',
|
|
|
+ password: 'root',
|
|
|
+};
|
|
|
+const db_url = "http://localhost:8000";
|
|
|
+const db_name = { namespace: "ts_test", database: "ts_test" };
|
|
|
+export function initDb() {
|
|
|
+ return __awaiter(this, void 0, void 0, function* () {
|
|
|
+ try {
|
|
|
+ yield db.connect(db_url, { auth });
|
|
|
+ yield db.use(db_name);
|
|
|
+ //create test db
|
|
|
+ const user_table = yield db.create('user');
|
|
|
+ //create two test entries
|
|
|
+ yield db.create(new RecordId("user", "test"), { first_name: "test", last_name: "user" });
|
|
|
+ yield db.create(new RecordId("user", "test2"), { first_name: "test2", last_name: "user" });
|
|
|
+ }
|
|
|
+ catch (err) {
|
|
|
+ console.error("Failed to init SurrealDB:", err instanceof Error ? err.message : String(err));
|
|
|
+ }
|
|
|
+ });
|
|
|
+}
|
|
|
+export function getDb() {
|
|
|
+ return __awaiter(this, void 0, void 0, function* () {
|
|
|
+ try {
|
|
|
+ yield db.connect(db_url, { auth });
|
|
|
+ yield db.use(db_name);
|
|
|
+ return db;
|
|
|
+ }
|
|
|
+ catch (err) {
|
|
|
+ console.error("Failed to connect to SurrealDB:", err instanceof Error ? err.message : String(err));
|
|
|
+ yield db.close();
|
|
|
+ throw err;
|
|
|
+ }
|
|
|
+ });
|
|
|
+}
|
|
|
+function run() {
|
|
|
+ return __awaiter(this, void 0, void 0, function* () {
|
|
|
+ if (appContainer) {
|
|
|
+ try {
|
|
|
+ yield initDb();
|
|
|
+ const db_con = yield getDb();
|
|
|
+ const users = yield db_con.select('User');
|
|
|
+ appContainer.textContent = JSON.stringify(users, null, 2);
|
|
|
+ }
|
|
|
+ catch (err) {
|
|
|
+ console.error("Failed to retrieve db_con:", err instanceof Error ? err.message : String(err));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+}
|
|
|
+run();
|