@wxn0brp/db-core 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +15 -3
- package/dist/version.js +1 -1
- package/package.json +10 -1
- package/.github/workflows/build-relase.yml +0 -25
- package/.github/workflows/build.yml +0 -20
- package/.github/workflows/types.yml +0 -89
- package/CHANGELOG.md +0 -9
- package/src/base/actions.ts +0 -71
- package/src/base/db.ts +0 -75
- package/src/customFileCpu.ts +0 -100
- package/src/db/memory.ts +0 -177
- package/src/db/valthera.ts +0 -132
- package/src/helpers/CollectionManager.ts +0 -67
- package/src/helpers/eventEmiter.ts +0 -73
- package/src/helpers/executor.ts +0 -55
- package/src/helpers/gen.ts +0 -154
- package/src/helpers/relation.ts +0 -192
- package/src/helpers/updateOneOrAdd.ts +0 -24
- package/src/index.ts +0 -25
- package/src/types/Id.ts +0 -3
- package/src/types/arg.ts +0 -15
- package/src/types/data.ts +0 -4
- package/src/types/export.ts +0 -13
- package/src/types/fileCpu.ts +0 -78
- package/src/types/options.ts +0 -22
- package/src/types/query.ts +0 -16
- package/src/types/relation.ts +0 -34
- package/src/types/searchOpts.ts +0 -78
- package/src/types/types.ts +0 -3
- package/src/types/updater.ts +0 -33
- package/src/types/valthera.ts +0 -38
- package/src/utils/hasFields.ts +0 -18
- package/src/utils/hasFieldsAdvanced.ts +0 -181
- package/src/utils/sort.ts +0 -25
- package/src/utils/updateFindObject.ts +0 -35
- package/src/utils/updateObject.ts +0 -156
- package/src/version.ts +0 -1
- package/suglite.json +0 -10
- package/tsconfig.json +0 -22
package/src/index.ts
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import genId from "./helpers/gen";
|
|
2
|
-
import Relation from "./helpers/relation";
|
|
3
|
-
import { RelationTypes } from "./types/relation";
|
|
4
|
-
import { ValtheraCompatible } from "./types/valthera";
|
|
5
|
-
import { ValtheraTypes } from "./types/export";
|
|
6
|
-
import CustomFileCpu from "./customFileCpu";
|
|
7
|
-
import ValtheraClass from "./db/valthera";
|
|
8
|
-
import ValtheraMemory, { createMemoryValthera } from "./db/memory";
|
|
9
|
-
|
|
10
|
-
export {
|
|
11
|
-
ValtheraClass,
|
|
12
|
-
ValtheraMemory,
|
|
13
|
-
createMemoryValthera,
|
|
14
|
-
Relation,
|
|
15
|
-
genId,
|
|
16
|
-
CustomFileCpu,
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export type {
|
|
20
|
-
ValtheraCompatible,
|
|
21
|
-
RelationTypes,
|
|
22
|
-
ValtheraTypes
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export type Id = import("./types/Id").Id;
|
package/src/types/Id.ts
DELETED
package/src/types/arg.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import Id from "./Id";
|
|
2
|
-
import { SearchOptions } from "./searchOpts";
|
|
3
|
-
import { VContext } from "./types";
|
|
4
|
-
import { UpdaterArg } from "./updater";
|
|
5
|
-
|
|
6
|
-
export interface Arg {
|
|
7
|
-
_id?: Id,
|
|
8
|
-
[key: string]: any
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export type SearchFunc<T=any> = (data: T, context: VContext) => boolean;
|
|
12
|
-
export type UpdaterFunc<T=any> = (data: T, context: VContext) => boolean;
|
|
13
|
-
|
|
14
|
-
export type Search<T=any> = SearchOptions | SearchFunc<T>;
|
|
15
|
-
export type Updater<T=any> = UpdaterArg | UpdaterArg[] | UpdaterFunc<T>;
|
package/src/types/data.ts
DELETED
package/src/types/export.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
export namespace ValtheraTypes {
|
|
2
|
-
export type Arg = import("./arg").Arg;
|
|
3
|
-
export type Search = import("./arg").Search;
|
|
4
|
-
export type Updater = import("./arg").Updater;
|
|
5
|
-
export type DbFindOpts = import("./options").DbFindOpts;
|
|
6
|
-
export type FindOpts = import("./options").FindOpts;
|
|
7
|
-
export type DbOpts = import("./options").DbOpts;
|
|
8
|
-
export type Data = import("./data").Data;
|
|
9
|
-
export type SearchOptions = import("./searchOpts").SearchOptions;
|
|
10
|
-
export type Id = import("./Id").Id;
|
|
11
|
-
export type Context = import("./types").VContext;
|
|
12
|
-
export type VContext = import("./types").VContext;
|
|
13
|
-
}
|
package/src/types/fileCpu.ts
DELETED
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
import { Search, Updater } from "./arg";
|
|
2
|
-
import Data from "./data";
|
|
3
|
-
import { FindOpts } from "./options";
|
|
4
|
-
import { VContext } from "./types";
|
|
5
|
-
|
|
6
|
-
interface FileCpu {
|
|
7
|
-
/**
|
|
8
|
-
* Asynchronously adds an entry to a file.
|
|
9
|
-
* @param file The path to the file.
|
|
10
|
-
* @param data The data to add.
|
|
11
|
-
* @returns A promise resolving to `void`.
|
|
12
|
-
*/
|
|
13
|
-
add(file: string, data: Data): Promise<void>;
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Asynchronously finds multiple entries in a file based on search criteria.
|
|
17
|
-
* @param file The path to the file.
|
|
18
|
-
* @param arg The search criteria.
|
|
19
|
-
* @param context Additional context for the search.
|
|
20
|
-
* @param findOpts Additional options for searching.
|
|
21
|
-
* @returns A promise resolving to an array of found entries, or `false` if the file does not exist.
|
|
22
|
-
*/
|
|
23
|
-
find(
|
|
24
|
-
file: string,
|
|
25
|
-
arg: Search,
|
|
26
|
-
context?: VContext,
|
|
27
|
-
findOpts?: FindOpts
|
|
28
|
-
): Promise<any[] | false>;
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Asynchronously finds one entry in a file based on search criteria.
|
|
32
|
-
* @param file The path to the file.
|
|
33
|
-
* @param arg The search criteria.
|
|
34
|
-
* @param context Additional context for the search.
|
|
35
|
-
* @param findOpts Additional options for searching.
|
|
36
|
-
* @returns A promise resolving to the found entry or `false` if not found.
|
|
37
|
-
*/
|
|
38
|
-
findOne(
|
|
39
|
-
file: string,
|
|
40
|
-
arg: Search,
|
|
41
|
-
context?: VContext,
|
|
42
|
-
findOpts?: FindOpts
|
|
43
|
-
): Promise<any | false>;
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Asynchronously removes entries from a file based on search criteria.
|
|
47
|
-
* @param file The path to the file.
|
|
48
|
-
* @param arg The search criteria.
|
|
49
|
-
* @param context Additional context for the operation.
|
|
50
|
-
* @param one If `true`, removes only the first matching entry.
|
|
51
|
-
* @returns A promise resolving to `true` if at least one entry was removed, otherwise `false`.
|
|
52
|
-
*/
|
|
53
|
-
remove(
|
|
54
|
-
file: string,
|
|
55
|
-
one: boolean,
|
|
56
|
-
arg: Search,
|
|
57
|
-
context?: VContext
|
|
58
|
-
): Promise<boolean>;
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* Asynchronously updates entries in a file based on search criteria and an updater function or object.
|
|
62
|
-
* @param file The path to the file.
|
|
63
|
-
* @param arg The search criteria.
|
|
64
|
-
* @param updater The updater function or object.
|
|
65
|
-
* @param context Additional context for the operation.
|
|
66
|
-
* @param one If `true`, updates only the first matching entry.
|
|
67
|
-
* @returns A promise resolving to `true` if at least one entry was updated, otherwise `false`.
|
|
68
|
-
*/
|
|
69
|
-
update(
|
|
70
|
-
file: string,
|
|
71
|
-
one: boolean,
|
|
72
|
-
arg: Search,
|
|
73
|
-
updater: Updater,
|
|
74
|
-
context?: VContext,
|
|
75
|
-
): Promise<boolean>;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
export default FileCpu;
|
package/src/types/options.ts
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import dbActionBase from "../base/actions";
|
|
2
|
-
import executorC from "../helpers/executor";
|
|
3
|
-
|
|
4
|
-
export interface DbOpts {
|
|
5
|
-
maxFileSize?: number;
|
|
6
|
-
dbAction?: dbActionBase;
|
|
7
|
-
executor?: executorC;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export interface DbFindOpts {
|
|
11
|
-
reverse?: boolean;
|
|
12
|
-
max?: number;
|
|
13
|
-
offset?: number;
|
|
14
|
-
sortBy?: string;
|
|
15
|
-
sortAsc?: boolean;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export interface FindOpts {
|
|
19
|
-
select?: string[];
|
|
20
|
-
exclude?: string[];
|
|
21
|
-
transform?: Function;
|
|
22
|
-
}
|
package/src/types/query.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { VContext } from "./types";
|
|
2
|
-
import { Arg, Search, Updater } from "./arg";
|
|
3
|
-
import { DbFindOpts, FindOpts } from "./options";
|
|
4
|
-
|
|
5
|
-
export interface VQuery {
|
|
6
|
-
collection?: string;
|
|
7
|
-
search?: Search;
|
|
8
|
-
context?: VContext;
|
|
9
|
-
dbFindOpts?: DbFindOpts;
|
|
10
|
-
findOpts?: FindOpts;
|
|
11
|
-
data?: Arg;
|
|
12
|
-
id_gen?: boolean;
|
|
13
|
-
limit?: number;
|
|
14
|
-
add_arg?: Arg;
|
|
15
|
-
updater?: Updater;
|
|
16
|
-
}
|
package/src/types/relation.ts
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { DbFindOpts } from "./options";
|
|
2
|
-
import { ValtheraCompatible } from "./valthera";
|
|
3
|
-
|
|
4
|
-
export namespace RelationTypes {
|
|
5
|
-
export type Path = [string, string];
|
|
6
|
-
export type FieldPath = string[];
|
|
7
|
-
|
|
8
|
-
export interface DBS {
|
|
9
|
-
[key: string]: ValtheraCompatible
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export interface Relation {
|
|
13
|
-
[key: string]: RelationConfig
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export interface RelationConfig {
|
|
17
|
-
path: Path;
|
|
18
|
-
pk?: string;
|
|
19
|
-
fk?: string;
|
|
20
|
-
as?: string;
|
|
21
|
-
select?: string[];
|
|
22
|
-
|
|
23
|
-
findOpts?: DbFindOpts;
|
|
24
|
-
type?: "1" | "11" | "1n" | "nm"
|
|
25
|
-
relations?: Relation;
|
|
26
|
-
|
|
27
|
-
through?: {
|
|
28
|
-
table: string;
|
|
29
|
-
db?: string;
|
|
30
|
-
pk: string; // local -> pivot
|
|
31
|
-
fk: string; // remote -> pivot
|
|
32
|
-
};
|
|
33
|
-
}
|
|
34
|
-
}
|
package/src/types/searchOpts.ts
DELETED
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Predefined Search Options Quick Reference
|
|
3
|
-
*
|
|
4
|
-
* This module defines the types and structures for search operators used
|
|
5
|
-
* to validate and query data objects.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
import { Arg } from "./arg";
|
|
9
|
-
|
|
10
|
-
/** Logical Operators */
|
|
11
|
-
export type LogicalOperators = {
|
|
12
|
-
/**
|
|
13
|
-
* Recursively applies multiple conditions, all of which must evaluate to true.
|
|
14
|
-
* Can include other operators such as $gt, $exists, or nested $and/$or conditions.
|
|
15
|
-
*/
|
|
16
|
-
$and?: Array<SearchOptions>;
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Recursively applies multiple conditions, at least one of which must evaluate to true.
|
|
20
|
-
* Can include other operators such as $lt, $type, or nested $and/$or conditions.
|
|
21
|
-
*/
|
|
22
|
-
$or?: Array<SearchOptions>;
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Negates a single condition.
|
|
26
|
-
* Can include any other operator as its value.
|
|
27
|
-
*/
|
|
28
|
-
$not?: SearchOptions;
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
/** Comparison Operators */
|
|
32
|
-
export type ComparisonOperators = {
|
|
33
|
-
$gt?: Record<string, number>;
|
|
34
|
-
$lt?: Record<string, number>;
|
|
35
|
-
$gte?: Record<string, number>;
|
|
36
|
-
$lte?: Record<string, number>;
|
|
37
|
-
$in?: Record<string, any[]>;
|
|
38
|
-
$nin?: Record<string, any[]>;
|
|
39
|
-
$between?: Record<string, [number, number]>;
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
/** Type and Existence Operators */
|
|
43
|
-
export type TypeAndExistenceOperators = {
|
|
44
|
-
$exists?: Record<string, boolean>;
|
|
45
|
-
$type?: Record<string, string>;
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
/** Array Operators */
|
|
49
|
-
export type ArrayOperators = {
|
|
50
|
-
$arrinc?: Record<string, any[]>;
|
|
51
|
-
$arrincall?: Record<string, any[]>;
|
|
52
|
-
$size?: Record<string, number>;
|
|
53
|
-
};
|
|
54
|
-
|
|
55
|
-
/** String Operators */
|
|
56
|
-
export type StringOperators = {
|
|
57
|
-
$regex?: Record<string, RegExp>;
|
|
58
|
-
$startsWith?: Record<string, string>;
|
|
59
|
-
$endsWith?: Record<string, string>;
|
|
60
|
-
};
|
|
61
|
-
|
|
62
|
-
/** Other Operators */
|
|
63
|
-
export type OtherOperators = {
|
|
64
|
-
$subset?: Record<string, any>;
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
/** Predefined Search Operators */
|
|
68
|
-
export type PredefinedSearchOperators = LogicalOperators &
|
|
69
|
-
ComparisonOperators &
|
|
70
|
-
TypeAndExistenceOperators &
|
|
71
|
-
ArrayOperators &
|
|
72
|
-
StringOperators &
|
|
73
|
-
OtherOperators;
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* SearchOptions can be either a function or an object with predefined operators.
|
|
77
|
-
*/
|
|
78
|
-
export type SearchOptions = PredefinedSearchOperators & Arg;
|
package/src/types/types.ts
DELETED
package/src/types/updater.ts
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Predefined type for updating data.
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
/** Arrays */
|
|
6
|
-
export type ArrayUpdater = {
|
|
7
|
-
$push?: any,
|
|
8
|
-
/** Pushes items into an array and removes duplicates */
|
|
9
|
-
$pushset?: any,
|
|
10
|
-
$pull?: any,
|
|
11
|
-
$pullall?: any,
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
/** Objects */
|
|
15
|
-
export type ObjectUpdater = {
|
|
16
|
-
$merge?: any,
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
/** Values */
|
|
20
|
-
export type ValueUpdater = {
|
|
21
|
-
$set?: any,
|
|
22
|
-
$inc?: any,
|
|
23
|
-
$dec?: any,
|
|
24
|
-
$unset?: any,
|
|
25
|
-
$rename?: any,
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
export type UpdaterArg =
|
|
30
|
-
ArrayUpdater &
|
|
31
|
-
ObjectUpdater &
|
|
32
|
-
ValueUpdater &
|
|
33
|
-
{ [key: string]: any };
|
package/src/types/valthera.ts
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import CollectionManager from "../helpers/CollectionManager";
|
|
2
|
-
import { Arg, Search, Updater } from "./arg";
|
|
3
|
-
import Data from "./data";
|
|
4
|
-
import { DbFindOpts, FindOpts } from "./options";
|
|
5
|
-
import { VQuery } from "./query";
|
|
6
|
-
import { VContext } from "./types";
|
|
7
|
-
|
|
8
|
-
export interface ValtheraCompatible {
|
|
9
|
-
c(collection: string): CollectionManager;
|
|
10
|
-
getCollections(): Promise<string[]>;
|
|
11
|
-
checkCollection(collection: string): Promise<boolean>;
|
|
12
|
-
issetCollection(collection: string): Promise<boolean>;
|
|
13
|
-
add<T = Data>(collection: string, data: Arg, id_gen?: boolean): Promise<T>;
|
|
14
|
-
find<T = Data>(collection: string, search: Search, context?: VContext, options?: DbFindOpts, findOpts?: FindOpts): Promise<T[]>;
|
|
15
|
-
findOne<T = Data>(collection: string, search: Search, context?: VContext, findOpts?: FindOpts): Promise<T | null>;
|
|
16
|
-
update(collection: string, search: Search, updater: Updater, context?: VContext): Promise<boolean>;
|
|
17
|
-
updateOne(collection: string, search: Search, updater: Updater, context?: VContext): Promise<boolean>;
|
|
18
|
-
remove(collection: string, search: Search, context?: VContext): Promise<boolean>;
|
|
19
|
-
removeOne(collection: string, search: Search, context?: VContext): Promise<boolean>;
|
|
20
|
-
removeCollection(collection: string): Promise<boolean>;
|
|
21
|
-
updateOneOrAdd(collection: string, search: Search, updater: Updater, add_arg?: Arg, context?: VContext, id_gen?: boolean): Promise<boolean>;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export interface ValtheraCompatibleInternal {
|
|
25
|
-
c(config: VQuery): CollectionManager;
|
|
26
|
-
getCollections(): Promise<string[]>;
|
|
27
|
-
checkCollection(config: VQuery): Promise<boolean>;
|
|
28
|
-
issetCollection(config: VQuery): Promise<boolean>;
|
|
29
|
-
add<T = Data>(config: VQuery): Promise<T>;
|
|
30
|
-
find<T = Data>(config: VQuery): Promise<T[]>;
|
|
31
|
-
findOne<T = Data>(config: VQuery): Promise<T | null>;
|
|
32
|
-
update(config: VQuery): Promise<boolean>;
|
|
33
|
-
updateOne(config: VQuery): Promise<boolean>;
|
|
34
|
-
remove(config: VQuery): Promise<boolean>;
|
|
35
|
-
removeOne(config: VQuery): Promise<boolean>;
|
|
36
|
-
removeCollection(config: VQuery): Promise<boolean>;
|
|
37
|
-
updateOneOrAdd(config: VQuery): Promise<boolean>;
|
|
38
|
-
}
|
package/src/utils/hasFields.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Checks if an object matches the standard field comparison.
|
|
3
|
-
* @param obj - The object to check.
|
|
4
|
-
* @param fields - Criteria to compare.
|
|
5
|
-
* @returns Whether the object matches the criteria.
|
|
6
|
-
*/
|
|
7
|
-
export default function hasFields(obj: Object, fields: Object): boolean {
|
|
8
|
-
const keys = Object.keys(fields);
|
|
9
|
-
return keys.every(key => {
|
|
10
|
-
if (obj[key] !== undefined) {
|
|
11
|
-
if (typeof fields[key] === "object" && fields[key] !== null) {
|
|
12
|
-
return hasFields(obj[key], fields[key]);
|
|
13
|
-
}
|
|
14
|
-
return obj[key] === fields[key];
|
|
15
|
-
}
|
|
16
|
-
return false;
|
|
17
|
-
});
|
|
18
|
-
}
|
|
@@ -1,181 +0,0 @@
|
|
|
1
|
-
import { Arg } from "../types/arg";
|
|
2
|
-
import hasFields from "./hasFields";
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Checks if an object meets the criteria specified in the fields with operators.
|
|
6
|
-
*/
|
|
7
|
-
export default function hasFieldsAdvanced(obj: Object, fields: Arg) {
|
|
8
|
-
if (typeof fields !== "object" || fields === null) {
|
|
9
|
-
throw new Error("Fields must be an object");
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
if ("$and" in fields) {
|
|
13
|
-
return fields["$and"].every(subFields => hasFieldsAdvanced(obj, subFields));
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
if ("$or" in fields) {
|
|
17
|
-
return fields["$or"].some(subFields => hasFieldsAdvanced(obj, subFields));
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
// Check various conditions
|
|
21
|
-
if (!checkConditions(obj, fields)) return false;
|
|
22
|
-
const fieldsSubset = removeAdvancedOperators({ ...fields });
|
|
23
|
-
return hasFields(obj, fieldsSubset);
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
function removeAdvancedOperators(fields: Object) {
|
|
27
|
-
const advancedOperators = [
|
|
28
|
-
"and", "or",
|
|
29
|
-
"gt", "lt", "gte", "lte", "in", "nin",
|
|
30
|
-
"exists",
|
|
31
|
-
"type",
|
|
32
|
-
"regex",
|
|
33
|
-
"arrinc", "arrincall", "size",
|
|
34
|
-
"startsWith", "endsWith",
|
|
35
|
-
"between",
|
|
36
|
-
"not",
|
|
37
|
-
"subset"
|
|
38
|
-
].map(operator => "$" + operator);
|
|
39
|
-
advancedOperators.forEach(operator => delete fields[operator]);
|
|
40
|
-
return fields;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
function checkConditions(obj: Object, fields: Object) {
|
|
44
|
-
return (
|
|
45
|
-
checkComparison(obj, fields) &&
|
|
46
|
-
checkExistence(obj, fields) &&
|
|
47
|
-
checkType(obj, fields) &&
|
|
48
|
-
checkRegex(obj, fields) &&
|
|
49
|
-
checkArrayConditions(obj, fields) &&
|
|
50
|
-
checkStringConditions(obj, fields) &&
|
|
51
|
-
checkBetween(obj, fields) &&
|
|
52
|
-
checkNot(obj, fields) &&
|
|
53
|
-
checkSubset(obj, fields)
|
|
54
|
-
);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
function checkComparison(obj: Object, fields: Object) {
|
|
58
|
-
const comparisonOperators = ["$gt", "$lt", "$gte", "$lte", "$in", "$nin"];
|
|
59
|
-
for (const operator of comparisonOperators) {
|
|
60
|
-
if (operator in fields) {
|
|
61
|
-
for (const entries of Object.entries(fields[operator])) {
|
|
62
|
-
const [key, value]: [string, any] = entries;
|
|
63
|
-
switch (operator) {
|
|
64
|
-
case "$gt":
|
|
65
|
-
if (!(obj[key] > value)) return false;
|
|
66
|
-
break;
|
|
67
|
-
case "$lt":
|
|
68
|
-
if (!(obj[key] < value)) return false;
|
|
69
|
-
break;
|
|
70
|
-
case "$gte":
|
|
71
|
-
if (!(obj[key] >= value)) return false;
|
|
72
|
-
break;
|
|
73
|
-
case "$lte":
|
|
74
|
-
if (!(obj[key] <= value)) return false;
|
|
75
|
-
break;
|
|
76
|
-
case "$in":
|
|
77
|
-
if (!value.includes(obj[key])) return false;
|
|
78
|
-
break;
|
|
79
|
-
case "$nin":
|
|
80
|
-
if (value.includes(obj[key])) return false;
|
|
81
|
-
break;
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
return true;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
function checkExistence(obj: Object, fields: Object) {
|
|
90
|
-
if ("$exists" in fields) {
|
|
91
|
-
for (const [key, shouldExist] of Object.entries(fields["$exists"])) {
|
|
92
|
-
if (shouldExist && !(key in obj)) return false;
|
|
93
|
-
if (!shouldExist && (key in obj)) return false;
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
return true;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
function checkType(obj: Object, fields: Object) {
|
|
100
|
-
if ("$type" in fields) {
|
|
101
|
-
for (const [key, type] of Object.entries(fields["$type"])) {
|
|
102
|
-
if (typeof obj[key] !== type) return false;
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
return true;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
function checkRegex(obj: Object, fields: Object) {
|
|
109
|
-
if ("$regex" in fields) {
|
|
110
|
-
for (const [key, regex] of Object.entries(fields["$regex"])) {
|
|
111
|
-
if (!regex.test(obj[key])) return false;
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
return true;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
function checkArrayConditions(obj: Object, fields: Object) {
|
|
118
|
-
if ("$arrinc" in fields) {
|
|
119
|
-
for (const [key, values] of Object.entries(fields["$arrinc"])) {
|
|
120
|
-
if (!Array.isArray(obj[key]) || !values.some(val => obj[key].includes(val))) return false;
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
if ("$arrincall" in fields) {
|
|
125
|
-
for (const [key, values] of Object.entries(fields["$arrincall"])) {
|
|
126
|
-
if (!Array.isArray(obj[key]) || !values.every(val => obj[key].includes(val))) return false;
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
if ("$size" in fields) {
|
|
131
|
-
for (const [key, size] of Object.entries(fields["$size"])) {
|
|
132
|
-
if (Array.isArray(obj[key]) || typeof obj[key] === "string") {
|
|
133
|
-
if (obj[key].length !== size) return false;
|
|
134
|
-
} else {
|
|
135
|
-
return false;
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
return true;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
function checkStringConditions(obj: Object, fields: Object) {
|
|
144
|
-
if ("$startsWith" in fields) {
|
|
145
|
-
for (const [key, value] of Object.entries(fields["$startsWith"])) {
|
|
146
|
-
if (typeof obj[key] !== "string" || !obj[key].startsWith(value)) return false;
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
if ("$endsWith" in fields) {
|
|
151
|
-
for (const [key, value] of Object.entries(fields["$endsWith"])) {
|
|
152
|
-
if (typeof obj[key] !== "string" || !obj[key].endsWith(value)) return false;
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
return true;
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
function checkBetween(obj: Object, fields: Object) {
|
|
160
|
-
if ("$between" in fields) {
|
|
161
|
-
for (const [key, [min, max]] of Object.entries(fields["$between"])) {
|
|
162
|
-
if (typeof obj[key] !== "number" || obj[key] < min || obj[key] > max) return false;
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
return true;
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
function checkNot(obj: Object, fields: Object) {
|
|
169
|
-
if ("$not" in fields) {
|
|
170
|
-
return !hasFieldsAdvanced(obj, fields["$not"]);
|
|
171
|
-
}
|
|
172
|
-
return true;
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
function checkSubset(obj: Object, fields: Object) {
|
|
176
|
-
if ("$subset" in fields) {
|
|
177
|
-
const setFields = fields["$subset"];
|
|
178
|
-
return hasFields(obj, setFields);
|
|
179
|
-
}
|
|
180
|
-
return true;
|
|
181
|
-
}
|
package/src/utils/sort.ts
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
export function compareSafe(a: any, b: any): -1 | 0 | 1 {
|
|
2
|
-
// Null/undefined at the end
|
|
3
|
-
if (a == null && b != null) return 1;
|
|
4
|
-
if (a != null && b == null) return -1;
|
|
5
|
-
if (a == null && b == null) return 0;
|
|
6
|
-
|
|
7
|
-
// Compare primitives and Date
|
|
8
|
-
const typeA = typeof a;
|
|
9
|
-
const typeB = typeof b;
|
|
10
|
-
|
|
11
|
-
if (typeA === "string" && typeB === "string") {
|
|
12
|
-
return a.localeCompare(b);
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
if ((typeA === "number" || a instanceof Date) && (typeB === "number" || b instanceof Date)) {
|
|
16
|
-
return (a < b) ? -1 : (a > b) ? 1 : 0;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
if (typeA === "boolean" && typeB === "boolean") {
|
|
20
|
-
return (a === b) ? 0 : a ? 1 : -1;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
// fallback: if both are objects/arrays or different types, consider equal
|
|
24
|
-
return 0;
|
|
25
|
-
}
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import { FindOpts } from "../types/options";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Updates an object with new values from a findOpts object.
|
|
5
|
-
* @param obj - The object to update.
|
|
6
|
-
* @param findOpts - An object containing options to update the target object.
|
|
7
|
-
* @param [findOpts.transform] - A function to transform the object before applying the other options.
|
|
8
|
-
* @param [findOpts.select] - An array of fields to select from the target object.
|
|
9
|
-
* @param [findOpts.exclude] - An array of fields to exclude from the target object.
|
|
10
|
-
* @returns The updated object.
|
|
11
|
-
*/
|
|
12
|
-
export default function updateFindObject(obj: Object, findOpts: FindOpts) {
|
|
13
|
-
const {
|
|
14
|
-
transform,
|
|
15
|
-
select,
|
|
16
|
-
exclude,
|
|
17
|
-
} = findOpts;
|
|
18
|
-
|
|
19
|
-
if (typeof transform === "function") obj = transform(obj);
|
|
20
|
-
|
|
21
|
-
if (Array.isArray(exclude)) {
|
|
22
|
-
exclude.forEach(field => {
|
|
23
|
-
if (obj.hasOwnProperty(field)) delete obj[field];
|
|
24
|
-
});
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
if (Array.isArray(select)) {
|
|
28
|
-
obj = select.reduce((acc, field) => {
|
|
29
|
-
if (obj.hasOwnProperty(field)) acc[field] = obj[field];
|
|
30
|
-
return acc;
|
|
31
|
-
}, {});
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
return obj;
|
|
35
|
-
}
|