@dimah-s3/db 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +37 -0
  3. package/dist/cli.d.ts +5 -0
  4. package/dist/cli.d.ts.map +1 -0
  5. package/dist/cli.js +16 -0
  6. package/dist/cli.js.map +1 -0
  7. package/dist/client/db-client.d.ts +20 -0
  8. package/dist/client/db-client.d.ts.map +1 -0
  9. package/dist/client/index.d.ts +3 -0
  10. package/dist/client/index.d.ts.map +1 -0
  11. package/dist/client/index.js +18 -0
  12. package/dist/client/index.js.map +1 -0
  13. package/dist/errors.d.ts +16 -0
  14. package/dist/errors.d.ts.map +1 -0
  15. package/dist/fuma-db.d.ts +25 -0
  16. package/dist/fuma-db.d.ts.map +1 -0
  17. package/dist/hooks/create-object-access-guard.d.ts +34 -0
  18. package/dist/hooks/create-object-access-guard.d.ts.map +1 -0
  19. package/dist/index.d.ts +15 -0
  20. package/dist/index.d.ts.map +1 -0
  21. package/dist/index.js +477 -0
  22. package/dist/index.js.map +1 -0
  23. package/dist/jobs/purge-stale-pending.d.ts +26 -0
  24. package/dist/jobs/purge-stale-pending.d.ts.map +1 -0
  25. package/dist/plugin/create-database-endpoints.d.ts +12 -0
  26. package/dist/plugin/create-database-endpoints.d.ts.map +1 -0
  27. package/dist/plugin/create-database-lifecycle-hooks.d.ts +18 -0
  28. package/dist/plugin/create-database-lifecycle-hooks.d.ts.map +1 -0
  29. package/dist/plugin/db.d.ts +73 -0
  30. package/dist/plugin/db.d.ts.map +1 -0
  31. package/dist/plugin/to-db-client-object.d.ts +7 -0
  32. package/dist/plugin/to-db-client-object.d.ts.map +1 -0
  33. package/dist/plugin/to-db-client-object.test.d.ts +2 -0
  34. package/dist/plugin/to-db-client-object.test.d.ts.map +1 -0
  35. package/dist/schema/v1.d.ts +29 -0
  36. package/dist/schema/v1.d.ts.map +1 -0
  37. package/dist/store/map-row.d.ts +25 -0
  38. package/dist/store/map-row.d.ts.map +1 -0
  39. package/dist/store/storage-object-store.d.ts +101 -0
  40. package/dist/store/storage-object-store.d.ts.map +1 -0
  41. package/dist/types/storage-object.d.ts +67 -0
  42. package/dist/types/storage-object.d.ts.map +1 -0
  43. package/package.json +77 -0
  44. package/src/schema/examples/README.md +10 -0
  45. package/src/schema/examples/drizzle.ts +75 -0
  46. package/src/schema/examples/schema.prisma +53 -0
@@ -0,0 +1,73 @@
1
+ import { type DimahS3DbClient, type StorageObjectStore } from "../store/storage-object-store";
2
+ import type { ScopeResolver } from "../types/storage-object";
3
+ export type DbPluginOptions = {
4
+ /** FumaDB client (`DimahS3DB.client(adapter)`) or a prebuilt objects store. */
5
+ client: DimahS3DbClient | StorageObjectStore;
6
+ /** Maps a request to its owning scope. `null` rejects with 401. */
7
+ resolveScope: ScopeResolver;
8
+ /**
9
+ * How long a `pending` row stays valid before the purge job may remove it.
10
+ * @default 86_400_000 (24h)
11
+ */
12
+ pendingTtlMs?: number;
13
+ /**
14
+ * `soft` keeps the row with `status = "deleted"`; `hard` removes it.
15
+ * @default "soft"
16
+ */
17
+ deleteMode?: "soft" | "hard";
18
+ };
19
+ export type DbPluginContext = {
20
+ /** `storage_object` data-access layer — listings, quota, resume, custom routes. */
21
+ objects: StorageObjectStore;
22
+ /** Underlying FumaDB client (or the store when one was passed as `client`). */
23
+ client: DimahS3DbClient | StorageObjectStore;
24
+ };
25
+ /**
26
+ * Persistence plugin for {@link dimahS3} — keeps rows in sync with upload /
27
+ * multipart / download / delete lifecycle and enforces scope ownership.
28
+ *
29
+ * Exposes `GET plugins/db/objects` for browser listing via {@link dbClient}.
30
+ *
31
+ * ```ts
32
+ * const s3 = dimahS3({
33
+ * s3: s3Client,
34
+ * defaultBucket,
35
+ * upload: { enabled: true },
36
+ * multipart: { enabled: true },
37
+ * download: { enabled: true },
38
+ * delete: { enabled: true },
39
+ * plugins: [
40
+ * db({ client: dimahS3Db, resolveScope }),
41
+ * ],
42
+ * });
43
+ *
44
+ * s3.db.objects.listByScope({ scope });
45
+ * s3.context.db === s3.db;
46
+ * ```
47
+ */
48
+ export declare function db(options: DbPluginOptions): import("@dimah-s3/server").DimahS3Plugin<"db", {
49
+ objects: StorageObjectStore;
50
+ client: import("fumadb").FumaDB<import("fumadb/schema").Schema<"1.0.0", {
51
+ storageObject: import("fumadb/schema").Table<{
52
+ id: import("fumadb/schema").IdColumn<"varchar(255)", string | null, string>;
53
+ scope: import("fumadb/schema").Column<"string", string, string>;
54
+ bucket: import("fumadb/schema").Column<"string", string, string>;
55
+ key: import("fumadb/schema").Column<"string", string, string>;
56
+ contentType: import("fumadb/schema").Column<"string", string | null, string | null>;
57
+ size: import("fumadb/schema").Column<"bigint", bigint | null, bigint | null>;
58
+ eTag: import("fumadb/schema").Column<"string", string | null, string | null>;
59
+ filename: import("fumadb/schema").Column<"string", string | null, string | null>;
60
+ status: import("fumadb/schema").Column<"string", string, string>;
61
+ metadata: import("fumadb/schema").Column<"json", unknown, unknown>;
62
+ acl: import("fumadb/schema").Column<"string", string | null, string | null>;
63
+ uploadId: import("fumadb/schema").Column<"string", string | null, string | null>;
64
+ declaredSize: import("fumadb/schema").Column<"bigint", bigint | null, bigint | null>;
65
+ confirmedAt: import("fumadb/schema").Column<"timestamp", Date | null, Date | null>;
66
+ expiresAt: import("fumadb/schema").Column<"timestamp", Date | null, Date | null>;
67
+ createdAt: import("fumadb/schema").Column<"timestamp", Date | null, Date>;
68
+ updatedAt: import("fumadb/schema").Column<"timestamp", Date | null, Date>;
69
+ deletedAt: import("fumadb/schema").Column<"timestamp", Date | null, Date | null>;
70
+ }, {}>;
71
+ }>[]> | StorageObjectStore;
72
+ }>;
73
+ //# sourceMappingURL=db.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"db.d.ts","sourceRoot":"","sources":["../../src/plugin/db.ts"],"names":[],"mappings":"AACA,OAAO,EACL,KAAK,eAAe,EACpB,KAAK,kBAAkB,EACxB,MAAM,+BAA+B,CAAC;AACvC,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC;AAI7D,MAAM,MAAM,eAAe,GAAG;IAC5B,+EAA+E;IAC/E,MAAM,EAAE,eAAe,GAAG,kBAAkB,CAAC;IAC7C,mEAAmE;IACnE,YAAY,EAAE,aAAa,CAAC;IAC5B;;;OAGG;IACH,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB;;;OAGG;IACH,UAAU,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;CAC9B,CAAC;AAEF,MAAM,MAAM,eAAe,GAAG;IAC5B,mFAAmF;IACnF,OAAO,EAAE,kBAAkB,CAAC;IAC5B,+EAA+E;IAC/E,MAAM,EAAE,eAAe,GAAG,kBAAkB,CAAC;CAC9C,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,EAAE,CAAC,OAAO,EAAE,eAAe;;;;;;;;;;;;;;;;;;;;;;;;GAe1C"}
@@ -0,0 +1,7 @@
1
+ import type { DbClientObject, StorageObject } from "../types/storage-object";
2
+ /**
3
+ * Map a full store row to the browser wire contract for `GET plugins/db/objects`.
4
+ * Drops server-only fields (`scope`, `eTag`, `metadata`, `acl`, …).
5
+ */
6
+ export declare function toDbClientObject(object: StorageObject): DbClientObject;
7
+ //# sourceMappingURL=to-db-client-object.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"to-db-client-object.d.ts","sourceRoot":"","sources":["../../src/plugin/to-db-client-object.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC;AAE7E;;;GAGG;AACH,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,aAAa,GAAG,cAAc,CAYtE"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=to-db-client-object.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"to-db-client-object.test.d.ts","sourceRoot":"","sources":["../../src/plugin/to-db-client-object.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,29 @@
1
+ export declare const v1: import("fumadb/schema").Schema<"1.0.0", {
2
+ storageObject: import("fumadb/schema").Table<{
3
+ id: import("fumadb/schema").IdColumn<"varchar(255)", string | null, string>;
4
+ scope: import("fumadb/schema").Column<"string", string, string>;
5
+ bucket: import("fumadb/schema").Column<"string", string, string>;
6
+ key: import("fumadb/schema").Column<"string", string, string>;
7
+ contentType: import("fumadb/schema").Column<"string", string | null, string | null>;
8
+ /** Verified size from HeadObject after confirm. */
9
+ size: import("fumadb/schema").Column<"bigint", bigint | null, bigint | null>;
10
+ eTag: import("fumadb/schema").Column<"string", string | null, string | null>;
11
+ filename: import("fumadb/schema").Column<"string", string | null, string | null>;
12
+ status: import("fumadb/schema").Column<"string", string, string>;
13
+ /** App-specific labels, visibility, publicUrl, etc. */
14
+ metadata: import("fumadb/schema").Column<"json", unknown, unknown>;
15
+ acl: import("fumadb/schema").Column<"string", string | null, string | null>;
16
+ /** Multipart upload ID while status = pending. */
17
+ uploadId: import("fumadb/schema").Column<"string", string | null, string | null>;
18
+ /** Client-declared size before confirm (quota reservation). */
19
+ declaredSize: import("fumadb/schema").Column<"bigint", bigint | null, bigint | null>;
20
+ confirmedAt: import("fumadb/schema").Column<"timestamp", Date | null, Date | null>;
21
+ /** Pending TTL or temporary object expiry. */
22
+ expiresAt: import("fumadb/schema").Column<"timestamp", Date | null, Date | null>;
23
+ createdAt: import("fumadb/schema").Column<"timestamp", Date | null, Date>;
24
+ updatedAt: import("fumadb/schema").Column<"timestamp", Date | null, Date>;
25
+ deletedAt: import("fumadb/schema").Column<"timestamp", Date | null, Date | null>;
26
+ }, {}>;
27
+ }>;
28
+ export type { StorageObjectStatus } from "../types/storage-object";
29
+ //# sourceMappingURL=v1.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"v1.d.ts","sourceRoot":"","sources":["../../src/schema/v1.ts"],"names":[],"mappings":"AAwCA,eAAO,MAAM,EAAE;;;;;;;QApBb,mDAAmD;;;;;QAKnD,uDAAuD;;;QAGvD,kDAAkD;;QAElD,+DAA+D;;;QAG/D,8CAA8C;;;;;;EAY9C,CAAC;AAEH,YAAY,EAAE,mBAAmB,EAAE,MAAM,yBAAyB,CAAC"}
@@ -0,0 +1,25 @@
1
+ import type { StorageObject } from "../types/storage-object";
2
+ /** Raw `storage_object` row as returned by the FumaDB ORM. */
3
+ export type StorageObjectRow = {
4
+ id: string;
5
+ scope: string;
6
+ bucket: string;
7
+ key: string;
8
+ contentType: string | null;
9
+ size: bigint | null;
10
+ eTag: string | null;
11
+ filename: string | null;
12
+ status: string;
13
+ metadata: unknown;
14
+ acl: string | null;
15
+ uploadId: string | null;
16
+ declaredSize: bigint | null;
17
+ confirmedAt: Date | null;
18
+ expiresAt: Date | null;
19
+ createdAt: Date;
20
+ updatedAt: Date;
21
+ deletedAt: Date | null;
22
+ };
23
+ /** Normalize a raw row: bigint → number, status narrowing, metadata cast. */
24
+ export declare function mapStorageObjectRow(row: StorageObjectRow): StorageObject;
25
+ //# sourceMappingURL=map-row.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"map-row.d.ts","sourceRoot":"","sources":["../../src/store/map-row.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,aAAa,EAEd,MAAM,yBAAyB,CAAC;AAEjC,8DAA8D;AAC9D,MAAM,MAAM,gBAAgB,GAAG;IAC7B,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,GAAG,EAAE,MAAM,CAAC;IACZ,WAAW,EAAE,MAAM,GAAG,IAAI,CAAC;IAC3B,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IACpB,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IACpB,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;IACxB,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE,OAAO,CAAC;IAClB,GAAG,EAAE,MAAM,GAAG,IAAI,CAAC;IACnB,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;IACxB,YAAY,EAAE,MAAM,GAAG,IAAI,CAAC;IAC5B,WAAW,EAAE,IAAI,GAAG,IAAI,CAAC;IACzB,SAAS,EAAE,IAAI,GAAG,IAAI,CAAC;IACvB,SAAS,EAAE,IAAI,CAAC;IAChB,SAAS,EAAE,IAAI,CAAC;IAChB,SAAS,EAAE,IAAI,GAAG,IAAI,CAAC;CACxB,CAAC;AAOF,6EAA6E;AAC7E,wBAAgB,mBAAmB,CAAC,GAAG,EAAE,gBAAgB,GAAG,aAAa,CAqBxE"}
@@ -0,0 +1,101 @@
1
+ import type { InferFumaDB } from "fumadb";
2
+ import type { DimahS3DB } from "../fuma-db";
3
+ import type { StorageObject, StorageObjectStatus } from "../types/storage-object";
4
+ /** FumaDB client for the @dimah-s3/db schema. */
5
+ export type DimahS3DbClient = InferFumaDB<typeof DimahS3DB>;
6
+ /** Identifies one object row — `(bucket, key)` is unique. */
7
+ export type ObjectRef = {
8
+ bucket: string;
9
+ key: string;
10
+ };
11
+ export type UpsertPendingInput = ObjectRef & {
12
+ scope: string;
13
+ contentType?: string | null;
14
+ /** Client-declared size in bytes (quota reservation — not verified). */
15
+ declaredSize?: number | null;
16
+ metadata?: Record<string, unknown> | null;
17
+ acl?: string | null;
18
+ filename?: string | null;
19
+ /** Multipart upload ID, when the pending upload is multipart. */
20
+ uploadId?: string | null;
21
+ /** Pending TTL — stale rows past this are eligible for purge. */
22
+ expiresAt?: Date | null;
23
+ };
24
+ export type MarkActiveInput = ObjectRef & {
25
+ /** Verified size in bytes (from HeadObject). */
26
+ size: number;
27
+ eTag?: string | null;
28
+ contentType?: string | null;
29
+ acl?: string | null;
30
+ filename?: string | null;
31
+ metadata?: Record<string, unknown> | null;
32
+ };
33
+ export type ListByScopeInput = {
34
+ scope: string;
35
+ /**
36
+ * Filter by exact status. When omitted, soft-deleted rows are excluded
37
+ * and pending + active rows are returned.
38
+ */
39
+ status?: StorageObjectStatus;
40
+ limit?: number;
41
+ offset?: number;
42
+ };
43
+ export type ScopeUsage = {
44
+ /** Sum of verified size (fallback: declared size) of non-deleted rows. */
45
+ totalBytes: number;
46
+ /** Non-deleted row count. */
47
+ objectCount: number;
48
+ };
49
+ /**
50
+ * Minimal multipart resume payload — structurally matches
51
+ * `@dimah-s3/react` `StoredUpload` so a custom client `UploadStore` can
52
+ * return it from `get()` without importing react types.
53
+ */
54
+ export type PendingMultipartResume = {
55
+ uploadId: string;
56
+ key: string;
57
+ /** Declared file size in bytes (must match the File being resumed). */
58
+ fileSize: number;
59
+ bucket: string;
60
+ };
61
+ export type FindPendingMultipartInput = ObjectRef & {
62
+ /** Must equal the pending row's `declaredSize`. */
63
+ fileSize: number;
64
+ };
65
+ /**
66
+ * Map a `storage_object` row to a client resume payload.
67
+ * Returns `null` unless the row is `pending` with `uploadId` and `declaredSize`.
68
+ */
69
+ export declare function toPendingMultipartResume(object: StorageObject): PendingMultipartResume | null;
70
+ /** Data-access layer for the `storage_object` table. */
71
+ export type StorageObjectStore = {
72
+ /** Insert or reset a row to `pending` (presign / multipart init). */
73
+ upsertPending: (input: UpsertPendingInput) => Promise<void>;
74
+ /** Promote a row to `active` with verified fields (confirm / complete). */
75
+ markActive: (input: MarkActiveInput) => Promise<void>;
76
+ find: (ref: ObjectRef) => Promise<StorageObject | null>;
77
+ findActive: (ref: ObjectRef) => Promise<StorageObject | null>;
78
+ /**
79
+ * Pending multipart row for resume: `pending` + `uploadId` + matching
80
+ * `declaredSize`. Returns the full row so callers can enforce scope.
81
+ */
82
+ findPendingMultipart: (input: FindPendingMultipartInput) => Promise<StorageObject | null>;
83
+ listByScope: (input: ListByScopeInput) => Promise<StorageObject[]>;
84
+ getScopeUsage: (scope: string) => Promise<ScopeUsage>;
85
+ countByScope: (scope: string, status?: StorageObjectStatus) => Promise<number>;
86
+ /** Mark a row `deleted` (keeps the record). */
87
+ softDelete: (ref: ObjectRef) => Promise<void>;
88
+ /** Remove the row entirely. */
89
+ hardDelete: (ref: ObjectRef) => Promise<void>;
90
+ /** Remove a `pending` row (multipart abort path). */
91
+ deletePending: (ref: ObjectRef) => Promise<void>;
92
+ /** Pending rows created before `olderThan` or whose `expiresAt` passed. */
93
+ findStalePending: (input: {
94
+ olderThan: Date;
95
+ }) => Promise<StorageObject[]>;
96
+ /** Delete rows by ID (used by the purge job after `findStalePending`). */
97
+ deleteByIds: (ids: string[]) => Promise<void>;
98
+ };
99
+ /** Create a store bound to schema v1 (`db.orm("1.0.0")`). */
100
+ export declare function createStorageObjectStore(db: DimahS3DbClient): StorageObjectStore;
101
+ //# sourceMappingURL=storage-object-store.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"storage-object-store.d.ts","sourceRoot":"","sources":["../../src/store/storage-object-store.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,QAAQ,CAAC;AAC1C,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAC5C,OAAO,KAAK,EACV,aAAa,EACb,mBAAmB,EACpB,MAAM,yBAAyB,CAAC;AAGjC,iDAAiD;AACjD,MAAM,MAAM,eAAe,GAAG,WAAW,CAAC,OAAO,SAAS,CAAC,CAAC;AAE5D,6DAA6D;AAC7D,MAAM,MAAM,SAAS,GAAG;IACtB,MAAM,EAAE,MAAM,CAAC;IACf,GAAG,EAAE,MAAM,CAAC;CACb,CAAC;AAEF,MAAM,MAAM,kBAAkB,GAAG,SAAS,GAAG;IAC3C,KAAK,EAAE,MAAM,CAAC;IACd,WAAW,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC5B,wEAAwE;IACxE,YAAY,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC7B,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,IAAI,CAAC;IAC1C,GAAG,CAAC,EAAE,MAAM,GAAG,IAAI,CA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KAAK,OAAO,CAAC,aAAa,EAAE,CAAC,CAAC;IAC3E,0EAA0E;IAC1E,WAAW,EAAE,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;CAC/C,CAAC;AAQF,6DAA6D;AAC7D,wBAAgB,wBAAwB,CACtC,EAAE,EAAE,eAAe,GAClB,kBAAkB,CAsLpB"}
@@ -0,0 +1,67 @@
1
+ /** Lifecycle status of a tracked object. */
2
+ export type StorageObjectStatus = "pending" | "active" | "deleted";
3
+ /**
4
+ * App-facing record for a `storage_object` row.
5
+ * Sizes are normalized from bigint to number (safe — S3 caps objects at 5 TB).
6
+ */
7
+ export type StorageObject = {
8
+ id: string;
9
+ /** Owner, e.g. `user:123` or `org:acme`. */
10
+ scope: string;
11
+ bucket: string;
12
+ key: string;
13
+ contentType: string | null;
14
+ /** Verified size in bytes (set on confirm). */
15
+ size: number | null;
16
+ eTag: string | null;
17
+ filename: string | null;
18
+ status: StorageObjectStatus;
19
+ /** App-specific labels, visibility, publicUrl, etc. */
20
+ metadata: Record<string, unknown> | null;
21
+ acl: string | null;
22
+ /** Multipart upload ID while status = pending. */
23
+ uploadId: string | null;
24
+ /** Client-declared size before confirm (quota reservation). */
25
+ declaredSize: number | null;
26
+ confirmedAt: Date | null;
27
+ /** Pending TTL or temporary object expiry. */
28
+ expiresAt: Date | null;
29
+ createdAt: Date;
30
+ updatedAt: Date;
31
+ deletedAt: Date | null;
32
+ };
33
+ /**
34
+ * Resolves the owning scope for a request (e.g. from a session cookie).
35
+ * Return `null` to reject the request as unauthenticated (401).
36
+ */
37
+ export type ScopeResolver = (request: Request) => string | null | Promise<string | null>;
38
+ /**
39
+ * Browser-facing object row from `GET plugins/db/objects`.
40
+ * Deliberately narrower than {@link StorageObject} — omits server-only fields.
41
+ */
42
+ export type DbClientObject = {
43
+ id: string;
44
+ bucket: string;
45
+ key: string;
46
+ filename: string | null;
47
+ contentType: string | null;
48
+ size: number | null;
49
+ declaredSize: number | null;
50
+ status: StorageObjectStatus;
51
+ /** ISO-8601 timestamp. */
52
+ createdAt: string;
53
+ };
54
+ export type DbClientListResponse = {
55
+ scope: string;
56
+ usage: {
57
+ totalBytes: number;
58
+ objectCount: number;
59
+ };
60
+ objects: DbClientObject[];
61
+ };
62
+ export type DbClientListInput = {
63
+ status?: StorageObjectStatus;
64
+ limit?: number;
65
+ offset?: number;
66
+ };
67
+ //# sourceMappingURL=storage-object.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"storage-object.d.ts","sourceRoot":"","sources":["../../src/types/storage-object.ts"],"names":[],"mappings":"AAAA,4CAA4C;AAC5C,MAAM,MAAM,mBAAmB,GAAG,SAAS,GAAG,QAAQ,GAAG,SAAS,CAAC;AAEnE;;;GAGG;AACH,MAAM,MAAM,aAAa,GAAG;IAC1B,EAAE,EAAE,MAAM,CAAC;IACX,4CAA4C;IAC5C,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,GAAG,EAAE,MAAM,CAAC;IACZ,WAAW,EAAE,MAAM,GAAG,IAAI,CAAC;IAC3B,+CAA+C;IAC/C,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IACpB,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IACpB,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;IACxB,MAAM,EAAE,mBAAmB,CAAC;IAC5B,uDAAuD;IACvD,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,IAAI,CAAC;IACzC,GAAG,EAAE,MAAM,GAAG,IAAI,CAAC;IACnB,kDAAkD;IAClD,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;IACxB,+DAA+D;IAC/D,YAAY,EAAE,MAAM,GAAG,IAAI,CAAC;IAC5B,WAAW,EAAE,IAAI,GAAG,IAAI,CAAC;IACzB,8CAA8C;IAC9C,SAAS,EAAE,IAAI,GAAG,IAAI,CAAC;IACvB,SAAS,EAAE,IAAI,CAAC;IAChB,SAAS,EAAE,IAAI,CAAC;IAChB,SAAS,EAAE,IAAI,GAAG,IAAI,CAAC;CACxB,CAAC;AAEF;;;GAGG;AACH,MAAM,MAAM,aAAa,GAAG,CAC1B,OAAO,EAAE,OAAO,KACb,MAAM,GAAG,IAAI,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAAC;AAE5C;;;GAGG;AACH,MAAM,MAAM,cAAc,GAAG;IAC3B,EAAE,EAAE,MAAM,CAAC;IACX,MAAM,EAAE,MAAM,CAAC;IACf,GAAG,EAAE,MAAM,CAAC;IACZ,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;IACxB,WAAW,EAAE,MAAM,GAAG,IAAI,CAAC;IAC3B,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IACpB,YAAY,EAAE,MAAM,GAAG,IAAI,CAAC;IAC5B,MAAM,EAAE,mBAAmB,CAAC;IAC5B,0BAA0B;IAC1B,SAAS,EAAE,MAAM,CAAC;CACnB,CAAC;AAEF,MAAM,MAAM,oBAAoB,GAAG;IACjC,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,EAAE;QAAE,UAAU,EAAE,MAAM,CAAC;QAAC,WAAW,EAAE,MAAM,CAAA;KAAE,CAAC;IACnD,OAAO,EAAE,cAAc,EAAE,CAAC;CAC3B,CAAC;AAEF,MAAM,MAAM,iBAAiB,GAAG;IAC9B,MAAM,CAAC,EAAE,mBAAmB,CAAC;IAC7B,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB,CAAC"}
package/package.json ADDED
@@ -0,0 +1,77 @@
1
+ {
2
+ "name": "@dimah-s3/db",
3
+ "version": "0.1.0",
4
+ "description": "Optional FumaDB persistence for dimah-s3 — db() plugin + storage_object schema",
5
+ "keywords": [
6
+ "s3",
7
+ "upload",
8
+ "database",
9
+ "fumadb",
10
+ "drizzle"
11
+ ],
12
+ "author": "Hamidrezakz",
13
+ "license": "MIT",
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "https://github.com/dimah-kz/dimah-s3.git",
17
+ "directory": "packages/db"
18
+ },
19
+ "homepage": "https://github.com/dimah-kz/dimah-s3/tree/main/packages/db",
20
+ "bugs": {
21
+ "url": "https://github.com/dimah-kz/dimah-s3/issues"
22
+ },
23
+ "sideEffects": false,
24
+ "type": "module",
25
+ "exports": {
26
+ ".": {
27
+ "types": "./dist/index.d.ts",
28
+ "default": "./dist/index.js"
29
+ },
30
+ "./client": {
31
+ "types": "./dist/client/index.d.ts",
32
+ "default": "./dist/client/index.js"
33
+ },
34
+ "./cli": {
35
+ "types": "./dist/cli.d.ts",
36
+ "default": "./dist/cli.js"
37
+ }
38
+ },
39
+ "files": [
40
+ "dist",
41
+ "src/schema/examples"
42
+ ],
43
+ "dependencies": {
44
+ "@paralleldrive/cuid2": "^3.3.0",
45
+ "fumadb": "^0.4.0",
46
+ "@dimah-s3/core": "0.1.0"
47
+ },
48
+ "peerDependencies": {
49
+ "@dimah-s3/server": "0.1.0",
50
+ "drizzle-orm": "^0.44.0 || ^0.45.0"
51
+ },
52
+ "peerDependenciesMeta": {
53
+ "drizzle-orm": {
54
+ "optional": true
55
+ }
56
+ },
57
+ "devDependencies": {
58
+ "@types/node": "^26.1.2",
59
+ "drizzle-orm": "^0.45.2",
60
+ "eslint": "^10.8.0",
61
+ "tsup": "^8.5.1",
62
+ "typescript": "7.0.2",
63
+ "vitest": "^4.1.10",
64
+ "@dimah-s3/server": "0.1.0",
65
+ "@workspace/typescript-config": "0.0.1",
66
+ "@workspace/eslint-config": "0.0.1"
67
+ },
68
+ "publishConfig": {
69
+ "access": "public"
70
+ },
71
+ "scripts": {
72
+ "build": "tsup --config tsup.config.ts && tsc --emitDeclarationOnly -p tsconfig.build.json",
73
+ "lint": "eslint",
74
+ "check-types": "tsc --noEmit",
75
+ "test": "vitest run"
76
+ }
77
+ }
@@ -0,0 +1,10 @@
1
+ # Schema examples
2
+
3
+ Copy-paste `storage_object` + recommended indexes. Not imported at runtime.
4
+
5
+ | File | ORM |
6
+ | ---------------------------------- | ---------------- |
7
+ | [`drizzle.ts`](./drizzle.ts) | Drizzle (SQLite) |
8
+ | [`schema.prisma`](./schema.prisma) | Prisma |
9
+
10
+ Docs: [Setup](https://dimah-s3.dev/docs/db/setup). Drizzle needs `fumadb/cuid` → `@paralleldrive/cuid2` in tsconfig paths.
@@ -0,0 +1,75 @@
1
+ /**
2
+ * Reference Drizzle schema — same shape as FumaDB `generate` (SQLite), plus
3
+ * the recommended secondary indexes. Not imported by `@dimah-s3/db` at runtime.
4
+ *
5
+ * `fumadb/cuid` is not a real fumadb export; map it to `@paralleldrive/cuid2`
6
+ * in tsconfig (this package and `examples/with-db` already do).
7
+ *
8
+ * @see ../v1.ts
9
+ * @see ./schema.prisma
10
+ */
11
+ import {
12
+ sqliteTable,
13
+ text,
14
+ blob,
15
+ integer,
16
+ uniqueIndex,
17
+ index,
18
+ } from "drizzle-orm/sqlite-core";
19
+ import { createId } from "fumadb/cuid";
20
+
21
+ export const storageObject = sqliteTable(
22
+ "storage_object",
23
+ {
24
+ id: text("id", { length: 255 })
25
+ .primaryKey()
26
+ .notNull()
27
+ .$defaultFn(() => createId()),
28
+ scope: text("scope").notNull(),
29
+ bucket: text("bucket").notNull(),
30
+ key: text("key").notNull(),
31
+ contentType: text("content_type"),
32
+ size: blob("size", { mode: "bigint" }),
33
+ eTag: text("e_tag"),
34
+ filename: text("filename"),
35
+ status: text("status").notNull(),
36
+ metadata: blob("metadata", { mode: "json" }),
37
+ acl: text("acl"),
38
+ uploadId: text("upload_id"),
39
+ declaredSize: blob("declared_size", { mode: "bigint" }),
40
+ confirmedAt: integer("confirmed_at", { mode: "timestamp" }),
41
+ expiresAt: integer("expires_at", { mode: "timestamp" }),
42
+ createdAt: integer("created_at", { mode: "timestamp" })
43
+ .notNull()
44
+ .defaultNow(),
45
+ updatedAt: integer("updated_at", { mode: "timestamp" })
46
+ .notNull()
47
+ .defaultNow(),
48
+ deletedAt: integer("deleted_at", { mode: "timestamp" }),
49
+ },
50
+ (table) => [
51
+ uniqueIndex("storage_object_bucket_key_uk").on(table.bucket, table.key),
52
+ // Consumer-added — FumaDB generate does not emit these (yet).
53
+ index("storage_object_scope_status_created_idx").on(
54
+ table.scope,
55
+ table.status,
56
+ table.createdAt,
57
+ ),
58
+ index("storage_object_status_expires_idx").on(
59
+ table.status,
60
+ table.expiresAt,
61
+ ),
62
+ index("storage_object_status_created_idx").on(
63
+ table.status,
64
+ table.createdAt,
65
+ ),
66
+ ],
67
+ );
68
+
69
+ export const private_dimah_s3_settings = sqliteTable(
70
+ "private_dimah_s3_settings",
71
+ {
72
+ id: text("id", { length: 255 }).primaryKey().notNull(),
73
+ version: text("version", { length: 255 }).notNull().default("1.0.0"),
74
+ },
75
+ );
@@ -0,0 +1,53 @@
1
+ // Reference Prisma schema for `storage_object`.
2
+ //
3
+ // Merge into your app's schema.prisma (or paste the model) after FumaDB
4
+ // `generate`. Not used by `@dimah-s3/db` at runtime.
5
+ //
6
+ // Default dialect here is PostgreSQL (`@db.VarChar`). For SQLite/MySQL, keep
7
+ // the same @@unique / @@index maps and adjust native types as needed.
8
+ //
9
+ // See: ../v1.ts — FumaDB source of truth (unique only)
10
+ // See: ./drizzle.ts — same table + indexes for Drizzle (SQLite)
11
+
12
+ generator client {
13
+ provider = "prisma-client-js"
14
+ }
15
+
16
+ datasource db {
17
+ provider = "postgresql"
18
+ }
19
+
20
+ model StorageObject {
21
+ id String @id @map("id") @db.VarChar(255)
22
+ scope String @map("scope")
23
+ bucket String @map("bucket")
24
+ key String @map("key")
25
+ contentType String? @map("content_type")
26
+ size BigInt? @map("size")
27
+ eTag String? @map("e_tag")
28
+ filename String? @map("filename")
29
+ status String @map("status")
30
+ metadata Json? @map("metadata")
31
+ acl String? @map("acl")
32
+ uploadId String? @map("upload_id")
33
+ declaredSize BigInt? @map("declared_size")
34
+ confirmedAt DateTime? @map("confirmed_at")
35
+ expiresAt DateTime? @map("expires_at")
36
+ createdAt DateTime @default(now()) @map("created_at")
37
+ updatedAt DateTime @updatedAt @map("updated_at")
38
+ deletedAt DateTime? @map("deleted_at")
39
+
40
+ @@unique([bucket, key], map: "storage_object_bucket_key_uk")
41
+ @@index([scope, status, createdAt], map: "storage_object_scope_status_created_idx")
42
+ @@index([status, expiresAt], map: "storage_object_status_expires_idx")
43
+ @@index([status, createdAt], map: "storage_object_status_created_idx")
44
+ @@map("storage_object")
45
+ }
46
+
47
+ /// FumaDB settings row — emitted by CLI alongside `storage_object`.
48
+ model PrivateDimahS3Settings {
49
+ id String @id @map("id") @db.VarChar(255)
50
+ version String @default("1.0.0") @map("version") @db.VarChar(255)
51
+
52
+ @@map("private_dimah_s3_settings")
53
+ }