- add references to dataset api request

This commit is contained in:
Arno Kaimbacher 2023-04-20 09:55:56 +02:00
parent 3356992af8
commit 77d620bdbd
4 changed files with 112 additions and 0 deletions

View File

@ -58,6 +58,7 @@ export class DatasetController {
"subjects", "subjects",
"coverage", "coverage",
"licenses", "licenses",
"references",
"project", "project",
"files", "files",
"identifier", "identifier",

View File

@ -53,6 +53,7 @@ import { Op, Model, DataTypes, InferAttributes, InferCreationAttributes, Creatio
import sequelizeConnection from "../config/db.config"; import sequelizeConnection from "../config/db.config";
import DocumentXmlCache from "./DocumentXmlCache"; import DocumentXmlCache from "./DocumentXmlCache";
import Collection from "./Collection"; import Collection from "./Collection";
import Reference from "./Reference";
class Dataset extends Model<InferAttributes<Dataset>, InferCreationAttributes<Dataset>> { class Dataset extends Model<InferAttributes<Dataset>, InferCreationAttributes<Dataset>> {
// id can be undefined during creation when using `autoIncrement` // id can be undefined during creation when using `autoIncrement`
@ -78,6 +79,7 @@ class Dataset extends Model<InferAttributes<Dataset>, InferCreationAttributes<Da
declare xmlCache?: NonAttribute<DocumentXmlCache>; // Note this is optional since it's only populated when explicitly requested in code declare xmlCache?: NonAttribute<DocumentXmlCache>; // Note this is optional since it's only populated when explicitly requested in code
declare collections?: NonAttribute<Collection[]>; declare collections?: NonAttribute<Collection[]>;
declare references?: NonAttribute<Reference[]>;
// declare static associations: { // declare static associations: {
// }; // };
@ -91,6 +93,7 @@ class Dataset extends Model<InferAttributes<Dataset>, InferCreationAttributes<Da
declare static associations: { declare static associations: {
xmlCache: Association<Dataset, DocumentXmlCache>; xmlCache: Association<Dataset, DocumentXmlCache>;
collections: Association<Dataset, Collection>; collections: Association<Dataset, Collection>;
references: Association<Dataset, Reference>;
}; };
public static async earliestPublicationDate(): Promise<Dataset | null> { public static async earliestPublicationDate(): Promise<Dataset | null> {

96
src/models/Reference.ts Normal file
View File

@ -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<InferAttributes<Reference>, InferCreationAttributes<Reference>> {
// id can be undefined during creation when using `autoIncrement`
declare id: CreationOptional<number>;
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<Date>;
// updatedAt can be undefined during creation
declare updated_at: CreationOptional<Date>;
// 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<Dataset>;
// You can also pre-declare possible inclusions, these will only be populated if you
// actively include a relation.belongsTo one collectionRole
declare dataset?: NonAttribute<Dataset>;
// declare datasets?: NonAttribute<Dataset>;
declare static associations: {
dataset: Association<Reference, Dataset>;
// datasets: Association<Collection, Dataset>;
};
// getters that are not attributes should be tagged using NonAttribute
// to remove them from the model's Attribute Typings.
get fullName(): NonAttribute<string> {
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;

View File

@ -10,6 +10,7 @@ import User from "./user.model.js";
import Person from "./Person"; import Person from "./Person";
import { Sequelize } from "sequelize"; import { Sequelize } from "sequelize";
import Subject from "./subject.model.js"; import Subject from "./subject.model.js";
import Reference from "./Reference";
import Project from "./Project"; import Project from "./Project";
// import File from "./file.model.js"; // import File from "./file.model.js";
import File from "./File"; import File from "./File";
@ -29,6 +30,7 @@ export {
Subject, Subject,
Coverage, Coverage,
License, License,
Reference,
Project, Project,
Identifier, Identifier,
DocumentXmlCache, DocumentXmlCache,
@ -197,6 +199,16 @@ export function initModels() {
as: "datasets", as: "datasets",
}); });
//project references
Dataset.hasMany(Reference, {
foreignKey: "document_id",
as: "references",
});
Reference.belongsTo(Dataset, {
foreignKey: "document_id",
as: "dataset",
});
//project relations //project relations
Project.hasMany(Dataset, { Project.hasMany(Dataset, {
foreignKey: "project_id", foreignKey: "project_id",