docbox-database 0.11.1

Docbox database structures, logic, and migrations
Documentation
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
use super::{
    document_box::DocumentBoxScopeRaw,
    file::{File, FileWithExtra},
    link::{Link, LinkWithExtra},
    user::{User, UserId},
};
use crate::{
    DbExecutor, DbPool, DbResult,
    models::shared::{CountResult, DocboxInputPair, FolderPathSegment, WithFullPath},
};
use chrono::{DateTime, Utc};
use serde::Serialize;
use sqlx::{postgres::PgQueryResult, prelude::FromRow};
use tokio::try_join;
use utoipa::ToSchema;
use uuid::Uuid;

pub type FolderId = Uuid;

/// Folder with all the children resolved
#[derive(Debug, Default, Serialize)]
pub struct ResolvedFolder {
    /// List of folders within the folder
    pub folders: Vec<Folder>,
    /// List of files within the folder
    pub files: Vec<File>,
    /// List of links within the folder
    pub links: Vec<Link>,
}

impl ResolvedFolder {
    pub async fn resolve(db: &DbPool, folder_id: FolderId) -> DbResult<ResolvedFolder> {
        let files_futures = File::find_by_parent(db, folder_id);
        let folders_future = Folder::find_by_parent(db, folder_id);
        let links_future = Link::find_by_parent(db, folder_id);

        let (files, folders, links) = try_join!(files_futures, folders_future, links_future)?;

        Ok(ResolvedFolder {
            folders,
            files,
            links,
        })
    }
}

/// Folder with all the children resolved, children also
/// resolve the user and last modified data
#[derive(Debug, Default, Serialize, ToSchema)]
pub struct ResolvedFolderWithExtra {
    /// Path to the resolved folder
    pub path: Vec<FolderPathSegment>,
    /// List of folders within the folder
    pub folders: Vec<FolderWithExtra>,
    /// List of files within the folder
    pub files: Vec<FileWithExtra>,
    /// List of links within the folder
    pub links: Vec<LinkWithExtra>,
}

impl ResolvedFolderWithExtra {
    pub async fn resolve(
        db: &DbPool,
        folder_id: FolderId,
        path: Vec<FolderPathSegment>,
    ) -> DbResult<ResolvedFolderWithExtra> {
        let files_futures = File::find_by_parent_folder_with_extra(db, folder_id);
        let folders_future = Folder::find_by_parent_with_extra(db, folder_id);
        let links_future = Link::find_by_parent_with_extra(db, folder_id);

        let (files, folders, links) = try_join!(files_futures, folders_future, links_future)?;

        Ok(ResolvedFolderWithExtra {
            path,
            folders,
            files,
            links,
        })
    }
}

#[derive(Debug, Clone, Serialize, ToSchema, FromRow, sqlx::Type)]
#[sqlx(type_name = "docbox_folder")]
pub struct Folder {
    /// Unique identifier for the folder
    #[schema(value_type = Uuid)]
    pub id: FolderId,
    /// Name of the file
    pub name: String,

    /// Whether the folder is marked as pinned
    pub pinned: bool,

    /// ID of the document box the folder belongs to
    pub document_box: DocumentBoxScopeRaw,
    /// Parent folder ID if the folder is a child
    #[schema(value_type = Option<Uuid>)]
    pub folder_id: Option<FolderId>,

    /// When the folder was created
    pub created_at: DateTime<Utc>,
    /// User who created the folder
    #[serde(skip)]
    pub created_by: Option<UserId>,
}

impl Eq for Folder {}

impl PartialEq for Folder {
    fn eq(&self, other: &Self) -> bool {
        self.id.eq(&other.id)
            && self.name.eq(&other.name)
            && self.pinned.eq(&other.pinned)
            && self.document_box.eq(&other.document_box)
            && self.folder_id.eq(&other.folder_id)
            && self.created_by.eq(&self.created_by)
            // Reduce precision when checking creation timestamp
            // (Database does not store the full precision)
            && self
                .created_at
                .timestamp_millis()
                .eq(&other.created_at.timestamp_millis())
    }
}

#[derive(Debug, Clone, FromRow, Serialize, ToSchema)]
pub struct FolderWithExtra {
    #[serde(flatten)]
    pub folder: Folder,
    #[schema(nullable, value_type = User)]
    pub created_by: Option<User>,
    #[schema(nullable, value_type = User)]
    pub last_modified_by: Option<User>,
    /// Last time the folder was modified
    pub last_modified_at: Option<DateTime<Utc>>,
}

#[derive(Debug, Clone, Default)]
pub struct CreateFolder {
    pub name: String,
    pub document_box: DocumentBoxScopeRaw,
    pub folder_id: Option<FolderId>,
    pub created_by: Option<UserId>,
}

#[derive(Debug, Serialize)]
pub struct FolderChildrenCount {
    pub file_count: i64,
    pub link_count: i64,
    pub folder_count: i64,
}

impl Folder {
    pub async fn create(
        db: impl DbExecutor<'_>,
        CreateFolder {
            name,
            document_box,
            folder_id,
            created_by,
        }: CreateFolder,
    ) -> DbResult<Folder> {
        let folder = Folder {
            id: Uuid::new_v4(),
            name,
            document_box,
            folder_id,
            created_by,
            created_at: Utc::now(),
            pinned: false,
        };

        sqlx::query(
            r#"
            INSERT INTO "docbox_folders" (
                "id", "name", "document_box",  "folder_id",
                "created_by", "created_at"
            )
            VALUES ($1, $2, $3, $4, $5, $6)
        "#,
        )
        .bind(folder.id)
        .bind(folder.name.as_str())
        .bind(folder.document_box.as_str())
        .bind(folder.folder_id)
        .bind(folder.created_by.as_ref())
        .bind(folder.created_at)
        .bind(folder.pinned)
        .execute(db)
        .await?;

        Ok(folder)
    }

    /// Collects the IDs of all child folders within the current folder
    ///
    /// Results are passed to the search engine when searching within a
    /// specific folder to only get results from the folder subtree
    pub async fn tree_all_children(&self, db: impl DbExecutor<'_>) -> DbResult<Vec<FolderId>> {
        #[derive(FromRow)]
        struct TempIdRow {
            id: FolderId,
        }

        let results: Vec<TempIdRow> =
            sqlx::query_as(r#"SELECT "id" FROM recursive_folder_children_ids($1)"#)
                .bind(self.id)
                .fetch_all(db)
                .await?;

        Ok(results.into_iter().map(|value| value.id).collect())
    }

    /// Uses a recursive query to count all the children in the provided
    /// folder
    pub async fn count_children(
        db: impl DbExecutor<'_>,
        folder_id: FolderId,
    ) -> DbResult<FolderChildrenCount> {
        let (file_count, link_count, folder_count): (i64, i64, i64) =
            sqlx::query_as(r#"SELECT * FROM count_folder_children($1) AS "counts""#)
                .bind(folder_id)
                .fetch_one(db)
                .await?;

        Ok(FolderChildrenCount {
            file_count,
            link_count,
            folder_count,
        })
    }

    /// Collects the IDs and names of all parent folders of the
    /// provided folder
    pub async fn resolve_path(
        db: impl DbExecutor<'_>,
        folder_id: FolderId,
    ) -> DbResult<Vec<FolderPathSegment>> {
        sqlx::query_as(r#"SELECT "id", "name" FROM resolve_folder_path($1)"#)
            .bind(folder_id)
            .fetch_all(db)
            .await
    }

    pub async fn move_to_folder(
        mut self,
        db: impl DbExecutor<'_>,
        folder_id: FolderId,
    ) -> DbResult<Folder> {
        // Should never try moving a root folder
        debug_assert!(self.folder_id.is_some());

        sqlx::query(r#"UPDATE "docbox_folders" SET "folder_id" = $1 WHERE "id" = $2"#)
            .bind(folder_id)
            .bind(self.id)
            .execute(db)
            .await?;

        self.folder_id = Some(folder_id);

        Ok(self)
    }

    pub async fn rename(mut self, db: impl DbExecutor<'_>, name: String) -> DbResult<Folder> {
        sqlx::query(r#"UPDATE "docbox_folders" SET "name" = $1 WHERE "id" = $2"#)
            .bind(name.as_str())
            .bind(self.id)
            .execute(db)
            .await?;

        self.name = name;

        Ok(self)
    }

    pub async fn set_pinned(mut self, db: impl DbExecutor<'_>, pinned: bool) -> DbResult<Folder> {
        sqlx::query(r#"UPDATE "docbox_folders" SET "pinned" = $1 WHERE "id" = $2"#)
            .bind(pinned)
            .bind(self.id)
            .execute(db)
            .await?;

        self.pinned = pinned;

        Ok(self)
    }

    pub async fn find_by_id(
        db: impl DbExecutor<'_>,
        scope: &DocumentBoxScopeRaw,
        id: FolderId,
    ) -> DbResult<Option<Folder>> {
        sqlx::query_as(r#"SELECT * FROM "docbox_folders" WHERE "id" = $1 AND "document_box" = $2"#)
            .bind(id)
            .bind(scope)
            .fetch_optional(db)
            .await
    }

    /// Get all folders and sub folder across any scope in a paginated fashion
    /// (Ignores roots of document boxes)
    pub async fn all_non_root(
        db: impl DbExecutor<'_>,
        offset: u64,
        page_size: u64,
    ) -> DbResult<Vec<Folder>> {
        sqlx::query_as(
            r#"
            SELECT * FROM "docbox_folders"
            WHERE "folder_id" IS NOT NULL
            ORDER BY "created_at" ASC
            OFFSET $1
            LIMIT $2
        "#,
        )
        .bind(offset as i64)
        .bind(page_size as i64)
        .fetch_all(db)
        .await
    }

    pub async fn find_by_parent(
        db: impl DbExecutor<'_>,
        parent_id: FolderId,
    ) -> DbResult<Vec<Folder>> {
        sqlx::query_as(r#"SELECT * FROM "docbox_folders" WHERE "folder_id" = $1"#)
            .bind(parent_id)
            .fetch_all(db)
            .await
    }

    pub async fn find_root(
        db: impl DbExecutor<'_>,
        document_box: &DocumentBoxScopeRaw,
    ) -> DbResult<Option<Folder>> {
        sqlx::query_as(
            r#"SELECT * FROM "docbox_folders" WHERE "document_box" = $1 AND "folder_id" IS NULL"#,
        )
        .bind(document_box)
        .fetch_optional(db)
        .await
    }

    /// Deletes the folder
    pub async fn delete(&self, db: impl DbExecutor<'_>) -> DbResult<PgQueryResult> {
        sqlx::query(r#"DELETE FROM "docbox_folders" WHERE "id" = $1"#)
            .bind(self.id)
            .execute(db)
            .await
    }

    /// Finds a collection of folders that are in various document box scopes, resolves
    /// both the folders themselves and the folder path to traverse to get to each folder
    pub async fn resolve_with_extra_mixed_scopes(
        db: impl DbExecutor<'_>,
        folders_scope_with_id: Vec<DocboxInputPair<'_>>,
    ) -> DbResult<Vec<WithFullPath<FolderWithExtra>>> {
        if folders_scope_with_id.is_empty() {
            return Ok(Vec::new());
        }

        sqlx::query_as(r#"SELECT * FROM resolve_folders_with_extra_mixed_scopes($1)"#)
            .bind(folders_scope_with_id)
            .fetch_all(db)
            .await
    }

    /// Finds a collection of folders that are all within the same document box, resolves
    /// both the folders themselves and the folder path to traverse to get to each folder
    pub async fn resolve_with_extra(
        db: impl DbExecutor<'_>,
        scope: &DocumentBoxScopeRaw,
        folder_ids: Vec<Uuid>,
    ) -> DbResult<Vec<WithFullPath<FolderWithExtra>>> {
        if folder_ids.is_empty() {
            return Ok(Vec::new());
        }

        sqlx::query_as(r#"SELECT * FROM resolve_folders_with_extra($1, $2)"#)
            .bind(scope)
            .bind(folder_ids)
            .fetch_all(db)
            .await
    }

    pub async fn find_by_id_with_extra(
        db: impl DbExecutor<'_>,
        scope: &DocumentBoxScopeRaw,
        id: FolderId,
    ) -> DbResult<Option<WithFullPath<FolderWithExtra>>> {
        sqlx::query_as(r#"SELECT * FROM resolve_folder_by_id_with_extra($1, $2)"#)
            .bind(scope)
            .bind(id)
            .fetch_optional(db)
            .await
    }

    pub async fn find_by_parent_with_extra(
        db: impl DbExecutor<'_>,
        parent_id: FolderId,
    ) -> DbResult<Vec<FolderWithExtra>> {
        sqlx::query_as(r#"SELECT * FROM resolve_folder_by_parent_with_extra($1)"#)
            .bind(parent_id)
            .fetch_all(db)
            .await
    }

    pub async fn find_root_with_extra(
        db: impl DbExecutor<'_>,
        document_box: &DocumentBoxScopeRaw,
    ) -> DbResult<Option<WithFullPath<FolderWithExtra>>> {
        sqlx::query_as(r#"SELECT * FROM resolve_root_folder_with_extra($1)"#)
            .bind(document_box)
            .fetch_optional(db)
            .await
    }

    /// Get the total number of folders in the tenant
    pub async fn total_count(db: impl DbExecutor<'_>) -> DbResult<i64> {
        let count_result: CountResult =
            sqlx::query_as(r#"SELECT COUNT(*) AS "count" FROM "docbox_folders""#)
                .fetch_one(db)
                .await?;

        Ok(count_result.count)
    }
}