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",
"scripts": {
"build": "tsc",
"watch": "tsc -w"
"dev": "tsc -w"
},
"dependencies": {
"@types/mongoose": "^5.11.97",
"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',
new Schema(
new Schema<ICommand>(
{
session: {
type: Schema.ObjectId,
@@ -33,5 +44,3 @@ const Command = model(
{ timestamps: true }
)
)
export default Command
+10 -6
View File
@@ -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<ICommandResponder>({
condition: {
type: conditionSchema,
required: false,
@@ -15,5 +21,3 @@ const commandResponderSchema = new Schema({
})
commandResponderSchema.add(hintSchema)
export default commandResponderSchema
+18 -4
View File
@@ -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<IFileCondition>({
path: {
type: String,
required: true,
@@ -12,7 +28,7 @@ const fileConditionSchema = new Schema({
},
})
const conditionSchema = new Schema({
export const conditionSchema = new Schema<ICondition>({
command: [String],
output: [String],
error: [String],
@@ -24,5 +40,3 @@ conditionSchema.add({
or: [conditionSchema],
not: conditionSchema,
})
export default conditionSchema
+7 -4
View File
@@ -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<IException>({})
exceptionSchema.add(commandResponderSchema)
export default exceptionSchema
+5 -5
View File
@@ -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<IGlobalException>({})
globalExceptionSchema.add(exceptionSchema)
globalExceptionSchema.add(requirementSchema)
export default globalExceptionSchema
+5 -3
View File
@@ -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
+5 -5
View File
@@ -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)
+14 -6
View File
@@ -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<IQuest>(
'Quest',
new Schema({
new Schema<IQuest>({
_id: {
type: String,
required: true,
@@ -40,5 +50,3 @@ const Quest = model(
},
})
)
export default Quest
+5 -3
View File
@@ -1,6 +1,10 @@
import { Schema } from 'mongoose'
const requirementSchema = new Schema({
export interface IRequirement {
requirements: string[]
}
export const requirementSchema = new Schema<IRequirement>({
requirements: [
{
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'
const responseSchema = new Schema({
export interface IResponse {
type: 'narrative' | 'dialogue'
content: string[]
speaker?: string
color?: string
}
export const responseSchema = new Schema<IResponse>({
type: {
type: String,
enum: ['narrative', 'dialogue'],
@@ -14,5 +21,3 @@ const responseSchema = new Schema({
speaker: { type: String, default: 'Ada' },
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,
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<ITask>({
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<ISession>(
'Session',
new Schema(
new Schema<ISession>(
{
user: {
type: Schema.ObjectId,
@@ -68,5 +95,3 @@ const Session = model(
{ timestamps: true }
)
)
export default Session
+13 -6
View File
@@ -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<IStage>({
id: {
type: String,
required: true,
@@ -20,5 +29,3 @@ const stageSchema = new Schema({
stageSchema.add(commandResponderSchema)
stageSchema.add(requirementSchema)
export default stageSchema
+6 -4
View File
@@ -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<IStageException>({
catchAll: {
type: Boolean,
required: true,
@@ -10,5 +14,3 @@ const stageExceptionSchema = new Schema({
})
stageExceptionSchema.add(exceptionSchema)
export default stageExceptionSchema
+18 -5
View File
@@ -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<ISocialLogin>({
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<string, ISocialLogin>
createdAt: Date
updatedAt: Date
}
export const User = model<IUser>(
'User',
new Schema(
new Schema<IUser>(
{
username: {
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: {
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<string, IProgress>
}
export const UserProfile = model<IUserProfile>(
'UserProfile',
new Schema(
new Schema<IUserProfile>(
{
user: {
type: Schema.ObjectId,
@@ -47,5 +60,3 @@ const UserProfile = model(
}
)
)
export default UserProfile