@strapi-community/plugin-io 5.3.4 → 5.3.5

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.
@@ -1050,6 +1050,33 @@ function getTransactionCtx() {
1050
1050
  return transactionCtx;
1051
1051
  }
1052
1052
  const { pluginId: pluginId$6 } = pluginId_1;
1053
+ function buildPopulateFromSchema(strapi2, uid) {
1054
+ const contentType = strapi2.contentTypes[uid];
1055
+ if (!contentType?.attributes) return {};
1056
+ const populate2 = {};
1057
+ for (const [key, attr] of Object.entries(contentType.attributes)) {
1058
+ if (attr.type === "relation" || attr.type === "media" || attr.type === "component" || attr.type === "dynamiczone") {
1059
+ populate2[key] = true;
1060
+ }
1061
+ }
1062
+ return populate2;
1063
+ }
1064
+ async function fetchWithRelations(strapi2, uid, rawResult) {
1065
+ const documentId = rawResult?.documentId;
1066
+ if (!documentId) return rawResult;
1067
+ try {
1068
+ const populate2 = buildPopulateFromSchema(strapi2, uid);
1069
+ if (Object.keys(populate2).length === 0) return rawResult;
1070
+ const fetched = await strapi2.documents(uid).findOne({ documentId, populate: populate2 });
1071
+ return fetched || rawResult;
1072
+ } catch (err) {
1073
+ strapi2.log.debug(`socket.io: Could not re-fetch ${uid} with relations: ${err.message}`);
1074
+ return rawResult;
1075
+ }
1076
+ }
1077
+ function shouldPopulateRelations(strapi2) {
1078
+ return strapi2.$ioSettings?.events?.includeRelations === true;
1079
+ }
1053
1080
  function scheduleAfterTransaction(callback, delay = 0) {
1054
1081
  const runner = () => setTimeout(callback, delay);
1055
1082
  const ctx = getTransactionCtx();
@@ -1097,15 +1124,12 @@ async function bootstrapLifecycles$1({ strapi: strapi2 }) {
1097
1124
  return;
1098
1125
  }
1099
1126
  try {
1100
- const eventData = {
1101
- event: "create",
1102
- schema: event.model,
1103
- data: JSON.parse(JSON.stringify(event.result))
1104
- // Deep clone
1105
- };
1106
- scheduleAfterTransaction(() => {
1127
+ const rawData = JSON.parse(JSON.stringify(event.result));
1128
+ const modelInfo = { singularName: event.model.singularName, uid: event.model.uid };
1129
+ scheduleAfterTransaction(async () => {
1107
1130
  try {
1108
- strapi2.$io.emit(eventData);
1131
+ const data = shouldPopulateRelations(strapi2) ? await fetchWithRelations(strapi2, uid, rawData) : rawData;
1132
+ strapi2.$io.emit({ event: "create", schema: modelInfo, data });
1109
1133
  } catch (error2) {
1110
1134
  strapi2.log.error(`socket.io: Could not emit create event for ${uid}:`, error2.message);
1111
1135
  }
@@ -1119,6 +1143,9 @@ async function bootstrapLifecycles$1({ strapi: strapi2 }) {
1119
1143
  const query = buildEventQuery({ event });
1120
1144
  if (query.filters) {
1121
1145
  const clonedQuery = JSON.parse(JSON.stringify(query));
1146
+ if (shouldPopulateRelations(strapi2)) {
1147
+ clonedQuery.populate = buildPopulateFromSchema(strapi2, uid);
1148
+ }
1122
1149
  const modelInfo = { singularName: event.model.singularName, uid: event.model.uid };
1123
1150
  scheduleAfterTransaction(async () => {
1124
1151
  try {
@@ -1143,7 +1170,11 @@ async function bootstrapLifecycles$1({ strapi: strapi2 }) {
1143
1170
  try {
1144
1171
  const documentId = event.params.where?.documentId || event.params.documentId;
1145
1172
  if (!documentId) return;
1146
- const existing = await strapi2.documents(uid).findOne({ documentId });
1173
+ const query = { documentId };
1174
+ if (shouldPopulateRelations(strapi2)) {
1175
+ query.populate = buildPopulateFromSchema(strapi2, uid);
1176
+ }
1177
+ const existing = await strapi2.documents(uid).findOne(query);
1147
1178
  if (existing) {
1148
1179
  if (!event.state.io) event.state.io = {};
1149
1180
  event.state.io.previousData = JSON.parse(JSON.stringify(existing));
@@ -1154,11 +1185,12 @@ async function bootstrapLifecycles$1({ strapi: strapi2 }) {
1154
1185
  };
1155
1186
  subscriber.afterUpdate = async (event) => {
1156
1187
  if (!isActionEnabled(strapi2, uid, "update")) return;
1157
- const newData = JSON.parse(JSON.stringify(event.result));
1188
+ const rawData = JSON.parse(JSON.stringify(event.result));
1158
1189
  const previousData = event.state.io?.previousData || null;
1159
1190
  const modelInfo = { singularName: event.model.singularName, uid: event.model.uid };
1160
- scheduleAfterTransaction(() => {
1191
+ scheduleAfterTransaction(async () => {
1161
1192
  try {
1193
+ const newData = shouldPopulateRelations(strapi2) ? await fetchWithRelations(strapi2, uid, rawData) : rawData;
1162
1194
  const diffService = strapi2.plugin(pluginId$6).service("diff");
1163
1195
  const previewService = strapi2.plugin(pluginId$6).service("preview");
1164
1196
  const fieldLevelEnabled = strapi2.$ioSettings?.fieldLevelChanges?.enabled !== false;
@@ -1195,11 +1227,13 @@ async function bootstrapLifecycles$1({ strapi: strapi2 }) {
1195
1227
  if (!params || !params.where) return;
1196
1228
  const clonedWhere = JSON.parse(JSON.stringify(params.where));
1197
1229
  const modelInfo = { singularName: event.model.singularName, uid: event.model.uid };
1230
+ const query = { filters: clonedWhere };
1231
+ if (shouldPopulateRelations(strapi2)) {
1232
+ query.populate = buildPopulateFromSchema(strapi2, uid);
1233
+ }
1198
1234
  scheduleAfterTransaction(async () => {
1199
1235
  try {
1200
- const records = await strapi2.documents(uid).findMany({
1201
- filters: clonedWhere
1202
- });
1236
+ const records = await strapi2.documents(uid).findMany(query);
1203
1237
  records.forEach((r) => {
1204
1238
  strapi2.$io.emit({
1205
1239
  event: "update",
@@ -1018,6 +1018,33 @@ function getTransactionCtx() {
1018
1018
  return transactionCtx;
1019
1019
  }
1020
1020
  const { pluginId: pluginId$6 } = pluginId_1;
1021
+ function buildPopulateFromSchema(strapi2, uid) {
1022
+ const contentType = strapi2.contentTypes[uid];
1023
+ if (!contentType?.attributes) return {};
1024
+ const populate2 = {};
1025
+ for (const [key, attr] of Object.entries(contentType.attributes)) {
1026
+ if (attr.type === "relation" || attr.type === "media" || attr.type === "component" || attr.type === "dynamiczone") {
1027
+ populate2[key] = true;
1028
+ }
1029
+ }
1030
+ return populate2;
1031
+ }
1032
+ async function fetchWithRelations(strapi2, uid, rawResult) {
1033
+ const documentId = rawResult?.documentId;
1034
+ if (!documentId) return rawResult;
1035
+ try {
1036
+ const populate2 = buildPopulateFromSchema(strapi2, uid);
1037
+ if (Object.keys(populate2).length === 0) return rawResult;
1038
+ const fetched = await strapi2.documents(uid).findOne({ documentId, populate: populate2 });
1039
+ return fetched || rawResult;
1040
+ } catch (err) {
1041
+ strapi2.log.debug(`socket.io: Could not re-fetch ${uid} with relations: ${err.message}`);
1042
+ return rawResult;
1043
+ }
1044
+ }
1045
+ function shouldPopulateRelations(strapi2) {
1046
+ return strapi2.$ioSettings?.events?.includeRelations === true;
1047
+ }
1021
1048
  function scheduleAfterTransaction(callback, delay = 0) {
1022
1049
  const runner = () => setTimeout(callback, delay);
1023
1050
  const ctx = getTransactionCtx();
@@ -1065,15 +1092,12 @@ async function bootstrapLifecycles$1({ strapi: strapi2 }) {
1065
1092
  return;
1066
1093
  }
1067
1094
  try {
1068
- const eventData = {
1069
- event: "create",
1070
- schema: event.model,
1071
- data: JSON.parse(JSON.stringify(event.result))
1072
- // Deep clone
1073
- };
1074
- scheduleAfterTransaction(() => {
1095
+ const rawData = JSON.parse(JSON.stringify(event.result));
1096
+ const modelInfo = { singularName: event.model.singularName, uid: event.model.uid };
1097
+ scheduleAfterTransaction(async () => {
1075
1098
  try {
1076
- strapi2.$io.emit(eventData);
1099
+ const data = shouldPopulateRelations(strapi2) ? await fetchWithRelations(strapi2, uid, rawData) : rawData;
1100
+ strapi2.$io.emit({ event: "create", schema: modelInfo, data });
1077
1101
  } catch (error2) {
1078
1102
  strapi2.log.error(`socket.io: Could not emit create event for ${uid}:`, error2.message);
1079
1103
  }
@@ -1087,6 +1111,9 @@ async function bootstrapLifecycles$1({ strapi: strapi2 }) {
1087
1111
  const query = buildEventQuery({ event });
1088
1112
  if (query.filters) {
1089
1113
  const clonedQuery = JSON.parse(JSON.stringify(query));
1114
+ if (shouldPopulateRelations(strapi2)) {
1115
+ clonedQuery.populate = buildPopulateFromSchema(strapi2, uid);
1116
+ }
1090
1117
  const modelInfo = { singularName: event.model.singularName, uid: event.model.uid };
1091
1118
  scheduleAfterTransaction(async () => {
1092
1119
  try {
@@ -1111,7 +1138,11 @@ async function bootstrapLifecycles$1({ strapi: strapi2 }) {
1111
1138
  try {
1112
1139
  const documentId = event.params.where?.documentId || event.params.documentId;
1113
1140
  if (!documentId) return;
1114
- const existing = await strapi2.documents(uid).findOne({ documentId });
1141
+ const query = { documentId };
1142
+ if (shouldPopulateRelations(strapi2)) {
1143
+ query.populate = buildPopulateFromSchema(strapi2, uid);
1144
+ }
1145
+ const existing = await strapi2.documents(uid).findOne(query);
1115
1146
  if (existing) {
1116
1147
  if (!event.state.io) event.state.io = {};
1117
1148
  event.state.io.previousData = JSON.parse(JSON.stringify(existing));
@@ -1122,11 +1153,12 @@ async function bootstrapLifecycles$1({ strapi: strapi2 }) {
1122
1153
  };
1123
1154
  subscriber.afterUpdate = async (event) => {
1124
1155
  if (!isActionEnabled(strapi2, uid, "update")) return;
1125
- const newData = JSON.parse(JSON.stringify(event.result));
1156
+ const rawData = JSON.parse(JSON.stringify(event.result));
1126
1157
  const previousData = event.state.io?.previousData || null;
1127
1158
  const modelInfo = { singularName: event.model.singularName, uid: event.model.uid };
1128
- scheduleAfterTransaction(() => {
1159
+ scheduleAfterTransaction(async () => {
1129
1160
  try {
1161
+ const newData = shouldPopulateRelations(strapi2) ? await fetchWithRelations(strapi2, uid, rawData) : rawData;
1130
1162
  const diffService = strapi2.plugin(pluginId$6).service("diff");
1131
1163
  const previewService = strapi2.plugin(pluginId$6).service("preview");
1132
1164
  const fieldLevelEnabled = strapi2.$ioSettings?.fieldLevelChanges?.enabled !== false;
@@ -1163,11 +1195,13 @@ async function bootstrapLifecycles$1({ strapi: strapi2 }) {
1163
1195
  if (!params || !params.where) return;
1164
1196
  const clonedWhere = JSON.parse(JSON.stringify(params.where));
1165
1197
  const modelInfo = { singularName: event.model.singularName, uid: event.model.uid };
1198
+ const query = { filters: clonedWhere };
1199
+ if (shouldPopulateRelations(strapi2)) {
1200
+ query.populate = buildPopulateFromSchema(strapi2, uid);
1201
+ }
1166
1202
  scheduleAfterTransaction(async () => {
1167
1203
  try {
1168
- const records = await strapi2.documents(uid).findMany({
1169
- filters: clonedWhere
1170
- });
1204
+ const records = await strapi2.documents(uid).findMany(query);
1171
1205
  records.forEach((r) => {
1172
1206
  strapi2.$io.emit({
1173
1207
  event: "update",
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package",
3
3
  "name": "@strapi-community/plugin-io",
4
- "version": "5.3.4",
4
+ "version": "5.3.5",
5
5
  "description": "A plugin for Strapi CMS that provides the ability for Socket IO integration",
6
6
  "keywords": [
7
7
  "strapi",