Skip to content

Instantly share code, notes, and snippets.

@koobitor
Created September 27, 2022 10:00
Show Gist options
  • Save koobitor/0fb7dbd3534fba74360cb9522db18286 to your computer and use it in GitHub Desktop.
Save koobitor/0fb7dbd3534fba74360cb9522db18286 to your computer and use it in GitHub Desktop.

Docker

docker run --name surrealdb -d -p 8000:8000 surrealdb/surrealdb:latest start --user root --pass root

Connect DB

DATA="INFO FOR DB;"
curl --request POST \
	--header "Content-Type: application/json" \
	--user "root:root" \
	--data "${DATA}" \
	http://localhost:8000/sql

Set Database, Namespace

DATA="INFO FOR DB;"
curl --request POST \
	--header "Content-Type: application/json" \
	--header "NS: test" \
	--header "DB: test" \
	--user "root:root" \
	--data "${DATA}" \
	http://localhost:8000/sql

Insert Data without specific id

DATA="CREATE account SET
	name = 'ACME Inc',
	created_at = time::now();"
curl -k -L -s --compressed POST \
	--header "Content-Type: application/json" \
	--header "NS: test" \
	--header "DB: test" \
	--user "root:root" \
	--data "${DATA}" \
	http://localhost:8000/sql

Insert Data with id

DATA="CREATE author:john SET
	name.first = 'John',
	name.last = 'Adams',
	name.full = string::join(' ', name.first, name.last),
	age = 29,
	admin = true,
	signup_at = time::now();"
curl -k -L -s --compressed POST \
	--header "Content-Type: application/json" \
	--header "NS: test" \
	--header "DB: test" \
	--user "root:root" \
	--data "${DATA}" \
	http://localhost:8000/sql

Insert with link table

DATA="CREATE article SET
	created_at = time::now(),
	author = author:john,
	title = 'Lorem ipsum dolor',
	text = 'Donec eleifend, nunc vitae commodo accumsan, mauris est fringilla.',
	account = (SELECT id FROM account WHERE name = 'ACME Inc' LIMIT 1);"
curl -k -L -s --compressed POST \
	--header "Content-Type: application/json" \
	--header "NS: test" \
	--header "DB: test" \
	--user "root:root" \
	--data "${DATA}" \
	http://localhost:8000/sql

Query Data

DATA="SELECT * FROM article;"
curl -k -L -s --compressed POST \
	--header "Content-Type: application/json" \
	--header "NS: test" \
	--header "DB: test" \
	--user "root:root" \
	--data "${DATA}" \
	http://localhost:8000/sql

Query Multi Table

DATA="SELECT * FROM article, account;"
curl -k -L -s --compressed POST \
	--header "Content-Type: application/json" \
	--header "NS: test" \
	--header "DB: test" \
	--user "root:root" \
	--data "${DATA}" \
	http://localhost:8000/sql

Query Relate

DATA="SELECT * FROM article WHERE author.age < 30 FETCH author, account;"
curl -k -L -s --compressed POST \
	--header "Content-Type: application/json" \
	--header "NS: test" \
	--header "DB: test" \
	--user "root:root" \
	--data "${DATA}" \
	http://localhost:8000/sql
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment