1use time::OffsetDateTime;
4
5use crate::crypto::Hash;
6use crate::model::{Col, Model, OnTable};
7use crate::schema::{ColumnDef, ColumnType as T, DefaultValue, OnDelete, SchemaBuilder};
8use crate::value::{ColumnValue, DecodeError, Id, Row, Value};
9
10const fn timestamps() -> [ColumnDef; 2] {
11 [
12 ColumnDef::new("created_at", T::Timestamp).default(DefaultValue::Now),
13 ColumnDef::new("updated_at", T::Timestamp).default(DefaultValue::Now),
14 ]
15}
16
17macro_rules! columns {
18 ($($col:expr),* $(,)?) => {{
19 const STAMPS: [ColumnDef; 2] = timestamps();
20 const COLUMNS: &[ColumnDef] = &[$($col,)* STAMPS[0], STAMPS[1]];
21 COLUMNS
22 }};
23}
24
25pub(crate) fn register(schema: &mut SchemaBuilder) {
26 schema
27 .model::<User>()
28 .model::<Session>()
29 .model::<Account>()
30 .model::<Verification>();
31}
32
33fn get<T: ColumnValue>(row: &Row, column: &str) -> Result<T, DecodeError> {
34 row.get(column)
35}
36
37#[derive(Clone, Debug, PartialEq, serde::Serialize)]
40#[serde(rename_all = "camelCase")]
41pub struct User {
42 pub id: Id,
43 pub email: String,
44 pub email_verified: bool,
45 pub name: String,
46 pub image: Option<String>,
47 #[serde(with = "time::serde::rfc3339")]
48 pub created_at: OffsetDateTime,
49 #[serde(with = "time::serde::rfc3339")]
50 pub updated_at: OffsetDateTime,
51}
52
53impl User {
54 pub const ID: Col<User, Id> = Col::new("id");
55 pub const EMAIL: Col<User, String> = Col::new("email");
56 pub const EMAIL_VERIFIED: Col<User, bool> = Col::new("email_verified");
57 pub const NAME: Col<User, String> = Col::new("name");
58 pub const IMAGE: Col<User, Option<String>> = Col::new("image");
59 pub const CREATED_AT: Col<User, OffsetDateTime> = Col::new("created_at");
60 pub const UPDATED_AT: Col<User, OffsetDateTime> = Col::new("updated_at");
61}
62
63impl OnTable for User {
64 type Table = User;
65}
66
67impl Model for User {
68 const TABLE: &'static str = "user";
69
70 fn columns() -> &'static [ColumnDef] {
71 columns![
72 ColumnDef::new("id", T::Id).primary_key(),
73 ColumnDef::new("email", T::Text).unique(),
74 ColumnDef::new("email_verified", T::Bool).default(DefaultValue::Bool(false)),
75 ColumnDef::new("name", T::Text).input(),
76 ColumnDef::new("image", T::Text).nullable().input(),
77 ]
78 }
79
80 fn decode(row: &Row) -> Result<Self, DecodeError> {
81 Ok(Self {
82 id: get(row, "id")?,
83 email: get(row, "email")?,
84 email_verified: get(row, "email_verified")?,
85 name: get(row, "name")?,
86 image: get(row, "image")?,
87 created_at: get(row, "created_at")?,
88 updated_at: get(row, "updated_at")?,
89 })
90 }
91
92 fn encode(&self) -> Vec<(&'static str, Value)> {
93 vec![
94 ("id", self.id.clone().into_value()),
95 ("email", self.email.clone().into_value()),
96 ("email_verified", self.email_verified.into_value()),
97 ("name", self.name.clone().into_value()),
98 ("image", self.image.clone().into_value()),
99 ("created_at", self.created_at.into_value()),
100 ("updated_at", self.updated_at.into_value()),
101 ]
102 }
103}
104
105#[derive(Clone, Debug, PartialEq)]
107pub struct Session {
108 pub id: Id,
109 pub token_hash: Hash,
110 pub user_id: Id,
111 pub expires_at: OffsetDateTime,
112 pub ip_address: Option<String>,
113 pub user_agent: Option<String>,
114 pub created_at: OffsetDateTime,
115 pub updated_at: OffsetDateTime,
116}
117
118impl Session {
119 pub const ID: Col<Session, Id> = Col::new("id");
120 pub const TOKEN_HASH: Col<Session, Hash> = Col::new("token_hash");
121 pub const USER_ID: Col<Session, Id> = Col::new("user_id");
122 pub const EXPIRES_AT: Col<Session, OffsetDateTime> = Col::new("expires_at");
123 pub const CREATED_AT: Col<Session, OffsetDateTime> = Col::new("created_at");
124 pub const UPDATED_AT: Col<Session, OffsetDateTime> = Col::new("updated_at");
125}
126
127impl OnTable for Session {
128 type Table = Session;
129}
130
131impl Model for Session {
132 const TABLE: &'static str = "session";
133
134 fn columns() -> &'static [ColumnDef] {
135 columns![
136 ColumnDef::new("id", T::Id).primary_key(),
137 ColumnDef::new("token_hash", T::Bytes).unique().hidden(),
138 ColumnDef::new("user_id", T::Id)
139 .references("user", "id", OnDelete::Cascade)
140 .index(),
141 ColumnDef::new("expires_at", T::Timestamp),
142 ColumnDef::new("ip_address", T::Text).nullable(),
143 ColumnDef::new("user_agent", T::Text).nullable(),
144 ]
145 }
146
147 fn decode(row: &Row) -> Result<Self, DecodeError> {
148 Ok(Self {
149 id: get(row, "id")?,
150 token_hash: get(row, "token_hash")?,
151 user_id: get(row, "user_id")?,
152 expires_at: get(row, "expires_at")?,
153 ip_address: get(row, "ip_address")?,
154 user_agent: get(row, "user_agent")?,
155 created_at: get(row, "created_at")?,
156 updated_at: get(row, "updated_at")?,
157 })
158 }
159
160 fn encode(&self) -> Vec<(&'static str, Value)> {
161 vec![
162 ("id", self.id.clone().into_value()),
163 ("token_hash", self.token_hash.into_value()),
164 ("user_id", self.user_id.clone().into_value()),
165 ("expires_at", self.expires_at.into_value()),
166 ("ip_address", self.ip_address.clone().into_value()),
167 ("user_agent", self.user_agent.clone().into_value()),
168 ("created_at", self.created_at.into_value()),
169 ("updated_at", self.updated_at.into_value()),
170 ]
171 }
172}
173
174#[derive(Clone, Debug, PartialEq)]
177pub struct Account {
178 pub id: Id,
179 pub user_id: Id,
180 pub provider_id: String,
181 pub account_id: String,
182 pub password_hash: Option<String>,
183 pub access_token: Option<Vec<u8>>,
185 pub refresh_token: Option<Vec<u8>>,
187 pub id_token: Option<String>,
188 pub access_token_expires_at: Option<OffsetDateTime>,
189 pub refresh_token_expires_at: Option<OffsetDateTime>,
190 pub scope: Option<String>,
191 pub created_at: OffsetDateTime,
192 pub updated_at: OffsetDateTime,
193}
194
195impl Account {
196 pub const CREDENTIAL: &'static str = "credential";
197
198 pub const ID: Col<Account, Id> = Col::new("id");
199 pub const USER_ID: Col<Account, Id> = Col::new("user_id");
200 pub const PROVIDER_ID: Col<Account, String> = Col::new("provider_id");
201 pub const ACCOUNT_ID: Col<Account, String> = Col::new("account_id");
202 pub const PASSWORD_HASH: Col<Account, Option<String>> = Col::new("password_hash");
203 pub const ACCESS_TOKEN: Col<Account, Option<Vec<u8>>> = Col::new("access_token");
204 pub const REFRESH_TOKEN: Col<Account, Option<Vec<u8>>> = Col::new("refresh_token");
205 pub const ID_TOKEN: Col<Account, Option<String>> = Col::new("id_token");
206 pub const ACCESS_TOKEN_EXPIRES_AT: Col<Account, Option<OffsetDateTime>> =
207 Col::new("access_token_expires_at");
208 pub const REFRESH_TOKEN_EXPIRES_AT: Col<Account, Option<OffsetDateTime>> =
209 Col::new("refresh_token_expires_at");
210 pub const SCOPE: Col<Account, Option<String>> = Col::new("scope");
211}
212
213impl OnTable for Account {
214 type Table = Account;
215}
216
217impl Model for Account {
218 const TABLE: &'static str = "account";
219
220 fn columns() -> &'static [ColumnDef] {
221 columns![
222 ColumnDef::new("id", T::Id).primary_key(),
223 ColumnDef::new("user_id", T::Id)
224 .references("user", "id", OnDelete::Cascade)
225 .index(),
226 ColumnDef::new("provider_id", T::Text),
227 ColumnDef::new("account_id", T::Text).index(),
228 ColumnDef::new("password_hash", T::Text).nullable().hidden(),
229 ColumnDef::new("access_token", T::Bytes).nullable().hidden(),
230 ColumnDef::new("refresh_token", T::Bytes)
231 .nullable()
232 .hidden(),
233 ColumnDef::new("id_token", T::Text).nullable().hidden(),
234 ColumnDef::new("access_token_expires_at", T::Timestamp).nullable(),
235 ColumnDef::new("refresh_token_expires_at", T::Timestamp).nullable(),
236 ColumnDef::new("scope", T::Text).nullable(),
237 ]
238 }
239
240 fn decode(row: &Row) -> Result<Self, DecodeError> {
241 Ok(Self {
242 id: get(row, "id")?,
243 user_id: get(row, "user_id")?,
244 provider_id: get(row, "provider_id")?,
245 account_id: get(row, "account_id")?,
246 password_hash: get(row, "password_hash")?,
247 access_token: get(row, "access_token")?,
248 refresh_token: get(row, "refresh_token")?,
249 id_token: get(row, "id_token")?,
250 access_token_expires_at: get(row, "access_token_expires_at")?,
251 refresh_token_expires_at: get(row, "refresh_token_expires_at")?,
252 scope: get(row, "scope")?,
253 created_at: get(row, "created_at")?,
254 updated_at: get(row, "updated_at")?,
255 })
256 }
257
258 fn encode(&self) -> Vec<(&'static str, Value)> {
259 vec![
260 ("id", self.id.clone().into_value()),
261 ("user_id", self.user_id.clone().into_value()),
262 ("provider_id", self.provider_id.clone().into_value()),
263 ("account_id", self.account_id.clone().into_value()),
264 ("password_hash", self.password_hash.clone().into_value()),
265 ("access_token", self.access_token.clone().into_value()),
266 ("refresh_token", self.refresh_token.clone().into_value()),
267 ("id_token", self.id_token.clone().into_value()),
268 (
269 "access_token_expires_at",
270 self.access_token_expires_at.into_value(),
271 ),
272 (
273 "refresh_token_expires_at",
274 self.refresh_token_expires_at.into_value(),
275 ),
276 ("scope", self.scope.clone().into_value()),
277 ("created_at", self.created_at.into_value()),
278 ("updated_at", self.updated_at.into_value()),
279 ]
280 }
281}
282
283#[derive(Clone, Debug, PartialEq)]
286pub struct Verification {
287 pub id: Id,
288 pub identifier: String,
289 pub value_hash: Hash,
290 pub data: Option<String>,
292 pub expires_at: OffsetDateTime,
293 pub created_at: OffsetDateTime,
294 pub updated_at: OffsetDateTime,
295}
296
297impl Verification {
298 pub const ID: Col<Verification, Id> = Col::new("id");
299 pub const IDENTIFIER: Col<Verification, String> = Col::new("identifier");
300 pub const VALUE_HASH: Col<Verification, Hash> = Col::new("value_hash");
301 pub const EXPIRES_AT: Col<Verification, OffsetDateTime> = Col::new("expires_at");
302}
303
304impl OnTable for Verification {
305 type Table = Verification;
306}
307
308impl Model for Verification {
309 const TABLE: &'static str = "verification";
310
311 fn columns() -> &'static [ColumnDef] {
312 columns![
313 ColumnDef::new("id", T::Id).primary_key(),
314 ColumnDef::new("identifier", T::Text).index(),
315 ColumnDef::new("value_hash", T::Bytes).unique().hidden(),
316 ColumnDef::new("data", T::Text).nullable().hidden(),
317 ColumnDef::new("expires_at", T::Timestamp),
318 ]
319 }
320
321 fn decode(row: &Row) -> Result<Self, DecodeError> {
322 Ok(Self {
323 id: get(row, "id")?,
324 identifier: get(row, "identifier")?,
325 value_hash: get(row, "value_hash")?,
326 data: get(row, "data")?,
327 expires_at: get(row, "expires_at")?,
328 created_at: get(row, "created_at")?,
329 updated_at: get(row, "updated_at")?,
330 })
331 }
332
333 fn encode(&self) -> Vec<(&'static str, Value)> {
334 vec![
335 ("id", self.id.clone().into_value()),
336 ("identifier", self.identifier.clone().into_value()),
337 ("value_hash", self.value_hash.into_value()),
338 ("data", self.data.clone().into_value()),
339 ("expires_at", self.expires_at.into_value()),
340 ("created_at", self.created_at.into_value()),
341 ("updated_at", self.updated_at.into_value()),
342 ]
343 }
344}