diff --git a/src/controllers/dataset.controller.ts b/src/controllers/dataset.controller.ts index b6f2fd0..0b3d439 100644 --- a/src/controllers/dataset.controller.ts +++ b/src/controllers/dataset.controller.ts @@ -61,7 +61,20 @@ export class DatasetController { "coverage", "licenses", "references", + // "referenced_by", "project", + { + model: dbContext.Reference, + as: "referenced_by", + include: [ + { + model: dbContext.Dataset, + as: "dataset", + foreignKey: "document_id", + include: ["identifier"], + }, + ], + }, // "files", { model: dbContext.File, @@ -149,7 +162,20 @@ export class DatasetController { "coverage", "licenses", "references", + // "referenced_by", "project", + { + model: dbContext.Reference, + as: "referenced_by", + include: [ + { + model: dbContext.Dataset, + as: "dataset", + foreignKey: "document_id", + include: ["identifier"], + }, + ], + }, // "files", { model: dbContext.File, diff --git a/src/models/Reference.ts b/src/models/Reference.ts index 75923d0..526cd92 100644 --- a/src/models/Reference.ts +++ b/src/models/Reference.ts @@ -24,6 +24,7 @@ class Reference extends Model, InferCreationAttribute declare created_at: CreationOptional; // updatedAt can be undefined during creation declare updated_at: CreationOptional; + declare related_document_id?: number; // https://sequelize.org/docs/v6/other-topics/typescript/ // Since TS cannot determine model association at compile time diff --git a/src/models/init-models.js b/src/models/init-models.js index a417d2e..aa13072 100644 --- a/src/models/init-models.js +++ b/src/models/init-models.js @@ -210,6 +210,15 @@ export function initModels() { foreignKey: "document_id", as: "dataset", }); + Dataset.hasMany(Reference, { + foreignKey: "related_document_id", + as: "referenced_by", + }); + Reference.belongsTo(Dataset, { + foreignKey: "related_document_id", + as: "new_dataset", + include: "identifier" + }); //project relations Project.hasMany(Dataset, { @@ -279,6 +288,7 @@ export function initModels() { Subject: Subject, License: License, DocumentPersons: DocumentPersons, - File: File + File: File, + Reference: Reference }; }