@adminforth/many2many 1.0.7 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build.log +1 -1
- package/dist/index.js +36 -12
- package/index.ts +36 -12
- package/package.json +1 -1
- package/types.ts +2 -0
package/build.log
CHANGED
package/dist/index.js
CHANGED
|
@@ -80,38 +80,52 @@ export default class extends AdminForthPlugin {
|
|
|
80
80
|
create: false,
|
|
81
81
|
edit: false
|
|
82
82
|
},
|
|
83
|
-
label: this.options.labelForColumn || this.options.linkedResourceId
|
|
83
|
+
label: this.options.labelForColumn || this.options.linkedResourceId,
|
|
84
|
+
showIf: this.options.showIf || null,
|
|
84
85
|
};
|
|
85
86
|
resourceConfig.columns.push(virtualColumn);
|
|
86
87
|
// ** HOOKS FOR CREATE **//
|
|
87
|
-
resourceConfig.hooks.create.afterSave.push((_a) => __awaiter(this, [_a], void 0, function* ({ recordId, recordWithVirtualColumns }) {
|
|
88
|
+
resourceConfig.hooks.create.afterSave.push((_a) => __awaiter(this, [_a], void 0, function* ({ recordId, recordWithVirtualColumns, adminUser }) {
|
|
88
89
|
if (recordWithVirtualColumns[`many2many_${this.pluginInstanceId}`]) {
|
|
89
90
|
for (const linkedId of recordWithVirtualColumns[`many2many_${this.pluginInstanceId}`]) {
|
|
90
|
-
yield this.adminforth.
|
|
91
|
-
|
|
92
|
-
|
|
91
|
+
yield this.adminforth.createResourceRecord({
|
|
92
|
+
resource: junctionResource,
|
|
93
|
+
record: {
|
|
94
|
+
[resourceColumnNameInJunctionResource]: recordId,
|
|
95
|
+
[linkedColumnNameInJunctionResource]: linkedId,
|
|
96
|
+
},
|
|
97
|
+
adminUser
|
|
93
98
|
});
|
|
94
99
|
}
|
|
95
100
|
}
|
|
96
101
|
return { ok: true };
|
|
97
102
|
}));
|
|
98
103
|
// ** HOOKS FOR EDIT **//
|
|
99
|
-
resourceConfig.hooks.edit.beforeSave.push((_a) => __awaiter(this, [_a], void 0, function* ({ recordId, updates }) {
|
|
104
|
+
resourceConfig.hooks.edit.beforeSave.push((_a) => __awaiter(this, [_a], void 0, function* ({ recordId, updates, adminUser }) {
|
|
100
105
|
if (updates[`many2many_${this.pluginInstanceId}`]) {
|
|
101
106
|
const existingJunctionRecords = yield this.adminforth.resource(junctionResource.resourceId).list([Filters.EQ(resourceColumnNameInJunctionResource, recordId)]);
|
|
102
107
|
const updatedLinkedIds = updates[`many2many_${this.pluginInstanceId}`];
|
|
103
108
|
for (const jr of existingJunctionRecords) {
|
|
104
109
|
const linkedId = jr[linkedColumnNameInJunctionResource];
|
|
105
110
|
if (!updatedLinkedIds.includes(linkedId)) {
|
|
106
|
-
yield this.adminforth.
|
|
111
|
+
yield this.adminforth.deleteResourceRecord({
|
|
112
|
+
resource: junctionResource,
|
|
113
|
+
recordId: jr[junctionResource.columns.find(c => c.primaryKey).name],
|
|
114
|
+
record: jr,
|
|
115
|
+
adminUser
|
|
116
|
+
});
|
|
107
117
|
}
|
|
108
118
|
}
|
|
109
119
|
for (const linkedId of updatedLinkedIds) {
|
|
110
120
|
const alreadyExists = existingJunctionRecords.find(jr => jr[linkedColumnNameInJunctionResource] === linkedId);
|
|
111
121
|
if (!alreadyExists) {
|
|
112
|
-
yield this.adminforth.
|
|
113
|
-
|
|
114
|
-
|
|
122
|
+
yield this.adminforth.createResourceRecord({
|
|
123
|
+
resource: junctionResource,
|
|
124
|
+
record: {
|
|
125
|
+
[resourceColumnNameInJunctionResource]: recordId,
|
|
126
|
+
[linkedColumnNameInJunctionResource]: linkedId,
|
|
127
|
+
},
|
|
128
|
+
adminUser
|
|
115
129
|
});
|
|
116
130
|
}
|
|
117
131
|
}
|
|
@@ -123,7 +137,12 @@ export default class extends AdminForthPlugin {
|
|
|
123
137
|
resourceConfig.hooks.delete.beforeSave.push((_a) => __awaiter(this, [_a], void 0, function* ({ recordId, record }) {
|
|
124
138
|
const existingJunctionRecords = yield this.adminforth.resource(junctionResource.resourceId).list([Filters.EQ(resourceColumnNameInJunctionResource, recordId)]);
|
|
125
139
|
for (const jr of existingJunctionRecords) {
|
|
126
|
-
yield this.adminforth.
|
|
140
|
+
yield this.adminforth.deleteResourceRecord({
|
|
141
|
+
resource: junctionResource,
|
|
142
|
+
recordId: jr[junctionResource.columns.find(c => c.primaryKey).name],
|
|
143
|
+
record: jr,
|
|
144
|
+
adminUser: null
|
|
145
|
+
});
|
|
127
146
|
}
|
|
128
147
|
return { ok: true };
|
|
129
148
|
}));
|
|
@@ -131,7 +150,12 @@ export default class extends AdminForthPlugin {
|
|
|
131
150
|
linkedResource.hooks.delete.beforeSave.push((_a) => __awaiter(this, [_a], void 0, function* ({ recordId, record }) {
|
|
132
151
|
const existingJunctionRecords = yield this.adminforth.resource(junctionResource.resourceId).list([Filters.EQ(linkedColumnNameInJunctionResource, recordId)]);
|
|
133
152
|
for (const jr of existingJunctionRecords) {
|
|
134
|
-
yield this.adminforth.
|
|
153
|
+
yield this.adminforth.deleteResourceRecord({
|
|
154
|
+
resource: junctionResource,
|
|
155
|
+
recordId: jr[junctionResource.columns.find(c => c.primaryKey).name],
|
|
156
|
+
record: jr,
|
|
157
|
+
adminUser: null
|
|
158
|
+
});
|
|
135
159
|
}
|
|
136
160
|
return { ok: true };
|
|
137
161
|
}));
|
package/index.ts
CHANGED
|
@@ -75,18 +75,23 @@ export default class extends AdminForthPlugin {
|
|
|
75
75
|
create: false,
|
|
76
76
|
edit: false
|
|
77
77
|
},
|
|
78
|
-
label: this.options.labelForColumn || this.options.linkedResourceId
|
|
78
|
+
label: this.options.labelForColumn || this.options.linkedResourceId,
|
|
79
|
+
showIf: this.options.showIf || null,
|
|
79
80
|
};
|
|
80
81
|
resourceConfig.columns.push(virtualColumn);
|
|
81
82
|
|
|
82
83
|
// ** HOOKS FOR CREATE **//
|
|
83
84
|
|
|
84
|
-
resourceConfig.hooks.create.afterSave.push(async ({ recordId, recordWithVirtualColumns }) => {
|
|
85
|
+
resourceConfig.hooks.create.afterSave.push(async ({ recordId, recordWithVirtualColumns, adminUser }) => {
|
|
85
86
|
if ( recordWithVirtualColumns[`many2many_${this.pluginInstanceId}`] ) {
|
|
86
87
|
for(const linkedId of recordWithVirtualColumns[`many2many_${this.pluginInstanceId}`]) {
|
|
87
|
-
await this.adminforth.
|
|
88
|
-
|
|
89
|
-
|
|
88
|
+
await this.adminforth.createResourceRecord({
|
|
89
|
+
resource: junctionResource,
|
|
90
|
+
record: {
|
|
91
|
+
[resourceColumnNameInJunctionResource]: recordId,
|
|
92
|
+
[linkedColumnNameInJunctionResource]: linkedId,
|
|
93
|
+
},
|
|
94
|
+
adminUser
|
|
90
95
|
});
|
|
91
96
|
}
|
|
92
97
|
}
|
|
@@ -95,22 +100,31 @@ export default class extends AdminForthPlugin {
|
|
|
95
100
|
|
|
96
101
|
// ** HOOKS FOR EDIT **//
|
|
97
102
|
|
|
98
|
-
resourceConfig.hooks.edit.beforeSave.push(async ({recordId, updates }: { recordId: any, updates: any }) => {
|
|
103
|
+
resourceConfig.hooks.edit.beforeSave.push(async ({recordId, updates, adminUser }: { recordId: any, updates: any, adminUser: any }) => {
|
|
99
104
|
if ( updates[`many2many_${this.pluginInstanceId}`] ) {
|
|
100
105
|
const existingJunctionRecords = await this.adminforth.resource(junctionResource.resourceId).list([Filters.EQ(resourceColumnNameInJunctionResource, recordId)]);
|
|
101
106
|
const updatedLinkedIds = updates[`many2many_${this.pluginInstanceId}`];
|
|
102
107
|
for(const jr of existingJunctionRecords) {
|
|
103
108
|
const linkedId = jr[linkedColumnNameInJunctionResource];
|
|
104
109
|
if ( !updatedLinkedIds.includes(linkedId) ) {
|
|
105
|
-
await this.adminforth.
|
|
110
|
+
await this.adminforth.deleteResourceRecord({
|
|
111
|
+
resource: junctionResource,
|
|
112
|
+
recordId: jr[junctionResource.columns.find(c => c.primaryKey).name],
|
|
113
|
+
record: jr,
|
|
114
|
+
adminUser
|
|
115
|
+
});
|
|
106
116
|
}
|
|
107
117
|
}
|
|
108
118
|
for(const linkedId of updatedLinkedIds) {
|
|
109
119
|
const alreadyExists = existingJunctionRecords.find(jr => jr[linkedColumnNameInJunctionResource] === linkedId);
|
|
110
120
|
if ( !alreadyExists ) {
|
|
111
|
-
await this.adminforth.
|
|
112
|
-
|
|
113
|
-
|
|
121
|
+
await this.adminforth.createResourceRecord({
|
|
122
|
+
resource: junctionResource,
|
|
123
|
+
record: {
|
|
124
|
+
[resourceColumnNameInJunctionResource]: recordId,
|
|
125
|
+
[linkedColumnNameInJunctionResource]: linkedId,
|
|
126
|
+
},
|
|
127
|
+
adminUser
|
|
114
128
|
});
|
|
115
129
|
}
|
|
116
130
|
}
|
|
@@ -123,7 +137,12 @@ export default class extends AdminForthPlugin {
|
|
|
123
137
|
resourceConfig.hooks.delete.beforeSave.push(async ({ recordId, record }: { recordId: any, record: any }) => {
|
|
124
138
|
const existingJunctionRecords = await this.adminforth.resource(junctionResource.resourceId).list([Filters.EQ(resourceColumnNameInJunctionResource, recordId)]);
|
|
125
139
|
for(const jr of existingJunctionRecords) {
|
|
126
|
-
await this.adminforth.
|
|
140
|
+
await this.adminforth.deleteResourceRecord({
|
|
141
|
+
resource: junctionResource,
|
|
142
|
+
recordId: jr[junctionResource.columns.find(c => c.primaryKey).name],
|
|
143
|
+
record: jr,
|
|
144
|
+
adminUser: null
|
|
145
|
+
});
|
|
127
146
|
}
|
|
128
147
|
return { ok: true };
|
|
129
148
|
});
|
|
@@ -131,7 +150,12 @@ export default class extends AdminForthPlugin {
|
|
|
131
150
|
linkedResource.hooks.delete.beforeSave.push(async ({ recordId, record }: { recordId: any, record: any }) => {
|
|
132
151
|
const existingJunctionRecords = await this.adminforth.resource(junctionResource.resourceId).list([Filters.EQ(linkedColumnNameInJunctionResource, recordId)]);
|
|
133
152
|
for(const jr of existingJunctionRecords) {
|
|
134
|
-
await this.adminforth.
|
|
153
|
+
await this.adminforth.deleteResourceRecord({
|
|
154
|
+
resource: junctionResource,
|
|
155
|
+
recordId: jr[junctionResource.columns.find(c => c.primaryKey).name],
|
|
156
|
+
record: jr,
|
|
157
|
+
adminUser: null
|
|
158
|
+
});
|
|
135
159
|
}
|
|
136
160
|
return { ok: true };
|
|
137
161
|
});
|
package/package.json
CHANGED