Skip to content

Instantly share code, notes, and snippets.

@Fogapod
Last active July 25, 2020 17:17
Show Gist options
  • Save Fogapod/4c4b31ea344b135059b291d772d056ec to your computer and use it in GitHub Desktop.
Save Fogapod/4c4b31ea344b135059b291d772d056ec to your computer and use it in GitHub Desktop.
ROLLBACK;
START TRANSACTION;
START MIGRATION TO {
module default {
type User {
required property nickname -> str;
required property email -> str;
required property email_verified -> bool {
default := false;
};
required property password -> bytes;
required property created_at -> datetime {
default := datetime_current();
};
}
function create_user(nickname: str, email: str, pass: bytes) -> optional User
{
volatility := 'VOLATILE';
using EdgeQL $$
WITH
MODULE DEFAULT
conflicting := (
SELECT User {
created_at,
}
FILTER .nickname = nickname OR .email = email
LIMIT 2
),
deleted := (
FOR x IN {conflicting}
UNION (
DELETE
x
IF
NOT .email_verified
AND (datetime_of_transaction() - .created_at) > to_duration(hours := 24 * 5)
ELSE
<User>{}
)
)
SELECT (
(INSERT User {
nickname := nickname,
email := email,
password := password,
})
IF
len(deleted) = len(conflicting)
ELSE
<User>{}
)
$$;
};
};
};
POPULATE MIGRATION;
COMMIT MIGRATION;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment