diff --git a/src/controllers/dataset.controller.ts b/src/controllers/dataset.controller.ts index e76b1e7..be12f8f 100644 --- a/src/controllers/dataset.controller.ts +++ b/src/controllers/dataset.controller.ts @@ -58,6 +58,7 @@ export class DatasetController { "subjects", "coverage", "licenses", + "references", "project", "files", "identifier", diff --git a/src/models/Dataset.ts b/src/models/Dataset.ts index 06cef56..d116805 100644 --- a/src/models/Dataset.ts +++ b/src/models/Dataset.ts @@ -53,6 +53,7 @@ import { Op, Model, DataTypes, InferAttributes, InferCreationAttributes, Creatio import sequelizeConnection from "../config/db.config"; import DocumentXmlCache from "./DocumentXmlCache"; import Collection from "./Collection"; +import Reference from "./Reference"; class Dataset extends Model, InferCreationAttributes> { // id can be undefined during creation when using `autoIncrement` @@ -78,6 +79,7 @@ class Dataset extends Model, InferCreationAttributes; // Note this is optional since it's only populated when explicitly requested in code declare collections?: NonAttribute; + declare references?: NonAttribute; // declare static associations: { // }; @@ -91,6 +93,7 @@ class Dataset extends Model, InferCreationAttributes; collections: Association; + references: Association; }; public static async earliestPublicationDate(): Promise { diff --git a/src/models/Reference.ts b/src/models/Reference.ts new file mode 100644 index 0000000..75923d0 --- /dev/null +++ b/src/models/Reference.ts @@ -0,0 +1,96 @@ +// import Sequelize from "sequelize"; +import { + Model, + DataTypes, + InferAttributes, + InferCreationAttributes, + CreationOptional, + NonAttribute, + Association, + BelongsToGetAssociationMixin, +} from "sequelize"; +import sequelizeConnection from "../config/db.config"; +import Dataset from "./Dataset"; + +class Reference extends Model, InferCreationAttributes> { + // id can be undefined during creation when using `autoIncrement` + declare id: CreationOptional; + declare document_id: number; + // declare role_id: number | null; + declare type: string; // nullable fields + declare relation: string; // not nullable fields + declare value: string; // nullable fields + declare label: string; // nullable fields + declare created_at: CreationOptional; + // updatedAt can be undefined during creation + declare updated_at: CreationOptional; + + // https://sequelize.org/docs/v6/other-topics/typescript/ + // Since TS cannot determine model association at compile time + // we have to declare them here purely virtually + // these will not exist until `Model.init` was called. + declare getDataset: BelongsToGetAssociationMixin; + + // You can also pre-declare possible inclusions, these will only be populated if you + // actively include a relation.belongsTo one collectionRole + declare dataset?: NonAttribute; + // declare datasets?: NonAttribute; + + declare static associations: { + dataset: Association; + // datasets: Association; + }; + + // getters that are not attributes should be tagged using NonAttribute + // to remove them from the model's Attribute Typings. + get fullName(): NonAttribute { + return this.value + ":" + this.label; + } +} + +Reference.init( + { + id: { + type: DataTypes.INTEGER, + primaryKey: true, + }, + document_id: { + type: DataTypes.INTEGER, + primaryKey: true, + }, + // role_id: { + // type: DataTypes.INTEGER, + // allowNull: true, + // }, + type: { + type: DataTypes.STRING(255), + allowNull: false, + }, + relation: { + type: DataTypes.STRING(255), + allowNull: false, + }, + value: { + type: DataTypes.STRING(255), + allowNull: false, + }, + label: { + type: DataTypes.STRING(255), + allowNull: false, + }, + created_at: DataTypes.DATE, + updated_at: DataTypes.DATE, + // visible_publish: { + // type: DataTypes.BOOLEAN, + // defaultValue: true, + // }, + }, + { + // timestamps: false, + createdAt: "created_at", + updatedAt: "updated_at", + sequelize: sequelizeConnection, + tableName: "document_references", + }, +); +export default Reference; diff --git a/src/models/init-models.js b/src/models/init-models.js index a8c6f34..a132461 100644 --- a/src/models/init-models.js +++ b/src/models/init-models.js @@ -10,6 +10,7 @@ import User from "./user.model.js"; import Person from "./Person"; import { Sequelize } from "sequelize"; import Subject from "./subject.model.js"; +import Reference from "./Reference"; import Project from "./Project"; // import File from "./file.model.js"; import File from "./File"; @@ -29,6 +30,7 @@ export { Subject, Coverage, License, + Reference, Project, Identifier, DocumentXmlCache, @@ -197,6 +199,16 @@ export function initModels() { as: "datasets", }); + //project references + Dataset.hasMany(Reference, { + foreignKey: "document_id", + as: "references", + }); + Reference.belongsTo(Dataset, { + foreignKey: "document_id", + as: "dataset", + }); + //project relations Project.hasMany(Dataset, { foreignKey: "project_id",