mirror of
https://github.com/hcengineering/platform.git
synced 2025-01-08 21:27:45 +03:00
rework triggers
Signed-off-by: Andrey Platov <andrey@hardcoreeng.com>
This commit is contained in:
parent
f90f9d5521
commit
aaaa6d26e4
26
common/config/rush/pnpm-lock.yaml
generated
26
common/config/rush/pnpm-lock.yaml
generated
@ -8,6 +8,7 @@ specifiers:
|
||||
'@rush-temp/platform': file:./projects/platform.tgz
|
||||
'@rush-temp/platform-rig': file:./projects/platform-rig.tgz
|
||||
'@rush-temp/prod': file:./projects/prod.tgz
|
||||
'@rush-temp/server': file:./projects/server.tgz
|
||||
'@rush-temp/server-core': file:./projects/server-core.tgz
|
||||
'@rush-temp/server-ws': file:./projects/server-ws.tgz
|
||||
'@rush-temp/theme': file:./projects/theme.tgz
|
||||
@ -56,6 +57,7 @@ dependencies:
|
||||
'@rush-temp/platform': file:projects/platform.tgz_6c259fadfeb3a4b20890aefe87070b8b
|
||||
'@rush-temp/platform-rig': file:projects/platform-rig.tgz_6ab28797e7a22071465f7d680ae81ae5
|
||||
'@rush-temp/prod': file:projects/prod.tgz_typescript@4.3.5
|
||||
'@rush-temp/server': file:projects/server.tgz_6c259fadfeb3a4b20890aefe87070b8b
|
||||
'@rush-temp/server-core': file:projects/server-core.tgz_6c259fadfeb3a4b20890aefe87070b8b
|
||||
'@rush-temp/server-ws': file:projects/server-ws.tgz_6c259fadfeb3a4b20890aefe87070b8b
|
||||
'@rush-temp/theme': file:projects/theme.tgz_c38cf1a7a413db8918b0b4754c21e4c5
|
||||
@ -7529,7 +7531,7 @@ packages:
|
||||
dev: false
|
||||
|
||||
file:projects/dev-storage.tgz_6c259fadfeb3a4b20890aefe87070b8b:
|
||||
resolution: {integrity: sha512-YmDGlFbQJXNPLUxWyHQMvnULcuGmWeW1CJUWGSatSSdQ0oSnPnCvhAZQ+XKl024M4WurMbLz8dtS2q4Mlvl6hA==, tarball: file:projects/dev-storage.tgz}
|
||||
resolution: {integrity: sha512-8aUE293xbOrDLOua1RsUMHGN5KMpU+FP0b6KRVBGnUMkbtvGZEghpU1lH3VkcNJgHbGKWFgdZsvhsZ+P1kohJg==, tarball: file:projects/dev-storage.tgz}
|
||||
id: file:projects/dev-storage.tgz
|
||||
name: '@rush-temp/dev-storage'
|
||||
version: 0.0.0
|
||||
@ -7632,7 +7634,7 @@ packages:
|
||||
dev: false
|
||||
|
||||
file:projects/server-core.tgz_6c259fadfeb3a4b20890aefe87070b8b:
|
||||
resolution: {integrity: sha512-Y/tFe0ms52BXamdAMTFcI8GLan6+g39TKzG8taX7PiwXTdpVr170mZ4zTnER6OnxBHOlKXfwaHvgFTUmdupnEw==, tarball: file:projects/server-core.tgz}
|
||||
resolution: {integrity: sha512-lHE4PAWrX+WKdM9/Yy1rYZW6rCacwLMkSUIJakrXZeyGlNytXXSvH20bgfocVn73grez3Zjy6qlNm7H7EClgDQ==, tarball: file:projects/server-core.tgz}
|
||||
id: file:projects/server-core.tgz
|
||||
name: '@rush-temp/server-core'
|
||||
version: 0.0.0
|
||||
@ -7675,6 +7677,26 @@ packages:
|
||||
- utf-8-validate
|
||||
dev: false
|
||||
|
||||
file:projects/server.tgz_6c259fadfeb3a4b20890aefe87070b8b:
|
||||
resolution: {integrity: sha512-DDKeiFrAHUr+H88vgiv9LFigEmlZyqSLez073wjTVT/WS1cbHQ4cSAxObaxl1w3jt2HZ7q0Twf0DK51NQXQZRA==, tarball: file:projects/server.tgz}
|
||||
id: file:projects/server.tgz
|
||||
name: '@rush-temp/server'
|
||||
version: 0.0.0
|
||||
dependencies:
|
||||
'@types/heft-jest': 1.0.2
|
||||
'@types/node': 16.4.10
|
||||
'@types/ws': 7.4.7
|
||||
'@typescript-eslint/eslint-plugin': 4.28.5_a8e83fcad666e1ba86be4b2e27a20aea
|
||||
eslint: 7.32.0
|
||||
eslint-plugin-import: 2.23.4_eslint@7.32.0
|
||||
eslint-plugin-node: 11.1.0_eslint@7.32.0
|
||||
eslint-plugin-promise: 4.3.1
|
||||
transitivePeerDependencies:
|
||||
- '@typescript-eslint/parser'
|
||||
- supports-color
|
||||
- typescript
|
||||
dev: false
|
||||
|
||||
file:projects/theme.tgz_c38cf1a7a413db8918b0b4754c21e4c5:
|
||||
resolution: {integrity: sha512-LLq+6SEKjH+rv8a9NMUF+uUZBaT0NYj0PDJvVsJEO5PeheKKgDPZrAm4VsKNNr23NAIjipSgiaZKWEb0aQi7qg==, tarball: file:projects/theme.tgz}
|
||||
id: file:projects/theme.tgz
|
||||
|
@ -28,7 +28,7 @@ export class DevSession implements Storage {
|
||||
return await this.storage.findAll(_class, query, options)
|
||||
}
|
||||
|
||||
async tx (tx: Tx<Doc>): Promise<void> {
|
||||
async tx (tx: Tx): Promise<void> {
|
||||
const derived = await this.storage.tx(tx)
|
||||
for (const tx of derived) {
|
||||
this.server.broadcast(this, { result: tx })
|
||||
|
@ -20,6 +20,7 @@
|
||||
"dependencies": {
|
||||
"@anticrm/core": "~0.6.7",
|
||||
"@anticrm/platform": "~0.6.3",
|
||||
"@anticrm/server-core": "~0.6.0"
|
||||
"@anticrm/server-core": "~0.6.0",
|
||||
"@anticrm/server": "~0.6.0"
|
||||
}
|
||||
}
|
||||
|
@ -14,9 +14,8 @@
|
||||
//
|
||||
|
||||
import type { Tx, Ref, Doc, Class, DocumentQuery, FindResult, FindOptions } from '@anticrm/core'
|
||||
import { getResource } from '@anticrm/platform'
|
||||
import core, { ModelDb, TxDb, Hierarchy, DOMAIN_TX, TxFactory } from '@anticrm/core'
|
||||
import serverCore from '@anticrm/server-core'
|
||||
import { Triggers } from '@anticrm/server'
|
||||
|
||||
import * as txJson from './model.tx.json'
|
||||
|
||||
@ -33,14 +32,14 @@ export interface ServerStorage {
|
||||
}
|
||||
|
||||
class DevStorage implements ServerStorage {
|
||||
private readonly txFactory: TxFactory
|
||||
private readonly triggers: Triggers
|
||||
|
||||
constructor (
|
||||
private readonly hierarchy: Hierarchy,
|
||||
private readonly txdb: TxDb,
|
||||
private readonly modeldb: ModelDb
|
||||
) {
|
||||
this.txFactory = new TxFactory(core.account.System)
|
||||
this.triggers = new Triggers(new TxFactory(core.account.System))
|
||||
}
|
||||
|
||||
async findAll<T extends Doc> (
|
||||
@ -56,24 +55,10 @@ class DevStorage implements ServerStorage {
|
||||
async tx (tx: Tx): Promise<Tx[]> {
|
||||
if (tx.objectSpace === core.space.Model) {
|
||||
this.hierarchy.tx(tx)
|
||||
await this.triggers.tx(tx)
|
||||
}
|
||||
await Promise.all([this.modeldb.tx(tx), this.txdb.tx(tx)])
|
||||
// invoke triggers
|
||||
const clazz = this.hierarchy.getClass(tx.objectClass)
|
||||
const triggers = this.hierarchy.as(clazz, serverCore.mixin.Triggers).triggers
|
||||
if (triggers !== undefined) {
|
||||
const derived: Tx[] = []
|
||||
for (const trigger of triggers) {
|
||||
const impl = await getResource(trigger)
|
||||
const txes = await impl(tx, this.txFactory)
|
||||
derived.push(...txes)
|
||||
for (const tx of txes) {
|
||||
await Promise.all([this.modeldb.tx(tx), this.txdb.tx(tx)])
|
||||
}
|
||||
}
|
||||
return derived
|
||||
}
|
||||
return []
|
||||
return await this.triggers.apply(tx)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -495,6 +495,11 @@
|
||||
"packageName": "@anticrm/server-core",
|
||||
"projectFolder": "server/core",
|
||||
"shouldPublish": true
|
||||
},
|
||||
{
|
||||
"packageName": "@anticrm/server",
|
||||
"projectFolder": "server/server",
|
||||
"shouldPublish": true
|
||||
}
|
||||
|
||||
]
|
||||
|
@ -16,18 +16,18 @@
|
||||
|
||||
import type { Resource, Plugin } from '@anticrm/platform'
|
||||
import { plugin } from '@anticrm/platform'
|
||||
import type { Class, Obj, Arr, Tx, TxFactory, Mixin, Ref } from '@anticrm/core'
|
||||
import type { Doc, Tx, TxFactory, Class, Ref } from '@anticrm/core'
|
||||
|
||||
/**
|
||||
* @public
|
||||
*/
|
||||
export type Trigger = Resource<(tx: Tx, txFactory: TxFactory) => Promise<Tx[]>>
|
||||
export type TriggerFunc = (tx: Tx, txFactory: TxFactory) => Promise<Tx[]>
|
||||
|
||||
/**
|
||||
* @public
|
||||
*/
|
||||
export interface Triggers extends Class<Obj> {
|
||||
triggers: Arr<Trigger>
|
||||
export interface Trigger extends Doc {
|
||||
trigger: Resource<TriggerFunc>
|
||||
}
|
||||
|
||||
/**
|
||||
@ -39,7 +39,7 @@ export const serverCoreId = 'server-core' as Plugin
|
||||
* @public
|
||||
*/
|
||||
export default plugin(serverCoreId, {
|
||||
mixin: {
|
||||
Triggers: '' as Ref<Mixin<Triggers>>
|
||||
class: {
|
||||
Trigger: '' as Ref<Class<Trigger>>
|
||||
}
|
||||
})
|
||||
|
6
server/server/.eslintrc.js
Normal file
6
server/server/.eslintrc.js
Normal file
@ -0,0 +1,6 @@
|
||||
module.exports = {
|
||||
extends: ['./node_modules/@anticrm/platform-rig/profiles/default/config/eslint.config.json'],
|
||||
parserOptions: {
|
||||
project: './tsconfig.json'
|
||||
}
|
||||
}
|
4
server/server/.npmignore
Normal file
4
server/server/.npmignore
Normal file
@ -0,0 +1,4 @@
|
||||
*
|
||||
!/lib/**
|
||||
!CHANGELOG.md
|
||||
/lib/**/__tests__/
|
18
server/server/config/rig.json
Normal file
18
server/server/config/rig.json
Normal file
@ -0,0 +1,18 @@
|
||||
// The "rig.json" file directs tools to look for their config files in an external package.
|
||||
// Documentation for this system: https://www.npmjs.com/package/@rushstack/rig-package
|
||||
{
|
||||
"$schema": "https://developer.microsoft.com/json-schemas/rig-package/rig.schema.json",
|
||||
|
||||
/**
|
||||
* (Required) The name of the rig package to inherit from.
|
||||
* It should be an NPM package name with the "-rig" suffix.
|
||||
*/
|
||||
"rigPackageName": "@anticrm/platform-rig"
|
||||
|
||||
/**
|
||||
* (Optional) Selects a config profile from the rig package. The name must consist of
|
||||
* lowercase alphanumeric words separated by hyphens, for example "sample-profile".
|
||||
* If omitted, then the "default" profile will be used."
|
||||
*/
|
||||
// "rigProfile": "your-profile-name"
|
||||
}
|
27
server/server/package.json
Normal file
27
server/server/package.json
Normal file
@ -0,0 +1,27 @@
|
||||
{
|
||||
"name": "@anticrm/server",
|
||||
"version": "0.6.0",
|
||||
"main": "lib/index.js",
|
||||
"author": "Anticrm Platform Contributors",
|
||||
"license": "EPL-2.0",
|
||||
"scripts": {
|
||||
"build": "heft build",
|
||||
"lint:fix": "eslint --fix src"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@anticrm/platform-rig":"~0.6.0",
|
||||
"@types/heft-jest":"^1.0.2",
|
||||
"@types/node": "^16.4.10",
|
||||
"@typescript-eslint/eslint-plugin":"4",
|
||||
"eslint-plugin-import":"2",
|
||||
"eslint-plugin-promise":"4",
|
||||
"eslint-plugin-node":"11",
|
||||
"eslint":"^7.32.0",
|
||||
"@types/ws":"^7.4.7"
|
||||
},
|
||||
"dependencies": {
|
||||
"@anticrm/core": "~0.6.3",
|
||||
"@anticrm/platform": "~0.6.3",
|
||||
"@anticrm/server-core": "~0.6.0"
|
||||
}
|
||||
}
|
17
server/server/src/index.ts
Normal file
17
server/server/src/index.ts
Normal file
@ -0,0 +1,17 @@
|
||||
//
|
||||
// Copyright © 2020, 2021 Anticrm Platform Contributors.
|
||||
// Copyright © 2021 Hardcore Engineering Inc.
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
export * from './triggers'
|
49
server/server/src/triggers.ts
Normal file
49
server/server/src/triggers.ts
Normal file
@ -0,0 +1,49 @@
|
||||
//
|
||||
// Copyright © 2020, 2021 Anticrm Platform Contributors.
|
||||
// Copyright © 2021 Hardcore Engineering Inc.
|
||||
//
|
||||
// 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 { Doc, Tx, TxCreateDoc, TxFactory } from '@anticrm/core'
|
||||
import { getResource } from '@anticrm/platform'
|
||||
|
||||
import core from '@anticrm/core'
|
||||
import serverCore, { Trigger, TriggerFunc } from '@anticrm/server-core'
|
||||
|
||||
/**
|
||||
* @public
|
||||
*/
|
||||
export class Triggers {
|
||||
private readonly triggers: TriggerFunc[] = []
|
||||
|
||||
constructor (private readonly txFactory: TxFactory) {
|
||||
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async apply (tx: Tx): Promise<Tx[]> {
|
||||
const derived = this.triggers.map(trigger => trigger(tx, this.txFactory))
|
||||
const result = await Promise.all(derived)
|
||||
return result.flatMap(x => x)
|
||||
}
|
||||
}
|
9
server/server/tsconfig.json
Normal file
9
server/server/tsconfig.json
Normal file
@ -0,0 +1,9 @@
|
||||
{
|
||||
"extends": "./node_modules/@anticrm/platform-rig/profiles/default/tsconfig.json",
|
||||
|
||||
"compilerOptions": {
|
||||
"rootDir": "./src",
|
||||
"outDir": "./lib",
|
||||
"esModuleInterop": true
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user