add index to model

Signed-off-by: Andrey Platov <andrey@hardcoreeng.com>
This commit is contained in:
Andrey Platov 2021-08-26 19:59:08 +02:00
parent 64e88b01ec
commit 753e5e4690
No known key found for this signature in database
GPG Key ID: C8787EFEB4B64AF0
5 changed files with 245 additions and 182 deletions

File diff suppressed because it is too large Load Diff

View File

@ -14,8 +14,9 @@
// //
import type { IntlString } from '@anticrm/platform' import type { IntlString } from '@anticrm/platform'
import { Builder, Model, UX } from '@anticrm/model' import { Builder, Model, Prop, UX, TypeString, Index } from '@anticrm/model'
import type { Ref, Doc, Class, Domain } from '@anticrm/core' import type { Ref, Doc, Class, Domain } from '@anticrm/core'
import { IndexKind } from '@anticrm/core'
import core, { TSpace, TDoc } from '@anticrm/model-core' import core, { TSpace, TDoc } from '@anticrm/model-core'
import type { Backlink, Channel, Message, Comment } from '@anticrm/chunter' import type { Backlink, Channel, Message, Comment } from '@anticrm/chunter'
import type { AnyComponent } from '@anticrm/ui' import type { AnyComponent } from '@anticrm/ui'
@ -34,6 +35,8 @@ export class TChannel extends TSpace implements Channel {}
@Model(chunter.class.Message, core.class.Doc, DOMAIN_CHUNTER) @Model(chunter.class.Message, core.class.Doc, DOMAIN_CHUNTER)
export class TMessage extends TDoc implements Message { export class TMessage extends TDoc implements Message {
@Prop(TypeString(), 'Content' as IntlString)
@Index(IndexKind.FullText)
content!: string content!: string
} }

View File

@ -67,6 +67,13 @@ export interface UXObject extends Obj {
// eslint-disable-next-line @typescript-eslint/no-unused-vars // eslint-disable-next-line @typescript-eslint/no-unused-vars
export interface Type<T extends PropertyType> extends UXObject {} export interface Type<T extends PropertyType> extends UXObject {}
/**
* @public
*/
export enum IndexKind {
FullText
}
/** /**
* @public * @public
*/ */
@ -74,6 +81,7 @@ export interface Attribute<T extends PropertyType> extends Doc, UXObject {
attributeOf: Ref<Class<Obj>> attributeOf: Ref<Class<Obj>>
name: string name: string
type: Type<T> type: Type<T>
index?: IndexKind
} }
/** /**

View File

@ -169,6 +169,14 @@ export class Hierarchy {
attributes.set(attribute.name, attribute) attributes.set(attribute.name, attribute)
} }
getAttributes (clazz: Ref<Class<Obj>>): Map<string, AnyAttribute> {
const attributes = this.attributes.get(clazz)
if (attributes === undefined) {
throw new Error('attributes not found for class ' + clazz)
}
return attributes
}
getAttribute (_class: Ref<Class<Obj>>, name: string): AnyAttribute { getAttribute (_class: Ref<Class<Obj>>, name: string): AnyAttribute {
const attribute = this.attributes.get(_class)?.get(name) const attribute = this.attributes.get(_class)?.get(name)
if (attribute === undefined) { if (attribute === undefined) {

View File

@ -29,7 +29,7 @@ import type {
Space, Space,
ExtendedAttributes ExtendedAttributes
} from '@anticrm/core' } from '@anticrm/core'
import { ClassifierKind, generateId, TxFactory } from '@anticrm/core' import { ClassifierKind, IndexKind, generateId, TxFactory } from '@anticrm/core'
import type { IntlString, Asset } from '@anticrm/platform' import type { IntlString, Asset } from '@anticrm/platform'
import toposort from 'toposort' import toposort from 'toposort'
@ -37,6 +37,21 @@ import core from './component'
type NoIDs<T extends Tx> = Omit<T, '_id' | 'objectId'> type NoIDs<T extends Tx> = Omit<T, '_id' | 'objectId'>
const targets = new Map<any, Map<string, IndexKind>>()
function setIndex (target: any, property: string, index: IndexKind): void {
let indexes = targets.get(target)
if (indexes === undefined) {
indexes = new Map<string, IndexKind>()
targets.set(target, indexes)
}
indexes.set(property, index)
}
function getIndex (target: any, property: string): IndexKind | undefined {
return targets.get(target)?.get(property)
}
interface ClassTxes { interface ClassTxes {
_id: Ref<Class<Obj>> _id: Ref<Class<Obj>>
extends?: Ref<Class<Obj>> extends?: Ref<Class<Obj>>
@ -77,8 +92,9 @@ export function Prop (type: Type<PropertyType>, label?: IntlString, icon?: Asset
objectSpace: core.space.Model, objectSpace: core.space.Model,
objectClass: core.class.Attribute, objectClass: core.class.Attribute,
attributes: { attributes: {
type,
name: propertyKey, name: propertyKey,
index: getIndex(target, propertyKey),
type,
label, label,
icon, icon,
attributeOf: txes._id // undefined, need to fix later attributeOf: txes._id // undefined, need to fix later
@ -88,6 +104,15 @@ export function Prop (type: Type<PropertyType>, label?: IntlString, icon?: Asset
} }
} }
/**
* @public
*/
export function Index (kind: IndexKind) {
return function (target: any, propertyKey: string): void {
setIndex(target, propertyKey, kind)
}
}
/** /**
* @public * @public
*/ */