surrealdb 试用

surrealdb 是一个很不错的多模数据库,以下是一个简单试用

环境准备

基于docker-compose

  • docker-compose 文件
    添加了本地存储
 
version: "3"
services:
    db:
       image: surrealdb/surrealdb:latest
       command: start  --log trace --user root --pass root  file:///var/lib/surrealdb/mydatabase.db
       volumes:
         - ./db/mydatabase.db:/var/lib/surrealdb/mydatabase.db
       ports:
       - "8000:8000"

代码集成

基于了nodejs,使用了nodejs 的js client 以及webassembly 的client

  • app.mjs
import { Surreal } from 'surrealdb.js';
 
const db = new Surreal({
    onClose: () => {
        console.log("CLOSED")
    },
    onError: (e) => {
        console.log("ERROR",e)
    },
    onConnect: () => {
        console.log("CONNECTED")
    },
}
);
 
async function main() {
    try {
        // Connect to the database
        await db.connect('http://127.0.0.1:8000/rpc', {
            // Set the namespace and database for the connection
            // Set the authentication details for the connection
            namespace: 'demo',
            database: 'demo',
            auth: {
                username: 'root',
                password: 'root',
                namespace: 'demo',
                database: 'demo',
            },
        });
        // Create a new person with a random id
        const created = await db.create('person', {
            title: 'Founder & CEO',
            name: {
                first: 'Tobie',
                last: 'Morgan Hitchcock',
            },
            marketing: true,
            identifier: Math.random().toString(36).substr(2, 10),
        });
 
        for (let index = 0; index < 10; index++) {
            const updated = await db.merge('person:jaime', {
                marketing: true,
                identifier: Math.random().toString(36).substr(2, 10)
            });
 
        }
        // Update a person record with a specific id
 
 
    } catch (e) {
        console.error('ERROR', e);
    }
}
 
main();
  • demo.mjs
    一个client 实现live 查询
 
import { Surreal } from 'surrealdb.js';
 
const db = new Surreal({
    onClose: () => {
        console.log("CLOSED")
    },
    onError: (e) => {
        console.log("ERROR",e)
    },
    onConnect: () => {
        console.log("CONNECTED")
    },
});
 
async function main() {
    try {
        // Connect to the database
        await db.connect('http://127.0.0.1:8000/rpc', {
            // Set the namespace and database for the connection
            // Set the authentication details for the connection
            namespace: 'demo',
            database: 'demo',
            auth: {
                username: 'root',
                password: 'root',
                namespace: 'demo',
                database: 'demo',
            },
        });
 
        // Select all people records
        const queryUuid = await db.live(
            "person",
            // The callback function takes an object with the "action" and "result" properties
            ({ action, result }) => {
                // action can be: "CREATE", "UPDATE", "DELETE" or "CLOSE"
                if (action === "CLOSE") return;
 
                // result contains either the entire record, or a set of JSON patches when diff mode is enabled
                console.log(result,action)
            }
        )
 
    } catch (e) {
        console.error('ERROR', e);
    }
}
 
main();
  • wasm.mjs
import { Surreal } from 'surrealdb.wasm';
 
const db = new Surreal({
    onClose: () => {
        console.log("CLOSED")
    },
    onError: (e) => {
        console.log("ERROR",e)
    },
    onConnect: () => {
        console.log("CONNECTED")
    },
});
 
async function main() {
    try {
        // Connect to the database
        await db.connect('http://127.0.0.1:8000');
 
        await db.signin({
            username: "root",
            password: "root",
        });
 
        // Select a specific namespace / database
        await db.use({ ns: "demo", db: "demo" });
 
 
        // Select all people records
        let people = await db.select("person");
        console.log(people);
 
    } catch (e) {
        console.error('ERROR', e);
    }
}
 
main();
  • 效果

说明

目前webassembly 模式不支持live 查询,而且目前webassembly 的代码与js 的还是有一些区别的,开发上基于了rust 模块,使用了wasm_bindgen
surrealdb 这个项目还是比较有意思的,很值得看看

参考资料

https://github.com/surrealdb/surrealdb.wasm
https://surrealdb.com/
https://github.com/surrealdb/surrealdb
https://github.com/rustwasm/wasm-bindgen
https://github.com/rongfengliang/surrealdb_learning

posted on 2023-12-01 11:51  荣锋亮  阅读(204)  评论(0编辑  收藏  举报

导航