Signed-off-by: Denis Bykhov <bykhov.denis@gmail.com>
This commit is contained in:
Denis Bykhov
2024-09-11 15:31:26 +07:00
committed by GitHub
parent 74c27d6dd7
commit bf1de1f436
62 changed files with 4187 additions and 632 deletions
+45 -4
View File
@@ -13,14 +13,15 @@
// limitations under the License.
//
import { type MeasureContext } from '@hcengineering/core'
import { DOMAIN_TX, type MeasureContext } from '@hcengineering/core'
import { PlatformError, unknownStatus } from '@hcengineering/platform'
import type {
DbAdapter,
DbConfiguration,
Middleware,
MiddlewareCreator,
PipelineContext
PipelineContext,
TxAdapter
} from '@hcengineering/server-core'
import { BaseMiddleware, createServiceAdaptersManager, DbAdapterManagerImpl } from '@hcengineering/server-core'
@@ -67,11 +68,51 @@ export class DBAdapterMiddleware extends BaseMiddleware implements Middleware {
}
})
const txAdapterName = this.conf.domains[DOMAIN_TX]
const txAdapter = adapters.get(txAdapterName) as TxAdapter
const txAdapterDomains: string[] = []
for (const key in this.conf.domains) {
if (this.conf.domains[key] === txAdapterName) {
txAdapterDomains.push(key)
}
}
await txAdapter.init?.(txAdapterDomains)
const model = await txAdapter.getModel(ctx)
for (const tx of model) {
try {
this.context.hierarchy.tx(tx)
} catch (err: any) {
ctx.warn('failed to apply model transaction, skipping', { tx: JSON.stringify(tx), err })
}
}
await ctx.with('init-adapters', {}, async (ctx) => {
for (const adapter of adapters.values()) {
await adapter.init?.()
for (const [key, adapter] of adapters) {
// already initialized
if (key !== this.conf.domains[DOMAIN_TX] && adapter.init !== undefined) {
let excludeDomains: string[] | undefined
let domains: string[] | undefined
if (this.conf.defaultAdapter === key) {
excludeDomains = []
for (const domain in this.conf.domains) {
if (this.conf.domains[domain] !== key) {
excludeDomains.push(domain)
}
}
} else {
domains = []
for (const domain in this.conf.domains) {
if (this.conf.domains[domain] === key) {
domains.push(domain)
}
}
}
await adapter.init(domains, excludeDomains)
}
}
})
const metrics = this.conf.metrics.newChild('📔 server-storage', {})
const defaultAdapter = adapters.get(this.conf.defaultAdapter)
+29 -2
View File
@@ -13,7 +13,17 @@
// limitations under the License.
//
import { type Doc, type Domain, type MeasureContext, type Ref, type StorageIterator } from '@hcengineering/core'
import {
DocumentQuery,
DocumentUpdate,
FindOptions,
type Doc,
type Domain,
type MeasureContext,
type Ref,
type StorageIterator,
type Iterator
} from '@hcengineering/core'
import { PlatformError, unknownStatus } from '@hcengineering/platform'
import type { Middleware, PipelineContext } from '@hcengineering/server-core'
import { BaseMiddleware } from '@hcengineering/server-core'
@@ -48,8 +58,25 @@ export class LowLevelMiddleware extends BaseMiddleware implements Middleware {
async clean (ctx: MeasureContext, domain: Domain, docs: Ref<Doc>[]): Promise<void> {
await adapterManager.getAdapter(domain, true).clean(ctx, domain, docs)
},
async groupBy (ctx, domain, field) {
async groupBy<T>(ctx: MeasureContext, domain: Domain, field: string): Promise<Set<T>> {
return await adapterManager.getAdapter(domain, false).groupBy(ctx, domain, field)
},
async rawFindAll<T extends Doc>(domain: Domain, query: DocumentQuery<T>, options?: FindOptions<T>): Promise<T[]> {
return await adapterManager.getAdapter(domain, false).rawFindAll(domain, query, options)
},
async rawUpdate<T extends Doc>(
domain: Domain,
query: DocumentQuery<T>,
operations: DocumentUpdate<T>
): Promise<void> {
await adapterManager.getAdapter(domain, true).rawUpdate(domain, query, operations)
},
async traverse<T extends Doc>(
domain: Domain,
query: DocumentQuery<T>,
options?: Pick<FindOptions<T>, 'sort' | 'limit' | 'projection'>
): Promise<Iterator<T>> {
return await adapterManager.getAdapter(domain, false).traverse(domain, query, options)
}
}
return undefined
+11 -2
View File
@@ -73,12 +73,21 @@ export class SpaceSecurityMiddleware extends BaseMiddleware implements Middlewar
core.space.Tx
]
private constructor (
private readonly skipFindCheck: boolean,
context: PipelineContext,
next?: Middleware
) {
super(context, next)
}
static async create (
skipFindCheck: boolean,
ctx: MeasureContext,
context: PipelineContext,
next: Middleware | undefined
): Promise<SpaceSecurityMiddleware> {
return new SpaceSecurityMiddleware(context, next)
return new SpaceSecurityMiddleware(skipFindCheck, context, next)
}
private resyncDomains (): void {
@@ -496,7 +505,7 @@ export class SpaceSecurityMiddleware extends BaseMiddleware implements Middlewar
let clientFilterSpaces: Set<Ref<Space>> | undefined
if (!isSystem(account) && account.role !== AccountRole.DocGuest && domain !== DOMAIN_MODEL) {
if (!this.skipFindCheck && !isSystem(account) && account.role !== AccountRole.DocGuest && domain !== DOMAIN_MODEL) {
if (!isOwner(account, ctx) || !isSpace) {
if (query[field] !== undefined) {
const res = await this.mergeQuery(ctx, account, query[field], domain, isSpace)
+3
View File
@@ -87,6 +87,9 @@ export class TriggersMiddleware extends BaseMiddleware implements Middleware {
const findAll: SessionFindAll = async (ctx, _class, query, options) => {
const _ctx: MeasureContext = (options as ServerFindOptions<Doc>)?.ctx ?? ctx
delete (options as ServerFindOptions<Doc>)?.ctx
if (_ctx.contextData !== undefined) {
_ctx.contextData.isTriggerCtx = true
}
const results = await this.findAll(_ctx, _class, query, options)
return toFindResult(