@dimah-s3/db 2.0.0-beta.0 → 2.0.0-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -3
- package/dist/adapters/drizzle.d.ts +2 -1
- package/dist/adapters/drizzle.d.ts.map +1 -1
- package/dist/cli.d.ts +8 -9
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +42 -103
- package/dist/cli.js.map +1 -1
- package/dist/client/db-client.d.ts +1 -1
- package/dist/client/db-client.d.ts.map +1 -1
- package/dist/client/db-object-source.d.ts +5 -4
- package/dist/client/db-object-source.d.ts.map +1 -1
- package/dist/client/index.d.ts +1 -1
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/index.js +27 -12
- package/dist/client/index.js.map +1 -1
- package/dist/drizzle/mysql.d.ts +46 -13
- package/dist/drizzle/mysql.d.ts.map +1 -1
- package/dist/drizzle/mysql.js +15 -18
- package/dist/drizzle/mysql.js.map +1 -1
- package/dist/drizzle/pg.d.ts +44 -13
- package/dist/drizzle/pg.d.ts.map +1 -1
- package/dist/drizzle/pg.js +17 -17
- package/dist/drizzle/pg.js.map +1 -1
- package/dist/drizzle/shared.d.ts +4 -13
- package/dist/drizzle/shared.d.ts.map +1 -1
- package/dist/drizzle/sqlite.d.ts +46 -13
- package/dist/drizzle/sqlite.d.ts.map +1 -1
- package/dist/drizzle/sqlite.js +17 -17
- package/dist/drizzle/sqlite.js.map +1 -1
- package/dist/folder-path.d.ts +9 -2
- package/dist/folder-path.d.ts.map +1 -1
- package/dist/fuma-db.d.ts +10 -30
- package/dist/fuma-db.d.ts.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +372 -231
- package/dist/index.js.map +1 -1
- package/dist/plugin/create-database-endpoints.d.ts +38 -18
- package/dist/plugin/create-database-endpoints.d.ts.map +1 -1
- package/dist/plugin/create-database-lifecycle-hooks.d.ts +1 -1
- package/dist/plugin/create-database-lifecycle-hooks.d.ts.map +1 -1
- package/dist/plugin/db.d.ts.map +1 -1
- package/dist/plugin/object-access.d.ts +1 -1
- package/dist/plugin/object-access.d.ts.map +1 -1
- package/dist/plugin/public-stores.d.ts +4 -7
- package/dist/plugin/public-stores.d.ts.map +1 -1
- package/dist/plugin/schemas.d.ts +21 -12
- package/dist/plugin/schemas.d.ts.map +1 -1
- package/dist/plugin/to-db-client-object.d.ts.map +1 -1
- package/dist/schema/{v2.d.ts → schema.d.ts} +23 -13
- package/dist/schema/schema.d.ts.map +1 -0
- package/dist/store/create-db-store.d.ts +2 -2
- package/dist/store/folder-store.d.ts +41 -14
- package/dist/store/folder-store.d.ts.map +1 -1
- package/dist/store/list-cursor.d.ts +1 -1
- package/dist/store/list-cursor.d.ts.map +1 -1
- package/dist/store/map-row.d.ts +2 -2
- package/dist/store/map-row.d.ts.map +1 -1
- package/dist/store/object-store.d.ts +9 -9
- package/dist/store/object-store.d.ts.map +1 -1
- package/dist/store/schema-version.d.ts +1 -1
- package/dist/types/client.d.ts +47 -18
- package/dist/types/client.d.ts.map +1 -1
- package/dist/types/plugin.d.ts +6 -1
- package/dist/types/plugin.d.ts.map +1 -1
- package/dist/types/storage-object.d.ts +8 -7
- package/dist/types/storage-object.d.ts.map +1 -1
- package/package.json +9 -6
- package/prisma/dimah-s3.prisma +14 -13
- package/sql/mysql.sql +12 -11
- package/sql/pg.sql +10 -10
- package/sql/sqlite.sql +10 -9
- package/src/drizzle/mysql.ts +175 -0
- package/src/drizzle/pg.ts +146 -0
- package/src/drizzle/sqlite.ts +150 -0
- package/dist/schema/v1.d.ts +0 -24
- package/dist/schema/v1.d.ts.map +0 -1
- package/dist/schema/v2.d.ts.map +0 -1
- package/sql/upgrade/1.0.0-2.0.0/mysql.sql +0 -71
- package/sql/upgrade/1.0.0-2.0.0/pg.sql +0 -47
- package/sql/upgrade/1.0.0-2.0.0/sqlite.sql +0 -119
package/README.md
CHANGED
|
@@ -18,12 +18,12 @@ You do not install `fumadb`: the adapters ship from `@dimah-s3/db/adapters/*`.
|
|
|
18
18
|
|
|
19
19
|
Create the schema with one of:
|
|
20
20
|
|
|
21
|
-
- Drizzle: add `dimahS3Tables` from `@dimah-s3/db/drizzle/{sqlite,pg,mysql}`
|
|
21
|
+
- Drizzle: copy `src/drizzle/{sqlite,pg,mysql}.ts` from the package into your schema so you can add columns. Or import `dimahS3Tables` from `@dimah-s3/db/drizzle/{sqlite,pg,mysql}` when the tables stay as they are.
|
|
22
22
|
- Prisma: copy [`prisma/dimah-s3.prisma`](./prisma/dimah-s3.prisma) into your schema.
|
|
23
23
|
- Plain SQL: run [`sql/<dialect>.sql`](./sql).
|
|
24
|
-
- Kysely: call `migrate(client)` from `@dimah-s3/db/cli`. It
|
|
24
|
+
- Kysely: call `migrate(client)` from `@dimah-s3/db/cli`. It creates the tables and the indexes. The FumaDB CLI from `runCli` does the same only with `migrate:up`; its `migrate` command jumps straight to the latest schema and skips the indexes.
|
|
25
25
|
|
|
26
|
-
|
|
26
|
+
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
27
|
|
|
28
28
|
## Server
|
|
29
29
|
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* The FumaDB Drizzle adapter. Pass a Drizzle instance whose schema includes
|
|
3
|
-
*
|
|
3
|
+
* the tables from `src/drizzle/<dialect>.ts`, copied into the app or
|
|
4
|
+
* imported as `dimahS3Tables` from `@dimah-s3/db/drizzle/<dialect>`:
|
|
4
5
|
* `DimahS3DB.client(drizzleAdapter({ db, provider: "postgresql" }))`.
|
|
5
6
|
*/
|
|
6
7
|
export { drizzleAdapter, type DrizzleConfig } from "fumadb/adapters/drizzle";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"drizzle.d.ts","sourceRoot":"","sources":["../../src/adapters/drizzle.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"drizzle.d.ts","sourceRoot":"","sources":["../../src/adapters/drizzle.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,OAAO,EAAE,cAAc,EAAE,KAAK,aAAa,EAAE,MAAM,yBAAyB,CAAC"}
|
package/dist/cli.d.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { type DimahS3DbClient } from "./fuma-db";
|
|
2
2
|
/**
|
|
3
3
|
* Runs the interactive FumaDB CLI: generate the ORM schema, or migrate when
|
|
4
|
-
* the client uses the Kysely adapter. Prefer {@link migrate}
|
|
5
|
-
*
|
|
6
|
-
*
|
|
4
|
+
* the client uses the Kysely adapter. Prefer {@link migrate}. Only
|
|
5
|
+
* `migrate:up`, one version at a time, runs the steps the schema needs,
|
|
6
|
+
* including the indexes. The CLI `migrate` command jumps to the latest
|
|
7
|
+
* schema and skips them.
|
|
7
8
|
*
|
|
8
9
|
* ```ts
|
|
9
10
|
* // scripts/db.ts
|
|
@@ -15,12 +16,10 @@ import { type DimahS3DbClient } from "./fuma-db";
|
|
|
15
16
|
*/
|
|
16
17
|
export declare function runCli(client: DimahS3DbClient): Promise<void>;
|
|
17
18
|
/**
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
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/`.
|
|
19
|
+
* Creates the schema this package reads, one version at a time, so the
|
|
20
|
+
* schema's own steps run (the indexes among them). It needs the Kysely
|
|
21
|
+
* adapter; with Drizzle or Prisma, use your ORM's migrations or the files
|
|
22
|
+
* in `sql/`. It does not upgrade a database at another version.
|
|
24
23
|
*
|
|
25
24
|
* ```ts
|
|
26
25
|
* 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;
|
|
1
|
+
{"version":3,"file":"cli.d.ts","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":"AACA,OAAO,EAAqB,KAAK,eAAe,EAAE,MAAM,WAAW,CAAC;AAYpE;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,MAAM,CAAC,MAAM,EAAE,eAAe,GAAG,OAAO,CAAC,IAAI,CAAC,CAS7D;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAsB,OAAO,CAAC,MAAM,EAAE,eAAe,GAAG,OAAO,CAAC,IAAI,CAAC,CAgBpE"}
|
package/dist/cli.js
CHANGED
|
@@ -3,37 +3,10 @@ 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
6
|
function byteCount(name) {
|
|
34
7
|
return column(name, "bigint").nullable();
|
|
35
8
|
}
|
|
36
|
-
var
|
|
9
|
+
var storageObject = table("storage_object", {
|
|
37
10
|
id: idColumn("id", "varchar(255)").defaultTo$("auto"),
|
|
38
11
|
/** Owner, for example `user:123` or `org:acme`. */
|
|
39
12
|
scope: column("scope", "string"),
|
|
@@ -41,8 +14,8 @@ var storageObject2 = table("storage_object", {
|
|
|
41
14
|
key: column("key", "string"),
|
|
42
15
|
/** Named dimah-s3 route that created the object. */
|
|
43
16
|
route: column("route", "string"),
|
|
44
|
-
/**
|
|
45
|
-
|
|
17
|
+
/** Folder that holds the object. `null` is the root. */
|
|
18
|
+
folderId: column("folder_id", "string").nullable(),
|
|
46
19
|
filename: column("filename", "string"),
|
|
47
20
|
contentType: column("content_type", "string").nullable(),
|
|
48
21
|
/** Verified size from HeadObject, set on confirm. */
|
|
@@ -70,19 +43,20 @@ var storageObject2 = table("storage_object", {
|
|
|
70
43
|
var storageFolder = table("storage_folder", {
|
|
71
44
|
id: idColumn("id", "varchar(255)").defaultTo$("auto"),
|
|
72
45
|
scope: column("scope", "string"),
|
|
73
|
-
/**
|
|
74
|
-
|
|
75
|
-
/** Path of the containing folder. `null` is the root. */
|
|
76
|
-
parent: column("parent", "string").nullable(),
|
|
46
|
+
/** Id of the containing folder. `""` is the root. */
|
|
47
|
+
parentId: column("parent_id", "string"),
|
|
77
48
|
/** Last segment of `path`. */
|
|
78
49
|
name: column("name", "string"),
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
50
|
+
/** Full folder path. */
|
|
51
|
+
path: column("path", "string"),
|
|
52
|
+
createdAt: column("created_at", "timestamp").defaultTo$("now"),
|
|
53
|
+
updatedAt: column("updated_at", "timestamp").defaultTo$("now")
|
|
54
|
+
}).unique("storage_folder_scope_path_uk", ["scope", "path"]).unique("storage_folder_scope_parent_name_uk", ["scope", "parentId", "name"]);
|
|
55
|
+
var schemaIndexes = [
|
|
82
56
|
{
|
|
83
57
|
name: "storage_object_scope_folder_idx",
|
|
84
58
|
table: "storage_object",
|
|
85
|
-
columns: ["scope", "status", "
|
|
59
|
+
columns: ["scope", "status", "folder_id", "created_at"]
|
|
86
60
|
},
|
|
87
61
|
{
|
|
88
62
|
name: "storage_object_scope_updated_idx",
|
|
@@ -103,99 +77,64 @@ var v2Indexes = [
|
|
|
103
77
|
name: "storage_object_status_expires_idx",
|
|
104
78
|
table: "storage_object",
|
|
105
79
|
columns: ["status", "expires_at"]
|
|
106
|
-
},
|
|
107
|
-
{
|
|
108
|
-
name: "storage_folder_scope_parent_idx",
|
|
109
|
-
table: "storage_folder",
|
|
110
|
-
columns: ["scope", "parent"]
|
|
111
80
|
}
|
|
112
81
|
];
|
|
113
|
-
var v1Indexes = [
|
|
114
|
-
"storage_object_scope_status_created_idx",
|
|
115
|
-
"storage_object_scope_route_idx",
|
|
116
|
-
"storage_object_status_created_idx"
|
|
117
|
-
];
|
|
118
82
|
var custom = (sql) => ({ type: "custom", sql });
|
|
119
|
-
var
|
|
120
|
-
var
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
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",
|
|
83
|
+
var PREDECESSOR_VERSION = "0.0.1";
|
|
84
|
+
var predecessor = schema({
|
|
85
|
+
version: PREDECESSOR_VERSION,
|
|
86
|
+
tables: {}
|
|
87
|
+
});
|
|
88
|
+
var dbSchema = schema({
|
|
89
|
+
version: "2.1.0",
|
|
136
90
|
tables: {
|
|
137
|
-
storageObject
|
|
91
|
+
storageObject,
|
|
138
92
|
storageFolder
|
|
139
93
|
},
|
|
140
94
|
async up({ auto }) {
|
|
141
|
-
|
|
95
|
+
const indexes = schemaIndexes.map((index) => {
|
|
96
|
+
const columns = index.columns.map((name) => `"${name}"`).join(", ");
|
|
97
|
+
return custom(
|
|
98
|
+
`CREATE INDEX IF NOT EXISTS "${index.name}" ON "${index.table}" (${columns})`
|
|
99
|
+
);
|
|
100
|
+
});
|
|
101
|
+
return [...await auto(), ...indexes];
|
|
142
102
|
}
|
|
143
103
|
});
|
|
144
104
|
|
|
145
105
|
// src/fuma-db.ts
|
|
146
106
|
fumadb({
|
|
147
107
|
namespace: "dimah_s3",
|
|
148
|
-
schemas: [
|
|
108
|
+
schemas: [predecessor, dbSchema]
|
|
149
109
|
});
|
|
150
|
-
var DB_SCHEMA_VERSION =
|
|
110
|
+
var DB_SCHEMA_VERSION = dbSchema.version;
|
|
151
111
|
|
|
152
112
|
// src/cli.ts
|
|
153
|
-
var
|
|
113
|
+
var RESUMABLE = /* @__PURE__ */ new Set([PREDECESSOR_VERSION, DB_SCHEMA_VERSION]);
|
|
154
114
|
function runCli(client) {
|
|
155
115
|
const { main } = createCli({
|
|
156
116
|
db: client,
|
|
157
117
|
command: "dimah-s3-db",
|
|
158
118
|
description: "FumaDB CLI for @dimah-s3/db",
|
|
159
|
-
version: "2.0.0-beta.
|
|
119
|
+
version: "2.0.0-beta.2"
|
|
160
120
|
});
|
|
161
121
|
return main();
|
|
162
122
|
}
|
|
163
123
|
async function migrate(client) {
|
|
124
|
+
let version;
|
|
125
|
+
try {
|
|
126
|
+
version = await client.version();
|
|
127
|
+
} catch {
|
|
128
|
+
version = void 0;
|
|
129
|
+
}
|
|
130
|
+
if (version !== void 0 && !RESUMABLE.has(version)) {
|
|
131
|
+
throw new Error(
|
|
132
|
+
`[dimah-s3/db] The database is at schema ${version}, which this package does not upgrade. Create schema ${DB_SCHEMA_VERSION} on a new database.`
|
|
133
|
+
);
|
|
134
|
+
}
|
|
164
135
|
const migrator = client.createMigrator();
|
|
165
136
|
for (let next = await migrator.next(); next; next = await migrator.next()) {
|
|
166
|
-
const from = await migrator.getVersion();
|
|
167
137
|
await (await migrator.up()).execute();
|
|
168
|
-
if (from === v1.version) await rewriteSizes(client);
|
|
169
|
-
}
|
|
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;
|
|
199
138
|
}
|
|
200
139
|
}
|
|
201
140
|
|
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;AAAA,EAC3B,KAAA,EAAO,MAAA,CAAO,OAAA,EAAS,QAAQ,CAAA;AAAA,EAC/B,WAAA,EAAa,MAAA,CAAO,cAAA,EAAgB,QAAQ,EAAE,QAAA,EAAS;AAAA,EACvD,IAAA,EAAM,MAAA,CAAO,MAAA,EAAQ,QAAQ,EAAE,QAAA,EAAS;AAAA,EACxC,IAAA,EAAM,MAAA,CAAO,OAAA,EAAS,QAAQ,EAAE,QAAA,EAAS;AAAA,EACzC,QAAA,EAAU,MAAA,CAAO,UAAA,EAAY,QAAQ,EAAE,QAAA,EAAS;AAAA,EAChD,MAAA,EAAQ,MAAA,CAAO,QAAA,EAAU,QAAQ,CAAA;AAAA,EACjC,UAAA,EAAY,MAAA,CAAO,UAAA,EAAY,MAAM,EAAE,QAAA,EAAS;AAAA,EAChD,GAAA,EAAK,MAAA,CAAO,KAAA,EAAO,QAAQ,EAAE,QAAA,EAAS;AAAA,EACtC,QAAA,EAAU,MAAA,CAAO,WAAA,EAAa,QAAQ,EAAE,QAAA,EAAS;AAAA,EACjD,YAAA,EAAc,MAAA,CAAO,eAAA,EAAiB,QAAQ,EAAE,QA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GAAO,MAAM,GAAA,CAAI,QAAA,CAAS,eAAA,EAAiB;AAAA,MAC/C,MAAA,EAAQ,CAAC,IAAA,EAAM,MAAA,EAAQ,cAAc,CAAA;AAAA,MACrC,KAAA,EAAO,CAAC,CAAA,KACN,CAAA,CAAE,GAAA;AAAA,QACA,CAAA,CAAE,GAAG,CAAA,CAAE,SAAA,CAAU,MAAM,CAAA,EAAG,CAAA,CAAE,SAAA,CAAU,cAAc,CAAC,CAAA;AAAA,QACrD,KAAA,KAAU,MAAA,IAAa,CAAA,CAAE,IAAA,EAAM,KAAK,KAAK;AAAA,OAC3C;AAAA,MACF,OAAA,EAAS,CAAC,CAAC,IAAA,EAAM,KAAK,CAAC,CAAA;AAAA,MACvB,KAAA,EAAO;AAAA,KACR,CAAA;AACD,IAAA,KAAA,MAAW,OAAO,IAAA,EAAM;AACtB,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.ts","../src/cli.ts"],"names":[],"mappings":";;;;;AASA,SAAS,UAAU,IAAA,EAAc;AAC/B,EAAA,OAAO,MAAA,CAAO,IAAA,EAAM,QAAQ,CAAA,CAAE,QAAA,EAAS;AAKzC;AAOA,IAAM,aAAA,GAAgB,MAAM,gBAAA,EAAkB;AAAA,EAC5C,IAAI,QAAA,CAAS,IAAA,EAAM,cAAc,CAAA,CAAE,WAAW,MAAM,CAAA;AAAA;AAAA,EAEpD,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;AAAA;AAAA,EAE3B,KAAA,EAAO,MAAA,CAAO,OAAA,EAAS,QAAQ,CAAA;AAAA;AAAA,EAE/B,QAAA,EAAU,MAAA,CAAO,WAAA,EAAa,QAAQ,EAAE,QAAA,EAAS;AAAA,EACjD,QAAA,EAAU,MAAA,CAAO,UAAA,EAAY,QAAQ,CAAA;AAAA,EACrC,WAAA,EAAa,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;AAiBnE,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;AAgBA,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,MAAA,IAAa,CAAC,SAAA,CAAU,GAAA,CAAI,OAAO,CAAA,EAAG;AACpD,IAAA,MAAM,IAAI,KAAA;AAAA,MACR,CAAA,wCAAA,EAA2C,OAAO,CAAA,qDAAA,EAAwD,iBAAiB,CAAA,mBAAA;AAAA,KAC7H;AAAA,EACF;AACA,EAAA,MAAM,QAAA,GAAW,OAAO,cAAA,EAAe;AACvC,EAAA,KAAA,IAAS,IAAA,GAAO,MAAM,QAAA,CAAS,IAAA,EAAK,EAAG,MAAM,IAAA,GAAO,MAAM,QAAA,CAAS,IAAA,EAAK,EAAG;AACzE,IAAA,MAAA,CAAO,MAAM,QAAA,CAAS,EAAA,EAAG,EAAG,OAAA,EAAQ;AAAA,EACtC;AACF","file":"cli.js","sourcesContent":["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.\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 /** Folder that holds the object. `null` is the root. */\n folderId: column(\"folder_id\", \"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/**\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)\").defaultTo$(\"auto\"),\n scope: column(\"scope\", \"string\"),\n /** Id of the containing folder. `\"\"` is the root. */\n parentId: column(\"parent_id\", \"string\"),\n /** Last segment of `path`. */\n name: column(\"name\", \"string\"),\n /** Full folder path. */\n path: column(\"path\", \"string\"),\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 .unique(\"storage_folder_scope_parent_name_uk\", [\"scope\", \"parentId\", \"name\"]);\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. FumaDB cannot declare them, so {@link dbSchema}'s\n * migration, the SQL files, the Drizzle tables, and the Prisma schema carry\n * them.\n */\nexport const schemaIndexes: readonly SchemaIndex[] = [\n {\n name: \"storage_object_scope_folder_idx\",\n table: \"storage_object\",\n columns: [\"scope\", \"status\", \"folder_id\", \"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\nconst custom = (sql: string) => ({ type: \"custom\" as const, sql });\n\n/**\n * FumaDB runs `up` only when migrating from the previous entry, and skips\n * `up` on the first one. This empty entry exists so {@link dbSchema} can\n * create the indexes on a new database. A finished database is at\n * {@link dbSchema}.\n */\nexport const PREDECESSOR_VERSION = \"0.0.1\";\n\nexport const predecessor = schema({\n version: PREDECESSOR_VERSION,\n tables: {},\n});\n\n/**\n * The @dimah-s3/db schema, version `2.1.0`. The migration creates the\n * secondary indexes after the tables, because FumaDB cannot declare them.\n */\nexport const dbSchema = schema({\n version: \"2.1.0\",\n tables: {\n storageObject,\n storageFolder,\n },\n async up({ auto }) {\n const indexes = schemaIndexes.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 return [...(await auto()), ...indexes];\n },\n});\n","import { fumadb, type InferFumaDB } from \"fumadb\";\nimport { dbSchema, predecessor } 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 *\n * `predecessor` is empty. FumaDB skips `up` on the first schema, and the\n * indexes are created in {@link dbSchema}'s `up`.\n */\nexport const DimahS3DB = fumadb({\n namespace: \"dimah_s3\",\n schemas: [predecessor, 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\";\nimport { PREDECESSOR_VERSION } from \"./schema/schema\";\n\ndeclare const __DIMAH_S3_DB_VERSION__: string;\n\n/**\n * Versions `migrate` may continue from: an empty database, a migration that\n * stopped on the empty predecessor, or a database already at\n * {@link DB_SCHEMA_VERSION}.\n */\nconst RESUMABLE = new Set<string>([PREDECESSOR_VERSION, DB_SCHEMA_VERSION]);\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}. Only\n * `migrate:up`, one version at a time, runs the steps the schema needs,\n * including the indexes. The CLI `migrate` command jumps to the latest\n * schema and skips them.\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 * Creates the schema this package reads, one version at a time, so the\n * schema's own steps run (the indexes among them). It needs the Kysely\n * adapter; with Drizzle or Prisma, use your ORM's migrations or the files\n * in `sql/`. It does not upgrade a database at another version.\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 !== undefined && !RESUMABLE.has(version)) {\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 const migrator = client.createMigrator();\n for (let next = await migrator.next(); next; next = await migrator.next()) {\n await (await migrator.up()).execute();\n }\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({
|
|
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,+
|
|
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"}
|
|
@@ -4,10 +4,11 @@ export type DbObjectSourceActions<TClientAttributes = never> = Pick<DbClientActi
|
|
|
4
4
|
/**
|
|
5
5
|
* A `useObjectList` source over `api.db`.
|
|
6
6
|
*
|
|
7
|
-
* With `folders` on (the default), `parent` is a folder
|
|
8
|
-
*
|
|
9
|
-
* the open folder, and "New folder" calls
|
|
10
|
-
* `folders: false`, the list is flat and has no
|
|
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`. With `folders: false`, the list is flat and has no
|
|
11
|
+
* folder actions.
|
|
11
12
|
*
|
|
12
13
|
* `search` matches the filename on the server. `sort` maps `name` to
|
|
13
14
|
* `filename`; `size` and `updatedAt` keep their names, and other fields keep
|
|
@@ -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
|
|
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;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,cAAc,CAAC,iBAAiB,GAAG,KAAK,EACtD,EAAE,EAAE,qBAAqB,CAAC,iBAAiB,CAAC,EAC5C,OAAO,GAAE,qBAA0B,GAClC,cAAc,CAAC,iBAAiB,CAAC,CAwDnC"}
|
package/dist/client/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export { dbClient } from "./db-client";
|
|
2
2
|
export { dbObjectSource, type DbObjectSourceActions } from "./db-object-source";
|
|
3
|
-
export type { DbClientActions,
|
|
3
|
+
export type { DbClientActions, DbClientCreateFolderInput, DbClientDeleteFolderInput, DbClientFolder, DbClientGetInput, DbClientListInput, DbClientListResponse, DbClientObject, DbClientObjectStatus, DbClientUpdateFolderInput, DbClientUpdateInput, DbListSort, DbListSortField, DbObjectSource, DbObjectSourceFile, DbObjectSourceFolder, DbObjectSourceOptions, DbObjectSourcePage, DbObjectSourceQueryInput, 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,
|
|
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,OAAO,EACP,OAAO,GACR,MAAM,gBAAgB,CAAC;AACxB,YAAY,EAAE,mBAAmB,EAAE,MAAM,wBAAwB,CAAC"}
|
package/dist/client/index.js
CHANGED
|
@@ -5,8 +5,8 @@ function call(options) {
|
|
|
5
5
|
return { signal: options?.signal, headers: options?.headers };
|
|
6
6
|
}
|
|
7
7
|
function listQuery(input) {
|
|
8
|
-
if (input?.
|
|
9
|
-
return { ...input,
|
|
8
|
+
if (input?.folderId !== null) return input;
|
|
9
|
+
return { ...input, folderId: "" };
|
|
10
10
|
}
|
|
11
11
|
function dbClient() {
|
|
12
12
|
return defineClientPlugin({
|
|
@@ -30,6 +30,11 @@ function dbClient() {
|
|
|
30
30
|
body: input,
|
|
31
31
|
...call(options)
|
|
32
32
|
}),
|
|
33
|
+
updateFolder: (input, options) => $fetch(pluginPath("db", "folder"), {
|
|
34
|
+
method: "PATCH",
|
|
35
|
+
body: input,
|
|
36
|
+
...call(options)
|
|
37
|
+
}),
|
|
33
38
|
deleteFolder: (input, options) => $fetch(pluginPath("db", "folder"), {
|
|
34
39
|
method: "DELETE",
|
|
35
40
|
query: input,
|
|
@@ -38,11 +43,14 @@ function dbClient() {
|
|
|
38
43
|
})
|
|
39
44
|
});
|
|
40
45
|
}
|
|
41
|
-
|
|
42
|
-
|
|
46
|
+
var FOLDER_NAME_MAX_LENGTH = 255;
|
|
47
|
+
var INVALID_CHARACTER = /[\p{Cc}\p{Cs}]/u;
|
|
48
|
+
function characterCount(value) {
|
|
49
|
+
return Array.from(value).length;
|
|
43
50
|
}
|
|
44
|
-
function
|
|
45
|
-
|
|
51
|
+
function isFolderName(name) {
|
|
52
|
+
const length = characterCount(name);
|
|
53
|
+
return length >= 1 && length <= FOLDER_NAME_MAX_LENGTH && name !== "." && name !== ".." && !name.includes("/") && !INVALID_CHARACTER.test(name);
|
|
46
54
|
}
|
|
47
55
|
|
|
48
56
|
// src/client/db-object-source.ts
|
|
@@ -71,7 +79,7 @@ function dbObjectSource(db, options = {}) {
|
|
|
71
79
|
getFile: toFile,
|
|
72
80
|
async query({ parent, cursor, sort, search, signal }) {
|
|
73
81
|
const input = {};
|
|
74
|
-
if (folders) input.
|
|
82
|
+
if (folders) input.folderId = parent ?? "";
|
|
75
83
|
if (cursor) input.cursor = cursor;
|
|
76
84
|
if (search) input.search = search;
|
|
77
85
|
const listSort = toListSort(sort);
|
|
@@ -86,7 +94,13 @@ function dbObjectSource(db, options = {}) {
|
|
|
86
94
|
nextCursor: page.nextCursor,
|
|
87
95
|
...page.folders && {
|
|
88
96
|
folders: page.folders.map((folder) => ({
|
|
89
|
-
id: folder.
|
|
97
|
+
id: folder.id,
|
|
98
|
+
name: folder.name
|
|
99
|
+
}))
|
|
100
|
+
},
|
|
101
|
+
...page.trail && {
|
|
102
|
+
trail: page.trail.map((folder) => ({
|
|
103
|
+
id: folder.id,
|
|
90
104
|
name: folder.name
|
|
91
105
|
}))
|
|
92
106
|
},
|
|
@@ -96,16 +110,17 @@ function dbObjectSource(db, options = {}) {
|
|
|
96
110
|
};
|
|
97
111
|
if (!folders) return source;
|
|
98
112
|
source.uploadOptions = (parent) => ({
|
|
99
|
-
metadata: {
|
|
113
|
+
metadata: { folderId: parent ?? "" }
|
|
100
114
|
});
|
|
101
115
|
const createFolder = db.createFolder?.bind(db);
|
|
102
116
|
if (createFolder) {
|
|
103
117
|
source.createFolder = async ({ parent, name }) => {
|
|
104
|
-
if (name
|
|
118
|
+
if (!isFolderName(name)) throw s3Error("FOLDER_INVALID");
|
|
105
119
|
const { folder } = await createFolder({
|
|
106
|
-
|
|
120
|
+
parentId: parent,
|
|
121
|
+
name
|
|
107
122
|
});
|
|
108
|
-
return { id: folder.
|
|
123
|
+
return { id: folder.id, name: folder.name };
|
|
109
124
|
};
|
|
110
125
|
}
|
|
111
126
|
return source;
|
package/dist/client/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
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;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AAA,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"]}
|
|
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,QAAA,KAAa,IAAA,EAAM,OAAO,KAAA;AACrC,EAAA,OAAO,EAAE,GAAG,KAAA,EAAO,QAAA,EAAU,EAAA,EAAG;AAClC;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,SAA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O,EAAA;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,QAAA,EAAU,MAAA,IAAU,EAAA;AAAG,GACrC,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,CAAC,YAAA,CAAa,IAAI,CAAA,EAAG,MAAMA,QAAQ,gBAAgB,CAAA;AACvD,MAAA,MAAM,EAAE,MAAA,EAAO,GAAI,MAAM,YAAA,CAAa;AAAA,QACpC,QAAA,EAAU,MAAA;AAAA,QACV;AAAA,OACD,CAAA;AACD,MAAA,OAAO,EAAE,EAAA,EAAI,MAAA,CAAO,EAAA,EAAI,IAAA,EAAM,OAAO,IAAA,EAAK;AAAA,IAC5C,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?.folderId !== null) return input;\n return { ...input, folderId: \"\" };\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({ folderId: \"\" });\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 updateFolder: (input, options) =>\n $fetch<{ folder: DbClientFolder }>(pluginPath(\"db\", \"folder\"), {\n method: \"PATCH\",\n body: input,\n ...call(options),\n }),\n deleteFolder: (input, options) =>\n $fetch<{ id: 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. The wire addresses a folder\n * by id; the path is the cached label of that id.\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\n/** True when `name` can be one segment of a folder path. */\nexport function isFolderName(name: string): boolean {\n const length = characterCount(name);\n return (\n length >= 1 &&\n length <= FOLDER_NAME_MAX_LENGTH &&\n name !== \".\" &&\n name !== \"..\" &&\n !name.includes(\"/\") &&\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(isFolderName) ? 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/**\n * The path of `name` inside `parent`. `null` when `name` is not a folder\n * name or the joined path is too long or too deep.\n */\nexport function childFolderPath(\n parent: string | null,\n name: string,\n): string | null {\n if (!isFolderName(name)) return null;\n return normalizeFolderPath(joinFolderPath(parent, name));\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 { isFolderName } 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 id: each page lists\n * one folder, the first page carries its child folders and the breadcrumb\n * trail, uploads go to the open folder, and \"New folder\" calls\n * `api.db.createFolder`. With `folders: false`, the list is flat and has no\n * 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.folderId = 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.id,\n name: folder.name,\n })),\n }),\n ...(page.trail && {\n trail: page.trail.map((folder) => ({\n id: folder.id,\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: { folderId: parent ?? \"\" },\n });\n const createFolder = db.createFolder?.bind(db);\n if (createFolder) {\n source.createFolder = async ({ parent, name }) => {\n // A `/` would be a nested path. The server creates one folder.\n if (!isFolderName(name)) throw s3Error(\"FOLDER_INVALID\");\n const { folder } = await createFolder({\n parentId: parent,\n name,\n });\n return { id: folder.id, name: folder.name };\n };\n }\n return source;\n}\n"]}
|