-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschemafull.surql
More file actions
51 lines (44 loc) · 2.11 KB
/
schemafull.surql
File metadata and controls
51 lines (44 loc) · 2.11 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
DEFINE NAMESPACE test;
DEFINE DATABASE test;
-- Define the user table with proper permissions
-- https://surrealdb.com/docs/surrealql/statements/define/table#defining-permissions
-- https://claude.ai/chat/4abf8c3e-c943-4b03-9ec2-359feb43fd82
DEFINE TABLE user SCHEMAFULL PERMISSIONS
-- this will be used to check permission when we are signin and select all users
FOR SELECT WHERE id = $auth.id OR $auth.admin = true,
-- all users can create user records, like it's own user
FOR CREATE FULL,
FOR UPDATE, DELETE WHERE id = $auth.id OR $auth.admin = true;
-- Define fields
DEFINE FIELD username ON user TYPE string ASSERT $value != NONE;
DEFINE FIELD password ON user TYPE string ASSERT $value != NONE;
-- flexible
DEFINE FIELD settings ON user TYPE object DEFAULT {};
DEFINE FIELD settings.marketing ON user TYPE bool DEFAULT false;
DEFINE FIELD tags ON user TYPE option<array<string>> DEFAULT [];
-- Define unique index
DEFINE INDEX idx_username ON user COLUMNS username UNIQUE;
-- Define access method for authentication
-- https://surrealdb.com/docs/sdk/javascript/core/handling-authentication#defining-access-in-your-application
-- define access method/function
DEFINE ACCESS account ON DATABASE TYPE RECORD
SIGNUP ( CREATE user SET username = $username, password = crypto::argon2::generate($password), settings.marketing = $marketing, tags = $tags )
SIGNIN ( SELECT * FROM user WHERE username = $username AND crypto::argon2::compare(password, $password) )
DURATION FOR TOKEN 15m, FOR SESSION 12h;
-- info for user
INFO FOR TABLE user;
-- Test function
DEFINE FUNCTION fn::greet($name: string) {
RETURN "Hello, " + $name + "!";
};
-- RETURN fn::greet("Tobie");
-- graphql to generate audit fields
-- REMOVE TABLE IF EXISTS restaurant;
-- DEFINE TABLE restaurant SCHEMALESS PERMISSIONS NONE;
-- DEFINE FIELD createdAt ON restaurant VALUE time::now() READONLY;
-- DEFINE FIELD updatedAt ON restaurant VALUE time::now();
--
-- REMOVE TABLE IF EXISTS recipe;
-- DEFINE TABLE recipe SCHEMALESS PERMISSIONS NONE;
-- DEFINE FIELD createdAt ON recipe VALUE time::now() READONLY;
-- DEFINE FIELD updatedAt ON recipe VALUE time::now();