coll-fns 1.4.1 → 1.4.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.
Files changed (2) hide show
  1. package/package.json +7 -4
  2. package/src/index.d.ts +222 -0
package/package.json CHANGED
@@ -1,20 +1,23 @@
1
1
  {
2
2
  "name": "coll-fns",
3
- "version": "1.4.1",
3
+ "version": "1.4.2",
4
4
  "type": "module",
5
5
  "description": "A functional interface to join MongoDB collections and add hooks before and after insertions, updates and removals.",
6
6
  "scripts": {
7
- "build": "microbundle -f esm,cjs",
8
- "dev": "microbundle watch -f esm,cjs"
7
+ "build": "microbundle -f esm,cjs --generateTypes false",
8
+ "dev": "microbundle watch -f esm,cjs --generateTypes false"
9
9
  },
10
10
  "files": [
11
- "dist"
11
+ "dist",
12
+ "src/index.d.ts"
12
13
  ],
13
14
  "source": "src/index.js",
14
15
  "main": "./dist/coll-fns.cjs",
15
16
  "module": "./dist/coll-fns.mjs",
17
+ "types": "./src/index.d.ts",
16
18
  "exports": {
17
19
  ".": {
20
+ "types": "./src/index.d.ts",
18
21
  "import": "./dist/coll-fns.mjs",
19
22
  "require": "./dist/coll-fns.cjs"
20
23
  }
package/src/index.d.ts ADDED
@@ -0,0 +1,222 @@
1
+ export type AnyObject = Record<string, any>;
2
+ export type MaybePromise<T> = T | Promise<T>;
3
+ export type Selector = AnyObject | string;
4
+
5
+ export type FieldValue = 0 | 1 | boolean | number | FieldSpec;
6
+ export interface FieldSpec {
7
+ [key: string]: FieldValue;
8
+ }
9
+
10
+ export interface FetchOptions<TDoc = AnyObject> extends AnyObject {
11
+ fields?: FieldSpec;
12
+ sort?: AnyObject;
13
+ limit?: number;
14
+ skip?: number;
15
+ transform?: ((doc: TDoc) => any) | null;
16
+ }
17
+
18
+ export interface Protocol<TColl = any, TDoc = AnyObject> {
19
+ count: (
20
+ Coll: TColl,
21
+ selector?: AnyObject,
22
+ options?: AnyObject
23
+ ) => MaybePromise<number>;
24
+ findList: (
25
+ Coll: TColl,
26
+ selector?: AnyObject,
27
+ options?: AnyObject
28
+ ) => MaybePromise<TDoc[]>;
29
+ getName?: (Coll: TColl) => string;
30
+ getTransform?: (Coll: TColl) => ((doc: TDoc) => any) | undefined;
31
+ insert: (
32
+ Coll: TColl,
33
+ doc: AnyObject,
34
+ options?: AnyObject
35
+ ) => MaybePromise<any>;
36
+ remove: (
37
+ Coll: TColl,
38
+ selector: AnyObject,
39
+ options?: AnyObject
40
+ ) => MaybePromise<number>;
41
+ update: (
42
+ Coll: TColl,
43
+ selector: AnyObject,
44
+ modifier: AnyObject,
45
+ options?: AnyObject
46
+ ) => MaybePromise<number>;
47
+ }
48
+
49
+ export type JoinArrayProp = string | [string];
50
+ export type JoinArrayOn =
51
+ | [JoinArrayProp, JoinArrayProp]
52
+ | [JoinArrayProp, JoinArrayProp, AnyObject];
53
+ export type JoinFunctionOn<TParent = AnyObject> = (doc: TParent) => AnyObject;
54
+ export type JoinObjectOn = AnyObject;
55
+ export type JoinOn<TParent = AnyObject> =
56
+ | JoinArrayOn
57
+ | JoinFunctionOn<TParent>
58
+ | JoinObjectOn;
59
+
60
+ export interface JoinDef<TParent = AnyObject> extends AnyObject {
61
+ Coll: any;
62
+ on: JoinOn<TParent>;
63
+ single?: boolean;
64
+ postFetch?: (joined: any[] | any, parent: TParent) => any;
65
+ fields?: FieldSpec;
66
+ limit?: number;
67
+ }
68
+
69
+ export type HookType =
70
+ | "beforeInsert"
71
+ | "beforeUpdate"
72
+ | "beforeRemove"
73
+ | "onInserted"
74
+ | "onUpdated"
75
+ | "onRemoved";
76
+
77
+ export type HookFn = (...args: any[]) => any | Promise<any>;
78
+ export type HookUnlessPredicate = (...args: any[]) => boolean;
79
+ export type HookWhenPredicate = (...args: any[]) => boolean;
80
+
81
+ export interface HookDef {
82
+ before?: boolean;
83
+ fields?: FieldSpec | true;
84
+ fn: HookFn;
85
+ onError?: (err: Error, hookDef: EnhancedHookDef) => void;
86
+ unless?: HookUnlessPredicate;
87
+ when?: HookWhenPredicate;
88
+ }
89
+
90
+ export interface EnhancedHookDef extends HookDef {
91
+ Coll: any;
92
+ collName: string;
93
+ hookType: HookType;
94
+ onError: (err: Error, hookDef: EnhancedHookDef) => void;
95
+ }
96
+
97
+ export interface PoolCall {
98
+ _id: symbol;
99
+ fn: (...args: any[]) => any;
100
+ args?: any[];
101
+ }
102
+
103
+ export type PoolOverflow =
104
+ | "drop"
105
+ | "shift"
106
+ | ((pendings: PoolCall[], call: PoolCall) => PoolCall[] | undefined);
107
+
108
+ export interface PoolConfig {
109
+ maxConcurrent?: number;
110
+ maxPending?: number;
111
+ onError?: (error: unknown, call: PoolCall) => void;
112
+ onOverflow?: PoolOverflow;
113
+ }
114
+
115
+ export interface SoftRemoveRegistrationOptions<TDoc = AnyObject> {
116
+ when?: (doc: TDoc) => boolean | Promise<boolean>;
117
+ docToCollSelectorPairs?: (
118
+ doc: TDoc
119
+ ) =>
120
+ | Array<[any, AnyObject | string]>
121
+ | Promise<Array<[any, AnyObject | string]>>;
122
+ fields?: FieldSpec;
123
+ keepModifier?:
124
+ | AnyObject
125
+ | (() => AnyObject | Promise<AnyObject>)
126
+ | null
127
+ | undefined;
128
+ }
129
+
130
+ export interface SoftRemoveOptions {
131
+ detailed?: boolean;
132
+ }
133
+
134
+ export function count<TColl>(
135
+ Coll: TColl,
136
+ selector: AnyObject,
137
+ options?: AnyObject
138
+ ): MaybePromise<number>;
139
+
140
+ export function fetchList<TColl, TDoc = AnyObject>(
141
+ Coll: TColl,
142
+ selector?: AnyObject,
143
+ options?: FetchOptions<TDoc>
144
+ ): MaybePromise<TDoc[]>;
145
+
146
+ export function fetchOne<TColl, TDoc = AnyObject>(
147
+ Coll: TColl,
148
+ selector: AnyObject,
149
+ options?: FetchOptions<TDoc>
150
+ ): MaybePromise<TDoc | undefined>;
151
+
152
+ export function fetchIds<TColl, TId = string>(
153
+ Coll: TColl,
154
+ selector: AnyObject,
155
+ options?: FetchOptions
156
+ ): MaybePromise<TId[]>;
157
+
158
+ export function exists<TColl>(
159
+ Coll: TColl,
160
+ selector: AnyObject
161
+ ): MaybePromise<boolean>;
162
+
163
+ export function flattenFields(
164
+ fields?: FieldSpec,
165
+ root?: string
166
+ ): Record<string, boolean> | FieldSpec | undefined;
167
+
168
+ export function hook<TColl>(
169
+ Coll: TColl,
170
+ hooksObj: Partial<Record<HookType, HookDef[]>>
171
+ ): void;
172
+
173
+ export function insert<TColl>(Coll: TColl, doc: AnyObject): MaybePromise<any>;
174
+
175
+ export function join<TColl>(
176
+ Collection: TColl,
177
+ joins?: Record<string, JoinDef> | null | undefined | false
178
+ ): void;
179
+
180
+ export function getJoins<TColl>(Coll: TColl): Record<string, JoinDef>;
181
+ export function getJoinPrefix(): string | null;
182
+ export function setJoinPrefix(prefix: string | null): void;
183
+
184
+ export function remove<TColl>(
185
+ Coll: TColl,
186
+ selector: AnyObject
187
+ ): MaybePromise<number>;
188
+
189
+ export function configurePool(config?: PoolConfig): void;
190
+
191
+ export function setProtocol<TColl = any, TDoc = AnyObject>(
192
+ methods?: Partial<Protocol<TColl, TDoc>>
193
+ ): void;
194
+
195
+ export function registerSoftRemove<TColl, TDoc = AnyObject>(
196
+ Coll: TColl,
197
+ options?: SoftRemoveRegistrationOptions<TDoc>
198
+ ): void;
199
+
200
+ export function softRemove<TColl>(
201
+ Coll: TColl,
202
+ selector?: Selector,
203
+ keepModifier?:
204
+ | AnyObject
205
+ | (() => AnyObject | Promise<AnyObject>)
206
+ | null
207
+ | undefined,
208
+ options?: SoftRemoveOptions
209
+ ): MaybePromise<number | { removed: number; updated: number | null }>;
210
+
211
+ export function update<TColl>(
212
+ Coll: TColl,
213
+ selector: AnyObject,
214
+ modifier: AnyObject,
215
+ options?: AnyObject
216
+ ): MaybePromise<number>;
217
+
218
+ export const protocols: {
219
+ meteorAsync: Protocol;
220
+ meteorSync: Protocol;
221
+ node: Protocol;
222
+ };