feat(serverless): add getMaster
This commit is contained in:
@@ -11,6 +11,7 @@ import { InjectModel } from 'nestjs-typegoose'
|
||||
import path from 'path'
|
||||
import { nextTick } from 'process'
|
||||
import { DATA_DIR, NODE_REQUIRE_PATH } from '~/constants/path.constant'
|
||||
import { DatabaseService } from '~/processors/database/database.service'
|
||||
import { AssetService } from '~/processors/helper/helper.asset.service'
|
||||
import { HttpService } from '~/processors/helper/helper.http.service'
|
||||
import { UniqueArray } from '~/ts-hepler/unique'
|
||||
@@ -31,6 +32,7 @@ export class ServerlessService {
|
||||
private readonly snippetModel: MongooseModel<SnippetModel>,
|
||||
private readonly assetService: AssetService,
|
||||
private readonly httpService: HttpService,
|
||||
private readonly databaseService: DatabaseService,
|
||||
) {
|
||||
nextTick(() => {
|
||||
// Add /includes/plugin to the path, also note that we need to support
|
||||
@@ -82,6 +84,31 @@ export class ServerlessService {
|
||||
document,
|
||||
name: model.name,
|
||||
reference: model.reference,
|
||||
getMaster: async () => {
|
||||
const collection = this.databaseService.db.collection('users')
|
||||
const cur = collection.aggregate([
|
||||
{
|
||||
$project: {
|
||||
id: 1,
|
||||
_id: 1,
|
||||
username: 1,
|
||||
name: 1,
|
||||
introduce: 1,
|
||||
avatar: 1,
|
||||
mail: 1,
|
||||
url: 1,
|
||||
lastLoginTime: 1,
|
||||
lastLoginIp: 1,
|
||||
socialIds: 1,
|
||||
},
|
||||
},
|
||||
])
|
||||
|
||||
return await cur.next().then((doc) => {
|
||||
cur.close()
|
||||
return doc
|
||||
})
|
||||
},
|
||||
|
||||
writeAsset: async (
|
||||
path: string,
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { Injectable } from '@nestjs/common'
|
||||
import { ReturnModelType } from '@typegoose/typegoose'
|
||||
import { InjectModel } from 'nestjs-typegoose'
|
||||
import { Inject, Injectable } from '@nestjs/common'
|
||||
import { mongoose, ReturnModelType } from '@typegoose/typegoose'
|
||||
import { getConnectionToken, InjectModel } from 'nestjs-typegoose'
|
||||
import { NoteModel } from '~/modules/note/note.model'
|
||||
import { PageModel } from '~/modules/page/page.model'
|
||||
import { PostModel } from '~/modules/post/post.model'
|
||||
@@ -14,6 +14,7 @@ export class DatabaseService {
|
||||
private readonly noteModel: ReturnModelType<typeof NoteModel>,
|
||||
@InjectModel(PageModel)
|
||||
private readonly pageModel: ReturnModelType<typeof PageModel>,
|
||||
@Inject(getConnectionToken()) private connection: mongoose.Connection,
|
||||
) {}
|
||||
|
||||
// @ts-ignore
|
||||
@@ -55,4 +56,8 @@ export class DatabaseService {
|
||||
type: ['Post', 'Note', 'Page'][index],
|
||||
}
|
||||
}
|
||||
|
||||
public get db() {
|
||||
return this.connection.db
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,6 +26,7 @@ import Paginate from 'mongoose-paginate-v2'
|
||||
createdAt: 'created',
|
||||
updatedAt: null,
|
||||
},
|
||||
versionKey: false,
|
||||
},
|
||||
})
|
||||
export class BaseModel {
|
||||
|
||||
@@ -6,6 +6,7 @@ import { createMockedContextResponse } from '~/modules/serverless/mock-response.
|
||||
import { ServerlessService } from '~/modules/serverless/serverless.service'
|
||||
import { SnippetModel, SnippetType } from '~/modules/snippet/snippet.model'
|
||||
import { CacheService } from '~/processors/cache/cache.service'
|
||||
import { DatabaseService } from '~/processors/database/database.service'
|
||||
import { AssetService } from '~/processors/helper/helper.asset.service'
|
||||
import { HttpService } from '~/processors/helper/helper.http.service'
|
||||
|
||||
@@ -23,6 +24,7 @@ describe('test serverless function service', () => {
|
||||
provide: CacheService,
|
||||
useValue: {},
|
||||
},
|
||||
{ provide: DatabaseService, useValue: {} },
|
||||
|
||||
{
|
||||
provide: getModelToken('SnippetModel'),
|
||||
|
||||
@@ -8,6 +8,7 @@ import { ServerlessService } from '~/modules/serverless/serverless.service'
|
||||
import { SnippetController } from '~/modules/snippet/snippet.controller'
|
||||
import { SnippetModel, SnippetType } from '~/modules/snippet/snippet.model'
|
||||
import { SnippetService } from '~/modules/snippet/snippet.service'
|
||||
import { DatabaseService } from '~/processors/database/database.service'
|
||||
|
||||
describe('test /snippets', () => {
|
||||
let app: NestFastifyApplication
|
||||
@@ -34,7 +35,7 @@ describe('test /snippets', () => {
|
||||
controllers: [SnippetController],
|
||||
providers: [
|
||||
SnippetService,
|
||||
|
||||
{ provide: DatabaseService, useValue: {} },
|
||||
{
|
||||
provide: ServerlessService,
|
||||
useValue: {
|
||||
|
||||
@@ -7,6 +7,7 @@ import { ServerlessService } from '~/modules/serverless/serverless.service'
|
||||
import { SnippetModel, SnippetType } from '~/modules/snippet/snippet.model'
|
||||
import { SnippetService } from '~/modules/snippet/snippet.service'
|
||||
import { CacheService } from '~/processors/cache/cache.service'
|
||||
import { DatabaseService } from '~/processors/database/database.service'
|
||||
|
||||
describe('test Snippet Service', () => {
|
||||
let service: SnippetService
|
||||
@@ -16,7 +17,7 @@ describe('test Snippet Service', () => {
|
||||
const moduleRef = Test.createTestingModule({
|
||||
providers: [
|
||||
SnippetService,
|
||||
|
||||
{ provide: DatabaseService, useValue: {} },
|
||||
{ provide: CacheService, useValue: {} },
|
||||
{ provide: ServerlessService, useValue: {} },
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user