@jrmc/adonis-attachment 6.0.0-alpha.0 → 6.0.0-alpha.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/build/commands/make/attachment_v5_migration.d.ts.map +1 -1
- package/build/commands/make/attachment_v5_migration.js +6 -3
- package/build/commands/make/attachment_v5_migration.js.map +1 -1
- package/build/commands/make/attachments_table.d.ts.map +1 -1
- package/build/commands/make/attachments_table.js +8 -2
- package/build/commands/make/attachments_table.js.map +1 -1
- package/build/index.d.ts +23 -22
- package/build/index.d.ts.map +1 -1
- package/build/index.js +15 -14
- package/build/index.js.map +1 -1
- package/build/providers/attachment_provider.d.ts.map +1 -1
- package/build/providers/attachment_provider.js +16 -0
- package/build/providers/attachment_provider.js.map +1 -1
- package/build/src/core/attachment.d.ts +37 -2
- package/build/src/core/attachment.d.ts.map +1 -1
- package/build/src/core/attachment.js +108 -5
- package/build/src/core/attachment.js.map +1 -1
- package/build/src/core/attachment_options.d.ts +32 -0
- package/build/src/core/attachment_options.d.ts.map +1 -0
- package/build/src/core/attachment_options.js +24 -0
- package/build/src/core/attachment_options.js.map +1 -0
- package/build/src/core/attachment_service.d.ts +7 -1
- package/build/src/core/attachment_service.d.ts.map +1 -1
- package/build/src/core/attachment_service.js +50 -4
- package/build/src/core/attachment_service.js.map +1 -1
- package/build/src/core/index.d.ts +2 -1
- package/build/src/core/index.d.ts.map +1 -1
- package/build/src/core/index.js +2 -1
- package/build/src/core/index.js.map +1 -1
- package/build/src/define_config.d.ts +17 -0
- package/build/src/define_config.d.ts.map +1 -1
- package/build/src/define_config.js +9 -0
- package/build/src/define_config.js.map +1 -1
- package/build/src/integrations/lucid/column/attachment_column.d.ts +18 -0
- package/build/src/integrations/lucid/column/attachment_column.d.ts.map +1 -0
- package/build/src/integrations/lucid/{attachment_column.js → column/attachment_column.js} +26 -13
- package/build/src/integrations/lucid/column/attachment_column.js.map +1 -0
- package/build/src/integrations/lucid/index.d.ts +16 -12
- package/build/src/integrations/lucid/index.d.ts.map +1 -1
- package/build/src/integrations/lucid/index.js +16 -12
- package/build/src/integrations/lucid/index.js.map +1 -1
- package/build/src/integrations/lucid/migrations/legacy/legacy_attachment_migration_stub.d.ts +20 -0
- package/build/src/integrations/lucid/migrations/legacy/legacy_attachment_migration_stub.d.ts.map +1 -0
- package/build/src/integrations/lucid/migrations/legacy/legacy_attachment_migration_stub.js +18 -0
- package/build/src/integrations/lucid/migrations/legacy/legacy_attachment_migration_stub.js.map +1 -0
- package/build/src/integrations/lucid/{migrate_legacy_attachment.d.ts → migrations/legacy/migrate_legacy_attachment.d.ts} +25 -9
- package/build/src/integrations/lucid/migrations/legacy/migrate_legacy_attachment.d.ts.map +1 -0
- package/build/src/integrations/lucid/migrations/legacy/migrate_legacy_attachment.js +94 -0
- package/build/src/integrations/lucid/migrations/legacy/migrate_legacy_attachment.js.map +1 -0
- package/build/src/integrations/lucid/{migrate_legacy_attachment_records.d.ts → migrations/legacy/migrate_legacy_attachment_records.d.ts} +3 -3
- package/build/src/integrations/lucid/migrations/legacy/migrate_legacy_attachment_records.d.ts.map +1 -0
- package/build/src/integrations/lucid/{migrate_legacy_attachment_records.js → migrations/legacy/migrate_legacy_attachment_records.js} +7 -6
- package/build/src/integrations/lucid/migrations/legacy/migrate_legacy_attachment_records.js.map +1 -0
- package/build/src/integrations/lucid/models/attachment_link_model.d.ts +30 -0
- package/build/src/integrations/lucid/models/attachment_link_model.d.ts.map +1 -0
- package/build/src/integrations/lucid/models/attachment_link_model.js +58 -0
- package/build/src/integrations/lucid/models/attachment_link_model.js.map +1 -0
- package/build/src/integrations/lucid/{attachment_model.d.ts → models/attachment_model.d.ts} +2 -6
- package/build/src/integrations/lucid/models/attachment_model.d.ts.map +1 -0
- package/build/src/integrations/lucid/{attachment_model.js → models/attachment_model.js} +1 -13
- package/build/src/integrations/lucid/models/attachment_model.js.map +1 -0
- package/build/src/integrations/lucid/persistence/lucid_attachment_lifecycle_service.d.ts +34 -0
- package/build/src/integrations/lucid/persistence/lucid_attachment_lifecycle_service.d.ts.map +1 -0
- package/build/src/integrations/lucid/persistence/lucid_attachment_lifecycle_service.js +195 -0
- package/build/src/integrations/lucid/persistence/lucid_attachment_lifecycle_service.js.map +1 -0
- package/build/src/integrations/lucid/{lucid_attachment_repository.d.ts → persistence/lucid_attachment_repository.d.ts} +3 -3
- package/build/src/integrations/lucid/persistence/lucid_attachment_repository.d.ts.map +1 -0
- package/build/src/integrations/lucid/{lucid_attachment_repository.js → persistence/lucid_attachment_repository.js} +1 -1
- package/build/src/integrations/lucid/persistence/lucid_attachment_repository.js.map +1 -0
- package/build/src/integrations/lucid/persistence/lucid_attachment_store.d.ts +48 -0
- package/build/src/integrations/lucid/persistence/lucid_attachment_store.d.ts.map +1 -0
- package/build/src/integrations/lucid/persistence/lucid_attachment_store.js +224 -0
- package/build/src/integrations/lucid/persistence/lucid_attachment_store.js.map +1 -0
- package/build/src/integrations/lucid/{lucid_variant_generation_service.d.ts → persistence/lucid_variant_generation_service.d.ts} +3 -3
- package/build/src/integrations/lucid/persistence/lucid_variant_generation_service.d.ts.map +1 -0
- package/build/src/integrations/lucid/persistence/lucid_variant_generation_service.js.map +1 -0
- package/build/src/integrations/lucid/{attachment_owner.d.ts → relations/attachment_owner.d.ts} +2 -1
- package/build/src/integrations/lucid/relations/attachment_owner.d.ts.map +1 -0
- package/build/src/integrations/lucid/relations/attachment_owner.js.map +1 -0
- package/build/src/integrations/lucid/relations/attachment_relation.d.ts +71 -0
- package/build/src/integrations/lucid/relations/attachment_relation.d.ts.map +1 -0
- package/build/src/integrations/lucid/relations/attachment_relation.js +266 -0
- package/build/src/integrations/lucid/relations/attachment_relation.js.map +1 -0
- package/build/src/integrations/lucid/schema/attachment_schema_service.d.ts +24 -0
- package/build/src/integrations/lucid/schema/attachment_schema_service.d.ts.map +1 -0
- package/build/src/integrations/lucid/schema/attachment_schema_service.js +68 -0
- package/build/src/integrations/lucid/schema/attachment_schema_service.js.map +1 -0
- package/build/src/integrations/lucid/schema/attachment_table_names.d.ts +15 -0
- package/build/src/integrations/lucid/schema/attachment_table_names.d.ts.map +1 -0
- package/build/src/integrations/lucid/schema/attachment_table_names.js +20 -0
- package/build/src/integrations/lucid/schema/attachment_table_names.js.map +1 -0
- package/build/src/integrations/lucid/schema/attachments_table_stub.d.ts.map +1 -0
- package/build/src/integrations/lucid/{attachments_table_stub.js → schema/attachments_table_stub.js} +2 -4
- package/build/src/integrations/lucid/schema/attachments_table_stub.js.map +1 -0
- package/build/src/integrations/lucid/schema/configure_lucid_attachment_tables.d.ts +12 -0
- package/build/src/integrations/lucid/schema/configure_lucid_attachment_tables.d.ts.map +1 -0
- package/build/src/integrations/lucid/schema/configure_lucid_attachment_tables.js +19 -0
- package/build/src/integrations/lucid/schema/configure_lucid_attachment_tables.js.map +1 -0
- package/build/src/sources/attachment_manager.d.ts +14 -12
- package/build/src/sources/attachment_manager.d.ts.map +1 -1
- package/build/src/sources/attachment_manager.js +14 -6
- package/build/src/sources/attachment_manager.js.map +1 -1
- package/build/stubs/migrations/attachments_table.stub +7 -24
- package/build/{src/integrations/lucid/render_legacy_attachment_migration_script.js → stubs/migrations/legacy_attachment_migration.stub} +13 -15
- package/build/tests/attachment_manager.spec.js +21 -18
- package/build/tests/attachment_manager.spec.js.map +1 -1
- package/build/tests/attachment_manager_service.spec.js +2 -2
- package/build/tests/attachment_manager_service.spec.js.map +1 -1
- package/build/tests/attachment_options.spec.d.ts +2 -0
- package/build/tests/attachment_options.spec.d.ts.map +1 -0
- package/build/tests/attachment_options.spec.js +31 -0
- package/build/tests/attachment_options.spec.js.map +1 -0
- package/build/tests/attachment_provider.spec.js +30 -1
- package/build/tests/attachment_provider.spec.js.map +1 -1
- package/build/tests/{create_legacy_attachment_migration_script.spec.d.ts → attachment_schema_service.spec.d.ts} +1 -1
- package/build/tests/attachment_schema_service.spec.d.ts.map +1 -0
- package/build/tests/attachment_schema_service.spec.js +38 -0
- package/build/tests/attachment_schema_service.spec.js.map +1 -0
- package/build/tests/attachment_service.spec.js +55 -0
- package/build/tests/attachment_service.spec.js.map +1 -1
- package/build/tests/attachment_table_names.spec.d.ts +8 -0
- package/build/tests/attachment_table_names.spec.d.ts.map +1 -0
- package/build/tests/attachment_table_names.spec.js +36 -0
- package/build/tests/attachment_table_names.spec.js.map +1 -0
- package/build/tests/attachments_table_stub.spec.js +41 -5
- package/build/tests/attachments_table_stub.spec.js.map +1 -1
- package/build/tests/define_config.spec.js +34 -0
- package/build/tests/define_config.spec.js.map +1 -1
- package/build/tests/helpers/lucid_test_database.d.ts.map +1 -1
- package/build/tests/helpers/lucid_test_database.js +16 -6
- package/build/tests/helpers/lucid_test_database.js.map +1 -1
- package/build/tests/legacy_attachment_migration_stub.spec.d.ts +8 -0
- package/build/tests/legacy_attachment_migration_stub.spec.d.ts.map +1 -0
- package/build/tests/legacy_attachment_migration_stub.spec.js +57 -0
- package/build/tests/legacy_attachment_migration_stub.spec.js.map +1 -0
- package/build/tests/lucid_attachment_collection_store.spec.d.ts +2 -0
- package/build/tests/lucid_attachment_collection_store.spec.d.ts.map +1 -0
- package/build/tests/lucid_attachment_collection_store.spec.js +53 -0
- package/build/tests/lucid_attachment_collection_store.spec.js.map +1 -0
- package/build/tests/lucid_attachment_column.spec.js +57 -1
- package/build/tests/lucid_attachment_column.spec.js.map +1 -1
- package/build/tests/lucid_attachment_lifecycle_service.spec.js +117 -111
- package/build/tests/lucid_attachment_lifecycle_service.spec.js.map +1 -1
- package/build/tests/lucid_attachment_relation.spec.d.ts +8 -0
- package/build/tests/lucid_attachment_relation.spec.d.ts.map +1 -0
- package/build/tests/lucid_attachment_relation.spec.js +295 -0
- package/build/tests/lucid_attachment_relation.spec.js.map +1 -0
- package/build/tests/lucid_attachment_repository.spec.js +1 -1
- package/build/tests/lucid_attachment_repository.spec.js.map +1 -1
- package/build/tests/lucid_attachment_store.spec.js +24 -50
- package/build/tests/lucid_attachment_store.spec.js.map +1 -1
- package/build/tests/lucid_sqlite_integration.spec.js +23 -18
- package/build/tests/lucid_sqlite_integration.spec.js.map +1 -1
- package/build/tests/lucid_variant_generation_service.spec.js +1 -1
- package/build/tests/lucid_variant_generation_service.spec.js.map +1 -1
- package/build/tests/migrate_legacy_attachment.spec.js +114 -67
- package/build/tests/migrate_legacy_attachment.spec.js.map +1 -1
- package/build/tests/migrate_legacy_attachment_records.spec.js +4 -3
- package/build/tests/migrate_legacy_attachment_records.spec.js.map +1 -1
- package/package.json +4 -2
- package/build/src/integrations/lucid/attachment_column.d.ts +0 -11
- package/build/src/integrations/lucid/attachment_column.d.ts.map +0 -1
- package/build/src/integrations/lucid/attachment_column.js.map +0 -1
- package/build/src/integrations/lucid/attachment_model.d.ts.map +0 -1
- package/build/src/integrations/lucid/attachment_model.js.map +0 -1
- package/build/src/integrations/lucid/attachment_owner.d.ts.map +0 -1
- package/build/src/integrations/lucid/attachment_owner.js.map +0 -1
- package/build/src/integrations/lucid/attachments_table_stub.d.ts.map +0 -1
- package/build/src/integrations/lucid/attachments_table_stub.js.map +0 -1
- package/build/src/integrations/lucid/create_legacy_attachment_migration_script.d.ts +0 -13
- package/build/src/integrations/lucid/create_legacy_attachment_migration_script.d.ts.map +0 -1
- package/build/src/integrations/lucid/create_legacy_attachment_migration_script.js +0 -20
- package/build/src/integrations/lucid/create_legacy_attachment_migration_script.js.map +0 -1
- package/build/src/integrations/lucid/lucid_attachment_lifecycle_service.d.ts +0 -21
- package/build/src/integrations/lucid/lucid_attachment_lifecycle_service.d.ts.map +0 -1
- package/build/src/integrations/lucid/lucid_attachment_lifecycle_service.js +0 -68
- package/build/src/integrations/lucid/lucid_attachment_lifecycle_service.js.map +0 -1
- package/build/src/integrations/lucid/lucid_attachment_repository.d.ts.map +0 -1
- package/build/src/integrations/lucid/lucid_attachment_repository.js.map +0 -1
- package/build/src/integrations/lucid/lucid_attachment_store.d.ts +0 -27
- package/build/src/integrations/lucid/lucid_attachment_store.d.ts.map +0 -1
- package/build/src/integrations/lucid/lucid_attachment_store.js +0 -84
- package/build/src/integrations/lucid/lucid_attachment_store.js.map +0 -1
- package/build/src/integrations/lucid/lucid_variant_generation_service.d.ts.map +0 -1
- package/build/src/integrations/lucid/lucid_variant_generation_service.js.map +0 -1
- package/build/src/integrations/lucid/migrate_legacy_attachment.d.ts.map +0 -1
- package/build/src/integrations/lucid/migrate_legacy_attachment.js +0 -67
- package/build/src/integrations/lucid/migrate_legacy_attachment.js.map +0 -1
- package/build/src/integrations/lucid/migrate_legacy_attachment_records.d.ts.map +0 -1
- package/build/src/integrations/lucid/migrate_legacy_attachment_records.js.map +0 -1
- package/build/src/integrations/lucid/render_legacy_attachment_migration_script.d.ts +0 -11
- package/build/src/integrations/lucid/render_legacy_attachment_migration_script.d.ts.map +0 -1
- package/build/src/integrations/lucid/render_legacy_attachment_migration_script.js.map +0 -1
- package/build/tests/create_legacy_attachment_migration_script.spec.d.ts.map +0 -1
- package/build/tests/create_legacy_attachment_migration_script.spec.js +0 -31
- package/build/tests/create_legacy_attachment_migration_script.spec.js.map +0 -1
- /package/build/src/integrations/lucid/{lucid_variant_generation_service.js → persistence/lucid_variant_generation_service.js} +0 -0
- /package/build/src/integrations/lucid/{attachment_owner.js → relations/attachment_owner.js} +0 -0
- /package/build/src/integrations/lucid/{attachments_table_stub.d.ts → schema/attachments_table_stub.d.ts} +0 -0
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @jrmc/adonis-attachment
|
|
3
|
+
*
|
|
4
|
+
* @license MIT
|
|
5
|
+
* @copyright Jeremy Chaufourier <jeremy@chaufourier.fr>
|
|
6
|
+
*/
|
|
7
|
+
import app from "@adonisjs/core/services/app";
|
|
8
|
+
import { AttachmentModel } from "../models/attachment_model.js";
|
|
9
|
+
import { LucidAttachmentLifecycleService } from "../persistence/lucid_attachment_lifecycle_service.js";
|
|
10
|
+
import { LucidAttachmentStore } from "../persistence/lucid_attachment_store.js";
|
|
11
|
+
const relationDefinitions = new WeakMap();
|
|
12
|
+
const relationInstances = new WeakMap();
|
|
13
|
+
const deleteHooks = new WeakSet();
|
|
14
|
+
const saveHooks = new WeakSet();
|
|
15
|
+
/**
|
|
16
|
+
* Declares one attachment persisted in the polymorphic attachments table.
|
|
17
|
+
*/
|
|
18
|
+
export function attachmentRelation(options = {}) {
|
|
19
|
+
return defineRelation("one", options);
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Declares an ordered attachment collection persisted in the polymorphic attachments table.
|
|
23
|
+
*/
|
|
24
|
+
export function attachmentsRelation(options = {}) {
|
|
25
|
+
return defineRelation("many", options);
|
|
26
|
+
}
|
|
27
|
+
export class AttachmentRelation {
|
|
28
|
+
#row;
|
|
29
|
+
#definition;
|
|
30
|
+
#pending;
|
|
31
|
+
constructor(row, definition) {
|
|
32
|
+
this.#row = row;
|
|
33
|
+
this.#definition = definition;
|
|
34
|
+
}
|
|
35
|
+
async get() {
|
|
36
|
+
const lifecycle = await this.#lifecycle();
|
|
37
|
+
return lifecycle.get(this.#owner());
|
|
38
|
+
}
|
|
39
|
+
get hasPending() {
|
|
40
|
+
return this.#pending !== undefined;
|
|
41
|
+
}
|
|
42
|
+
attach(input) {
|
|
43
|
+
if (this.#pending && this.#pending.type !== "detach") {
|
|
44
|
+
throw new Error(`Attachment relation "${this.#definition.field}" already has a pending attachment; use replace() or set()`);
|
|
45
|
+
}
|
|
46
|
+
this.#pending = { type: "attach", input };
|
|
47
|
+
}
|
|
48
|
+
attachExisting(attachmentId) {
|
|
49
|
+
if (this.#pending && this.#pending.type !== "detach") {
|
|
50
|
+
throw new Error(`Attachment relation "${this.#definition.field}" already has a pending attachment; use replace() or set()`);
|
|
51
|
+
}
|
|
52
|
+
this.#pending = { type: "attachExisting", attachmentId };
|
|
53
|
+
}
|
|
54
|
+
set(input) {
|
|
55
|
+
this.replace(input);
|
|
56
|
+
}
|
|
57
|
+
replace(input) {
|
|
58
|
+
this.#pending = { type: "replace", input };
|
|
59
|
+
}
|
|
60
|
+
detach() {
|
|
61
|
+
this.#pending = { type: "detach" };
|
|
62
|
+
}
|
|
63
|
+
async persist() {
|
|
64
|
+
const pending = this.#pending;
|
|
65
|
+
if (!pending) {
|
|
66
|
+
return this.get();
|
|
67
|
+
}
|
|
68
|
+
const owner = this.#owner();
|
|
69
|
+
const lifecycle = await this.#lifecycle();
|
|
70
|
+
let result;
|
|
71
|
+
switch (pending.type) {
|
|
72
|
+
case "attach":
|
|
73
|
+
if (await lifecycle.get(owner)) {
|
|
74
|
+
throw new Error(`Attachment relation "${owner.field}" already has an attachment; use replace() or set()`);
|
|
75
|
+
}
|
|
76
|
+
result = await lifecycle.attach(owner, pending.input, this.#definition.options);
|
|
77
|
+
break;
|
|
78
|
+
case "attachExisting":
|
|
79
|
+
if (await lifecycle.get(owner)) {
|
|
80
|
+
throw new Error(`Attachment relation "${owner.field}" already has an attachment; use replace() or set()`);
|
|
81
|
+
}
|
|
82
|
+
result = await lifecycle.attachExisting(owner, pending.attachmentId);
|
|
83
|
+
break;
|
|
84
|
+
case "replace":
|
|
85
|
+
result = await lifecycle.replace(owner, pending.input, this.#definition.options);
|
|
86
|
+
break;
|
|
87
|
+
case "detach":
|
|
88
|
+
await lifecycle.detach(owner);
|
|
89
|
+
result = null;
|
|
90
|
+
break;
|
|
91
|
+
}
|
|
92
|
+
this.#pending = undefined;
|
|
93
|
+
return result;
|
|
94
|
+
}
|
|
95
|
+
async variants() {
|
|
96
|
+
const lifecycle = await this.#lifecycle();
|
|
97
|
+
return lifecycle.listVariants(this.#owner());
|
|
98
|
+
}
|
|
99
|
+
async regenerateVariants(variantKeys) {
|
|
100
|
+
const attachment = await this.get();
|
|
101
|
+
if (!attachment) {
|
|
102
|
+
return false;
|
|
103
|
+
}
|
|
104
|
+
const service = await resolveAttachmentService();
|
|
105
|
+
await service.scheduleVariantGeneration(attachment.toAttachment(), variantKeys);
|
|
106
|
+
return true;
|
|
107
|
+
}
|
|
108
|
+
async #lifecycle() {
|
|
109
|
+
return new LucidAttachmentLifecycleService(await resolveAttachmentService(), new LucidAttachmentStore(AttachmentModel, this.#row.$trx ? { client: this.#row.$trx } : {}));
|
|
110
|
+
}
|
|
111
|
+
#owner() {
|
|
112
|
+
return createOwner(this.#row, this.#definition);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
export class AttachmentCollectionRelation {
|
|
116
|
+
#row;
|
|
117
|
+
#definition;
|
|
118
|
+
#pending = [];
|
|
119
|
+
constructor(row, definition) {
|
|
120
|
+
this.#row = row;
|
|
121
|
+
this.#definition = definition;
|
|
122
|
+
}
|
|
123
|
+
async all() {
|
|
124
|
+
const lifecycle = await this.#lifecycle();
|
|
125
|
+
return lifecycle.listCollection(this.#owner());
|
|
126
|
+
}
|
|
127
|
+
get hasPending() {
|
|
128
|
+
return this.#pending.length > 0;
|
|
129
|
+
}
|
|
130
|
+
add(input, position) {
|
|
131
|
+
this.#pending.push({ type: "add", input, ...(position !== undefined ? { position } : {}) });
|
|
132
|
+
}
|
|
133
|
+
addExisting(attachmentId, position) {
|
|
134
|
+
this.#pending.push({
|
|
135
|
+
type: "addExisting",
|
|
136
|
+
attachmentId,
|
|
137
|
+
...(position !== undefined ? { position } : {}),
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
remove(id) {
|
|
141
|
+
this.#pending.push({ type: "remove", id });
|
|
142
|
+
}
|
|
143
|
+
clear() {
|
|
144
|
+
this.#pending = [{ type: "clear" }];
|
|
145
|
+
}
|
|
146
|
+
replaceAll(inputs) {
|
|
147
|
+
this.#pending = [{ type: "replaceAll", inputs }];
|
|
148
|
+
}
|
|
149
|
+
move(id, position) {
|
|
150
|
+
this.#pending.push({ type: "move", id, position });
|
|
151
|
+
}
|
|
152
|
+
async persist() {
|
|
153
|
+
if (this.#pending.length === 0) {
|
|
154
|
+
return this.all();
|
|
155
|
+
}
|
|
156
|
+
const lifecycle = await this.#lifecycle();
|
|
157
|
+
const owner = this.#owner();
|
|
158
|
+
while (this.#pending.length > 0) {
|
|
159
|
+
const operation = this.#pending[0];
|
|
160
|
+
switch (operation.type) {
|
|
161
|
+
case "add":
|
|
162
|
+
await lifecycle.add(owner, operation.input, operation.position, this.#definition.options);
|
|
163
|
+
break;
|
|
164
|
+
case "addExisting":
|
|
165
|
+
await lifecycle.addExisting(owner, operation.attachmentId, operation.position);
|
|
166
|
+
break;
|
|
167
|
+
case "remove":
|
|
168
|
+
await lifecycle.removeCollectionItem(owner, operation.id);
|
|
169
|
+
break;
|
|
170
|
+
case "clear":
|
|
171
|
+
await lifecycle.clearCollection(owner);
|
|
172
|
+
break;
|
|
173
|
+
case "replaceAll":
|
|
174
|
+
await lifecycle.replaceCollection(owner, operation.inputs, this.#definition.options);
|
|
175
|
+
break;
|
|
176
|
+
case "move":
|
|
177
|
+
await lifecycle.moveCollectionItem(owner, operation.id, operation.position);
|
|
178
|
+
break;
|
|
179
|
+
}
|
|
180
|
+
this.#pending.shift();
|
|
181
|
+
}
|
|
182
|
+
return lifecycle.listCollection(owner);
|
|
183
|
+
}
|
|
184
|
+
async #lifecycle() {
|
|
185
|
+
return new LucidAttachmentLifecycleService(await resolveAttachmentService(), new LucidAttachmentStore(AttachmentModel, this.#row.$trx ? { client: this.#row.$trx } : {}));
|
|
186
|
+
}
|
|
187
|
+
#owner() {
|
|
188
|
+
return createOwner(this.#row, this.#definition);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
function defineRelation(kind, options) {
|
|
192
|
+
return (target, propertyKey) => {
|
|
193
|
+
const Model = target.constructor;
|
|
194
|
+
const field = String(propertyKey);
|
|
195
|
+
const definitions = relationDefinitions.get(Model) ?? new Map();
|
|
196
|
+
if (definitions.has(field)) {
|
|
197
|
+
throw new Error(`Attachment relation "${field}" is already declared on this model`);
|
|
198
|
+
}
|
|
199
|
+
Model.boot();
|
|
200
|
+
definitions.set(field, { kind, field, options });
|
|
201
|
+
relationDefinitions.set(Model, definitions);
|
|
202
|
+
if (!deleteHooks.has(Model)) {
|
|
203
|
+
deleteHooks.add(Model);
|
|
204
|
+
Model.after("delete", async (row) => {
|
|
205
|
+
for (const definition of relationDefinitions.get(Model)?.values() ?? []) {
|
|
206
|
+
const lifecycle = new LucidAttachmentLifecycleService(await resolveAttachmentService(), new LucidAttachmentStore(AttachmentModel, row.$trx ? { client: row.$trx } : {}));
|
|
207
|
+
await lifecycle.purgeOwner(createOwner(row, definition, true));
|
|
208
|
+
}
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
if (!saveHooks.has(Model)) {
|
|
212
|
+
saveHooks.add(Model);
|
|
213
|
+
wrapSave(Model);
|
|
214
|
+
}
|
|
215
|
+
Object.defineProperty(target, propertyKey, {
|
|
216
|
+
configurable: true,
|
|
217
|
+
enumerable: false,
|
|
218
|
+
get() {
|
|
219
|
+
const instances = relationInstances.get(this) ?? new Map();
|
|
220
|
+
const existing = instances.get(field);
|
|
221
|
+
if (existing) {
|
|
222
|
+
return existing;
|
|
223
|
+
}
|
|
224
|
+
const relation = kind === "one"
|
|
225
|
+
? new AttachmentRelation(this, definitions.get(field))
|
|
226
|
+
: new AttachmentCollectionRelation(this, definitions.get(field));
|
|
227
|
+
instances.set(field, relation);
|
|
228
|
+
relationInstances.set(this, instances);
|
|
229
|
+
return relation;
|
|
230
|
+
},
|
|
231
|
+
});
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
function createOwner(row, definition, allowDeleted = false) {
|
|
235
|
+
if ((!allowDeleted && !row.$isPersisted) ||
|
|
236
|
+
row.$primaryKeyValue === null ||
|
|
237
|
+
row.$primaryKeyValue === undefined) {
|
|
238
|
+
throw new Error("Attachment relations require a persisted Lucid model");
|
|
239
|
+
}
|
|
240
|
+
const type = definition.options.type ?? row.constructor.table;
|
|
241
|
+
if (!type) {
|
|
242
|
+
throw new Error("Attachment relations require a Lucid model table or an explicit relation type");
|
|
243
|
+
}
|
|
244
|
+
return {
|
|
245
|
+
type,
|
|
246
|
+
id: String(row.$primaryKeyValue),
|
|
247
|
+
field: definition.field,
|
|
248
|
+
model: row,
|
|
249
|
+
};
|
|
250
|
+
}
|
|
251
|
+
async function resolveAttachmentService() {
|
|
252
|
+
return (await app.container.make("jrmc.attachment"));
|
|
253
|
+
}
|
|
254
|
+
function wrapSave(Model) {
|
|
255
|
+
const save = Model.prototype.save;
|
|
256
|
+
Model.prototype.save = (async function saveWithAttachmentRelations() {
|
|
257
|
+
const result = await save.call(this);
|
|
258
|
+
for (const relation of relationInstances.get(this)?.values() ?? []) {
|
|
259
|
+
if (relation.hasPending) {
|
|
260
|
+
await relation.persist();
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
return result;
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
//# sourceMappingURL=attachment_relation.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"attachment_relation.js","sourceRoot":"","sources":["../../../../../src/integrations/lucid/relations/attachment_relation.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,GAAG,MAAM,6BAA6B,CAAC;AAY9C,OAAO,EAAE,eAAe,EAAE,MAAM,+BAA+B,CAAC;AAChE,OAAO,EAAE,+BAA+B,EAAE,MAAM,sDAAsD,CAAC;AACvG,OAAO,EAAE,oBAAoB,EAAE,MAAM,0CAA0C,CAAC;AAgDhF,MAAM,mBAAmB,GAAG,IAAI,OAAO,EAGpC,CAAC;AACJ,MAAM,iBAAiB,GAAG,IAAI,OAAO,EAGlC,CAAC;AACJ,MAAM,WAAW,GAAG,IAAI,OAAO,EAAU,CAAC;AAC1C,MAAM,SAAS,GAAG,IAAI,OAAO,EAAU,CAAC;AAExC;;GAEG;AACH,MAAM,UAAU,kBAAkB,CAChC,UAA4C,EAAE;IAE9C,OAAO,cAAc,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;AACxC,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,mBAAmB,CACjC,UAA4C,EAAE;IAE9C,OAAO,cAAc,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AACzC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,SAAS,QAAQ,CAAC,KAA2C;IAC3D,MAAM,IAAI,GAAG,KAAK,CAAC,SAAS,CAAC,IAAI,CAAC;IAElC,KAAK,CAAC,SAAS,CAAC,IAAI,GAAG,CAAC,KAAK,UAAU,2BAA2B;QAChE,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAErC,KAAK,MAAM,QAAQ,IAAI,iBAAiB,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,IAAI,EAAE,EAAE,CAAC;YACnE,IAAI,QAAQ,CAAC,UAAU,EAAE,CAAC;gBACxB,MAAM,QAAQ,CAAC,OAAO,EAAE,CAAC;YAC3B,CAAC;QACH,CAAC;QAED,OAAO,MAAM,CAAC;IAChB,CAAC,CAAgC,CAAC;AACpC,CAAC"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @jrmc/adonis-attachment
|
|
3
|
+
*
|
|
4
|
+
* @license MIT
|
|
5
|
+
* @copyright Jeremy Chaufourier <jeremy@chaufourier.fr>
|
|
6
|
+
*/
|
|
7
|
+
import type { Knex } from 'knex';
|
|
8
|
+
export type AttachmentSchemaServiceOptions = {
|
|
9
|
+
tableName?: string;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Owns the versioned database schema used by the Lucid attachment integration.
|
|
13
|
+
*/
|
|
14
|
+
export declare class AttachmentSchemaService {
|
|
15
|
+
#private;
|
|
16
|
+
constructor(connection: Knex, options?: AttachmentSchemaServiceOptions);
|
|
17
|
+
createTables(): Promise<void>;
|
|
18
|
+
createBlobsTable(): Promise<void>;
|
|
19
|
+
createLinksTable(): Promise<void>;
|
|
20
|
+
dropTables(): Promise<void>;
|
|
21
|
+
dropBlobsTable(): Promise<void>;
|
|
22
|
+
dropLinksTable(): Promise<void>;
|
|
23
|
+
}
|
|
24
|
+
//# sourceMappingURL=attachment_schema_service.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"attachment_schema_service.d.ts","sourceRoot":"","sources":["../../../../../src/integrations/lucid/schema/attachment_schema_service.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,MAAM,CAAA;AAGhC,MAAM,MAAM,8BAA8B,GAAG;IAC3C,SAAS,CAAC,EAAE,MAAM,CAAA;CACnB,CAAA;AAED;;GAEG;AACH,qBAAa,uBAAuB;;gBAKtB,UAAU,EAAE,IAAI,EAAE,OAAO,GAAE,8BAAmC;IAO1E,YAAY,IAAI,OAAO,CAAC,IAAI,CAAC;IAI7B,gBAAgB,IAAI,OAAO,CAAC,IAAI,CAAC;IAqBjC,gBAAgB,IAAI,OAAO,CAAC,IAAI,CAAC;IAiBjC,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC;IAI3B,cAAc,IAAI,OAAO,CAAC,IAAI,CAAC;IAI/B,cAAc,IAAI,OAAO,CAAC,IAAI,CAAC;CAGhC"}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @jrmc/adonis-attachment
|
|
3
|
+
*
|
|
4
|
+
* @license MIT
|
|
5
|
+
* @copyright Jeremy Chaufourier <jeremy@chaufourier.fr>
|
|
6
|
+
*/
|
|
7
|
+
import { resolveAttachmentTableNames } from './attachment_table_names.js';
|
|
8
|
+
/**
|
|
9
|
+
* Owns the versioned database schema used by the Lucid attachment integration.
|
|
10
|
+
*/
|
|
11
|
+
export class AttachmentSchemaService {
|
|
12
|
+
#connection;
|
|
13
|
+
#tableName;
|
|
14
|
+
#linksTableName;
|
|
15
|
+
constructor(connection, options = {}) {
|
|
16
|
+
this.#connection = connection;
|
|
17
|
+
const tables = resolveAttachmentTableNames(options.tableName);
|
|
18
|
+
this.#tableName = tables.tableName;
|
|
19
|
+
this.#linksTableName = tables.linksTableName;
|
|
20
|
+
}
|
|
21
|
+
createTables() {
|
|
22
|
+
return this.createBlobsTable().then(() => this.createLinksTable());
|
|
23
|
+
}
|
|
24
|
+
createBlobsTable() {
|
|
25
|
+
return this.#connection.schema.createTable(this.#tableName, (table) => {
|
|
26
|
+
table.uuid('id').primary();
|
|
27
|
+
table.uuid('parent_id').nullable().references('id').inTable(this.#tableName).onDelete('CASCADE');
|
|
28
|
+
table.string('variant_key').nullable();
|
|
29
|
+
table.string('disk').notNullable();
|
|
30
|
+
table.string('path').notNullable();
|
|
31
|
+
table.string('name').notNullable();
|
|
32
|
+
table.string('original_name').notNullable();
|
|
33
|
+
table.string('mime_type').notNullable();
|
|
34
|
+
table.string('extname').notNullable();
|
|
35
|
+
table.bigInteger('size').unsigned().notNullable();
|
|
36
|
+
table.json('metadata').nullable();
|
|
37
|
+
table.timestamp('created_at').notNullable();
|
|
38
|
+
table.timestamp('updated_at').notNullable();
|
|
39
|
+
table.index(['parent_id']);
|
|
40
|
+
table.unique(['parent_id', 'variant_key']);
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
createLinksTable() {
|
|
44
|
+
return this.#connection.schema.createTable(this.#linksTableName, (table) => {
|
|
45
|
+
table.uuid('id').primary();
|
|
46
|
+
table.string('attachable_type').notNullable();
|
|
47
|
+
table.string('attachable_id').notNullable();
|
|
48
|
+
table.string('field').notNullable();
|
|
49
|
+
table.string('owner_key', 64).nullable().unique();
|
|
50
|
+
table.integer('position').unsigned().nullable();
|
|
51
|
+
table.uuid('attachment_id').notNullable().references('id').inTable(this.#tableName).onDelete('CASCADE');
|
|
52
|
+
table.timestamp('created_at').notNullable();
|
|
53
|
+
table.timestamp('updated_at').notNullable();
|
|
54
|
+
table.index(['attachable_type', 'attachable_id', 'field']);
|
|
55
|
+
table.index(['attachment_id']);
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
dropTables() {
|
|
59
|
+
return this.dropLinksTable().then(() => this.dropBlobsTable());
|
|
60
|
+
}
|
|
61
|
+
dropBlobsTable() {
|
|
62
|
+
return this.#connection.schema.dropTableIfExists(this.#tableName);
|
|
63
|
+
}
|
|
64
|
+
dropLinksTable() {
|
|
65
|
+
return this.#connection.schema.dropTableIfExists(this.#linksTableName);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
//# sourceMappingURL=attachment_schema_service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"attachment_schema_service.js","sourceRoot":"","sources":["../../../../../src/integrations/lucid/schema/attachment_schema_service.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,EAAE,2BAA2B,EAAE,MAAM,6BAA6B,CAAA;AAMzE;;GAEG;AACH,MAAM,OAAO,uBAAuB;IACzB,WAAW,CAAM;IACjB,UAAU,CAAQ;IAClB,eAAe,CAAQ;IAEhC,YAAY,UAAgB,EAAE,UAA0C,EAAE;QACxE,IAAI,CAAC,WAAW,GAAG,UAAU,CAAA;QAC7B,MAAM,MAAM,GAAG,2BAA2B,CAAC,OAAO,CAAC,SAAS,CAAC,CAAA;QAC7D,IAAI,CAAC,UAAU,GAAG,MAAM,CAAC,SAAS,CAAA;QAClC,IAAI,CAAC,eAAe,GAAG,MAAM,CAAC,cAAc,CAAA;IAC9C,CAAC;IAED,YAAY;QACV,OAAO,IAAI,CAAC,gBAAgB,EAAE,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,gBAAgB,EAAE,CAAC,CAAA;IACpE,CAAC;IAED,gBAAgB;QACd,OAAO,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,WAAW,CAAC,IAAI,CAAC,UAAU,EAAE,CAAC,KAAK,EAAE,EAAE;YACpE,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,OAAO,EAAE,CAAA;YAC1B,KAAK,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC,QAAQ,EAAE,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC,QAAQ,CAAC,SAAS,CAAC,CAAA;YAChG,KAAK,CAAC,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Jeremy Chaufourier <jeremy@chaufourier.fr>
|
|
6
|
+
*/
|
|
7
|
+
export type AttachmentTableNames = {
|
|
8
|
+
tableName: string;
|
|
9
|
+
linksTableName: string;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Resolves the blob and polymorphic-link table names from one base table name.
|
|
13
|
+
*/
|
|
14
|
+
export declare function resolveAttachmentTableNames(tableName?: string): AttachmentTableNames;
|
|
15
|
+
//# sourceMappingURL=attachment_table_names.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"attachment_table_names.d.ts","sourceRoot":"","sources":["../../../../../src/integrations/lucid/schema/attachment_table_names.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAIH,MAAM,MAAM,oBAAoB,GAAG;IACjC,SAAS,EAAE,MAAM,CAAA;IACjB,cAAc,EAAE,MAAM,CAAA;CACvB,CAAA;AAED;;GAEG;AACH,wBAAgB,2BAA2B,CAAC,SAAS,SAAgB,GAAG,oBAAoB,CAS3F"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @jrmc/adonis-attachment
|
|
3
|
+
*
|
|
4
|
+
* @license MIT
|
|
5
|
+
* @copyright Jeremy Chaufourier <jeremy@chaufourier.fr>
|
|
6
|
+
*/
|
|
7
|
+
import string from '@adonisjs/core/helpers/string';
|
|
8
|
+
/**
|
|
9
|
+
* Resolves the blob and polymorphic-link table names from one base table name.
|
|
10
|
+
*/
|
|
11
|
+
export function resolveAttachmentTableNames(tableName = 'attachments') {
|
|
12
|
+
if (!/^[a-z][a-z0-9_]*$/.test(tableName)) {
|
|
13
|
+
throw new Error('Lucid attachment table names must be snake_case identifiers');
|
|
14
|
+
}
|
|
15
|
+
return {
|
|
16
|
+
tableName,
|
|
17
|
+
linksTableName: `${string.singular(tableName)}_links`,
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=attachment_table_names.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"attachment_table_names.js","sourceRoot":"","sources":["../../../../../src/integrations/lucid/schema/attachment_table_names.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,MAAM,MAAM,+BAA+B,CAAA;AAOlD;;GAEG;AACH,MAAM,UAAU,2BAA2B,CAAC,SAAS,GAAG,aAAa;IACnE,IAAI,CAAC,mBAAmB,CAAC,IAAI,CAAC,SAAS,CAAC,EAAE,CAAC;QACzC,MAAM,IAAI,KAAK,CAAC,6DAA6D,CAAC,CAAA;IAChF,CAAC;IAED,OAAO;QACL,SAAS;QACT,cAAc,EAAE,GAAG,MAAM,CAAC,QAAQ,CAAC,SAAS,CAAC,QAAQ;KACtD,CAAA;AACH,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"attachments_table_stub.d.ts","sourceRoot":"","sources":["../../../../../src/integrations/lucid/schema/attachments_table_stub.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAKH,MAAM,MAAM,2BAA2B,GAAG;IACxC,SAAS,EAAE,MAAM,CAAA;IACjB,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,SAAS,CAAC,EAAE,MAAM,CAAA;CACnB,CAAA;AAED,MAAM,MAAM,yBAAyB,GAAG;IACtC,WAAW,EAAE,MAAM,CAAA;IACnB,SAAS,EAAE,MAAM,CAAA;IACjB,SAAS,EAAE,MAAM,CAAA;CAClB,CAAA;AAED;;GAEG;AACH,wBAAgB,+BAA+B,CAC7C,OAAO,EAAE,2BAA2B,GACnC,yBAAyB,CAU3B"}
|
package/build/src/integrations/lucid/{attachments_table_stub.js → schema/attachments_table_stub.js}
RENAMED
|
@@ -5,14 +5,12 @@
|
|
|
5
5
|
* @copyright Jeremy Chaufourier <jeremy@chaufourier.fr>
|
|
6
6
|
*/
|
|
7
7
|
import { join } from 'node:path';
|
|
8
|
+
import { resolveAttachmentTableNames } from './attachment_table_names.js';
|
|
8
9
|
/**
|
|
9
10
|
* Builds the state consumed by the attachments-table migration stub.
|
|
10
11
|
*/
|
|
11
12
|
export function createAttachmentsTableStubState(options) {
|
|
12
|
-
const tableName = options.tableName
|
|
13
|
-
if (!/^[a-z][a-z0-9_]*$/.test(tableName)) {
|
|
14
|
-
throw new Error('Lucid attachment table names must be snake_case identifiers');
|
|
15
|
-
}
|
|
13
|
+
const { tableName } = resolveAttachmentTableNames(options.tableName);
|
|
16
14
|
const timestamp = options.timestamp ?? Date.now();
|
|
17
15
|
return {
|
|
18
16
|
destination: join(options.directory, `${timestamp}_create_${tableName}_table.ts`),
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"attachments_table_stub.js","sourceRoot":"","sources":["../../../../../src/integrations/lucid/schema/attachments_table_stub.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAA;AAChC,OAAO,EAAE,2BAA2B,EAAE,MAAM,6BAA6B,CAAA;AAczE;;GAEG;AACH,MAAM,UAAU,+BAA+B,CAC7C,OAAoC;IAEpC,MAAM,EAAE,SAAS,EAAE,GAAG,2BAA2B,CAAC,OAAO,CAAC,SAAS,CAAC,CAAA;IAEpE,MAAM,SAAS,GAAG,OAAO,CAAC,SAAS,IAAI,IAAI,CAAC,GAAG,EAAE,CAAA;IAEjD,OAAO;QACL,WAAW,EAAE,IAAI,CAAC,OAAO,CAAC,SAAS,EAAE,GAAG,SAAS,WAAW,SAAS,WAAW,CAAC;QACjF,SAAS;QACT,SAAS,EAAE,YAAY,CAAC,SAAS,CAAC;KACnC,CAAA;AACH,CAAC;AAED,SAAS,YAAY,CAAC,KAAa;IACjC,OAAO,KAAK,CAAC,OAAO,CAAC,kBAAkB,EAAE,CAAC,CAAC,EAAE,UAAkB,EAAE,SAAiB,EAAE,EAAE,CACpF,SAAS,CAAC,WAAW,EAAE,CACxB,CAAA;AACH,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @jrmc/adonis-attachment
|
|
3
|
+
*
|
|
4
|
+
* @license MIT
|
|
5
|
+
* @copyright Jeremy Chaufourier <jeremy@chaufourier.fr>
|
|
6
|
+
*/
|
|
7
|
+
import { type AttachmentTableNames } from './attachment_table_names.js';
|
|
8
|
+
/**
|
|
9
|
+
* Applies an application's configured attachment table names to the default Lucid models.
|
|
10
|
+
*/
|
|
11
|
+
export declare function configureLucidAttachmentTables(tableName?: string): AttachmentTableNames;
|
|
12
|
+
//# sourceMappingURL=configure_lucid_attachment_tables.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"configure_lucid_attachment_tables.d.ts","sourceRoot":"","sources":["../../../../../src/integrations/lucid/schema/configure_lucid_attachment_tables.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAIH,OAAO,EAA+B,KAAK,oBAAoB,EAAE,MAAM,6BAA6B,CAAA;AAEpG;;GAEG;AACH,wBAAgB,8BAA8B,CAAC,SAAS,CAAC,EAAE,MAAM,GAAG,oBAAoB,CAOvF"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @jrmc/adonis-attachment
|
|
3
|
+
*
|
|
4
|
+
* @license MIT
|
|
5
|
+
* @copyright Jeremy Chaufourier <jeremy@chaufourier.fr>
|
|
6
|
+
*/
|
|
7
|
+
import { AttachmentLinkModel } from '../models/attachment_link_model.js';
|
|
8
|
+
import { AttachmentModel } from '../models/attachment_model.js';
|
|
9
|
+
import { resolveAttachmentTableNames } from './attachment_table_names.js';
|
|
10
|
+
/**
|
|
11
|
+
* Applies an application's configured attachment table names to the default Lucid models.
|
|
12
|
+
*/
|
|
13
|
+
export function configureLucidAttachmentTables(tableName) {
|
|
14
|
+
const tables = resolveAttachmentTableNames(tableName);
|
|
15
|
+
AttachmentModel.table = tables.tableName;
|
|
16
|
+
AttachmentLinkModel.table = tables.linksTableName;
|
|
17
|
+
return tables;
|
|
18
|
+
}
|
|
19
|
+
//# sourceMappingURL=configure_lucid_attachment_tables.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"configure_lucid_attachment_tables.js","sourceRoot":"","sources":["../../../../../src/integrations/lucid/schema/configure_lucid_attachment_tables.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,mBAAmB,EAAE,MAAM,oCAAoC,CAAA;AACxE,OAAO,EAAE,eAAe,EAAE,MAAM,+BAA+B,CAAA;AAC/D,OAAO,EAAE,2BAA2B,EAA6B,MAAM,6BAA6B,CAAA;AAEpG;;GAEG;AACH,MAAM,UAAU,8BAA8B,CAAC,SAAkB;IAC/D,MAAM,MAAM,GAAG,2BAA2B,CAAC,SAAS,CAAC,CAAA;IAErD,eAAe,CAAC,KAAK,GAAG,MAAM,CAAC,SAAS,CAAA;IACxC,mBAAmB,CAAC,KAAK,GAAG,MAAM,CAAC,cAAc,CAAA;IAEjD,OAAO,MAAM,CAAA;AACf,CAAC"}
|
|
@@ -5,7 +5,8 @@
|
|
|
5
5
|
* @copyright Jeremy Chaufourier <jeremy@chaufourier.fr>
|
|
6
6
|
*/
|
|
7
7
|
import type { Readable } from 'node:stream';
|
|
8
|
-
import type {
|
|
8
|
+
import type { AttachmentDraft, CreateAttachmentInput } from '../core/attachment.js';
|
|
9
|
+
import type { AttachmentPersistenceOptions } from '../core/attachment_options.js';
|
|
9
10
|
import type { AttachmentService } from '../core/attachment_service.js';
|
|
10
11
|
export type MultipartAttachmentFile = {
|
|
11
12
|
tmpPath?: string;
|
|
@@ -13,9 +14,10 @@ export type MultipartAttachmentFile = {
|
|
|
13
14
|
type?: string;
|
|
14
15
|
subtype?: string;
|
|
15
16
|
};
|
|
16
|
-
export type AttachmentSourceOptions =
|
|
17
|
+
export type AttachmentSourceOptions = AttachmentPersistenceOptions & {
|
|
17
18
|
originalName?: string;
|
|
18
19
|
mimeType?: string;
|
|
20
|
+
metadata?: CreateAttachmentInput['metadata'];
|
|
19
21
|
maxBytes?: number;
|
|
20
22
|
};
|
|
21
23
|
export type AttachmentSourceResponse = {
|
|
@@ -31,19 +33,19 @@ export type AttachmentManagerOptions = {
|
|
|
31
33
|
fetch?: AttachmentSourceFetch;
|
|
32
34
|
};
|
|
33
35
|
/**
|
|
34
|
-
* Normalizes common input sources
|
|
35
|
-
*
|
|
36
|
+
* Normalizes common input sources into drafts. The file is written only when the
|
|
37
|
+
* caller invokes `draft.persist()` or an integration persists it automatically.
|
|
36
38
|
*/
|
|
37
39
|
export declare class AttachmentManager {
|
|
38
40
|
#private;
|
|
39
|
-
constructor(attachments: Pick<AttachmentService, '
|
|
40
|
-
createFromBuffer(input: Uint8Array, options?: AttachmentSourceOptions): Promise<
|
|
41
|
-
createFromBase64(input: string, options?: AttachmentSourceOptions): Promise<
|
|
42
|
-
createFromPath(input: string, options?: AttachmentSourceOptions): Promise<
|
|
43
|
-
createFromStream(input: Readable, options?: AttachmentSourceOptions): Promise<
|
|
44
|
-
createFromUrl(input: URL | string, options?: AttachmentSourceOptions): Promise<
|
|
45
|
-
createFromFile(input: MultipartAttachmentFile, options?: AttachmentSourceOptions): Promise<
|
|
46
|
-
createFromFiles(inputs: readonly MultipartAttachmentFile[], options?: AttachmentSourceOptions): Promise<
|
|
41
|
+
constructor(attachments: Pick<AttachmentService, 'createDraft'>, options?: AttachmentManagerOptions);
|
|
42
|
+
createFromBuffer(input: Uint8Array, options?: AttachmentSourceOptions): Promise<AttachmentDraft>;
|
|
43
|
+
createFromBase64(input: string, options?: AttachmentSourceOptions): Promise<AttachmentDraft>;
|
|
44
|
+
createFromPath(input: string, options?: AttachmentSourceOptions): Promise<AttachmentDraft>;
|
|
45
|
+
createFromStream(input: Readable, options?: AttachmentSourceOptions): Promise<AttachmentDraft>;
|
|
46
|
+
createFromUrl(input: URL | string, options?: AttachmentSourceOptions): Promise<AttachmentDraft>;
|
|
47
|
+
createFromFile(input: MultipartAttachmentFile, options?: AttachmentSourceOptions): Promise<AttachmentDraft>;
|
|
48
|
+
createFromFiles(inputs: readonly MultipartAttachmentFile[], options?: AttachmentSourceOptions): Promise<AttachmentDraft[]>;
|
|
47
49
|
}
|
|
48
50
|
export declare class AttachmentSourceError extends Error {
|
|
49
51
|
constructor(message: string);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"attachment_manager.d.ts","sourceRoot":"","sources":["../../../src/sources/attachment_manager.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAKH,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAA;AAE3C,OAAO,KAAK,EAAE,
|
|
1
|
+
{"version":3,"file":"attachment_manager.d.ts","sourceRoot":"","sources":["../../../src/sources/attachment_manager.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAKH,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAA;AAE3C,OAAO,KAAK,EAAE,eAAe,EAAE,qBAAqB,EAAE,MAAM,uBAAuB,CAAA;AACnF,OAAO,KAAK,EAAE,4BAA4B,EAAE,MAAM,+BAA+B,CAAA;AACjF,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,+BAA+B,CAAA;AAEtE,MAAM,MAAM,uBAAuB,GAAG;IACpC,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,UAAU,EAAE,MAAM,CAAA;IAClB,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,OAAO,CAAC,EAAE,MAAM,CAAA;CACjB,CAAA;AAED,MAAM,MAAM,uBAAuB,GAAG,4BAA4B,GAAG;IACnE,YAAY,CAAC,EAAE,MAAM,CAAA;IACrB,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,QAAQ,CAAC,EAAE,qBAAqB,CAAC,UAAU,CAAC,CAAA;IAC5C,QAAQ,CAAC,EAAE,MAAM,CAAA;CAClB,CAAA;AAED,MAAM,MAAM,wBAAwB,GAAG;IACrC,EAAE,EAAE,OAAO,CAAA;IACX,MAAM,EAAE,MAAM,CAAA;IACd,UAAU,EAAE,MAAM,CAAA;I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E,MAAM;CAI5B"}
|
|
@@ -7,8 +7,8 @@
|
|
|
7
7
|
import { basename, extname } from 'node:path';
|
|
8
8
|
import { readFile, stat } from 'node:fs/promises';
|
|
9
9
|
/**
|
|
10
|
-
* Normalizes common input sources
|
|
11
|
-
*
|
|
10
|
+
* Normalizes common input sources into drafts. The file is written only when the
|
|
11
|
+
* caller invokes `draft.persist()` or an integration persists it automatically.
|
|
12
12
|
*/
|
|
13
13
|
export class AttachmentManager {
|
|
14
14
|
#attachments;
|
|
@@ -97,14 +97,12 @@ export class AttachmentManager {
|
|
|
97
97
|
}
|
|
98
98
|
async #create(body, input) {
|
|
99
99
|
this.#assertSize(body.byteLength, input.options.maxBytes);
|
|
100
|
-
return this.#attachments.
|
|
100
|
+
return this.#attachments.createDraft({
|
|
101
101
|
body,
|
|
102
102
|
originalName: input.originalName,
|
|
103
103
|
mimeType: input.mimeType,
|
|
104
|
-
...(input.options.disk ? { disk: input.options.disk } : {}),
|
|
105
|
-
...(input.options.folder ? { folder: input.options.folder } : {}),
|
|
106
104
|
...(input.options.metadata ? { metadata: input.options.metadata } : {}),
|
|
107
|
-
});
|
|
105
|
+
}, toPersistenceOptions(input.options));
|
|
108
106
|
}
|
|
109
107
|
#assertSize(size, override) {
|
|
110
108
|
if (override !== undefined && (!Number.isSafeInteger(override) || override < 1)) {
|
|
@@ -120,6 +118,16 @@ export class AttachmentManager {
|
|
|
120
118
|
}
|
|
121
119
|
}
|
|
122
120
|
}
|
|
121
|
+
function toPersistenceOptions(options) {
|
|
122
|
+
return {
|
|
123
|
+
...(options.disk !== undefined ? { disk: options.disk } : {}),
|
|
124
|
+
...(options.folder !== undefined ? { folder: options.folder } : {}),
|
|
125
|
+
...(options.rename !== undefined ? { rename: options.rename } : {}),
|
|
126
|
+
...(options.meta !== undefined ? { meta: options.meta } : {}),
|
|
127
|
+
...(options.preComputeUrl !== undefined ? { preComputeUrl: options.preComputeUrl } : {}),
|
|
128
|
+
...(options.variants !== undefined ? { variants: options.variants } : {}),
|
|
129
|
+
};
|
|
130
|
+
}
|
|
123
131
|
export class AttachmentSourceError extends Error {
|
|
124
132
|
constructor(message) {
|
|
125
133
|
super(message);
|