Skip to content

Commit

Permalink
feat: update post table and event
Browse files Browse the repository at this point in the history
  • Loading branch information
dancixx committed Sep 9, 2024
1 parent 83c3740 commit 4181b17
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 21 deletions.
4 changes: 2 additions & 2 deletions events/post_read_time.surql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
DEFINE EVENT post_read_time ON TABLE post WHEN $event = "UPDATE" && $before.body != $after.body THEN {
DEFINE EVENT post_read_time ON TABLE post WHEN ($event = "UPDATE" || $event = "CREATE") && $before.body != $after.body THEN {
LET $read_time = array::len(string::words($after.body)) / 200;
UPDATE post SET read_time = math::round($read_time) WHERE id = $after.id;
UPDATE post SET read_time = math::max([1, $read_time]) WHERE id = $after.id;
};
9 changes: 0 additions & 9 deletions migrations/20240813_235948_update_read_time_event.surql

This file was deleted.

8 changes: 0 additions & 8 deletions migrations/20240815_215405_update_read_time_event.surql

This file was deleted.

5 changes: 4 additions & 1 deletion migrations/definitions/_initial.json
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
{"schemas":"DEFINE TABLE author TYPE NORMAL SCHEMAFULL\n PERMISSIONS \n FOR select FULL\n FOR create, update, delete NONE;\n\nDEFINE FIELD id ON author TYPE record<author> ASSERT $value != NONE;\nDEFINE FIELD name ON author TYPE string ASSERT $value != NONE;\nDEFINE FIELD bio ON author TYPE option<string>;\nDEFINE FIELD email ON author TYPE string ASSERT string::is::email($value);\nDEFINE FIELD linkedin ON author TYPE option<string>;\nDEFINE FIELD twitter ON author TYPE option<string>;\nDEFINE FIELD github ON author TYPE option<string>;\n\nDEFINE TABLE post TYPE NORMAL SCHEMAFULL \n PERMISSIONS \n FOR select FULL\n FOR create, update, delete NONE;\n\nDEFINE FIELD id ON post TYPE record<post> ASSERT $value != NONE;\nDEFINE FIELD title ON post TYPE string ASSERT $value != NONE;\nDEFINE FIELD summary ON post TYPE string ASSERT $value != NONE;\nDEFINE FIELD body ON post TYPE string ASSERT $value != NONE;\nDEFINE FIELD slug ON post TYPE option<string>;\nDEFINE FIELD tags ON post TYPE array<string> ASSERT $value != NONE;\nDEFINE FIELD author ON post TYPE record<author> ASSERT $value != NONE;\nDEFINE FIELD read_time ON post TYPE int ASSERT $value != NONE;\nDEFINE FIELD total_views ON post TYPE int DEFAULT 0;\nDEFINE FIELD created_at ON post TYPE datetime DEFAULT time::now();\nDEFINE FIELD updated_at ON post TYPE datetime VALUE time::now();\nDEFINE TABLE script_migration SCHEMAFULL\n PERMISSIONS\n FOR select FULL\n FOR create, update, delete NONE;\n\nDEFINE FIELD script_name ON script_migration TYPE string;\nDEFINE FIELD executed_at ON script_migration TYPE datetime VALUE time::now() READONLY;","events":"DEFINE EVENT post_read_time ON TABLE post WHEN $event = \"UPDATE\" && $before.body != $after.body THEN {\n LET $read_time = array::len(string::words($after.body)) / 200;\n UPDATE post SET read_time = math::round($read_time) WHERE id = $after.id;\n};\n\nDEFINE EVENT post_slug ON TABLE post WHEN ($event = \"CREATE\" OR $event = \"UPDATE\") && $before.title != $after.title THEN (\n UPDATE post SET slug = string::slug($after.title) WHERE id = $after.id\n);\n"}
{
"schemas": "DEFINE TABLE author TYPE NORMAL SCHEMAFULL\n PERMISSIONS \n FOR select FULL\n FOR create, update, delete NONE;\n\nDEFINE FIELD id ON author TYPE record<author> ASSERT $value != NONE;\nDEFINE FIELD name ON author TYPE string ASSERT $value != NONE;\nDEFINE FIELD bio ON author TYPE option<string>;\nDEFINE FIELD email ON author TYPE string ASSERT string::is::email($value);\nDEFINE FIELD linkedin ON author TYPE option<string>;\nDEFINE FIELD twitter ON author TYPE option<string>;\nDEFINE FIELD github ON author TYPE option<string>;\n\nDEFINE TABLE post TYPE NORMAL SCHEMAFULL \n PERMISSIONS \n FOR select FULL\n FOR create, update, delete NONE;\n\nDEFINE FIELD id ON post TYPE record<post> ASSERT $value != NONE;\nDEFINE FIELD title ON post TYPE string ASSERT $value != NONE;\nDEFINE FIELD summary ON post TYPE string ASSERT $value != NONE;\nDEFINE FIELD body ON post TYPE string ASSERT $value != NONE;\nDEFINE FIELD slug ON post TYPE option<string>;\nDEFINE FIELD tags ON post TYPE array<string> ASSERT $value != NONE;\nDEFINE FIELD author ON post TYPE record<author> ASSERT $value != NONE;\nDEFINE FIELD read_time ON post TYPE int ASSERT $value != NONE;\nDEFINE FIELD total_views ON post TYPE int DEFAULT 0;\nDEFINE FIELD created_at ON post TYPE datetime DEFAULT time::now();\nDEFINE FIELD updated_at ON post TYPE datetime VALUE time::now();\nDEFINE TABLE script_migration SCHEMAFULL\n PERMISSIONS\n FOR select FULL\n FOR create, update, delete NONE;\n\nDEFINE FIELD script_name ON script_migration TYPE string;\nDEFINE FIELD executed_at ON script_migration TYPE datetime VALUE time::now() READONLY;",
"events": "DEFINE EVENT post_read_time ON TABLE post WHEN $event = \"UPDATE\" && $before.body != $after.body THEN {\n LET $read_time = array::len(string::words($after.body)) / 200;\n UPDATE post SET read_time = math::round($read_time) WHERE id = $after.id;\n};\n\nDEFINE EVENT post_slug ON TABLE post WHEN ($event = \"CREATE\" OR $event = \"UPDATE\") && $before.title != $after.title THEN (\n UPDATE post SET slug = string::slug($after.title) WHERE id = $after.id\n);\n"
}
2 changes: 1 addition & 1 deletion schemas/post.surql
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ DEFINE FIELD body ON post TYPE string ASSERT $value != NONE;
DEFINE FIELD slug ON post TYPE option<string>;
DEFINE FIELD tags ON post TYPE array<string> ASSERT $value != NONE;
DEFINE FIELD author ON post TYPE record<author> ASSERT $value != NONE;
DEFINE FIELD read_time ON post TYPE int ASSERT $value != NONE;
DEFINE FIELD read_time ON post TYPE option<int>;
DEFINE FIELD total_views ON post TYPE int DEFAULT 0;
DEFINE FIELD created_at ON post TYPE datetime DEFAULT time::now();
DEFINE FIELD updated_at ON post TYPE datetime VALUE time::now();

0 comments on commit 4181b17

Please sign in to comment.