2021-08-05 06:47:54 +00:00
|
|
|
//
|
|
|
|
// Copyright © 2020, 2021 Anticrm Platform Contributors.
|
|
|
|
// Copyright © 2021 Hardcore Engineering Inc.
|
2021-08-05 09:30:14 +00:00
|
|
|
//
|
2021-08-05 06:47:54 +00:00
|
|
|
// Licensed under the Eclipse Public License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License. You may
|
|
|
|
// obtain a copy of the License at https://www.eclipse.org/legal/epl-2.0
|
2021-08-05 09:30:14 +00:00
|
|
|
//
|
2021-08-05 06:47:54 +00:00
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
2021-08-05 09:30:14 +00:00
|
|
|
//
|
2021-08-05 06:47:54 +00:00
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
|
2021-08-26 12:16:45 +00:00
|
|
|
import type { Doc, Tx, TxCreateDoc, Ref, Class, ServerStorage, DocumentQuery, FindOptions, FindResult, Storage, Account, Domain, TxCUD } from '@anticrm/core'
|
|
|
|
import core, { Hierarchy, TxFactory, DOMAIN_TX } from '@anticrm/core'
|
2021-08-05 09:30:14 +00:00
|
|
|
import type { Resource, Plugin } from '@anticrm/platform'
|
2021-08-18 17:43:17 +00:00
|
|
|
import { getResource, plugin } from '@anticrm/platform'
|
|
|
|
|
2021-08-05 06:47:54 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
2021-08-05 13:19:27 +00:00
|
|
|
export type TriggerFunc = (tx: Tx, txFactory: TxFactory) => Promise<Tx[]>
|
2021-08-05 06:47:54 +00:00
|
|
|
|
2021-08-05 09:30:14 +00:00
|
|
|
/**
|
2021-08-18 17:43:17 +00:00
|
|
|
* @public
|
|
|
|
*/
|
2021-08-05 13:19:27 +00:00
|
|
|
export interface Trigger extends Doc {
|
|
|
|
trigger: Resource<TriggerFunc>
|
2021-08-05 09:30:14 +00:00
|
|
|
}
|
|
|
|
|
2021-08-18 17:43:17 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export class Triggers {
|
|
|
|
private readonly triggers: TriggerFunc[] = []
|
|
|
|
|
|
|
|
async tx (tx: Tx): Promise<void> {
|
|
|
|
if (tx._class === core.class.TxCreateDoc) {
|
|
|
|
const createTx = tx as TxCreateDoc<Doc>
|
|
|
|
if (createTx.objectClass === serverCore.class.Trigger) {
|
|
|
|
const trigger = (createTx as TxCreateDoc<Trigger>).attributes.trigger
|
|
|
|
const func = await getResource(trigger)
|
|
|
|
this.triggers.push(func)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-26 08:26:06 +00:00
|
|
|
async apply (account: Ref<Account>, tx: Tx): Promise<Tx[]> {
|
|
|
|
const derived = this.triggers.map(trigger => trigger(tx, new TxFactory(account)))
|
2021-08-18 17:43:17 +00:00
|
|
|
const result = await Promise.all(derived)
|
|
|
|
return result.flatMap(x => x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-25 17:55:26 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export interface DbAdapter extends Storage {
|
2021-08-26 12:16:45 +00:00
|
|
|
/**
|
|
|
|
* Method called after hierarchy is ready to use.
|
|
|
|
*/
|
|
|
|
init: (model: Tx[]) => Promise<void>
|
2021-08-25 17:55:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
2021-08-26 12:16:45 +00:00
|
|
|
export interface TxAdapter extends DbAdapter {
|
|
|
|
getModel: () => Promise<Tx[]>
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export type DbAdapterFactory = (hierarchy: Hierarchy, url: string, db: string) => Promise<DbAdapter>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export interface DbAdapterConfiguration {
|
|
|
|
factory: DbAdapterFactory
|
|
|
|
url: string
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export interface DbConfiguration {
|
|
|
|
adapters: Record<string, DbAdapterConfiguration>
|
|
|
|
domains: Record<string, string>
|
|
|
|
defaultAdapter: string
|
|
|
|
workspace: string
|
|
|
|
}
|
2021-08-25 17:55:26 +00:00
|
|
|
|
|
|
|
class TServerStorage implements ServerStorage {
|
|
|
|
constructor (
|
2021-08-26 12:16:45 +00:00
|
|
|
private readonly domains: Record<string, string>,
|
|
|
|
private readonly defaultAdapter: string,
|
|
|
|
private readonly adapters: Map<string, DbAdapter>,
|
2021-08-25 17:55:26 +00:00
|
|
|
private readonly hierarchy: Hierarchy,
|
2021-08-26 12:16:45 +00:00
|
|
|
private readonly triggers: Triggers
|
2021-08-25 17:55:26 +00:00
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
2021-08-26 12:16:45 +00:00
|
|
|
private getAdapter (domain: Domain): DbAdapter {
|
|
|
|
const name = this.domains[domain] ?? this.defaultAdapter
|
|
|
|
const adapter = this.adapters.get(name)
|
|
|
|
if (adapter === undefined) {
|
|
|
|
throw new Error('adapter not provided: ' + name)
|
|
|
|
}
|
|
|
|
return adapter
|
|
|
|
}
|
|
|
|
|
|
|
|
private routeTx (tx: Tx): Promise<void> {
|
|
|
|
if (this.hierarchy.isDerived(tx._class, core.class.TxCUD)) {
|
|
|
|
const txCUD = tx as TxCUD<Doc>
|
|
|
|
const domain = this.hierarchy.getDomain(txCUD.objectClass)
|
|
|
|
return this.getAdapter(domain).tx(txCUD)
|
|
|
|
} else {
|
|
|
|
throw new Error('not implemented (not derived from TxCUD)')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-25 17:55:26 +00:00
|
|
|
async findAll<T extends Doc> (
|
|
|
|
clazz: Ref<Class<T>>,
|
|
|
|
query: DocumentQuery<T>,
|
|
|
|
options?: FindOptions<T>
|
|
|
|
): Promise<FindResult<T>> {
|
|
|
|
const domain = this.hierarchy.getDomain(clazz)
|
2021-08-26 12:16:45 +00:00
|
|
|
return await this.getAdapter(domain).findAll(clazz, query, options)
|
2021-08-25 17:55:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async tx (tx: Tx): Promise<Tx[]> {
|
2021-08-26 12:16:45 +00:00
|
|
|
// store tx
|
|
|
|
await this.getAdapter(DOMAIN_TX).tx(tx)
|
|
|
|
|
2021-08-26 16:58:45 +00:00
|
|
|
if (tx.objectSpace === core.space.Model) {
|
|
|
|
// maintain hiearachy and triggers
|
|
|
|
this.hierarchy.tx(tx)
|
|
|
|
await this.triggers.tx(tx)
|
|
|
|
return []
|
|
|
|
} else {
|
|
|
|
// store object
|
2021-08-26 12:16:45 +00:00
|
|
|
await this.routeTx(tx)
|
2021-08-26 16:58:45 +00:00
|
|
|
// invoke triggers and store derived objects
|
|
|
|
const derived = await this.triggers.apply(tx.modifiedBy, tx)
|
|
|
|
for (const tx of derived) {
|
|
|
|
await this.routeTx(tx)
|
|
|
|
}
|
|
|
|
return derived
|
2021-08-26 12:16:45 +00:00
|
|
|
}
|
2021-08-25 17:55:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
2021-08-26 12:16:45 +00:00
|
|
|
export async function createServerStorage (conf: DbConfiguration): Promise<ServerStorage> {
|
2021-08-25 17:55:26 +00:00
|
|
|
const hierarchy = new Hierarchy()
|
2021-08-26 08:26:06 +00:00
|
|
|
const triggers = new Triggers()
|
2021-08-26 12:16:45 +00:00
|
|
|
const adapters = new Map<string, DbAdapter>()
|
2021-08-25 17:55:26 +00:00
|
|
|
|
2021-08-26 12:16:45 +00:00
|
|
|
for (const key in conf.adapters) {
|
|
|
|
const adapterConf = conf.adapters[key]
|
|
|
|
adapters.set(key, await adapterConf.factory(hierarchy, adapterConf.url, conf.workspace))
|
|
|
|
}
|
2021-08-25 17:55:26 +00:00
|
|
|
|
2021-08-26 12:16:45 +00:00
|
|
|
const txAdapter = adapters.get(conf.domains[DOMAIN_TX]) as TxAdapter
|
|
|
|
if (txAdapter === undefined) {
|
|
|
|
console.log('no txadapter found')
|
2021-08-25 17:55:26 +00:00
|
|
|
}
|
|
|
|
|
2021-08-26 12:16:45 +00:00
|
|
|
const model = await txAdapter.getModel()
|
|
|
|
|
|
|
|
for (const tx of model) {
|
|
|
|
hierarchy.tx(tx)
|
2021-08-25 17:55:26 +00:00
|
|
|
await triggers.tx(tx)
|
|
|
|
}
|
|
|
|
|
2021-08-26 12:16:45 +00:00
|
|
|
for (const [, adapter] of adapters) {
|
|
|
|
await adapter.init(model)
|
|
|
|
}
|
2021-08-25 17:55:26 +00:00
|
|
|
|
2021-08-26 12:16:45 +00:00
|
|
|
return new TServerStorage(conf.domains, conf.defaultAdapter, adapters, hierarchy, triggers)
|
2021-08-25 17:55:26 +00:00
|
|
|
}
|
|
|
|
|
2021-08-05 09:30:14 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export const serverCoreId = 'server-core' as Plugin
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
2021-08-18 17:43:17 +00:00
|
|
|
const serverCore = plugin(serverCoreId, {
|
2021-08-05 13:19:27 +00:00
|
|
|
class: {
|
|
|
|
Trigger: '' as Ref<Class<Trigger>>
|
2021-08-05 09:30:14 +00:00
|
|
|
}
|
|
|
|
})
|
2021-08-18 17:43:17 +00:00
|
|
|
|
|
|
|
export default serverCore
|