edgedb 基本试用

Posted rongfengliang-荣锋亮

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了edgedb 基本试用相关的知识,希望对你有一定的参考价值。

环境准备

使用docker-compose 运行

  • docker-compose 文件
version: "3"
services:
  db:
    image: edgedb/edgedb
    ports:
    - "5656:5656"
    - "8888:8888"

基本数据库schema 操作

  • 修改密码

    进入容器

按照提下提示操作
edgedb --admin alter role edgedb --password
  • 创建数据库
CREATE DATABASE tutorial; 
  • 连接创建的数据库
\\c tutorial
  • 创建sdl

    数据库schema 定义语言

START TRANSACTION;

CREATE MIGRATION movies TO {
    type Movie {
        required property title -> str;
        # the year of release
        property year -> int64;
        required link director -> Person;
        multi link cast -> Person;
    }
    type Person {
        required property first_name -> str;
        required property last_name -> str;
    }
};


COMMIT MIGRATION movies;

COMMIT;

效果

START TRANSACTION;                                                                                                                         
START TRANSACTION
edgedb> CREATE MIGRATION movies TO { 
....... type Movie { 
....... required property title -> str; 
....... # the year of release 
....... property year -> int64; 
....... required link director -> Person; 
....... multi link cast -> Person; 
....... } 
....... type Person { 
....... required property first_name -> str; 
....... required property last_name -> str; 
....... } 
....... };                                                                                                                                         
CREATE MIGRATION
edgedb> COMMIT MIGRATION movies; 
.......                                                                                                                                            
COMMIT MIGRATION
edgedb> COMMIT;                                                                                                                                    
COMMIT TRANSACTION
  • ddl

    可选的sdl 方式

CREATE TYPE Person {
    CREATE REQUIRED PROPERTY first_name -> str;
    CREATE REQUIRED PROPERTY last_name -> str;
};
CREATE TYPE Movie {
    CREATE REQUIRED PROPERTY title -> str;
    # the year of release
    CREATE PROPERTY year -> int64;
    CREATE REQUIRED LINK director -> Person;
    CREATE MULTI LINK cast -> Person;
};

EdgeQL查询语言

  • 插入数据
INSERT Movie {
    title := \'Blade Runner 2049\',
    year := 2017,
    director := (
        INSERT Person {
            first_name := \'Denis\',
            last_name := \'Villeneuve\',
        }
    ),
    cast := {
        (INSERT Person {
            first_name := \'Harrison\',
            last_name := \'Ford\',
        }),
        (INSERT Person {
            first_name := \'Ryan\',
            last_name := \'Gosling\',
        }),
        (INSERT Person {
            first_name := \'Ana\',
            last_name := \'de Armas\',
        }),
    }
};

效果

{Object { id: <uuid>\'66807bcc-5e05-11e9-a37c-2fb9bd2b9a50\' }}
  • insert select 操作
INSERT Movie {
    title := \'Dune\',
    director := (
        SELECT Person
        FILTER
            # the last name is sufficient
            # to identify the right person
            .last_name = \'Villeneuve\'
    )
};
  • select 操作
select Movie;

效果

select Movie; 
.......                                                                                                                                            
{
    Object { id: <uuid>\'66807bcc-5e05-11e9-a37c-2fb9bd2b9a50\' },
    Object { id: <uuid>\'ae442d8c-5e05-11e9-a37c-cfe0a3967be9\' }
}
  • 查询指定字段数据
SELECT Movie {
    title,
    year
};

效果

SELECT Movie { 
....... title, 
....... year 
....... };                                                                                                                                         
{Object { title: \'Blade Runner 2049\', year: 2017 }, Object { title: \'Dune\', year: {} }}
  • filter 操作
SELECT Movie {
    title,
    year
}
FILTER .title ILIKE \'blade runner%\';

效果

SELECT Movie { 
....... title, 
....... year 
....... } 
....... FILTER .title ILIKE \'blade runner%\';                                                                                                       
{Object { title: \'Blade Runner 2049\', year: 2017 }}
  • 数据类型修改
ALTER TYPE Person {
    ALTER PROPERTY first_name {
        DROP REQUIRED;
    }
};

graphql 查询

  • 启动graphql

    注意需要在数据库中操作,先连接数据库 \\c tutorial

CONFIGURE SYSTEM INSERT Port {
    protocol := "graphql+http",
    database := "tutorial",
    address := "0.0.0.0",
    port := 8888,
    user := "http",
    concurrency := 4,
};

效果

tutorial> CONFIGURE SYSTEM INSERT Port { 
......... protocol := "graphql+http", 
......... database := "tutorial", 
......... address := "0.0.0.0", 
......... port := 8888, 
......... user := "http", 
......... concurrency := 4, 
......... };                                                                                                                                       
CONFIGURE SYSTEM
  • 访问地址
http://localhost:8888/explore
  • 操作

  • graphql 查询
query {
  Movie {
    director {
      id
      last_name
      first_name
    }
    id
    cast {
      id
      last_name
      first_name
    }
    title
    year
  }
}

说明

edgedb schema 定义的地方以及查询上有好多地方和graphql 规范很相近,同时保留了sql 强大的能力,还是很不错的,但是目前的 
client sdk 还是有点少

参考资料

https://edgedb.com/docs/intro 
https://hub.docker.com/r/edgedb/edgedb 
https://github.com/edgedb/edgedb

以上是关于edgedb 基本试用的主要内容,如果未能解决你的问题,请参考以下文章

edgedb-js 来自官方的js 驱动

EdgeDB 1.0 Alpha 4 发布了

edgedb 内部pg 数据存储的探索

edgedb 1.0 Alpha 2 了

超越PostgreSQL?下一代对象关系数据库EdgeDB

c_cpp Robolution基本代码片段