fix: app config shared

This commit is contained in:
Innei
2021-10-02 13:11:40 +08:00
parent 6faf5a8cec
commit b7db64e3ae
17 changed files with 909 additions and 693 deletions

41
patch/bootstrap.js vendored Normal file
View File

@@ -0,0 +1,41 @@
// import { getModelForClass, mongoose } from '@typegoose/typegoose'
// import { config } from 'dotenv'
// import { ConnectionBase } from 'mongoose'
// import * as APP from '../src/app.config.mjs'
// import { CategoryModel } from '../src/modules/category/category.model'
// import { NoteModel } from '../src/modules/note/note.model'
// import { PostModel } from '../src/modules/post/post.model'
// const env = config().parsed || {}
// const url = APP.MONGO_DB.uri
// const opt = {
// useCreateIndex: true,
// useFindAndModify: false,
// useNewUrlParser: true,
// useUnifiedTopology: true,
// autoIndex: true,
// }
// mongoose.connect(url, opt)
// const post = getModelForClass(PostModel)
// const note = getModelForClass(NoteModel)
// const category = getModelForClass(CategoryModel)
// const Config = {
// env,
// db: (mongoose.connection as any).client.db(
// APP.MONGO_DB.collectionName,
// ) as ConnectionBase,
// models: {
// post,
// note,
// category,
// },
// }
// async function bootstrap(cb: (config: typeof Config) => any) {
// await cb.call(this, Config)
// mongoose.disconnect()
// process.exit()
// }
// export { bootstrap as patch }

View File

@@ -1,41 +0,0 @@
import { getModelForClass, mongoose } from '@typegoose/typegoose'
import { config } from 'dotenv'
import { ConnectionBase } from 'mongoose'
import * as APP from '../src/app.config'
import { CategoryModel } from '../src/modules/category/category.model'
import { NoteModel } from '../src/modules/note/note.model'
import { PostModel } from '../src/modules/post/post.model'
const env = config().parsed || {}
const url = APP.MONGO_DB.uri
const opt = {
useCreateIndex: true,
useFindAndModify: false,
useNewUrlParser: true,
useUnifiedTopology: true,
autoIndex: true,
}
mongoose.connect(url, opt)
const post = getModelForClass(PostModel)
const note = getModelForClass(NoteModel)
const category = getModelForClass(CategoryModel)
const Config = {
env,
db: (mongoose.connection as any).client.db(
APP.MONGO_DB.collectionName,
) as ConnectionBase,
models: {
post,
note,
category,
},
}
async function bootstrap(cb: (config: typeof Config) => any) {
await cb.call(this, Config)
mongoose.disconnect()
process.exit()
}
export { bootstrap as patch }

11
patch/global.d.ts vendored
View File

@@ -1,11 +0,0 @@
import { ModelType } from '@typegoose/typegoose/lib/types'
import { Document, PaginateModel } from 'mongoose'
/// <reference types="../global" />
declare global {
export type KV<T = any> = Record<string, T>
// @ts-ignore
export type MongooseModel<T> = ModelType<T> & PaginateModel<T & Document>
}
export {}

View File

@@ -1,34 +0,0 @@
{
"compilerOptions": {
"module": "CommonJS",
"declaration": false,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"target": "es2017",
"sourceMap": false,
"outDir": "./dist",
"baseUrl": ".",
"noImplicitAny": false,
"incremental": true,
"resolveJsonModule": true,
"skipLibCheck": true,
"paths": {
"~": [
"../src"
],
"~/*": [
"../src/*"
]
},
},
"include": [
"*.ts",
"../src"
],
"exclude": [
"dist"
]
}

View File

@@ -1,17 +1,17 @@
// patch for version lower than v2.0.0-alpha.1
// // patch for version lower than v2.0.0-alpha.1
import { patch } from './bootstrap'
// import { patch } from './bootstrap'
patch(async ({ models: { note, post, category } }) => {
await Promise.all([
[note, post].map((model) => {
return model.updateMany(
{},
{
$unset: ['options'],
},
)
}),
category.updateMany({}, { $unset: { count: '' } }),
])
})
// patch(async ({ models: { note, post, category } }) => {
// await Promise.all([
// [note, post].map((model) => {
// return model.updateMany(
// {},
// {
// $unset: ['options'],
// },
// )
// }),
// category.updateMany({}, { $unset: { count: '' } }),
// ])
// })