mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
core: use serviceInstance for system model
This commit is contained in:
30 files changed
+143
-104
No files matched your search
@@ -4,7 +4,7 @@ import bus from '../service/bus';
|
||||
import db from '../service/db';
|
||||
import { ArgMethod } from '../utils';
|
||||
import { PRIV } from './builtin';
|
||||
import * as system from './system';
|
||||
import system from './system';
|
||||
import user from './user';
|
||||
|
||||
class MessageModel {
|
||||
|
||||
@@ -30,7 +30,7 @@ import DomainModel from './domain';
|
||||
import RecordModel from './record';
|
||||
import SolutionModel from './solution';
|
||||
import storage from './storage';
|
||||
import * as SystemModel from './system';
|
||||
import SystemModel from './system';
|
||||
|
||||
export interface ProblemDoc extends Document { }
|
||||
export type Field = keyof ProblemDoc;
|
||||
|
||||
@@ -14,7 +14,7 @@ import { ArgMethod, buildProjection, Time } from '../utils';
|
||||
import { STATUS } from './builtin';
|
||||
import DomainModel from './domain';
|
||||
import problem from './problem';
|
||||
import * as SystemModel from './system';
|
||||
import SystemModel from './system';
|
||||
import task from './task';
|
||||
|
||||
export default class RecordModel {
|
||||
|
||||
@@ -9,7 +9,7 @@ import mime from '../lib/mime';
|
||||
import db from '../service/db';
|
||||
import storage from '../service/storage';
|
||||
import ScheduleModel from './schedule';
|
||||
import * as system from './system';
|
||||
import system from './system';
|
||||
|
||||
export class StorageModel {
|
||||
static coll = db.collection('storage');
|
||||
|
||||
@@ -1,81 +1,74 @@
|
||||
import { Context, Service } from '../context';
|
||||
import { SystemKeys } from '../interface';
|
||||
import bus from '../service/bus';
|
||||
import db from '../service/db';
|
||||
import { NumberKeys } from '../typeutils';
|
||||
import { serviceInstance } from '../utils';
|
||||
import { SYSTEM_SETTINGS } from './setting';
|
||||
|
||||
const coll = db.collection('system');
|
||||
const cache: Record<string, any> = Object.create(null);
|
||||
class SystemModelService extends Service {
|
||||
static readonly inject = ['db'];
|
||||
static readonly name = 'model:system';
|
||||
|
||||
export function get<K extends keyof SystemKeys>(key: K): SystemKeys[K];
|
||||
export function get(key: string): any;
|
||||
export function get(key: string): any {
|
||||
return cache[key];
|
||||
}
|
||||
coll = this.ctx.db.collection('system');
|
||||
cache: Record<string, any> = Object.create(null);
|
||||
|
||||
export function getMany<
|
||||
A extends keyof SystemKeys, B extends keyof SystemKeys,
|
||||
>(keys: [A, B]): [SystemKeys[A], SystemKeys[B]];
|
||||
export function getMany<
|
||||
A extends keyof SystemKeys, B extends keyof SystemKeys, C extends keyof SystemKeys,
|
||||
>(keys: [A, B, C]): [SystemKeys[A], SystemKeys[B], SystemKeys[C]];
|
||||
export function getMany<
|
||||
A extends keyof SystemKeys, B extends keyof SystemKeys, C extends keyof SystemKeys,
|
||||
D extends keyof SystemKeys,
|
||||
>(keys: [A, B, C, D]): [SystemKeys[A], SystemKeys[B], SystemKeys[C], SystemKeys[D]];
|
||||
export function getMany<
|
||||
A extends keyof SystemKeys, B extends keyof SystemKeys, C extends keyof SystemKeys,
|
||||
D extends keyof SystemKeys, E extends keyof SystemKeys,
|
||||
>(keys: [A, B, C, D, E]): [SystemKeys[A], SystemKeys[B], SystemKeys[C], SystemKeys[D], SystemKeys[E]];
|
||||
export function getMany<
|
||||
A extends keyof SystemKeys, B extends keyof SystemKeys, C extends keyof SystemKeys,
|
||||
D extends keyof SystemKeys, E extends keyof SystemKeys, F extends keyof SystemKeys,
|
||||
>(keys: [A, B, C, D, E, F]): [SystemKeys[A], SystemKeys[B], SystemKeys[C], SystemKeys[D], SystemKeys[E], SystemKeys[F]];
|
||||
export function getMany(keys: (keyof SystemKeys)[]): any[];
|
||||
export function getMany(keys: string[]): any[] {
|
||||
return keys.map((key) => cache[key]);
|
||||
}
|
||||
|
||||
export async function set<K extends keyof SystemKeys>(_id: K, value: SystemKeys[K], broadcast?: boolean): Promise<SystemKeys[K]>;
|
||||
export async function set<K>(_id: string, value: K, broadcast?: boolean): Promise<K>;
|
||||
export async function set(_id: string, value: any, broadcast = true) {
|
||||
if (broadcast) bus.broadcast('system/setting', { [_id]: value });
|
||||
const res = await coll.findOneAndUpdate(
|
||||
{ _id },
|
||||
{ $set: { value } },
|
||||
{ upsert: true, returnDocument: 'after' },
|
||||
);
|
||||
cache[_id] = res.value;
|
||||
return res.value;
|
||||
}
|
||||
|
||||
export async function inc<K extends NumberKeys<SystemKeys>>(_id: K) {
|
||||
const res = await coll.findOneAndUpdate(
|
||||
{ _id },
|
||||
{ $inc: { value: 1 } as any },
|
||||
{ upsert: true, returnDocument: 'after' },
|
||||
);
|
||||
cache[_id] = res.value;
|
||||
return res.value;
|
||||
}
|
||||
|
||||
export async function runConfig() {
|
||||
for (const setting of SYSTEM_SETTINGS) {
|
||||
if (setting.value) cache[setting.key] = setting.value;
|
||||
constructor(ctx: Context) {
|
||||
super(ctx, 'model:system');
|
||||
}
|
||||
|
||||
get<K extends keyof SystemKeys>(key: K): SystemKeys[K];
|
||||
get(key: string): any;
|
||||
get(key: string): any {
|
||||
return this.cache[key];
|
||||
}
|
||||
|
||||
getMany<
|
||||
A extends keyof SystemKeys, B extends keyof SystemKeys,
|
||||
>(keys: [A, B]): [SystemKeys[A], SystemKeys[B]];
|
||||
getMany<
|
||||
A extends keyof SystemKeys, B extends keyof SystemKeys, C extends keyof SystemKeys,
|
||||
>(keys: [A, B, C]): [SystemKeys[A], SystemKeys[B], SystemKeys[C]];
|
||||
getMany<
|
||||
A extends keyof SystemKeys, B extends keyof SystemKeys, C extends keyof SystemKeys,
|
||||
D extends keyof SystemKeys,
|
||||
>(keys: [A, B, C, D]): [SystemKeys[A], SystemKeys[B], SystemKeys[C], SystemKeys[D]];
|
||||
getMany<
|
||||
A extends keyof SystemKeys, B extends keyof SystemKeys, C extends keyof SystemKeys,
|
||||
D extends keyof SystemKeys, E extends keyof SystemKeys,
|
||||
>(keys: [A, B, C, D, E]): [SystemKeys[A], SystemKeys[B], SystemKeys[C], SystemKeys[D], SystemKeys[E]];
|
||||
getMany<
|
||||
A extends keyof SystemKeys, B extends keyof SystemKeys, C extends keyof SystemKeys,
|
||||
D extends keyof SystemKeys, E extends keyof SystemKeys, F extends keyof SystemKeys,
|
||||
>(keys: [A, B, C, D, E, F]): [SystemKeys[A], SystemKeys[B], SystemKeys[C], SystemKeys[D], SystemKeys[E], SystemKeys[F]];
|
||||
getMany(keys: (keyof SystemKeys)[]): any[];
|
||||
getMany(keys: string[]): any[] {
|
||||
return keys.map((key) => this.cache[key]);
|
||||
}
|
||||
|
||||
async set<K extends keyof SystemKeys>(_id: K, value: SystemKeys[K], broadcast?: boolean): Promise<SystemKeys[K]>;
|
||||
async set<K>(_id: string, value: K, broadcast?: boolean): Promise<K>;
|
||||
async set(_id: string, value: any, broadcast = true) {
|
||||
if (broadcast) this.ctx.broadcast('system/setting', { [_id]: value });
|
||||
const res = await this.coll.findOneAndUpdate(
|
||||
{ _id },
|
||||
{ $set: { value } },
|
||||
{ upsert: true, returnDocument: 'after' },
|
||||
);
|
||||
this.cache[_id] = res.value;
|
||||
return res.value;
|
||||
}
|
||||
|
||||
async [Service.init]() {
|
||||
for (const setting of SYSTEM_SETTINGS) {
|
||||
if (setting.value) this.cache[setting.key] = setting.value;
|
||||
}
|
||||
const config = await this.coll.find().toArray();
|
||||
for (const i of config) this.cache[i._id] = i.value;
|
||||
this.ctx.emit('database/config');
|
||||
return this.ctx.on('system/setting', (args) => {
|
||||
for (const key in args) this.cache[key] = args[key];
|
||||
});
|
||||
}
|
||||
const config = await coll.find().toArray();
|
||||
for (const i of config) cache[i._id] = i.value;
|
||||
await bus.emit('database/config');
|
||||
}
|
||||
|
||||
bus.on('system/setting', (args) => {
|
||||
for (const key in args) cache[key] = args[key];
|
||||
});
|
||||
|
||||
global.Hydro.model.system = {
|
||||
runConfig,
|
||||
get,
|
||||
getMany,
|
||||
inc,
|
||||
set,
|
||||
};
|
||||
const SystemModel = serviceInstance(SystemModelService);
|
||||
export default SystemModel;
|
||||
global.Hydro.model.system = SystemModel;
|
||||
@@ -16,7 +16,7 @@ import { ArgMethod, buildProjection, randomstring, sleep } from '../utils';
|
||||
import { PERM, PRIV } from './builtin';
|
||||
import domain from './domain';
|
||||
import * as setting from './setting';
|
||||
import * as system from './system';
|
||||
import system from './system';
|
||||
import token from './token';
|
||||
|
||||
export const coll: Collection<Udoc> = db.collection('user');
|
||||
|
||||
Reference in new issue
Block a user