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   荣锋亮  阅读(315)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2022-12-01 dremio 联邦查询试用
2022-12-01 dremio 23.1 发布
2022-12-01 lavinmq & rabbitmq压测对比
2022-12-01 使用lavinmq 做为minio amqp 消息服务
2022-12-01 使用arrow flight-sql-jdbc-driver 链接dremio
2020-12-01 gophercloud 不错的openstack golang sdk
2020-12-01 基于alpine docker镜像应用的参考Dockerfile配置

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示