@dimah-s3/db 2.0.0-beta.0 → 2.0.0-beta.2
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/README.md +3 -3
- package/dist/adapters/drizzle.d.ts +2 -1
- package/dist/adapters/drizzle.d.ts.map +1 -1
- package/dist/cli.d.ts +8 -9
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +42 -103
- package/dist/cli.js.map +1 -1
- package/dist/client/db-client.d.ts +1 -1
- package/dist/client/db-client.d.ts.map +1 -1
- package/dist/client/db-object-source.d.ts +5 -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 +27 -12
- package/dist/client/index.js.map +1 -1
- package/dist/drizzle/mysql.d.ts +46 -13
- package/dist/drizzle/mysql.d.ts.map +1 -1
- package/dist/drizzle/mysql.js +15 -18
- package/dist/drizzle/mysql.js.map +1 -1
- package/dist/drizzle/pg.d.ts +44 -13
- package/dist/drizzle/pg.d.ts.map +1 -1
- package/dist/drizzle/pg.js +17 -17
- package/dist/drizzle/pg.js.map +1 -1
- package/dist/drizzle/shared.d.ts +4 -13
- package/dist/drizzle/shared.d.ts.map +1 -1
- package/dist/drizzle/sqlite.d.ts +46 -13
- package/dist/drizzle/sqlite.d.ts.map +1 -1
- package/dist/drizzle/sqlite.js +17 -17
- package/dist/drizzle/sqlite.js.map +1 -1
- package/dist/folder-path.d.ts +9 -2
- package/dist/folder-path.d.ts.map +1 -1
- package/dist/fuma-db.d.ts +10 -30
- package/dist/fuma-db.d.ts.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +372 -231
- package/dist/index.js.map +1 -1
- package/dist/plugin/create-database-endpoints.d.ts +38 -18
- package/dist/plugin/create-database-endpoints.d.ts.map +1 -1
- package/dist/plugin/create-database-lifecycle-hooks.d.ts +1 -1
- package/dist/plugin/create-database-lifecycle-hooks.d.ts.map +1 -1
- package/dist/plugin/db.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 +4 -7
- package/dist/plugin/public-stores.d.ts.map +1 -1
- package/dist/plugin/schemas.d.ts +21 -12
- package/dist/plugin/schemas.d.ts.map +1 -1
- package/dist/plugin/to-db-client-object.d.ts.map +1 -1
- package/dist/schema/{v2.d.ts → schema.d.ts} +23 -13
- package/dist/schema/schema.d.ts.map +1 -0
- package/dist/store/create-db-store.d.ts +2 -2
- package/dist/store/folder-store.d.ts +41 -14
- package/dist/store/folder-store.d.ts.map +1 -1
- package/dist/store/list-cursor.d.ts +1 -1
- package/dist/store/list-cursor.d.ts.map +1 -1
- package/dist/store/map-row.d.ts +2 -2
- package/dist/store/map-row.d.ts.map +1 -1
- package/dist/store/object-store.d.ts +9 -9
- package/dist/store/object-store.d.ts.map +1 -1
- package/dist/store/schema-version.d.ts +1 -1
- package/dist/types/client.d.ts +47 -18
- package/dist/types/client.d.ts.map +1 -1
- package/dist/types/plugin.d.ts +6 -1
- package/dist/types/plugin.d.ts.map +1 -1
- package/dist/types/storage-object.d.ts +8 -7
- package/dist/types/storage-object.d.ts.map +1 -1
- package/package.json +9 -6
- package/prisma/dimah-s3.prisma +14 -13
- package/sql/mysql.sql +12 -11
- package/sql/pg.sql +10 -10
- package/sql/sqlite.sql +10 -9
- package/src/drizzle/mysql.ts +175 -0
- package/src/drizzle/pg.ts +146 -0
- package/src/drizzle/sqlite.ts +150 -0
- package/dist/schema/v1.d.ts +0 -24
- package/dist/schema/v1.d.ts.map +0 -1
- package/dist/schema/v2.d.ts.map +0 -1
- package/sql/upgrade/1.0.0-2.0.0/mysql.sql +0 -71
- package/sql/upgrade/1.0.0-2.0.0/pg.sql +0 -47
- package/sql/upgrade/1.0.0-2.0.0/sqlite.sql +0 -119
package/dist/index.js
CHANGED
|
@@ -7,37 +7,10 @@ import * as z from 'zod';
|
|
|
7
7
|
import { AbortMultipartUploadCommand, HeadObjectCommand } from '@aws-sdk/client-s3';
|
|
8
8
|
|
|
9
9
|
// src/fuma-db.ts
|
|
10
|
-
var storageObject = table("storage_object", {
|
|
11
|
-
id: idColumn("id", "varchar(255)").defaultTo$("auto"),
|
|
12
|
-
scope: column("scope", "string"),
|
|
13
|
-
bucket: column("bucket", "string"),
|
|
14
|
-
key: column("key", "string"),
|
|
15
|
-
route: column("route", "string"),
|
|
16
|
-
contentType: column("content_type", "string").nullable(),
|
|
17
|
-
size: column("size", "bigint").nullable(),
|
|
18
|
-
eTag: column("e_tag", "string").nullable(),
|
|
19
|
-
filename: column("filename", "string").nullable(),
|
|
20
|
-
status: column("status", "string"),
|
|
21
|
-
s3Metadata: column("metadata", "json").nullable(),
|
|
22
|
-
acl: column("acl", "string").nullable(),
|
|
23
|
-
uploadId: column("upload_id", "string").nullable(),
|
|
24
|
-
declaredSize: column("declared_size", "bigint").nullable(),
|
|
25
|
-
confirmedAt: column("confirmed_at", "timestamp").nullable(),
|
|
26
|
-
expiresAt: column("expires_at", "timestamp").nullable(),
|
|
27
|
-
createdAt: column("created_at", "timestamp").defaultTo$("now"),
|
|
28
|
-
updatedAt: column("updated_at", "timestamp").defaultTo$("now"),
|
|
29
|
-
deletedAt: column("deleted_at", "timestamp").nullable()
|
|
30
|
-
}).unique("storage_object_bucket_key_uk", ["bucket", "key"]);
|
|
31
|
-
var v1 = schema({
|
|
32
|
-
version: "1.0.0",
|
|
33
|
-
tables: {
|
|
34
|
-
storageObject
|
|
35
|
-
}
|
|
36
|
-
});
|
|
37
10
|
function byteCount(name) {
|
|
38
11
|
return column(name, "bigint").nullable();
|
|
39
12
|
}
|
|
40
|
-
var
|
|
13
|
+
var storageObject = table("storage_object", {
|
|
41
14
|
id: idColumn("id", "varchar(255)").defaultTo$("auto"),
|
|
42
15
|
/** Owner, for example `user:123` or `org:acme`. */
|
|
43
16
|
scope: column("scope", "string"),
|
|
@@ -45,8 +18,8 @@ var storageObject2 = table("storage_object", {
|
|
|
45
18
|
key: column("key", "string"),
|
|
46
19
|
/** Named dimah-s3 route that created the object. */
|
|
47
20
|
route: column("route", "string"),
|
|
48
|
-
/**
|
|
49
|
-
|
|
21
|
+
/** Folder that holds the object. `null` is the root. */
|
|
22
|
+
folderId: column("folder_id", "string").nullable(),
|
|
50
23
|
filename: column("filename", "string"),
|
|
51
24
|
contentType: column("content_type", "string").nullable(),
|
|
52
25
|
/** Verified size from HeadObject, set on confirm. */
|
|
@@ -74,19 +47,20 @@ var storageObject2 = table("storage_object", {
|
|
|
74
47
|
var storageFolder = table("storage_folder", {
|
|
75
48
|
id: idColumn("id", "varchar(255)").defaultTo$("auto"),
|
|
76
49
|
scope: column("scope", "string"),
|
|
77
|
-
/**
|
|
78
|
-
|
|
79
|
-
/** Path of the containing folder. `null` is the root. */
|
|
80
|
-
parent: column("parent", "string").nullable(),
|
|
50
|
+
/** Id of the containing folder. `""` is the root. */
|
|
51
|
+
parentId: column("parent_id", "string"),
|
|
81
52
|
/** Last segment of `path`. */
|
|
82
53
|
name: column("name", "string"),
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
54
|
+
/** Full folder path. */
|
|
55
|
+
path: column("path", "string"),
|
|
56
|
+
createdAt: column("created_at", "timestamp").defaultTo$("now"),
|
|
57
|
+
updatedAt: column("updated_at", "timestamp").defaultTo$("now")
|
|
58
|
+
}).unique("storage_folder_scope_path_uk", ["scope", "path"]).unique("storage_folder_scope_parent_name_uk", ["scope", "parentId", "name"]);
|
|
59
|
+
var schemaIndexes = [
|
|
86
60
|
{
|
|
87
61
|
name: "storage_object_scope_folder_idx",
|
|
88
62
|
table: "storage_object",
|
|
89
|
-
columns: ["scope", "status", "
|
|
63
|
+
columns: ["scope", "status", "folder_id", "created_at"]
|
|
90
64
|
},
|
|
91
65
|
{
|
|
92
66
|
name: "storage_object_scope_updated_idx",
|
|
@@ -107,51 +81,37 @@ var v2Indexes = [
|
|
|
107
81
|
name: "storage_object_status_expires_idx",
|
|
108
82
|
table: "storage_object",
|
|
109
83
|
columns: ["status", "expires_at"]
|
|
110
|
-
},
|
|
111
|
-
{
|
|
112
|
-
name: "storage_folder_scope_parent_idx",
|
|
113
|
-
table: "storage_folder",
|
|
114
|
-
columns: ["scope", "parent"]
|
|
115
84
|
}
|
|
116
85
|
];
|
|
117
|
-
var v1Indexes = [
|
|
118
|
-
"storage_object_scope_status_created_idx",
|
|
119
|
-
"storage_object_scope_route_idx",
|
|
120
|
-
"storage_object_status_created_idx"
|
|
121
|
-
];
|
|
122
86
|
var custom = (sql) => ({ type: "custom", sql });
|
|
123
|
-
var
|
|
124
|
-
var
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
...v1Indexes.map((name) => custom(`DROP INDEX IF EXISTS "${name}"`)),
|
|
131
|
-
...v2Indexes.map((index) => {
|
|
132
|
-
const columns = index.columns.map((name) => `"${name}"`).join(", ");
|
|
133
|
-
return custom(
|
|
134
|
-
`CREATE INDEX IF NOT EXISTS "${index.name}" ON "${index.table}" (${columns})`
|
|
135
|
-
);
|
|
136
|
-
})
|
|
137
|
-
];
|
|
138
|
-
var v2 = schema({
|
|
139
|
-
version: "2.0.0",
|
|
87
|
+
var PREDECESSOR_VERSION = "0.0.1";
|
|
88
|
+
var predecessor = schema({
|
|
89
|
+
version: PREDECESSOR_VERSION,
|
|
90
|
+
tables: {}
|
|
91
|
+
});
|
|
92
|
+
var dbSchema = schema({
|
|
93
|
+
version: "2.1.0",
|
|
140
94
|
tables: {
|
|
141
|
-
storageObject
|
|
95
|
+
storageObject,
|
|
142
96
|
storageFolder
|
|
143
97
|
},
|
|
144
98
|
async up({ auto }) {
|
|
145
|
-
|
|
99
|
+
const indexes = schemaIndexes.map((index) => {
|
|
100
|
+
const columns = index.columns.map((name) => `"${name}"`).join(", ");
|
|
101
|
+
return custom(
|
|
102
|
+
`CREATE INDEX IF NOT EXISTS "${index.name}" ON "${index.table}" (${columns})`
|
|
103
|
+
);
|
|
104
|
+
});
|
|
105
|
+
return [...await auto(), ...indexes];
|
|
146
106
|
}
|
|
147
107
|
});
|
|
148
108
|
|
|
149
109
|
// src/fuma-db.ts
|
|
150
110
|
var DimahS3DB = fumadb({
|
|
151
111
|
namespace: "dimah_s3",
|
|
152
|
-
schemas: [
|
|
112
|
+
schemas: [predecessor, dbSchema]
|
|
153
113
|
});
|
|
154
|
-
var DB_SCHEMA_VERSION =
|
|
114
|
+
var DB_SCHEMA_VERSION = dbSchema.version;
|
|
155
115
|
var FOLDER_PATH_MAX_LENGTH = 1024;
|
|
156
116
|
var FOLDER_NAME_MAX_LENGTH = 255;
|
|
157
117
|
var FOLDER_MAX_DEPTH = 32;
|
|
@@ -159,9 +119,9 @@ var INVALID_CHARACTER = /[\p{Cc}\p{Cs}]/u;
|
|
|
159
119
|
function characterCount(value) {
|
|
160
120
|
return Array.from(value).length;
|
|
161
121
|
}
|
|
162
|
-
function
|
|
122
|
+
function isFolderName(name) {
|
|
163
123
|
const length = characterCount(name);
|
|
164
|
-
return length >= 1 && length <= FOLDER_NAME_MAX_LENGTH && name !== "." && name !== ".." && !INVALID_CHARACTER.test(name);
|
|
124
|
+
return length >= 1 && length <= FOLDER_NAME_MAX_LENGTH && name !== "." && name !== ".." && !name.includes("/") && !INVALID_CHARACTER.test(name);
|
|
165
125
|
}
|
|
166
126
|
function normalizeFolderPath(path) {
|
|
167
127
|
if (!path.isWellFormed()) return null;
|
|
@@ -169,24 +129,14 @@ function normalizeFolderPath(path) {
|
|
|
169
129
|
if (characterCount(normalized) > FOLDER_PATH_MAX_LENGTH) return null;
|
|
170
130
|
const names = normalized.split("/");
|
|
171
131
|
if (names.length > FOLDER_MAX_DEPTH) return null;
|
|
172
|
-
return names.every(
|
|
173
|
-
}
|
|
174
|
-
function parseFolder(value) {
|
|
175
|
-
if (value === "") return null;
|
|
176
|
-
const path = normalizeFolderPath(value);
|
|
177
|
-
if (path === null) throw s3Error("FOLDER_INVALID");
|
|
178
|
-
return path;
|
|
132
|
+
return names.every(isFolderName) ? normalized : null;
|
|
179
133
|
}
|
|
180
|
-
function
|
|
181
|
-
|
|
182
|
-
return
|
|
134
|
+
function childFolderPath(parent, name) {
|
|
135
|
+
if (!isFolderName(name)) return null;
|
|
136
|
+
return normalizeFolderPath(joinFolderPath(parent, name));
|
|
183
137
|
}
|
|
184
|
-
function
|
|
185
|
-
return
|
|
186
|
-
}
|
|
187
|
-
function folderAncestors(path) {
|
|
188
|
-
const segments = path.split("/");
|
|
189
|
-
return segments.map((_, index) => segments.slice(0, index + 1).join("/"));
|
|
138
|
+
function joinFolderPath(parent, name) {
|
|
139
|
+
return parent === null ? name : `${parent}/${name}`;
|
|
190
140
|
}
|
|
191
141
|
var DEFAULT_LIST_SORT = {
|
|
192
142
|
field: "createdAt",
|
|
@@ -195,8 +145,8 @@ var DEFAULT_LIST_SORT = {
|
|
|
195
145
|
var decoder = new TextDecoder();
|
|
196
146
|
function fingerprint(filters) {
|
|
197
147
|
const text = JSON.stringify([
|
|
198
|
-
filters.
|
|
199
|
-
filters.
|
|
148
|
+
filters.folderId === void 0,
|
|
149
|
+
filters.folderId ?? null,
|
|
200
150
|
filters.search ?? null,
|
|
201
151
|
filters.status,
|
|
202
152
|
filters.route ?? null,
|
|
@@ -259,7 +209,7 @@ function mapStorageObjectRow(row) {
|
|
|
259
209
|
bucket: row.bucket,
|
|
260
210
|
key: row.key,
|
|
261
211
|
route: row.route,
|
|
262
|
-
|
|
212
|
+
folderId: row.folderId,
|
|
263
213
|
filename: row.filename,
|
|
264
214
|
contentType: row.contentType,
|
|
265
215
|
size: toNumber(row.size),
|
|
@@ -340,9 +290,9 @@ function filterConditions(b, filter, now) {
|
|
|
340
290
|
b("scope", "=", filter.scope),
|
|
341
291
|
status === "pending" ? b.and(b("status", "=", "pending"), b("expiresAt", ">=", now)) : b("status", "=", status)
|
|
342
292
|
];
|
|
343
|
-
if (filter.
|
|
344
|
-
else if (filter.
|
|
345
|
-
parts.push(b("
|
|
293
|
+
if (filter.folderId === null) parts.push(b.isNull("folderId"));
|
|
294
|
+
else if (filter.folderId !== void 0) {
|
|
295
|
+
parts.push(b("folderId", "=", filter.folderId));
|
|
346
296
|
}
|
|
347
297
|
if (filter.route) parts.push(b("route", "=", filter.route));
|
|
348
298
|
if (filter.contentType?.endsWith("/*")) {
|
|
@@ -381,7 +331,7 @@ function createObjectStore(getOrm) {
|
|
|
381
331
|
const after = direction === "desc" ? "<" : ">";
|
|
382
332
|
const nullable = field === "size";
|
|
383
333
|
const filters = {
|
|
384
|
-
|
|
334
|
+
folderId: input.folderId,
|
|
385
335
|
search: input.search || void 0,
|
|
386
336
|
status: input.status ?? "active",
|
|
387
337
|
route: input.route || void 0,
|
|
@@ -498,7 +448,7 @@ function createObjectStore(getOrm) {
|
|
|
498
448
|
...ref,
|
|
499
449
|
scope: input.scope,
|
|
500
450
|
route: input.route,
|
|
501
|
-
|
|
451
|
+
folderId: input.folderId,
|
|
502
452
|
filename: input.filename,
|
|
503
453
|
contentType: input.contentType ?? null,
|
|
504
454
|
size: null,
|
|
@@ -585,7 +535,7 @@ function createObjectStore(getOrm) {
|
|
|
585
535
|
),
|
|
586
536
|
set: {
|
|
587
537
|
...input.filename !== void 0 && { filename: input.filename },
|
|
588
|
-
...input.
|
|
538
|
+
...input.folderId !== void 0 && { folderId: input.folderId },
|
|
589
539
|
...input.attributes !== void 0 && {
|
|
590
540
|
attributes: input.attributes
|
|
591
541
|
},
|
|
@@ -694,72 +644,121 @@ function createObjectStore(getOrm) {
|
|
|
694
644
|
|
|
695
645
|
// src/store/folder-store.ts
|
|
696
646
|
var DEFAULT_FOLDER_CHILDREN_LIMIT = 1e3;
|
|
697
|
-
function
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
647
|
+
function isUniqueViolation(error) {
|
|
648
|
+
if (typeof error !== "object" || error === null) return false;
|
|
649
|
+
const code = "code" in error ? String(error.code) : "";
|
|
650
|
+
if (code === "23505" || code === "ER_DUP_ENTRY") return true;
|
|
651
|
+
const message = error instanceof Error ? error.message : "";
|
|
652
|
+
return /unique/i.test(message);
|
|
653
|
+
}
|
|
654
|
+
function storedParent(parentId) {
|
|
655
|
+
return parentId ?? "";
|
|
656
|
+
}
|
|
657
|
+
function readParent(parentId) {
|
|
658
|
+
return parentId === "" ? null : parentId;
|
|
659
|
+
}
|
|
660
|
+
function mapFolder(row) {
|
|
661
|
+
return {
|
|
662
|
+
id: row.id,
|
|
663
|
+
scope: row.scope,
|
|
664
|
+
parentId: readParent(row.parentId),
|
|
665
|
+
name: row.name,
|
|
666
|
+
path: row.path,
|
|
667
|
+
createdAt: row.createdAt,
|
|
668
|
+
updatedAt: row.updatedAt
|
|
669
|
+
};
|
|
705
670
|
}
|
|
706
671
|
function createFolderStore(getOrm) {
|
|
707
|
-
const find = async (scope,
|
|
672
|
+
const find = async (scope, id) => {
|
|
708
673
|
const orm = await getOrm();
|
|
709
|
-
|
|
710
|
-
where: (b) => b.and(b("scope", "=", scope), b("
|
|
674
|
+
const row = await orm.findFirst("storageFolder", {
|
|
675
|
+
where: (b) => b.and(b("scope", "=", scope), b("id", "=", id))
|
|
711
676
|
});
|
|
677
|
+
return row ? mapFolder(row) : null;
|
|
712
678
|
};
|
|
713
|
-
const
|
|
714
|
-
const existing = await find(scope, path);
|
|
715
|
-
if (existing) return existing;
|
|
716
|
-
try {
|
|
717
|
-
return await insertFolder(await getOrm(), scope, path);
|
|
718
|
-
} catch (error) {
|
|
719
|
-
const raced = await find(scope, path);
|
|
720
|
-
if (raced) return raced;
|
|
721
|
-
throw error;
|
|
722
|
-
}
|
|
723
|
-
};
|
|
724
|
-
const ensure = async (scope, path) => {
|
|
725
|
-
if (path === null) return;
|
|
726
|
-
const ancestors = folderAncestors(path);
|
|
679
|
+
const findNamed = async (scope, parentId, name) => {
|
|
727
680
|
const orm = await getOrm();
|
|
728
|
-
const
|
|
729
|
-
(
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
681
|
+
const row = await orm.findFirst("storageFolder", {
|
|
682
|
+
where: (b) => b.and(
|
|
683
|
+
b("scope", "=", scope),
|
|
684
|
+
b("parentId", "=", storedParent(parentId)),
|
|
685
|
+
b("name", "=", name)
|
|
686
|
+
)
|
|
687
|
+
});
|
|
688
|
+
return row ? mapFolder(row) : null;
|
|
689
|
+
};
|
|
690
|
+
const insertFolder = async (orm, folder) => {
|
|
691
|
+
const now = /* @__PURE__ */ new Date();
|
|
692
|
+
const row = await orm.create("storageFolder", {
|
|
693
|
+
...folder.id !== void 0 && { id: folder.id },
|
|
694
|
+
scope: folder.scope,
|
|
695
|
+
parentId: storedParent(folder.parentId),
|
|
696
|
+
name: folder.name,
|
|
697
|
+
path: folder.path,
|
|
698
|
+
createdAt: folder.createdAt ?? now,
|
|
699
|
+
updatedAt: folder.updatedAt ?? now
|
|
700
|
+
});
|
|
701
|
+
return mapFolder(row);
|
|
702
|
+
};
|
|
703
|
+
const destination = async (scope, parentId, folderId) => {
|
|
704
|
+
if (parentId === null) return null;
|
|
705
|
+
if (parentId === folderId) throw s3Error("FOLDER_INVALID");
|
|
706
|
+
const parent = await find(scope, parentId);
|
|
707
|
+
if (!parent) throw s3Error("FOLDER_NOT_FOUND");
|
|
708
|
+
if (folderId !== void 0) {
|
|
709
|
+
let cursor = parent;
|
|
710
|
+
const seen = /* @__PURE__ */ new Set();
|
|
711
|
+
while (cursor && !seen.has(cursor.id)) {
|
|
712
|
+
if (cursor.id === folderId) throw s3Error("FOLDER_INVALID");
|
|
713
|
+
seen.add(cursor.id);
|
|
714
|
+
cursor = cursor.parentId ? await find(scope, cursor.parentId) : null;
|
|
715
|
+
}
|
|
736
716
|
}
|
|
717
|
+
return parent;
|
|
737
718
|
};
|
|
738
|
-
const countContents = async (scope,
|
|
719
|
+
const countContents = async (scope, id) => {
|
|
739
720
|
const orm = await getOrm();
|
|
740
721
|
const now = /* @__PURE__ */ new Date();
|
|
741
722
|
const [folders, objects] = await Promise.all([
|
|
742
723
|
orm.count("storageFolder", {
|
|
743
|
-
where: (b) => b.and(b("scope", "=", scope), b("
|
|
724
|
+
where: (b) => b.and(b("scope", "=", scope), b("parentId", "=", id))
|
|
744
725
|
}),
|
|
745
726
|
orm.count("storageObject", {
|
|
746
727
|
where: (b) => b.and(
|
|
747
728
|
b("scope", "=", scope),
|
|
748
|
-
b("
|
|
729
|
+
b("folderId", "=", id),
|
|
749
730
|
liveObject(b, now)
|
|
750
731
|
)
|
|
751
732
|
})
|
|
752
733
|
]);
|
|
753
734
|
return folders + objects;
|
|
754
735
|
};
|
|
736
|
+
const descendants = async (scope, path) => {
|
|
737
|
+
const orm = await getOrm();
|
|
738
|
+
const rows = await orm.findMany("storageFolder", {
|
|
739
|
+
where: (b) => b.and(b("scope", "=", scope), b("path", "starts with", `${path}/`)),
|
|
740
|
+
orderBy: [["id", "asc"]]
|
|
741
|
+
});
|
|
742
|
+
return rows.map(mapFolder);
|
|
743
|
+
};
|
|
744
|
+
const retain = async (folder) => {
|
|
745
|
+
if (await find(folder.scope, folder.id)) return;
|
|
746
|
+
const orm = await getOrm();
|
|
747
|
+
try {
|
|
748
|
+
await insertFolder(orm, folder);
|
|
749
|
+
} catch (error) {
|
|
750
|
+
if (await find(folder.scope, folder.id)) return;
|
|
751
|
+
throw error;
|
|
752
|
+
}
|
|
753
|
+
};
|
|
755
754
|
return {
|
|
756
755
|
find,
|
|
757
|
-
async children(scope,
|
|
756
|
+
async children(scope, parentId, options = {}) {
|
|
758
757
|
const orm = await getOrm();
|
|
759
|
-
|
|
758
|
+
const rows = await orm.findMany("storageFolder", {
|
|
760
759
|
where: (b) => b.and(
|
|
761
760
|
b("scope", "=", scope),
|
|
762
|
-
|
|
761
|
+
b("parentId", "=", storedParent(parentId))
|
|
763
762
|
),
|
|
764
763
|
orderBy: [
|
|
765
764
|
["name", "asc"],
|
|
@@ -767,33 +766,104 @@ function createFolderStore(getOrm) {
|
|
|
767
766
|
],
|
|
768
767
|
limit: options.limit ?? DEFAULT_FOLDER_CHILDREN_LIMIT
|
|
769
768
|
});
|
|
769
|
+
return rows.map(mapFolder);
|
|
770
|
+
},
|
|
771
|
+
async trail(scope, id) {
|
|
772
|
+
const chain = [];
|
|
773
|
+
const seen = /* @__PURE__ */ new Set();
|
|
774
|
+
let current = await find(scope, id);
|
|
775
|
+
if (!current) throw s3Error("FOLDER_NOT_FOUND");
|
|
776
|
+
while (current && !seen.has(current.id)) {
|
|
777
|
+
seen.add(current.id);
|
|
778
|
+
chain.push(current);
|
|
779
|
+
if (!current.parentId || chain.length > FOLDER_MAX_DEPTH) break;
|
|
780
|
+
current = await find(scope, current.parentId);
|
|
781
|
+
}
|
|
782
|
+
return chain.reverse();
|
|
770
783
|
},
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
784
|
+
async create(scope, input) {
|
|
785
|
+
const parent = await destination(scope, input.parentId);
|
|
786
|
+
const path = childFolderPath(parent?.path ?? null, input.name);
|
|
787
|
+
if (path === null) throw s3Error("FOLDER_INVALID");
|
|
788
|
+
const name = path.slice(path.lastIndexOf("/") + 1);
|
|
789
|
+
const orm = await getOrm();
|
|
775
790
|
try {
|
|
776
|
-
|
|
791
|
+
return await insertFolder(orm, {
|
|
792
|
+
scope,
|
|
793
|
+
parentId: input.parentId,
|
|
794
|
+
name,
|
|
795
|
+
path
|
|
796
|
+
});
|
|
777
797
|
} catch (error) {
|
|
778
|
-
if (await
|
|
798
|
+
if (await findNamed(scope, input.parentId, name)) {
|
|
799
|
+
throw s3Error("FOLDER_EXISTS");
|
|
800
|
+
}
|
|
779
801
|
throw error;
|
|
780
802
|
}
|
|
781
|
-
await ensure(scope, parentFolderPath(path));
|
|
782
|
-
return folder;
|
|
783
803
|
},
|
|
784
|
-
async
|
|
785
|
-
|
|
804
|
+
async update(scope, id, input) {
|
|
805
|
+
const folder = await find(scope, id);
|
|
806
|
+
if (!folder) throw s3Error("FOLDER_NOT_FOUND");
|
|
807
|
+
const name = input.name ?? folder.name;
|
|
808
|
+
const parentId = input.parentId === void 0 ? folder.parentId : input.parentId;
|
|
809
|
+
if (name === folder.name && parentId === folder.parentId) return folder;
|
|
810
|
+
const parent = await destination(scope, parentId, id);
|
|
811
|
+
const path = childFolderPath(parent?.path ?? null, name);
|
|
812
|
+
if (path === null) throw s3Error("FOLDER_INVALID");
|
|
813
|
+
const storedName = path.slice(path.lastIndexOf("/") + 1);
|
|
814
|
+
const below = await descendants(scope, folder.path);
|
|
815
|
+
const prefix = folder.path;
|
|
816
|
+
const rewritten = below.map((child) => ({
|
|
817
|
+
id: child.id,
|
|
818
|
+
path: `${path}${child.path.slice(prefix.length)}`
|
|
819
|
+
}));
|
|
820
|
+
for (const next of [path, ...rewritten.map((child) => child.path)]) {
|
|
821
|
+
if (normalizeFolderPath(next) !== next) throw s3Error("FOLDER_INVALID");
|
|
822
|
+
}
|
|
823
|
+
const orm = await getOrm();
|
|
824
|
+
const now = /* @__PURE__ */ new Date();
|
|
825
|
+
await orm.transaction(async (tx) => {
|
|
826
|
+
try {
|
|
827
|
+
await tx.updateMany("storageFolder", {
|
|
828
|
+
where: (b) => b.and(b("scope", "=", scope), b("id", "=", id)),
|
|
829
|
+
set: {
|
|
830
|
+
name: storedName,
|
|
831
|
+
parentId: storedParent(parentId),
|
|
832
|
+
path,
|
|
833
|
+
updatedAt: now
|
|
834
|
+
}
|
|
835
|
+
});
|
|
836
|
+
} catch (error) {
|
|
837
|
+
if (isUniqueViolation(error)) throw s3Error("FOLDER_EXISTS");
|
|
838
|
+
throw error;
|
|
839
|
+
}
|
|
840
|
+
for (const child of rewritten) {
|
|
841
|
+
await tx.updateMany("storageFolder", {
|
|
842
|
+
where: (b) => b.and(b("scope", "=", scope), b("id", "=", child.id)),
|
|
843
|
+
set: { path: child.path, updatedAt: now }
|
|
844
|
+
});
|
|
845
|
+
}
|
|
846
|
+
});
|
|
847
|
+
const updated = await find(scope, id);
|
|
848
|
+
if (!updated) throw s3Error("FOLDER_NOT_FOUND");
|
|
849
|
+
return updated;
|
|
850
|
+
},
|
|
851
|
+
async delete(scope, id) {
|
|
852
|
+
const folder = await find(scope, id);
|
|
853
|
+
if (!folder) return;
|
|
854
|
+
if (await countContents(scope, id) > 0) {
|
|
786
855
|
throw s3Error("FOLDER_NOT_EMPTY");
|
|
787
856
|
}
|
|
788
857
|
const orm = await getOrm();
|
|
789
858
|
await orm.deleteMany("storageFolder", {
|
|
790
|
-
where: (b) => b.and(b("scope", "=", scope), b("
|
|
859
|
+
where: (b) => b.and(b("scope", "=", scope), b("id", "=", id))
|
|
791
860
|
});
|
|
792
|
-
if (await countContents(scope,
|
|
793
|
-
await
|
|
861
|
+
if (await countContents(scope, id) > 0) {
|
|
862
|
+
await retain(folder);
|
|
794
863
|
throw s3Error("FOLDER_NOT_EMPTY");
|
|
795
864
|
}
|
|
796
|
-
}
|
|
865
|
+
},
|
|
866
|
+
retain
|
|
797
867
|
};
|
|
798
868
|
}
|
|
799
869
|
|
|
@@ -815,7 +885,7 @@ async function checkSchemaVersion(client) {
|
|
|
815
885
|
}
|
|
816
886
|
if (version !== DB_SCHEMA_VERSION) {
|
|
817
887
|
throw new Error(
|
|
818
|
-
`[dimah-s3/db] The database is at schema ${version}, but @dimah-s3/db
|
|
888
|
+
`[dimah-s3/db] The database is at schema ${version}, but @dimah-s3/db reads ${DB_SCHEMA_VERSION}. This package does not upgrade another schema. Create it with sql/<dialect>.sql, your ORM's migrations, or migrate(client) from @dimah-s3/db/cli.`
|
|
819
889
|
);
|
|
820
890
|
}
|
|
821
891
|
}
|
|
@@ -851,8 +921,8 @@ function createDbStore(client) {
|
|
|
851
921
|
}
|
|
852
922
|
|
|
853
923
|
// src/standard-schema.ts
|
|
854
|
-
async function validateStandardSchema(
|
|
855
|
-
return
|
|
924
|
+
async function validateStandardSchema(schema2, value) {
|
|
925
|
+
return schema2["~standard"].validate(value);
|
|
856
926
|
}
|
|
857
927
|
function formatStandardIssue(issues) {
|
|
858
928
|
const [issue] = issues;
|
|
@@ -865,10 +935,10 @@ function formatStandardIssue(issues) {
|
|
|
865
935
|
var DB_LIST_MAX_LIMIT = 100;
|
|
866
936
|
var MAX_SEARCH_LENGTH = 255;
|
|
867
937
|
var MAX_CURSOR_LENGTH = 2048;
|
|
868
|
-
function queryInt(
|
|
938
|
+
function queryInt(schema2) {
|
|
869
939
|
return z.codec(
|
|
870
|
-
z.union([z.string().trim().regex(z.regexes.integer),
|
|
871
|
-
|
|
940
|
+
z.union([z.string().trim().regex(z.regexes.integer), schema2]),
|
|
941
|
+
schema2,
|
|
872
942
|
{
|
|
873
943
|
decode: (value) => typeof value === "number" ? value : Number.parseInt(value, 10),
|
|
874
944
|
encode: (value) => value
|
|
@@ -901,7 +971,7 @@ var dbListSortQuerySchema = z.codec(
|
|
|
901
971
|
}
|
|
902
972
|
);
|
|
903
973
|
var dbListQuerySchema = z.strictObject({
|
|
904
|
-
|
|
974
|
+
folderId: z.string().optional(),
|
|
905
975
|
route: routeNameSchema.optional(),
|
|
906
976
|
// Rows that purge has claimed stay internal, as on `GET /db/object`.
|
|
907
977
|
status: z.enum(["pending", "active", "deleted"]).optional(),
|
|
@@ -926,12 +996,21 @@ var dbUpdateObjectBodySchema = z.strictObject({
|
|
|
926
996
|
key: objectKeySchema,
|
|
927
997
|
bucket: trimmedString.optional(),
|
|
928
998
|
filename: fileNameSchema.optional(),
|
|
929
|
-
|
|
999
|
+
folderId: z.string().nullable().optional(),
|
|
930
1000
|
/** `null` clears the attributes. */
|
|
931
1001
|
attributes: z.record(z.string(), z.unknown()).nullable().optional()
|
|
932
1002
|
});
|
|
933
|
-
var
|
|
934
|
-
|
|
1003
|
+
var dbCreateFolderBodySchema = z.strictObject({
|
|
1004
|
+
parentId: z.string().nullable().optional(),
|
|
1005
|
+
name: z.string()
|
|
1006
|
+
});
|
|
1007
|
+
var dbUpdateFolderBodySchema = z.strictObject({
|
|
1008
|
+
id: trimmedString,
|
|
1009
|
+
name: z.string().optional(),
|
|
1010
|
+
parentId: z.string().nullable().optional()
|
|
1011
|
+
});
|
|
1012
|
+
var dbDeleteFolderQuerySchema = z.strictObject({
|
|
1013
|
+
id: trimmedString
|
|
935
1014
|
});
|
|
936
1015
|
|
|
937
1016
|
// src/plugin/to-db-client-object.ts
|
|
@@ -942,7 +1021,7 @@ function toDbClientObject(object2, toClient) {
|
|
|
942
1021
|
bucket: object2.bucket,
|
|
943
1022
|
key: object2.key,
|
|
944
1023
|
route: object2.route,
|
|
945
|
-
|
|
1024
|
+
folderId: object2.folderId,
|
|
946
1025
|
filename: object2.filename,
|
|
947
1026
|
contentType: object2.contentType,
|
|
948
1027
|
size: object2.size,
|
|
@@ -960,10 +1039,12 @@ function toDbClientObject(object2, toClient) {
|
|
|
960
1039
|
}
|
|
961
1040
|
function toDbClientFolder(folder) {
|
|
962
1041
|
return {
|
|
963
|
-
|
|
1042
|
+
id: folder.id,
|
|
964
1043
|
name: folder.name,
|
|
965
|
-
|
|
966
|
-
|
|
1044
|
+
parentId: folder.parentId,
|
|
1045
|
+
path: folder.path,
|
|
1046
|
+
createdAt: folder.createdAt.toISOString(),
|
|
1047
|
+
updatedAt: folder.updatedAt.toISOString()
|
|
967
1048
|
};
|
|
968
1049
|
}
|
|
969
1050
|
|
|
@@ -995,13 +1076,16 @@ function matchingFolders(children, search) {
|
|
|
995
1076
|
const text = search.toLowerCase();
|
|
996
1077
|
return children.filter((child) => child.name.toLowerCase().includes(text));
|
|
997
1078
|
}
|
|
998
|
-
function
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1079
|
+
function wireFolderId(value) {
|
|
1080
|
+
if (value === void 0) return void 0;
|
|
1081
|
+
return value === "" || value === null ? null : value;
|
|
1082
|
+
}
|
|
1083
|
+
function folderTarget(folder) {
|
|
1084
|
+
if (folder === null) return { folder: null, folderId: null };
|
|
1085
|
+
return { folder: folder.path, folderId: folder.id };
|
|
1002
1086
|
}
|
|
1003
1087
|
function createDatabaseEndpoints(options) {
|
|
1004
|
-
const { access, quota, toClientObject } = options;
|
|
1088
|
+
const { access, quota, resolveScope, toClientObject } = options;
|
|
1005
1089
|
const { objects, folders } = options.store;
|
|
1006
1090
|
const parseAttributes = async (value) => {
|
|
1007
1091
|
const { attributes } = options;
|
|
@@ -1025,33 +1109,46 @@ function createDatabaseEndpoints(options) {
|
|
|
1025
1109
|
}
|
|
1026
1110
|
return result.value;
|
|
1027
1111
|
};
|
|
1112
|
+
const openFolder = async (scope, wire) => {
|
|
1113
|
+
const folderId = wireFolderId(wire);
|
|
1114
|
+
if (folderId === void 0) return void 0;
|
|
1115
|
+
if (folderId === null) return null;
|
|
1116
|
+
const folder = await folders.find(scope, folderId);
|
|
1117
|
+
if (!folder) throw s3Error("FOLDER_NOT_FOUND");
|
|
1118
|
+
return folder;
|
|
1119
|
+
};
|
|
1028
1120
|
return {
|
|
1029
|
-
/** One page of the caller's objects. `
|
|
1121
|
+
/** One page of the caller's objects. `folderId` omitted lists every folder. */
|
|
1030
1122
|
objects: createS3Endpoint(
|
|
1031
1123
|
pluginPath("db", "objects"),
|
|
1032
1124
|
{ method: "GET", query: dbListQuerySchema },
|
|
1033
1125
|
async (ctx) => {
|
|
1034
|
-
const {
|
|
1035
|
-
const
|
|
1126
|
+
const { folderId: wireFolder, ...filters } = ctx.query;
|
|
1127
|
+
const peeked = await resolveScope(ctx.context, {
|
|
1128
|
+
operation: "list",
|
|
1129
|
+
route: filters.route
|
|
1130
|
+
});
|
|
1131
|
+
const opened = await openFolder(peeked.scope, wireFolder);
|
|
1036
1132
|
const scope = await access.scope(ctx.context, {
|
|
1037
1133
|
operation: "list",
|
|
1038
1134
|
route: filters.route,
|
|
1039
|
-
...
|
|
1135
|
+
...opened !== void 0 && folderTarget(opened)
|
|
1040
1136
|
});
|
|
1041
1137
|
const page = await objects.list({
|
|
1042
1138
|
...filters,
|
|
1043
1139
|
scope: scope.scope,
|
|
1044
|
-
|
|
1140
|
+
folderId: opened === void 0 ? void 0 : opened?.id ?? null
|
|
1045
1141
|
});
|
|
1046
1142
|
const response = {
|
|
1047
1143
|
objects: page.objects.map((object2) => toClientObject(object2)),
|
|
1048
1144
|
nextCursor: page.nextCursor
|
|
1049
1145
|
};
|
|
1050
1146
|
if (!page.firstPage) return response;
|
|
1051
|
-
const [usage, limits, children] = await Promise.all([
|
|
1147
|
+
const [usage, limits, children, trail] = await Promise.all([
|
|
1052
1148
|
objects.getUsage(scope.scope),
|
|
1053
1149
|
quota.limits(scope),
|
|
1054
|
-
|
|
1150
|
+
opened === void 0 ? void 0 : folders.children(scope.scope, opened?.id ?? null),
|
|
1151
|
+
opened === void 0 ? void 0 : opened === null ? [] : folders.trail(scope.scope, opened.id)
|
|
1055
1152
|
]);
|
|
1056
1153
|
return {
|
|
1057
1154
|
...response,
|
|
@@ -1061,7 +1158,8 @@ function createDatabaseEndpoints(options) {
|
|
|
1061
1158
|
folders: matchingFolders(children, filters.search).map(
|
|
1062
1159
|
toDbClientFolder
|
|
1063
1160
|
)
|
|
1064
|
-
}
|
|
1161
|
+
},
|
|
1162
|
+
...trail && { trail: trail.map(toDbClientFolder) }
|
|
1065
1163
|
};
|
|
1066
1164
|
}
|
|
1067
1165
|
),
|
|
@@ -1087,8 +1185,7 @@ function createDatabaseEndpoints(options) {
|
|
|
1087
1185
|
),
|
|
1088
1186
|
/**
|
|
1089
1187
|
* Renames, moves, or sets the attributes of an active object. A move
|
|
1090
|
-
*
|
|
1091
|
-
* created when missing.
|
|
1188
|
+
* requires the target folder to exist and authorizes that folder.
|
|
1092
1189
|
*/
|
|
1093
1190
|
updateObject: createS3Endpoint(
|
|
1094
1191
|
pluginPath("db", "object"),
|
|
@@ -1100,55 +1197,95 @@ function createDatabaseEndpoints(options) {
|
|
|
1100
1197
|
ctx.body.bucket,
|
|
1101
1198
|
"body.bucket"
|
|
1102
1199
|
);
|
|
1103
|
-
const
|
|
1200
|
+
const folderId = wireFolderId(ctx.body.folderId);
|
|
1201
|
+
const peeked = await resolveScope(ctx.context, {
|
|
1202
|
+
operation: "update"
|
|
1203
|
+
});
|
|
1204
|
+
const opened = folderId === void 0 ? void 0 : await openFolder(peeked.scope, folderId ?? "");
|
|
1104
1205
|
const { scope, object: object2 } = await access.object(
|
|
1105
1206
|
ctx.context,
|
|
1106
1207
|
{
|
|
1107
1208
|
operation: "update",
|
|
1108
1209
|
key,
|
|
1109
|
-
...
|
|
1210
|
+
...opened !== void 0 && folderTarget(opened)
|
|
1110
1211
|
},
|
|
1111
1212
|
{ bucket, key }
|
|
1112
1213
|
);
|
|
1113
1214
|
if (object2.status !== "active") throw s3Error("OBJECT_NOT_FOUND");
|
|
1114
1215
|
const attributes = ctx.body.attributes === void 0 || ctx.body.attributes === null ? ctx.body.attributes : await parseAttributes(ctx.body.attributes);
|
|
1115
|
-
if (filename === void 0 &&
|
|
1216
|
+
if (filename === void 0 && folderId === void 0 && attributes === void 0) {
|
|
1116
1217
|
return { object: toClientObject(object2) };
|
|
1117
1218
|
}
|
|
1118
1219
|
const updated = await options.objects.update(
|
|
1119
1220
|
{ bucket, key, id: object2.id, scope: scope.scope },
|
|
1120
|
-
{ filename,
|
|
1221
|
+
{ filename, folderId, attributes }
|
|
1121
1222
|
);
|
|
1122
1223
|
if (!updated) throw s3Error("OBJECT_NOT_FOUND");
|
|
1123
1224
|
return { object: toClientObject(updated) };
|
|
1124
1225
|
}
|
|
1125
1226
|
),
|
|
1126
|
-
/** Creates
|
|
1227
|
+
/** Creates one folder inside a parent that already exists. */
|
|
1127
1228
|
createFolder: createS3Endpoint(
|
|
1128
1229
|
pluginPath("db", "folder"),
|
|
1129
|
-
{ method: "POST", body:
|
|
1230
|
+
{ method: "POST", body: dbCreateFolderBodySchema },
|
|
1130
1231
|
async (ctx) => {
|
|
1131
|
-
const
|
|
1232
|
+
const parentId = wireFolderId(ctx.body.parentId) ?? null;
|
|
1233
|
+
const peeked = await resolveScope(ctx.context, { operation: "update" });
|
|
1234
|
+
const parent = parentId === null ? null : await openFolder(peeked.scope, parentId);
|
|
1235
|
+
const path = childFolderPath(parent?.path ?? null, ctx.body.name);
|
|
1236
|
+
if (path === null) throw s3Error("FOLDER_INVALID");
|
|
1132
1237
|
const scope = await access.scope(ctx.context, {
|
|
1133
1238
|
operation: "update",
|
|
1134
1239
|
folder: path
|
|
1135
1240
|
});
|
|
1136
|
-
const folder = await folders.create(scope.scope,
|
|
1241
|
+
const folder = await folders.create(scope.scope, {
|
|
1242
|
+
parentId,
|
|
1243
|
+
name: ctx.body.name
|
|
1244
|
+
});
|
|
1245
|
+
return { folder: toDbClientFolder(folder) };
|
|
1246
|
+
}
|
|
1247
|
+
),
|
|
1248
|
+
/** Renames a folder, moves it, or both. */
|
|
1249
|
+
updateFolder: createS3Endpoint(
|
|
1250
|
+
pluginPath("db", "folder"),
|
|
1251
|
+
{ method: "PATCH", body: dbUpdateFolderBodySchema },
|
|
1252
|
+
async (ctx) => {
|
|
1253
|
+
const peeked = await resolveScope(ctx.context, { operation: "update" });
|
|
1254
|
+
const current = await folders.find(peeked.scope, ctx.body.id);
|
|
1255
|
+
if (!current) throw s3Error("FOLDER_NOT_FOUND");
|
|
1256
|
+
const parentId = ctx.body.parentId === void 0 ? current.parentId : wireFolderId(ctx.body.parentId) ?? null;
|
|
1257
|
+
const parent = parentId === null ? null : await openFolder(peeked.scope, parentId);
|
|
1258
|
+
const path = childFolderPath(
|
|
1259
|
+
parent?.path ?? null,
|
|
1260
|
+
ctx.body.name ?? current.name
|
|
1261
|
+
);
|
|
1262
|
+
if (path === null) throw s3Error("FOLDER_INVALID");
|
|
1263
|
+
await access.scope(ctx.context, {
|
|
1264
|
+
operation: "update",
|
|
1265
|
+
folder: path,
|
|
1266
|
+
folderId: current.id
|
|
1267
|
+
});
|
|
1268
|
+
const folder = await folders.update(peeked.scope, current.id, {
|
|
1269
|
+
...ctx.body.name !== void 0 && { name: ctx.body.name },
|
|
1270
|
+
...ctx.body.parentId !== void 0 && { parentId }
|
|
1271
|
+
});
|
|
1137
1272
|
return { folder: toDbClientFolder(folder) };
|
|
1138
1273
|
}
|
|
1139
1274
|
),
|
|
1140
1275
|
/** Removes an empty folder. A missing folder is not an error. */
|
|
1141
1276
|
deleteFolder: createS3Endpoint(
|
|
1142
1277
|
pluginPath("db", "folder"),
|
|
1143
|
-
{ method: "DELETE", query:
|
|
1278
|
+
{ method: "DELETE", query: dbDeleteFolderQuerySchema },
|
|
1144
1279
|
async (ctx) => {
|
|
1145
|
-
const
|
|
1146
|
-
const
|
|
1280
|
+
const peeked = await resolveScope(ctx.context, { operation: "update" });
|
|
1281
|
+
const current = await folders.find(peeked.scope, ctx.query.id);
|
|
1282
|
+
if (!current) return { id: ctx.query.id };
|
|
1283
|
+
await access.scope(ctx.context, {
|
|
1147
1284
|
operation: "update",
|
|
1148
|
-
|
|
1285
|
+
...folderTarget(current)
|
|
1149
1286
|
});
|
|
1150
|
-
await folders.delete(
|
|
1151
|
-
return {
|
|
1287
|
+
await folders.delete(peeked.scope, current.id);
|
|
1288
|
+
return { id: current.id };
|
|
1152
1289
|
}
|
|
1153
1290
|
)
|
|
1154
1291
|
};
|
|
@@ -1168,12 +1305,9 @@ function refOf(context) {
|
|
|
1168
1305
|
function objectTarget(context, operation) {
|
|
1169
1306
|
return { operation, route: context.route, key: context.key };
|
|
1170
1307
|
}
|
|
1171
|
-
function
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
folder: parseFolder(context.clientMetadata?.folder ?? ""),
|
|
1175
|
-
clientMetadata: context.clientMetadata
|
|
1176
|
-
};
|
|
1308
|
+
function requestedFolderId(metadata) {
|
|
1309
|
+
const value = metadata?.folderId ?? "";
|
|
1310
|
+
return value === "" ? null : value;
|
|
1177
1311
|
}
|
|
1178
1312
|
function writtenBy(object2, uploadToken) {
|
|
1179
1313
|
return object2 !== null && object2.uploadToken === uploadToken;
|
|
@@ -1199,16 +1333,22 @@ function createDatabaseLifecycleHooks(options) {
|
|
|
1199
1333
|
if (!live) throw s3Error("OBJECT_NOT_FOUND");
|
|
1200
1334
|
};
|
|
1201
1335
|
const uploadWrite = async (context) => {
|
|
1202
|
-
const target = uploadTarget(context);
|
|
1203
1336
|
const existing = await objects.find(refOf(context));
|
|
1204
|
-
|
|
1205
|
-
|
|
1337
|
+
const replaced = existing?.status === "active" && routes.replace(context.route) === "overwrite" ? existing : null;
|
|
1338
|
+
const folderId = replaced ? replaced.folderId : requestedFolderId(context.clientMetadata);
|
|
1339
|
+
const scope = await resolveScope(context, objectTarget(context, "upload"));
|
|
1340
|
+
let folder = null;
|
|
1341
|
+
if (folderId !== null) {
|
|
1342
|
+
folder = await folders.find(scope.scope, folderId);
|
|
1343
|
+
if (!folder && !replaced) throw s3Error("FOLDER_NOT_FOUND");
|
|
1206
1344
|
}
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1345
|
+
const target = {
|
|
1346
|
+
...objectTarget(context, "upload"),
|
|
1347
|
+
folderId,
|
|
1348
|
+
clientMetadata: context.clientMetadata,
|
|
1349
|
+
...folder ? { folder: folder.path } : folderId === null ? { folder: null } : {}
|
|
1211
1350
|
};
|
|
1351
|
+
return { target, existing, replaced, folder };
|
|
1212
1352
|
};
|
|
1213
1353
|
const abortPendingMultipart = async (context, row) => {
|
|
1214
1354
|
const client = routes.client(context.route);
|
|
@@ -1256,13 +1396,13 @@ function createDatabaseLifecycleHooks(options) {
|
|
|
1256
1396
|
});
|
|
1257
1397
|
},
|
|
1258
1398
|
async onStart(context) {
|
|
1259
|
-
const
|
|
1260
|
-
const { scope } = await resolveScope(context, target);
|
|
1261
|
-
|
|
1399
|
+
const written = await uploadWrite(context);
|
|
1400
|
+
const { scope } = await resolveScope(context, written.target);
|
|
1401
|
+
await objects.upsertPending({
|
|
1262
1402
|
...refOf(context),
|
|
1263
1403
|
scope,
|
|
1264
1404
|
route: context.route,
|
|
1265
|
-
|
|
1405
|
+
folderId: written.target.folderId ?? null,
|
|
1266
1406
|
filename: context.file.name,
|
|
1267
1407
|
contentType: context.file.type ?? null,
|
|
1268
1408
|
declaredSize: context.file.size,
|
|
@@ -1272,7 +1412,7 @@ function createDatabaseLifecycleHooks(options) {
|
|
|
1272
1412
|
expiresAt: context.expiresAt,
|
|
1273
1413
|
overwrite: routes.replace(context.route) === "overwrite"
|
|
1274
1414
|
});
|
|
1275
|
-
await folders.
|
|
1415
|
+
if (written.folder) await folders.retain(written.folder);
|
|
1276
1416
|
},
|
|
1277
1417
|
async confirmGuard(context) {
|
|
1278
1418
|
await assertUploadRow(context, "confirm", {
|
|
@@ -1377,6 +1517,7 @@ function createObjectAccess(options) {
|
|
|
1377
1517
|
...scope,
|
|
1378
1518
|
...target?.key !== void 0 && { key: target.key },
|
|
1379
1519
|
...target?.folder !== void 0 && { folder: target.folder },
|
|
1520
|
+
...target?.folderId !== void 0 && { folderId: target.folderId },
|
|
1380
1521
|
...target?.clientMetadata !== void 0 && {
|
|
1381
1522
|
clientMetadata: target.clientMetadata
|
|
1382
1523
|
},
|
|
@@ -1406,10 +1547,16 @@ function createObjectAccess(options) {
|
|
|
1406
1547
|
assertOwner
|
|
1407
1548
|
};
|
|
1408
1549
|
}
|
|
1409
|
-
function
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1550
|
+
function rootId(folderId) {
|
|
1551
|
+
return folderId === "" ? null : folderId;
|
|
1552
|
+
}
|
|
1553
|
+
async function targetFolder(folders, scope, folderId) {
|
|
1554
|
+
if (folderId === void 0) return void 0;
|
|
1555
|
+
const id = rootId(folderId);
|
|
1556
|
+
if (id === null) return null;
|
|
1557
|
+
const folder = await folders.find(scope, id);
|
|
1558
|
+
if (!folder) throw s3Error("FOLDER_NOT_FOUND");
|
|
1559
|
+
return folder;
|
|
1413
1560
|
}
|
|
1414
1561
|
function createPublicStores(store) {
|
|
1415
1562
|
const { objects, folders } = store;
|
|
@@ -1420,26 +1567,19 @@ function createPublicStores(store) {
|
|
|
1420
1567
|
count: objects.count,
|
|
1421
1568
|
getUsage: objects.getUsage,
|
|
1422
1569
|
async update(ref, input) {
|
|
1423
|
-
const
|
|
1424
|
-
|
|
1425
|
-
if (
|
|
1426
|
-
|
|
1570
|
+
const folderId = input.folderId === void 0 ? void 0 : rootId(input.folderId);
|
|
1571
|
+
let folder;
|
|
1572
|
+
if (typeof folderId === "string") {
|
|
1573
|
+
const scope = ref.scope ?? (await objects.find(ref))?.scope;
|
|
1574
|
+
if (!scope) return null;
|
|
1575
|
+
folder = await targetFolder(folders, scope, folderId);
|
|
1427
1576
|
}
|
|
1577
|
+
const row = await objects.update(ref, { ...input, folderId });
|
|
1578
|
+
if (row && folder) await folders.retain(folder);
|
|
1428
1579
|
return row;
|
|
1429
1580
|
}
|
|
1430
1581
|
},
|
|
1431
|
-
|
|
1432
|
-
folders: {
|
|
1433
|
-
find: async (scope, path) => folders.find(scope, folderPath(path)),
|
|
1434
|
-
children: async (scope, parent, options) => folders.children(
|
|
1435
|
-
scope,
|
|
1436
|
-
parent === null ? null : folderPath(parent),
|
|
1437
|
-
options
|
|
1438
|
-
),
|
|
1439
|
-
ensure: async (scope, path) => folders.ensure(scope, path === null ? null : folderPath(path)),
|
|
1440
|
-
create: async (scope, path) => folders.create(scope, folderPath(path)),
|
|
1441
|
-
delete: async (scope, path) => folders.delete(scope, folderPath(path))
|
|
1442
|
-
}
|
|
1582
|
+
folders
|
|
1443
1583
|
};
|
|
1444
1584
|
}
|
|
1445
1585
|
async function abortUpload(client, row, uploadId) {
|
|
@@ -1636,6 +1776,7 @@ function db(options) {
|
|
|
1636
1776
|
store,
|
|
1637
1777
|
objects: stores.objects,
|
|
1638
1778
|
access,
|
|
1779
|
+
resolveScope,
|
|
1639
1780
|
quota,
|
|
1640
1781
|
attributes: options.attributes,
|
|
1641
1782
|
toClientObject
|
|
@@ -1670,6 +1811,6 @@ function warnUnguardedRoutes(env) {
|
|
|
1670
1811
|
}
|
|
1671
1812
|
}
|
|
1672
1813
|
|
|
1673
|
-
export { DB_SCHEMA_VERSION, DimahS3DB, db
|
|
1814
|
+
export { DB_SCHEMA_VERSION, DimahS3DB, db };
|
|
1674
1815
|
//# sourceMappingURL=index.js.map
|
|
1675
1816
|
//# sourceMappingURL=index.js.map
|