@dimah-s3/db 1.7.0 → 2.0.0-beta.0

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 (120) hide show
  1. package/README.md +62 -17
  2. package/dist/adapters/drizzle.d.ts +7 -0
  3. package/dist/adapters/drizzle.d.ts.map +1 -0
  4. package/dist/adapters/drizzle.js +3 -0
  5. package/dist/adapters/drizzle.js.map +1 -0
  6. package/dist/adapters/kysely.d.ts +7 -0
  7. package/dist/adapters/kysely.d.ts.map +1 -0
  8. package/dist/adapters/kysely.js +3 -0
  9. package/dist/adapters/kysely.js.map +1 -0
  10. package/dist/adapters/prisma.d.ts +7 -0
  11. package/dist/adapters/prisma.d.ts.map +1 -0
  12. package/dist/adapters/prisma.js +3 -0
  13. package/dist/adapters/prisma.js.map +1 -0
  14. package/dist/cli.d.ts +33 -4
  15. package/dist/cli.d.ts.map +1 -1
  16. package/dist/cli.js +192 -4
  17. package/dist/cli.js.map +1 -1
  18. package/dist/client/db-client.d.ts +9 -85
  19. package/dist/client/db-client.d.ts.map +1 -1
  20. package/dist/client/db-object-source.d.ts +25 -0
  21. package/dist/client/db-object-source.d.ts.map +1 -0
  22. package/dist/client/index.d.ts +3 -3
  23. package/dist/client/index.d.ts.map +1 -1
  24. package/dist/client/index.js +96 -42
  25. package/dist/client/index.js.map +1 -1
  26. package/dist/drizzle/mysql.d.ts +1024 -0
  27. package/dist/drizzle/mysql.d.ts.map +1 -0
  28. package/dist/drizzle/mysql.js +123 -0
  29. package/dist/drizzle/mysql.js.map +1 -0
  30. package/dist/drizzle/pg.d.ts +963 -0
  31. package/dist/drizzle/pg.d.ts.map +1 -0
  32. package/dist/drizzle/pg.js +104 -0
  33. package/dist/drizzle/pg.js.map +1 -0
  34. package/dist/drizzle/shared.d.ts +16 -0
  35. package/dist/drizzle/shared.d.ts.map +1 -0
  36. package/dist/drizzle/sqlite.d.ts +1023 -0
  37. package/dist/drizzle/sqlite.d.ts.map +1 -0
  38. package/dist/drizzle/sqlite.js +107 -0
  39. package/dist/drizzle/sqlite.js.map +1 -0
  40. package/dist/folder-path.d.ts +32 -0
  41. package/dist/folder-path.d.ts.map +1 -0
  42. package/dist/fuma-db.d.ts +47 -5
  43. package/dist/fuma-db.d.ts.map +1 -1
  44. package/dist/index.d.ts +9 -13
  45. package/dist/index.d.ts.map +1 -1
  46. package/dist/index.js +1452 -620
  47. package/dist/index.js.map +1 -1
  48. package/dist/plugin/create-database-endpoints.d.ts +105 -21
  49. package/dist/plugin/create-database-endpoints.d.ts.map +1 -1
  50. package/dist/plugin/create-database-lifecycle-hooks.d.ts +37 -19
  51. package/dist/plugin/create-database-lifecycle-hooks.d.ts.map +1 -1
  52. package/dist/plugin/db.d.ts +29 -118
  53. package/dist/plugin/db.d.ts.map +1 -1
  54. package/dist/plugin/object-access.d.ts +46 -0
  55. package/dist/plugin/object-access.d.ts.map +1 -0
  56. package/dist/plugin/public-stores.d.ts +33 -0
  57. package/dist/plugin/public-stores.d.ts.map +1 -0
  58. package/dist/plugin/purge.d.ts +49 -0
  59. package/dist/plugin/purge.d.ts.map +1 -0
  60. package/dist/plugin/quota.d.ts +25 -0
  61. package/dist/plugin/quota.d.ts.map +1 -0
  62. package/dist/plugin/schemas.d.ts +69 -0
  63. package/dist/plugin/schemas.d.ts.map +1 -0
  64. package/dist/plugin/scope.d.ts +31 -0
  65. package/dist/plugin/scope.d.ts.map +1 -0
  66. package/dist/plugin/to-db-client-object.d.ts +8 -4
  67. package/dist/plugin/to-db-client-object.d.ts.map +1 -1
  68. package/dist/schema/v1.d.ts +1 -8
  69. package/dist/schema/v1.d.ts.map +1 -1
  70. package/dist/schema/v2.d.ts +64 -0
  71. package/dist/schema/v2.d.ts.map +1 -0
  72. package/dist/standard-schema.d.ts +42 -0
  73. package/dist/standard-schema.d.ts.map +1 -0
  74. package/dist/store/create-db-store.d.ts +17 -0
  75. package/dist/store/create-db-store.d.ts.map +1 -0
  76. package/dist/store/folder-store.d.ts +36 -0
  77. package/dist/store/folder-store.d.ts.map +1 -0
  78. package/dist/store/list-cursor.d.ts +26 -4
  79. package/dist/store/list-cursor.d.ts.map +1 -1
  80. package/dist/store/map-row.d.ts +17 -8
  81. package/dist/store/map-row.d.ts.map +1 -1
  82. package/dist/store/object-store.d.ts +208 -0
  83. package/dist/store/object-store.d.ts.map +1 -0
  84. package/dist/store/schema-version.d.ts +7 -0
  85. package/dist/store/schema-version.d.ts.map +1 -0
  86. package/dist/types/client.d.ts +209 -0
  87. package/dist/types/client.d.ts.map +1 -0
  88. package/dist/types/plugin.d.ts +112 -0
  89. package/dist/types/plugin.d.ts.map +1 -0
  90. package/dist/types/storage-object.d.ts +40 -85
  91. package/dist/types/storage-object.d.ts.map +1 -1
  92. package/package.json +51 -12
  93. package/prisma/dimah-s3.prisma +80 -0
  94. package/sql/mysql.sql +59 -0
  95. package/sql/pg.sql +53 -0
  96. package/sql/sqlite.sql +54 -0
  97. package/sql/upgrade/1.0.0-2.0.0/mysql.sql +71 -0
  98. package/sql/upgrade/1.0.0-2.0.0/pg.sql +47 -0
  99. package/sql/upgrade/1.0.0-2.0.0/sqlite.sql +119 -0
  100. package/dist/client/db-object-list-accessors.d.ts +0 -36
  101. package/dist/client/db-object-list-accessors.d.ts.map +0 -1
  102. package/dist/client/db-object-query.d.ts +0 -40
  103. package/dist/client/db-object-query.d.ts.map +0 -1
  104. package/dist/errors.d.ts +0 -6
  105. package/dist/errors.d.ts.map +0 -1
  106. package/dist/hooks/create-object-access-guard.d.ts +0 -51
  107. package/dist/hooks/create-object-access-guard.d.ts.map +0 -1
  108. package/dist/hooks/create-quota-guard.d.ts +0 -28
  109. package/dist/hooks/create-quota-guard.d.ts.map +0 -1
  110. package/dist/jobs/purge-stale-pending.d.ts +0 -31
  111. package/dist/jobs/purge-stale-pending.d.ts.map +0 -1
  112. package/dist/plugin/list-query-schema.d.ts +0 -21
  113. package/dist/plugin/list-query-schema.d.ts.map +0 -1
  114. package/dist/plugin/memoize-scope-resolver.d.ts +0 -8
  115. package/dist/plugin/memoize-scope-resolver.d.ts.map +0 -1
  116. package/dist/store/storage-object-store.d.ts +0 -137
  117. package/dist/store/storage-object-store.d.ts.map +0 -1
  118. package/src/schema/examples/README.md +0 -10
  119. package/src/schema/examples/drizzle.ts +0 -92
  120. package/src/schema/examples/schema.prisma +0 -55
@@ -1,62 +1,116 @@
1
- import { defineClientPlugin, pluginPath, formatFileSize, fileNameFromKey } from '@dimah-s3/core';
1
+ import { defineClientPlugin, pluginPath, s3Error } from '@dimah-s3/core';
2
2
 
3
3
  // src/client/db-client.ts
4
+ function call(options) {
5
+ return { signal: options?.signal, headers: options?.headers };
6
+ }
7
+ function listQuery(input) {
8
+ if (input?.folder !== null) return input;
9
+ return { ...input, folder: "" };
10
+ }
4
11
  function dbClient() {
5
12
  return defineClientPlugin({
6
13
  id: "db",
7
14
  $InferServerPlugin: {},
8
15
  getActions: ($fetch) => ({
9
- /**
10
- * List the caller's objects and usage. Scope comes from server `resolveScope`
11
- * — not a parameter here. For another scope, use `listByScope` on the server.
12
- * Pass `init.signal` to cancel the request.
13
- */
14
- listObjects: (input, init) => $fetch(pluginPath("db", "objects"), {
15
- method: "GET",
16
- query: input,
17
- signal: init?.signal
16
+ listObjects: (input, options) => $fetch(
17
+ pluginPath("db", "objects"),
18
+ { method: "GET", query: listQuery(input), ...call(options) }
19
+ ),
20
+ getObject: (input, options) => $fetch(
21
+ pluginPath("db", "object"),
22
+ { method: "GET", query: input, ...call(options) }
23
+ ),
24
+ updateObject: (input, options) => $fetch(
25
+ pluginPath("db", "object"),
26
+ { method: "PATCH", body: input, ...call(options) }
27
+ ),
28
+ createFolder: (input, options) => $fetch(pluginPath("db", "folder"), {
29
+ method: "POST",
30
+ body: input,
31
+ ...call(options)
18
32
  }),
19
- getObject: (input) => $fetch(pluginPath("db", "object"), {
20
- method: "GET",
21
- query: input
33
+ deleteFolder: (input, options) => $fetch(pluginPath("db", "folder"), {
34
+ method: "DELETE",
35
+ query: input,
36
+ ...call(options)
22
37
  })
23
38
  })
24
39
  });
25
40
  }
26
-
27
- // src/client/db-object-query.ts
28
- function dbObjectQuery(listObjects, options) {
29
- return async function query(input) {
30
- const queryInput = {};
31
- if (input.parent) queryInput.prefix = input.parent;
32
- if (input.cursor) queryInput.cursor = input.cursor;
33
- if (options?.route) queryInput.route = options.route;
34
- if (options?.status) queryInput.status = options.status;
35
- if (options?.contentType) queryInput.contentType = options.contentType;
36
- if (options?.limit != null) queryInput.limit = options.limit;
37
- const page = await listObjects(queryInput, { signal: input.signal });
38
- return {
39
- items: page.objects,
40
- nextCursor: page.nextCursor ?? null,
41
- usage: page.usage
42
- };
43
- };
41
+ function formatFolder(folder) {
42
+ return folder ?? "";
44
43
  }
45
- function displayName(object) {
46
- return object.filename || fileNameFromKey(object.key) || object.key;
44
+ function joinFolderPath(parent, name) {
45
+ return parent === null ? name : `${parent}/${name}`;
47
46
  }
48
- function dbObjectListAccessors(options = {}) {
49
- const formatSize = options.formatSize ?? ((bytes) => formatFileSize(bytes));
47
+
48
+ // src/client/db-object-source.ts
49
+ var SORT_FIELDS = /* @__PURE__ */ new Map([
50
+ ["name", "filename"],
51
+ ["size", "size"],
52
+ ["updatedAt", "updatedAt"]
53
+ ]);
54
+ function toListSort(sort) {
55
+ const field = sort ? SORT_FIELDS.get(sort.field) : void 0;
56
+ return sort && field ? { field, direction: sort.direction } : void 0;
57
+ }
58
+ function toFile(object) {
50
59
  return {
51
- getKey: (object) => object.id,
52
- getObjectKey: (object) => object.key,
53
- getLabel: displayName,
54
- getFileName: displayName,
55
- getFileType: (object) => object.contentType,
56
- getDescription: (object) => object.size != null ? formatSize(object.size) : null
60
+ name: object.filename,
61
+ objectKey: object.key,
62
+ size: object.size,
63
+ contentType: object.contentType,
64
+ updatedAt: object.updatedAt
65
+ };
66
+ }
67
+ function dbObjectSource(db, options = {}) {
68
+ const folders = options.folders !== false;
69
+ const source = {
70
+ getKey: (item) => item.id,
71
+ getFile: toFile,
72
+ async query({ parent, cursor, sort, search, signal }) {
73
+ const input = {};
74
+ if (folders) input.folder = formatFolder(parent);
75
+ if (cursor) input.cursor = cursor;
76
+ if (search) input.search = search;
77
+ const listSort = toListSort(sort);
78
+ if (listSort) input.sort = listSort;
79
+ if (options.route) input.route = options.route;
80
+ if (options.status) input.status = options.status;
81
+ if (options.contentType) input.contentType = options.contentType;
82
+ if (options.limit !== void 0) input.limit = options.limit;
83
+ const page = await db.listObjects(input, { signal });
84
+ return {
85
+ items: page.objects,
86
+ nextCursor: page.nextCursor,
87
+ ...page.folders && {
88
+ folders: page.folders.map((folder) => ({
89
+ id: folder.path,
90
+ name: folder.name
91
+ }))
92
+ },
93
+ ...page.usage && { usage: { ...page.usage, ...page.limits } }
94
+ };
95
+ }
57
96
  };
97
+ if (!folders) return source;
98
+ source.uploadOptions = (parent) => ({
99
+ metadata: { folder: formatFolder(parent) }
100
+ });
101
+ const createFolder = db.createFolder?.bind(db);
102
+ if (createFolder) {
103
+ source.createFolder = async ({ parent, name }) => {
104
+ if (name.includes("/")) throw s3Error("FOLDER_INVALID");
105
+ const { folder } = await createFolder({
106
+ path: joinFolderPath(parent, name)
107
+ });
108
+ return { id: folder.path, name: folder.name };
109
+ };
110
+ }
111
+ return source;
58
112
  }
59
113
 
60
- export { dbClient, dbObjectListAccessors, dbObjectQuery };
114
+ export { dbClient, dbObjectSource };
61
115
  //# sourceMappingURL=index.js.map
62
116
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/client/db-client.ts","../../src/client/db-object-query.ts","../../src/client/db-object-list-accessors.ts"],"names":[],"mappings":";;;AAkCO,SAAS,QAAA,GAAW;AACzB,EAAA,OAAO,kBAAA,CAAmB;AAAA,IACxB,EAAA,EAAI,IAAA;AAAA,IACJ,oBAAoB,EAAC;AAAA,IACrB,UAAA,EAAY,CAAC,MAAA,MAAY;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,MAMvB,WAAA,EAAa,CACX,KAAA,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AAK;AAAA,KACd;AAAA,EACF,CAAA;AACF;AC5CA,SAAS,YAAY,MAAA,EAAgC;AACnD,EAAA,OAAO,OAAO,QAAA,IAAY,eAAA,CAAgB,MAAA,CAAO,GAAG,KAAK,MAAA,CAAO,GAAA;AAClE;AAaO,SAAS,qBAAA,CACd,OAAA,GAAwC,EAAC,EAClB;AACvB,EAAA,MAAM,aACJ,OAAA,CAAQ,UAAA,KAAe,CAAC,KAAA,KAAkB,eAAe,KAAK,CAAA,CAAA;AAChE,EAAA,OAAO;AAAA,IACL,MAAA,EAAQ,CAAC,MAAA,KAAW,MAAA,CAAO,EAAA;AAAA,IAC3B,YAAA,EAAc,CAAC,MAAA,KAAW,MAAA,CAAO,GAAA;AAAA,IACjC,QAAA,EAAU,WAAA;AAAA,IACV,WAAA,EAAa,WAAA;AAAA,IACb,WAAA,EAAa,CAAC,MAAA,KAAW,MAAA,CAAO,WAAA;AAAA,IAChC,cAAA,EAAgB,CAAC,MAAA,KACf,MAAA,CAAO,QAAQ,IAAA,GAAO,UAAA,CAAW,MAAA,CAAO,IAAI,CAAA,GAAI;AAAA,GACpD;AACF","file":"index.js","sourcesContent":["import { defineClientPlugin, pluginPath } from \"@dimah-s3/core\";\nimport type { db } from \"@/plugin/db\";\nimport type {\n DbClientGetInput,\n DbClientListInput,\n DbClientListResponse,\n DbClientObject,\n} from \"@/types/storage-object\";\n\nexport type {\n DbClientGetInput,\n DbClientListInput,\n DbClientListResponse,\n DbClientObject,\n} from \"@/types/storage-object\";\n\n/**\n * Client plugin for the server `db()` endpoints.\n *\n * ```ts\n * import { createS3Client } from \"@dimah-s3/react\";\n * import { dbClient } from \"@dimah-s3/db/client\";\n *\n * export const { api, useApi } = createS3Client({\n * plugins: [dbClient()],\n * });\n *\n * await api.db.listObjects({ limit: 50 });\n * ```\n *\n * Server code does not use `listObjects` — it calls `s3.db.objects.listByScope`\n * and `getScopeUsage` directly (no HTTP round-trip). `listObjects` is the browser\n * wrapper for `GET /db/objects`.\n */\nexport function dbClient() {\n return defineClientPlugin({\n id: \"db\",\n $InferServerPlugin: {} as ReturnType<typeof db>,\n getActions: ($fetch) => ({\n /**\n * List the caller's objects and usage. Scope comes from server `resolveScope`\n * — not a parameter here. For another scope, use `listByScope` on the server.\n * Pass `init.signal` to cancel the request.\n */\n listObjects: (\n input?: DbClientListInput,\n init?: { signal?: AbortSignal },\n ) =>\n $fetch<DbClientListResponse>(pluginPath(\"db\", \"objects\"), {\n method: \"GET\",\n query: input,\n signal: init?.signal,\n }),\n getObject: (input: DbClientGetInput) =>\n $fetch<{ object: DbClientObject }>(pluginPath(\"db\", \"object\"), {\n method: \"GET\",\n query: input,\n }),\n }),\n });\n}\n","import type {\n DbClientListInput,\n DbClientListResponse,\n DbClientObject,\n StorageObjectStatus,\n} from \"@/types/storage-object\";\n\n/** `listObjects` plus an optional abort signal. Matches `dbClient()`. */\nexport type DbObjectListSource = (\n input?: DbClientListInput,\n init?: { signal?: AbortSignal },\n) => Promise<DbClientListResponse>;\n\nexport type DbObjectQueryOptions = {\n route?: string;\n status?: StorageObjectStatus;\n contentType?: string;\n limit?: number;\n};\n\nexport type DbObjectQueryInput = {\n parent: string | null;\n cursor: string | null;\n signal: AbortSignal;\n};\n\nexport type DbObjectQueryPage = {\n items: DbClientObject[];\n nextCursor: string | null;\n /** Scope totals from `listObjects`. Not limited to the open folder. */\n usage: DbClientListResponse[\"usage\"];\n};\n\n/**\n * Adapt `api.db.listObjects` to a `useObjectList` query.\n *\n * `parent` is sent as the key `prefix`. This does not invent folder rows:\n * default object keys contain a uuid segment, so a delimiter over the key\n * is not a folder tree. Return `folders` from your own query when the\n * catalog has them. `usage` is the scope total from `listObjects`, not the\n * open folder.\n *\n * ```ts\n * const list = useObjectList({\n * query: dbObjectQuery(api.db.listObjects, { route: \"docs\" }),\n * queryKey: [\"docs\"],\n * });\n * ```\n */\nexport function dbObjectQuery(\n listObjects: DbObjectListSource,\n options?: DbObjectQueryOptions,\n) {\n return async function query(\n input: DbObjectQueryInput,\n ): Promise<DbObjectQueryPage> {\n const queryInput: DbClientListInput = {};\n if (input.parent) queryInput.prefix = input.parent;\n if (input.cursor) queryInput.cursor = input.cursor;\n if (options?.route) queryInput.route = options.route;\n if (options?.status) queryInput.status = options.status;\n if (options?.contentType) queryInput.contentType = options.contentType;\n if (options?.limit != null) queryInput.limit = options.limit;\n\n const page = await listObjects(queryInput, { signal: input.signal });\n return {\n items: page.objects,\n nextCursor: page.nextCursor ?? null,\n usage: page.usage,\n };\n };\n}\n","import { fileNameFromKey, formatFileSize } from \"@dimah-s3/core\";\nimport type { DbClientObject } from \"@/types/storage-object\";\n\n/** Accessors that map a `listObjects` row onto `ObjectList`. */\nexport type DbObjectListAccessors = {\n /** Row id. */\n getKey: (object: DbClientObject) => string;\n /** Stored object key. Bulk delete uses this. */\n getObjectKey: (object: DbClientObject) => string;\n /** Filename, or the last segment of the key. */\n getLabel: (object: DbClientObject) => string;\n /** Same name the type glyph uses. */\n getFileName: (object: DbClientObject) => string;\n getFileType: (object: DbClientObject) => string | null;\n /** Verified size. `null` while the object is still pending. */\n getDescription: (object: DbClientObject) => string | null;\n};\n\n/** Options for {@link dbObjectListAccessors}. */\nexport type DbObjectListAccessorsOptions = {\n /**\n * Size label for `getDescription`. In React, pass `useFormatFileSize()`\n * from `@dimah-s3/react` to follow the `S3Provider` `locale`.\n */\n formatSize?: (bytes: number) => string;\n};\n\nfunction displayName(object: DbClientObject): string {\n return object.filename || fileNameFromKey(object.key) || object.key;\n}\n\n/**\n * Map `api.db.listObjects` rows onto `ObjectList` accessors.\n *\n * This does not invent folders. Pass the result of `dbObjectQuery` as `list`.\n *\n * ```tsx\n * const files = dbObjectListAccessors();\n *\n * <ObjectList list={list} {...files} />\n * ```\n */\nexport function dbObjectListAccessors(\n options: DbObjectListAccessorsOptions = {},\n): DbObjectListAccessors {\n const formatSize =\n options.formatSize ?? ((bytes: number) => formatFileSize(bytes));\n return {\n getKey: (object) => object.id,\n getObjectKey: (object) => object.key,\n getLabel: displayName,\n getFileName: displayName,\n getFileType: (object) => object.contentType,\n getDescription: (object) =>\n object.size != null ? formatSize(object.size) : null,\n };\n}\n"]}
1
+ {"version":3,"sources":["../../src/client/db-client.ts","../../src/folder-path.ts","../../src/client/db-object-source.ts"],"names":["s3Error"],"mappings":";;;AAeA,SAAS,KAAK,OAAA,EAAuC;AACnD,EAAA,OAAO,EAAE,MAAA,EAAQ,OAAA,EAAS,MAAA,EAAQ,OAAA,EAAS,SAAS,OAAA,EAAQ;AAC9D;AAGA,SAAS,UAAU,KAAA,EAAsC;AACvD,EAAA,IAAI,KAAA,EAAO,MAAA,KAAW,IAAA,EAAM,OAAO,KAAA;AACnC,EAAA,OAAO,EAAE,GAAG,KAAA,EAAO,MAAA,EAAQ,EAAA,EAAG;AAChC;AAqBO,SAAS,QAAA,GAAsC;AACpD,EAAA,OAAO,kBAAA,CAAmB;AAAA,IACxB,EAAA,EAAI,IAAA;AAAA,IACJ,oBAAoB,EAAC;AAAA,IACrB,UAAA,EAAY,CAAC,MAAA,MAAgD;AAAA,MAC3D,WAAA,EAAa,CAAC,KAAA,EAAO,OAAA,KACnB,MAAA;AAAA,QACE,UAAA,CAAW,MAAM,SAAS,CAAA;AAAA,QAC1B,EAAE,MAAA,EAAQ,KAAA,EAAO,KAAA,EAAO,SAAA,CAAU,KAAK,CAAA,EAAG,GAAG,IAAA,CAAK,OAAO,CAAA;AAAE,OAC7D;AAAA,MACF,SAAA,EAAW,CAAC,KAAA,EAAO,OAAA,KACjB,MAAA;AAAA,QACE,UAAA,CAAW,MAAM,QAAQ,CAAA;AAAA,QACzB,EAAE,QAAQ,KAAA,EAAO,KAAA,EAAO,OAAO,GAAG,IAAA,CAAK,OAAO,CAAA;AAAE,OAClD;AAAA,MACF,YAAA,EAAc,CAAC,KAAA,EAAO,OAAA,KACpB,MAAA;AAAA,QACE,UAAA,CAAW,MAAM,QAAQ,CAAA;AAAA,QACzB,EAAE,QAAQ,OAAA,EAAS,IAAA,EAAM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A;AAAA,QACZ,YAAY,IAAA,CAAK,UAAA;AAAA,QACjB,GAAI,KAAK,OAAA,IAAW;AAAA,UAClB,OAAA,EAAS,IAAA,CAAK,OAAA,CAAQ,GAAA,CAAI,CAAC,MAAA,MAAY;AAAA,YACrC,IAAI,MAAA,CAAO,IAAA;AAAA,YACX,MAAM,MAAA,CAAO;AAAA,WACf,CAAE;AAAA,SACJ;AAAA,QACA,GAAI,IAAA,CAAK,KAAA,IAAS,EAAE,KAAA,EAAO,EAAE,GAAG,IAAA,CAAK,KAAA,EAAO,GAAG,IAAA,CAAK,MAAA,EAAO;AAAE,OAC/D;AAAA,IACF;AAAA,GACF;AACA,EAAA,IAAI,CAAC,SAAS,OAAO,MAAA;AAErB,EAAA,MAAA,CAAO,aAAA,GAAgB,CAAC,MAAA,MAAY;AAAA,IAClC,QAAA,EAAU,EAAE,MAAA,EAAQ,YAAA,CAAa,MAAM,CAAA;AAAE,GAC3C,CAAA;AACA,EAAA,MAAM,YAAA,GAAe,EAAA,CAAG,YAAA,EAAc,IAAA,CAAK,EAAE,CAAA;AAC7C,EAAA,IAAI,YAAA,EAAc;AAChB,IAAA,MAAA,CAAO,YAAA,GAAe,OAAO,EAAE,MAAA,EAAQ,MAAK,KAAM;AAEhD,MAAA,IAAI,KAAK,QAAA,CAAS,GAAG,CAAA,EAAG,MAAMA,QAAQ,gBAAgB,CAAA;AACtD,MAAA,MAAM,EAAE,MAAA,EAAO,GAAI,MAAM,YAAA,CAAa;AAAA,QACpC,IAAA,EAAM,cAAA,CAAe,MAAA,EAAQ,IAAI;AAAA,OAClC,CAAA;AACD,MAAA,OAAO,EAAE,EAAA,EAAI,MAAA,CAAO,IAAA,EAAM,IAAA,EAAM,OAAO,IAAA,EAAK;AAAA,IAC9C,CAAA;AAAA,EACF;AACA,EAAA,OAAO,MAAA;AACT","file":"index.js","sourcesContent":["import {\n defineClientPlugin,\n pluginPath,\n type S3RequestOptions,\n} from \"@dimah-s3/core\";\nimport type { db } from \"@/plugin/db\";\nimport type {\n DbClientActions,\n DbClientFolder,\n DbClientListInput,\n DbClientListResponse,\n DbClientObject,\n} from \"@/types/client\";\n\n/** The per-call options better-fetch reads: the abort signal and headers. */\nfunction call(options: S3RequestOptions | undefined) {\n return { signal: options?.signal, headers: options?.headers };\n}\n\n/** The list query on the wire, where the root folder is `\"\"`. */\nfunction listQuery(input: DbClientListInput | undefined) {\n if (input?.folder !== null) return input;\n return { ...input, folder: \"\" };\n}\n\n/**\n * Client plugin for the server `db()` endpoints. Its actions are\n * {@link DbClientActions} on `api.db`. Pass `TClientAttributes` to type the\n * `attributes` the plugin's `attributes.toClient` returns.\n *\n * ```ts\n * import { createS3Client } from \"@dimah-s3/react\";\n * import { dbClient } from \"@dimah-s3/db/client\";\n *\n * export const s3Client = createS3Client({\n * plugins: [dbClient()],\n * });\n *\n * const page = await s3Client.db.listObjects({ folder: \"\" });\n * ```\n *\n * The server resolves the scope from the request, so no action takes one.\n * Server code skips HTTP and uses `s3.db.objects` and `s3.db.folders`.\n */\nexport function dbClient<TClientAttributes = never>() {\n return defineClientPlugin({\n id: \"db\",\n $InferServerPlugin: {} as ReturnType<typeof db>,\n getActions: ($fetch): DbClientActions<TClientAttributes> => ({\n listObjects: (input, options) =>\n $fetch<DbClientListResponse<TClientAttributes>>(\n pluginPath(\"db\", \"objects\"),\n { method: \"GET\", query: listQuery(input), ...call(options) },\n ),\n getObject: (input, options) =>\n $fetch<{ object: DbClientObject<TClientAttributes> }>(\n pluginPath(\"db\", \"object\"),\n { method: \"GET\", query: input, ...call(options) },\n ),\n updateObject: (input, options) =>\n $fetch<{ object: DbClientObject<TClientAttributes> }>(\n pluginPath(\"db\", \"object\"),\n { method: \"PATCH\", body: input, ...call(options) },\n ),\n createFolder: (input, options) =>\n $fetch<{ folder: DbClientFolder }>(pluginPath(\"db\", \"folder\"), {\n method: \"POST\",\n body: input,\n ...call(options),\n }),\n deleteFolder: (input, options) =>\n $fetch<{ path: string }>(pluginPath(\"db\", \"folder\"), {\n method: \"DELETE\",\n query: input,\n ...call(options),\n }),\n }),\n });\n}\n","import { s3Error } from \"@dimah-s3/core\";\n\n/**\n * Logical folder paths. A path is NFC-normalized and `/`-separated, with no\n * leading or trailing `/`. `null` is the root in types and JSON. On the wire\n * (`?folder=`, `clientMetadata.folder`) the root is `\"\"`.\n */\n\n/** Longest folder path, in characters. */\nexport const FOLDER_PATH_MAX_LENGTH = 1024;\n\n/** Longest folder name (one path segment), in characters. */\nexport const FOLDER_NAME_MAX_LENGTH = 255;\n\n/** Most segments in a folder path. Each one is a row the server keeps. */\nexport const FOLDER_MAX_DEPTH = 32;\n\n/** Control characters and lone surrogates. */\nconst INVALID_CHARACTER = /[\\p{Cc}\\p{Cs}]/u;\n\n/** Counts code points, so a character outside the BMP counts once. */\nfunction characterCount(value: string): number {\n return Array.from(value).length;\n}\n\nfunction isValidName(name: string): boolean {\n const length = characterCount(name);\n return (\n length >= 1 &&\n length <= FOLDER_NAME_MAX_LENGTH &&\n name !== \".\" &&\n name !== \"..\" &&\n !INVALID_CHARACTER.test(name)\n );\n}\n\n/**\n * Returns the NFC form of `path`, or `null` when it is not a valid folder\n * path. The root (`\"\"`) is not a folder path.\n */\nexport function normalizeFolderPath(path: string): string | null {\n if (!path.isWellFormed()) return null;\n const normalized = path.normalize(\"NFC\");\n if (characterCount(normalized) > FOLDER_PATH_MAX_LENGTH) return null;\n const names = normalized.split(\"/\");\n if (names.length > FOLDER_MAX_DEPTH) return null;\n return names.every(isValidName) ? normalized : null;\n}\n\n/**\n * Reads a wire folder value. `\"\"` is the root and returns `null`.\n * @throws `FOLDER_INVALID` when the value is not a valid folder path.\n */\nexport function parseFolder(value: string): string | null {\n if (value === \"\") return null;\n const path = normalizeFolderPath(value);\n if (path === null) throw s3Error(\"FOLDER_INVALID\");\n return path;\n}\n\n/** Writes a folder for the wire. The root becomes `\"\"`. */\nexport function formatFolder(folder: string | null): string {\n return folder ?? \"\";\n}\n\n/** Appends `name` to `parent`. The result is not validated. */\nexport function joinFolderPath(parent: string | null, name: string): string {\n return parent === null ? name : `${parent}/${name}`;\n}\n\n/** The folder that contains `path`. `null` for a top-level folder. */\nexport function parentFolderPath(path: string): string | null {\n const index = path.lastIndexOf(\"/\");\n return index === -1 ? null : path.slice(0, index);\n}\n\n/** The last segment of `path`. */\nexport function folderName(path: string): string {\n return path.slice(path.lastIndexOf(\"/\") + 1);\n}\n\n/** Every folder from the top level down to `path` itself. */\nexport function folderAncestors(path: string): string[] {\n const segments = path.split(\"/\");\n return segments.map((_, index) => segments.slice(0, index + 1).join(\"/\"));\n}\n","import { s3Error } from \"@dimah-s3/core\";\nimport { formatFolder, joinFolderPath } from \"@/folder-path\";\nimport type {\n DbClientActions,\n DbClientListInput,\n DbClientObject,\n DbListSort,\n DbListSortField,\n DbObjectSource,\n DbObjectSourceFile,\n DbObjectSourceOptions,\n DbObjectSourceQueryInput,\n} from \"@/types/client\";\n\n/** The `api.db` actions {@link dbObjectSource} calls. */\nexport type DbObjectSourceActions<TClientAttributes = never> = Pick<\n DbClientActions<TClientAttributes>,\n \"listObjects\"\n> &\n Partial<Pick<DbClientActions<TClientAttributes>, \"createFolder\">>;\n\n/** List sort fields and the `listObjects` columns they order by. */\nconst SORT_FIELDS = new Map<string, DbListSortField>([\n [\"name\", \"filename\"],\n [\"size\", \"size\"],\n [\"updatedAt\", \"updatedAt\"],\n]);\n\nfunction toListSort(\n sort: DbObjectSourceQueryInput[\"sort\"],\n): DbListSort | undefined {\n const field = sort ? SORT_FIELDS.get(sort.field) : undefined;\n return sort && field ? { field, direction: sort.direction } : undefined;\n}\n\nfunction toFile(object: DbClientObject<unknown>): DbObjectSourceFile {\n return {\n name: object.filename,\n objectKey: object.key,\n size: object.size,\n contentType: object.contentType,\n updatedAt: object.updatedAt,\n };\n}\n\n/**\n * A `useObjectList` source over `api.db`.\n *\n * With `folders` on (the default), `parent` is a folder path: each page\n * lists one folder, the first page carries its child folders, uploads go to\n * the open folder, and \"New folder\" calls `api.db.createFolder`. With\n * `folders: false`, the list is flat and has no folder actions.\n *\n * `search` matches the filename on the server. `sort` maps `name` to\n * `filename`; `size` and `updatedAt` keep their names, and other fields keep\n * the server's order (newest first). `usage` is the scope total with its\n * caps, not the open folder's.\n *\n * ```ts\n * const list = useObjectList({\n * ...dbObjectSource(api.db, { route: \"docs\" }),\n * queryKey: [\"docs\"],\n * });\n * ```\n */\nexport function dbObjectSource<TClientAttributes = never>(\n db: DbObjectSourceActions<TClientAttributes>,\n options: DbObjectSourceOptions = {},\n): DbObjectSource<TClientAttributes> {\n const folders = options.folders !== false;\n\n const source: DbObjectSource<TClientAttributes> = {\n getKey: (item) => item.id,\n getFile: toFile,\n async query({ parent, cursor, sort, search, signal }) {\n const input: DbClientListInput = {};\n if (folders) input.folder = formatFolder(parent);\n if (cursor) input.cursor = cursor;\n if (search) input.search = search;\n const listSort = toListSort(sort);\n if (listSort) input.sort = listSort;\n if (options.route) input.route = options.route;\n if (options.status) input.status = options.status;\n if (options.contentType) input.contentType = options.contentType;\n if (options.limit !== undefined) input.limit = options.limit;\n\n const page = await db.listObjects(input, { signal });\n return {\n items: page.objects,\n nextCursor: page.nextCursor,\n ...(page.folders && {\n folders: page.folders.map((folder) => ({\n id: folder.path,\n name: folder.name,\n })),\n }),\n ...(page.usage && { usage: { ...page.usage, ...page.limits } }),\n };\n },\n };\n if (!folders) return source;\n\n source.uploadOptions = (parent) => ({\n metadata: { folder: formatFolder(parent) },\n });\n const createFolder = db.createFolder?.bind(db);\n if (createFolder) {\n source.createFolder = async ({ parent, name }) => {\n // A `/` would create a nested folder instead of a child of `parent`.\n if (name.includes(\"/\")) throw s3Error(\"FOLDER_INVALID\");\n const { folder } = await createFolder({\n path: joinFolderPath(parent, name),\n });\n return { id: folder.path, name: folder.name };\n };\n }\n return source;\n}\n"]}