eduproj/materialdb/db/page.ts
2021-08-29 02:39:52 -05:00

65 lines
1.3 KiB
TypeScript

import { PrimaryKey, ForeignKey, HasMany, Sequelize, Column, Table, Model, DataType } from "sequelize-typescript";
@Table
export class Page extends Model {
@PrimaryKey
@Column(DataType.STRING)
public slug: string;
@Column(DataType.STRING)
public title: string;
@Column(DataType.STRING)
public type: string;
@Column(DataType.STRING)
public content: string;
}
@Table
export class Exercise extends Model {
@PrimaryKey
@Column(DataType.STRING)
public name: string;
@HasMany(() => Grader)
public graders: Grader[];
}
@Table
export class ExerciseSatisfiesConcept extends Model {
@ForeignKey(() => Exercise)
@Column(DataType.STRING)
public exercise_name: string;
@ForeignKey(() => Page)
@Column(DataType.STRING)
public concept_slug: string;
}
@Table
export class Grader extends Model {
@PrimaryKey
@ForeignKey(() => Exercise)
@Column(DataType.STRING)
public exercise_name: string;
@PrimaryKey
@Column(DataType.STRING)
public language: string;
@Column(DataType.STRING)
public style: string;
@Column(DataType.JSON)
public props: any;
}
export async function init(path: string): Promise<Sequelize> {
let sequelize = new Sequelize(`sqlite:${path}`, {
models: [Page, Exercise, Grader],
});
await sequelize.sync({ force: true });
return sequelize;
}