|
| 1 | +<!DOCTYPE html> |
| 2 | +<html> |
| 3 | + <head> |
| 4 | + <meta charset="UTF-8" /> |
| 5 | + <title>Development server</title> |
| 6 | + </head> |
| 7 | + <body> |
| 8 | + <p>Open a console to access the Surreal class!</p> |
| 9 | + <script type="module"> |
| 10 | + import { Surreal } from "./assets/wasm/surrealdb/index.js"; |
| 11 | + if (typeof window !== "undefined") { |
| 12 | + window.Surreal = Surreal; |
| 13 | + } |
| 14 | + |
| 15 | + const db = new Surreal(); |
| 16 | + |
| 17 | + try { |
| 18 | + // Connect to the database |
| 19 | + // https://github.com/surrealdb/surrealdb.wasm/issues/3#issuecomment-1264377954 |
| 20 | + await db.connect("indxdb://test"); |
| 21 | + |
| 22 | + // Signin as a namespace, database, or root user |
| 23 | + //await db.signin({ |
| 24 | + // username: "root", |
| 25 | + // password: "root", |
| 26 | + //}); |
| 27 | + |
| 28 | + // Select a specific namespace / database |
| 29 | + await db.use({ ns: "test", db: "test" }); |
| 30 | + |
| 31 | + // Create a new person with a random id |
| 32 | + let created = await db.create("person", { |
| 33 | + title: "Founder & CEO", |
| 34 | + name: { |
| 35 | + first: "Tobie", |
| 36 | + last: "Morgan Hitchcock", |
| 37 | + }, |
| 38 | + marketing: true, |
| 39 | + identifier: Math.random().toString(36).substr(2, 10), |
| 40 | + }); |
| 41 | + console.log("created", created); |
| 42 | + |
| 43 | + // Update a person record with a specific id |
| 44 | + let updated = await db.merge("person:jaime", { |
| 45 | + marketing: true, |
| 46 | + }); |
| 47 | + console.log("updated", updated); |
| 48 | + |
| 49 | + // Select all people records |
| 50 | + let people = await db.select("person"); |
| 51 | + console.log("people", people); |
| 52 | + |
| 53 | + // Perform a custom advanced query |
| 54 | + let groups = await db.query( |
| 55 | + "SELECT marketing, count() FROM type::table($table) GROUP BY marketing", |
| 56 | + { |
| 57 | + table: "person", |
| 58 | + } |
| 59 | + ); |
| 60 | + console.log("groups", groups); |
| 61 | + |
| 62 | + // Delete all people upto but not including Jaime |
| 63 | + let deleted = await db.delete("person:..jaime"); |
| 64 | + console.log("deleted", deleted); |
| 65 | + |
| 66 | + // Delete all people |
| 67 | + await db.delete("person"); |
| 68 | + |
| 69 | + // REF: https://surrealdb.com/docs/surrealql/functions/vector |
| 70 | + // https://github.com/surrealdb/surrealdb/issues/1903 |
| 71 | + let cos = await db.query( |
| 72 | + "RETURN vector::similarity::cosine([1,2,3],[4,5,6])" |
| 73 | + ); |
| 74 | + console.log("cos", cos); |
| 75 | + } catch (e) { |
| 76 | + console.error("ERROR", e); |
| 77 | + } |
| 78 | + </script> |
| 79 | + </body> |
| 80 | +</html> |
0 commit comments