add types to models

This commit is contained in:
wancat committed 2024-02-19 18:05:01 +08:00
1 parent 5489de2432
commit 9dc0b43737
16 files changed
+179 -73

No files matched your search

+2 -1
View File
@@ -8,9 +8,10 @@
"exports": "./dist/index.js", "exports": "./dist/index.js",
"scripts": { "scripts": {
"build": "tsc", "build": "tsc",
"watch": "tsc -w" "dev": "tsc -w"
}, },
"dependencies": { "dependencies": {
"@types/mongoose": "^5.11.97",
"mongoose": "^8.0.3" "mongoose": "^8.0.3"
} }
} }
+14 -5
View File
@@ -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<ICommand>(
'Command', 'Command',
new Schema( new Schema<ICommand>(
{ {
session: { session: {
type: Schema.ObjectId, type: Schema.ObjectId,
@@ -33,5 +44,3 @@ const Command = model(
{ timestamps: true } { timestamps: true }
) )
) )
export default Command
+10 -6
View File
@@ -1,9 +1,15 @@
import { Schema } from 'mongoose' import { Schema } from 'mongoose'
import conditionSchema from './condition.js' import { conditionSchema, ICondition } from './condition.js'
import responseSchema from './response.js' import { responseSchema, IResponse } from './response.js'
import hintSchema from './hint.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<ICommandResponder>({
condition: { condition: {
type: conditionSchema, type: conditionSchema,
required: false, required: false,
@@ -15,5 +21,3 @@ const commandResponderSchema = new Schema({
}) })
commandResponderSchema.add(hintSchema) commandResponderSchema.add(hintSchema)
export default commandResponderSchema
+18 -4
View File
@@ -1,6 +1,22 @@
import { Schema } from 'mongoose' 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<IFileCondition>({
path: { path: {
type: String, type: String,
required: true, required: true,
@@ -12,7 +28,7 @@ const fileConditionSchema = new Schema({
}, },
}) })
const conditionSchema = new Schema({ export const conditionSchema = new Schema<ICondition>({
command: [String], command: [String],
output: [String], output: [String],
error: [String], error: [String],
@@ -24,5 +40,3 @@ conditionSchema.add({
or: [conditionSchema], or: [conditionSchema],
not: conditionSchema, not: conditionSchema,
}) })
export default conditionSchema
+7 -4
View File
@@ -1,8 +1,11 @@
import { Schema } from 'mongoose' 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<IException>({})
exceptionSchema.add(commandResponderSchema) exceptionSchema.add(commandResponderSchema)
export default exceptionSchema
+5 -5
View File
@@ -1,10 +1,10 @@
import { Schema } from 'mongoose' import { Schema } from 'mongoose'
import exceptionSchema from './exception.js' import { exceptionSchema, IException } from './exception.js'
import requirementSchema from './requirement.js' import { requirementSchema, IRequirement } from './requirement.js'
const globalExceptionSchema = new Schema({}) export interface IGlobalException extends IException, IRequirement {}
export const globalExceptionSchema = new Schema<IGlobalException>({})
globalExceptionSchema.add(exceptionSchema) globalExceptionSchema.add(exceptionSchema)
globalExceptionSchema.add(requirementSchema) globalExceptionSchema.add(requirementSchema)
export default globalExceptionSchema
+5 -3
View File
@@ -1,10 +1,12 @@
import { Schema } from 'mongoose' import { Schema } from 'mongoose'
const hintSchema = new Schema({ export interface IHint {
hints: string[]
}
export const hintSchema = new Schema({
hints: { hints: {
type: [String], type: [String],
required: true, required: true,
}, },
}) })
export default hintSchema
+5 -5
View File
@@ -1,10 +1,10 @@
import mongoose from 'mongoose' import mongoose from 'mongoose'
export { default as User } from './user.js' export { User } from './user.js'
export { default as Quest } from './quest.js' export { Quest, IQuest } from './quest.js'
export { default as Session } from './session.js' export { Session } from './session.js'
export { default as Command } from './command.js' export { Command, ICommand } from './command.js'
export { default as UserProfile } from './userProfile.js' export { UserProfile } from './userProfile.js'
export default async function connectDB(uri: string) { export default async function connectDB(uri: string) {
const db = await mongoose.connect(uri) const db = await mongoose.connect(uri)
+14 -6
View File
@@ -1,10 +1,20 @@
import { model, Schema } from 'mongoose' import { model, Schema } from 'mongoose'
import stageSchema from './stage.js' import { IStage, stageSchema } from './stage.js'
import globalExceptionSchema from './globalException.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<IQuest>(
'Quest', 'Quest',
new Schema({ new Schema<IQuest>({
_id: { _id: {
type: String, type: String,
required: true, required: true,
@@ -40,5 +50,3 @@ const Quest = model(
}, },
}) })
) )
export default Quest
+5 -3
View File
@@ -1,6 +1,10 @@
import { Schema } from 'mongoose' import { Schema } from 'mongoose'
const requirementSchema = new Schema({ export interface IRequirement {
requirements: string[]
}
export const requirementSchema = new Schema<IRequirement>({
requirements: [ requirements: [
{ {
type: String, type: String,
@@ -8,5 +12,3 @@ const requirementSchema = new Schema({
}, },
], ],
}) })
export default requirementSchema
+8 -3
View File
@@ -1,6 +1,13 @@
import { Schema } from 'mongoose' 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<IResponse>({
type: { type: {
type: String, type: String,
enum: ['narrative', 'dialogue'], enum: ['narrative', 'dialogue'],
@@ -14,5 +21,3 @@ const responseSchema = new Schema({
speaker: { type: String, default: 'Ada' }, speaker: { type: String, default: 'Ada' },
color: String, color: String,
}) })
export default responseSchema
+32 -7
View File
@@ -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<INode>({
path: String, path: String,
type: String, type: String,
discovered: { discovered: {
@@ -13,7 +20,13 @@ nodeSchema.add({
children: [nodeSchema], children: [nodeSchema],
}) })
const taskSchema = new Schema({ export interface ITask {
id: string
name: string
completed: boolean
}
const taskSchema = new Schema<ITask>({
id: { id: {
type: String, type: String,
required: true, 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<ISession>(
'Session', 'Session',
new Schema( new Schema<ISession>(
{ {
user: { user: {
type: Schema.ObjectId, type: Schema.ObjectId,
@@ -68,5 +95,3 @@ const Session = model(
{ timestamps: true } { timestamps: true }
) )
) )
export default Session
+13 -6
View File
@@ -1,9 +1,18 @@
import { Schema } from 'mongoose' import { Schema } from 'mongoose'
import commandResponderSchema from './commandResponder.js' import {
import stageExceptionSchema from './stageException.js' commandResponderSchema,
import requirementSchema from './requirement.js' 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<IStage>({
id: { id: {
type: String, type: String,
required: true, required: true,
@@ -20,5 +29,3 @@ const stageSchema = new Schema({
stageSchema.add(commandResponderSchema) stageSchema.add(commandResponderSchema)
stageSchema.add(requirementSchema) stageSchema.add(requirementSchema)
export default stageSchema
+6 -4
View File
@@ -1,7 +1,11 @@
import { Schema } from 'mongoose' 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<IStageException>({
catchAll: { catchAll: {
type: Boolean, type: Boolean,
required: true, required: true,
@@ -10,5 +14,3 @@ const stageExceptionSchema = new Schema({
}) })
stageExceptionSchema.add(exceptionSchema) stageExceptionSchema.add(exceptionSchema)
export default stageExceptionSchema
+18 -5
View File
@@ -1,6 +1,12 @@
import { model, Schema } from 'mongoose' import { model, Schema } from 'mongoose'
const socialLoginSchema = new Schema({ export interface ISocialLogin {
provider: string
id: string
displayName?: string
}
const socialLoginSchema = new Schema<ISocialLogin>({
provider: { provider: {
type: String, type: String,
required: true, required: true,
@@ -15,9 +21,18 @@ const socialLoginSchema = new Schema({
}, },
}) })
const User = model( export interface IUser {
username: string
email: string
hashedPassword: string
socialLogins: Map<string, ISocialLogin>
createdAt: Date
updatedAt: Date
}
export const User = model<IUser>(
'User', 'User',
new Schema( new Schema<IUser>(
{ {
username: { username: {
type: String, type: String,
@@ -42,5 +57,3 @@ const User = model(
} }
) )
) )
export default User
+17 -6
View File
@@ -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<IProgress>({
quest: { quest: {
type: String, type: String,
ref: 'Quest', ref: 'Quest',
@@ -25,9 +33,14 @@ const progressSchema = new Schema({
finishedAt: Date, finishedAt: Date,
}) })
const UserProfile = model( export interface IUserProfile {
user: Types.ObjectId
progress: Map<string, IProgress>
}
export const UserProfile = model<IUserProfile>(
'UserProfile', 'UserProfile',
new Schema( new Schema<IUserProfile>(
{ {
user: { user: {
type: Schema.ObjectId, type: Schema.ObjectId,
@@ -47,5 +60,3 @@ const UserProfile = model(
} }
) )
) )
export default UserProfile