From 466fdfbd1438d90fd531eee10f998b41b28237fe Mon Sep 17 00:00:00 2001 From: Arno Kaimbacher Date: Wed, 10 May 2023 16:44:25 +0200 Subject: [PATCH] - request also hash values of the files via pai --- src/controllers/dataset.controller.ts | 10 +++++- src/models/Dataset.ts | 3 ++ src/models/HashValue.ts | 46 +++++++++++++++++++++++++++ src/models/init-models.js | 12 +++++++ 4 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 src/models/HashValue.ts diff --git a/src/controllers/dataset.controller.ts b/src/controllers/dataset.controller.ts index 0664fcb..240aca3 100644 --- a/src/controllers/dataset.controller.ts +++ b/src/controllers/dataset.controller.ts @@ -56,12 +56,20 @@ export class DatasetController { through: { where: { role: "contributor" } }, as: "contributors", }, + "subjects", "coverage", "licenses", "references", "project", - "files", + // "files", + { + model: dbContext.File, + as: "files", + include: [ + "hashvalues" + ] + }, "identifier", ], order: [ diff --git a/src/models/Dataset.ts b/src/models/Dataset.ts index d116805..90b1bb5 100644 --- a/src/models/Dataset.ts +++ b/src/models/Dataset.ts @@ -54,6 +54,7 @@ import sequelizeConnection from "../config/db.config"; import DocumentXmlCache from "./DocumentXmlCache"; import Collection from "./Collection"; import Reference from "./Reference"; +import File from "./File"; class Dataset extends Model, InferCreationAttributes> { // id can be undefined during creation when using `autoIncrement` @@ -80,6 +81,7 @@ class Dataset extends Model, InferCreationAttributes; declare references?: NonAttribute; + declare files?: NonAttribute; // declare static associations: { // }; @@ -94,6 +96,7 @@ class Dataset extends Model, InferCreationAttributes; collections: Association; references: Association; + files: Association; }; public static async earliestPublicationDate(): Promise { diff --git a/src/models/HashValue.ts b/src/models/HashValue.ts new file mode 100644 index 0000000..f4ca512 --- /dev/null +++ b/src/models/HashValue.ts @@ -0,0 +1,46 @@ +import { Model, DataTypes, InferAttributes, InferCreationAttributes, CreationOptional } from "sequelize"; +import sequelizeConnection from "../config/db.config"; + +class HashValue extends Model, InferCreationAttributes> { + // id can be undefined during creation when using `autoIncrement` + declare file_id: number; + + declare type: string; + declare value: string; + + // // createdAt can be undefined during creation + // declare created_at: CreationOptional; + // // updatedAt can be undefined during creation + // declare updated_at: CreationOptional; +} + +HashValue.init( + { + file_id: { + type: DataTypes.INTEGER, + primaryKey: true, + }, + type: { + type: DataTypes.STRING(50), + allowNull: false, + primaryKey: true, + }, + value: { + type: DataTypes.STRING(255), + allowNull: false, + }, + + + // created_at: DataTypes.DATE, + // updated_at: DataTypes.DATE, + }, + { + // createdAt: "created_at", + // updatedAt: "updated_at", + timestamps: false, + tableName: "file_hashvalues", + sequelize: sequelizeConnection, + }, +); + +export default HashValue; diff --git a/src/models/init-models.js b/src/models/init-models.js index a132461..a417d2e 100644 --- a/src/models/init-models.js +++ b/src/models/init-models.js @@ -14,6 +14,7 @@ import Reference from "./Reference"; import Project from "./Project"; // import File from "./file.model.js"; import File from "./File"; +import HashValue from "./HashValue"; import Identifier from "./Identifier"; import DocumentXmlCache from "./DocumentXmlCache"; import CollectionRole from "./CollectionRole"; @@ -35,6 +36,7 @@ export { Identifier, DocumentXmlCache, File, + HashValue, Collection, CollectionRole, }; @@ -230,6 +232,15 @@ export function initModels() { as: "dataset", }); + File.hasMany(HashValue, { + as: "hashvalues", + foreignKey: "file_id", + }); + HashValue.belongsTo(File, { + foreignKey: "file_id", + as: "file", + }); + // collection an collectionRole relations CollectionRole.hasMany(Collection, { as: "collections", @@ -268,5 +279,6 @@ export function initModels() { Subject: Subject, License: License, DocumentPersons: DocumentPersons, + File: File }; }