@adminforth/many2many 1.1.1 → 1.1.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.log +1 -1
- package/dist/index.js +66 -39
- package/index.ts +67 -41
- package/package.json +1 -1
package/build.log
CHANGED
package/dist/index.js
CHANGED
|
@@ -8,12 +8,12 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
10
|
import { AdminForthPlugin, Filters } from "adminforth";
|
|
11
|
-
|
|
12
|
-
let linkedColumnNameInJunctionResource = null;
|
|
13
|
-
let resourceColumnNameInJunctionResource = null;
|
|
14
|
-
export default class extends AdminForthPlugin {
|
|
11
|
+
export default class ManyToManyPlugin extends AdminForthPlugin {
|
|
15
12
|
constructor(options) {
|
|
16
13
|
super(options, import.meta.url);
|
|
14
|
+
this.junctionResource = null;
|
|
15
|
+
this.linkedColumnNameInJunctionResource = null;
|
|
16
|
+
this.resourceColumnNameInJunctionResource = null;
|
|
17
17
|
this.options = options;
|
|
18
18
|
}
|
|
19
19
|
modifyResourceConfig(adminforth, resourceConfig) {
|
|
@@ -35,21 +35,25 @@ export default class extends AdminForthPlugin {
|
|
|
35
35
|
}
|
|
36
36
|
}
|
|
37
37
|
if (wasLinkedResourceFound && wasCurrentResourceFound) {
|
|
38
|
-
junctionResource = resource;
|
|
38
|
+
this.junctionResource = resource;
|
|
39
39
|
break;
|
|
40
40
|
}
|
|
41
41
|
}
|
|
42
|
-
if (!junctionResource) {
|
|
43
|
-
throw new Error(`Junction resource not found for many-to-many relation between ${resourceConfig.resourceId} and ${this.options.linkedResourceId}`
|
|
42
|
+
if (!this.junctionResource) {
|
|
43
|
+
throw new Error(`Junction resource not found for many-to-many relation between ${resourceConfig.resourceId} and ${this.options.linkedResourceId}. ` +
|
|
44
|
+
`Checked resources: ${this.adminforth.config.resources.map(r => r.resourceId).join(", ")}`);
|
|
45
|
+
}
|
|
46
|
+
this.linkedColumnNameInJunctionResource = ((_c = this.junctionResource.columns.find(c => { var _a; return ((_a = c.foreignResource) === null || _a === void 0 ? void 0 : _a.resourceId) === this.options.linkedResourceId; })) === null || _c === void 0 ? void 0 : _c.name) || null;
|
|
47
|
+
this.resourceColumnNameInJunctionResource = ((_d = this.junctionResource.columns.find(c => { var _a; return ((_a = c.foreignResource) === null || _a === void 0 ? void 0 : _a.resourceId) === resourceConfig.resourceId; })) === null || _d === void 0 ? void 0 : _d.name) || null;
|
|
48
|
+
if (!this.linkedColumnNameInJunctionResource || !this.resourceColumnNameInJunctionResource) {
|
|
49
|
+
throw new Error(`Junction resource is missing foreign key columns for relation ${resourceConfig.resourceId} <-> ${this.options.linkedResourceId}`);
|
|
44
50
|
}
|
|
45
|
-
linkedColumnNameInJunctionResource = (_c = junctionResource.columns.find(c => { var _a; return ((_a = c.foreignResource) === null || _a === void 0 ? void 0 : _a.resourceId) === this.options.linkedResourceId; })) === null || _c === void 0 ? void 0 : _c.name;
|
|
46
|
-
resourceColumnNameInJunctionResource = (_d = junctionResource.columns.find(c => { var _a; return ((_a = c.foreignResource) === null || _a === void 0 ? void 0 : _a.resourceId) === resourceConfig.resourceId; })) === null || _d === void 0 ? void 0 : _d.name;
|
|
47
51
|
const pluginFrontendOptions = {
|
|
48
52
|
pluginInstanceId: this.pluginInstanceId,
|
|
49
53
|
resourceId: resourceConfig.resourceId,
|
|
50
54
|
linkedResourceId: this.options.linkedResourceId,
|
|
51
|
-
junctionResourceId: junctionResource
|
|
52
|
-
linkedColumnName: linkedColumnNameInJunctionResource,
|
|
55
|
+
junctionResourceId: this.junctionResource.resourceId,
|
|
56
|
+
linkedColumnName: this.linkedColumnNameInJunctionResource,
|
|
53
57
|
resourcePrimaryKeyColumnName: (_e = resourceConfig.columns.find(c => c.primaryKey)) === null || _e === void 0 ? void 0 : _e.name,
|
|
54
58
|
};
|
|
55
59
|
const virtualColumn = {
|
|
@@ -89,10 +93,10 @@ export default class extends AdminForthPlugin {
|
|
|
89
93
|
if (recordWithVirtualColumns[`many2many_${this.pluginInstanceId}`]) {
|
|
90
94
|
for (const linkedId of recordWithVirtualColumns[`many2many_${this.pluginInstanceId}`]) {
|
|
91
95
|
yield this.adminforth.createResourceRecord({
|
|
92
|
-
resource: junctionResource,
|
|
96
|
+
resource: this.junctionResource,
|
|
93
97
|
record: {
|
|
94
|
-
[resourceColumnNameInJunctionResource]: recordId,
|
|
95
|
-
[linkedColumnNameInJunctionResource]: linkedId,
|
|
98
|
+
[this.resourceColumnNameInJunctionResource]: recordId,
|
|
99
|
+
[this.linkedColumnNameInJunctionResource]: linkedId,
|
|
96
100
|
},
|
|
97
101
|
adminUser
|
|
98
102
|
});
|
|
@@ -102,28 +106,30 @@ export default class extends AdminForthPlugin {
|
|
|
102
106
|
}));
|
|
103
107
|
// ** HOOKS FOR EDIT **//
|
|
104
108
|
resourceConfig.hooks.edit.beforeSave.push((_a) => __awaiter(this, [_a], void 0, function* ({ recordId, updates, adminUser }) {
|
|
109
|
+
var _b;
|
|
105
110
|
if (updates[`many2many_${this.pluginInstanceId}`]) {
|
|
106
|
-
const existingJunctionRecords = yield this.adminforth.resource(junctionResource.resourceId).list([Filters.EQ(resourceColumnNameInJunctionResource, recordId)]);
|
|
111
|
+
const existingJunctionRecords = yield this.adminforth.resource(this.junctionResource.resourceId).list([Filters.EQ(this.resourceColumnNameInJunctionResource, recordId)]);
|
|
112
|
+
const junctionPkName = (_b = this.junctionResource.columns.find(c => c.primaryKey)) === null || _b === void 0 ? void 0 : _b.name;
|
|
107
113
|
const updatedLinkedIds = updates[`many2many_${this.pluginInstanceId}`];
|
|
108
114
|
for (const jr of existingJunctionRecords) {
|
|
109
|
-
const linkedId = jr[linkedColumnNameInJunctionResource];
|
|
115
|
+
const linkedId = jr[this.linkedColumnNameInJunctionResource];
|
|
110
116
|
if (!updatedLinkedIds.includes(linkedId)) {
|
|
111
117
|
yield this.adminforth.deleteResourceRecord({
|
|
112
|
-
resource: junctionResource,
|
|
113
|
-
recordId: jr[
|
|
118
|
+
resource: this.junctionResource,
|
|
119
|
+
recordId: jr[junctionPkName],
|
|
114
120
|
record: jr,
|
|
115
121
|
adminUser
|
|
116
122
|
});
|
|
117
123
|
}
|
|
118
124
|
}
|
|
119
125
|
for (const linkedId of updatedLinkedIds) {
|
|
120
|
-
const alreadyExists = existingJunctionRecords.find(jr => jr[linkedColumnNameInJunctionResource] === linkedId);
|
|
126
|
+
const alreadyExists = existingJunctionRecords.find(jr => jr[this.linkedColumnNameInJunctionResource] === linkedId);
|
|
121
127
|
if (!alreadyExists) {
|
|
122
128
|
yield this.adminforth.createResourceRecord({
|
|
123
|
-
resource: junctionResource,
|
|
129
|
+
resource: this.junctionResource,
|
|
124
130
|
record: {
|
|
125
|
-
[resourceColumnNameInJunctionResource]: recordId,
|
|
126
|
-
[linkedColumnNameInJunctionResource]: linkedId,
|
|
131
|
+
[this.resourceColumnNameInJunctionResource]: recordId,
|
|
132
|
+
[this.linkedColumnNameInJunctionResource]: linkedId,
|
|
127
133
|
},
|
|
128
134
|
adminUser
|
|
129
135
|
});
|
|
@@ -135,11 +141,16 @@ export default class extends AdminForthPlugin {
|
|
|
135
141
|
// ** HOOKS FOR DELETE **//
|
|
136
142
|
if (!this.options.dontDeleteJunctionRecords) {
|
|
137
143
|
resourceConfig.hooks.delete.beforeSave.push((_a) => __awaiter(this, [_a], void 0, function* ({ recordId, record, adminUser }) {
|
|
138
|
-
|
|
144
|
+
var _b;
|
|
145
|
+
if (recordId === undefined || recordId === null || recordId === '') {
|
|
146
|
+
return { ok: true };
|
|
147
|
+
}
|
|
148
|
+
const existingJunctionRecords = yield this.adminforth.resource(this.junctionResource.resourceId).list([Filters.EQ(this.resourceColumnNameInJunctionResource, recordId)]);
|
|
149
|
+
const junctionPkName = (_b = this.junctionResource.columns.find(c => c.primaryKey)) === null || _b === void 0 ? void 0 : _b.name;
|
|
139
150
|
for (const jr of existingJunctionRecords) {
|
|
140
151
|
yield this.adminforth.deleteResourceRecord({
|
|
141
|
-
resource: junctionResource,
|
|
142
|
-
recordId: jr[
|
|
152
|
+
resource: this.junctionResource,
|
|
153
|
+
recordId: jr[junctionPkName],
|
|
143
154
|
record: jr,
|
|
144
155
|
adminUser
|
|
145
156
|
});
|
|
@@ -147,12 +158,20 @@ export default class extends AdminForthPlugin {
|
|
|
147
158
|
return { ok: true };
|
|
148
159
|
}));
|
|
149
160
|
const linkedResource = this.adminforth.config.resources.find(r => r.resourceId === this.options.linkedResourceId);
|
|
161
|
+
if (!linkedResource) {
|
|
162
|
+
throw new Error(`Linked resource not found: ${this.options.linkedResourceId}`);
|
|
163
|
+
}
|
|
150
164
|
linkedResource.hooks.delete.beforeSave.push((_a) => __awaiter(this, [_a], void 0, function* ({ recordId, record, adminUser }) {
|
|
151
|
-
|
|
165
|
+
var _b;
|
|
166
|
+
if (recordId === undefined || recordId === null || recordId === '') {
|
|
167
|
+
return { ok: true };
|
|
168
|
+
}
|
|
169
|
+
const existingJunctionRecords = yield this.adminforth.resource(this.junctionResource.resourceId).list([Filters.EQ(this.linkedColumnNameInJunctionResource, recordId)]);
|
|
170
|
+
const junctionPkName = (_b = this.junctionResource.columns.find(c => c.primaryKey)) === null || _b === void 0 ? void 0 : _b.name;
|
|
152
171
|
for (const jr of existingJunctionRecords) {
|
|
153
172
|
yield this.adminforth.deleteResourceRecord({
|
|
154
|
-
resource: junctionResource,
|
|
155
|
-
recordId: jr[
|
|
173
|
+
resource: this.junctionResource,
|
|
174
|
+
recordId: jr[junctionPkName],
|
|
156
175
|
record: jr,
|
|
157
176
|
adminUser
|
|
158
177
|
});
|
|
@@ -179,22 +198,30 @@ export default class extends AdminForthPlugin {
|
|
|
179
198
|
if (recordId === undefined || recordId === null || recordId === '') {
|
|
180
199
|
return { ok: true, data: [] };
|
|
181
200
|
}
|
|
182
|
-
const junctionRecords = yield this.adminforth.resource(junctionResource.resourceId).list([Filters.EQ(resourceColumnNameInJunctionResource, recordId)]);
|
|
183
|
-
|
|
184
|
-
const
|
|
185
|
-
|
|
201
|
+
const junctionRecords = yield this.adminforth.resource(this.junctionResource.resourceId).list([Filters.EQ(this.resourceColumnNameInJunctionResource, recordId)]);
|
|
202
|
+
const dataToReturn = [];
|
|
203
|
+
const linkedResourceConfig = this.adminforth.config.resources.find(r => r.resourceId === this.options.linkedResourceId);
|
|
204
|
+
if (!linkedResourceConfig) {
|
|
205
|
+
throw new Error(`Linked resource not found: ${this.options.linkedResourceId}`);
|
|
206
|
+
}
|
|
207
|
+
const linkedPkColumn = linkedResourceConfig.columns.find(c => c.primaryKey);
|
|
208
|
+
if (!linkedPkColumn) {
|
|
209
|
+
throw new Error(`Linked resource ${this.options.linkedResourceId} has no primary key`);
|
|
210
|
+
}
|
|
211
|
+
const linkedOperationalResource = this.adminforth.resource(this.options.linkedResourceId);
|
|
186
212
|
for (const jr of junctionRecords) {
|
|
187
|
-
const
|
|
213
|
+
const linkedId = jr[this.linkedColumnNameInJunctionResource];
|
|
188
214
|
if (!returnLabels) {
|
|
189
|
-
dataToReturn.push(
|
|
215
|
+
dataToReturn.push(linkedId);
|
|
190
216
|
}
|
|
191
217
|
else {
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
218
|
+
const linkedRecord = yield linkedOperationalResource.get([Filters.EQ(linkedPkColumn.name, linkedId)]);
|
|
219
|
+
dataToReturn.push(linkedOperationalResource.resourceConfig.recordLabel ? {
|
|
220
|
+
label: linkedOperationalResource.resourceConfig.recordLabel(linkedRecord),
|
|
221
|
+
value: linkedRecord[linkedPkColumn.name]
|
|
195
222
|
} : {
|
|
196
|
-
label:
|
|
197
|
-
value:
|
|
223
|
+
label: linkedRecord[linkedPkColumn.name],
|
|
224
|
+
value: linkedRecord[linkedPkColumn.name]
|
|
198
225
|
});
|
|
199
226
|
}
|
|
200
227
|
}
|
package/index.ts
CHANGED
|
@@ -2,11 +2,11 @@ import { AdminForthPlugin, Filters } from "adminforth";
|
|
|
2
2
|
import type { IAdminForth, IHttpServer, AdminForthResourcePages, AdminForthResourceColumn, AdminForthDataTypes, AdminForthResource } from "adminforth";
|
|
3
3
|
import type { PluginOptions } from './types.js';
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
let linkedColumnNameInJunctionResource = null;
|
|
7
|
-
let resourceColumnNameInJunctionResource = null;
|
|
8
|
-
export default class extends AdminForthPlugin {
|
|
5
|
+
export default class ManyToManyPlugin extends AdminForthPlugin {
|
|
9
6
|
options: PluginOptions;
|
|
7
|
+
private junctionResource: AdminForthResource | null = null;
|
|
8
|
+
private linkedColumnNameInJunctionResource: string | null = null;
|
|
9
|
+
private resourceColumnNameInJunctionResource: string | null = null;
|
|
10
10
|
|
|
11
11
|
constructor(options: PluginOptions) {
|
|
12
12
|
super(options, import.meta.url);
|
|
@@ -27,23 +27,29 @@ export default class extends AdminForthPlugin {
|
|
|
27
27
|
}
|
|
28
28
|
}
|
|
29
29
|
if ( wasLinkedResourceFound && wasCurrentResourceFound ) {
|
|
30
|
-
junctionResource = resource;
|
|
30
|
+
this.junctionResource = resource;
|
|
31
31
|
break;
|
|
32
32
|
}
|
|
33
33
|
}
|
|
34
|
-
if (!junctionResource) {
|
|
35
|
-
throw new Error(
|
|
34
|
+
if (!this.junctionResource) {
|
|
35
|
+
throw new Error(
|
|
36
|
+
`Junction resource not found for many-to-many relation between ${resourceConfig.resourceId} and ${this.options.linkedResourceId}. ` +
|
|
37
|
+
`Checked resources: ${this.adminforth.config.resources.map(r => r.resourceId).join(", ")}`
|
|
38
|
+
);
|
|
36
39
|
}
|
|
40
|
+
this.linkedColumnNameInJunctionResource = this.junctionResource.columns.find(c => c.foreignResource?.resourceId === this.options.linkedResourceId)?.name || null;
|
|
41
|
+
this.resourceColumnNameInJunctionResource = this.junctionResource.columns.find(c => c.foreignResource?.resourceId === resourceConfig.resourceId)?.name || null;
|
|
37
42
|
|
|
38
|
-
|
|
39
|
-
|
|
43
|
+
if (!this.linkedColumnNameInJunctionResource || !this.resourceColumnNameInJunctionResource) {
|
|
44
|
+
throw new Error(`Junction resource is missing foreign key columns for relation ${resourceConfig.resourceId} <-> ${this.options.linkedResourceId}`);
|
|
45
|
+
}
|
|
40
46
|
|
|
41
47
|
const pluginFrontendOptions = {
|
|
42
48
|
pluginInstanceId: this.pluginInstanceId,
|
|
43
49
|
resourceId: resourceConfig.resourceId,
|
|
44
50
|
linkedResourceId: this.options.linkedResourceId,
|
|
45
|
-
junctionResourceId: junctionResource
|
|
46
|
-
linkedColumnName: linkedColumnNameInJunctionResource,
|
|
51
|
+
junctionResourceId: this.junctionResource.resourceId,
|
|
52
|
+
linkedColumnName: this.linkedColumnNameInJunctionResource,
|
|
47
53
|
resourcePrimaryKeyColumnName: resourceConfig.columns.find(c => c.primaryKey)?.name,
|
|
48
54
|
}
|
|
49
55
|
|
|
@@ -86,10 +92,10 @@ export default class extends AdminForthPlugin {
|
|
|
86
92
|
if ( recordWithVirtualColumns[`many2many_${this.pluginInstanceId}`] ) {
|
|
87
93
|
for(const linkedId of recordWithVirtualColumns[`many2many_${this.pluginInstanceId}`]) {
|
|
88
94
|
await this.adminforth.createResourceRecord({
|
|
89
|
-
resource: junctionResource,
|
|
95
|
+
resource: this.junctionResource,
|
|
90
96
|
record: {
|
|
91
|
-
[resourceColumnNameInJunctionResource]: recordId,
|
|
92
|
-
[linkedColumnNameInJunctionResource]: linkedId,
|
|
97
|
+
[this.resourceColumnNameInJunctionResource]: recordId,
|
|
98
|
+
[this.linkedColumnNameInJunctionResource]: linkedId,
|
|
93
99
|
},
|
|
94
100
|
adminUser
|
|
95
101
|
});
|
|
@@ -102,27 +108,28 @@ export default class extends AdminForthPlugin {
|
|
|
102
108
|
|
|
103
109
|
resourceConfig.hooks.edit.beforeSave.push(async ({recordId, updates, adminUser }: { recordId: any, updates: any, adminUser: any }) => {
|
|
104
110
|
if ( updates[`many2many_${this.pluginInstanceId}`] ) {
|
|
105
|
-
const existingJunctionRecords = await this.adminforth.resource(junctionResource.resourceId).list([Filters.EQ(resourceColumnNameInJunctionResource, recordId)]);
|
|
111
|
+
const existingJunctionRecords = await this.adminforth.resource(this.junctionResource.resourceId).list([Filters.EQ(this.resourceColumnNameInJunctionResource, recordId)]);
|
|
112
|
+
const junctionPkName = this.junctionResource.columns.find(c => c.primaryKey)?.name;
|
|
106
113
|
const updatedLinkedIds = updates[`many2many_${this.pluginInstanceId}`];
|
|
107
114
|
for(const jr of existingJunctionRecords) {
|
|
108
|
-
const linkedId = jr[linkedColumnNameInJunctionResource];
|
|
115
|
+
const linkedId = jr[this.linkedColumnNameInJunctionResource];
|
|
109
116
|
if ( !updatedLinkedIds.includes(linkedId) ) {
|
|
110
117
|
await this.adminforth.deleteResourceRecord({
|
|
111
|
-
resource: junctionResource,
|
|
112
|
-
recordId: jr[
|
|
118
|
+
resource: this.junctionResource,
|
|
119
|
+
recordId: jr[junctionPkName],
|
|
113
120
|
record: jr,
|
|
114
121
|
adminUser
|
|
115
122
|
});
|
|
116
123
|
}
|
|
117
124
|
}
|
|
118
125
|
for(const linkedId of updatedLinkedIds) {
|
|
119
|
-
const alreadyExists = existingJunctionRecords.find(jr => jr[linkedColumnNameInJunctionResource] === linkedId);
|
|
126
|
+
const alreadyExists = existingJunctionRecords.find(jr => jr[this.linkedColumnNameInJunctionResource] === linkedId);
|
|
120
127
|
if ( !alreadyExists ) {
|
|
121
128
|
await this.adminforth.createResourceRecord({
|
|
122
|
-
resource: junctionResource,
|
|
129
|
+
resource: this.junctionResource,
|
|
123
130
|
record: {
|
|
124
|
-
[resourceColumnNameInJunctionResource]: recordId,
|
|
125
|
-
[linkedColumnNameInJunctionResource]: linkedId,
|
|
131
|
+
[this.resourceColumnNameInJunctionResource]: recordId,
|
|
132
|
+
[this.linkedColumnNameInJunctionResource]: linkedId,
|
|
126
133
|
},
|
|
127
134
|
adminUser
|
|
128
135
|
});
|
|
@@ -135,11 +142,15 @@ export default class extends AdminForthPlugin {
|
|
|
135
142
|
// ** HOOKS FOR DELETE **//
|
|
136
143
|
if (!this.options.dontDeleteJunctionRecords) {
|
|
137
144
|
resourceConfig.hooks.delete.beforeSave.push(async ({ recordId, record, adminUser }: { recordId: any, record: any, adminUser: any }) => {
|
|
138
|
-
|
|
145
|
+
if (recordId === undefined || recordId === null || recordId === '') {
|
|
146
|
+
return { ok: true };
|
|
147
|
+
}
|
|
148
|
+
const existingJunctionRecords = await this.adminforth.resource(this.junctionResource.resourceId).list([Filters.EQ(this.resourceColumnNameInJunctionResource, recordId)]);
|
|
149
|
+
const junctionPkName = this.junctionResource.columns.find(c => c.primaryKey)?.name;
|
|
139
150
|
for(const jr of existingJunctionRecords) {
|
|
140
151
|
await this.adminforth.deleteResourceRecord({
|
|
141
|
-
resource: junctionResource,
|
|
142
|
-
recordId: jr[
|
|
152
|
+
resource: this.junctionResource,
|
|
153
|
+
recordId: jr[junctionPkName],
|
|
143
154
|
record: jr,
|
|
144
155
|
adminUser
|
|
145
156
|
});
|
|
@@ -147,12 +158,19 @@ export default class extends AdminForthPlugin {
|
|
|
147
158
|
return { ok: true };
|
|
148
159
|
});
|
|
149
160
|
const linkedResource = this.adminforth.config.resources.find(r => r.resourceId === this.options.linkedResourceId);
|
|
161
|
+
if (!linkedResource) {
|
|
162
|
+
throw new Error(`Linked resource not found: ${this.options.linkedResourceId}`);
|
|
163
|
+
}
|
|
150
164
|
linkedResource.hooks.delete.beforeSave.push(async ({ recordId, record, adminUser }: { recordId: any, record: any, adminUser: any }) => {
|
|
151
|
-
|
|
165
|
+
if (recordId === undefined || recordId === null || recordId === '') {
|
|
166
|
+
return { ok: true };
|
|
167
|
+
}
|
|
168
|
+
const existingJunctionRecords = await this.adminforth.resource(this.junctionResource.resourceId).list([Filters.EQ(this.linkedColumnNameInJunctionResource, recordId)]);
|
|
169
|
+
const junctionPkName = this.junctionResource.columns.find(c => c.primaryKey)?.name;
|
|
152
170
|
for(const jr of existingJunctionRecords) {
|
|
153
171
|
await this.adminforth.deleteResourceRecord({
|
|
154
|
-
resource: junctionResource,
|
|
155
|
-
recordId: jr[
|
|
172
|
+
resource: this.junctionResource,
|
|
173
|
+
recordId: jr[junctionPkName],
|
|
156
174
|
record: jr,
|
|
157
175
|
adminUser
|
|
158
176
|
});
|
|
@@ -181,22 +199,30 @@ export default class extends AdminForthPlugin {
|
|
|
181
199
|
if (recordId === undefined || recordId === null || recordId === '') {
|
|
182
200
|
return { ok: true, data: [] };
|
|
183
201
|
}
|
|
184
|
-
const junctionRecords = await this.adminforth.resource(junctionResource.resourceId).list([Filters.EQ(resourceColumnNameInJunctionResource, recordId)]);
|
|
185
|
-
|
|
186
|
-
const
|
|
187
|
-
|
|
202
|
+
const junctionRecords = await this.adminforth.resource(this.junctionResource.resourceId).list([Filters.EQ(this.resourceColumnNameInJunctionResource, recordId)]);
|
|
203
|
+
const dataToReturn = [];
|
|
204
|
+
const linkedResourceConfig = this.adminforth.config.resources.find(r => r.resourceId === this.options.linkedResourceId);
|
|
205
|
+
if (!linkedResourceConfig) {
|
|
206
|
+
throw new Error(`Linked resource not found: ${this.options.linkedResourceId}`);
|
|
207
|
+
}
|
|
208
|
+
const linkedPkColumn = linkedResourceConfig.columns.find(c => c.primaryKey);
|
|
209
|
+
if (!linkedPkColumn) {
|
|
210
|
+
throw new Error(`Linked resource ${this.options.linkedResourceId} has no primary key`);
|
|
211
|
+
}
|
|
212
|
+
const linkedOperationalResource = this.adminforth.resource(this.options.linkedResourceId);
|
|
188
213
|
for(const jr of junctionRecords) {
|
|
189
|
-
const
|
|
190
|
-
if (
|
|
191
|
-
dataToReturn.push
|
|
214
|
+
const linkedId = jr[this.linkedColumnNameInJunctionResource];
|
|
215
|
+
if (!returnLabels) {
|
|
216
|
+
dataToReturn.push(linkedId);
|
|
192
217
|
} else {
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
218
|
+
const linkedRecord = await linkedOperationalResource.get([Filters.EQ(linkedPkColumn.name, linkedId)]);
|
|
219
|
+
dataToReturn.push(
|
|
220
|
+
(linkedOperationalResource as any).resourceConfig.recordLabel ? {
|
|
221
|
+
label: (linkedOperationalResource as any).resourceConfig.recordLabel(linkedRecord),
|
|
222
|
+
value: linkedRecord[linkedPkColumn.name]
|
|
197
223
|
} : {
|
|
198
|
-
label:
|
|
199
|
-
value:
|
|
224
|
+
label: linkedRecord[linkedPkColumn.name],
|
|
225
|
+
value: linkedRecord[linkedPkColumn.name]
|
|
200
226
|
}
|
|
201
227
|
);
|
|
202
228
|
}
|