From 9dc0b437375454092972ddbea6170ea23dce585b Mon Sep 17 00:00:00 2001 From: wancat Date: Fri, 9 Feb 2024 16:24:12 +0800 Subject: [PATCH] add types to models --- packages/models/package.json | 3 +- packages/models/src/command.ts | 19 ++++++++---- packages/models/src/commandResponder.ts | 16 ++++++---- packages/models/src/condition.ts | 22 +++++++++++--- packages/models/src/exception.ts | 11 ++++--- packages/models/src/globalException.ts | 10 +++---- packages/models/src/hint.ts | 8 +++-- packages/models/src/index.ts | 10 +++---- packages/models/src/quest.ts | 20 +++++++++---- packages/models/src/requirement.ts | 8 +++-- packages/models/src/response.ts | 11 +++++-- packages/models/src/session.ts | 39 ++++++++++++++++++++----- packages/models/src/stage.ts | 19 ++++++++---- packages/models/src/stageException.ts | 10 ++++--- packages/models/src/user.ts | 23 +++++++++++---- packages/models/src/userProfile.ts | 23 +++++++++++---- 16 files changed, 179 insertions(+), 73 deletions(-) diff --git a/packages/models/package.json b/packages/models/package.json index 32bf95f..a49e61b 100644 --- a/packages/models/package.json +++ b/packages/models/package.json @@ -8,9 +8,10 @@ "exports": "./dist/index.js", "scripts": { "build": "tsc", - "watch": "tsc -w" + "dev": "tsc -w" }, "dependencies": { + "@types/mongoose": "^5.11.97", "mongoose": "^8.0.3" } } diff --git a/packages/models/src/command.ts b/packages/models/src/command.ts index 40a03bb..079a933 100644 --- a/packages/models/src/command.ts +++ b/packages/models/src/command.ts @@ -1,8 +1,19 @@ -import { model, Schema } from 'mongoose' +import { model, Schema, Types } from 'mongoose' -const Command = model( +export interface ICommand { + session: Types.ObjectId + pwd: string + command: string + output?: string + error?: string + stage?: string + createdAt: Date + updatedAt: Date +} + +export const Command = model( 'Command', - new Schema( + new Schema( { session: { type: Schema.ObjectId, @@ -33,5 +44,3 @@ const Command = model( { timestamps: true } ) ) - -export default Command diff --git a/packages/models/src/commandResponder.ts b/packages/models/src/commandResponder.ts index 0078cb7..5ec08a6 100644 --- a/packages/models/src/commandResponder.ts +++ b/packages/models/src/commandResponder.ts @@ -1,9 +1,15 @@ import { Schema } from 'mongoose' -import conditionSchema from './condition.js' -import responseSchema from './response.js' -import hintSchema from './hint.js' +import { conditionSchema, ICondition } from './condition.js' +import { responseSchema, IResponse } from './response.js' +import { hintSchema, IHint } from './hint.js' -const commandResponderSchema = new Schema({ +export interface ICommandResponder { + condition?: ICondition + responses: IResponse[] + hints?: IHint[] +} + +export const commandResponderSchema = new Schema({ condition: { type: conditionSchema, required: false, @@ -15,5 +21,3 @@ const commandResponderSchema = new Schema({ }) commandResponderSchema.add(hintSchema) - -export default commandResponderSchema diff --git a/packages/models/src/condition.ts b/packages/models/src/condition.ts index 8f236af..d878348 100644 --- a/packages/models/src/condition.ts +++ b/packages/models/src/condition.ts @@ -1,6 +1,22 @@ import { Schema } from 'mongoose' -const fileConditionSchema = new Schema({ +export interface IFileCondition { + path: string + type?: string + exists?: boolean +} + +export interface ICondition { + command?: string[] + output?: string[] + error?: string[] + pwd?: string[] + files?: IFileCondition[] + or?: ICondition[] + not?: ICondition +} + +export const fileConditionSchema = new Schema({ path: { type: String, required: true, @@ -12,7 +28,7 @@ const fileConditionSchema = new Schema({ }, }) -const conditionSchema = new Schema({ +export const conditionSchema = new Schema({ command: [String], output: [String], error: [String], @@ -24,5 +40,3 @@ conditionSchema.add({ or: [conditionSchema], not: conditionSchema, }) - -export default conditionSchema diff --git a/packages/models/src/exception.ts b/packages/models/src/exception.ts index 60e90b6..b883489 100644 --- a/packages/models/src/exception.ts +++ b/packages/models/src/exception.ts @@ -1,8 +1,11 @@ import { Schema } from 'mongoose' -import commandResponderSchema from './commandResponder.js' +import { + commandResponderSchema, + ICommandResponder, +} from './commandResponder.js' -const exceptionSchema = new Schema({}) +export interface IException extends ICommandResponder {} + +export const exceptionSchema = new Schema({}) exceptionSchema.add(commandResponderSchema) - -export default exceptionSchema diff --git a/packages/models/src/globalException.ts b/packages/models/src/globalException.ts index 5fd191d..a307c56 100644 --- a/packages/models/src/globalException.ts +++ b/packages/models/src/globalException.ts @@ -1,10 +1,10 @@ import { Schema } from 'mongoose' -import exceptionSchema from './exception.js' -import requirementSchema from './requirement.js' +import { exceptionSchema, IException } from './exception.js' +import { requirementSchema, IRequirement } from './requirement.js' -const globalExceptionSchema = new Schema({}) +export interface IGlobalException extends IException, IRequirement {} + +export const globalExceptionSchema = new Schema({}) globalExceptionSchema.add(exceptionSchema) globalExceptionSchema.add(requirementSchema) - -export default globalExceptionSchema diff --git a/packages/models/src/hint.ts b/packages/models/src/hint.ts index a46eab9..867c333 100644 --- a/packages/models/src/hint.ts +++ b/packages/models/src/hint.ts @@ -1,10 +1,12 @@ import { Schema } from 'mongoose' -const hintSchema = new Schema({ +export interface IHint { + hints: string[] +} + +export const hintSchema = new Schema({ hints: { type: [String], required: true, }, }) - -export default hintSchema diff --git a/packages/models/src/index.ts b/packages/models/src/index.ts index d69ae25..aa34aad 100644 --- a/packages/models/src/index.ts +++ b/packages/models/src/index.ts @@ -1,10 +1,10 @@ import mongoose from 'mongoose' -export { default as User } from './user.js' -export { default as Quest } from './quest.js' -export { default as Session } from './session.js' -export { default as Command } from './command.js' -export { default as UserProfile } from './userProfile.js' +export { User } from './user.js' +export { Quest, IQuest } from './quest.js' +export { Session } from './session.js' +export { Command, ICommand } from './command.js' +export { UserProfile } from './userProfile.js' export default async function connectDB(uri: string) { const db = await mongoose.connect(uri) diff --git a/packages/models/src/quest.ts b/packages/models/src/quest.ts index 69f4c80..5a9607c 100644 --- a/packages/models/src/quest.ts +++ b/packages/models/src/quest.ts @@ -1,10 +1,20 @@ import { model, Schema } from 'mongoose' -import stageSchema from './stage.js' -import globalExceptionSchema from './globalException.js' +import { IStage, stageSchema } from './stage.js' +import { globalExceptionSchema, IGlobalException } from './globalException.js' +import { IRequirement } from './requirement.js' -const Quest = model( +export interface IQuest extends IRequirement { + _id: string + title: string + image: string + instruction: string + stages: IStage[] + exceptions: IGlobalException[] +} + +export const Quest = model( 'Quest', - new Schema({ + new Schema({ _id: { type: String, required: true, @@ -40,5 +50,3 @@ const Quest = model( }, }) ) - -export default Quest diff --git a/packages/models/src/requirement.ts b/packages/models/src/requirement.ts index 9edf61e..7c95b3a 100644 --- a/packages/models/src/requirement.ts +++ b/packages/models/src/requirement.ts @@ -1,6 +1,10 @@ import { Schema } from 'mongoose' -const requirementSchema = new Schema({ +export interface IRequirement { + requirements: string[] +} + +export const requirementSchema = new Schema({ requirements: [ { type: String, @@ -8,5 +12,3 @@ const requirementSchema = new Schema({ }, ], }) - -export default requirementSchema diff --git a/packages/models/src/response.ts b/packages/models/src/response.ts index d108df6..44fd231 100644 --- a/packages/models/src/response.ts +++ b/packages/models/src/response.ts @@ -1,6 +1,13 @@ import { Schema } from 'mongoose' -const responseSchema = new Schema({ +export interface IResponse { + type: 'narrative' | 'dialogue' + content: string[] + speaker?: string + color?: string +} + +export const responseSchema = new Schema({ type: { type: String, enum: ['narrative', 'dialogue'], @@ -14,5 +21,3 @@ const responseSchema = new Schema({ speaker: { type: String, default: 'Ada' }, color: String, }) - -export default responseSchema diff --git a/packages/models/src/session.ts b/packages/models/src/session.ts index 4d4e185..ab3ba45 100644 --- a/packages/models/src/session.ts +++ b/packages/models/src/session.ts @@ -1,6 +1,13 @@ -import { model, Schema } from 'mongoose' +import { model, Schema, Types } from 'mongoose' -const nodeSchema = new Schema({ +export interface INode { + path: string + type: string + discovered: boolean + children?: INode[] +} + +const nodeSchema = new Schema({ path: String, type: String, discovered: { @@ -13,7 +20,13 @@ nodeSchema.add({ children: [nodeSchema], }) -const taskSchema = new Schema({ +export interface ITask { + id: string + name: string + completed: boolean +} + +const taskSchema = new Schema({ id: { type: String, required: true, @@ -28,9 +41,23 @@ const taskSchema = new Schema({ }, }) -const Session = model( +export interface ISession { + user: Types.ObjectId + quest: string + containerId: string + status: string + createdAt: Date + updatedAt: Date + finishedAt?: Date + lastActivityAt: Date + tasks: ITask[] + hints: string[][] + graph: INode +} + +export const Session = model( 'Session', - new Schema( + new Schema( { user: { type: Schema.ObjectId, @@ -68,5 +95,3 @@ const Session = model( { timestamps: true } ) ) - -export default Session diff --git a/packages/models/src/stage.ts b/packages/models/src/stage.ts index 73c6070..05ac5ff 100644 --- a/packages/models/src/stage.ts +++ b/packages/models/src/stage.ts @@ -1,9 +1,18 @@ import { Schema } from 'mongoose' -import commandResponderSchema from './commandResponder.js' -import stageExceptionSchema from './stageException.js' -import requirementSchema from './requirement.js' +import { + commandResponderSchema, + ICommandResponder, +} from './commandResponder.js' +import { stageExceptionSchema, IStageException } from './stageException.js' +import { requirementSchema, IRequirement } from './requirement.js' -const stageSchema = new Schema({ +export interface IStage extends IRequirement, ICommandResponder { + id: string + task: string + exceptions: IStageException[] +} + +export const stageSchema = new Schema({ id: { type: String, required: true, @@ -20,5 +29,3 @@ const stageSchema = new Schema({ stageSchema.add(commandResponderSchema) stageSchema.add(requirementSchema) - -export default stageSchema diff --git a/packages/models/src/stageException.ts b/packages/models/src/stageException.ts index f11d7d7..8d886f2 100644 --- a/packages/models/src/stageException.ts +++ b/packages/models/src/stageException.ts @@ -1,7 +1,11 @@ import { Schema } from 'mongoose' -import exceptionSchema from './exception.js' +import { exceptionSchema, IException } from './exception.js' -const stageExceptionSchema = new Schema({ +export interface IStageException extends IException { + catchAll: boolean +} + +export const stageExceptionSchema = new Schema({ catchAll: { type: Boolean, required: true, @@ -10,5 +14,3 @@ const stageExceptionSchema = new Schema({ }) stageExceptionSchema.add(exceptionSchema) - -export default stageExceptionSchema diff --git a/packages/models/src/user.ts b/packages/models/src/user.ts index 1fad86d..8ac5bcd 100644 --- a/packages/models/src/user.ts +++ b/packages/models/src/user.ts @@ -1,6 +1,12 @@ import { model, Schema } from 'mongoose' -const socialLoginSchema = new Schema({ +export interface ISocialLogin { + provider: string + id: string + displayName?: string +} + +const socialLoginSchema = new Schema({ provider: { type: String, required: true, @@ -15,9 +21,18 @@ const socialLoginSchema = new Schema({ }, }) -const User = model( +export interface IUser { + username: string + email: string + hashedPassword: string + socialLogins: Map + createdAt: Date + updatedAt: Date +} + +export const User = model( 'User', - new Schema( + new Schema( { username: { type: String, @@ -42,5 +57,3 @@ const User = model( } ) ) - -export default User diff --git a/packages/models/src/userProfile.ts b/packages/models/src/userProfile.ts index 61fe45d..d6bfcae 100644 --- a/packages/models/src/userProfile.ts +++ b/packages/models/src/userProfile.ts @@ -1,6 +1,14 @@ -import { model, Schema } from 'mongoose' +import { model, Schema, Types } from 'mongoose' -const progressSchema = new Schema({ +export interface IProgress { + quest: string + sessions: Types.ObjectId[] + completed: boolean + startedAt: Date + finishedAt?: Date +} + +const progressSchema = new Schema({ quest: { type: String, ref: 'Quest', @@ -25,9 +33,14 @@ const progressSchema = new Schema({ finishedAt: Date, }) -const UserProfile = model( +export interface IUserProfile { + user: Types.ObjectId + progress: Map +} + +export const UserProfile = model( 'UserProfile', - new Schema( + new Schema( { user: { type: Schema.ObjectId, @@ -47,5 +60,3 @@ const UserProfile = model( } ) ) - -export default UserProfile