2021-08-03 16:55:52 +00:00
|
|
|
//
|
|
|
|
// Copyright © 2020, 2021 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 { KeysByType } from 'simplytyped'
|
2021-09-15 10:53:11 +00:00
|
|
|
import type { Class, Data, Doc, Domain, Ref, Account, Space, Arr, Mixin, PropertyType } from './classes'
|
2021-08-24 19:00:56 +00:00
|
|
|
import { DocumentQuery, FindOptions, FindResult, Storage, WithLookup } from './storage'
|
2021-08-03 16:55:52 +00:00
|
|
|
import core from './component'
|
|
|
|
import { generateId } from './utils'
|
|
|
|
|
2021-08-05 06:47:54 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
2021-08-05 12:39:43 +00:00
|
|
|
export interface Tx extends Doc {
|
|
|
|
objectSpace: Ref<Space> // space where transaction will operate
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export interface TxCUD<T extends Doc> extends Tx {
|
2021-08-05 06:47:54 +00:00
|
|
|
objectId: Ref<T>
|
|
|
|
objectClass: Ref<Class<T>>
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
2021-08-05 12:39:43 +00:00
|
|
|
export interface TxCreateDoc<T extends Doc> extends TxCUD<T> {
|
2021-08-05 06:47:54 +00:00
|
|
|
attributes: Data<T>
|
|
|
|
}
|
|
|
|
|
2021-09-15 10:53:11 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export interface TxPutBag<T extends PropertyType> extends TxCUD<Doc> {
|
|
|
|
bag: string
|
|
|
|
key: string
|
|
|
|
value: T
|
|
|
|
}
|
|
|
|
|
2021-10-07 20:01:17 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export interface TxBulkWrite extends Tx {
|
|
|
|
txes: TxCUD<Doc>[]
|
|
|
|
}
|
|
|
|
|
2021-08-05 06:47:54 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export type ExtendedAttributes<D extends Doc, M extends D> = Omit<M, keyof D>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
2021-08-05 12:39:43 +00:00
|
|
|
export interface TxMixin<D extends Doc, M extends D> extends TxCUD<D> {
|
2021-08-05 06:47:54 +00:00
|
|
|
mixin: Ref<Mixin<M>>
|
|
|
|
attributes: ExtendedAttributes<D, M>
|
|
|
|
}
|
|
|
|
|
2021-08-04 20:24:30 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export type ArrayAsElement<T extends Doc> = {
|
2021-08-03 16:55:52 +00:00
|
|
|
[P in keyof T]: T[P] extends Arr<infer X> ? X : never
|
|
|
|
}
|
|
|
|
|
2021-09-22 08:41:03 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export interface Position<X extends PropertyType> {
|
|
|
|
$each: X[]
|
|
|
|
$position: number
|
|
|
|
}
|
|
|
|
|
2021-10-09 11:17:00 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export interface MoveDescriptor<X extends PropertyType> {
|
|
|
|
$value: X
|
|
|
|
$position: number
|
|
|
|
}
|
|
|
|
|
2021-09-22 08:41:03 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export type ArrayAsElementPosition<T extends Doc> = {
|
|
|
|
[P in keyof T]: T[P] extends Arr<infer X> ? X | Position<X> : never
|
|
|
|
}
|
|
|
|
|
2021-10-09 11:17:00 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export type ArrayMoveDescriptor<T extends Doc> = {
|
|
|
|
[P in keyof T]: T[P] extends Arr<infer X> ? MoveDescriptor<X> : never
|
|
|
|
}
|
|
|
|
|
2021-09-26 10:48:15 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export type NumberProperties<T extends Doc> = {
|
|
|
|
[P in keyof T]: T[P] extends number | undefined ? T[P] : never
|
|
|
|
}
|
|
|
|
|
2021-08-04 20:24:30 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export type OmitNever<T extends object> = Omit<T, KeysByType<T, never>>
|
2021-08-03 16:55:52 +00:00
|
|
|
|
2021-08-04 20:24:30 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export interface PushOptions<T extends Doc> {
|
2021-09-22 08:41:03 +00:00
|
|
|
$push?: Partial<OmitNever<ArrayAsElementPosition<T>>>
|
|
|
|
$pull?: Partial<OmitNever<ArrayAsElement<T>>>
|
2021-10-09 11:17:00 +00:00
|
|
|
$move?: Partial<OmitNever<ArrayMoveDescriptor<T>>>
|
2021-08-03 16:55:52 +00:00
|
|
|
}
|
|
|
|
|
2021-08-04 20:24:30 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
2021-08-05 09:15:09 +00:00
|
|
|
export interface PushMixinOptions<D extends Doc> {
|
|
|
|
$pushMixin?: {
|
|
|
|
$mixin: Ref<Mixin<D>>
|
|
|
|
values: Partial<OmitNever<ArrayAsElement<D>>>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
2021-09-26 10:48:15 +00:00
|
|
|
export interface IncOptions<T extends Doc> {
|
|
|
|
$inc?: Partial<OmitNever<NumberProperties<T>>>
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export type DocumentUpdate<T extends Doc> = Partial<Data<T>> & PushOptions<T> & PushMixinOptions<T> & IncOptions<T>
|
2021-08-03 16:55:52 +00:00
|
|
|
|
2021-08-04 20:24:30 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
2021-08-05 12:39:43 +00:00
|
|
|
export interface TxUpdateDoc<T extends Doc> extends TxCUD<T> {
|
2021-08-03 16:55:52 +00:00
|
|
|
operations: DocumentUpdate<T>
|
|
|
|
}
|
|
|
|
|
2021-08-04 20:24:30 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
2021-08-05 12:39:43 +00:00
|
|
|
export interface TxRemoveDoc<T extends Doc> extends TxCUD<T> {
|
2021-08-03 16:55:52 +00:00
|
|
|
}
|
|
|
|
|
2021-08-04 20:24:30 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
2021-08-03 16:55:52 +00:00
|
|
|
export const DOMAIN_TX = 'tx' as Domain
|
|
|
|
|
2021-08-04 20:24:30 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export interface WithTx {
|
2021-08-03 16:55:52 +00:00
|
|
|
tx: (tx: Tx) => Promise<void>
|
|
|
|
}
|
|
|
|
|
2021-08-04 20:24:30 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
2021-09-15 10:53:11 +00:00
|
|
|
export abstract class TxProcessor implements WithTx {
|
2021-08-03 16:55:52 +00:00
|
|
|
async tx (tx: Tx): Promise<void> {
|
|
|
|
switch (tx._class) {
|
|
|
|
case core.class.TxCreateDoc:
|
|
|
|
return await this.txCreateDoc(tx as TxCreateDoc<Doc>)
|
|
|
|
case core.class.TxUpdateDoc:
|
|
|
|
return await this.txUpdateDoc(tx as TxUpdateDoc<Doc>)
|
|
|
|
case core.class.TxRemoveDoc:
|
|
|
|
return await this.txRemoveDoc(tx as TxRemoveDoc<Doc>)
|
|
|
|
case core.class.TxMixin:
|
|
|
|
return await this.txMixin(tx as TxMixin<Doc, Doc>)
|
2021-09-15 10:53:11 +00:00
|
|
|
case core.class.TxPutBag:
|
|
|
|
return await this.txPutBag(tx as TxPutBag<PropertyType>)
|
2021-10-07 20:01:17 +00:00
|
|
|
case core.class.TxBulkWrite:
|
|
|
|
return await this.txBulkWrite(tx as TxBulkWrite)
|
2021-08-03 16:55:52 +00:00
|
|
|
}
|
2021-08-05 12:39:43 +00:00
|
|
|
throw new Error('TxProcessor: unhandled transaction class: ' + tx._class)
|
2021-08-03 16:55:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static createDoc2Doc<T extends Doc> (tx: TxCreateDoc<T>): T {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
|
|
return {
|
|
|
|
_id: tx.objectId,
|
|
|
|
_class: tx.objectClass,
|
|
|
|
space: tx.objectSpace,
|
|
|
|
modifiedBy: tx.modifiedBy,
|
|
|
|
modifiedOn: tx.modifiedOn,
|
|
|
|
...tx.attributes
|
|
|
|
} as T
|
|
|
|
}
|
|
|
|
|
2021-09-15 10:53:11 +00:00
|
|
|
protected abstract txCreateDoc (tx: TxCreateDoc<Doc>): Promise<void>
|
|
|
|
protected abstract txPutBag (tx: TxPutBag<PropertyType>): Promise<void>
|
|
|
|
protected abstract txUpdateDoc (tx: TxUpdateDoc<Doc>): Promise<void>
|
|
|
|
protected abstract txRemoveDoc (tx: TxRemoveDoc<Doc>): Promise<void>
|
|
|
|
protected abstract txMixin (tx: TxMixin<Doc, Doc>): Promise<void>
|
2021-10-07 20:01:17 +00:00
|
|
|
|
|
|
|
protected async txBulkWrite (bulkTx: TxBulkWrite): Promise<void> {
|
|
|
|
for (const tx of bulkTx.txes) {
|
|
|
|
console.log('bulk', tx)
|
|
|
|
await this.tx(tx)
|
|
|
|
}
|
|
|
|
}
|
2021-08-03 16:55:52 +00:00
|
|
|
}
|
|
|
|
|
2021-08-04 20:24:30 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
2021-08-05 07:23:13 +00:00
|
|
|
export class TxOperations implements Storage {
|
2021-10-07 20:01:17 +00:00
|
|
|
readonly txFactory: TxFactory
|
2021-08-05 07:23:13 +00:00
|
|
|
|
|
|
|
constructor (private readonly storage: Storage, user: Ref<Account>) {
|
|
|
|
this.txFactory = new TxFactory(user)
|
|
|
|
}
|
|
|
|
|
|
|
|
findAll <T extends Doc>(_class: Ref<Class<T>>, query: DocumentQuery<T>, options?: FindOptions<T> | undefined): Promise<FindResult<T>> {
|
|
|
|
return this.storage.findAll(_class, query, options)
|
|
|
|
}
|
|
|
|
|
2021-08-24 19:00:56 +00:00
|
|
|
async findOne <T extends Doc>(_class: Ref<Class<T>>, query: DocumentQuery<T>, options?: FindOptions<T> | undefined): Promise<WithLookup<T> | undefined> {
|
|
|
|
return (await this.findAll(_class, query, options))[0]
|
|
|
|
}
|
|
|
|
|
2021-08-05 12:39:43 +00:00
|
|
|
tx (tx: Tx): Promise<void> {
|
2021-08-05 07:23:13 +00:00
|
|
|
return this.storage.tx(tx)
|
|
|
|
}
|
|
|
|
|
|
|
|
async createDoc<T extends Doc> (
|
|
|
|
_class: Ref<Class<T>>,
|
|
|
|
space: Ref<Space>,
|
2021-08-29 15:30:22 +00:00
|
|
|
attributes: Data<T>,
|
|
|
|
id?: Ref<T>
|
2021-08-05 07:23:13 +00:00
|
|
|
): Promise<Ref<T>> {
|
2021-08-29 15:30:22 +00:00
|
|
|
const tx = this.txFactory.createTxCreateDoc(_class, space, attributes, id)
|
2021-08-05 07:23:13 +00:00
|
|
|
await this.storage.tx(tx)
|
|
|
|
return tx.objectId
|
|
|
|
}
|
|
|
|
|
2021-09-15 10:53:11 +00:00
|
|
|
putBag <P extends PropertyType>(
|
|
|
|
_class: Ref<Class<Doc>>,
|
|
|
|
space: Ref<Space>,
|
|
|
|
objectId: Ref<Doc>,
|
|
|
|
bag: string,
|
|
|
|
key: string,
|
|
|
|
value: P
|
|
|
|
): Promise<void> {
|
|
|
|
const tx = this.txFactory.createTxPutBag(_class, space, objectId, bag, key, value)
|
|
|
|
return this.storage.tx(tx)
|
|
|
|
}
|
|
|
|
|
2021-08-05 07:23:13 +00:00
|
|
|
updateDoc <T extends Doc>(
|
2021-08-03 16:55:52 +00:00
|
|
|
_class: Ref<Class<T>>,
|
|
|
|
space: Ref<Space>,
|
|
|
|
objectId: Ref<T>,
|
|
|
|
operations: DocumentUpdate<T>
|
2021-08-05 07:23:13 +00:00
|
|
|
): Promise<void> {
|
|
|
|
const tx = this.txFactory.createTxUpdateDoc(_class, space, objectId, operations)
|
|
|
|
return this.storage.tx(tx)
|
|
|
|
}
|
|
|
|
|
|
|
|
removeDoc<T extends Doc> (
|
|
|
|
_class: Ref<Class<T>>,
|
|
|
|
space: Ref<Space>,
|
|
|
|
objectId: Ref<T>
|
|
|
|
): Promise<void> {
|
|
|
|
const tx = this.txFactory.createTxRemoveDoc(_class, space, objectId)
|
|
|
|
return this.storage.tx(tx)
|
|
|
|
}
|
2021-08-05 09:15:09 +00:00
|
|
|
|
|
|
|
createMixin<D extends Doc, M extends D>(
|
|
|
|
objectId: Ref<D>,
|
|
|
|
objectClass: Ref<Class<D>>,
|
|
|
|
mixin: Ref<Mixin<M>>,
|
|
|
|
attributes: ExtendedAttributes<D, M>
|
|
|
|
): Promise<void> {
|
|
|
|
const tx = this.txFactory.createTxMixin(objectId, objectClass, mixin, attributes)
|
|
|
|
return this.storage.tx(tx)
|
|
|
|
}
|
2021-08-03 16:55:52 +00:00
|
|
|
}
|
|
|
|
|
2021-08-04 20:24:30 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
2021-08-05 07:23:13 +00:00
|
|
|
export class TxFactory {
|
|
|
|
constructor (readonly account: Ref<Account>) {}
|
2021-08-03 16:55:52 +00:00
|
|
|
|
2021-08-05 07:23:13 +00:00
|
|
|
createTxCreateDoc<T extends Doc>(_class: Ref<Class<T>>, space: Ref<Space>, attributes: Data<T>, objectId?: Ref<T>): TxCreateDoc<T> {
|
|
|
|
return {
|
2021-08-03 16:55:52 +00:00
|
|
|
_id: generateId(),
|
|
|
|
_class: core.class.TxCreateDoc,
|
|
|
|
space: core.space.Tx,
|
2021-08-05 07:23:13 +00:00
|
|
|
objectId: objectId ?? generateId(),
|
2021-08-03 16:55:52 +00:00
|
|
|
objectClass: _class,
|
|
|
|
objectSpace: space,
|
2021-08-05 07:23:13 +00:00
|
|
|
modifiedOn: Date.now(),
|
|
|
|
modifiedBy: this.account,
|
2021-08-03 16:55:52 +00:00
|
|
|
attributes
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-15 10:53:11 +00:00
|
|
|
createTxPutBag <P extends PropertyType>(
|
|
|
|
_class: Ref<Class<Doc>>,
|
|
|
|
space: Ref<Space>,
|
|
|
|
objectId: Ref<Doc>,
|
|
|
|
bag: string,
|
|
|
|
key: string,
|
|
|
|
value: P
|
|
|
|
): TxPutBag<P> {
|
|
|
|
return {
|
|
|
|
_id: generateId(),
|
2021-09-15 17:03:34 +00:00
|
|
|
_class: core.class.TxPutBag,
|
2021-09-15 10:53:11 +00:00
|
|
|
space: core.space.Tx,
|
|
|
|
modifiedBy: this.account,
|
|
|
|
modifiedOn: Date.now(),
|
|
|
|
objectId,
|
|
|
|
objectClass: _class,
|
|
|
|
objectSpace: space,
|
|
|
|
bag,
|
|
|
|
key,
|
|
|
|
value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-05 07:23:13 +00:00
|
|
|
createTxUpdateDoc <T extends Doc>(
|
2021-08-03 16:55:52 +00:00
|
|
|
_class: Ref<Class<T>>,
|
|
|
|
space: Ref<Space>,
|
|
|
|
objectId: Ref<T>,
|
|
|
|
operations: DocumentUpdate<T>
|
2021-08-05 07:23:13 +00:00
|
|
|
): TxUpdateDoc<T> {
|
|
|
|
return {
|
2021-08-03 16:55:52 +00:00
|
|
|
_id: generateId(),
|
|
|
|
_class: core.class.TxUpdateDoc,
|
|
|
|
space: core.space.Tx,
|
2021-08-05 07:23:13 +00:00
|
|
|
modifiedBy: this.account,
|
2021-08-03 16:55:52 +00:00
|
|
|
modifiedOn: Date.now(),
|
|
|
|
objectId,
|
|
|
|
objectClass: _class,
|
|
|
|
objectSpace: space,
|
|
|
|
operations
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-05 07:23:13 +00:00
|
|
|
createTxRemoveDoc<T extends Doc> (
|
2021-08-03 16:55:52 +00:00
|
|
|
_class: Ref<Class<T>>,
|
|
|
|
space: Ref<Space>,
|
|
|
|
objectId: Ref<T>
|
2021-08-05 07:23:13 +00:00
|
|
|
): TxRemoveDoc<T> {
|
|
|
|
return {
|
2021-08-03 16:55:52 +00:00
|
|
|
_id: generateId(),
|
|
|
|
_class: core.class.TxRemoveDoc,
|
|
|
|
space: core.space.Tx,
|
2021-08-05 07:23:13 +00:00
|
|
|
modifiedBy: this.account,
|
2021-08-03 16:55:52 +00:00
|
|
|
modifiedOn: Date.now(),
|
|
|
|
objectId,
|
|
|
|
objectClass: _class,
|
|
|
|
objectSpace: space
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
createTxMixin<D extends Doc, M extends D>(objectId: Ref<D>, objectClass: Ref<Class<D>>, mixin: Ref<Mixin<M>>, attributes: ExtendedAttributes<D, M>): TxMixin<D, M> {
|
|
|
|
return {
|
|
|
|
_id: generateId(),
|
|
|
|
_class: core.class.TxMixin,
|
|
|
|
space: core.space.Tx,
|
|
|
|
modifiedBy: this.account,
|
|
|
|
modifiedOn: Date.now(),
|
|
|
|
objectId,
|
|
|
|
objectClass,
|
|
|
|
objectSpace: core.space.Model,
|
|
|
|
mixin,
|
|
|
|
attributes
|
|
|
|
}
|
|
|
|
}
|
2021-10-07 20:01:17 +00:00
|
|
|
|
|
|
|
createTxBulkWrite (space: Ref<Space>, txes: TxCUD<Doc>[]): TxBulkWrite {
|
|
|
|
return {
|
|
|
|
_id: generateId(),
|
|
|
|
_class: core.class.TxBulkWrite,
|
|
|
|
space: core.space.Tx,
|
|
|
|
modifiedBy: this.account,
|
|
|
|
modifiedOn: Date.now(),
|
|
|
|
objectSpace: space,
|
|
|
|
txes
|
|
|
|
}
|
|
|
|
}
|
2021-08-03 16:55:52 +00:00
|
|
|
}
|