@dimah-s3/db 2.0.0-beta.8 → 2.0.0-beta.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli.js +11 -2
- package/dist/cli.js.map +1 -1
- package/dist/client/db-object-source.d.ts +7 -4
- package/dist/client/db-object-source.d.ts.map +1 -1
- package/dist/client/index.d.ts +1 -1
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/index.js +53 -11
- package/dist/client/index.js.map +1 -1
- package/dist/fuma-db.d.ts +4 -2
- package/dist/fuma-db.d.ts.map +1 -1
- package/dist/index.js +228 -45
- package/dist/index.js.map +1 -1
- package/dist/plugin/create-database-endpoints.d.ts +7 -3
- package/dist/plugin/create-database-endpoints.d.ts.map +1 -1
- package/dist/plugin/object-access.d.ts +1 -1
- package/dist/plugin/object-access.d.ts.map +1 -1
- package/dist/plugin/public-stores.d.ts +1 -1
- package/dist/plugin/public-stores.d.ts.map +1 -1
- package/dist/plugin/schemas.d.ts +6 -1
- package/dist/plugin/schemas.d.ts.map +1 -1
- package/dist/schema/schema.d.ts +15 -2
- package/dist/schema/schema.d.ts.map +1 -1
- package/dist/store/create-db-store.d.ts.map +1 -1
- package/dist/store/folder-store.d.ts +31 -0
- package/dist/store/folder-store.d.ts.map +1 -1
- package/dist/store/list-cursor.d.ts +2 -0
- package/dist/store/list-cursor.d.ts.map +1 -1
- package/dist/store/object-store.d.ts +15 -5
- package/dist/store/object-store.d.ts.map +1 -1
- package/dist/store/search-name.d.ts +7 -0
- package/dist/store/search-name.d.ts.map +1 -0
- package/dist/types/client.d.ts +51 -5
- package/dist/types/client.d.ts.map +1 -1
- package/dist/types/plugin.d.ts +5 -0
- package/dist/types/plugin.d.ts.map +1 -1
- package/package.json +10 -10
package/dist/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { fumadb, UniqueConstraintError } from 'fumadb';
|
|
2
2
|
import { table, column, idColumn, schema } from 'fumadb/schema';
|
|
3
3
|
import { definePlugin, isFeatureOn, createS3Endpoint, isAwsNotFound, readUploadMarker, deleteObjectIfMatch } from '@dimah-s3/server/plugins';
|
|
4
|
-
import { s3Error, pluginPath, base64UrlDecode, base64UrlEncode } from '@dimah-s3/core';
|
|
4
|
+
import { s3Error, pluginPath, normalizeSearchText, base64UrlDecode, base64UrlEncode, matchesSearchText } from '@dimah-s3/core';
|
|
5
5
|
import { optionalContentTypeSchema, optionalTrimmedString, routeNameSchema, objectKeySchema, trimmedString, fileNameSchema } from '@dimah-s3/core/schema';
|
|
6
6
|
import * as z from 'zod';
|
|
7
7
|
import { AbortMultipartUploadCommand, HeadObjectCommand } from '@aws-sdk/client-s3';
|
|
@@ -9,6 +9,7 @@ import { AbortMultipartUploadCommand, HeadObjectCommand } from '@aws-sdk/client-
|
|
|
9
9
|
// src/fuma-db.ts
|
|
10
10
|
var BINARY = { mysql: "utf8mb4_bin" };
|
|
11
11
|
var TEXT = { mysql: "utf8mb4_unicode_ci" };
|
|
12
|
+
var SEARCH_NAME_MAX_LENGTH = 1024;
|
|
12
13
|
var LONG_PREFIX = 512;
|
|
13
14
|
var SCOPE_MAX_LENGTH = 255;
|
|
14
15
|
function string(name, length = 255) {
|
|
@@ -25,6 +26,12 @@ var storageObject = table("storage_object", {
|
|
|
25
26
|
/** Folder that holds the object. `null` is the root. */
|
|
26
27
|
folderId: string("folder_id").nullable(),
|
|
27
28
|
filename: column("filename", "varchar(255)").collate(TEXT),
|
|
29
|
+
/**
|
|
30
|
+
* `filename` folded for search: lowercase, without accents, with `%`,
|
|
31
|
+
* `_`, and `\` kept literal. Search matches it on every database the
|
|
32
|
+
* same way.
|
|
33
|
+
*/
|
|
34
|
+
searchName: string("search_name", SEARCH_NAME_MAX_LENGTH),
|
|
28
35
|
contentType: string("content_type").nullable(),
|
|
29
36
|
/** Verified size from HeadObject, set on confirm. */
|
|
30
37
|
size: column("size", "bigint").nullable(),
|
|
@@ -62,6 +69,8 @@ var storageFolder = table("storage_folder", {
|
|
|
62
69
|
parentId: string("parent_id").defaultTo(""),
|
|
63
70
|
/** Last segment of `path`. */
|
|
64
71
|
name: string("name"),
|
|
72
|
+
/** `name` folded for search, like `storage_object.search_name`. */
|
|
73
|
+
searchName: string("search_name", SEARCH_NAME_MAX_LENGTH),
|
|
65
74
|
/** Full folder path. */
|
|
66
75
|
path: string("path", 1024),
|
|
67
76
|
createdAt: column("created_at", "timestamp").defaultTo$("now"),
|
|
@@ -70,7 +79,7 @@ var storageFolder = table("storage_folder", {
|
|
|
70
79
|
length: { path: LONG_PREFIX }
|
|
71
80
|
}).unique("storage_folder_scope_parent_name_uk", ["scope", "parentId", "name"]);
|
|
72
81
|
var dbSchema = schema({
|
|
73
|
-
version: "2.
|
|
82
|
+
version: "2.2.0",
|
|
74
83
|
tables: {
|
|
75
84
|
storageObject,
|
|
76
85
|
storageFolder
|
|
@@ -110,6 +119,10 @@ function childFolderPath(parent, name) {
|
|
|
110
119
|
function joinFolderPath(parent, name) {
|
|
111
120
|
return parent === null ? name : `${parent}/${name}`;
|
|
112
121
|
}
|
|
122
|
+
function folderAncestors(path) {
|
|
123
|
+
const segments = path.split("/");
|
|
124
|
+
return segments.map((_, index) => segments.slice(0, index + 1).join("/"));
|
|
125
|
+
}
|
|
113
126
|
var DEFAULT_LIST_SORT = {
|
|
114
127
|
field: "createdAt",
|
|
115
128
|
direction: "desc"
|
|
@@ -119,6 +132,7 @@ function fingerprint(filters) {
|
|
|
119
132
|
const text = JSON.stringify([
|
|
120
133
|
filters.folderId === void 0,
|
|
121
134
|
filters.folderId ?? null,
|
|
135
|
+
filters.recursive,
|
|
122
136
|
filters.search ?? null,
|
|
123
137
|
filters.status,
|
|
124
138
|
filters.route ?? null,
|
|
@@ -200,6 +214,19 @@ function mapStorageObjectRow(row) {
|
|
|
200
214
|
deletedAt: row.deletedAt
|
|
201
215
|
};
|
|
202
216
|
}
|
|
217
|
+
var LIKE_LITERALS = {
|
|
218
|
+
"%": "\uE000",
|
|
219
|
+
_: "\uE001",
|
|
220
|
+
"\\": "\uE002"
|
|
221
|
+
};
|
|
222
|
+
function toSearchName(value) {
|
|
223
|
+
const folded = normalizeSearchText(value).replaceAll(
|
|
224
|
+
/[%_\\]/g,
|
|
225
|
+
(char) => LIKE_LITERALS[char] ?? char
|
|
226
|
+
);
|
|
227
|
+
const characters = Array.from(folded);
|
|
228
|
+
return characters.length > SEARCH_NAME_MAX_LENGTH ? characters.slice(0, SEARCH_NAME_MAX_LENGTH).join("") : folded;
|
|
229
|
+
}
|
|
203
230
|
|
|
204
231
|
// src/store/object-store.ts
|
|
205
232
|
var DEFAULT_LIST_LIMIT = 50;
|
|
@@ -256,15 +283,17 @@ function claimDeadline() {
|
|
|
256
283
|
);
|
|
257
284
|
return new Date(lastClaimDeadline);
|
|
258
285
|
}
|
|
259
|
-
function filterConditions(b, filter, now) {
|
|
286
|
+
function filterConditions(b, filter, folders, now) {
|
|
260
287
|
const status = filter.status ?? "active";
|
|
261
288
|
const parts = [
|
|
262
289
|
b("scope", "=", filter.scope),
|
|
263
290
|
status === "pending" ? b.and(b("status", "=", "pending"), b("expiresAt", ">=", now)) : b("status", "=", status)
|
|
264
291
|
];
|
|
265
|
-
if (
|
|
266
|
-
else if (
|
|
267
|
-
parts.push(
|
|
292
|
+
if (folders === null) parts.push(b.isNull("folderId"));
|
|
293
|
+
else if (folders !== void 0) {
|
|
294
|
+
parts.push(
|
|
295
|
+
folders.length === 1 ? b("folderId", "=", folders[0] ?? "") : b("folderId", "in", [...folders])
|
|
296
|
+
);
|
|
268
297
|
}
|
|
269
298
|
if (filter.route) parts.push(b("route", "=", filter.route));
|
|
270
299
|
if (filter.contentType?.endsWith("/*")) {
|
|
@@ -273,7 +302,8 @@ function filterConditions(b, filter, now) {
|
|
|
273
302
|
} else if (filter.contentType) {
|
|
274
303
|
parts.push(b("contentType", "=", filter.contentType));
|
|
275
304
|
}
|
|
276
|
-
|
|
305
|
+
const needle = filter.search ? toSearchName(filter.search) : "";
|
|
306
|
+
if (needle) parts.push(b("searchName", "contains", needle));
|
|
277
307
|
return parts;
|
|
278
308
|
}
|
|
279
309
|
function toByteCount(value) {
|
|
@@ -282,8 +312,14 @@ function toByteCount(value) {
|
|
|
282
312
|
function toColumnValue(value) {
|
|
283
313
|
return typeof value === "number" ? BigInt(value) : value;
|
|
284
314
|
}
|
|
285
|
-
function createObjectStore(getOrm) {
|
|
315
|
+
function createObjectStore(getOrm, subtreeIds) {
|
|
286
316
|
const map = mapStorageObjectRow;
|
|
317
|
+
const folderSet = async (filter) => {
|
|
318
|
+
const { folderId } = filter;
|
|
319
|
+
if (folderId == null) return filter.recursive ? void 0 : folderId;
|
|
320
|
+
if (!filter.recursive) return [folderId];
|
|
321
|
+
return [folderId, ...await subtreeIds(filter.scope, folderId)];
|
|
322
|
+
};
|
|
287
323
|
const find = async (ref) => {
|
|
288
324
|
const orm = await getOrm();
|
|
289
325
|
const row = await orm.findFirst("storageObject", {
|
|
@@ -310,6 +346,7 @@ function createObjectStore(getOrm) {
|
|
|
310
346
|
const nullable = field === "size";
|
|
311
347
|
const filters = {
|
|
312
348
|
folderId: input.folderId,
|
|
349
|
+
recursive: input.recursive === true && input.folderId !== void 0,
|
|
313
350
|
search: input.search || void 0,
|
|
314
351
|
status: input.status ?? "active",
|
|
315
352
|
route: input.route || void 0,
|
|
@@ -317,8 +354,9 @@ function createObjectStore(getOrm) {
|
|
|
317
354
|
sort
|
|
318
355
|
};
|
|
319
356
|
const cursor = input.cursor ? decodeListCursor(input.cursor, filters) : null;
|
|
357
|
+
const folders = await folderSet(input);
|
|
320
358
|
const where = (segment) => (b) => {
|
|
321
|
-
const parts = filterConditions(b, input, now);
|
|
359
|
+
const parts = filterConditions(b, input, folders, now);
|
|
322
360
|
const position = segment.after;
|
|
323
361
|
if (segment.nulls) {
|
|
324
362
|
parts.push(b.isNull(field));
|
|
@@ -371,8 +409,9 @@ function createObjectStore(getOrm) {
|
|
|
371
409
|
async count(filter) {
|
|
372
410
|
const orm = await getOrm();
|
|
373
411
|
const now = /* @__PURE__ */ new Date();
|
|
412
|
+
const folders = await folderSet(filter);
|
|
374
413
|
return orm.count("storageObject", {
|
|
375
|
-
where: (b) => b.and(...filterConditions(b, filter, now))
|
|
414
|
+
where: (b) => b.and(...filterConditions(b, filter, folders, now))
|
|
376
415
|
});
|
|
377
416
|
},
|
|
378
417
|
async getUsage(scope, options = {}) {
|
|
@@ -426,6 +465,7 @@ function createObjectStore(getOrm) {
|
|
|
426
465
|
route: input.route,
|
|
427
466
|
folderId: input.folderId,
|
|
428
467
|
filename: input.filename,
|
|
468
|
+
searchName: toSearchName(input.filename),
|
|
429
469
|
contentType: input.contentType ?? null,
|
|
430
470
|
size: null,
|
|
431
471
|
eTag: null,
|
|
@@ -483,7 +523,10 @@ function createObjectStore(getOrm) {
|
|
|
483
523
|
...input.contentType !== void 0 && {
|
|
484
524
|
contentType: input.contentType
|
|
485
525
|
},
|
|
486
|
-
...input.filename !== void 0 && {
|
|
526
|
+
...input.filename !== void 0 && {
|
|
527
|
+
filename: input.filename,
|
|
528
|
+
searchName: toSearchName(input.filename)
|
|
529
|
+
},
|
|
487
530
|
...input.s3Metadata !== void 0 && {
|
|
488
531
|
s3Metadata: input.s3Metadata
|
|
489
532
|
},
|
|
@@ -510,7 +553,10 @@ function createObjectStore(getOrm) {
|
|
|
510
553
|
ref.scope === void 0 || b("scope", "=", ref.scope)
|
|
511
554
|
),
|
|
512
555
|
set: {
|
|
513
|
-
...input.filename !== void 0 && {
|
|
556
|
+
...input.filename !== void 0 && {
|
|
557
|
+
filename: input.filename,
|
|
558
|
+
searchName: toSearchName(input.filename)
|
|
559
|
+
},
|
|
514
560
|
...input.folderId !== void 0 && { folderId: input.folderId },
|
|
515
561
|
...input.attributes !== void 0 && {
|
|
516
562
|
attributes: input.attributes
|
|
@@ -620,12 +666,28 @@ function createObjectStore(getOrm) {
|
|
|
620
666
|
|
|
621
667
|
// src/store/folder-store.ts
|
|
622
668
|
var DEFAULT_FOLDER_CHILDREN_LIMIT = 1e3;
|
|
669
|
+
var DEFAULT_FOLDER_DESCENDANTS_LIMIT = 1e3;
|
|
670
|
+
var FOLDER_SUBTREE_LIMIT = 1e4;
|
|
671
|
+
var IN_BATCH = 500;
|
|
623
672
|
function storedParent(parentId) {
|
|
624
673
|
return parentId ?? "";
|
|
625
674
|
}
|
|
626
675
|
function readParent(parentId) {
|
|
627
676
|
return parentId === "" ? null : parentId;
|
|
628
677
|
}
|
|
678
|
+
function batches(values, size) {
|
|
679
|
+
const runs = [];
|
|
680
|
+
for (let index = 0; index < values.length; index += size) {
|
|
681
|
+
runs.push(values.slice(index, index + size));
|
|
682
|
+
}
|
|
683
|
+
return runs;
|
|
684
|
+
}
|
|
685
|
+
function byNameThenPath(a, b) {
|
|
686
|
+
return a.name.localeCompare(b.name, void 0, {
|
|
687
|
+
numeric: true,
|
|
688
|
+
sensitivity: "base"
|
|
689
|
+
}) || (a.path < b.path ? -1 : a.path > b.path ? 1 : 0);
|
|
690
|
+
}
|
|
629
691
|
function mapFolder(row) {
|
|
630
692
|
return {
|
|
631
693
|
id: row.id,
|
|
@@ -652,6 +714,7 @@ function createFolderStore(getOrm) {
|
|
|
652
714
|
scope: folder.scope,
|
|
653
715
|
parentId: storedParent(folder.parentId),
|
|
654
716
|
name: folder.name,
|
|
717
|
+
searchName: toSearchName(folder.name),
|
|
655
718
|
path: folder.path,
|
|
656
719
|
createdAt: folder.createdAt ?? now,
|
|
657
720
|
updatedAt: folder.updatedAt ?? now
|
|
@@ -691,13 +754,41 @@ function createFolderStore(getOrm) {
|
|
|
691
754
|
]);
|
|
692
755
|
return folders + objects;
|
|
693
756
|
};
|
|
694
|
-
const
|
|
757
|
+
const subtree = async (scope, id, limit = Infinity) => {
|
|
695
758
|
const orm = await getOrm();
|
|
696
|
-
const
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
759
|
+
const found = [];
|
|
760
|
+
const seen = /* @__PURE__ */ new Set([id]);
|
|
761
|
+
let level = [id];
|
|
762
|
+
for (let depth = 0; depth < FOLDER_MAX_DEPTH && level.length > 0; depth++) {
|
|
763
|
+
const next = [];
|
|
764
|
+
for (const parents of batches(level, IN_BATCH)) {
|
|
765
|
+
const rows = await orm.findMany("storageFolder", {
|
|
766
|
+
where: (b) => b.and(b("scope", "=", scope), b("parentId", "in", parents)),
|
|
767
|
+
orderBy: [["id", "asc"]]
|
|
768
|
+
});
|
|
769
|
+
for (const row of rows) {
|
|
770
|
+
if (seen.has(row.id)) continue;
|
|
771
|
+
seen.add(row.id);
|
|
772
|
+
found.push(mapFolder(row));
|
|
773
|
+
next.push(row.id);
|
|
774
|
+
if (found.length >= limit) return found;
|
|
775
|
+
}
|
|
776
|
+
}
|
|
777
|
+
level = next;
|
|
778
|
+
}
|
|
779
|
+
return found;
|
|
780
|
+
};
|
|
781
|
+
const walks = /* @__PURE__ */ new Map();
|
|
782
|
+
const sharedSubtree = (scope, id) => {
|
|
783
|
+
const key = JSON.stringify([scope, id]);
|
|
784
|
+
let walk = walks.get(key);
|
|
785
|
+
if (!walk) {
|
|
786
|
+
walk = subtree(scope, id, FOLDER_SUBTREE_LIMIT).finally(() => {
|
|
787
|
+
walks.delete(key);
|
|
788
|
+
});
|
|
789
|
+
walks.set(key, walk);
|
|
790
|
+
}
|
|
791
|
+
return walk;
|
|
701
792
|
};
|
|
702
793
|
const retain = async (folder) => {
|
|
703
794
|
if (await find(folder.scope, folder.id)) return;
|
|
@@ -713,6 +804,10 @@ function createFolderStore(getOrm) {
|
|
|
713
804
|
};
|
|
714
805
|
return {
|
|
715
806
|
find,
|
|
807
|
+
async subtreeIds(scope, id) {
|
|
808
|
+
const below = await sharedSubtree(scope, id);
|
|
809
|
+
return below.map((folder) => folder.id);
|
|
810
|
+
},
|
|
716
811
|
async children(scope, parentId, options = {}) {
|
|
717
812
|
const orm = await getOrm();
|
|
718
813
|
const rows = await orm.findMany("storageFolder", {
|
|
@@ -741,6 +836,59 @@ function createFolderStore(getOrm) {
|
|
|
741
836
|
}
|
|
742
837
|
return chain.reverse();
|
|
743
838
|
},
|
|
839
|
+
async descendants(scope, parentId, options = {}) {
|
|
840
|
+
const limit = options.limit ?? DEFAULT_FOLDER_DESCENDANTS_LIMIT;
|
|
841
|
+
const needle = options.search ? toSearchName(options.search) : "";
|
|
842
|
+
let folders;
|
|
843
|
+
if (parentId === null) {
|
|
844
|
+
const orm = await getOrm();
|
|
845
|
+
const rows = await orm.findMany("storageFolder", {
|
|
846
|
+
where: (b) => b.and(
|
|
847
|
+
b("scope", "=", scope),
|
|
848
|
+
needle === "" || b("searchName", "contains", needle)
|
|
849
|
+
),
|
|
850
|
+
orderBy: [["path", "asc"]],
|
|
851
|
+
limit: FOLDER_SUBTREE_LIMIT
|
|
852
|
+
});
|
|
853
|
+
folders = rows.map(mapFolder);
|
|
854
|
+
} else {
|
|
855
|
+
const [parent, below] = await Promise.all([
|
|
856
|
+
find(scope, parentId),
|
|
857
|
+
sharedSubtree(scope, parentId)
|
|
858
|
+
]);
|
|
859
|
+
if (!parent) throw s3Error("FOLDER_NOT_FOUND");
|
|
860
|
+
folders = below.filter(
|
|
861
|
+
(folder) => needle === "" || toSearchName(folder.name).includes(needle)
|
|
862
|
+
);
|
|
863
|
+
}
|
|
864
|
+
return folders.sort(byNameThenPath).slice(0, limit);
|
|
865
|
+
},
|
|
866
|
+
async ancestors(scope, ids) {
|
|
867
|
+
if (ids.length === 0) return [];
|
|
868
|
+
const orm = await getOrm();
|
|
869
|
+
const byPath = /* @__PURE__ */ new Map();
|
|
870
|
+
for (const run of batches([...new Set(ids)], IN_BATCH)) {
|
|
871
|
+
const rows = await orm.findMany("storageFolder", {
|
|
872
|
+
where: (b) => b.and(b("scope", "=", scope), b("id", "in", run))
|
|
873
|
+
});
|
|
874
|
+
for (const row of rows) byPath.set(row.path, mapFolder(row));
|
|
875
|
+
}
|
|
876
|
+
const missing = /* @__PURE__ */ new Set();
|
|
877
|
+
for (const path of byPath.keys()) {
|
|
878
|
+
for (const above of folderAncestors(path)) {
|
|
879
|
+
if (!byPath.has(above)) missing.add(above);
|
|
880
|
+
}
|
|
881
|
+
}
|
|
882
|
+
for (const run of batches([...missing], IN_BATCH)) {
|
|
883
|
+
const rows = await orm.findMany("storageFolder", {
|
|
884
|
+
where: (b) => b.and(b("scope", "=", scope), b("path", "in", run))
|
|
885
|
+
});
|
|
886
|
+
for (const row of rows) byPath.set(row.path, mapFolder(row));
|
|
887
|
+
}
|
|
888
|
+
return [...byPath.values()].sort(
|
|
889
|
+
(a, b) => a.path < b.path ? -1 : a.path > b.path ? 1 : 0
|
|
890
|
+
);
|
|
891
|
+
},
|
|
744
892
|
async create(scope, input) {
|
|
745
893
|
const parent = await destination(scope, input.parentId);
|
|
746
894
|
const path = childFolderPath(parent?.path ?? null, input.name);
|
|
@@ -771,7 +919,7 @@ function createFolderStore(getOrm) {
|
|
|
771
919
|
const path = childFolderPath(parent?.path ?? null, name);
|
|
772
920
|
if (path === null) throw s3Error("FOLDER_INVALID");
|
|
773
921
|
const storedName = path.slice(path.lastIndexOf("/") + 1);
|
|
774
|
-
const below = await
|
|
922
|
+
const below = await subtree(scope, id);
|
|
775
923
|
const prefix = folder.path;
|
|
776
924
|
const rewritten = below.map((child) => ({
|
|
777
925
|
id: child.id,
|
|
@@ -788,6 +936,7 @@ function createFolderStore(getOrm) {
|
|
|
788
936
|
where: (b) => b.and(b("scope", "=", scope), b("id", "=", id)),
|
|
789
937
|
set: {
|
|
790
938
|
name: storedName,
|
|
939
|
+
searchName: toSearchName(storedName),
|
|
791
940
|
parentId: storedParent(parentId),
|
|
792
941
|
path,
|
|
793
942
|
updatedAt: now
|
|
@@ -875,9 +1024,10 @@ function createDbStore(client) {
|
|
|
875
1024
|
await checkSchema();
|
|
876
1025
|
return client.orm(DB_SCHEMA_VERSION);
|
|
877
1026
|
};
|
|
1027
|
+
const folders = createFolderStore(getOrm);
|
|
878
1028
|
return {
|
|
879
|
-
objects: createObjectStore(getOrm),
|
|
880
|
-
folders
|
|
1029
|
+
objects: createObjectStore(getOrm, folders.subtreeIds),
|
|
1030
|
+
folders,
|
|
881
1031
|
checkSchema
|
|
882
1032
|
};
|
|
883
1033
|
}
|
|
@@ -907,6 +1057,14 @@ function queryInt(schema2) {
|
|
|
907
1057
|
}
|
|
908
1058
|
);
|
|
909
1059
|
}
|
|
1060
|
+
var queryBoolean = z.codec(
|
|
1061
|
+
z.union([z.enum(["true", "false"]), z.boolean()]),
|
|
1062
|
+
z.boolean(),
|
|
1063
|
+
{
|
|
1064
|
+
decode: (value) => value === true || value === "true",
|
|
1065
|
+
encode: (value) => value
|
|
1066
|
+
}
|
|
1067
|
+
);
|
|
910
1068
|
var dbListSortSchema = z.object({
|
|
911
1069
|
field: z.enum(["createdAt", "updatedAt", "size", "filename"]),
|
|
912
1070
|
direction: z.enum(["asc", "desc"])
|
|
@@ -934,11 +1092,12 @@ var dbListSortQuerySchema = z.codec(
|
|
|
934
1092
|
);
|
|
935
1093
|
var dbListQuerySchema = z.strictObject({
|
|
936
1094
|
folderId: z.string().optional(),
|
|
1095
|
+
recursive: queryBoolean.optional(),
|
|
937
1096
|
route: routeNameSchema.optional(),
|
|
938
1097
|
// Rows that purge has claimed stay internal, as on `GET /db/object`.
|
|
939
1098
|
status: z.enum(["pending", "active", "deleted"]).optional(),
|
|
940
1099
|
contentType: optionalContentTypeSchema,
|
|
941
|
-
/** Text the filename contains. */
|
|
1100
|
+
/** Text the filename contains, ignoring case and accents. */
|
|
942
1101
|
search: optionalTrimmedString.pipe(
|
|
943
1102
|
z.string().max(MAX_SEARCH_LENGTH).optional()
|
|
944
1103
|
),
|
|
@@ -1035,8 +1194,7 @@ function resolveBucket(config, bucket, field) {
|
|
|
1035
1194
|
}
|
|
1036
1195
|
function matchingFolders(children, search) {
|
|
1037
1196
|
if (!search) return children;
|
|
1038
|
-
|
|
1039
|
-
return children.filter((child) => child.name.toLowerCase().includes(text));
|
|
1197
|
+
return children.filter((child) => matchesSearchText(child.name, search));
|
|
1040
1198
|
}
|
|
1041
1199
|
function wireFolderId(value) {
|
|
1042
1200
|
if (value === void 0) return void 0;
|
|
@@ -1085,43 +1243,66 @@ function createDatabaseEndpoints(options) {
|
|
|
1085
1243
|
pluginPath("db", "objects"),
|
|
1086
1244
|
{ method: "GET", query: dbListQuerySchema },
|
|
1087
1245
|
async (ctx) => {
|
|
1088
|
-
const {
|
|
1246
|
+
const {
|
|
1247
|
+
folderId: wireFolder,
|
|
1248
|
+
recursive: wantsSubtree,
|
|
1249
|
+
...filters
|
|
1250
|
+
} = ctx.query;
|
|
1089
1251
|
const peeked = await resolveScope(ctx.context, {
|
|
1090
1252
|
operation: "list",
|
|
1091
1253
|
route: filters.route
|
|
1092
1254
|
});
|
|
1093
1255
|
const opened = await openFolder(peeked.scope, wireFolder);
|
|
1256
|
+
const recursive = wantsSubtree === true && opened !== void 0;
|
|
1094
1257
|
const scope = await access.scope(ctx.context, {
|
|
1095
1258
|
operation: "list",
|
|
1096
1259
|
route: filters.route,
|
|
1097
|
-
...opened !== void 0 && folderTarget(opened)
|
|
1098
|
-
|
|
1099
|
-
const page = await objects.list({
|
|
1100
|
-
...filters,
|
|
1101
|
-
scope: scope.scope,
|
|
1102
|
-
folderId: opened === void 0 ? void 0 : opened?.id ?? null
|
|
1260
|
+
...opened !== void 0 && folderTarget(opened),
|
|
1261
|
+
...recursive && { recursive }
|
|
1103
1262
|
});
|
|
1263
|
+
const folderId = opened === void 0 ? void 0 : opened?.id ?? null;
|
|
1264
|
+
const filter = { ...filters, scope: scope.scope, folderId, recursive };
|
|
1265
|
+
const firstPage = () => Promise.all([
|
|
1266
|
+
objects.getUsage(scope.scope),
|
|
1267
|
+
quota.limits(scope),
|
|
1268
|
+
opened === void 0 ? void 0 : recursive ? folders.descendants(scope.scope, folderId ?? null, {
|
|
1269
|
+
search: filters.search
|
|
1270
|
+
}) : folders.children(scope.scope, folderId ?? null).then(
|
|
1271
|
+
(children) => matchingFolders(children, filters.search)
|
|
1272
|
+
),
|
|
1273
|
+
opened === void 0 ? void 0 : opened === null ? [] : folders.trail(scope.scope, opened.id),
|
|
1274
|
+
filters.search ? objects.count(filter) : void 0
|
|
1275
|
+
]);
|
|
1276
|
+
const early = filters.cursor ? null : firstPage();
|
|
1277
|
+
early?.catch(() => void 0);
|
|
1278
|
+
const page = await objects.list(filter);
|
|
1104
1279
|
const response = {
|
|
1105
1280
|
objects: page.objects.map((object2) => toClientObject(object2)),
|
|
1106
1281
|
nextCursor: page.nextCursor
|
|
1107
1282
|
};
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1283
|
+
const located = page.objects.flatMap(
|
|
1284
|
+
(object2) => object2.folderId === null ? [] : [object2.folderId]
|
|
1285
|
+
);
|
|
1286
|
+
if (!page.firstPage) {
|
|
1287
|
+
if (!recursive) return response;
|
|
1288
|
+
const locations2 = await folders.ancestors(scope.scope, located);
|
|
1289
|
+
return { ...response, locations: locations2.map(toDbClientFolder) };
|
|
1290
|
+
}
|
|
1291
|
+
const [usage, limits, listed, trail, total] = await (early ?? firstPage());
|
|
1292
|
+
const locations = recursive ? await folders.ancestors(scope.scope, [
|
|
1293
|
+
...located,
|
|
1294
|
+
...(listed ?? []).flatMap(
|
|
1295
|
+
(folder) => folder.parentId === null ? [] : [folder.parentId]
|
|
1296
|
+
)
|
|
1297
|
+
]) : void 0;
|
|
1115
1298
|
return {
|
|
1116
1299
|
...response,
|
|
1117
1300
|
usage,
|
|
1118
1301
|
limits,
|
|
1119
|
-
...
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
},
|
|
1124
|
-
...trail && { trail: trail.map(toDbClientFolder) }
|
|
1302
|
+
...listed && { folders: listed.map(toDbClientFolder) },
|
|
1303
|
+
...trail && { trail: trail.map(toDbClientFolder) },
|
|
1304
|
+
...locations && { locations: locations.map(toDbClientFolder) },
|
|
1305
|
+
...total !== void 0 && { total }
|
|
1125
1306
|
};
|
|
1126
1307
|
}
|
|
1127
1308
|
),
|
|
@@ -1480,6 +1661,7 @@ function createObjectAccess(options) {
|
|
|
1480
1661
|
...target?.key !== void 0 && { key: target.key },
|
|
1481
1662
|
...target?.folder !== void 0 && { folder: target.folder },
|
|
1482
1663
|
...target?.folderId !== void 0 && { folderId: target.folderId },
|
|
1664
|
+
...target?.recursive === true && { recursive: true },
|
|
1483
1665
|
...target?.clientMetadata !== void 0 && {
|
|
1484
1666
|
clientMetadata: target.clientMetadata
|
|
1485
1667
|
},
|
|
@@ -1521,7 +1703,8 @@ async function targetFolder(folders, scope, folderId) {
|
|
|
1521
1703
|
return folder;
|
|
1522
1704
|
}
|
|
1523
1705
|
function createPublicStores(store) {
|
|
1524
|
-
const { objects
|
|
1706
|
+
const { objects } = store;
|
|
1707
|
+
const { subtreeIds: _subtreeIds, ...folders } = store.folders;
|
|
1525
1708
|
return {
|
|
1526
1709
|
objects: {
|
|
1527
1710
|
find: objects.find,
|