mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-08 03:20:12 +08:00
feat: separate db models from server
This commit is contained in:
1 parent
784ae29ed0
commit
50bf514805
23 files changed
+90
-31
No files matched your search
@@ -0,0 +1,33 @@
|
||||
import { model, Schema } from 'mongoose'
|
||||
|
||||
const Command = model(
|
||||
'Command',
|
||||
new Schema(
|
||||
{
|
||||
session: {
|
||||
type: Schema.ObjectId,
|
||||
ref: 'Session',
|
||||
required: true,
|
||||
},
|
||||
pwd: {
|
||||
type: String,
|
||||
required: true,
|
||||
},
|
||||
command: {
|
||||
type: String,
|
||||
required: true,
|
||||
},
|
||||
output: {
|
||||
type: String,
|
||||
required: false,
|
||||
},
|
||||
error: {
|
||||
type: String,
|
||||
required: false,
|
||||
},
|
||||
},
|
||||
{ timestamps: true }
|
||||
)
|
||||
)
|
||||
|
||||
export default Command
|
||||
@@ -0,0 +1,22 @@
|
||||
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 async function connectDB(uri) {
|
||||
try {
|
||||
const db = await mongoose.connect(uri, {
|
||||
useNewUrlParser: true,
|
||||
useUnifiedTopology: true,
|
||||
})
|
||||
console.log('Connected to MongoDB')
|
||||
console.log(mongoose.models)
|
||||
return db.connection.getClient()
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
process.exit(1)
|
||||
return null
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"name": "@linux-odyssey/models",
|
||||
"type": "module",
|
||||
"version": "1.0.0",
|
||||
"main": "index.js",
|
||||
"license": "GPL",
|
||||
"exports": "./index.js",
|
||||
"dependencies": {
|
||||
"@linux-odyssey/file-graph": "*",
|
||||
"mongoose": "^7.6.0"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
import { model, Schema } from 'mongoose'
|
||||
|
||||
const responseSchema = new Schema({
|
||||
type: {
|
||||
type: String,
|
||||
required: true,
|
||||
},
|
||||
content: [String],
|
||||
speaker: String,
|
||||
color: String,
|
||||
})
|
||||
|
||||
const stageSchema = new Schema({
|
||||
id: {
|
||||
type: String,
|
||||
required: true,
|
||||
},
|
||||
task: {
|
||||
type: String,
|
||||
},
|
||||
requirements: [
|
||||
{
|
||||
type: String,
|
||||
required: true,
|
||||
},
|
||||
],
|
||||
repeatable: {
|
||||
type: Boolean,
|
||||
required: true,
|
||||
default: false,
|
||||
},
|
||||
condition: {
|
||||
command: [String],
|
||||
output: [String],
|
||||
error: [String],
|
||||
pwd: [String],
|
||||
},
|
||||
responses: {
|
||||
type: [responseSchema],
|
||||
required: true,
|
||||
},
|
||||
hints: {
|
||||
type: [String],
|
||||
required: true,
|
||||
},
|
||||
})
|
||||
|
||||
const Quest = model(
|
||||
'Quest',
|
||||
new Schema({
|
||||
_id: String,
|
||||
title: String,
|
||||
order: Number,
|
||||
instruction: String,
|
||||
tasks: {
|
||||
id: String,
|
||||
content: String,
|
||||
display: {
|
||||
type: Boolean,
|
||||
default: false,
|
||||
},
|
||||
completed: {
|
||||
type: Boolean,
|
||||
default: false,
|
||||
},
|
||||
},
|
||||
stages: [stageSchema],
|
||||
})
|
||||
)
|
||||
|
||||
export default Quest
|
||||
@@ -0,0 +1,71 @@
|
||||
import { model, Schema } from 'mongoose'
|
||||
import { FileNode } from '@linux-odyssey/file-graph'
|
||||
|
||||
const nodeSchema = new Schema({
|
||||
path: String,
|
||||
type: String,
|
||||
discovered: {
|
||||
type: Boolean,
|
||||
default: false,
|
||||
},
|
||||
})
|
||||
|
||||
nodeSchema.add({
|
||||
children: [nodeSchema],
|
||||
})
|
||||
|
||||
const taskSchema = new Schema({
|
||||
id: {
|
||||
type: String,
|
||||
required: true,
|
||||
},
|
||||
name: {
|
||||
type: String,
|
||||
required: true,
|
||||
},
|
||||
completed: {
|
||||
type: Boolean,
|
||||
default: false,
|
||||
},
|
||||
})
|
||||
|
||||
const Session = model(
|
||||
'Session',
|
||||
new Schema(
|
||||
{
|
||||
user: {
|
||||
type: Schema.ObjectId,
|
||||
ref: 'User',
|
||||
},
|
||||
quest: {
|
||||
type: String,
|
||||
ref: 'Quest',
|
||||
},
|
||||
containerId: String,
|
||||
status: {
|
||||
type: String,
|
||||
enum: ['active', 'finished', 'inactive'],
|
||||
required: true,
|
||||
default: 'active',
|
||||
},
|
||||
finishedAt: Date,
|
||||
lastActivityAt: {
|
||||
type: Date,
|
||||
default: Date.now,
|
||||
required: true,
|
||||
},
|
||||
tasks: [taskSchema],
|
||||
hints: [String],
|
||||
graph: {
|
||||
type: nodeSchema,
|
||||
default: new FileNode({
|
||||
path: '/',
|
||||
type: 'folder',
|
||||
}),
|
||||
},
|
||||
},
|
||||
{ timestamps: true }
|
||||
)
|
||||
)
|
||||
|
||||
export default Session
|
||||
@@ -0,0 +1,25 @@
|
||||
import { model } from 'mongoose'
|
||||
|
||||
const User = model('User', {
|
||||
username: {
|
||||
type: String,
|
||||
unique: true,
|
||||
required: true,
|
||||
},
|
||||
email: {
|
||||
type: String,
|
||||
unique: true,
|
||||
required: true,
|
||||
},
|
||||
hashedPassword: String,
|
||||
google: {
|
||||
id: String,
|
||||
displayName: String,
|
||||
},
|
||||
github: {
|
||||
id: String,
|
||||
displayName: String,
|
||||
},
|
||||
})
|
||||
|
||||
export default User
|
||||
Reference in new issue
Block a user