Skip to content

Commit

Permalink
🍱 improve ecommerce template
Browse files Browse the repository at this point in the history
  • Loading branch information
Odonno committed Jul 9, 2023
1 parent b559d33 commit 1a768cf
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 38 deletions.
7 changes: 5 additions & 2 deletions templates/ecommerce/events/purchase.surql
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
DEFINE TABLE purchase SCHEMALESS;
DEFINE TABLE purchase SCHEMALESS
PERMISSIONS
FOR select, create FULL
FOR update, delete NONE;

DEFINE FIELD customer ON purchase TYPE record(customer);
DEFINE FIELD product ON purchase TYPE record(product);
Expand All @@ -9,7 +12,7 @@ DEFINE EVENT purchase ON TABLE purchase WHEN $before == NONE THEN {
LET $from = (SELECT * FROM customer WHERE id == $after.customer);
LET $to = (SELECT * FROM product WHERE id == $after.product);

RELATE $from->purchased->$to CONTENT {
RELATE $from->purchases->$to CONTENT {
quantity: $after.quantity,
total: $after.total,
status: 'Pending',
Expand Down
8 changes: 0 additions & 8 deletions templates/ecommerce/events/register_customer.surql

This file was deleted.

18 changes: 12 additions & 6 deletions templates/ecommerce/migrations/YYYYMMDD_HHMM01_AddCustomers.surql
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,48 @@ INSERT INTO address {
number: '221B',
street: 'Baker street',
city: 'London',
country: 'UK'
country: 'UK',
customer: customer:pratim
};

INSERT INTO customer {
id: 'pratim',
name: 'Pratim',
email: '[email protected]',
address: address:pratim_home
password: '12345678',
addresses: [address:pratim_home]
};

INSERT INTO address {
id: 'tobie_home',
number: '221A',
street: 'Church street',
city: 'London',
country: 'UK'
country: 'UK',
customer: customer:tobie
};

INSERT INTO customer {
id: 'tobie',
name: 'Tobie',
email: '[email protected]',
address: address:tobie_home
password: '12345678',
addresses: [address:tobie_home]
};

INSERT INTO address {
id: 'alex_home',
number: '221C',
street: 'Pound street',
city: 'London',
country: 'UK'
country: 'UK',
customer: customer:alex
};

INSERT INTO customer {
id: 'alex',
name: 'Alex',
email: '[email protected]',
address: address:alex_home
password: '12345678',
addresses: [address:alex_home]
};
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
RELATE customer:pratim->purchased->product:iphone CONTENT {
RELATE customer:pratim->purchases->product:iphone CONTENT {
quantity: 1,
total: 600,
status: 'Pending',
};

RELATE customer:pratim->purchased->product:shirt CONTENT {
RELATE customer:pratim->purchases->product:shirt CONTENT {
quantity: 2,
total: 40,
status: 'Delivered',
};

RELATE customer:alex->purchased->product:iphone CONTENT {
RELATE customer:alex->purchases->product:iphone CONTENT {
quantity: 1,
total: 600,
status: 'Pending',
};

RELATE customer:alex->purchased->product:shirt CONTENT {
RELATE customer:alex->purchases->product:shirt CONTENT {
quantity: 2,
total: 12,
status: 'Delivered',
};

RELATE customer:tobie->purchased->product:iphone CONTENT {
RELATE customer:tobie->purchases->product:iphone CONTENT {
quantity: 1,
total: 600,
status: 'Pending',
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
DELETE purchased;
DELETE purchases;
7 changes: 5 additions & 2 deletions templates/ecommerce/schemas/address.surql
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
DEFINE TABLE address SCHEMALESS;
DEFINE TABLE address SCHEMALESS
PERMISSIONS
FOR select, create, update, delete WHERE customer = $auth.id;

DEFINE FIELD number ON address TYPE string;
DEFINE FIELD street ON address TYPE string;
DEFINE FIELD city ON address TYPE string;
DEFINE FIELD state ON address TYPE string;
DEFINE FIELD zip_code ON address TYPE string;
DEFINE FIELD country ON address TYPE string;
DEFINE FIELD country ON address TYPE string;
DEFINE FIELD customer ON address TYPE record(customer);
27 changes: 25 additions & 2 deletions templates/ecommerce/schemas/customer.surql
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
DEFINE TABLE customer SCHEMALESS;
DEFINE TABLE customer SCHEMALESS
PERMISSIONS
FOR select FULL
FOR update WHERE id = $auth.id
FOR create, delete NONE;

DEFINE FIELD name ON customer TYPE string;
DEFINE FIELD email ON customer TYPE string;
DEFINE FIELD address ON customer TYPE record(address);
DEFINE FIELD password ON customer TYPE string ASSERT $value != NONE;
DEFINE FIELD addresses ON customer TYPE array;
DEFINE FIELD addresses.* ON customer TYPE record(address);

DEFINE INDEX unique_email ON customer COLUMNS email UNIQUE;

DEFINE SCOPE customer_scope
SESSION 30d
SIGNUP (
CREATE customer
SET
name = $name,
email = $email,
password = crypto::argon2::generate($password)
)
SIGNIN (
SELECT *
FROM customer
WHERE email = $email AND crypto::argon2::compare(password, $password)
);
8 changes: 6 additions & 2 deletions templates/ecommerce/schemas/product.surql
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
DEFINE TABLE product SCHEMALESS;
DEFINE TABLE product SCHEMALESS
PERMISSIONS
FOR select FULL
FOR create, update, delete NONE;

DEFINE FIELD name ON product TYPE string;
DEFINE FIELD description ON product TYPE string;
DEFINE FIELD price ON product TYPE number;
DEFINE FIELD category ON product TYPE string;
DEFINE FIELD images ON product TYPE array;
DEFINE FIELD images ON product TYPE array;
DEFINE FIELD images.* ON product TYPE string;
10 changes: 0 additions & 10 deletions templates/ecommerce/schemas/purchased.surql

This file was deleted.

13 changes: 13 additions & 0 deletions templates/ecommerce/schemas/purchases.surql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# in: customer
# out: product
DEFINE TABLE purchases SCHEMALESS
PERMISSIONS
FOR select WHERE in = $auth.id
FOR create, update, delete NONE;

DEFINE FIELD quantity ON purchases TYPE number;
DEFINE FIELD shipping_address ON purchases TYPE record(address);
DEFINE FIELD created_at ON purchases TYPE datetime VALUE $before OR time::now();
DEFINE FIELD shipped_at ON purchases TYPE datetime;
DEFINE FIELD total ON purchases TYPE number;
DEFINE FIELD status ON purchases TYPE string VALUE $value OR $before OR 'Pending' ASSERT $value == NONE OR $value INSIDE ['Pending', 'Delivered'];

0 comments on commit 1a768cf

Please sign in to comment.