feat: add config model and add action for get client config

pull/90/head
moonrailgun 2 years ago
parent 0ddf5c1002
commit b45d782f8e

@ -0,0 +1,69 @@
import {
getModelForClass,
prop,
DocumentType,
modelOptions,
Severity,
ReturnModelType,
} from '@typegoose/typegoose';
import type { Base } from '@typegoose/typegoose/lib/defaultClasses';
import type { Types } from 'mongoose';
@modelOptions({
options: {
allowMixed: Severity.ALLOW,
},
})
export class Config implements Base {
_id: Types.ObjectId;
id: string;
static globalClientConfigName = '__client_config__';
@prop()
name: string;
/**
* config data
*/
@prop()
data: object;
static async getAllClientPersistConfig(
this: ReturnModelType<typeof Config>
): Promise<object> {
const config = await this.findOne({
name: Config.globalClientConfigName,
});
return config?.data ?? {};
}
/**
* set global client persist config from mongodb
*/
static async setClientPersistConfig(
this: ReturnModelType<typeof Config>,
key: string,
value: any
): Promise<void> {
await this.updateOne(
{
name: Config.globalClientConfigName,
},
{
$set: {
[key]: value,
},
}
);
}
}
export type ConfigDocument = DocumentType<Config>;
const model = getModelForClass(Config);
export type ConfigModel = typeof model;
export default model;

@ -1,9 +1,19 @@
import _ from 'lodash';
import { TcService, TcPureContext, config } from 'tailchat-server-sdk';
import {
TcService,
TcPureContext,
config,
TcDbService,
TcContext,
} from 'tailchat-server-sdk';
import type { ConfigDocument, ConfigModel } from '../../models/config';
/**
*
*/
interface ConfigService
extends TcService,
TcDbService<ConfigDocument, ConfigModel> {}
class ConfigService extends TcService {
config = {}; // 自管理的配置项globalConfig是同步过来的
@ -12,7 +22,20 @@ class ConfigService extends TcService {
}
onInit(): void {
this.registerAction('client', this.client);
this.registerLocalDb(require('../../models/config').default);
this.registerAction('client', this.client, {
cache: {
keys: [],
ttl: 24 * 60 * 60, // 1 day
},
});
this.registerAction('setClientConfig', this.setClientConfig, {
params: {
key: 'string',
value: 'any',
},
visibility: 'public',
});
this.registerAction('all', this.all, {
visibility: 'public',
});
@ -48,21 +71,40 @@ class ConfigService extends TcService {
* NOTICE:
*/
async client(ctx: TcPureContext) {
const persistConfig = this.adapter.model.getAllClientPersistConfig();
return {
uploadFileLimit: config.storage.limit,
emailVerification: config.emailVerification,
...persistConfig,
};
}
async all(ctx: TcPureContext) {
/**
* set client config in tailchat network
*
* usually call from admin
*/
async setClientConfig(
ctx: TcContext<{
key: string;
value: any;
}>
) {
const { key, value } = ctx.params;
await this.adapter.model.setClientPersistConfig(key, value);
await this.cleanActionCache('client', []);
}
async all(ctx: TcContext) {
return this.config;
}
async get(ctx: TcPureContext<{ key: string }>) {
async get(ctx: TcContext<{ key: string }>) {
return this.config[ctx.params.key] ?? null;
}
async set(ctx: TcPureContext<{ key: string; value: any }>) {
async set(ctx: TcContext<{ key: string; value: any }>) {
const { key, value } = ctx.params;
_.set(this.config, key, value);
@ -72,7 +114,7 @@ class ConfigService extends TcService {
/**
*
*/
async addToSet(ctx: TcPureContext<{ key: string; value: any }>) {
async addToSet(ctx: TcContext<{ key: string; value: any }>) {
const { key, value } = ctx.params;
const originConfig = _.get(this.config, key) ?? [];

Loading…
Cancel
Save