@anteros/core 0.0.2-alpha.6 → 0.0.2-alpha.7

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 (2) hide show
  1. package/lib/files.ts +33 -4
  2. package/package.json +1 -1
package/lib/files.ts CHANGED
@@ -260,8 +260,10 @@ export async function handleUpload(options: UploadOptions): Promise<FileResult>
260
260
  // Insert metadata first to get the MongoDB auto-generated ObjectId
261
261
  let _id: string;
262
262
  try {
263
- const rest = new useRest({ tenant_id, internal: false, useHook: false, useCustomApi: false });
264
- const result = await rest.db.collection(collection).insertOne({
263
+ // Coerce relation field IDs from string to ObjectId before insert.
264
+ // Form-data / JSON always sends IDs as strings, but MongoDB expects
265
+ // ObjectId for fields that reference other collections.
266
+ const insertDoc: Record<string, any> = {
265
267
  _file: {
266
268
  filename: '',
267
269
  name: file.name,
@@ -269,8 +271,35 @@ export async function handleUpload(options: UploadOptions): Promise<FileResult>
269
271
  size: 0,
270
272
  url: '',
271
273
  },
272
- ...(data || {}),
273
- });
274
+ }
275
+ if (data) {
276
+ const relationFields =
277
+ col.fields?.filter(
278
+ (f) => f.type === 'relationship' && f.relation?.to && data[f.name] !== undefined && data[f.name] !== null,
279
+ ) ?? []
280
+ for (const [key, value] of Object.entries(data)) {
281
+ const field = relationFields.find((f) => f.name === key)
282
+ if (field) {
283
+ const hasMany = field.relation?.hasMany ?? false
284
+ if (hasMany && Array.isArray(value)) {
285
+ insertDoc[key] = value.map((id) =>
286
+ ObjectId.isValid(id) ? new ObjectId(id) : id,
287
+ )
288
+ } else if (!hasMany) {
289
+ insertDoc[key] =
290
+ typeof value === 'string' && ObjectId.isValid(value)
291
+ ? new ObjectId(value)
292
+ : value
293
+ } else {
294
+ insertDoc[key] = value
295
+ }
296
+ } else {
297
+ insertDoc[key] = value
298
+ }
299
+ }
300
+ }
301
+ const rest = new useRest({ tenant_id, internal: false, useHook: false, useCustomApi: false });
302
+ const result = await rest.db.collection(collection).insertOne(insertDoc);
274
303
  _id = String(result.insertedId);
275
304
  } catch (err) {
276
305
  throw new AppError('Failed to save file metadata', {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@anteros/core",
3
3
  "module": "index.ts",
4
- "version": "0.0.2-alpha.6",
4
+ "version": "0.0.2-alpha.7",
5
5
  "type": "module",
6
6
  "description": "Anteros Core",
7
7
  "private": false,