Quick answer
SurrealDB is a multi-model database: one server that stores documents, graph relations, and time-stamped records, and queries them all in one language called SurrealQL. You can keep customers, products, and orders in one place, then follow the links between them like a graph. This post runs a small Karachi shop on a local SurrealDB server and fixes the record-link error that stops the first insert.
A Karachi shop owner keeps three lists: customers in a notebook, products in a spreadsheet, and orders in a messaging app. The question that matters, which customers bought which products this week, needs all three lists at once, so every answer means copying rows by hand. A document database holds the orders well but cannot follow the links. A graph database follows the links but is overkill for a small shop. SurrealDB promises one database for all three shapes, so this post tests that promise on a laptop with real shop data.
What I would check before using it
My first check is whether the data actually has links worth following. If every query is "find this order by id" or "list all products", a plain document store or a relational table does the job, and the PostgreSQL Explained post covers the safe default. SurrealDB earns its place when the question crosses tables, like "which products did this customer buy", instead of joining three tables or running a second database.
My second check is the schema habit. SurrealDB lets you start schemaless, adding any field to any record, which feels fast until a typo creates two spellings of the same field. This post defines tables as SCHEMAFULL with typed fields, so a wrong value fails loudly at insert time instead of quietly corrupting a query. That strictness is the point.
My third check is the ecosystem around the version. SurrealDB 4.0.1 is the current release, and the storage flag changed between major versions, so a version 2 tutorial will not start the server the same way. Check the docs for your version, and confirm the client libraries you need exist first. The SQLite Explained post shows the opposite end of the scale, a database with almost nothing to install.
My verdict, as judgement: adopt SurrealDB when one project genuinely needs documents, links, and time-based queries together and you want one server instead of three. It flips for a simple app where one model covers everything, or for a team that needs a large hiring pool.
A Karachi shop in one SurrealDB database
The demo is a small Karachi clothing shop: three customers, three products, six orders. One order is cancelled, one is ten days old. Everything runs on a local server started from the standalone binary, from the SurrealDB GitHub releases page. Start it with:
$ ./surreal start --user root --pass root --bind 127.0.0.1:8000 surrealkv://shop.db
Started surrealkv kvs store
Startup complete; instance is now ready to serve
Started web server on 127.0.0.1:8000
Version 4 dropped the old file:// storage path, so use surrealkv:// for a local file. The server listens on port 8000. First, the schema: tables are SCHEMAFULL, so every field has a type, and orders link to customers and products with record links, not copied names:
USE NS shop DB shopdb;
DEFINE TABLE customer SCHEMAFULL;
DEFINE FIELD name ON TABLE customer TYPE string;
DEFINE FIELD city ON TABLE customer TYPE string;
DEFINE TABLE product SCHEMAFULL;
DEFINE FIELD name ON TABLE product TYPE string;
DEFINE FIELD price_rs ON TABLE product TYPE int;
DEFINE TABLE order SCHEMAFULL;
DEFINE FIELD customer ON TABLE order TYPE record<customer>;
DEFINE FIELD product ON TABLE order TYPE record<product>;
DEFINE FIELD qty ON TABLE order TYPE int;
DEFINE FIELD total_rs ON TABLE order TYPE int;
DEFINE FIELD status ON TABLE order TYPE string;
DEFINE FIELD ordered_at ON TABLE order TYPE datetime;
Three customers and three products go in with fixed ids:
CREATE customer:ayesha SET name = 'Ayesha Khan', city = 'Karachi';
CREATE customer:bilal SET name = 'Bilal Ahmed', city = 'Lahore';
CREATE customer:sana SET name = 'Sana Malik', city = 'Islamabad';
CREATE product:kurta_white SET name = 'White Cotton Kurta', price_rs = 2450;
CREATE product:lawn_suit SET name = 'Embroidered Lawn Suit, 3 Piece', price_rs = 4850;
CREATE product:silk_kurta SET name = 'Black Silk Formal Kurta', price_rs = 6200;
The first order insert uses the customer name as plain text:
CREATE order SET customer = 'Ayesha Khan', product = product:kurta_white,
qty = 1, total_rs = 2450, status = 'paid', ordered_at = time::now();
It fails, with a precise error:
["Couldn't coerce value for field `customer` of `order:f3qp8ww2zknkw1zr3xff`:
Expected `record<customer>` but found `'Ayesha Khan'`"]
The field expects a record link, not a string. The fix is to point at the record itself, customer:ayesha, for both fields. Six orders go in that way:
CREATE order:ord2001 SET customer = customer:ayesha, product = product:kurta_white, qty = 1, total_rs = 2450, status = 'paid', ordered_at = time::now();
CREATE order:ord2002 SET customer = customer:ayesha, product = product:lawn_suit, qty = 1, total_rs = 4850, status = 'paid', ordered_at = time::now();
CREATE order:ord2003 SET customer = customer:bilal, product = product:silk_kurta, qty = 1, total_rs = 6200, status = 'paid', ordered_at = time::now();
CREATE order:ord2004 SET customer = customer:sana, product = product:kurta_white, qty = 2, total_rs = 4900, status = 'paid', ordered_at = time::now();
CREATE order:ord2005 SET customer = customer:bilal, product = product:kurta_white, qty = 1, total_rs = 2450, status = 'cancelled', ordered_at = time::now();
CREATE order:ord2006 SET customer = customer:sana, product = product:lawn_suit, qty = 1, total_rs = 4850, status = 'paid', ordered_at = time::now() - 10d;
Record links are one kind of relation. Graph edges are another: RELATE draws a named edge between two records, and the edge itself can carry data like quantity.
RELATE customer:ayesha->bought->product:kurta_white SET qty = 1, ordered_at = time::now();
RELATE customer:ayesha->bought->product:lawn_suit SET qty = 1, ordered_at = time::now();
RELATE customer:bilal->bought->product:silk_kurta SET qty = 1, ordered_at = time::now();
RELATE customer:sana->bought->product:kurta_white SET qty = 2, ordered_at = time::now();
Paid orders, with names resolved through the links, no join syntax needed:
SELECT id, customer.name AS customer_name, product.name AS product_name, qty, total_rs
FROM order WHERE status = 'paid' ORDER BY id;
[{ customer_name: 'Ayesha Khan', id: order:ord2001, product_name: 'White Cotton Kurta', qty: 1, total_rs: 2450 },
{ customer_name: 'Ayesha Khan', id: order:ord2002, product_name: 'Embroidered Lawn Suit, 3 Piece', qty: 1, total_rs: 4850 },
{ customer_name: 'Bilal Ahmed', id: order:ord2003, product_name: 'Black Silk Formal Kurta', qty: 1, total_rs: 6200 },
{ customer_name: 'Sana Malik', id: order:ord2004, product_name: 'White Cotton Kurta', qty: 2, total_rs: 4900 },
{ customer_name: 'Sana Malik', id: order:ord2006, product_name: 'Embroidered Lawn Suit, 3 Piece', qty: 1, total_rs: 4850 }]
Totals by customer, with the cancelled order left out:
SELECT customer.name AS customer, math::sum(total_rs) AS total_paid
FROM order WHERE status = 'paid' GROUP BY customer ORDER BY total_paid DESC;
[{ customer: 'Sana Malik', total_paid: 9750 },
{ customer: 'Ayesha Khan', total_paid: 7300 },
{ customer: 'Bilal Ahmed', total_paid: 6200 }]
The graph query, everything Ayesha bought, by walking the edges:
SELECT * FROM customer:ayesha->bought->product;
[{ id: product:lawn_suit, name: 'Embroidered Lawn Suit, 3 Piece', price_rs: 4850 },
{ id: product:kurta_white, name: 'White Cotton Kurta', price_rs: 2450 }]
Orders from the last seven days, paid only:
SELECT id, total_rs FROM order
WHERE ordered_at > time::now() - 7d AND status = 'paid';
Four rows come back, ord2001 to ord2004. The ten-day-old order stays out, and so does the cancelled one. One database answered a document query, a graph walk, and a time filter without switching tools. For the analytical side, the DuckDB post and the ClickHouse post show what a specialised engine does with the same data.
Common mistakes and how to fix them
First, passing a name string where a record link belongs. The symptom is the error above, "Expected record<customer> but found 'Ayesha Khan'". The fix is to use the record id, customer:ayesha, and define the field as record<customer> so the mistake fails at insert time.
Second, starting the server with the old storage path. The symptom is the server refusing to start: "The file:// scheme is no longer supported; use surrealkv:// instead". The fix is surrealkv://shop.db for a local file, or memory for a throwaway run.
Third, walking graph edges that were never created. The symptom is customer:ayesha->bought->product returning an empty list even though the orders exist, because record links are not graph edges. The fix is to create edges with RELATE, or follow the links with dot notation.
Fourth, forgetting the status filter in totals. The symptom is Bilal's total reading Rs 8,650 instead of Rs 6,200, because his cancelled order sneaks into the sum. The fix is WHERE status = 'paid'.
Frequently Asked Questions
Is SurrealDB a replacement for PostgreSQL? Not for every app. PostgreSQL is the safer default, and the PostgreSQL Explained post covers it. SurrealDB fits when one project needs documents, graph links, and time queries together.
Do I need Docker to try SurrealDB? No. The standalone binary runs the server with one command, as this post does, and the Docker image is an alternative.
What is SurrealQL? The query language for SurrealDB. It covers SELECT, CREATE, UPDATE, and DELETE, plus record links, graph traversal, and time functions, all in one language.
How is SurrealDB different from MongoDB? Both store documents, but SurrealDB adds typed record links and graph edges between them, plus time-based queries. A document store alone cannot walk from a customer to the products they bought.
Can I use SurrealDB for search, like finding products by meaning? Not directly. SurrealDB stores and links data; meaning-based search belongs to a vector database. The Qdrant Tutorial post covers that side.
What to remember
- SurrealDB stores documents, graph relations, and time-stamped records in one server.
- A record link points at another record, like
customer:ayesha. - RELATE creates graph edges, and an edge can carry its own fields.
- SCHEMAFULL with typed fields turns a wrong value into a clear error.
- Version 4 uses
surrealkv://for local storage.
Where this leaves you
Six orders, three customers, and three products went into one local SurrealDB server, which answered a document query, a graph walk, and a time filter. The record-link error was the useful part: it caught a plain name where a link belonged instead of storing bad data. That is the deal: one server for three shapes of data, with strictness that pays off the moment the links matter. Take a small dataset of your own, define its links, and walk them with one query.
Further reading
- SurrealDB documentation
- SurrealQL documentation
- SurrealDB installation
- PostgreSQL Explained: The Reliable Database You Default To
- DuckDB: Analytics Queries on a File, Without a Server
- SQLite Explained: The Database That Lives Inside Your App
- Qdrant Tutorial: Search Your Shop by Meaning, Not Words
- ClickHouse Tutorial: Fast Analytics on Shop Orders
Waqas Shahid is a software engineer from Karachi with more than 12 years of experience, including 6+ years in engineering leadership. He writes The Practical Developer.
Originally published at https://practicaldevblog.blogspot.com/2026/12/surrealdb-tutorial-shop-orders.html

Top comments (0)