2021-08-04 09:10:22 +00:00
|
|
|
//
|
|
|
|
// Copyright © 2020 Anticrm Platform Contributors.
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
|
|
|
|
import type {
|
|
|
|
Tx,
|
|
|
|
Ref,
|
|
|
|
Doc,
|
|
|
|
Class,
|
|
|
|
DocumentQuery,
|
|
|
|
FindResult,
|
|
|
|
FindOptions
|
|
|
|
} from '@anticrm/core'
|
|
|
|
import { getResource } from '@anticrm/platform'
|
|
|
|
import core, { ModelDb, TxDb, Hierarchy, DOMAIN_TX, DefaultTxFactory } from '@anticrm/core'
|
|
|
|
|
2021-08-04 17:13:14 +00:00
|
|
|
import * as txJson from './model.tx.json'
|
|
|
|
|
2021-08-04 16:17:01 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export interface ServerStorage {
|
|
|
|
findAll: <T extends Doc>(
|
|
|
|
_class: Ref<Class<T>>,
|
|
|
|
query: DocumentQuery<T>,
|
|
|
|
options?: FindOptions<T>
|
|
|
|
) => Promise<FindResult<T>>
|
|
|
|
tx: (tx: Tx) => Promise<Tx[]>
|
|
|
|
}
|
|
|
|
|
|
|
|
class DevStorage implements ServerStorage {
|
2021-08-04 09:10:22 +00:00
|
|
|
private readonly txFactory: DefaultTxFactory
|
|
|
|
|
|
|
|
constructor (
|
|
|
|
private readonly hierarchy: Hierarchy,
|
|
|
|
private readonly txdb: TxDb,
|
2021-08-04 16:17:01 +00:00
|
|
|
private readonly modeldb: ModelDb
|
|
|
|
) {
|
2021-08-04 09:10:22 +00:00
|
|
|
this.txFactory = new DefaultTxFactory(core.account.System)
|
|
|
|
}
|
|
|
|
|
|
|
|
async findAll<T extends Doc> (
|
|
|
|
_class: Ref<Class<T>>,
|
|
|
|
query: DocumentQuery<T>,
|
|
|
|
options?: FindOptions<T>
|
|
|
|
): Promise<FindResult<T>> {
|
|
|
|
const domain = this.hierarchy.getClass(_class).domain
|
|
|
|
if (domain === DOMAIN_TX) return await this.txdb.findAll(_class, query, options)
|
|
|
|
return await this.modeldb.findAll(_class, query, options)
|
|
|
|
}
|
|
|
|
|
2021-08-04 16:17:01 +00:00
|
|
|
async tx (tx: Tx): Promise<Tx[]> {
|
2021-08-04 09:10:22 +00:00
|
|
|
if (tx.objectSpace === core.space.Model) {
|
|
|
|
this.hierarchy.tx(tx)
|
|
|
|
}
|
|
|
|
await Promise.all([this.modeldb.tx(tx), this.txdb.tx(tx)])
|
|
|
|
// invoke triggers
|
|
|
|
const triggers = this.hierarchy.getClass(tx.objectClass).triggers
|
|
|
|
if (triggers !== undefined) {
|
2021-08-04 16:17:01 +00:00
|
|
|
const derived: Tx[] = []
|
2021-08-04 09:10:22 +00:00
|
|
|
for (const trigger of triggers) {
|
|
|
|
const impl = await getResource(trigger)
|
|
|
|
const txes = await impl(tx, this.txFactory)
|
2021-08-04 16:17:01 +00:00
|
|
|
derived.push(...txes)
|
2021-08-04 09:10:22 +00:00
|
|
|
for (const tx of txes) {
|
|
|
|
await Promise.all([this.modeldb.tx(tx), this.txdb.tx(tx)])
|
|
|
|
}
|
|
|
|
}
|
2021-08-04 16:17:01 +00:00
|
|
|
return derived
|
2021-08-04 09:10:22 +00:00
|
|
|
}
|
2021-08-04 16:17:01 +00:00
|
|
|
return []
|
2021-08-04 09:10:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-04 09:16:47 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
2021-08-04 16:17:01 +00:00
|
|
|
export async function createStorage (): Promise<ServerStorage> {
|
2021-08-04 17:13:14 +00:00
|
|
|
const txes = txJson as unknown as Tx[]
|
2021-08-04 09:10:22 +00:00
|
|
|
const hierarchy = new Hierarchy()
|
|
|
|
for (const tx of txes) hierarchy.tx(tx)
|
|
|
|
|
|
|
|
const transactions = new TxDb(hierarchy)
|
|
|
|
const model = new ModelDb(hierarchy)
|
|
|
|
for (const tx of txes) {
|
|
|
|
await Promise.all([transactions.tx(tx), model.tx(tx)])
|
|
|
|
}
|
|
|
|
|
2021-08-04 16:17:01 +00:00
|
|
|
return new DevStorage(hierarchy, transactions, model)
|
2021-08-04 09:10:22 +00:00
|
|
|
}
|