-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathindex.d.ts
More file actions
916 lines (818 loc) · 23.7 KB
/
Copy pathindex.d.ts
File metadata and controls
916 lines (818 loc) · 23.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
declare module "@moleculer/database" {
import {
Context,
ServiceSchema,
ServiceMethods,
ServiceActions,
Service as MoleculerService,
ActionParams,
ActionHandler,
ServiceSettingSchema,
ServiceHooks,
Errors as MoleculerErrors,
CallingOptions,
ServiceBroker,
Logger,
Service
} from "moleculer";
// Extended Context with meta properties for common use cases
export interface DatabaseContext<TParams = Record<string, any>, TMeta = Record<string, any>>
extends Context<TParams, TMeta> {
meta: TMeta & {
tenantId?: string;
user?: {
id?: string;
[key: string]: any;
};
[key: string]: any;
};
}
// Re-export the main entity not found error
export class EntityNotFoundError extends MoleculerErrors.MoleculerClientError {
constructor(id: any);
}
export namespace Errors {
export class EntityNotFoundError extends MoleculerErrors.MoleculerClientError {
constructor(id: any);
}
}
// Field type definitions
export type FieldType =
| "any"
| "array"
| "boolean"
| "class"
| "currency"
| "custom"
| "date"
| "email"
| "enum"
| "equal"
| "forbidden"
| "function"
| "luhn"
| "mac"
| "multi"
| "number"
| "object"
| "record"
| "string"
| "tuple"
| "url"
| "uuid";
export type EntityChangedEventType = "broadcast" | "emit" | null;
// Hook function type for field lifecycle hooks
export type HookCustomFunctionArgument<TValue = any, TEntity = any> = {
ctx: Context<any, any>;
value: TValue;
params: any;
field: BaseFieldDefinition;
id?: any;
operation: "create" | "update" | "replace" | "remove";
entity?: TEntity;
root?: any;
};
export interface PopulateDefinition {
/** Service name for populating */
service?: string;
/** Action name for populating */
action?: string;
/** Params for populating action */
params?: Record<string, any>;
/** Handler function for populating */
handler?: (
ids: any[],
docs: any[],
rule: PopulateDefinition,
ctx: Context<any, any>
) => Promise<any>;
/** Key field name in the target service */
keyField?: string;
/** Populated field name in entity */
populateFields?: string | string[];
}
export interface BaseFieldDefinition {
/** Field type */
type?: FieldType | string;
/** Field is required */
required?: boolean;
/** Field is optional (inverse of required) */
optional?: boolean;
/** Column name in database */
columnName?: string;
/** Column type for SQL databases */
columnType?: string;
/** Primary key field */
primaryKey?: boolean;
/** Field is hidden from results */
hidden?: boolean | "byDefault";
/** Field is read-only */
readonly?: boolean;
/** Field is immutable after creation */
immutable?: boolean;
/** Default value */
default?: any | (() => Promise<any> | any);
/** Permission required for this field */
permission?: string | string[];
/** Read permission required for this field */
readPermission?: string | string[];
/** Virtual field (not stored in DB) */
virtual?: boolean;
/** Population configuration */
populate?:
| PopulateDefinition
| ((ctx: Context<any, any>, values: any[], docs: any[]) => Promise<any>);
/** Transformation function when getting value */
get?: (value: any, entity: any, field: BaseFieldDefinition, ctx: Context<any, any>) => any;
/** Transformation function when setting value */
set?: ((args: HookCustomFunctionArgument) => Promise<any> | any) | string;
/** Custom validation function */
validate?: ((args: HookCustomFunctionArgument) => boolean | string | Promise<boolean | string>) | string;
/** Lifecycle hook: called when entity is created */
onCreate?: ((args: HookCustomFunctionArgument) => Promise<any> | any) | string | any;
/** Lifecycle hook: called when entity is updated */
onUpdate?: ((args: HookCustomFunctionArgument) => Promise<any> | any) | string | any;
/** Lifecycle hook: called when entity is replaced */
onReplace?: ((args: HookCustomFunctionArgument) => Promise<any> | any) | string | any;
/** Lifecycle hook: called when entity is removed (enables soft delete) */
onRemove?: ((args: HookCustomFunctionArgument) => Promise<any> | any) | string | any;
}
export interface StringFieldDefinition extends BaseFieldDefinition {
type: "string";
/** Minimum length */
min?: number;
/** Maximum length */
max?: number;
/** Exact length */
length?: number;
/** Pattern to match */
pattern?: string | RegExp;
/** Whether to trim whitespace */
trim?: boolean;
/** Whether field is required to be empty */
empty?: boolean;
/** Enumeration of allowed values */
enum?: string[];
/** Lowercase transformation */
lowercase?: boolean;
/** Uppercase transformation */
uppercase?: boolean;
/** Convert to alphanumeric */
alphanum?: boolean;
/** Convert to alpha only */
alpha?: boolean;
/** Convert to numeric only */
numeric?: boolean;
/** Convert to base64 */
base64?: boolean;
/** Convert to hex */
hex?: boolean;
/** Starts with string */
startsWith?: string;
/** Ends with string */
endsWith?: string;
/** Contains string */
contains?: string | string[];
/** Convert value */
convert?: boolean;
}
export interface NumberFieldDefinition extends BaseFieldDefinition {
type: "number";
/** Minimum value */
min?: number;
/** Maximum value */
max?: number;
/** Equal to value */
equal?: number;
/** Not equal to value */
notEqual?: number;
/** Integer validation */
integer?: boolean;
/** Positive number validation */
positive?: boolean;
/** Negative number validation */
negative?: boolean;
/** Convert value */
convert?: boolean;
}
export interface BooleanFieldDefinition extends BaseFieldDefinition {
type: "boolean";
/** Convert value */
convert?: boolean;
}
export interface DateFieldDefinition extends BaseFieldDefinition {
type: "date";
/** Convert value */
convert?: boolean;
}
export interface ArrayFieldDefinition extends BaseFieldDefinition {
type: "array";
/** Minimum length */
min?: number;
/** Maximum length */
max?: number;
/** Exact length */
length?: number;
/** Whether array can be empty */
empty?: boolean;
/** Items validation schema */
items?: FieldDefinition | string;
/** Enumeration of allowed values */
enum?: any[];
/** Convert value */
convert?: boolean;
}
export interface ObjectFieldDefinition extends BaseFieldDefinition {
type: "object";
/** Strict mode - remove unknown properties */
strict?: boolean | "remove";
/** Object properties schema */
properties?: Record<string, FieldDefinition>;
/** Item properties for dynamic objects */
itemProperties?: FieldDefinition;
/** Convert value */
convert?: boolean;
}
export interface CustomFieldDefinition extends BaseFieldDefinition {
type: "custom";
/** Custom checker function */
check: (
value: any,
errors: any[],
schema: any,
name: string,
parent: any,
context: any
) => any;
}
export type FieldDefinition =
| StringFieldDefinition
| NumberFieldDefinition
| BooleanFieldDefinition
| DateFieldDefinition
| ArrayFieldDefinition
| ObjectFieldDefinition
| CustomFieldDefinition
| BaseFieldDefinition
| string
| boolean;
export interface Fields {
[fieldName: string]: FieldDefinition;
}
export interface Scopes {
[scopeName: string]:
| Record<string, any>
| ((
query: Record<string, any>,
ctx: Context<any, any>,
params: any
) => Record<string, any>);
}
export interface IndexDefinition {
/** Fields to index */
fields: Record<string, string> | string | string[];
/** Index name */
name?: string;
/** Index options */
unique?: boolean;
sparse?: boolean;
type?: string;
expireAfterSeconds?: number;
}
// Adapter interfaces
export interface BaseAdapterOptions {}
export interface MongoDBAdapterOptions extends BaseAdapterOptions {
/** MongoDB connection URI */
uri?: string;
/** Database name */
dbName?: string;
/** Collection name */
collection?: string;
/** MongoDB client options */
mongoClientOptions?: Record<string, any>;
/** Database options */
dbOptions?: Record<string, any>;
}
export interface KnexAdapterOptions extends BaseAdapterOptions {
/** Table name */
tableName?: string;
/** Database schema */
schema?: string;
/** Knex configuration */
knex?: Record<string, any>;
}
export interface NeDBAdapterOptions extends BaseAdapterOptions {
/** NeDB configuration */
neDB?: Record<string, any>;
}
export type AdapterDefinition =
| string
| {
type?: "NeDB";
options?: NeDBAdapterOptions | string;
}
| {
type?: "MongoDB";
options?: MongoDBAdapterOptions;
}
| {
type?: "Knex";
options?: KnexAdapterOptions;
}
| BaseAdapter;
// Database adapter base class
export abstract class BaseAdapter {
protected opts: BaseAdapterOptions;
protected service: Service;
protected logger: Logger;
protected broker: ServiceBroker;
protected Promise: PromiseConstructor;
constructor(opts?: BaseAdapterOptions);
/** Whether adapter supports nested fields */
get hasNestedFieldSupport(): boolean;
/** Initialize adapter */
init(service: any): void;
/** Check client library version */
checkClientLibVersion(library: string, requiredVersions: string): boolean;
/** Get client from global store */
getClientFromGlobalStore(key: string): any;
/** Set client to global store */
setClientToGlobalStore(key: string, client: any): void;
/** Remove adapter from client global store */
removeAdapterFromClientGlobalStore(key: string): boolean;
/** Connect to database */
connect(): Promise<void>;
/** Disconnect from database */
disconnect(): Promise<void>;
/** Find entities */
find<TEntity>(params: any): Promise<TEntity[]>;
/** Find entity */
findOne<TEntity>(params: any): Promise<TEntity>;
/** Find entity */
findById<TEntity>(id: any): Promise<TEntity>;
/** Find entities */
findByIds<TEntity>(id: any[]): Promise<TEntity[]>;
/** Stream entities */
findStream<TEntity>(params: any, opts?: any): TEntity;
/** Count entities */
count(params?: any): Promise<number>;
/** Insert entity */
insert<TEntity>(entity: TEntity, opts?: any): Promise<TEntity>;
/** Insert many entities */
insertMany<TEntity>(entities: TEntity[], opts?: any): Promise<TEntity[]>;
/** Update entity by ID */
updateById<TEntity>(id: any, update: any, opts?: any): Promise<TEntity>;
/** Update many entities */
updateMany(query: any, update: any, opts?: any): Promise<number>;
/** Replace entity by ID */
replaceById<TEntity>(id: any, entity: TEntity, opts?: any): Promise<TEntity>;
/** Remove entity by ID */
removeById<TEntity>(id: any, opts?: any): Promise<TEntity>;
/** Remove many entities */
removeMany(query: any, opts?: any): Promise<number>;
/** Clear all entities */
clear(opts?: any): Promise<number>;
/** Convert entity to JSON */
entityToJSON(entity: any): any;
/** Before save transformation */
beforeSave(entity: any, ctx: Context<any, any>): any;
/** After retrieve transformation */
afterRetrieve(entity: any): any;
/** Create table/collection */
createTable?(): Promise<void>;
}
// Specific adapter classes
export declare class MongoDBAdapter extends BaseAdapter {
constructor(opts?: MongoDBAdapterOptions);
}
export declare class KnexAdapter extends BaseAdapter {
constructor(opts?: KnexAdapterOptions);
}
export declare class NeDBAdapter extends BaseAdapter {
constructor(opts?: NeDBAdapterOptions);
}
export namespace Adapters {
export const Base: typeof BaseAdapter;
export const MongoDB: typeof MongoDBAdapter;
export const Knex: typeof KnexAdapter;
export const NeDB: typeof NeDBAdapter;
export function resolve(opt: AdapterDefinition): BaseAdapter;
export function register(name: string, adapter: typeof BaseAdapter): void;
}
// Mixin options interface
export interface DatabaseMixinOptions {
/** Adapter configuration */
adapter?: AdapterDefinition;
/** Whether to create CRUD actions */
createActions?:
| boolean
| Partial<
Record<
| "find"
| "list"
| "count"
| "get"
| "resolve"
| "create"
| "createMany"
| "update"
| "replace"
| "remove",
boolean
>
>;
/** Default visibility of generated actions */
actionVisibility?: "published" | "public" | "protected" | "private";
/** Generate params schema for actions from fields */
generateActionParams?: boolean;
/** Enable params conversion */
enableParamsConversion?: boolean;
/** Strict mode for validation */
strict?: boolean | "remove";
/** Caching configuration */
cache?: {
enabled?: boolean;
eventName?: string | false;
eventType?: "broadcast" | "emit";
cacheCleanOnDeps?: boolean | string[];
additionalKeys?: string[];
cacheCleaner?: (this: any) => Promise<void>;
};
/** Enable REST endpoints */
rest?: boolean;
/** Entity changed event type */
entityChangedEventType?: EntityChangedEventType;
/** Include old entity in changed events */
entityChangedOldEntity?: boolean;
/** Auto reconnect setting */
autoReconnect?: boolean;
/** Maximum number of adapters for multi-tenancy */
maximumAdapters?: number;
/** Maximum limit for find operations */
maxLimit?: number;
/** Default page size for list operations */
defaultPageSize?: number;
}
// Database service settings
export interface DatabaseServiceSettings extends ServiceSettingSchema {
/** Field definitions */
fields?: Fields;
/** Scope definitions */
scopes?: Scopes;
/** Default scopes applied to find & list actions */
defaultScopes?: string[];
/** Default populated fields */
defaultPopulates?: string[];
/** Index definitions */
indexes?: IndexDefinition[];
}
type ScopeParam = boolean | string | string[];
type PopulateParam = string | string[];
type FieldsParam = string | string[];
// Query parameters for database operations
export interface FindParams {
/** Maximum number of entities to return */
limit?: number | undefined;
/** Number of entities to skip */
offset?: number | undefined;
/** Fields to include in results */
fields?: FieldsParam;
/** Sort order */
sort?: string | string[] | Record<string, any>;
/** Search text */
search?: string;
/** Fields to search in */
searchFields?: string | string[];
/** Collation options */
collation?: Record<string, any>;
/** Scope to apply */
scope?: ScopeParam;
/** Fields to populate */
populate?: PopulateParam;
/** Custom query */
query?: Record<string, any> | string;
}
export interface ListParams extends FindParams {
/** Page number (starts from 1) */
page?: number;
/** Page size */
pageSize?: number;
}
export type GetParams<TIdField extends string = "id", TIdType = string> = {
/** Entity ID(s) */
[K in TIdField]: TIdType;
} & {
/** Fields to include in results */
fields?: FieldsParam;
/** Scope to apply */
scope?: ScopeParam;
/** Fields to populate */
populate?: PopulateParam;
};
export type ResolveParams<TIdField extends string = "id", TIdType = string> = {
/** Entity ID(s) */
[K in TIdField]: TIdType | TIdType[];
} & {
/** Fields to include in results */
fields?: FieldsParam;
/** Scope to apply */
scope?: ScopeParam;
/** Fields to populate */
populate?: PopulateParam;
/** Convert result array to object with ID as key */
mapping?: boolean;
/** Throw error if entity not found */
throwIfNotExist?: boolean;
/** Reorder results to match input order */
reorderResult?: boolean;
}
export interface ListResult<T = any> {
/** Result rows */
rows: T[];
/** Total count */
total: number;
/** Current page */
page: number;
/** Page size */
pageSize: number;
/** Total pages */
totalPages: number;
}
export interface FindOptions {
/** Enable transformation of results */
transform?: boolean;
}
export interface ResolveEntityOptions {
/** Enable transformation of results */
transform?: boolean;
/** Throw error if entity not found */
throwIfNotExist?: boolean;
/** Reorder results to match input order */
reorderResult?: boolean;
}
export interface CreateEntityOptions {
/** Enable transformation of results */
transform?: boolean;
/** Enable permissive updates */
permissive?: boolean;
}
export interface CreateEntitiesOptions extends CreateEntityOptions {
/** Return created entities */
returnEntities?: boolean;
}
export interface UpdateEntityOptions {
/** Return raw database response */
raw?: boolean;
/** Enable permissive updates */
permissive?: boolean;
/** Skip "onCreate", "onUpdate" and "onDelete" hooks */
skipOnHooks?: boolean;
/** Enable transformation of results */
transform?: boolean;
/** Scope to apply */
scope?: ScopeParam;
}
export interface UpdateEntitiesParams<T = any> {
/** Query to match entities */
query: Record<string, any>;
/** Fields to update */
changes: Partial<T>;
/** Scope to apply */
scope?: ScopeParam;
}
export interface UpdateEntityRawOptions<T = any> extends UpdateEntityOptions {
raw: true;
}
export interface RemoveEntityOptions {
/** Enable transformation of results */
transform?: boolean;
/** Scope to apply */
scope?: ScopeParam;
/** Enable soft delete */
softDelete?: boolean;
}
export interface RemoveEntitiesOptions {
/** Enable transformation of results */
transform?: boolean;
/** Enable soft delete */
softDelete?: boolean;
}
export interface RemoveEntitiesParams {
/** Query to match entities */
query: Record<string, any>;
/** Scope to apply */
scope?: ScopeParam;
}
export interface ValidateParamsOptions {
/** Type of validation */
type?: "create" | "update" | "replace";
/** Enable permissive updates */
permissive?: boolean;
/** Skip "onCreate", "onUpdate" and "onDelete" hooks */
skipOnHooks?: boolean;
}
export interface EntityChangedOptions {
/** It was a batch operation */
batch: boolean;
/** It's soft deleted */
softDelete: boolean;
}
export interface ListResult<T = any> {
/** Result rows */
rows: T[];
/** Total count */
total: number;
/** Current page */
page: number;
/** Page size */
pageSize: number;
/** Total pages */
totalPages: number;
}
// Database service methods
export interface DatabaseMethods {
// Adapter methods
getAdapter(ctx?: Context<any, any>): Promise<BaseAdapter>;
getAdapterByContext(ctx?: Context<any, any>, adapterDef?: any): [string, any];
maintenanceAdapters(): Promise<void>;
// Entity methods
findEntities<T = any>(
ctx: Context<any, any>,
params?: FindParams,
opts?: FindOptions
): Promise<T[]>;
countEntities(
ctx: Context<any, any>,
params?: Omit<FindParams, "limit" | "offset" | "fields" | "sort">
): Promise<number>;
findEntity<T = any>(
ctx: Context<any, any>,
params?: Omit<FindParams, "limit" | "offset">,
opts?: FindOptions
): Promise<T | null>;
resolveEntities<T = any, TIdField = "id", TIdType = string>(
ctx: Context<any, any>,
params: ResolveParams<TIdField, TIdType>,
opts?: ResolveEntityOptions
): Promise<T | T[] | Record<string, T>>;
createEntity<T = any>(
ctx: Context<any, any>,
params: Partial<T>,
opts?: CreateEntityOptions
): Promise<T>;
createEntities<T = any>(
ctx: Context<any, any>,
entities: Partial<T>[],
opts?: CreateEntitiesOptions
): Promise<T[] | number>;
updateEntity<T = any>(
ctx: Context<any, any>,
params: Partial<T>,
opts?: UpdateEntityOptions
): Promise<T>;
updateEntity<T = any>(
ctx: Context<any, any>,
params: any,
opts: UpdateEntityRawOptions
): Promise<T>;
updateEntities<T = any>(
ctx: Context<any, any>,
params: UpdateEntitiesParams<T>,
opts?: UpdateEntityOptions
): Promise<T[]>;
replaceEntity<T = any>(
ctx: Context<any, any>,
params: Partial<T>,
opts?: UpdateEntityOptions
): Promise<T>;
removeEntity(ctx: Context<any, any>, params: any, opts?: RemoveEntityOptions): Promise<any>;
removeEntities(
ctx: Context<any, any>,
params: RemoveEntitiesParams,
opts?: RemoveEntitiesOptions
): Promise<number>;
clearEntities(ctx?: Context<any, any>): Promise<number>;
streamEntities(ctx: Context<any, any>, params?: FindParams, opts?: FindOptions): any;
// Validation methods
validateParams<T = any>(
ctx: Context<any, any>,
params?: Partial<T>,
opts?: ValidateParamsOptions
): Promise<T>;
authorizeFields(
fields: any[],
ctx: Context<any, any>,
params: any,
opts?: any
): Promise<any[]>;
sanitizeParams(params: any, opts?: any): any;
// Transformation methods
transformResult<T = any>(
adapter: BaseAdapter | null,
docs: T | T[],
params?: Record<string, any>,
ctx?: Context<any, any>
): Promise<T | T[]>;
// Event methods
entityChanged<T = any>(
type: "create" | "update" | "replace" | "remove" | "clear",
data: T | T[],
oldData: T,
ctx: Context<any, any>,
opts?: EntityChangedOptions
): void;
// Encode & Decode ID
encodeID<TOld, TNew>(id: TOld): TNew;
decodeID<TOld, TNew>(id: TOld): TNew;
// Field authorization
checkFieldAuthority<T = any>(
ctx?: Context<any, any> | null,
permission: any,
params: T,
field: Record<string, any>
): Promise<boolean>;
checkScopeAuthority(
ctx?: Context<any, any> | null,
name: string,
operation: "add" | "remove",
scope: Record<string, any>
): Promise<boolean>;
// Monitoring
startSpan(ctx: Context<any, any>, name: string, tags?: Record<string, any>): any;
finishSpan(ctx: Context<any, any>, span: any, tags?: Record<string, any>): void;
}
// Database service actions
export interface DatabaseActions {
find?: ActionHandler<FindParams>;
list?: ActionHandler<ListParams>;
count?: ActionHandler<Omit<FindParams, "limit" | "offset" | "fields" | "sort">>;
get?: ActionHandler<GetParams>;
resolve?: ActionHandler<ResolveParams>;
create?: ActionHandler<any>;
createMany?: ActionHandler<any[]>;
update?: ActionHandler<any>;
replace?: ActionHandler<any>;
remove?: ActionHandler<any>;
}
// Database action handler with proper this context
export interface DatabaseActionHandler<TParams = any, TMeta = any> {
(this: DatabaseService, ctx: Context<TParams, TMeta>): Promise<any> | any;
}
// Database action definition
export interface DatabaseActionDef<TParams = any, TMeta = any> {
params?: ActionParams;
handler?: DatabaseActionHandler<TParams, TMeta>;
}
// Custom hooks interface
export interface DatabaseHooks extends ServiceHooks {
customs?: Record<string, Function | Function[] | string | string[]>;
}
export interface DatabaseLocalVariables {
// Adapter management
adapters: Map<string, any>;
// Field definitions
$fields: any[] | null;
$primaryField: { name: string; columnName: string } | null;
$softDelete: boolean;
$shouldAuthorizeFields: boolean;
$validators: {
create: Function;
update: Function;
replace: Function;
};
// Custom hooks
$hooks: Record<string, Function>;
}
// Main service factory function
export function Service(options?: DatabaseMixinOptions): ServiceSchema;
// Schema generation utilities
export function generateValidatorSchemaFromFields(
fields: Fields,
opts?: {
type?: "create" | "update" | "replace";
strict?: boolean | "remove";
enableParamsConversion?: boolean;
level?: number;
}
): Record<string, any>;
export function generateFieldValidatorSchema(
field: FieldDefinition,
opts?: {
type?: "create" | "update" | "replace";
strict?: boolean | "remove";
enableParamsConversion?: boolean;
level?: number;
}
): Record<string, any> | null;
// Default export is the Service factory function
declare const DatabaseModule: {
Service: ServiceSchema;
Adapters: typeof Adapters;
Errors: {
EntityNotFoundError: typeof EntityNotFoundError;
};
generateValidatorSchemaFromFields: typeof generateValidatorSchemaFromFields;
generateFieldValidatorSchema: typeof generateFieldValidatorSchema;
};
export default DatabaseModule;
}