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.
|
|
|
|
//
|
|
|
|
|
2023-11-20 08:10:51 +00:00
|
|
|
import { deepEqual } from 'fast-equals'
|
2023-06-06 10:06:32 +00:00
|
|
|
import { Account, AnyAttribute, Class, Doc, DocData, DocIndexState, IndexKind, Obj, Ref, Space } from './classes'
|
2023-01-04 17:58:54 +00:00
|
|
|
import core from './component'
|
2023-03-21 16:08:45 +00:00
|
|
|
import { Hierarchy } from './hierarchy'
|
2023-11-10 07:09:38 +00:00
|
|
|
import { isPredicate } from './predicate'
|
2023-11-20 10:01:43 +00:00
|
|
|
import { DocumentQuery, FindResult } from './storage'
|
2021-08-03 16:55:52 +00:00
|
|
|
|
|
|
|
function toHex (value: number, chars: number): string {
|
|
|
|
const result = value.toString(16)
|
|
|
|
if (result.length < chars) {
|
|
|
|
return '0'.repeat(chars - result.length) + result
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
let counter = (Math.random() * (1 << 24)) | 0
|
|
|
|
const random = toHex((Math.random() * (1 << 24)) | 0, 6) + toHex((Math.random() * (1 << 16)) | 0, 4)
|
|
|
|
|
|
|
|
function timestamp (): string {
|
|
|
|
const time = (Date.now() / 1000) | 0
|
|
|
|
return toHex(time, 8)
|
|
|
|
}
|
|
|
|
|
|
|
|
function count (): string {
|
|
|
|
const val = counter++ & 0xffffff
|
|
|
|
return toHex(val, 6)
|
|
|
|
}
|
|
|
|
|
2021-08-04 20:24:30 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
* @returns
|
|
|
|
*/
|
2021-08-03 16:55:52 +00:00
|
|
|
export function generateId<T extends Doc> (): Ref<T> {
|
|
|
|
return (timestamp() + random + count()) as Ref<T>
|
|
|
|
}
|
2021-09-10 15:54:13 +00:00
|
|
|
|
|
|
|
let currentAccount: Account
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
* @returns
|
|
|
|
*/
|
2022-04-08 03:06:38 +00:00
|
|
|
export function getCurrentAccount (): Account {
|
|
|
|
return currentAccount
|
|
|
|
}
|
2021-09-10 15:54:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
* @param account -
|
|
|
|
*/
|
|
|
|
export function setCurrentAccount (account: Account): void {
|
|
|
|
currentAccount = account
|
|
|
|
}
|
2022-02-16 09:02:31 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export function escapeLikeForRegexp (value: string): string {
|
|
|
|
return value.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&')
|
|
|
|
}
|
2022-04-08 03:06:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export function toFindResult<T extends Doc> (docs: T[], total?: number): FindResult<T> {
|
|
|
|
const length = total ?? docs.length
|
|
|
|
return Object.assign(docs, { total: length })
|
|
|
|
}
|
2022-11-16 15:03:03 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export interface WorkspaceId {
|
|
|
|
name: string
|
|
|
|
productId: string
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*
|
|
|
|
* Combine workspace with productId, if not equal ''
|
|
|
|
*/
|
|
|
|
export function getWorkspaceId (workspace: string, productId: string = ''): WorkspaceId {
|
|
|
|
return {
|
|
|
|
name: workspace,
|
|
|
|
productId
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export function toWorkspaceString (id: WorkspaceId, sep = '@'): string {
|
|
|
|
return id.name + (id.productId === '' ? '' : sep + id.productId)
|
|
|
|
}
|
2023-01-04 17:58:54 +00:00
|
|
|
|
|
|
|
const attributesPrefix = 'attributes.'
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export interface IndexKeyOptions {
|
|
|
|
_class?: Ref<Class<Obj>>
|
|
|
|
docId?: Ref<DocIndexState>
|
|
|
|
extra?: string[]
|
2023-11-17 07:35:09 +00:00
|
|
|
relative?: boolean
|
|
|
|
refAttribute?: string
|
2023-01-04 17:58:54 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
|
|
|
|
export function docUpdKey (name: string, opt?: IndexKeyOptions): string {
|
|
|
|
return attributesPrefix + docKey(name, opt)
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export function docKey (name: string, opt?: IndexKeyOptions): string {
|
|
|
|
const extra = opt?.extra !== undefined && opt?.extra?.length > 0 ? `#${opt.extra?.join('#') ?? ''}` : ''
|
2023-12-27 07:29:30 +00:00
|
|
|
return opt?._class === undefined ? name : `${opt?._class}%${name}${extra}`
|
2023-01-04 17:58:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export function extractDocKey (key: string): {
|
|
|
|
_class?: Ref<Class<Doc>>
|
|
|
|
attr: string
|
|
|
|
docId?: Ref<DocIndexState>
|
|
|
|
extra: string[]
|
|
|
|
} {
|
|
|
|
let k = key
|
|
|
|
if (k.startsWith(attributesPrefix)) {
|
|
|
|
k = k.slice(attributesPrefix.length)
|
|
|
|
}
|
|
|
|
let docId: Ref<DocIndexState> | undefined
|
|
|
|
let _class: Ref<Class<Doc>> | undefined
|
|
|
|
let attr = ''
|
|
|
|
const docSepPos = k.indexOf('|')
|
|
|
|
if (docSepPos !== -1) {
|
|
|
|
docId = k.substring(0, docSepPos).replace('_', '.') as Ref<DocIndexState>
|
|
|
|
k = k.substring(docSepPos + 1)
|
|
|
|
}
|
|
|
|
const clPos = k.indexOf('%')
|
|
|
|
if (clPos !== -1) {
|
|
|
|
_class = k.substring(0, clPos) as Ref<Class<Doc>>
|
|
|
|
attr = k.substring(clPos + 1)
|
|
|
|
} else {
|
|
|
|
attr = k
|
|
|
|
}
|
|
|
|
const extra = attr.split('#')
|
|
|
|
attr = extra.splice(0, 1)[0]
|
|
|
|
|
|
|
|
return { docId, attr, _class, extra }
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export function isFullTextAttribute (attr: AnyAttribute): boolean {
|
2023-04-20 10:11:22 +00:00
|
|
|
return (
|
|
|
|
attr.index === IndexKind.FullText ||
|
|
|
|
attr.type._class === core.class.TypeAttachment ||
|
|
|
|
attr.type._class === core.class.EnumOf
|
|
|
|
)
|
2023-01-04 17:58:54 +00:00
|
|
|
}
|
2023-01-18 09:57:13 +00:00
|
|
|
|
2023-10-17 08:21:59 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export function isIndexedAttribute (attr: AnyAttribute): boolean {
|
2024-01-15 16:10:30 +00:00
|
|
|
return attr.index === IndexKind.Indexed || attr.index === IndexKind.IndexedDsc
|
2023-10-17 08:21:59 +00:00
|
|
|
}
|
|
|
|
|
2023-01-18 09:57:13 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export interface IdMap<T extends Doc> extends Map<Ref<T>, T> {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export function toIdMap<T extends Doc> (arr: T[]): IdMap<T> {
|
|
|
|
return new Map(arr.map((p) => [p._id, p]))
|
|
|
|
}
|
2023-02-01 11:27:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export function concatLink (host: string, path: string): string {
|
|
|
|
if (!host.endsWith('/') && !path.startsWith('/')) {
|
|
|
|
return `${host}/${path}`
|
|
|
|
} else if (host.endsWith('/') && path.startsWith('/')) {
|
|
|
|
const newPath = path.slice(1)
|
|
|
|
return `${host}${newPath}`
|
|
|
|
} else {
|
|
|
|
return `${host}${path}`
|
|
|
|
}
|
|
|
|
}
|
2023-03-21 16:08:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export function fillDefaults<T extends Doc> (
|
|
|
|
hierarchy: Hierarchy,
|
|
|
|
object: DocData<T> | T,
|
|
|
|
_class: Ref<Class<T>>
|
|
|
|
): DocData<T> | T {
|
|
|
|
const baseClass = hierarchy.isDerived(_class, core.class.AttachedDoc) ? core.class.AttachedDoc : core.class.Doc
|
|
|
|
const attributes = hierarchy.getAllAttributes(_class, baseClass)
|
|
|
|
for (const attribute of attributes) {
|
|
|
|
if (attribute[1].defaultValue !== undefined) {
|
|
|
|
if ((object as any)[attribute[0]] === undefined) {
|
2023-12-26 15:31:22 +00:00
|
|
|
// Clone default value as it might be an object (e.g. array)
|
|
|
|
;(object as any)[attribute[0]] = structuredClone(attribute[1].defaultValue)
|
2023-03-21 16:08:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return object
|
|
|
|
}
|
2023-06-06 10:06:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export class AggregateValueData {
|
|
|
|
constructor (
|
|
|
|
readonly name: string,
|
|
|
|
readonly _id: Ref<Doc>,
|
|
|
|
readonly space: Ref<Space>,
|
|
|
|
readonly rank?: string,
|
|
|
|
readonly category?: Ref<Doc>
|
|
|
|
) {}
|
|
|
|
|
|
|
|
getRank (): string {
|
|
|
|
return this.rank ?? ''
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export class AggregateValue {
|
2023-11-20 10:01:43 +00:00
|
|
|
constructor (
|
|
|
|
readonly name: string | undefined,
|
|
|
|
readonly values: AggregateValueData[]
|
|
|
|
) {}
|
2023-06-06 10:06:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export type CategoryType = number | string | undefined | Ref<Doc> | AggregateValue
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export class DocManager {
|
|
|
|
protected readonly byId: IdMap<Doc>
|
|
|
|
|
|
|
|
constructor (protected readonly docs: Doc[]) {
|
|
|
|
this.byId = toIdMap(docs)
|
|
|
|
}
|
|
|
|
|
|
|
|
get (ref: Ref<Doc>): Doc | undefined {
|
|
|
|
return this.byId.get(ref)
|
|
|
|
}
|
|
|
|
|
|
|
|
getDocs (): Doc[] {
|
|
|
|
return this.docs
|
|
|
|
}
|
|
|
|
|
|
|
|
getIdMap (): IdMap<Doc> {
|
|
|
|
return this.byId
|
|
|
|
}
|
|
|
|
|
|
|
|
filter (predicate: (value: Doc) => boolean): Doc[] {
|
|
|
|
return this.docs.filter(predicate)
|
|
|
|
}
|
|
|
|
}
|
2023-09-14 11:17:44 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
|
|
|
|
export class RateLimitter {
|
|
|
|
idCounter: number = 0
|
|
|
|
processingQueue = new Map<string, Promise<void>>()
|
|
|
|
last: number = 0
|
|
|
|
|
|
|
|
queue: (() => Promise<void>)[] = []
|
|
|
|
|
|
|
|
constructor (readonly config: () => { rate: number, perSecond?: number }) {}
|
|
|
|
|
2023-11-20 10:01:43 +00:00
|
|
|
async exec<T, B extends Record<string, any> = any>(op: (args?: B) => Promise<T>, args?: B): Promise<T> {
|
2023-09-14 11:17:44 +00:00
|
|
|
const processingId = `${this.idCounter++}`
|
|
|
|
const cfg = this.config()
|
|
|
|
|
|
|
|
while (this.processingQueue.size > cfg.rate) {
|
|
|
|
await Promise.race(this.processingQueue.values())
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
const p = op(args)
|
|
|
|
this.processingQueue.set(processingId, p as Promise<void>)
|
|
|
|
return await p
|
|
|
|
} finally {
|
|
|
|
this.processingQueue.delete(processingId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-20 10:01:43 +00:00
|
|
|
async add<T, B extends Record<string, any> = any>(op: (args?: B) => Promise<T>, args?: B): Promise<void> {
|
2023-09-14 11:17:44 +00:00
|
|
|
const cfg = this.config()
|
|
|
|
|
|
|
|
if (this.processingQueue.size < cfg.rate) {
|
|
|
|
void this.exec(op, args)
|
|
|
|
} else {
|
|
|
|
await this.exec(op, args)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async waitProcessing (): Promise<void> {
|
2023-11-20 10:01:43 +00:00
|
|
|
await Promise.race(this.processingQueue.values())
|
2023-09-14 11:17:44 +00:00
|
|
|
}
|
|
|
|
}
|
2023-11-10 07:09:38 +00:00
|
|
|
|
|
|
|
export function mergeQueries<T extends Doc> (query1: DocumentQuery<T>, query2: DocumentQuery<T>): DocumentQuery<T> {
|
2023-11-20 08:10:51 +00:00
|
|
|
const keys1 = Object.keys(query1)
|
|
|
|
const keys2 = Object.keys(query2)
|
|
|
|
|
|
|
|
const query = {}
|
|
|
|
|
|
|
|
for (const key of keys1) {
|
|
|
|
if (!keys2.includes(key)) {
|
|
|
|
Object.assign(query, { [key]: query1[key] })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const key of keys2) {
|
|
|
|
if (!keys1.includes(key)) {
|
|
|
|
Object.assign(query, { [key]: query2[key] })
|
|
|
|
} else {
|
|
|
|
const value = mergeField(query1[key], query2[key])
|
|
|
|
if (value !== undefined) {
|
|
|
|
Object.assign(query, { [key]: value })
|
|
|
|
}
|
2023-11-10 07:09:38 +00:00
|
|
|
}
|
2023-11-20 08:10:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return query
|
|
|
|
}
|
|
|
|
|
2023-11-20 10:01:43 +00:00
|
|
|
function mergeField (field1: any, field2: any): any | undefined {
|
2023-11-20 08:10:51 +00:00
|
|
|
// this is a special predicate that causes query never return any docs
|
|
|
|
// it is used in cases when queries intersection is empty
|
|
|
|
const never = { $in: [] }
|
|
|
|
// list of ignored predicates, handled separately
|
|
|
|
const ignored = ['$in', '$nin', '$ne']
|
|
|
|
|
|
|
|
const isPredicate1 = isPredicate(field1)
|
|
|
|
const isPredicate2 = isPredicate(field2)
|
|
|
|
|
|
|
|
if (isPredicate1 && isPredicate2) {
|
|
|
|
// $in, $nin, $eq are related fields so handle them separately here
|
|
|
|
const result = getInNiN(field1, field2)
|
|
|
|
|
|
|
|
const keys1 = Object.keys(field1)
|
|
|
|
const keys2 = Object.keys(field2)
|
|
|
|
|
|
|
|
for (const key of keys1) {
|
|
|
|
if (ignored.includes(key)) continue
|
|
|
|
|
|
|
|
if (!keys2.includes(key)) {
|
|
|
|
Object.assign(result, { [key]: field1[key] })
|
|
|
|
} else {
|
|
|
|
const value = mergePredicateWithPredicate(key, field1[key], field2[key])
|
|
|
|
if (value !== undefined) {
|
|
|
|
Object.assign(result, { [key]: value })
|
2023-11-10 07:09:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-11-20 08:10:51 +00:00
|
|
|
|
|
|
|
for (const key of keys2) {
|
|
|
|
if (ignored.includes(key)) continue
|
|
|
|
|
|
|
|
if (!keys1.includes(key)) {
|
|
|
|
Object.assign(result, { [key]: field2[key] })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Object.keys(result).length > 0 ? result : undefined
|
|
|
|
} else if (isPredicate1 || isPredicate2) {
|
|
|
|
// when one field is a predicate and the other is a simple value
|
|
|
|
// we need to ensure that the value matches predicate
|
|
|
|
const predicate = isPredicate1 ? field1 : field2
|
|
|
|
const value = isPredicate1 ? field2 : field1
|
|
|
|
|
|
|
|
for (const x in predicate) {
|
|
|
|
const result = mergePredicateWithValue(x, predicate[x], value)
|
|
|
|
if (result !== undefined) {
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we reached here, the value does not match the predicate
|
|
|
|
return never
|
|
|
|
} else {
|
|
|
|
// both are not predicates, can filter only when values are equal
|
|
|
|
return deepEqual(field1, field2) ? field1 : never
|
2023-11-10 07:09:38 +00:00
|
|
|
}
|
2023-11-20 08:10:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function mergePredicateWithPredicate (predicate: string, val1: any, val2: any): any | undefined {
|
|
|
|
if (val1 === undefined) return val2
|
|
|
|
if (val2 === undefined) return val1
|
|
|
|
|
|
|
|
switch (predicate) {
|
|
|
|
case '$lt':
|
|
|
|
return val1 < val2 ? val1 : val2
|
|
|
|
case '$lte':
|
|
|
|
return val1 <= val2 ? val1 : val2
|
|
|
|
case '$gt':
|
|
|
|
return val1 > val2 ? val1 : val2
|
|
|
|
case '$gte':
|
|
|
|
return val1 >= val2 ? val1 : val2
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO we should properly support all available predicates here
|
|
|
|
// until then, fallback to the first predicate value
|
|
|
|
|
|
|
|
return val1
|
|
|
|
}
|
|
|
|
|
|
|
|
function mergePredicateWithValue (predicate: string, val1: any, val2: any): any | undefined {
|
|
|
|
switch (predicate) {
|
|
|
|
case '$in':
|
|
|
|
return Array.isArray(val1) && val1.includes(val2) ? val2 : undefined
|
|
|
|
case '$nin':
|
|
|
|
return Array.isArray(val1) && !val1.includes(val2) ? val2 : undefined
|
|
|
|
case '$lt':
|
|
|
|
return val2 < val1 ? val2 : undefined
|
|
|
|
case '$lte':
|
|
|
|
return val2 <= val1 ? val2 : undefined
|
|
|
|
case '$gt':
|
|
|
|
return val2 > val1 ? val2 : undefined
|
|
|
|
case '$gte':
|
|
|
|
return val2 >= val1 ? val2 : undefined
|
|
|
|
case '$ne':
|
|
|
|
return val1 !== val2 ? val2 : undefined
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO we should properly support all available predicates here
|
|
|
|
// until then, fallback to the non-predicate value
|
|
|
|
|
|
|
|
return val2
|
2023-11-10 07:09:38 +00:00
|
|
|
}
|
|
|
|
|
2023-11-20 10:01:43 +00:00
|
|
|
function getInNiN (query1: any, query2: any): any {
|
2023-11-20 08:10:51 +00:00
|
|
|
const aIn = typeof query1 === 'object' && '$in' in query1 ? query1.$in : undefined
|
|
|
|
const bIn = typeof query2 === 'object' && '$in' in query2 ? query2.$in : undefined
|
2023-11-10 07:09:38 +00:00
|
|
|
const aNIn =
|
|
|
|
(typeof query1 === 'object' && '$nin' in query1 ? query1.$nin : undefined) ??
|
|
|
|
(typeof query1 === 'object' && query1.$ne !== undefined ? [query1.$ne] : [])
|
|
|
|
const bNIn =
|
|
|
|
(typeof query2 === 'object' && '$nin' in query2 ? query2.$nin : undefined) ??
|
2023-11-20 08:10:51 +00:00
|
|
|
(typeof query1 === 'object' && query2.$ne !== undefined ? [query2.$ne] : [])
|
|
|
|
|
2023-11-10 07:09:38 +00:00
|
|
|
const finalNin = Array.from(new Set([...aNIn, ...bNIn]))
|
2023-11-20 08:10:51 +00:00
|
|
|
|
|
|
|
// we must keep $in if it was in the original query
|
|
|
|
if (aIn !== undefined || bIn !== undefined) {
|
|
|
|
const finalIn =
|
|
|
|
aIn !== undefined && bIn !== undefined
|
|
|
|
? aIn.length - bIn.length < 0
|
|
|
|
? bIn.filter((c: any) => aIn.includes(c))
|
|
|
|
: aIn.filter((c: any) => bIn.includes(c))
|
|
|
|
: aIn ?? bIn
|
|
|
|
return { $in: finalIn.filter((p: any) => !finalNin.includes(p)) }
|
2023-11-10 07:09:38 +00:00
|
|
|
}
|
2023-11-20 08:10:51 +00:00
|
|
|
// try to preserve original $ne instead of $nin
|
|
|
|
if ((typeof query1 === 'object' && '$ne' in query1) || (typeof query2 === 'object' && '$ne' in query2)) {
|
|
|
|
if (finalNin.length === 1) {
|
|
|
|
return { $ne: finalNin[0] }
|
|
|
|
}
|
2023-11-10 07:09:38 +00:00
|
|
|
}
|
|
|
|
if (finalNin.length > 0) {
|
2023-11-20 08:10:51 +00:00
|
|
|
return { $nin: finalNin }
|
2023-11-10 07:09:38 +00:00
|
|
|
}
|
2023-11-20 08:10:51 +00:00
|
|
|
return {}
|
2023-11-10 07:09:38 +00:00
|
|
|
}
|
2024-01-16 01:27:33 +00:00
|
|
|
|
|
|
|
export function cutObjectArray (obj: any): any {
|
|
|
|
const r = {}
|
|
|
|
for (const key of Object.keys(obj)) {
|
|
|
|
if (Array.isArray(obj[key])) {
|
|
|
|
if (obj[key].length > 3) {
|
|
|
|
Object.assign(r, { [key]: `[${obj[key].slice(0, 3)}, ... and ${obj[key].length - 3} more]` })
|
|
|
|
} else Object.assign(r, { [key]: obj[key] })
|
|
|
|
continue
|
|
|
|
}
|
2024-01-25 16:41:27 +00:00
|
|
|
if (typeof obj[key] === 'object' && obj[key] !== null) {
|
2024-01-16 01:27:33 +00:00
|
|
|
Object.assign(r, { [key]: cutObjectArray(obj[key]) })
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
Object.assign(r, { [key]: obj[key] })
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|