@dimah-s3/db 2.0.0-beta.5 → 2.0.0-beta.7

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.
Files changed (49) hide show
  1. package/README.md +15 -6
  2. package/dist/adapters/drizzle.d.ts +2 -3
  3. package/dist/adapters/drizzle.d.ts.map +1 -1
  4. package/dist/adapters/kysely.d.ts +1 -1
  5. package/dist/adapters/prisma.d.ts +1 -1
  6. package/dist/cli.d.ts +17 -12
  7. package/dist/cli.d.ts.map +1 -1
  8. package/dist/cli.js +42 -74
  9. package/dist/cli.js.map +1 -1
  10. package/dist/client/index.js +1 -0
  11. package/dist/client/index.js.map +1 -1
  12. package/dist/folder-path.d.ts +8 -2
  13. package/dist/folder-path.d.ts.map +1 -1
  14. package/dist/fuma-db.d.ts +19 -22
  15. package/dist/fuma-db.d.ts.map +1 -1
  16. package/dist/index.js +69 -101
  17. package/dist/index.js.map +1 -1
  18. package/dist/plugin/scope.d.ts +2 -0
  19. package/dist/plugin/scope.d.ts.map +1 -1
  20. package/dist/schema/schema.d.ts +33 -48
  21. package/dist/schema/schema.d.ts.map +1 -1
  22. package/dist/store/folder-store.d.ts.map +1 -1
  23. package/dist/store/map-row.d.ts +2 -8
  24. package/dist/store/map-row.d.ts.map +1 -1
  25. package/dist/store/object-store.d.ts.map +1 -1
  26. package/dist/types/plugin.d.ts +4 -3
  27. package/dist/types/plugin.d.ts.map +1 -1
  28. package/package.json +8 -26
  29. package/dist/drizzle/mysql.d.ts +0 -1057
  30. package/dist/drizzle/mysql.d.ts.map +0 -1
  31. package/dist/drizzle/mysql.js +0 -120
  32. package/dist/drizzle/mysql.js.map +0 -1
  33. package/dist/drizzle/pg.d.ts +0 -994
  34. package/dist/drizzle/pg.d.ts.map +0 -1
  35. package/dist/drizzle/pg.js +0 -104
  36. package/dist/drizzle/pg.js.map +0 -1
  37. package/dist/drizzle/shared.d.ts +0 -7
  38. package/dist/drizzle/shared.d.ts.map +0 -1
  39. package/dist/drizzle/sqlite.d.ts +0 -1056
  40. package/dist/drizzle/sqlite.d.ts.map +0 -1
  41. package/dist/drizzle/sqlite.js +0 -107
  42. package/dist/drizzle/sqlite.js.map +0 -1
  43. package/prisma/dimah-s3.prisma +0 -81
  44. package/sql/mysql.sql +0 -60
  45. package/sql/pg.sql +0 -53
  46. package/sql/sqlite.sql +0 -55
  47. package/src/drizzle/mysql.ts +0 -175
  48. package/src/drizzle/pg.ts +0 -146
  49. package/src/drizzle/sqlite.ts +0 -150
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { fumadb } from 'fumadb';
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
4
  import { s3Error, pluginPath, base64UrlDecode, base64UrlEncode } from '@dimah-s3/core';
@@ -7,115 +7,87 @@ import * as z from 'zod';
7
7
  import { AbortMultipartUploadCommand, HeadObjectCommand } from '@aws-sdk/client-s3';
8
8
 
9
9
  // src/fuma-db.ts
10
- function byteCount(name) {
11
- return column(name, "bigint").nullable();
10
+ var BINARY = { mysql: "utf8mb4_bin" };
11
+ var TEXT = { mysql: "utf8mb4_unicode_ci" };
12
+ var LONG_PREFIX = 512;
13
+ var SCOPE_MAX_LENGTH = 255;
14
+ function string(name, length = 255) {
15
+ return column(name, `varchar(${length})`).collate(BINARY);
12
16
  }
13
17
  var storageObject = table("storage_object", {
14
- id: idColumn("id", "varchar(255)").defaultTo$("auto"),
18
+ id: idColumn("id", "varchar(255)").collate(BINARY).defaultTo$("auto"),
15
19
  /** Owner, for example `user:123` or `org:acme`. */
16
- scope: column("scope", "string"),
17
- bucket: column("bucket", "string"),
18
- key: column("key", "string"),
20
+ scope: string("scope", SCOPE_MAX_LENGTH),
21
+ bucket: string("bucket"),
22
+ key: string("key", 1024),
19
23
  /** Named dimah-s3 route that created the object. */
20
- route: column("route", "string"),
24
+ route: string("route"),
21
25
  /** Folder that holds the object. `null` is the root. */
22
- folderId: column("folder_id", "string").nullable(),
23
- filename: column("filename", "string"),
24
- contentType: column("content_type", "string").nullable(),
26
+ folderId: string("folder_id").nullable(),
27
+ filename: column("filename", "varchar(255)").collate(TEXT),
28
+ contentType: string("content_type").nullable(),
25
29
  /** Verified size from HeadObject, set on confirm. */
26
- size: byteCount("size"),
30
+ size: column("size", "bigint").nullable(),
27
31
  /** Size the client declared before confirm. Quota counts it while pending. */
28
- declaredSize: byteCount("declared_size"),
29
- eTag: column("e_tag", "string").nullable(),
30
- status: column("status", "string"),
32
+ declaredSize: column("declared_size", "bigint").nullable(),
33
+ eTag: string("e_tag").nullable(),
34
+ status: string("status", 16),
31
35
  /** S3 user metadata of the verified object. */
32
36
  s3Metadata: column("s3_metadata", "json").nullable(),
33
37
  /** App attributes, validated by the plugin's `attributes.schema`. */
34
38
  attributes: column("attributes", "json").nullable(),
35
- acl: column("acl", "string").nullable(),
39
+ acl: string("acl").nullable(),
36
40
  /** Multipart upload ID while the row is pending. */
37
- uploadId: column("upload_id", "string").nullable(),
41
+ uploadId: column("upload_id", "string").collate(BINARY).nullable(),
38
42
  /** Token of the upload ticket that wrote the row. Confirm and purge match it. */
39
- uploadToken: column("upload_token", "string").nullable(),
43
+ uploadToken: string("upload_token", 64).nullable(),
40
44
  /** Confirm deadline of a pending row (the upload ticket's expiry). */
41
45
  expiresAt: column("expires_at", "timestamp").nullable(),
42
46
  confirmedAt: column("confirmed_at", "timestamp").nullable(),
43
47
  createdAt: column("created_at", "timestamp").defaultTo$("now"),
44
48
  updatedAt: column("updated_at", "timestamp").defaultTo$("now"),
45
49
  deletedAt: column("deleted_at", "timestamp").nullable()
46
- }).unique("storage_object_bucket_key_uk", ["bucket", "key"]);
50
+ }).unique("storage_object_bucket_key_uk", ["bucket", "key"], {
51
+ length: { key: LONG_PREFIX }
52
+ }).index("storage_object_scope_folder_idx", [
53
+ "scope",
54
+ "status",
55
+ "folderId",
56
+ "createdAt"
57
+ ]).index("storage_object_scope_updated_idx", ["scope", "status", "updatedAt"]).index("storage_object_scope_filename_idx", ["scope", "status", "filename"]).index("storage_object_scope_size_idx", ["scope", "status", "size"]).index("storage_object_status_expires_idx", ["status", "expiresAt"]);
47
58
  var storageFolder = table("storage_folder", {
48
- id: idColumn("id", "varchar(255)").defaultTo$("auto"),
49
- scope: column("scope", "string"),
59
+ id: idColumn("id", "varchar(255)").collate(BINARY).defaultTo$("auto"),
60
+ scope: string("scope", SCOPE_MAX_LENGTH),
50
61
  /** Id of the containing folder. `""` is the root. */
51
- parentId: column("parent_id", "string"),
62
+ parentId: string("parent_id").defaultTo(""),
52
63
  /** Last segment of `path`. */
53
- name: column("name", "string"),
64
+ name: string("name"),
54
65
  /** Full folder path. */
55
- path: column("path", "string"),
66
+ path: string("path", 1024),
56
67
  createdAt: column("created_at", "timestamp").defaultTo$("now"),
57
68
  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 = [
60
- {
61
- name: "storage_object_scope_folder_idx",
62
- table: "storage_object",
63
- columns: ["scope", "status", "folder_id", "created_at"]
64
- },
65
- {
66
- name: "storage_object_scope_updated_idx",
67
- table: "storage_object",
68
- columns: ["scope", "status", "updated_at"]
69
- },
70
- {
71
- name: "storage_object_scope_filename_idx",
72
- table: "storage_object",
73
- columns: ["scope", "status", "filename"]
74
- },
75
- {
76
- name: "storage_object_scope_size_idx",
77
- table: "storage_object",
78
- columns: ["scope", "status", "size"]
79
- },
80
- {
81
- name: "storage_object_status_expires_idx",
82
- table: "storage_object",
83
- columns: ["status", "expires_at"]
84
- }
85
- ];
86
- var custom = (sql) => ({ type: "custom", sql });
87
- var PREDECESSOR_VERSION = "0.0.1";
88
- var predecessor = schema({
89
- version: PREDECESSOR_VERSION,
90
- tables: {}
91
- });
69
+ }).unique("storage_folder_scope_path_uk", ["scope", "path"], {
70
+ length: { path: LONG_PREFIX }
71
+ }).unique("storage_folder_scope_parent_name_uk", ["scope", "parentId", "name"]);
92
72
  var dbSchema = schema({
93
73
  version: "2.1.0",
94
74
  tables: {
95
75
  storageObject,
96
76
  storageFolder
97
- },
98
- async up({ auto }) {
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];
106
77
  }
107
78
  });
108
79
 
109
80
  // src/fuma-db.ts
110
81
  var DimahS3DB = fumadb({
111
82
  namespace: "dimah_s3",
112
- schemas: [predecessor, dbSchema]
83
+ schemas: [dbSchema]
113
84
  });
114
85
  var DB_SCHEMA_VERSION = dbSchema.version;
115
- var FOLDER_PATH_MAX_LENGTH = 1024;
86
+ var FOLDER_PATH_MAX_BYTES = 1024;
116
87
  var FOLDER_NAME_MAX_LENGTH = 255;
117
88
  var FOLDER_MAX_DEPTH = 32;
118
89
  var INVALID_CHARACTER = /[\p{Cc}\p{Cs}]/u;
90
+ var utf8 = new TextEncoder();
119
91
  function characterCount(value) {
120
92
  return Array.from(value).length;
121
93
  }
@@ -126,7 +98,7 @@ function isFolderName(name) {
126
98
  function normalizeFolderPath(path) {
127
99
  if (!path.isWellFormed()) return null;
128
100
  const normalized = path.normalize("NFC");
129
- if (characterCount(normalized) > FOLDER_PATH_MAX_LENGTH) return null;
101
+ if (utf8.encode(normalized).byteLength > FOLDER_PATH_MAX_BYTES) return null;
130
102
  const names = normalized.split("/");
131
103
  if (names.length > FOLDER_MAX_DEPTH) return null;
132
104
  return names.every(isFolderName) ? normalized : null;
@@ -304,6 +276,12 @@ function filterConditions(b, filter, now) {
304
276
  if (filter.search) parts.push(b("filename", "contains", filter.search));
305
277
  return parts;
306
278
  }
279
+ function toByteCount(value) {
280
+ return value == null ? null : BigInt(value);
281
+ }
282
+ function toColumnValue(value) {
283
+ return typeof value === "number" ? BigInt(value) : value;
284
+ }
307
285
  function createObjectStore(getOrm) {
308
286
  const map = mapStorageObjectRow;
309
287
  const find = async (ref) => {
@@ -348,13 +326,11 @@ function createObjectStore(getOrm) {
348
326
  } else {
349
327
  if (nullable) parts.push(b.isNotNull(field));
350
328
  if (position && position.value !== null) {
329
+ const value = toColumnValue(position.value);
351
330
  parts.push(
352
331
  b.or(
353
- b(field, after, position.value),
354
- b.and(
355
- b(field, "=", position.value),
356
- b("id", after, position.id)
357
- )
332
+ b(field, after, value),
333
+ b.and(b(field, "=", value), b("id", after, position.id))
358
334
  )
359
335
  );
360
336
  }
@@ -439,7 +415,7 @@ function createObjectStore(getOrm) {
439
415
  const upload = {
440
416
  uploadId: input.uploadId ?? null,
441
417
  uploadToken: input.uploadToken,
442
- declaredSize: input.declaredSize ?? null,
418
+ declaredSize: toByteCount(input.declaredSize),
443
419
  expiresAt: input.expiresAt
444
420
  };
445
421
  const now = /* @__PURE__ */ new Date();
@@ -466,7 +442,7 @@ function createObjectStore(getOrm) {
466
442
  try {
467
443
  await orm.create("storageObject", fresh);
468
444
  } catch (error) {
469
- if (await find(ref)) throw s3Error("CONFLICT");
445
+ if (error instanceof UniqueConstraintError) throw s3Error("CONFLICT");
470
446
  throw error;
471
447
  }
472
448
  };
@@ -501,7 +477,7 @@ function createObjectStore(getOrm) {
501
477
  where: (b) => b.and(byUpload(b, input), uploading(b)),
502
478
  set: {
503
479
  status: "active",
504
- size: input.size,
480
+ size: BigInt(input.size),
505
481
  declaredSize: null,
506
482
  eTag: input.eTag ?? null,
507
483
  ...input.contentType !== void 0 && {
@@ -644,13 +620,6 @@ function createObjectStore(getOrm) {
644
620
 
645
621
  // src/store/folder-store.ts
646
622
  var DEFAULT_FOLDER_CHILDREN_LIMIT = 1e3;
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
623
  function storedParent(parentId) {
655
624
  return parentId ?? "";
656
625
  }
@@ -676,17 +645,6 @@ function createFolderStore(getOrm) {
676
645
  });
677
646
  return row ? mapFolder(row) : null;
678
647
  };
679
- const findNamed = async (scope, parentId, name) => {
680
- const orm = await getOrm();
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
648
  const insertFolder = async (orm, folder) => {
691
649
  const now = /* @__PURE__ */ new Date();
692
650
  const row = await orm.create("storageFolder", {
@@ -747,7 +705,9 @@ function createFolderStore(getOrm) {
747
705
  try {
748
706
  await insertFolder(orm, folder);
749
707
  } catch (error) {
750
- if (await find(folder.scope, folder.id)) return;
708
+ if (error instanceof UniqueConstraintError && await find(folder.scope, folder.id)) {
709
+ return;
710
+ }
751
711
  throw error;
752
712
  }
753
713
  };
@@ -795,7 +755,7 @@ function createFolderStore(getOrm) {
795
755
  path
796
756
  });
797
757
  } catch (error) {
798
- if (await findNamed(scope, input.parentId, name)) {
758
+ if (error instanceof UniqueConstraintError) {
799
759
  throw s3Error("FOLDER_EXISTS");
800
760
  }
801
761
  throw error;
@@ -834,7 +794,9 @@ function createFolderStore(getOrm) {
834
794
  }
835
795
  });
836
796
  } catch (error) {
837
- if (isUniqueViolation(error)) throw s3Error("FOLDER_EXISTS");
797
+ if (error instanceof UniqueConstraintError) {
798
+ throw s3Error("FOLDER_EXISTS");
799
+ }
838
800
  throw error;
839
801
  }
840
802
  for (const child of rewritten) {
@@ -1050,7 +1012,7 @@ function toDbClientFolder(folder) {
1050
1012
 
1051
1013
  // src/plugin/create-database-endpoints.ts
1052
1014
  var DEFAULT_ATTRIBUTES_MAX_BYTES = 16384;
1053
- var utf8 = new TextEncoder();
1015
+ var utf82 = new TextEncoder();
1054
1016
  function resolveBucket(config, bucket, field) {
1055
1017
  const buckets = /* @__PURE__ */ new Set();
1056
1018
  for (const route of Object.values(config.routes)) {
@@ -1101,7 +1063,7 @@ function createDatabaseEndpoints(options) {
1101
1063
  });
1102
1064
  }
1103
1065
  const maxBytes = attributes.maxBytes ?? DEFAULT_ATTRIBUTES_MAX_BYTES;
1104
- const size = utf8.encode(JSON.stringify(result.value)).byteLength;
1066
+ const size = utf82.encode(JSON.stringify(result.value)).byteLength;
1105
1067
  if (size > maxBytes) {
1106
1068
  throw s3Error("VALIDATION_ERROR", {
1107
1069
  issue: `[body.attributes] Exceeds ${maxBytes} bytes`
@@ -1728,6 +1690,12 @@ function createScopeResolver(options) {
1728
1690
  }
1729
1691
  const resolved = await scope;
1730
1692
  if (resolved === null) throw s3Error("UNAUTHORIZED");
1693
+ const length = characterCount(resolved);
1694
+ if (length > SCOPE_MAX_LENGTH) {
1695
+ throw new Error(
1696
+ `[dimah-s3/db] resolveScope returned a scope of ${length} characters. A scope holds at most ${SCOPE_MAX_LENGTH}.`
1697
+ );
1698
+ }
1731
1699
  return { ...context, scope: resolved };
1732
1700
  };
1733
1701
  }