mirror of
https://github.com/hcengineering/platform.git
synced 2025-04-14 04:08:19 +00:00
mongodb $like
implementation
Signed-off-by: Andrey Platov <andrey@hardcoreeng.com>
This commit is contained in:
parent
7e33fd8906
commit
1d7c296d8a
@ -19,8 +19,6 @@ import type { Doc } from './classes'
|
|||||||
type Predicate = (docs: Doc[]) => Doc[]
|
type Predicate = (docs: Doc[]) => Doc[]
|
||||||
type PredicateFactory = (pred: any, propertyKey: string) => Predicate
|
type PredicateFactory = (pred: any, propertyKey: string) => Predicate
|
||||||
|
|
||||||
const likeSymbol = '%'
|
|
||||||
|
|
||||||
const predicates: Record<string, PredicateFactory> = {
|
const predicates: Record<string, PredicateFactory> = {
|
||||||
$in: (o: any, propertyKey: string): Predicate => {
|
$in: (o: any, propertyKey: string): Predicate => {
|
||||||
if (!Array.isArray(o)) {
|
if (!Array.isArray(o)) {
|
||||||
@ -36,7 +34,7 @@ const predicates: Record<string, PredicateFactory> = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
$like: (query: string, propertyKey: string): Predicate => {
|
$like: (query: string, propertyKey: string): Predicate => {
|
||||||
const searchString = query.split(likeSymbol).join('.*')
|
const searchString = query.split('%').join('.*')
|
||||||
const regex = RegExp(`^${searchString}$`, 'i')
|
const regex = RegExp(`^${searchString}$`, 'i')
|
||||||
return (docs: Doc[]): Doc[] => {
|
return (docs: Doc[]): Doc[] => {
|
||||||
const result: Doc[] = []
|
const result: Doc[] = []
|
||||||
|
@ -35,8 +35,26 @@ abstract class MongoAdapterBase extends TxProcessor {
|
|||||||
async init (): Promise<void> {}
|
async init (): Promise<void> {}
|
||||||
|
|
||||||
private translateQuery<T extends Doc> (clazz: Ref<Class<T>>, query: DocumentQuery<T>): Filter<Document> {
|
private translateQuery<T extends Doc> (clazz: Ref<Class<T>>, query: DocumentQuery<T>): Filter<Document> {
|
||||||
|
const translated: any = {}
|
||||||
|
for (const key in query) {
|
||||||
|
const value = (query as any)[key]
|
||||||
|
if (typeof value === 'object') {
|
||||||
|
const keys = Object.keys(value)
|
||||||
|
if (keys[0] === '$like') {
|
||||||
|
const pattern = value.$like as string
|
||||||
|
translated[key] = {
|
||||||
|
$regex: `^${pattern.split('%').join('.*')}$`,
|
||||||
|
$options: 'i'
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
translated[key] = value
|
||||||
|
}
|
||||||
const classes = this.hierarchy.getDescendants(clazz)
|
const classes = this.hierarchy.getDescendants(clazz)
|
||||||
return Object.assign({}, query, { _class: { $in: classes } })
|
translated._class = { $in: classes }
|
||||||
|
// return Object.assign({}, query, { _class: { $in: classes } })
|
||||||
|
return translated
|
||||||
}
|
}
|
||||||
|
|
||||||
private async lookup<T extends Doc> (clazz: Ref<Class<T>>, query: DocumentQuery<T>, options: FindOptions<T>): Promise<FindResult<T>> {
|
private async lookup<T extends Doc> (clazz: Ref<Class<T>>, query: DocumentQuery<T>, options: FindOptions<T>): Promise<FindResult<T>> {
|
||||||
|
Loading…
Reference in New Issue
Block a user