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

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 (87) hide show
  1. package/README.md +19 -10
  2. package/dist/adapters/drizzle.d.ts +2 -2
  3. package/dist/adapters/kysely.d.ts +1 -1
  4. package/dist/adapters/prisma.d.ts +1 -1
  5. package/dist/cli.d.ts +18 -14
  6. package/dist/cli.d.ts.map +1 -1
  7. package/dist/cli.js +69 -153
  8. package/dist/cli.js.map +1 -1
  9. package/dist/client/db-client.d.ts +1 -1
  10. package/dist/client/db-client.d.ts.map +1 -1
  11. package/dist/client/db-object-source.d.ts +15 -9
  12. package/dist/client/db-object-source.d.ts.map +1 -1
  13. package/dist/client/index.d.ts +3 -3
  14. package/dist/client/index.d.ts.map +1 -1
  15. package/dist/client/index.js +101 -21
  16. package/dist/client/index.js.map +1 -1
  17. package/dist/folder-path.d.ts +17 -4
  18. package/dist/folder-path.d.ts.map +1 -1
  19. package/dist/fuma-db.d.ts +22 -43
  20. package/dist/fuma-db.d.ts.map +1 -1
  21. package/dist/index.d.ts +9 -9
  22. package/dist/index.d.ts.map +1 -1
  23. package/dist/index.js +606 -314
  24. package/dist/index.js.map +1 -1
  25. package/dist/plugin/create-database-endpoints.d.ts +46 -22
  26. package/dist/plugin/create-database-endpoints.d.ts.map +1 -1
  27. package/dist/plugin/create-database-lifecycle-hooks.d.ts +4 -4
  28. package/dist/plugin/create-database-lifecycle-hooks.d.ts.map +1 -1
  29. package/dist/plugin/db.d.ts +2 -2
  30. package/dist/plugin/db.d.ts.map +1 -1
  31. package/dist/plugin/object-access.d.ts +2 -2
  32. package/dist/plugin/object-access.d.ts.map +1 -1
  33. package/dist/plugin/public-stores.d.ts +5 -8
  34. package/dist/plugin/public-stores.d.ts.map +1 -1
  35. package/dist/plugin/quota.d.ts +1 -1
  36. package/dist/plugin/schemas.d.ts +25 -11
  37. package/dist/plugin/schemas.d.ts.map +1 -1
  38. package/dist/plugin/scope.d.ts +2 -0
  39. package/dist/plugin/scope.d.ts.map +1 -1
  40. package/dist/plugin/to-db-client-object.d.ts.map +1 -1
  41. package/dist/schema/schema.d.ts +72 -0
  42. package/dist/schema/schema.d.ts.map +1 -0
  43. package/dist/store/create-db-store.d.ts +4 -4
  44. package/dist/store/create-db-store.d.ts.map +1 -1
  45. package/dist/store/folder-store.d.ts +73 -15
  46. package/dist/store/folder-store.d.ts.map +1 -1
  47. package/dist/store/list-cursor.d.ts +3 -1
  48. package/dist/store/list-cursor.d.ts.map +1 -1
  49. package/dist/store/map-row.d.ts +3 -9
  50. package/dist/store/map-row.d.ts.map +1 -1
  51. package/dist/store/object-store.d.ts +24 -14
  52. package/dist/store/object-store.d.ts.map +1 -1
  53. package/dist/store/schema-version.d.ts +1 -1
  54. package/dist/store/search-name.d.ts +7 -0
  55. package/dist/store/search-name.d.ts.map +1 -0
  56. package/dist/types/client.d.ts +114 -23
  57. package/dist/types/client.d.ts.map +1 -1
  58. package/dist/types/plugin.d.ts +17 -6
  59. package/dist/types/plugin.d.ts.map +1 -1
  60. package/dist/types/storage-object.d.ts +8 -7
  61. package/dist/types/storage-object.d.ts.map +1 -1
  62. package/package.json +15 -30
  63. package/dist/drizzle/mysql.d.ts +0 -1024
  64. package/dist/drizzle/mysql.d.ts.map +0 -1
  65. package/dist/drizzle/mysql.js +0 -123
  66. package/dist/drizzle/mysql.js.map +0 -1
  67. package/dist/drizzle/pg.d.ts +0 -963
  68. package/dist/drizzle/pg.d.ts.map +0 -1
  69. package/dist/drizzle/pg.js +0 -104
  70. package/dist/drizzle/pg.js.map +0 -1
  71. package/dist/drizzle/shared.d.ts +0 -16
  72. package/dist/drizzle/shared.d.ts.map +0 -1
  73. package/dist/drizzle/sqlite.d.ts +0 -1023
  74. package/dist/drizzle/sqlite.d.ts.map +0 -1
  75. package/dist/drizzle/sqlite.js +0 -107
  76. package/dist/drizzle/sqlite.js.map +0 -1
  77. package/dist/schema/v1.d.ts +0 -24
  78. package/dist/schema/v1.d.ts.map +0 -1
  79. package/dist/schema/v2.d.ts +0 -64
  80. package/dist/schema/v2.d.ts.map +0 -1
  81. package/prisma/dimah-s3.prisma +0 -80
  82. package/sql/mysql.sql +0 -59
  83. package/sql/pg.sql +0 -53
  84. package/sql/sqlite.sql +0 -54
  85. package/sql/upgrade/1.0.0-2.0.0/mysql.sql +0 -71
  86. package/sql/upgrade/1.0.0-2.0.0/pg.sql +0 -47
  87. package/sql/upgrade/1.0.0-2.0.0/sqlite.sql +0 -119
package/README.md CHANGED
@@ -1,10 +1,10 @@
1
1
  # @dimah-s3/db
2
2
 
3
- **2.0 beta.** Install with `@beta`. `@latest` stays on 1.x, and betas can include breaking changes until 2.0.0.
3
+ **2.0 beta.** Install with `@beta`. `@latest` stays on 1.x ([v1.s3.dimah.dev](https://v1.s3.dimah.dev/docs)), and betas can include breaking changes until 2.0.0.
4
4
 
5
5
  Optional database plugin for [dimah-s3](https://github.com/dimah-kz/dimah-s3). It tracks each upload in a `storage_object` table, binds uploads to the caller's scope, and adds logical folders, listings, and quotas.
6
6
 
7
- **Docs:** [Database Setup](https://v2.s3.dimah.dev/docs/db/setup) · [Lifecycle Hooks](https://v2.s3.dimah.dev/docs/db/lifecycle-hooks) · [API](https://v2.s3.dimah.dev/docs/db/api) · [Purge](https://v2.s3.dimah.dev/docs/db/purge) · [llms.txt](https://v2.s3.dimah.dev/llms.txt) · **Example:** [`examples/with-db`](../../examples/with-db)
7
+ **Docs:** [Database Setup](https://s3.dimah.dev/docs/db/setup) · [Lifecycle Hooks](https://s3.dimah.dev/docs/db/lifecycle-hooks) · [API](https://s3.dimah.dev/docs/db/api) · [Purge](https://s3.dimah.dev/docs/db/purge) · [llms.txt](https://s3.dimah.dev/llms.txt) · **Example:** [`examples/with-db`](../../examples/with-db)
8
8
 
9
9
  ## Install
10
10
 
@@ -12,18 +12,27 @@ Optional database plugin for [dimah-s3](https://github.com/dimah-kz/dimah-s3). I
12
12
  pnpm add @dimah-s3/db@beta @dimah-s3/server@beta
13
13
  ```
14
14
 
15
- You do not install `fumadb`: the adapters ship from `@dimah-s3/db/adapters/*`.
15
+ You do not install `fumadb`: the adapters ship from `@dimah-s3/db/adapters/*`, and the FumaDB CLI from `@dimah-s3/db/cli`.
16
16
 
17
17
  ## Tables
18
18
 
19
- Create the schema with one of:
19
+ `runCli(client)` from `@dimah-s3/db/cli` runs the FumaDB CLI for this schema:
20
20
 
21
- - Drizzle: add `dimahS3Tables` from `@dimah-s3/db/drizzle/{sqlite,pg,mysql}` to your Drizzle schema.
22
- - Prisma: copy [`prisma/dimah-s3.prisma`](./prisma/dimah-s3.prisma) into your schema.
23
- - Plain SQL: run [`sql/<dialect>.sql`](./sql).
24
- - Kysely: call `migrate(client)` from `@dimah-s3/db/cli`. It steps one schema version at a time, so the indexes are created too. The FumaDB CLI from `runCli` does the same only with `migrate:up`; its `migrate` command jumps straight to the latest schema and skips them.
21
+ - Drizzle or Prisma: `generate` writes the tables or models, with the indexes and the MySQL prefix lengths and collations. Apply them with `drizzle-kit` or Prisma Migrate. Generating does not connect to the database.
22
+ - Kysely: `migrate` creates the tables and the indexes, and so does `migrate(client)`.
25
23
 
26
- Upgrading from 1.x: run `sql/upgrade/1.0.0-2.0.0/<dialect>.sql`, or `migrate(client)` with Kysely. A database at an older schema throws on the first query; call `s3.db.checkSchema()` at startup to fail fast.
24
+ ```ts
25
+ // scripts/dimah-s3.ts — npx tsx scripts/dimah-s3.ts generate latest -o db/dimah-s3.ts
26
+ import { DimahS3DB } from "@dimah-s3/db";
27
+ import { drizzleAdapter } from "@dimah-s3/db/adapters/drizzle";
28
+ import { runCli } from "@dimah-s3/db/cli";
29
+
30
+ await runCli(
31
+ DimahS3DB.client(drizzleAdapter({ db: undefined, provider: "sqlite" })),
32
+ );
33
+ ```
34
+
35
+ The package reads schema `2.1.0` and does not upgrade a 1.x or `2.0.0` database: create the tables on a new database. A database at another schema version throws on the first query. Call `s3.db.checkSchema()` at startup to fail fast.
27
36
 
28
37
  ## Server
29
38
 
@@ -74,7 +83,7 @@ const list = useObjectList({
74
83
  });
75
84
  ```
76
85
 
77
- `dbObjectSource` lists one folder at a time. Uploads go to the open folder, and "New folder" calls `api.db.createFolder`. Pass `folders: false` for a flat list.
86
+ `dbObjectSource` lists one folder at a time. Uploads go to the open folder, and "New folder" calls `api.db.createFolder`. Rename calls `updateObject` or `updateFolder` and leaves the object key as it is. Pass `folders: false` for a flat list; file rename stays available.
78
87
 
79
88
  `api.db` also has `getObject`, `updateObject` (rename, move, or set `attributes`), and `deleteFolder`.
80
89
 
@@ -1,6 +1,6 @@
1
1
  /**
2
- * The FumaDB Drizzle adapter. Pass a Drizzle instance whose schema includes
3
- * `dimahS3Tables` from `@dimah-s3/db/drizzle/<dialect>`:
2
+ * The FumaDB Drizzle adapter. Pass a Drizzle instance whose relations include
3
+ * the tables `runCli` → `generate` writes:
4
4
  * `DimahS3DB.client(drizzleAdapter({ db, provider: "postgresql" }))`.
5
5
  */
6
6
  export { drizzleAdapter, type DrizzleConfig } from "fumadb/adapters/drizzle";
@@ -1,5 +1,5 @@
1
1
  /**
2
- * The FumaDB Kysely adapter. It can also migrate the schema:
2
+ * The FumaDB Kysely adapter. `migrate(client)` creates the schema through it:
3
3
  * `DimahS3DB.client(kyselyAdapter({ db, provider: "postgresql" }))`.
4
4
  */
5
5
  export { kyselyAdapter } from "fumadb/adapters/kysely";
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * The FumaDB Prisma adapter. Pass a Prisma client generated with the models
3
- * from `prisma/dimah-s3.prisma`:
3
+ * `runCli` → `generate` writes:
4
4
  * `DimahS3DB.client(prismaAdapter({ prisma, provider: "postgresql" }))`.
5
5
  */
6
6
  export { prismaAdapter, type PrismaConfig } from "fumadb/adapters/prisma";
package/dist/cli.d.ts CHANGED
@@ -1,26 +1,30 @@
1
- import { type DimahS3DbClient } from "./fuma-db";
1
+ import { type DimahS3DbClient } from "./fuma-db.js";
2
2
  /**
3
- * Runs the interactive FumaDB CLI: generate the ORM schema, or migrate when
4
- * the client uses the Kysely adapter. Prefer {@link migrate} to migrate:
5
- * only `migrate:up`, one version at a time, runs the steps 2.0.0 needs,
6
- * and no CLI command converts 1.x SQLite sizes.
3
+ * Runs the FumaDB CLI for the @dimah-s3/db schema. `generate` writes the
4
+ * Drizzle or Prisma schema for the client's adapter, and with Kysely, the SQL
5
+ * that creates the tables. `migrate` creates them with Kysely.
6
+ *
7
+ * Generating does not query the database, so the client can wrap an adapter
8
+ * that has no connection yet.
7
9
  *
8
10
  * ```ts
9
- * // scripts/db.ts
11
+ * // scripts/dimah-s3.mts
12
+ * import { DimahS3DB } from "@dimah-s3/db";
13
+ * import { drizzleAdapter } from "@dimah-s3/db/adapters/drizzle";
10
14
  * import { runCli } from "@dimah-s3/db/cli";
11
- * import { dimahS3Db } from "../lib/s3";
12
15
  *
13
- * await runCli(dimahS3Db);
16
+ * await runCli(DimahS3DB.client(drizzleAdapter({ db: undefined, provider: "postgresql" })));
14
17
  * ```
18
+ *
19
+ * `node scripts/dimah-s3.mts generate latest -o db/dimah-s3.ts`
15
20
  */
16
21
  export declare function runCli(client: DimahS3DbClient): Promise<void>;
17
22
  /**
18
- * Migrates the database to the schema this version needs, from an empty
19
- * database or from schema 1.0.0, one version at a time, so each version's
20
- * own steps run (the 2.0.0 indexes among them). An upgrade from 1.0.0 also
21
- * turns 1.x SQLite blob sizes into numbers, like
22
- * `sql/upgrade/1.0.0-2.0.0/sqlite.sql`. It needs the Kysely adapter; with
23
- * Drizzle or Prisma, use your ORM's migrations or the files in `sql/`.
23
+ * Creates the schema this package reads, with its indexes, on an empty
24
+ * database. It needs the Kysely adapter; with Drizzle or Prisma, generate the
25
+ * schema with {@link runCli} and apply it with your ORM. It does nothing on a
26
+ * database already at {@link DB_SCHEMA_VERSION}, and throws on a database at
27
+ * another version: this package does not upgrade one.
24
28
  *
25
29
  * ```ts
26
30
  * import { DimahS3DB } from "@dimah-s3/db";
package/dist/cli.d.ts.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"cli.d.ts","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":"AACA,OAAO,EAAqB,KAAK,eAAe,EAAE,MAAM,WAAW,CAAC;AAQpE;;;;;;;;;;;;;GAaG;AACH,wBAAgB,MAAM,CAAC,MAAM,EAAE,eAAe,GAAG,OAAO,CAAC,IAAI,CAAC,CAS7D;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAsB,OAAO,CAAC,MAAM,EAAE,eAAe,GAAG,OAAO,CAAC,IAAI,CAAC,CAOpE"}
1
+ {"version":3,"file":"cli.d.ts","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":"AACA,OAAO,EAAqB,KAAK,eAAe,EAAE,MAAM,WAAW,CAAC;AAIpE;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,MAAM,CAAC,MAAM,EAAE,eAAe,GAAG,OAAO,CAAC,IAAI,CAAC,CAS7D;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAsB,OAAO,CAAC,MAAM,EAAE,eAAe,GAAG,OAAO,CAAC,IAAI,CAAC,CAcpE"}
package/dist/cli.js CHANGED
@@ -3,200 +3,116 @@ import { fumadb } from 'fumadb';
3
3
  import { table, column, idColumn, schema } from 'fumadb/schema';
4
4
 
5
5
  // src/cli.ts
6
- var storageObject = table("storage_object", {
7
- id: idColumn("id", "varchar(255)").defaultTo$("auto"),
8
- scope: column("scope", "string"),
9
- bucket: column("bucket", "string"),
10
- key: column("key", "string"),
11
- route: column("route", "string"),
12
- contentType: column("content_type", "string").nullable(),
13
- size: column("size", "bigint").nullable(),
14
- eTag: column("e_tag", "string").nullable(),
15
- filename: column("filename", "string").nullable(),
16
- status: column("status", "string"),
17
- s3Metadata: column("metadata", "json").nullable(),
18
- acl: column("acl", "string").nullable(),
19
- uploadId: column("upload_id", "string").nullable(),
20
- declaredSize: column("declared_size", "bigint").nullable(),
21
- confirmedAt: column("confirmed_at", "timestamp").nullable(),
22
- expiresAt: column("expires_at", "timestamp").nullable(),
23
- createdAt: column("created_at", "timestamp").defaultTo$("now"),
24
- updatedAt: column("updated_at", "timestamp").defaultTo$("now"),
25
- deletedAt: column("deleted_at", "timestamp").nullable()
26
- }).unique("storage_object_bucket_key_uk", ["bucket", "key"]);
27
- var v1 = schema({
28
- version: "1.0.0",
29
- tables: {
30
- storageObject
31
- }
32
- });
33
- function byteCount(name) {
34
- return column(name, "bigint").nullable();
6
+ var BINARY = { mysql: "utf8mb4_bin" };
7
+ var TEXT = { mysql: "utf8mb4_unicode_ci" };
8
+ var SEARCH_NAME_MAX_LENGTH = 1024;
9
+ var LONG_PREFIX = 512;
10
+ var SCOPE_MAX_LENGTH = 255;
11
+ function string(name, length = 255) {
12
+ return column(name, `varchar(${length})`).collate(BINARY);
35
13
  }
36
- var storageObject2 = table("storage_object", {
37
- id: idColumn("id", "varchar(255)").defaultTo$("auto"),
14
+ var storageObject = table("storage_object", {
15
+ id: idColumn("id", "varchar(255)").collate(BINARY).defaultTo$("auto"),
38
16
  /** Owner, for example `user:123` or `org:acme`. */
39
- scope: column("scope", "string"),
40
- bucket: column("bucket", "string"),
41
- key: column("key", "string"),
17
+ scope: string("scope", SCOPE_MAX_LENGTH),
18
+ bucket: string("bucket"),
19
+ key: string("key", 1024),
42
20
  /** Named dimah-s3 route that created the object. */
43
- route: column("route", "string"),
44
- /** Logical folder path. `null` is the root. */
45
- folder: column("folder", "string").nullable(),
46
- filename: column("filename", "string"),
47
- contentType: column("content_type", "string").nullable(),
21
+ route: string("route"),
22
+ /** Folder that holds the object. `null` is the root. */
23
+ folderId: string("folder_id").nullable(),
24
+ filename: column("filename", "varchar(255)").collate(TEXT),
25
+ /**
26
+ * `filename` folded for search: lowercase, without accents, with `%`,
27
+ * `_`, and `\` kept literal. Search matches it on every database the
28
+ * same way.
29
+ */
30
+ searchName: string("search_name", SEARCH_NAME_MAX_LENGTH),
31
+ contentType: string("content_type").nullable(),
48
32
  /** Verified size from HeadObject, set on confirm. */
49
- size: byteCount("size"),
33
+ size: column("size", "bigint").nullable(),
50
34
  /** Size the client declared before confirm. Quota counts it while pending. */
51
- declaredSize: byteCount("declared_size"),
52
- eTag: column("e_tag", "string").nullable(),
53
- status: column("status", "string"),
35
+ declaredSize: column("declared_size", "bigint").nullable(),
36
+ eTag: string("e_tag").nullable(),
37
+ status: string("status", 16),
54
38
  /** S3 user metadata of the verified object. */
55
39
  s3Metadata: column("s3_metadata", "json").nullable(),
56
40
  /** App attributes, validated by the plugin's `attributes.schema`. */
57
41
  attributes: column("attributes", "json").nullable(),
58
- acl: column("acl", "string").nullable(),
42
+ acl: string("acl").nullable(),
59
43
  /** Multipart upload ID while the row is pending. */
60
- uploadId: column("upload_id", "string").nullable(),
44
+ uploadId: column("upload_id", "string").collate(BINARY).nullable(),
61
45
  /** Token of the upload ticket that wrote the row. Confirm and purge match it. */
62
- uploadToken: column("upload_token", "string").nullable(),
46
+ uploadToken: string("upload_token", 64).nullable(),
63
47
  /** Confirm deadline of a pending row (the upload ticket's expiry). */
64
48
  expiresAt: column("expires_at", "timestamp").nullable(),
65
49
  confirmedAt: column("confirmed_at", "timestamp").nullable(),
66
50
  createdAt: column("created_at", "timestamp").defaultTo$("now"),
67
51
  updatedAt: column("updated_at", "timestamp").defaultTo$("now"),
68
52
  deletedAt: column("deleted_at", "timestamp").nullable()
69
- }).unique("storage_object_bucket_key_uk", ["bucket", "key"]);
53
+ }).unique("storage_object_bucket_key_uk", ["bucket", "key"], {
54
+ length: { key: LONG_PREFIX }
55
+ }).index("storage_object_scope_folder_idx", [
56
+ "scope",
57
+ "status",
58
+ "folderId",
59
+ "createdAt"
60
+ ]).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"]);
70
61
  var storageFolder = table("storage_folder", {
71
- id: idColumn("id", "varchar(255)").defaultTo$("auto"),
72
- scope: column("scope", "string"),
73
- /** Full folder path. */
74
- path: column("path", "string"),
75
- /** Path of the containing folder. `null` is the root. */
76
- parent: column("parent", "string").nullable(),
62
+ id: idColumn("id", "varchar(255)").collate(BINARY).defaultTo$("auto"),
63
+ scope: string("scope", SCOPE_MAX_LENGTH),
64
+ /** Id of the containing folder. `""` is the root. */
65
+ parentId: string("parent_id").defaultTo(""),
77
66
  /** Last segment of `path`. */
78
- name: column("name", "string"),
79
- createdAt: column("created_at", "timestamp").defaultTo$("now")
80
- }).unique("storage_folder_scope_path_uk", ["scope", "path"]);
81
- var v2Indexes = [
82
- {
83
- name: "storage_object_scope_folder_idx",
84
- table: "storage_object",
85
- columns: ["scope", "status", "folder", "created_at"]
86
- },
87
- {
88
- name: "storage_object_scope_updated_idx",
89
- table: "storage_object",
90
- columns: ["scope", "status", "updated_at"]
91
- },
92
- {
93
- name: "storage_object_scope_filename_idx",
94
- table: "storage_object",
95
- columns: ["scope", "status", "filename"]
96
- },
97
- {
98
- name: "storage_object_scope_size_idx",
99
- table: "storage_object",
100
- columns: ["scope", "status", "size"]
101
- },
102
- {
103
- name: "storage_object_status_expires_idx",
104
- table: "storage_object",
105
- columns: ["status", "expires_at"]
106
- },
107
- {
108
- name: "storage_folder_scope_parent_idx",
109
- table: "storage_folder",
110
- columns: ["scope", "parent"]
111
- }
112
- ];
113
- var v1Indexes = [
114
- "storage_object_scope_status_created_idx",
115
- "storage_object_scope_route_idx",
116
- "storage_object_status_created_idx"
117
- ];
118
- var custom = (sql) => ({ type: "custom", sql });
119
- var keyName = `COALESCE(NULLIF(substr("key", length(rtrim("key", replace("key", '/', ''))) + 1), ''), "key")`;
120
- var beforeV2 = [
121
- custom(
122
- `UPDATE "storage_object" SET "filename" = ${keyName} WHERE "filename" IS NULL`
123
- )
124
- ];
125
- var afterV2 = [
126
- ...v1Indexes.map((name) => custom(`DROP INDEX IF EXISTS "${name}"`)),
127
- ...v2Indexes.map((index) => {
128
- const columns = index.columns.map((name) => `"${name}"`).join(", ");
129
- return custom(
130
- `CREATE INDEX IF NOT EXISTS "${index.name}" ON "${index.table}" (${columns})`
131
- );
132
- })
133
- ];
134
- var v2 = schema({
135
- version: "2.0.0",
67
+ name: string("name"),
68
+ /** `name` folded for search, like `storage_object.search_name`. */
69
+ searchName: string("search_name", SEARCH_NAME_MAX_LENGTH),
70
+ /** Full folder path. */
71
+ path: string("path", 1024),
72
+ createdAt: column("created_at", "timestamp").defaultTo$("now"),
73
+ updatedAt: column("updated_at", "timestamp").defaultTo$("now")
74
+ }).unique("storage_folder_scope_path_uk", ["scope", "path"], {
75
+ length: { path: LONG_PREFIX }
76
+ }).unique("storage_folder_scope_parent_name_uk", ["scope", "parentId", "name"]);
77
+ var dbSchema = schema({
78
+ version: "2.2.0",
136
79
  tables: {
137
- storageObject: storageObject2,
80
+ storageObject,
138
81
  storageFolder
139
- },
140
- async up({ auto }) {
141
- return [...beforeV2, ...await auto(), ...afterV2];
142
82
  }
143
83
  });
144
84
 
145
85
  // src/fuma-db.ts
146
86
  fumadb({
147
87
  namespace: "dimah_s3",
148
- schemas: [v1, v2]
88
+ schemas: [dbSchema]
149
89
  });
150
- var DB_SCHEMA_VERSION = v2.version;
90
+ var DB_SCHEMA_VERSION = dbSchema.version;
151
91
 
152
92
  // src/cli.ts
153
- var MIGRATE_BATCH = 500;
154
93
  function runCli(client) {
155
94
  const { main } = createCli({
156
95
  db: client,
157
96
  command: "dimah-s3-db",
158
97
  description: "FumaDB CLI for @dimah-s3/db",
159
- version: "2.0.0-beta.0"
98
+ version: "2.0.0-beta.10"
160
99
  });
161
100
  return main();
162
101
  }
163
102
  async function migrate(client) {
164
- const migrator = client.createMigrator();
165
- for (let next = await migrator.next(); next; next = await migrator.next()) {
166
- const from = await migrator.getVersion();
167
- await (await migrator.up()).execute();
168
- if (from === v1.version) await rewriteSizes(client);
103
+ let version;
104
+ try {
105
+ version = await client.version();
106
+ } catch {
107
+ version = void 0;
169
108
  }
170
- }
171
- async function rewriteSizes(client) {
172
- const orm = client.orm(DB_SCHEMA_VERSION);
173
- const toNumber = (value) => value === null ? null : Number(value);
174
- let after;
175
- for (; ; ) {
176
- const rows = await orm.findMany("storageObject", {
177
- select: ["id", "size", "declaredSize"],
178
- where: (b) => b.and(
179
- b.or(b.isNotNull("size"), b.isNotNull("declaredSize")),
180
- after === void 0 || b("id", ">", after)
181
- ),
182
- orderBy: [["id", "asc"]],
183
- limit: MIGRATE_BATCH
184
- });
185
- for (const row of rows) {
186
- if (typeof row.size !== "bigint" && typeof row.declaredSize !== "bigint") {
187
- continue;
188
- }
189
- await orm.updateMany("storageObject", {
190
- where: (b) => b("id", "=", row.id),
191
- set: {
192
- size: toNumber(row.size),
193
- declaredSize: toNumber(row.declaredSize)
194
- }
195
- });
196
- }
197
- if (rows.length < MIGRATE_BATCH) return;
198
- after = rows.at(-1)?.id;
109
+ if (version === DB_SCHEMA_VERSION) return;
110
+ if (version !== void 0) {
111
+ throw new Error(
112
+ `[dimah-s3/db] The database is at schema ${version}, which this package does not upgrade. Create schema ${DB_SCHEMA_VERSION} on a new database.`
113
+ );
199
114
  }
115
+ await (await client.createMigrator().migrateToLatest()).execute();
200
116
  }
201
117
 
202
118
  export { migrate, runCli };
package/dist/cli.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/schema/v1.ts","../src/schema/v2.ts","../src/fuma-db.ts","../src/cli.ts"],"names":["column","storageObject","table","idColumn","schema"],"mappings":";;;;;AAUA,IAAM,aAAA,GAAgB,MAAM,gBAAA,EAAkB;AAAA,EAC5C,IAAI,QAAA,CAAS,IAAA,EAAM,cAAc,CAAA,CAAE,WAAW,MAAM,CAAA;AAAA,EACpD,KAAA,EAAO,MAAA,CAAO,OAAA,EAAS,QAAQ,CAAA;AAAA,EAC/B,MAAA,EAAQ,MAAA,CAAO,QAAA,EAAU,QAAQ,CAAA;AAAA,EACjC,GAAA,EAAK,MAAA,CAAO,KAAA,EAAO,QAAQ,CAAA;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tB,MAAA,IACE,OAAO,GAAA,CAAI,IAAA,KAAS,YACpB,OAAO,GAAA,CAAI,iBAAiB,QAAA,EAC5B;AACA,QAAA;AAAA,MACF;AACA,MAAA,MAAM,GAAA,CAAI,WAAW,eAAA,EAAiB;AAAA,QACpC,OAAO,CAAC,CAAA,KAAM,EAAE,IAAA,EAAM,GAAA,EAAK,IAAI,EAAE,CAAA;AAAA,QACjC,GAAA,EAAK;AAAA,UACH,IAAA,EAAM,QAAA,CAAS,GAAA,CAAI,IAAI,CAAA;AAAA,UACvB,YAAA,EAAc,QAAA,CAAS,GAAA,CAAI,YAAY;AAAA;AACzC,OACD,CAAA;AAAA,IACH;AACA,IAAA,IAAI,IAAA,CAAK,SAAS,aAAA,EAAe;AACjC,IAAA,KAAA,GAAQ,IAAA,CAAK,EAAA,CAAG,EAAE,CAAA,EAAG,EAAA;AAAA,EACvB;AACF","file":"cli.js","sourcesContent":["import { column, idColumn, schema, table } from \"fumadb/schema\";\n\n/**\n * Schema 1.0.0, kept so the FumaDB migrator can move a 1.x database to\n * 2.0.0 (`v2.ts`). Nothing queries it.\n *\n * The `metadata` column has the ORM name `s3Metadata`, the name it has in\n * 2.0.0, so the migrator renames the column (and SQLite's table rebuild\n * copies it) instead of dropping its data.\n */\nconst storageObject = table(\"storage_object\", {\n id: idColumn(\"id\", \"varchar(255)\").defaultTo$(\"auto\"),\n scope: column(\"scope\", \"string\"),\n bucket: column(\"bucket\", \"string\"),\n key: column(\"key\", \"string\"),\n route: column(\"route\", \"string\"),\n contentType: column(\"content_type\", \"string\").nullable(),\n size: column(\"size\", \"bigint\").nullable(),\n eTag: column(\"e_tag\", \"string\").nullable(),\n filename: column(\"filename\", \"string\").nullable(),\n status: column(\"status\", \"string\"),\n s3Metadata: column(\"metadata\", \"json\").nullable(),\n acl: column(\"acl\", \"string\").nullable(),\n uploadId: column(\"upload_id\", \"string\").nullable(),\n declaredSize: column(\"declared_size\", \"bigint\").nullable(),\n confirmedAt: column(\"confirmed_at\", \"timestamp\").nullable(),\n expiresAt: column(\"expires_at\", \"timestamp\").nullable(),\n createdAt: column(\"created_at\", \"timestamp\").defaultTo$(\"now\"),\n updatedAt: column(\"updated_at\", \"timestamp\").defaultTo$(\"now\"),\n deletedAt: column(\"deleted_at\", \"timestamp\").nullable(),\n}).unique(\"storage_object_bucket_key_uk\", [\"bucket\", \"key\"]);\n\nexport const v1 = schema({\n version: \"1.0.0\",\n tables: {\n storageObject,\n },\n});\n","import { column, idColumn, schema, table, type Column } from \"fumadb/schema\";\n\n/**\n * A byte count. The column is a `bigint` (int8 on PostgreSQL and MySQL), but\n * values are bound as numbers so SQLite stores a number instead of FumaDB's\n * 8-byte blob, which would sort after every number: INTEGER in the shipped\n * SQL, or REAL in a column FumaDB created, compared by value either way.\n * Drivers read int8 back as a number, a bigint, or a string, and a 1.x\n * SQLite row not yet migrated still holds a blob (read as a bigint).\n */\nfunction byteCount(name: string) {\n return column(name, \"bigint\").nullable() as unknown as Column<\n \"bigint\",\n number | null,\n number | bigint | string | null\n >;\n}\n\n/**\n * One row per tracked object:\n * `pending` (presigned) → `active` (confirmed) → `deleted` (soft delete).\n * `purging` marks a stale pending row that purge has claimed.\n */\nconst storageObject = table(\"storage_object\", {\n id: idColumn(\"id\", \"varchar(255)\").defaultTo$(\"auto\"),\n /** Owner, for example `user:123` or `org:acme`. */\n scope: column(\"scope\", \"string\"),\n bucket: column(\"bucket\", \"string\"),\n key: column(\"key\", \"string\"),\n /** Named dimah-s3 route that created the object. */\n route: column(\"route\", \"string\"),\n /** Logical folder path. `null` is the root. */\n folder: column(\"folder\", \"string\").nullable(),\n filename: column(\"filename\", \"string\"),\n contentType: column(\"content_type\", \"string\").nullable(),\n /** Verified size from HeadObject, set on confirm. */\n size: byteCount(\"size\"),\n /** Size the client declared before confirm. Quota counts it while pending. */\n declaredSize: byteCount(\"declared_size\"),\n eTag: column(\"e_tag\", \"string\").nullable(),\n status: column(\"status\", \"string\"),\n /** S3 user metadata of the verified object. */\n s3Metadata: column(\"s3_metadata\", \"json\").nullable(),\n /** App attributes, validated by the plugin's `attributes.schema`. */\n attributes: column(\"attributes\", \"json\").nullable(),\n acl: column(\"acl\", \"string\").nullable(),\n /** Multipart upload ID while the row is pending. */\n uploadId: column(\"upload_id\", \"string\").nullable(),\n /** Token of the upload ticket that wrote the row. Confirm and purge match it. */\n uploadToken: column(\"upload_token\", \"string\").nullable(),\n /** Confirm deadline of a pending row (the upload ticket's expiry). */\n expiresAt: column(\"expires_at\", \"timestamp\").nullable(),\n confirmedAt: column(\"confirmed_at\", \"timestamp\").nullable(),\n createdAt: column(\"created_at\", \"timestamp\").defaultTo$(\"now\"),\n updatedAt: column(\"updated_at\", \"timestamp\").defaultTo$(\"now\"),\n deletedAt: column(\"deleted_at\", \"timestamp\").nullable(),\n}).unique(\"storage_object_bucket_key_uk\", [\"bucket\", \"key\"]);\n\n/** One row per folder, so an empty folder still lists. */\nconst storageFolder = table(\"storage_folder\", {\n id: idColumn(\"id\", \"varchar(255)\").defaultTo$(\"auto\"),\n scope: column(\"scope\", \"string\"),\n /** Full folder path. */\n path: column(\"path\", \"string\"),\n /** Path of the containing folder. `null` is the root. */\n parent: column(\"parent\", \"string\").nullable(),\n /** Last segment of `path`. */\n name: column(\"name\", \"string\"),\n createdAt: column(\"created_at\", \"timestamp\").defaultTo$(\"now\"),\n}).unique(\"storage_folder_scope_path_uk\", [\"scope\", \"path\"]);\n\n/** A secondary index, by SQL table and column names. */\nexport type SchemaIndex = {\n name: string;\n table: \"storage_object\" | \"storage_folder\";\n columns: readonly string[];\n};\n\n/**\n * Secondary indexes of schema 2.0.0. FumaDB cannot declare them, so the SQL\n * files, the Drizzle tables, and the Prisma schema carry them.\n */\nexport const v2Indexes: readonly SchemaIndex[] = [\n {\n name: \"storage_object_scope_folder_idx\",\n table: \"storage_object\",\n columns: [\"scope\", \"status\", \"folder\", \"created_at\"],\n },\n {\n name: \"storage_object_scope_updated_idx\",\n table: \"storage_object\",\n columns: [\"scope\", \"status\", \"updated_at\"],\n },\n {\n name: \"storage_object_scope_filename_idx\",\n table: \"storage_object\",\n columns: [\"scope\", \"status\", \"filename\"],\n },\n {\n name: \"storage_object_scope_size_idx\",\n table: \"storage_object\",\n columns: [\"scope\", \"status\", \"size\"],\n },\n {\n name: \"storage_object_status_expires_idx\",\n table: \"storage_object\",\n columns: [\"status\", \"expires_at\"],\n },\n {\n name: \"storage_folder_scope_parent_idx\",\n table: \"storage_folder\",\n columns: [\"scope\", \"parent\"],\n },\n];\n\n/** The secondary indexes 1.x recommended that 2.0.0 replaces. */\nconst v1Indexes = [\n \"storage_object_scope_status_created_idx\",\n \"storage_object_scope_route_idx\",\n \"storage_object_status_created_idx\",\n];\n\nconst custom = (sql: string) => ({ type: \"custom\" as const, sql });\n\n/**\n * The last segment of `key`, or the whole key when it ends in `/`, in syntax\n * SQLite and PostgreSQL share. Trimming every character but `/` from the\n * right leaves the key up to its last `/`.\n */\nconst keyName = `COALESCE(NULLIF(substr(\"key\", length(rtrim(\"key\", replace(\"key\", '/', ''))) + 1), ''), \"key\")`;\n\n/**\n * Runs before the statements FumaDB generates for 1.0.0 → 2.0.0, in syntax\n * SQLite and PostgreSQL share. `filename` becomes required, so a missing one\n * is filled from the key the way `sql/upgrade/1.0.0-2.0.0/<dialect>.sql`\n * does. `migrate()` converts 1.x SQLite blob sizes afterwards.\n */\nconst beforeV2 = [\n custom(\n `UPDATE \"storage_object\" SET \"filename\" = ${keyName} WHERE \"filename\" IS NULL`,\n ),\n];\n\n/** Runs after them: the 1.x recommended indexes give way to {@link v2Indexes}. */\nconst afterV2 = [\n ...v1Indexes.map((name) => custom(`DROP INDEX IF EXISTS \"${name}\"`)),\n ...v2Indexes.map((index) => {\n const columns = index.columns.map((name) => `\"${name}\"`).join(\", \");\n return custom(\n `CREATE INDEX IF NOT EXISTS \"${index.name}\" ON \"${index.table}\" (${columns})`,\n );\n }),\n];\n\n/**\n * Schema 2.0.0: logical folders (`folder` plus `storage_folder`), S3 metadata\n * split from typed `attributes`, the upload token, and the sort indexes.\n */\nexport const v2 = schema({\n version: \"2.0.0\",\n tables: {\n storageObject,\n storageFolder,\n },\n async up({ auto }) {\n return [...beforeV2, ...(await auto()), ...afterV2];\n },\n});\n","import { fumadb, type InferFumaDB } from \"fumadb\";\nimport { v1 } from \"./schema/v1\";\nimport { v2 } from \"./schema/v2\";\n\n/**\n * The @dimah-s3/db FumaDB library. Call `DimahS3DB.client(adapter)` with an\n * adapter from `@dimah-s3/db/adapters/*` to get a {@link DimahS3DbClient}.\n */\nexport const DimahS3DB = fumadb({\n namespace: \"dimah_s3\",\n schemas: [v1, v2],\n});\n\n/** FumaDB client for the @dimah-s3/db schemas. */\nexport type DimahS3DbClient = InferFumaDB<typeof DimahS3DB>;\n\n/** The schema version the stores read and write. */\nexport const DB_SCHEMA_VERSION = v2.version;\n\nexport { v2 };\n","import { createCli } from \"fumadb/cli\";\nimport { DB_SCHEMA_VERSION, type DimahS3DbClient } from \"./fuma-db\";\nimport { v1 } from \"./schema/v1\";\n\ndeclare const __DIMAH_S3_DB_VERSION__: string;\n\n/** Rows `migrate` rewrites per query. */\nconst MIGRATE_BATCH = 500;\n\n/**\n * Runs the interactive FumaDB CLI: generate the ORM schema, or migrate when\n * the client uses the Kysely adapter. Prefer {@link migrate} to migrate:\n * only `migrate:up`, one version at a time, runs the steps 2.0.0 needs,\n * and no CLI command converts 1.x SQLite sizes.\n *\n * ```ts\n * // scripts/db.ts\n * import { runCli } from \"@dimah-s3/db/cli\";\n * import { dimahS3Db } from \"../lib/s3\";\n *\n * await runCli(dimahS3Db);\n * ```\n */\nexport function runCli(client: DimahS3DbClient): Promise<void> {\n const { main } = createCli({\n db: client,\n command: \"dimah-s3-db\",\n description: \"FumaDB CLI for @dimah-s3/db\",\n version: __DIMAH_S3_DB_VERSION__,\n });\n\n return main();\n}\n\n/**\n * Migrates the database to the schema this version needs, from an empty\n * database or from schema 1.0.0, one version at a time, so each version's\n * own steps run (the 2.0.0 indexes among them). An upgrade from 1.0.0 also\n * turns 1.x SQLite blob sizes into numbers, like\n * `sql/upgrade/1.0.0-2.0.0/sqlite.sql`. It needs the Kysely adapter; with\n * Drizzle or Prisma, use your ORM's migrations or the files in `sql/`.\n *\n * ```ts\n * import { DimahS3DB } from \"@dimah-s3/db\";\n * import { kyselyAdapter } from \"@dimah-s3/db/adapters/kysely\";\n * import { migrate } from \"@dimah-s3/db/cli\";\n *\n * await migrate(DimahS3DB.client(kyselyAdapter({ db: kysely, provider: \"postgresql\" })));\n * ```\n */\nexport async function migrate(client: DimahS3DbClient): Promise<void> {\n const migrator = client.createMigrator();\n for (let next = await migrator.next(); next; next = await migrator.next()) {\n const from = await migrator.getVersion();\n await (await migrator.up()).execute();\n if (from === v1.version) await rewriteSizes(client);\n }\n}\n\n/**\n * Rewrites the sizes 1.x stored as FumaDB bigints. SQLite kept those as\n * 8-byte blobs, which sort after every number. FumaDB reads a blob back as\n * a bigint and stores a number as a number.\n */\nasync function rewriteSizes(client: DimahS3DbClient): Promise<void> {\n const orm = client.orm(DB_SCHEMA_VERSION);\n const toNumber = (value: number | bigint | string | null) =>\n value === null ? null : Number(value);\n let after: string | undefined;\n for (;;) {\n const rows = await orm.findMany(\"storageObject\", {\n select: [\"id\", \"size\", \"declaredSize\"],\n where: (b) =>\n b.and(\n b.or(b.isNotNull(\"size\"), b.isNotNull(\"declaredSize\")),\n after === undefined || b(\"id\", \">\", after),\n ),\n orderBy: [[\"id\", \"asc\"]],\n limit: MIGRATE_BATCH,\n });\n for (const row of rows) {\n if (\n typeof row.size !== \"bigint\" &&\n typeof row.declaredSize !== \"bigint\"\n ) {\n continue;\n }\n await orm.updateMany(\"storageObject\", {\n where: (b) => b(\"id\", \"=\", row.id),\n set: {\n size: toNumber(row.size),\n declaredSize: toNumber(row.declaredSize),\n },\n });\n }\n if (rows.length < MIGRATE_BATCH) return;\n after = rows.at(-1)?.id;\n }\n}\n"]}
1
+ {"version":3,"sources":["../src/schema/schema.ts","../src/fuma-db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AAA,IACN,aAAA;AAAA,IACA;AAAA;AAEJ,CAAC,CAAA;;;AC1HwB,MAAA,CAAO;AAAA,EAC9B,SAAA,EAAW,UAAA;AAAA,EACX,OAAA,EAAS,CAAC,QAAQ;AACpB,CAAC;AAMM,IAAM,oBAAoB,QAAA,CAAS,OAAA;;;ACQnC,SAAS,OAAO,MAAA,EAAwC;AAC7D,EAAA,MAAM,EAAE,IAAA,EAAK,GAAI,SAAA,CAAU;AAAA,IACzB,EAAA,EAAI,MAAA;AAAA,IACJ,OAAA,EAAS,aAAA;AAAA,IACT,WAAA,EAAa,6BAAA;AAAA,IACb,OAAA,EAAS;AAAA,GACV,CAAA;AAED,EAAA,OAAO,IAAA,EAAK;AACd;AAiBA,eAAsB,QAAQ,MAAA,EAAwC;AACpE,EAAA,IAAI,OAAA;AACJ,EAAA,IAAI;AACF,IAAA,OAAA,GAAU,MAAM,OAAO,OAAA,EAAQ;AAAA,EACjC,CAAA,CAAA,MAAQ;AACN,IAAA,OAAA,GAAU,MAAA;AAAA,EACZ;AACA,EAAA,IAAI,YAAY,iBAAA,EAAmB;AACnC,EAAA,IAAI,YAAY,MAAA,EAAW;AACzB,IAAA,MAAM,IAAI,KAAA;AAAA,MACR,CAAA,wCAAA,EAA2C,OAAO,CAAA,qDAAA,EAAwD,iBAAiB,CAAA,mBAAA;AAAA,KAC7H;AAAA,EACF;AACA,EAAA,MAAA,CAAO,MAAM,MAAA,CAAO,cAAA,EAAe,CAAE,eAAA,IAAmB,OAAA,EAAQ;AAClE","file":"cli.js","sourcesContent":["import { column, idColumn, schema, table } from \"fumadb/schema\";\n\n/**\n * MySQL collation of every text column but `filename`: keys, scopes, ids, and\n * upload tokens compare byte for byte.\n */\nconst BINARY = { mysql: \"utf8mb4_bin\" };\n\n/** MySQL collation of `filename`: case-insensitive, for the name sort. */\nconst TEXT = { mysql: \"utf8mb4_unicode_ci\" };\n\n/**\n * Longest `search_name`, in characters. Folding can lengthen a name (`ß`\n * becomes `ss`), so it has more room than the 255-character name.\n */\nexport const SEARCH_NAME_MAX_LENGTH = 1024;\n\n/**\n * InnoDB indexes at most 3072 bytes, so S3 keys and folder paths are indexed\n * on their first 512 characters on MySQL. Two values that share those\n * characters share an index entry, and the unique keys treat them as equal.\n */\nconst LONG_PREFIX = 512;\n\n/**\n * Longest scope `resolveScope` may return, in characters. The `scope`\n * columns are `varchar(255)`.\n */\nexport const SCOPE_MAX_LENGTH = 255;\n\nfunction string(name: string, length = 255) {\n return column(name, `varchar(${length})`).collate(BINARY);\n}\n\n/**\n * One row per tracked object:\n * `pending` (presigned) → `active` (confirmed) → `deleted` (soft delete).\n * `purging` marks a stale pending row that purge has claimed.\n */\nconst storageObject = table(\"storage_object\", {\n id: idColumn(\"id\", \"varchar(255)\").collate(BINARY).defaultTo$(\"auto\"),\n /** Owner, for example `user:123` or `org:acme`. */\n scope: string(\"scope\", SCOPE_MAX_LENGTH),\n bucket: string(\"bucket\"),\n key: string(\"key\", 1024),\n /** Named dimah-s3 route that created the object. */\n route: string(\"route\"),\n /** Folder that holds the object. `null` is the root. */\n folderId: string(\"folder_id\").nullable(),\n filename: column(\"filename\", \"varchar(255)\").collate(TEXT),\n /**\n * `filename` folded for search: lowercase, without accents, with `%`,\n * `_`, and `\\` kept literal. Search matches it on every database the\n * same way.\n */\n searchName: string(\"search_name\", SEARCH_NAME_MAX_LENGTH),\n contentType: string(\"content_type\").nullable(),\n /** Verified size from HeadObject, set on confirm. */\n size: column(\"size\", \"bigint\").nullable(),\n /** Size the client declared before confirm. Quota counts it while pending. */\n declaredSize: column(\"declared_size\", \"bigint\").nullable(),\n eTag: string(\"e_tag\").nullable(),\n status: string(\"status\", 16),\n /** S3 user metadata of the verified object. */\n s3Metadata: column(\"s3_metadata\", \"json\").nullable(),\n /** App attributes, validated by the plugin's `attributes.schema`. */\n attributes: column(\"attributes\", \"json\").nullable(),\n acl: string(\"acl\").nullable(),\n /** Multipart upload ID while the row is pending. */\n uploadId: column(\"upload_id\", \"string\").collate(BINARY).nullable(),\n /** Token of the upload ticket that wrote the row. Confirm and purge match it. */\n uploadToken: string(\"upload_token\", 64).nullable(),\n /** Confirm deadline of a pending row (the upload ticket's expiry). */\n expiresAt: column(\"expires_at\", \"timestamp\").nullable(),\n confirmedAt: column(\"confirmed_at\", \"timestamp\").nullable(),\n createdAt: column(\"created_at\", \"timestamp\").defaultTo$(\"now\"),\n updatedAt: column(\"updated_at\", \"timestamp\").defaultTo$(\"now\"),\n deletedAt: column(\"deleted_at\", \"timestamp\").nullable(),\n})\n .unique(\"storage_object_bucket_key_uk\", [\"bucket\", \"key\"], {\n length: { key: LONG_PREFIX },\n })\n .index(\"storage_object_scope_folder_idx\", [\n \"scope\",\n \"status\",\n \"folderId\",\n \"createdAt\",\n ])\n .index(\"storage_object_scope_updated_idx\", [\"scope\", \"status\", \"updatedAt\"])\n .index(\"storage_object_scope_filename_idx\", [\"scope\", \"status\", \"filename\"])\n .index(\"storage_object_scope_size_idx\", [\"scope\", \"status\", \"size\"])\n .index(\"storage_object_status_expires_idx\", [\"status\", \"expiresAt\"]);\n\n/**\n * One row per folder, so an empty folder still lists. `parentId` is `\"\"` at\n * the root: a nullable parent cannot be unique on PostgreSQL, MySQL, and\n * SQLite, and the root is not a row. `path` is a cache of `name` joined\n * through the parents, rewritten when a folder is renamed or moved.\n */\nconst storageFolder = table(\"storage_folder\", {\n id: idColumn(\"id\", \"varchar(255)\").collate(BINARY).defaultTo$(\"auto\"),\n scope: string(\"scope\", SCOPE_MAX_LENGTH),\n /** Id of the containing folder. `\"\"` is the root. */\n parentId: string(\"parent_id\").defaultTo(\"\"),\n /** Last segment of `path`. */\n name: string(\"name\"),\n /** `name` folded for search, like `storage_object.search_name`. */\n searchName: string(\"search_name\", SEARCH_NAME_MAX_LENGTH),\n /** Full folder path. */\n path: string(\"path\", 1024),\n createdAt: column(\"created_at\", \"timestamp\").defaultTo$(\"now\"),\n updatedAt: column(\"updated_at\", \"timestamp\").defaultTo$(\"now\"),\n})\n .unique(\"storage_folder_scope_path_uk\", [\"scope\", \"path\"], {\n length: { path: LONG_PREFIX },\n })\n .unique(\"storage_folder_scope_parent_name_uk\", [\"scope\", \"parentId\", \"name\"]);\n\n/**\n * The @dimah-s3/db schema, version `2.2.0`. The tables, their indexes, and\n * the MySQL collations and prefix lengths are all declared here, so FumaDB\n * creates them (`migrate`) and generates them for Drizzle and Prisma.\n */\nexport const dbSchema = schema({\n version: \"2.2.0\",\n tables: {\n storageObject,\n storageFolder,\n },\n});\n","import { fumadb, type InferFumaDB } from \"fumadb\";\nimport { dbSchema } from \"./schema/schema\";\n\n/**\n * The @dimah-s3/db FumaDB library. Call `DimahS3DB.client(adapter)` with an\n * adapter from `@dimah-s3/db/adapters/*` to get a {@link DimahS3DbClient}.\n */\nexport const DimahS3DB = fumadb({\n namespace: \"dimah_s3\",\n schemas: [dbSchema],\n});\n\n/** FumaDB client for the @dimah-s3/db schema. */\nexport type DimahS3DbClient = InferFumaDB<typeof DimahS3DB>;\n\n/** The schema version the stores read and write. */\nexport const DB_SCHEMA_VERSION = dbSchema.version;\n","import { createCli } from \"fumadb/cli\";\nimport { DB_SCHEMA_VERSION, type DimahS3DbClient } from \"./fuma-db\";\n\ndeclare const __DIMAH_S3_DB_VERSION__: string;\n\n/**\n * Runs the FumaDB CLI for the @dimah-s3/db schema. `generate` writes the\n * Drizzle or Prisma schema for the client's adapter, and with Kysely, the SQL\n * that creates the tables. `migrate` creates them with Kysely.\n *\n * Generating does not query the database, so the client can wrap an adapter\n * that has no connection yet.\n *\n * ```ts\n * // scripts/dimah-s3.mts\n * import { DimahS3DB } from \"@dimah-s3/db\";\n * import { drizzleAdapter } from \"@dimah-s3/db/adapters/drizzle\";\n * import { runCli } from \"@dimah-s3/db/cli\";\n *\n * await runCli(DimahS3DB.client(drizzleAdapter({ db: undefined, provider: \"postgresql\" })));\n * ```\n *\n * `node scripts/dimah-s3.mts generate latest -o db/dimah-s3.ts`\n */\nexport function runCli(client: DimahS3DbClient): Promise<void> {\n const { main } = createCli({\n db: client,\n command: \"dimah-s3-db\",\n description: \"FumaDB CLI for @dimah-s3/db\",\n version: __DIMAH_S3_DB_VERSION__,\n });\n\n return main();\n}\n\n/**\n * Creates the schema this package reads, with its indexes, on an empty\n * database. It needs the Kysely adapter; with Drizzle or Prisma, generate the\n * schema with {@link runCli} and apply it with your ORM. It does nothing on a\n * database already at {@link DB_SCHEMA_VERSION}, and throws on a database at\n * another version: this package does not upgrade one.\n *\n * ```ts\n * import { DimahS3DB } from \"@dimah-s3/db\";\n * import { kyselyAdapter } from \"@dimah-s3/db/adapters/kysely\";\n * import { migrate } from \"@dimah-s3/db/cli\";\n *\n * await migrate(DimahS3DB.client(kyselyAdapter({ db: kysely, provider: \"postgresql\" })));\n * ```\n */\nexport async function migrate(client: DimahS3DbClient): Promise<void> {\n let version: string | undefined;\n try {\n version = await client.version();\n } catch {\n version = undefined;\n }\n if (version === DB_SCHEMA_VERSION) return;\n if (version !== undefined) {\n throw new Error(\n `[dimah-s3/db] The database is at schema ${version}, which this package does not upgrade. Create schema ${DB_SCHEMA_VERSION} on a new database.`,\n );\n }\n await (await client.createMigrator().migrateToLatest()).execute();\n}\n"]}
@@ -12,7 +12,7 @@ import type { DbClientActions } from "../types/client.js";
12
12
  * plugins: [dbClient()],
13
13
  * });
14
14
  *
15
- * const page = await s3Client.db.listObjects({ folder: "" });
15
+ * const page = await s3Client.db.listObjects({ folderId: "" });
16
16
  * ```
17
17
  *
18
18
  * The server resolves the scope from the request, so no action takes one.
@@ -1 +1 @@
1
- {"version":3,"file":"db-client.d.ts","sourceRoot":"","sources":["../../src/client/db-client.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EACV,eAAe,EAKhB,MAAM,gBAAgB,CAAC;AAaxB;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,QAAQ,CAAC,iBAAiB,GAAG,KAAK,+LAkCjD"}
1
+ {"version":3,"file":"db-client.d.ts","sourceRoot":"","sources":["../../src/client/db-client.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EACV,eAAe,EAKhB,MAAM,gBAAgB,CAAC;AAaxB;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,QAAQ,CAAC,iBAAiB,GAAG,KAAK,+LAwCjD"}
@@ -1,18 +1,24 @@
1
1
  import type { DbClientActions, DbObjectSource, DbObjectSourceOptions } from "../types/client.js";
2
2
  /** The `api.db` actions {@link dbObjectSource} calls. */
3
- export type DbObjectSourceActions<TClientAttributes = never> = Pick<DbClientActions<TClientAttributes>, "listObjects"> & Partial<Pick<DbClientActions<TClientAttributes>, "createFolder">>;
3
+ export type DbObjectSourceActions<TClientAttributes = never> = Pick<DbClientActions<TClientAttributes>, "listObjects"> & Partial<Pick<DbClientActions<TClientAttributes>, "createFolder" | "updateObject" | "updateFolder">>;
4
4
  /**
5
5
  * A `useObjectList` source over `api.db`.
6
6
  *
7
- * With `folders` on (the default), `parent` is a folder path: each page
8
- * lists one folder, the first page carries its child folders, uploads go to
9
- * the open folder, and "New folder" calls `api.db.createFolder`. With
10
- * `folders: false`, the list is flat and has no folder actions.
7
+ * With `folders` on (the default), `parent` is a folder id: each page lists
8
+ * one folder, the first page carries its child folders and the breadcrumb
9
+ * trail, uploads go to the open folder, and "New folder" calls
10
+ * `api.db.createFolder`. Rename calls `api.db.updateObject` (`filename`) or
11
+ * `api.db.updateFolder` (`name`) and leaves the object key as it is. With
12
+ * `folders: false`, the list is flat: file rename stays, and folder actions
13
+ * do not.
11
14
  *
12
- * `search` matches the filename on the server. `sort` maps `name` to
13
- * `filename`; `size` and `updatedAt` keep their names, and other fields keep
14
- * the server's order (newest first). `usage` is the scope total with its
15
- * caps, not the open folder's.
15
+ * `search` runs on the server and ignores case and accents. With `folders`
16
+ * on it is recursive: `searchScope: "folder"` looks in the open folder and
17
+ * every folder below it, `"all"` in the whole scope, and each result's
18
+ * `location` says where it lives. `sort` maps `name` to `filename`; `size`
19
+ * and `updatedAt` keep their names, and other fields keep the server's
20
+ * order (newest first). `usage` is the scope total with its caps, not the
21
+ * open folder's.
16
22
  *
17
23
  * ```ts
18
24
  * const list = useObjectList({
@@ -1 +1 @@
1
- {"version":3,"file":"db-object-source.d.ts","sourceRoot":"","sources":["../../src/client/db-object-source.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EACV,eAAe,EAKf,cAAc,EAEd,qBAAqB,EAEtB,MAAM,gBAAgB,CAAC;AAExB,yDAAyD;AACzD,MAAM,MAAM,qBAAqB,CAAC,iBAAiB,GAAG,KAAK,IAAI,IAAI,CACjE,eAAe,CAAC,iBAAiB,CAAC,EAClC,aAAa,CACd,GACC,OAAO,CAAC,IAAI,CAAC,eAAe,CAAC,iBAAiB,CAAC,EAAE,cAAc,CAAC,CAAC,CAAC;AA0BpE;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,cAAc,CAAC,iBAAiB,GAAG,KAAK,EACtD,EAAE,EAAE,qBAAqB,CAAC,iBAAiB,CAAC,EAC5C,OAAO,GAAE,qBAA0B,GAClC,cAAc,CAAC,iBAAiB,CAAC,CAiDnC"}
1
+ {"version":3,"file":"db-object-source.d.ts","sourceRoot":"","sources":["../../src/client/db-object-source.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EACV,eAAe,EAMf,cAAc,EAGd,qBAAqB,EAEtB,MAAM,gBAAgB,CAAC;AAExB,yDAAyD;AACzD,MAAM,MAAM,qBAAqB,CAAC,iBAAiB,GAAG,KAAK,IAAI,IAAI,CACjE,eAAe,CAAC,iBAAiB,CAAC,EAClC,aAAa,CACd,GACC,OAAO,CACL,IAAI,CACF,eAAe,CAAC,iBAAiB,CAAC,EAClC,cAAc,GAAG,cAAc,GAAG,cAAc,CACjD,CACF,CAAC;AA4CJ;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,wBAAgB,cAAc,CAAC,iBAAiB,GAAG,KAAK,EACtD,EAAE,EAAE,qBAAqB,CAAC,iBAAiB,CAAC,EAC5C,OAAO,GAAE,qBAA0B,GAClC,cAAc,CAAC,iBAAiB,CAAC,CAqHnC"}
@@ -1,5 +1,5 @@
1
- export { dbClient } from "./db-client";
2
- export { dbObjectSource, type DbObjectSourceActions } from "./db-object-source";
3
- export type { DbClientActions, DbClientFolder, DbClientFolderInput, DbClientGetInput, DbClientListInput, DbClientListResponse, DbClientObject, DbClientObjectStatus, DbClientUpdateInput, DbListSort, DbListSortField, DbObjectSource, DbObjectSourceFile, DbObjectSourceFolder, DbObjectSourceOptions, DbObjectSourcePage, DbObjectSourceQueryInput, DbQuota, DbUsage, } from "../types/client.js";
1
+ export { dbClient } from "./db-client.js";
2
+ export { dbObjectSource, type DbObjectSourceActions } from "./db-object-source.js";
3
+ export type { DbClientActions, DbClientCreateFolderInput, DbClientDeleteFolderInput, DbClientFolder, DbClientGetInput, DbClientListInput, DbClientListResponse, DbClientObject, DbClientObjectStatus, DbClientUpdateFolderInput, DbClientUpdateInput, DbListSort, DbListSortField, DbObjectSource, DbObjectSourceFile, DbObjectSourceFolder, DbObjectSourceOptions, DbObjectSourcePage, DbObjectSourceQueryInput, DbObjectSourceSearchScope, DbQuota, DbUsage, } from "../types/client.js";
4
4
  export type { StorageObjectStatus } from "../types/storage-object.js";
5
5
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/client/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AACvC,OAAO,EAAE,cAAc,EAAE,KAAK,qBAAqB,EAAE,MAAM,oBAAoB,CAAC;AAChF,YAAY,EACV,eAAe,EACf,cAAc,EACd,mBAAmB,EACnB,gBAAgB,EAChB,iBAAiB,EACjB,oBAAoB,EACpB,cAAc,EACd,oBAAoB,EACpB,mBAAmB,EACnB,UAAU,EACV,eAAe,EACf,cAAc,EACd,kBAAkB,EAClB,oBAAoB,EACpB,qBAAqB,EACrB,kBAAkB,EAClB,wBAAwB,EACxB,OAAO,EACP,OAAO,GACR,MAAM,gBAAgB,CAAC;AACxB,YAAY,EAAE,mBAAmB,EAAE,MAAM,wBAAwB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/client/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AACvC,OAAO,EAAE,cAAc,EAAE,KAAK,qBAAqB,EAAE,MAAM,oBAAoB,CAAC;AAChF,YAAY,EACV,eAAe,EACf,yBAAyB,EACzB,yBAAyB,EACzB,cAAc,EACd,gBAAgB,EAChB,iBAAiB,EACjB,oBAAoB,EACpB,cAAc,EACd,oBAAoB,EACpB,yBAAyB,EACzB,mBAAmB,EACnB,UAAU,EACV,eAAe,EACf,cAAc,EACd,kBAAkB,EAClB,oBAAoB,EACpB,qBAAqB,EACrB,kBAAkB,EAClB,wBAAwB,EACxB,yBAAyB,EACzB,OAAO,EACP,OAAO,GACR,MAAM,gBAAgB,CAAC;AACxB,YAAY,EAAE,mBAAmB,EAAE,MAAM,wBAAwB,CAAC"}