Update mongodb indexes

This commit is contained in:
LordMathis 2020-01-07 22:39:04 +01:00
parent c0d14fe247
commit 8c0415c3f7
No known key found for this signature in database
GPG Key ID: 575849FD91CE470C
2 changed files with 14 additions and 7 deletions

View File

@ -9,6 +9,7 @@ import { ServerRenderer } from './utils/serverRender'
import { Scanner } from './utils/scanner' import { Scanner } from './utils/scanner'
import { FileStorage } from './utils/storage/file' import { FileStorage } from './utils/storage/file'
import { MongoStorage } from './utils/storage/mongo' import { MongoStorage } from './utils/storage/mongo'
import { Api } from './utils/api'
const port = process.env.PORT || 3000 const port = process.env.PORT || 3000
const app = express() const app = express()
@ -53,6 +54,9 @@ if (config.storage === 'file') {
storage = new MongoStorage(config) storage = new MongoStorage(config)
} }
const postApi = new Api(storage)
app.get('/api/v1/posts', postApi.getPosts.bind(postApi))
const scanner = new Scanner(config, storage) const scanner = new Scanner(config, storage)
const serverRenderer = new ServerRenderer(head, config, storage) const serverRenderer = new ServerRenderer(head, config, storage)

View File

@ -12,14 +12,20 @@ export class MongoStorage {
link: String, link: String,
body: String body: String
}) })
PostSchema.index({ filename: 1 }) PostSchema.index({
body: 'text',
title: 'text'
})
PostSchema.index({
filename: 'hashed'
})
this.Post = mongoose.model('Post', PostSchema) this.Post = mongoose.model('Post', PostSchema)
const OtherSchema = new Schema({ const OtherSchema = new Schema({
filename: String, filename: String,
body: String body: String
}) })
OtherSchema.index({ filename: 1 }) OtherSchema.index({ filename: 'hashed' })
this.Other = mongoose.model('Other', OtherSchema) this.Other = mongoose.model('Other', OtherSchema)
this.options = { this.options = {
@ -76,10 +82,7 @@ export class MongoStorage {
this._getAllPosts() this._getAllPosts()
]).then((res) => { ]).then((res) => {
data.other.about = res[0].data data.other.about = res[0].data
data.posts = res[1].map((post) => { data.posts = res[1]
delete post.body
return post
})
return data return data
}) })
} else if (reqPath.startsWith('/post')) { } else if (reqPath.startsWith('/post')) {
@ -102,7 +105,7 @@ export class MongoStorage {
_getAllPosts () { _getAllPosts () {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this.Post.find({}, (err, data) => { this.Post.find({}, { body: false }, (err, data) => {
err ? reject(err) : resolve(data) err ? reject(err) : resolve(data)
}) })
}) })