@kava/kava-api-core 1.0.0

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 (59) hide show
  1. package/bun.lock +160 -0
  2. package/dist/auth.util.d.ts +23 -0
  3. package/dist/auth.util.js +175 -0
  4. package/dist/auth.util.js.map +1 -0
  5. package/dist/context.util.d.ts +7 -0
  6. package/dist/context.util.js +11 -0
  7. package/dist/context.util.js.map +1 -0
  8. package/dist/controller.util.d.ts +118 -0
  9. package/dist/controller.util.js +144 -0
  10. package/dist/controller.util.js.map +1 -0
  11. package/dist/conversion.util.d.ts +8 -0
  12. package/dist/conversion.util.js +52 -0
  13. package/dist/conversion.util.js.map +1 -0
  14. package/dist/db.util.d.ts +80 -0
  15. package/dist/db.util.js +166 -0
  16. package/dist/db.util.js.map +1 -0
  17. package/dist/index.d.ts +13 -0
  18. package/dist/index.js +14 -0
  19. package/dist/index.js.map +1 -0
  20. package/dist/logger.util.d.ts +30 -0
  21. package/dist/logger.util.js +117 -0
  22. package/dist/logger.util.js.map +1 -0
  23. package/dist/mail.util.d.ts +21 -0
  24. package/dist/mail.util.js +53 -0
  25. package/dist/mail.util.js.map +1 -0
  26. package/dist/middleware.util.d.ts +263 -0
  27. package/dist/middleware.util.js +233 -0
  28. package/dist/middleware.util.js.map +1 -0
  29. package/dist/model.util.d.ts +204 -0
  30. package/dist/model.util.js +1495 -0
  31. package/dist/model.util.js.map +1 -0
  32. package/dist/permission.util.d.ts +38 -0
  33. package/dist/permission.util.js +91 -0
  34. package/dist/permission.util.js.map +1 -0
  35. package/dist/route.util.d.ts +1 -0
  36. package/dist/route.util.js +12 -0
  37. package/dist/route.util.js.map +1 -0
  38. package/dist/storage.util.d.ts +56 -0
  39. package/dist/storage.util.js +82 -0
  40. package/dist/storage.util.js.map +1 -0
  41. package/dist/validation.util.d.ts +7 -0
  42. package/dist/validation.util.js +237 -0
  43. package/dist/validation.util.js.map +1 -0
  44. package/package.json +34 -0
  45. package/src/auth.util.ts +242 -0
  46. package/src/context.util.ts +17 -0
  47. package/src/controller.util.ts +237 -0
  48. package/src/conversion.util.ts +65 -0
  49. package/src/db.util.ts +405 -0
  50. package/src/index.ts +13 -0
  51. package/src/logger.util.ts +170 -0
  52. package/src/mail.util.ts +86 -0
  53. package/src/middleware.util.ts +289 -0
  54. package/src/model.util.ts +2211 -0
  55. package/src/permission.util.ts +136 -0
  56. package/src/route.util.ts +12 -0
  57. package/src/storage.util.ts +102 -0
  58. package/src/validation.util.ts +338 -0
  59. package/tsconfig.json +23 -0
@@ -0,0 +1,204 @@
1
+ import type { Knex } from 'knex';
2
+ export declare const PRIMARY_KEY_META: unique symbol;
3
+ export declare const FIELD_META: unique symbol;
4
+ export declare const RELATION_META: unique symbol;
5
+ export declare const SOFT_DELETE_META: unique symbol;
6
+ export declare const ATTRIBUTE_META: unique symbol;
7
+ export declare const FORMATTER_META: unique symbol;
8
+ export declare const SCOPE_META: unique symbol;
9
+ export type FieldFlag = 'fillable' | 'selectable' | 'searchable' | 'hidden';
10
+ export type FieldMeta = {
11
+ cast?: ModelCastType;
12
+ fillable?: boolean;
13
+ selectable?: boolean;
14
+ searchable?: boolean;
15
+ hidden?: boolean;
16
+ };
17
+ type NonFunctionKeys<T> = {
18
+ [K in keyof T]: T[K] extends Function ? never : K;
19
+ }[keyof T];
20
+ type DataShape<T> = Pick<T, NonFunctionKeys<T>>;
21
+ type ModelPayload<T> = Partial<DataShape<T>>;
22
+ export type ModelCastType = 'string' | 'number' | 'boolean' | 'date' | 'json';
23
+ export type ModelRelationType = 'hasMany' | 'hasOne' | 'belongsTo' | 'belongsToMany';
24
+ export type ModelRelationDescriptor = {
25
+ type: ModelRelationType;
26
+ model: () => typeof Model;
27
+ foreignKey: string;
28
+ localKey: string;
29
+ pivotTable?: string;
30
+ pivotLocal?: string;
31
+ pivotForeign?: string;
32
+ callback?: (q: any) => void;
33
+ };
34
+ export type ModelHookEventType = "before-create" | "after-create" | "before-update" | "after-update" | "before-delete" | "after-delete";
35
+ export type ModelHookFn = (ctx: ModelHookContextType) => any;
36
+ export type ModelHookContextType<T extends Model = Model> = {
37
+ model: T;
38
+ trx?: any;
39
+ snapshot?: any;
40
+ };
41
+ export type ScopeType = {
42
+ fn: Function;
43
+ mode: 'global' | 'internal';
44
+ };
45
+ declare module 'knex' {
46
+ namespace Knex {
47
+ interface QueryBuilder<TRecord = any, TResult = any> {
48
+ $model?: any;
49
+ _withTree?: Record<string, any>;
50
+ _formatter?: ((item: any) => any) | null;
51
+ _softDeleteScope?: 'default' | 'with' | 'only';
52
+ _withAggregates?: Array<{
53
+ relation: string;
54
+ alias: string;
55
+ fn: 'count' | 'sum' | 'avg' | 'min' | 'max';
56
+ column: string;
57
+ callback?: (q: any) => void;
58
+ }>;
59
+ _orderByAggregates?: Array<{
60
+ relation: string;
61
+ alias?: string;
62
+ fn: 'count' | 'sum' | 'avg' | 'min' | 'max';
63
+ column: string;
64
+ direction: 'asc' | 'desc';
65
+ callback?: (q: any) => void;
66
+ }>;
67
+ }
68
+ }
69
+ }
70
+ export interface ModelQueryBuilder<T extends Record<string, any> = Record<string, any>> extends Knex.QueryBuilder<T, T[]> {
71
+ _withTree?: Record<string, any>;
72
+ _formatter?: ((item: T) => any) | null;
73
+ _softDeleteScope?: 'default' | 'with' | 'only';
74
+ findOrNotFound(id: string | number): Promise<T>;
75
+ firstOrNotFound(): Promise<T>;
76
+ search(keyword?: string, options?: {
77
+ includes?: string[];
78
+ searchable?: string[];
79
+ }): this;
80
+ filter(filters?: Record<string, string>): this;
81
+ selects(options?: {
82
+ includes?: string[];
83
+ selectable?: string[];
84
+ }): this;
85
+ sorts(sorts?: string[]): this;
86
+ with(relation: string, callback?: any): this;
87
+ expand(relations?: Array<string | Record<string, (q: any) => void>>): this;
88
+ whereHas(relation: string, callback?: (q: ModelQueryBuilder<any>) => void): this;
89
+ orWhereHas(relation: string, callback?: (q: ModelQueryBuilder<any>) => void): this;
90
+ whereDoesntHave(relation: string, callback?: (q: ModelQueryBuilder<any>) => void): this;
91
+ orWhereDoesntHave(relation: string, callback?: (q: ModelQueryBuilder<any>) => void): this;
92
+ withAggregate(expr: string, fn: 'count' | 'sum' | 'avg' | 'min' | 'max', column?: string, callback?: (q: ModelQueryBuilder<any>) => void): this;
93
+ orderByAggregate(expr: string, fn: 'count' | 'sum' | 'avg' | 'min' | 'max', column?: string, direction?: 'asc' | 'desc', callback?: (q: ModelQueryBuilder<any>) => void): this;
94
+ get(): Promise<T[]>;
95
+ getFirst(): Promise<T>;
96
+ paginate(page?: number, limit?: number): Promise<{
97
+ data: T[];
98
+ total: number;
99
+ }>;
100
+ option(selectableOption?: string[]): Promise<Array<{
101
+ value: any;
102
+ label: any;
103
+ }>>;
104
+ paginateOrOption(page?: number, limit?: number, option?: string | boolean, selectableOption?: string[]): Promise<{
105
+ data: any[];
106
+ total: number;
107
+ }>;
108
+ resolve(input?: any): Promise<{
109
+ data: T[];
110
+ total: number;
111
+ }>;
112
+ format(formatter: string | ((item: T) => any)): this;
113
+ withTrashed(): this;
114
+ onlyTrashed(): this;
115
+ }
116
+ export declare abstract class Model {
117
+ id: number;
118
+ created_at: Date;
119
+ updated_at: Date;
120
+ deleted_at: Date;
121
+ static table: string;
122
+ static primaryKey: string;
123
+ static softDelete: boolean;
124
+ static deletedAtColumn: string;
125
+ protected _original: Record<string, any>;
126
+ protected _exists: boolean;
127
+ protected _trx?: Knex.Transaction;
128
+ private static _hooks;
129
+ private _instanceHooks;
130
+ private _disabledHooks;
131
+ constructor(data?: Record<string, any>);
132
+ protected static newInstance<T extends typeof Model>(this: T): InstanceType<T> & Model;
133
+ static [FIELD_META]?: Record<string, any>;
134
+ static getDefaultFields(): Record<string, FieldMeta>;
135
+ static get fields(): Record<string, FieldMeta>;
136
+ static get fillable(): string[];
137
+ static get selectable(): string[];
138
+ static get searchable(): string[];
139
+ static getTable(): string;
140
+ static getPrimaryKey(): string;
141
+ static [RELATION_META]?: Record<string, any>;
142
+ static get relations(): Record<string, any>;
143
+ static get attributes(): Record<string, Function>;
144
+ static get formatters(): Record<string, Function>;
145
+ static get scopes(): Record<string, ScopeType>;
146
+ getOriginal(key?: string): any;
147
+ getChanges(): Record<string, any>;
148
+ getPrevious(): Record<string, any>;
149
+ static query<T extends Record<string, any> = Record<string, any>>(trx?: Knex | Knex.Transaction): ModelQueryBuilder<T>;
150
+ castFromDB(row: Record<string, any>): this;
151
+ castToDB(): Record<string, any>;
152
+ getDirty(): Record<string, any>;
153
+ static hydrate<T extends typeof Model>(this: T, rows: any[] | null | undefined): InstanceType<T>[];
154
+ fill<T extends this>(this: T, payload: ModelPayload<T>): T;
155
+ static create<T extends typeof Model>(this: T, payload: ModelPayload<InstanceType<T>>, trx?: Knex.Transaction): Promise<InstanceType<T>>;
156
+ static update<T extends typeof Model>(this: T, payload: ModelPayload<InstanceType<T>>, uniqueKeys: (keyof InstanceType<T> & string)[], trx?: Knex.Transaction): Promise<InstanceType<T>>;
157
+ static upsert<T extends typeof Model>(this: T, payload: ModelPayload<InstanceType<T>>, uniqueKeys: (keyof InstanceType<T> & string)[], trx?: Knex.Transaction): Promise<InstanceType<T>>;
158
+ save(): Promise<this>;
159
+ pump<T extends this>(this: T, payload: ModelPayload<T> | ModelPayload<T>[], options?: {
160
+ trx?: Knex.Transaction;
161
+ }): Promise<T | T[]>;
162
+ static getSoftDeleteConfig(): any;
163
+ static isSoftDelete(): boolean;
164
+ static getDeletedAtColumn(): string | null;
165
+ delete(): Promise<Record<string, any> | null>;
166
+ forceDelete(): Promise<any>;
167
+ restore(): Promise<this>;
168
+ on<T extends this>(event: ModelHookEventType, fn: (ctx: ModelHookContextType<T>) => any): this;
169
+ off(event: ModelHookEventType): this;
170
+ protected runHook(event: ModelHookEventType, ctx: ModelHookContextType): Promise<void>;
171
+ toJSON(): Record<string, any>;
172
+ useTransaction(trx: Knex.Transaction): this;
173
+ }
174
+ export declare function extendModelQuery(query: Knex.QueryBuilder, Model: any): Knex.QueryBuilder<any, any>;
175
+ export declare function PrimaryKey(): (target: any, key: string) => void;
176
+ export declare function Field(defs: string[]): (target: any, key: string) => void;
177
+ export declare function HasMany(model: () => typeof Model, options?: {
178
+ foreignKey?: string;
179
+ localKey?: string;
180
+ callback?: (q: any) => void;
181
+ }): (target: any, key: string) => void;
182
+ export declare function HasOne(model: () => typeof Model, options?: {
183
+ foreignKey?: string;
184
+ localKey?: string;
185
+ callback?: (q: any) => void;
186
+ }): (target: any, key: string) => void;
187
+ export declare function BelongsTo(model: () => typeof Model, options?: {
188
+ foreignKey?: string;
189
+ ownerKey?: string;
190
+ callback?: (q: any) => void;
191
+ }): (target: any, key: string) => void;
192
+ export declare function BelongsToMany(model: () => typeof Model, options?: {
193
+ pivotTable?: string;
194
+ pivotLocal?: string;
195
+ pivotForeign?: string;
196
+ localKey?: string;
197
+ callback?: (q: any) => void;
198
+ }): (target: any, key: string) => void;
199
+ export declare function SoftDelete(): (target: any, propertyKey: string) => void;
200
+ export declare function Attribute(): (target: any, key: string, descriptor: PropertyDescriptor) => void;
201
+ export declare function Formatter(): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void;
202
+ export declare function Scope(mode?: 'global' | 'internal'): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void;
203
+ export declare function On(event: ModelHookEventType): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void;
204
+ export {};