From 74660120bb312fe0f593eca753b102ee6aec1145 Mon Sep 17 00:00:00 2001 From: Michael Zhang Date: Sat, 28 Aug 2021 13:03:38 -0500 Subject: [PATCH] parse from yml and stick into db --- ' | 28 ----------------------- .ignore | 1 + compile-database/db.ts | 25 +++++++++++++------- compile-database/index.ts | 42 ++++++++++++++++++++++++---------- compile-database/package.json | 9 +++++++- compile-database/page.ts | 24 +++++++++++++++++++ compile-database/tsconfig.json | 3 +++ material/fp-function.yml | 9 +++----- 8 files changed, 86 insertions(+), 55 deletions(-) delete mode 100644 ' create mode 100644 .ignore create mode 100644 compile-database/page.ts diff --git a/' b/' deleted file mode 100644 index 1db7909..0000000 --- a/' +++ /dev/null @@ -1,28 +0,0 @@ -{ writeTextFile, nsjail }: - -let - inner = writeTextFile { - name = "ocamlStudentModuleInner"; - executable = true; - text = '' - INTERFACE_FILE=$1 - STUDENT_FILE=$2 - DRIVER_FILE=$3 - - ocamlc -o student.cmi $INTERFACE_FILE - ocamlc -o student.cmo $STUDENT_FILE - ocaml student.cmo $DRIVER_FILE - ''; - }; -in -writeTextFile { - name = "ocamlStudentModule"; - executable = true; - text = '' - JAIL=$(mktemp -d) - ${nsjail}/bin/nsjail \ - -Mo \ # launch a single process using clone/execve - --chroot $JAIL - ${inner} - ''; -} diff --git a/.ignore b/.ignore new file mode 100644 index 0000000..0dec61d --- /dev/null +++ b/.ignore @@ -0,0 +1 @@ +/package-lock.json diff --git a/compile-database/db.ts b/compile-database/db.ts index 2ea1b69..4b42ef5 100644 --- a/compile-database/db.ts +++ b/compile-database/db.ts @@ -1,14 +1,23 @@ -import { Sequelize, Model, DataTypes } from "sequelize"; +import { PrimaryKey, Sequelize, Column, Table, Model } from "sequelize-typescript"; -class Page extends Model {} +@Table +export class Page extends Model { + @PrimaryKey + @Column + public slug: string; -export async function init(path: string) { - let sequelize = new Sequelize(`sqlite:${path}`); - - Page.init({ + @Column + public title: string; +} - }, { sequelize, modelName: "page" }); +@Table +export class Exercise extends Model { +} - await sequelize.sync(); +export async function init(path: string): Promise { + let sequelize = new Sequelize(`sqlite:${path}`, { + models: [Page, Exercise], + }); + await sequelize.sync({ force: true }); return sequelize; } diff --git a/compile-database/index.ts b/compile-database/index.ts index a768a40..32c0143 100644 --- a/compile-database/index.ts +++ b/compile-database/index.ts @@ -1,26 +1,44 @@ import { readdir, readFile } from "fs/promises"; import { join } from "path"; import * as yaml from "js-yaml"; +import { plainToClass } from "class-transformer"; +import { validate } from "class-validator"; -import { init } from "./db"; +import { init, Page } from "./db"; +import { Page as PageConfig } from "./page"; + +// TODO: configure this thru cmdline or something later +let materials_dir = "../material"; +let db_file = "test.db"; + +let db; + +async function loadPageIntoDb(name: string): Promise { + // check if this slug has already been loaded into the database + + let path = join(materials_dir, name); + let rawData = await readFile(path, { encoding: "utf8" }); + let parsedData = yaml.load(rawData); + let page_config = plainToClass(PageConfig, parsedData); + await validate(page_config); + console.log("Validated!", page_config); + + let page = new Page({ + slug: page_config.slug, + title: page_config.title, + }); + await page.save(); +} async function main() { - // TODO: configure this thru cmdline or something later - let materials_dir = "../material"; - let db_file = "test.db"; - - let db = await init(db_file); + db = await init(db_file); + // TODO: streaming version of readdir when the folder gets big let names = await readdir(materials_dir); await Promise.all(names .filter(name => name.toLowerCase().endsWith(".yml")) - .map(async name => { - let path = join(materials_dir, name); - let rawData = await readFile(path, { encoding: "utf8" }); - let parsedData = yaml.load(rawData); - console.log("data", parsedData); - }) + .map(loadPageIntoDb) ); } diff --git a/compile-database/package.json b/compile-database/package.json index 6e9ed95..5cc2e1c 100644 --- a/compile-database/package.json +++ b/compile-database/package.json @@ -4,11 +4,18 @@ "start": "ts-node index.ts" }, "devDependencies": { + "@types/js-yaml": "^4.0.3", + "@types/node": "^16.7.4", + "@types/validator": "^13.6.3", "ts-node": "^10.2.1" }, "dependencies": { + "class-transformer": "^0.4.0", + "class-validator": "^0.13.1", "js-yaml": "^4.1.0", + "reflect-metadata": "^0.1.13", "sequelize": "^6.6.5", - "sqlite3": "^5.0.2" + "sequelize-typescript": "^2.1.0", + "sqlite3": "^4.2.0" } } diff --git a/compile-database/page.ts b/compile-database/page.ts new file mode 100644 index 0000000..686e59b --- /dev/null +++ b/compile-database/page.ts @@ -0,0 +1,24 @@ +export class Page { + public slug: string; + + public title: string; + public type: string; + + public summary?: string; + + public content?: string; + + public exercises?: Exercise[]; +}; + +export class Exercise { + public slug: string; + public description: string; + public graders: Grader[]; +}; + +export class Grader { + public language: string; + public style: string; + public props: any; +}; diff --git a/compile-database/tsconfig.json b/compile-database/tsconfig.json index 504cd64..6935746 100644 --- a/compile-database/tsconfig.json +++ b/compile-database/tsconfig.json @@ -1,5 +1,8 @@ { "compilerOptions": { + "target": "es6", + "module": "commonjs", + "emitDecoratorMetadata": true, "experimentalDecorators": true } } diff --git a/material/fp-function.yml b/material/fp-function.yml index fc63ed3..9769d34 100644 --- a/material/fp-function.yml +++ b/material/fp-function.yml @@ -12,14 +12,11 @@ content: | exercises: - - description: | + - name: doubleIt + description: | Write a function called `doubleIt` that takes an integer and doubles it. - - examples: - ocaml: - - | - grader: + graders: ocaml: style: studentModule props: