@cedarjs/storage 0.0.4

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 (52) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +123 -0
  3. package/dist/UrlSigner.d.ts +46 -0
  4. package/dist/UrlSigner.d.ts.map +1 -0
  5. package/dist/UrlSigner.js +106 -0
  6. package/dist/adapters/BaseStorageAdapter.d.ts +31 -0
  7. package/dist/adapters/BaseStorageAdapter.d.ts.map +1 -0
  8. package/dist/adapters/BaseStorageAdapter.js +19 -0
  9. package/dist/adapters/FileSystemStorage/FileSystemStorage.d.ts +16 -0
  10. package/dist/adapters/FileSystemStorage/FileSystemStorage.d.ts.map +1 -0
  11. package/dist/adapters/FileSystemStorage/FileSystemStorage.js +38 -0
  12. package/dist/adapters/MemoryStorage/MemoryStorage.d.ts +15 -0
  13. package/dist/adapters/MemoryStorage/MemoryStorage.d.ts.map +1 -0
  14. package/dist/adapters/MemoryStorage/MemoryStorage.js +33 -0
  15. package/dist/cjs/UrlSigner.d.ts +46 -0
  16. package/dist/cjs/UrlSigner.d.ts.map +1 -0
  17. package/dist/cjs/UrlSigner.js +142 -0
  18. package/dist/cjs/adapters/BaseStorageAdapter.d.ts +31 -0
  19. package/dist/cjs/adapters/BaseStorageAdapter.d.ts.map +1 -0
  20. package/dist/cjs/adapters/BaseStorageAdapter.js +53 -0
  21. package/dist/cjs/adapters/FileSystemStorage/FileSystemStorage.d.ts +16 -0
  22. package/dist/cjs/adapters/FileSystemStorage/FileSystemStorage.d.ts.map +1 -0
  23. package/dist/cjs/adapters/FileSystemStorage/FileSystemStorage.js +72 -0
  24. package/dist/cjs/adapters/MemoryStorage/MemoryStorage.d.ts +15 -0
  25. package/dist/cjs/adapters/MemoryStorage/MemoryStorage.d.ts.map +1 -0
  26. package/dist/cjs/adapters/MemoryStorage/MemoryStorage.js +67 -0
  27. package/dist/cjs/createSavers.d.ts +15 -0
  28. package/dist/cjs/createSavers.d.ts.map +1 -0
  29. package/dist/cjs/createSavers.js +75 -0
  30. package/dist/cjs/fileToDataUri.d.ts +3 -0
  31. package/dist/cjs/fileToDataUri.d.ts.map +1 -0
  32. package/dist/cjs/fileToDataUri.js +32 -0
  33. package/dist/cjs/index.d.ts +40 -0
  34. package/dist/cjs/index.d.ts.map +1 -0
  35. package/dist/cjs/index.js +50 -0
  36. package/dist/cjs/package.json +1 -0
  37. package/dist/cjs/prismaExtension.d.ts +29 -0
  38. package/dist/cjs/prismaExtension.d.ts.map +1 -0
  39. package/dist/cjs/prismaExtension.js +240 -0
  40. package/dist/createSavers.d.ts +15 -0
  41. package/dist/createSavers.d.ts.map +1 -0
  42. package/dist/createSavers.js +50 -0
  43. package/dist/fileToDataUri.d.ts +3 -0
  44. package/dist/fileToDataUri.d.ts.map +1 -0
  45. package/dist/fileToDataUri.js +8 -0
  46. package/dist/index.d.ts +40 -0
  47. package/dist/index.d.ts.map +1 -0
  48. package/dist/index.js +25 -0
  49. package/dist/prismaExtension.d.ts +29 -0
  50. package/dist/prismaExtension.d.ts.map +1 -0
  51. package/dist/prismaExtension.js +216 -0
  52. package/package.json +77 -0
@@ -0,0 +1,31 @@
1
+ /**
2
+ * The storage adapter will just save the file and return
3
+ * {
4
+ * fileId: string,
5
+ * location: string, // depending on storage it could be a path
6
+ * }
7
+ */
8
+ import mime from 'mime-types';
9
+ export type AdapterResult = {
10
+ location: string;
11
+ };
12
+ export type SaveOptionsOverride = {
13
+ fileName?: string;
14
+ path?: string;
15
+ };
16
+ export type AdapterOptions = {
17
+ baseDir: string;
18
+ };
19
+ export declare abstract class BaseStorageAdapter {
20
+ adapterOpts: AdapterOptions;
21
+ constructor(adapterOpts: AdapterOptions);
22
+ getAdapterOptions(): AdapterOptions;
23
+ generateFileNameWithExtension(saveOpts: SaveOptionsOverride | undefined, file: File): string;
24
+ abstract save(file: File, saveOpts?: SaveOptionsOverride): Promise<AdapterResult>;
25
+ abstract remove(fileLocation: AdapterResult['location']): Promise<void>;
26
+ abstract read(fileLocation: AdapterResult['location']): Promise<{
27
+ contents: Buffer | string;
28
+ type: ReturnType<typeof mime.lookup>;
29
+ }>;
30
+ }
31
+ //# sourceMappingURL=BaseStorageAdapter.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"BaseStorageAdapter.d.ts","sourceRoot":"","sources":["../../../src/adapters/BaseStorageAdapter.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,IAAI,MAAM,YAAY,CAAA;AAG7B,MAAM,MAAM,aAAa,GAAG;IAC1B,QAAQ,EAAE,MAAM,CAAA;CACjB,CAAA;AAED,MAAM,MAAM,mBAAmB,GAAG;IAChC,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,IAAI,CAAC,EAAE,MAAM,CAAA;CACd,CAAA;AAED,MAAM,MAAM,cAAc,GAAG;IAC3B,OAAO,EAAE,MAAM,CAAA;CAChB,CAAA;AAED,8BAAsB,kBAAkB;IACtC,WAAW,EAAE,cAAc,CAAA;gBACf,WAAW,EAAE,cAAc;IAIvC,iBAAiB;IAIjB,6BAA6B,CAC3B,QAAQ,EAAE,mBAAmB,GAAG,SAAS,EACzC,IAAI,EAAE,IAAI;IASZ,QAAQ,CAAC,IAAI,CACX,IAAI,EAAE,IAAI,EACV,QAAQ,CAAC,EAAE,mBAAmB,GAC7B,OAAO,CAAC,aAAa,CAAC;IACzB,QAAQ,CAAC,MAAM,CAAC,YAAY,EAAE,aAAa,CAAC,UAAU,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC;IACvE,QAAQ,CAAC,IAAI,CAAC,YAAY,EAAE,aAAa,CAAC,UAAU,CAAC,GAAG,OAAO,CAAC;QAC9D,QAAQ,EAAE,MAAM,GAAG,MAAM,CAAA;QACzB,IAAI,EAAE,UAAU,CAAC,OAAO,IAAI,CAAC,MAAM,CAAC,CAAA;KACrC,CAAC;CACH"}
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var BaseStorageAdapter_exports = {};
30
+ __export(BaseStorageAdapter_exports, {
31
+ BaseStorageAdapter: () => BaseStorageAdapter
32
+ });
33
+ module.exports = __toCommonJS(BaseStorageAdapter_exports);
34
+ var import_mime_types = __toESM(require("mime-types"), 1);
35
+ var import_ulid = require("ulid");
36
+ class BaseStorageAdapter {
37
+ adapterOpts;
38
+ constructor(adapterOpts) {
39
+ this.adapterOpts = adapterOpts;
40
+ }
41
+ getAdapterOptions() {
42
+ return this.adapterOpts;
43
+ }
44
+ generateFileNameWithExtension(saveOpts, file) {
45
+ const fileName = saveOpts?.fileName || (0, import_ulid.ulid)();
46
+ const extension = import_mime_types.default.extension(file.type) ? `.${import_mime_types.default.extension(file.type)}` : "";
47
+ return `${fileName}${extension}`;
48
+ }
49
+ }
50
+ // Annotate the CommonJS export names for ESM import in node:
51
+ 0 && (module.exports = {
52
+ BaseStorageAdapter
53
+ });
@@ -0,0 +1,16 @@
1
+ import type { SaveOptionsOverride } from '../BaseStorageAdapter.js';
2
+ import { BaseStorageAdapter } from '../BaseStorageAdapter.js';
3
+ export declare class FileSystemStorage extends BaseStorageAdapter implements BaseStorageAdapter {
4
+ constructor(opts: {
5
+ baseDir: string;
6
+ });
7
+ save(file: File, saveOverride?: SaveOptionsOverride): Promise<{
8
+ location: string;
9
+ }>;
10
+ read(filePath: string): Promise<{
11
+ contents: Buffer;
12
+ type: string | false;
13
+ }>;
14
+ remove(filePath: string): Promise<void>;
15
+ }
16
+ //# sourceMappingURL=FileSystemStorage.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"FileSystemStorage.d.ts","sourceRoot":"","sources":["../../../../src/adapters/FileSystemStorage/FileSystemStorage.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAA;AACnE,OAAO,EAAE,kBAAkB,EAAE,MAAM,0BAA0B,CAAA;AAE7D,qBAAa,iBACX,SAAQ,kBACR,YAAW,kBAAkB;gBAEjB,IAAI,EAAE;QAAE,OAAO,EAAE,MAAM,CAAA;KAAE;IAQ/B,IAAI,CAAC,IAAI,EAAE,IAAI,EAAE,YAAY,CAAC,EAAE,mBAAmB;;;IAanD,IAAI,CAAC,QAAQ,EAAE,MAAM;;;;IAOrB,MAAM,CAAC,QAAQ,EAAE,MAAM;CAG9B"}
@@ -0,0 +1,72 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var FileSystemStorage_exports = {};
30
+ __export(FileSystemStorage_exports, {
31
+ FileSystemStorage: () => FileSystemStorage
32
+ });
33
+ module.exports = __toCommonJS(FileSystemStorage_exports);
34
+ var import_node_fs = require("node:fs");
35
+ var import_promises = __toESM(require("node:fs/promises"), 1);
36
+ var import_node_path = __toESM(require("node:path"), 1);
37
+ var import_mime_types = __toESM(require("mime-types"), 1);
38
+ var import_project_config = require("@cedarjs/project-config");
39
+ var import_BaseStorageAdapter = require("../BaseStorageAdapter.js");
40
+ class FileSystemStorage extends import_BaseStorageAdapter.BaseStorageAdapter {
41
+ constructor(opts) {
42
+ super(opts);
43
+ if (!(0, import_node_fs.existsSync)(opts.baseDir)) {
44
+ const posixBaseDir = (0, import_project_config.ensurePosixPath)(opts.baseDir);
45
+ console.log("Creating baseDir >", posixBaseDir);
46
+ (0, import_node_fs.mkdirSync)(posixBaseDir, { recursive: true });
47
+ }
48
+ }
49
+ async save(file, saveOverride) {
50
+ const fileName = this.generateFileNameWithExtension(saveOverride, file);
51
+ const location = import_node_path.default.join(
52
+ (0, import_project_config.ensurePosixPath)(saveOverride?.path || this.adapterOpts.baseDir),
53
+ fileName
54
+ );
55
+ const nodeBuffer = await file.arrayBuffer();
56
+ await import_promises.default.writeFile(location, Buffer.from(nodeBuffer));
57
+ return { location };
58
+ }
59
+ async read(filePath) {
60
+ return {
61
+ contents: await import_promises.default.readFile(filePath),
62
+ type: import_mime_types.default.lookup(filePath)
63
+ };
64
+ }
65
+ async remove(filePath) {
66
+ await import_promises.default.unlink(filePath);
67
+ }
68
+ }
69
+ // Annotate the CommonJS export names for ESM import in node:
70
+ 0 && (module.exports = {
71
+ FileSystemStorage
72
+ });
@@ -0,0 +1,15 @@
1
+ import { BaseStorageAdapter } from '../BaseStorageAdapter.js';
2
+ import type { SaveOptionsOverride } from '../BaseStorageAdapter.js';
3
+ export declare class MemoryStorage extends BaseStorageAdapter implements BaseStorageAdapter {
4
+ store: Record<string, any>;
5
+ save(file: File, saveOpts?: SaveOptionsOverride): Promise<{
6
+ location: string;
7
+ }>;
8
+ remove(filePath: string): Promise<void>;
9
+ read(filePath: string): Promise<{
10
+ contents: any;
11
+ type: string | false;
12
+ }>;
13
+ clear(): Promise<void>;
14
+ }
15
+ //# sourceMappingURL=MemoryStorage.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"MemoryStorage.d.ts","sourceRoot":"","sources":["../../../../src/adapters/MemoryStorage/MemoryStorage.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,kBAAkB,EAAE,MAAM,0BAA0B,CAAA;AAC7D,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAA;AAEnE,qBAAa,aACX,SAAQ,kBACR,YAAW,kBAAkB;IAE7B,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAK;IAEzB,IAAI,CAAC,IAAI,EAAE,IAAI,EAAE,QAAQ,CAAC,EAAE,mBAAmB;;;IAgB/C,MAAM,CAAC,QAAQ,EAAE,MAAM;IAIvB,IAAI,CAAC,QAAQ,EAAE,MAAM;;;;IAOrB,KAAK;CAGZ"}
@@ -0,0 +1,67 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var MemoryStorage_exports = {};
30
+ __export(MemoryStorage_exports, {
31
+ MemoryStorage: () => MemoryStorage
32
+ });
33
+ module.exports = __toCommonJS(MemoryStorage_exports);
34
+ var import_node_path = __toESM(require("node:path"), 1);
35
+ var import_mime_types = __toESM(require("mime-types"), 1);
36
+ var import_BaseStorageAdapter = require("../BaseStorageAdapter.js");
37
+ class MemoryStorage extends import_BaseStorageAdapter.BaseStorageAdapter {
38
+ store = {};
39
+ async save(file, saveOpts) {
40
+ const fileName = this.generateFileNameWithExtension(saveOpts, file);
41
+ const location = import_node_path.default.join(
42
+ saveOpts?.path || this.adapterOpts.baseDir,
43
+ fileName
44
+ );
45
+ const nodeBuffer = await file.arrayBuffer();
46
+ this.store[location] = Buffer.from(nodeBuffer);
47
+ return {
48
+ location
49
+ };
50
+ }
51
+ async remove(filePath) {
52
+ delete this.store[filePath];
53
+ }
54
+ async read(filePath) {
55
+ return {
56
+ contents: this.store[filePath],
57
+ type: import_mime_types.default.lookup(filePath)
58
+ };
59
+ }
60
+ async clear() {
61
+ this.store = {};
62
+ }
63
+ }
64
+ // Annotate the CommonJS export names for ESM import in node:
65
+ 0 && (module.exports = {
66
+ MemoryStorage
67
+ });
@@ -0,0 +1,15 @@
1
+ import type { SaveOptionsOverride, BaseStorageAdapter } from './adapters/BaseStorageAdapter.js';
2
+ import type { ModelNames, UploadsConfig } from './prismaExtension.js';
3
+ type MakeFilesString<T> = {
4
+ [K in keyof T]: T[K] extends File ? string : T[K];
5
+ };
6
+ export declare const createFileListSaver: (storage: BaseStorageAdapter) => (files?: File[], pathOverrideOnly?: {
7
+ path?: string;
8
+ }) => Promise<string[]>;
9
+ export declare const createUploadSavers: <MNames extends ModelNames = ModelNames>(uploadConfig: UploadsConfig<MNames>, storage: BaseStorageAdapter) => { [K in `for${Capitalize<MNames>}`]: <T extends Record<string, any>>(data: T, overrideSaveOptions?: SaveOptionsOverride) => Promise<MakeFilesString<T>>; } & {
10
+ inList: (files?: File[], pathOverrideOnly?: {
11
+ path?: string;
12
+ }) => Promise<string[]>;
13
+ };
14
+ export {};
15
+ //# sourceMappingURL=createSavers.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createSavers.d.ts","sourceRoot":"","sources":["../../src/createSavers.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EACV,mBAAmB,EACnB,kBAAkB,EACnB,MAAM,kCAAkC,CAAA;AACzC,OAAO,KAAK,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAA;AAGrE,KAAK,eAAe,CAAC,CAAC,IAAI;KACvB,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,SAAS,IAAI,GAAG,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC;CAClD,CAAA;AAED,eAAO,MAAM,mBAAmB,YAAa,kBAAkB,cACxC,IAAI,EAAE,qBAA0B;IAAE,IAAI,CAAC,EAAE,MAAM,CAAA;CAAE,sBAUvE,CAAA;AAMD,eAAO,MAAM,kBAAkB,GAAI,MAAM,SAAS,UAAU,6BAC5C,aAAa,CAAC,MAAM,CAAC,WAC1B,kBAAkB,QAMxB,CAAC,kCAAwB,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,QAC/C,CAAC,wBACe,mBAAmB,KACtC,OAAO,CAAC,eAAe,CAAC,CAAC,CAAC,CAAC;qBA3Bb,IAAI,EAAE,qBAA0B;QAAE,IAAI,CAAC,EAAE,MAAM,CAAA;KAAE;CA2EvE,CAAA"}
@@ -0,0 +1,75 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var createSavers_exports = {};
20
+ __export(createSavers_exports, {
21
+ createFileListSaver: () => createFileListSaver,
22
+ createUploadSavers: () => createUploadSavers
23
+ });
24
+ module.exports = __toCommonJS(createSavers_exports);
25
+ var import_ulid = require("ulid");
26
+ const createFileListSaver = (storage) => {
27
+ return async (files = [], pathOverrideOnly) => {
28
+ const locations = await Promise.all(
29
+ files.map(async (file) => {
30
+ const { location } = await storage.save(file, pathOverrideOnly);
31
+ return location;
32
+ })
33
+ );
34
+ return locations;
35
+ };
36
+ };
37
+ const createUploadSavers = (uploadConfig, storage) => {
38
+ const savers = {};
39
+ Object.keys(uploadConfig).forEach((model) => {
40
+ const modelKey = model;
41
+ const currentModelConfig = uploadConfig[modelKey];
42
+ if (!currentModelConfig) {
43
+ return;
44
+ }
45
+ const currentModelUploadFields = Array.isArray(currentModelConfig.fields) ? currentModelConfig.fields : [currentModelConfig.fields];
46
+ const capitalCaseModel = `${model.charAt(0).toUpperCase() + model.slice(1)}`;
47
+ const saverKey = `for${capitalCaseModel}`;
48
+ savers[saverKey] = async (data, overrideSaveOptions) => {
49
+ const updatedFields = {};
50
+ for (const field of currentModelUploadFields) {
51
+ if (data[field]) {
52
+ const file = data[field];
53
+ const saveOptions = overrideSaveOptions || {
54
+ fileName: `${model}-${field}-${(0, import_ulid.ulid)()}`
55
+ };
56
+ const { location } = await storage.save(file, saveOptions);
57
+ updatedFields[field] = location;
58
+ }
59
+ }
60
+ return {
61
+ ...data,
62
+ ...updatedFields
63
+ };
64
+ };
65
+ });
66
+ return {
67
+ ...savers,
68
+ inList: createFileListSaver(storage)
69
+ };
70
+ };
71
+ // Annotate the CommonJS export names for ESM import in node:
72
+ 0 && (module.exports = {
73
+ createFileListSaver,
74
+ createUploadSavers
75
+ });
@@ -0,0 +1,3 @@
1
+ import type { BaseStorageAdapter } from './adapters/BaseStorageAdapter.js';
2
+ export declare function fileToDataUri(filePath: string, storage: BaseStorageAdapter): Promise<string>;
3
+ //# sourceMappingURL=fileToDataUri.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fileToDataUri.d.ts","sourceRoot":"","sources":["../../src/fileToDataUri.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,kCAAkC,CAAA;AAE1E,wBAAsB,aAAa,CACjC,QAAQ,EAAE,MAAM,EAChB,OAAO,EAAE,kBAAkB,mBAO5B"}
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var fileToDataUri_exports = {};
20
+ __export(fileToDataUri_exports, {
21
+ fileToDataUri: () => fileToDataUri
22
+ });
23
+ module.exports = __toCommonJS(fileToDataUri_exports);
24
+ async function fileToDataUri(filePath, storage) {
25
+ const { contents, type: mimeType } = await storage.read(filePath);
26
+ const base64Data = Buffer.from(contents).toString("base64");
27
+ return `data:${mimeType};base64,${base64Data}`;
28
+ }
29
+ // Annotate the CommonJS export names for ESM import in node:
30
+ 0 && (module.exports = {
31
+ fileToDataUri
32
+ });
@@ -0,0 +1,40 @@
1
+ import type { BaseStorageAdapter } from './adapters/BaseStorageAdapter.js';
2
+ import type { ModelNames, UploadConfigForModel, UploadsConfig } from './prismaExtension.js';
3
+ import type { UrlSigner } from './UrlSigner.js';
4
+ type SetupStorageOptions<MNames extends ModelNames> = {
5
+ uploadsConfig: UploadsConfig<MNames>;
6
+ storageAdapter: BaseStorageAdapter;
7
+ urlSigner?: UrlSigner;
8
+ };
9
+ export declare const setupStorage: <MNames extends ModelNames>({ uploadsConfig, storageAdapter, urlSigner, }: SetupStorageOptions<MNames>) => {
10
+ storagePrismaExtension: (client: any) => import("@prisma/client/extension").PrismaClientExtends<import("@prisma/client/runtime/library").InternalArgs<{ [K in MNames]: {
11
+ withDataUri: {
12
+ needs: Record<string, boolean>;
13
+ compute: (modelData: Record<string, unknown>) => <T>(this: T) => Promise<T>;
14
+ };
15
+ withSignedUrl: {
16
+ needs: Record<string, boolean>;
17
+ compute: (modelData: Record<string, unknown>) => <T>(this: T, signArgs?: {
18
+ expiresIn?: number;
19
+ }) => T;
20
+ };
21
+ }; }, {}, {}, {}> & import("@prisma/client/runtime/library").DefaultArgs>;
22
+ saveFiles: { [K in `for${Capitalize<MNames>}`]: <T extends Record<string, any>>(data: T, overrideSaveOptions?: import("./adapters/BaseStorageAdapter.js").SaveOptionsOverride) => Promise<{ [K_1 in keyof T]: T[K_1] extends File ? string : T[K_1]; }>; } & {
23
+ inList: (files?: File[], pathOverrideOnly?: {
24
+ path?: string;
25
+ }) => Promise<string[]>;
26
+ };
27
+ };
28
+ /**
29
+ * This utility function ensures that you receive accurate type suggestions for your savers.
30
+ * If you use the type UploadsConfig directly, you may receive suggestions for saveFiles.forY where Y hasn't been configured.
31
+ * By using this utility function, you will only receive suggestions for the models that you have configured.
32
+ *
33
+ * @param uploadsConfig The uploads configuration object.
34
+ * @returns The same uploads configuration object, but with filtered types
35
+ */
36
+ export declare function createUploadsConfig<T extends Partial<{
37
+ [K in ModelNames]?: UploadConfigForModel<K>;
38
+ }>>(uploadsConfig: T): T;
39
+ export type { ModelNames, UploadsConfig } from './prismaExtension.js';
40
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,kCAAkC,CAAA;AAE1E,OAAO,KAAK,EACV,UAAU,EACV,oBAAoB,EACpB,aAAa,EACd,MAAM,sBAAsB,CAAA;AAE7B,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAE/C,KAAK,mBAAmB,CAAC,MAAM,SAAS,UAAU,IAAI;IACpD,aAAa,EAAE,aAAa,CAAC,MAAM,CAAC,CAAA;IACpC,cAAc,EAAE,kBAAkB,CAAA;IAClC,SAAS,CAAC,EAAE,SAAS,CAAA;CACtB,CAAA;AAED,eAAO,MAAM,YAAY,GAAI,MAAM,SAAS,UAAU,iDAInD,mBAAmB,CAAC,MAAM,CAAC;;;;;;;;;;;;;;;gBARV,CAAC;;;CAqBpB,CAAA;AAED;;;;;;;GAOG;AACH,wBAAgB,mBAAmB,CACjC,CAAC,SAAS,OAAO,CAAC;KACf,CAAC,IAAI,UAAU,CAAC,CAAC,EAAE,oBAAoB,CAAC,CAAC,CAAC;CAC5C,CAAC,EACF,aAAa,EAAE,CAAC,GAAG,CAAC,CAErB;AAED,YAAY,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAA"}
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var index_exports = {};
20
+ __export(index_exports, {
21
+ createUploadsConfig: () => createUploadsConfig,
22
+ setupStorage: () => setupStorage
23
+ });
24
+ module.exports = __toCommonJS(index_exports);
25
+ var import_createSavers = require("./createSavers.js");
26
+ var import_prismaExtension = require("./prismaExtension.js");
27
+ const setupStorage = ({
28
+ uploadsConfig,
29
+ storageAdapter,
30
+ urlSigner
31
+ }) => {
32
+ const prismaExtension = (0, import_prismaExtension.createUploadsExtension)(
33
+ uploadsConfig,
34
+ storageAdapter,
35
+ urlSigner
36
+ );
37
+ const saveFiles = (0, import_createSavers.createUploadSavers)(uploadsConfig, storageAdapter);
38
+ return {
39
+ storagePrismaExtension: prismaExtension,
40
+ saveFiles
41
+ };
42
+ };
43
+ function createUploadsConfig(uploadsConfig) {
44
+ return uploadsConfig;
45
+ }
46
+ // Annotate the CommonJS export names for ESM import in node:
47
+ 0 && (module.exports = {
48
+ createUploadsConfig,
49
+ setupStorage
50
+ });
@@ -0,0 +1 @@
1
+ {"type":"commonjs"}
@@ -0,0 +1,29 @@
1
+ import { PrismaClient } from '@prisma/client';
2
+ import type { Prisma } from '@prisma/client';
3
+ import type * as runtime from '@prisma/client/runtime/library';
4
+ import type { BaseStorageAdapter } from './adapters/BaseStorageAdapter.js';
5
+ import type { UrlSigner } from './UrlSigner.js';
6
+ type FilterOutDollarPrefixed<T> = T extends `$${string}` ? never : T extends symbol ? never : T;
7
+ export type ModelNames = FilterOutDollarPrefixed<keyof PrismaClient>;
8
+ type PrismaModelFields<MName extends ModelNames> = keyof Prisma.Result<PrismaClient[MName], any, 'findFirstOrThrow'>;
9
+ export type UploadConfigForModel<TPrismaModelName extends ModelNames> = {
10
+ fields: PrismaModelFields<TPrismaModelName> | PrismaModelFields<TPrismaModelName>[];
11
+ };
12
+ export type UploadsConfig<MNames extends ModelNames = ModelNames> = {
13
+ [K in MNames]?: UploadConfigForModel<K>;
14
+ };
15
+ type WithSignedUrlArgs = {
16
+ expiresIn?: number;
17
+ };
18
+ export declare const createUploadsExtension: <MNames extends ModelNames = ModelNames>(config: UploadsConfig<MNames>, storageAdapter: BaseStorageAdapter, urlSigner?: UrlSigner) => (client: any) => import("@prisma/client/extension").PrismaClientExtends<runtime.InternalArgs<{ [K in MNames]: {
19
+ withDataUri: {
20
+ needs: Record<string, boolean>;
21
+ compute: (modelData: Record<string, unknown>) => <T>(this: T) => Promise<T>;
22
+ };
23
+ withSignedUrl: {
24
+ needs: Record<string, boolean>;
25
+ compute: (modelData: Record<string, unknown>) => <T>(this: T, signArgs?: WithSignedUrlArgs) => T;
26
+ };
27
+ }; }, {}, {}, {}> & runtime.DefaultArgs>;
28
+ export {};
29
+ //# sourceMappingURL=prismaExtension.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"prismaExtension.d.ts","sourceRoot":"","sources":["../../src/prismaExtension.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAA;AAC7C,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAA;AAE5C,OAAO,KAAK,KAAK,OAAO,MAAM,gCAAgC,CAAA;AAE9D,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,kCAAkC,CAAA;AAE1E,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAE/C,KAAK,uBAAuB,CAAC,CAAC,IAAI,CAAC,SAAS,IAAI,MAAM,EAAE,GACpD,KAAK,GACL,CAAC,SAAS,MAAM,GACd,KAAK,GACL,CAAC,CAAA;AAGP,MAAM,MAAM,UAAU,GAAG,uBAAuB,CAAC,MAAM,YAAY,CAAC,CAAA;AAEpE,KAAK,iBAAiB,CAAC,KAAK,SAAS,UAAU,IAAI,MAAM,MAAM,CAAC,MAAM,CACpE,YAAY,CAAC,KAAK,CAAC,EACnB,GAAG,EACH,kBAAkB,CACnB,CAAA;AAED,MAAM,MAAM,oBAAoB,CAAC,gBAAgB,SAAS,UAAU,IAAI;IACtE,MAAM,EACF,iBAAiB,CAAC,gBAAgB,CAAC,GACnC,iBAAiB,CAAC,gBAAgB,CAAC,EAAE,CAAA;CAC1C,CAAA;AAED,MAAM,MAAM,aAAa,CAAC,MAAM,SAAS,UAAU,GAAG,UAAU,IAAI;KACjE,CAAC,IAAI,MAAM,CAAC,CAAC,EAAE,oBAAoB,CAAC,CAAC,CAAC;CACxC,CAAA;AAED,KAAK,iBAAiB,GAAG;IACvB,SAAS,CAAC,EAAE,MAAM,CAAA;CACnB,CAAA;AAED,eAAO,MAAM,sBAAsB,GAAI,MAAM,SAAS,UAAU,uBACtD,aAAa,CAAC,MAAM,CAAC,kBACb,kBAAkB,cACtB,SAAS,qGAOlB,CAAC;iBACa;QACX,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;QAC9B,OAAO,EAAE,CACP,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAC/B,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,CAAA;KAChC;mBACc;QACb,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;QAC9B,OAAO,EAAE,CACP,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAC/B,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,EAAE,QAAQ,CAAC,EAAE,iBAAiB,KAAK,CAAC,CAAA;KACrD;wCA4RN,CAAA"}