@akanjs/document 0.9.41 → 0.9.43

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.
@@ -88,7 +88,6 @@ class DatabaseModelStorage {
88
88
  __ModelType__ = "DatabaseModelStorage";
89
89
  }
90
90
  const databaseModelOf = (database, model, redis, meili) => {
91
- //! 잘되는지 확인 필요
92
91
  const [modelName, className] = [database.refName, (0, import_common.capitalize)(database.refName)];
93
92
  const insightFieldMetas = (0, import_constant.getFieldMetas)(database.Insight);
94
93
  const accumulator = Object.fromEntries(insightFieldMetas.map((fieldMeta) => [fieldMeta.key, fieldMeta.accumulate]));
@@ -182,15 +181,18 @@ const databaseModelOf = (database, model, redis, meili) => {
182
181
  const data = res[0];
183
182
  return data ?? defaultInsight;
184
183
  }
185
- async getDefaultSummary(addQuery = {}) {
184
+ async _preSummary() {
185
+ return Promise.resolve({});
186
+ }
187
+ async getSummary() {
186
188
  if (!database.Summary)
187
189
  return {};
190
+ const summary = await this._preSummary();
188
191
  const fieldMetas = (0, import_constant.getFieldMetas)(database.Summary);
189
- const summary = {};
190
192
  await Promise.all(
191
193
  fieldMetas.filter((fieldMeta) => !!fieldMeta.query).map(async (fieldMeta) => {
192
194
  const query = typeof fieldMeta.query === "function" ? fieldMeta.query() : fieldMeta.query;
193
- summary[fieldMeta.key] = query ? await model.countDocuments({ ...query, ...addQuery }) : fieldMeta.default;
195
+ summary[fieldMeta.key] = query ? await model.countDocuments({ ...query }) : fieldMeta.default;
194
196
  })
195
197
  );
196
198
  return summary;
@@ -72,7 +72,6 @@ class DatabaseModelStorage {
72
72
  __ModelType__ = "DatabaseModelStorage";
73
73
  }
74
74
  const databaseModelOf = (database, model, redis, meili) => {
75
- //! 잘되는지 확인 필요
76
75
  const [modelName, className] = [database.refName, capitalize(database.refName)];
77
76
  const insightFieldMetas = getFieldMetas(database.Insight);
78
77
  const accumulator = Object.fromEntries(insightFieldMetas.map((fieldMeta) => [fieldMeta.key, fieldMeta.accumulate]));
@@ -166,15 +165,18 @@ const databaseModelOf = (database, model, redis, meili) => {
166
165
  const data = res[0];
167
166
  return data ?? defaultInsight;
168
167
  }
169
- async getDefaultSummary(addQuery = {}) {
168
+ async _preSummary() {
169
+ return Promise.resolve({});
170
+ }
171
+ async getSummary() {
170
172
  if (!database.Summary)
171
173
  return {};
174
+ const summary = await this._preSummary();
172
175
  const fieldMetas = getFieldMetas(database.Summary);
173
- const summary = {};
174
176
  await Promise.all(
175
177
  fieldMetas.filter((fieldMeta) => !!fieldMeta.query).map(async (fieldMeta) => {
176
178
  const query = typeof fieldMeta.query === "function" ? fieldMeta.query() : fieldMeta.query;
177
- summary[fieldMeta.key] = query ? await model.countDocuments({ ...query, ...addQuery }) : fieldMeta.default;
179
+ summary[fieldMeta.key] = query ? await model.countDocuments({ ...query }) : fieldMeta.default;
178
180
  })
179
181
  );
180
182
  return summary;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@akanjs/document",
3
- "version": "0.9.41",
3
+ "version": "0.9.43",
4
4
  "sourceType": "module",
5
5
  "publishConfig": {
6
6
  "access": "public"
package/src/database.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { type Dayjs } from "@akanjs/base";
1
+ import { type Dayjs, type MergedValues } from "@akanjs/base";
2
2
  import { Logger } from "@akanjs/common";
3
3
  import { type DocumentModel, type FilterType, type FindQueryOption, type GetActionObject, type ListQueryOption, type QueryOf, type SortOf } from "@akanjs/constant";
4
4
  import type DataLoader from "dataloader";
@@ -41,25 +41,53 @@ declare class SearchDatabase<T = any> {
41
41
  sort?: string | null;
42
42
  }): Promise<number>;
43
43
  }
44
- export type DatabaseModel<T extends string, Input, Doc, Obj, Insight, Filter, Summary = any> = DatabaseModelWithQuerySort<T, Input, Doc, Obj, Insight, GetActionObject<Filter>, SortOf<Filter>, Summary>;
45
- type DatabaseModelWithQuerySort<T extends string, Input, Doc, Obj, Insight, Query, Sort, Summary = any> = {
44
+ type QueryMethodOfKey<CapitalizedK extends string, Doc, Insight, _Args extends any[], _ListArgs extends any[], _FindArgs extends any[]> = {
45
+ [K in `list${CapitalizedK}`]: (...args: _ListArgs) => Promise<Doc[]>;
46
+ } & {
47
+ [K in `listIds${CapitalizedK}`]: (...args: _ListArgs) => Promise<string[]>;
48
+ } & {
49
+ [K in `find${CapitalizedK}`]: (...args: _FindArgs) => Promise<Doc | null>;
50
+ } & {
51
+ [K in `findId${CapitalizedK}`]: (...args: _FindArgs) => Promise<string | null>;
52
+ } & {
53
+ [K in `pick${CapitalizedK}`]: (...args: _FindArgs) => Promise<Doc>;
54
+ } & {
55
+ [K in `pickId${CapitalizedK}`]: (...args: _FindArgs) => Promise<string>;
56
+ } & {
57
+ [K in `exists${CapitalizedK}`]: (...args: _Args) => Promise<string | null>;
58
+ } & {
59
+ [K in `count${CapitalizedK}`]: (...args: _Args) => Promise<number>;
60
+ } & {
61
+ [K in `insight${CapitalizedK}`]: (...args: _Args) => Promise<Insight>;
62
+ };
63
+ export type QueryMethodPart<Query, Sort, Obj, Doc, Insight, _FindQueryOption = FindQueryOption<Sort, Obj>, _ListQueryOption = ListQueryOption<Sort, Obj>> = MergedValues<{
64
+ [K in keyof Query]: K extends string ? Query[K] extends (...args: infer Args) => any ? QueryMethodOfKey<Capitalize<K>, Doc, Insight, Args, [
65
+ ...Args,
66
+ queryOption?: _ListQueryOption
67
+ ], [
68
+ ...Args,
69
+ queryOption?: _FindQueryOption
70
+ ]> : never : never;
71
+ }>;
72
+ type DatabaseModelWithQuerySort<T extends string, Input, Doc, Obj, Insight, Query, Sort, Summary = any, _CapitalizedT extends string = Capitalize<T>, _QueryOfDoc = QueryOf<Doc>, _DataInput = DataInputOf<Input, DocumentModel<Obj>>, _FindQueryOption = FindQueryOption<Sort, Obj>, _ListQueryOption = ListQueryOption<Sort, Obj>> = {
46
73
  logger: Logger;
47
74
  __model: Mdl<Doc, Obj>;
48
75
  __cache: CacheDatabase<T>;
49
76
  __search: SearchDatabase<T>;
50
77
  __loader: DataLoader<string, Doc, string>;
51
- __list(query?: QueryOf<Doc>, queryOption?: ListQueryOption<Sort, Obj>): Promise<Doc[]>;
52
- __listIds(query?: QueryOf<Doc>, queryOption?: ListQueryOption<Sort, Obj>): Promise<string[]>;
53
- __find(query?: QueryOf<Doc>, queryOption?: FindQueryOption<Sort, Obj>): Promise<Doc | null>;
54
- __findId(query?: QueryOf<Doc>, queryOption?: FindQueryOption<Sort, Obj>): Promise<string | null>;
55
- __pick(query?: QueryOf<Doc>, queryOption?: FindQueryOption<Sort, Obj>): Promise<Doc>;
56
- __pickId(query?: QueryOf<Doc>, queryOption?: FindQueryOption<Sort, Obj>): Promise<string>;
57
- __exists(query?: QueryOf<Doc>): Promise<string | null>;
58
- __count(query?: QueryOf<Doc>): Promise<number>;
59
- __insight(query?: QueryOf<Doc>): Promise<Insight>;
60
- getDefaultSummary(addQuery?: QueryOf<Doc>): Promise<Summary>;
61
- } & {
62
- [key in Capitalize<T>]: Mdl<Doc, Obj>;
78
+ __list(query: _QueryOfDoc, queryOption?: _ListQueryOption): Promise<Doc[]>;
79
+ __listIds(query: _QueryOfDoc, queryOption?: _ListQueryOption): Promise<string[]>;
80
+ __find(query: _QueryOfDoc, queryOption?: _FindQueryOption): Promise<Doc | null>;
81
+ __findId(query: _QueryOfDoc, queryOption?: _FindQueryOption): Promise<string | null>;
82
+ __pick(query: _QueryOfDoc, queryOption?: _FindQueryOption): Promise<Doc>;
83
+ __pickId(query: _QueryOfDoc, queryOption?: _FindQueryOption): Promise<string>;
84
+ __exists(query: _QueryOfDoc): Promise<string | null>;
85
+ __count(query: _QueryOfDoc): Promise<number>;
86
+ __insight(query: _QueryOfDoc): Promise<Insight>;
87
+ _preSummary(): Promise<Partial<Summary>>;
88
+ getSummary(): Promise<Summary>;
89
+ } & {
90
+ [key in _CapitalizedT]: Mdl<Doc, Obj>;
63
91
  } & {
64
92
  [key in `${T}Loader`]: DataLoader<string, Doc, string>;
65
93
  } & {
@@ -67,44 +95,27 @@ type DatabaseModelWithQuerySort<T extends string, Input, Doc, Obj, Insight, Quer
67
95
  } & {
68
96
  [key in `${T}Search`]: SearchDatabase<T>;
69
97
  } & {
70
- [K in `get${Capitalize<T>}`]: (id: string) => Promise<Doc>;
98
+ [K in `get${_CapitalizedT}`]: (id: string) => Promise<Doc>;
71
99
  } & {
72
- [K in `load${Capitalize<T>}`]: (id?: string) => Promise<Doc | null>;
100
+ [K in `load${_CapitalizedT}`]: (id?: string) => Promise<Doc | null>;
73
101
  } & {
74
- [K in `load${Capitalize<T>}Many`]: (ids: string) => Promise<Doc[]>;
102
+ [K in `load${_CapitalizedT}Many`]: (ids: string[]) => Promise<Doc[]>;
75
103
  } & {
76
- [K in `create${Capitalize<T>}`]: (data: DataInputOf<Input, DocumentModel<Obj>>) => Promise<Doc>;
104
+ [K in `create${_CapitalizedT}`]: (data: _DataInput) => Promise<Doc>;
77
105
  } & {
78
- [K in `update${Capitalize<T>}`]: (id: string, data: DataInputOf<Input, DocumentModel<Obj>>) => Promise<Doc>;
106
+ [K in `update${_CapitalizedT}`]: (id: string, data: _DataInput) => Promise<Doc>;
79
107
  } & {
80
- [K in `remove${Capitalize<T>}`]: (id: string) => Promise<Doc>;
108
+ [K in `remove${_CapitalizedT}`]: (id: string) => Promise<Doc>;
81
109
  } & {
82
- [K in `search${Capitalize<T>}`]: (searchText: string, queryOption?: ListQueryOption<Sort, Obj>) => Promise<{
110
+ [K in `search${_CapitalizedT}`]: (searchText: string, queryOption?: _ListQueryOption) => Promise<{
83
111
  docs: Doc[];
84
112
  count: number;
85
113
  }>;
86
114
  } & {
87
- [K in `searchDocs${Capitalize<T>}`]: (searchText: string, queryOption?: ListQueryOption<Sort, Obj>) => Promise<Doc[]>;
88
- } & {
89
- [K in `searchCount${Capitalize<T>}`]: (searchText: string) => Promise<number>;
115
+ [K in `searchDocs${_CapitalizedT}`]: (searchText: string, queryOption?: _ListQueryOption) => Promise<Doc[]>;
90
116
  } & {
91
- [K in keyof Query as K extends string ? `list${Capitalize<K>}` : never]: Query[K] extends (...args: infer Args) => any ? (...args: [...Args, queryOption?: ListQueryOption<Sort, Obj>]) => Promise<Doc[]> : never;
92
- } & {
93
- [K in keyof Query as K extends string ? `listIds${Capitalize<K>}` : never]: Query[K] extends (...args: infer Args) => any ? (...args: [...Args, queryOption?: ListQueryOption<Sort, Obj>]) => Promise<string[]> : never;
94
- } & {
95
- [K in keyof Query as K extends string ? `find${Capitalize<K>}` : never]: Query[K] extends (...args: infer Args) => any ? (...args: [...Args, queryOption?: FindQueryOption<Sort, Obj>]) => Promise<Doc | null> : never;
96
- } & {
97
- [K in keyof Query as K extends string ? `findId${Capitalize<K>}` : never]: Query[K] extends (...args: infer Args) => any ? (...args: [...Args, queryOption?: FindQueryOption<Sort, Obj>]) => Promise<string | null> : never;
98
- } & {
99
- [K in keyof Query as K extends string ? `pick${Capitalize<K>}` : never]: Query[K] extends (...args: infer Args) => any ? (...args: [...Args, queryOption?: FindQueryOption<Sort, Obj>]) => Promise<Doc> : never;
100
- } & {
101
- [K in keyof Query as K extends string ? `pickId${Capitalize<K>}` : never]: Query[K] extends (...args: infer Args) => any ? (...args: [...Args, queryOption?: FindQueryOption<Sort, Obj>]) => Promise<string> : never;
102
- } & {
103
- [K in keyof Query as K extends string ? `exists${Capitalize<K>}` : never]: Query[K] extends (...args: infer Args) => any ? (...args: [...Args]) => Promise<string | null> : never;
104
- } & {
105
- [K in keyof Query as K extends string ? `count${Capitalize<K>}` : never]: Query[K] extends (...args: infer Args) => any ? (...args: [...Args]) => Promise<number> : never;
106
- } & {
107
- [K in keyof Query as K extends string ? `insight${Capitalize<K>}` : never]: Query[K] extends (...args: infer Args) => any ? (...args: [...Args]) => Promise<Insight> : never;
108
- };
117
+ [K in `searchCount${_CapitalizedT}`]: (searchText: string) => Promise<number>;
118
+ } & QueryMethodPart<Query, Sort, Obj, Doc, Insight, _FindQueryOption, _ListQueryOption>;
119
+ export type DatabaseModel<T extends string, Input, Doc, Obj, Insight, Filter, Summary = any, _CapitalizedT extends string = Capitalize<T>, _QueryOfDoc = QueryOf<Doc>, _Query = GetActionObject<Filter>, _Sort = SortOf<Filter>, _DataInput = DataInputOf<Input, DocumentModel<Obj>>, _FindQueryOption = FindQueryOption<_Sort, Obj>, _ListQueryOption = ListQueryOption<_Sort, Obj>> = DatabaseModelWithQuerySort<T, Input, Doc, Obj, Insight, _Query, _Sort, Summary, _CapitalizedT, _QueryOfDoc, _DataInput, _FindQueryOption, _ListQueryOption>;
109
120
  export declare const databaseModelOf: <T extends string, Input, Doc extends HydratedDocument<any>, Model extends Mdl<any, any>, Middleware extends BaseMiddleware, Insight, Obj, Filter extends FilterType, Summary>(database: Database<T, Input, Doc, Model, Middleware, Insight, Obj, Filter, Summary>, model: Mdl<any, any>, redis: RedisClientType, meili: MeiliSearch) => DatabaseModel<T, Input, Doc, Model, Insight, Filter, Summary>;
110
121
  export {};
@@ -12,16 +12,16 @@ type HydratedDocumentWithId<TDocument> = Omit<HydratedDocument<TDocument, Defaul
12
12
  id: string;
13
13
  } & DefaultDocMtds<TDocument>;
14
14
  export type Doc<M> = HydratedDocumentWithId<DocumentModel<M>>;
15
- interface DefaultMdlStats<TDocument, TSchema> {
16
- pickOneAndWrite: (query: FilterQuery<TSchema>, rawData: Partial<TSchema>) => Promise<TDocument>;
17
- pickAndWrite: (docId: string, rawData: Partial<TSchema>) => Promise<TDocument>;
18
- pickOne: (query: FilterQuery<TSchema>, projection?: ProjectionType<TSchema>) => Promise<TDocument>;
19
- pickById: (docId: string | undefined, projection?: ProjectionType<TSchema>) => Promise<TDocument>;
15
+ interface DefaultMdlStats<TDocument, TSchema, _Partial extends Partial<TSchema> = Partial<TSchema>, _FilterQuery extends FilterQuery<TSchema> = FilterQuery<TSchema>, _Projection extends ProjectionType<TSchema> = ProjectionType<TSchema>> {
16
+ pickOneAndWrite: (query: _FilterQuery, rawData: _Partial) => Promise<TDocument>;
17
+ pickAndWrite: (docId: string, rawData: _Partial) => Promise<TDocument>;
18
+ pickOne: (query: _FilterQuery, projection?: _Projection) => Promise<TDocument>;
19
+ pickById: (docId: string | undefined, projection?: _Projection) => Promise<TDocument>;
20
20
  addSummary: (prefix?: string | string[], num?: number) => Promise<void>;
21
21
  moveSummary: (prev: string, next: string, num?: number) => Promise<void>;
22
22
  subSummary: (prefix?: string | string[], num?: number) => Promise<void>;
23
- sample: (query: FilterQuery<TSchema>, size?: number, aggregations?: PipelineStage[]) => Promise<TDocument[]>;
24
- sampleOne: (query: FilterQuery<TSchema>, aggregations?: PipelineStage[]) => Promise<TDocument | null>;
23
+ sample: (query: _FilterQuery, size?: number, aggregations?: PipelineStage[]) => Promise<TDocument[]>;
24
+ sampleOne: (query: _FilterQuery, aggregations?: PipelineStage[]) => Promise<TDocument | null>;
25
25
  }
26
26
  export type Mdl<Doc extends HydratedDocument<any>, Raw> = MongooseModel<Raw, unknown, unknown, unknown, Doc> & DefaultMdlStats<Doc, DocumentModel<Raw>>;
27
27
  export type SchemaOf<Mdl, Doc> = Schema<null, Mdl, Doc, undefined, null, Mdl>;
@@ -55,8 +55,8 @@ export declare const Database: {
55
55
  Model: (returns: () => Type) => (target: any) => void;
56
56
  Middleware: (returns: () => Type) => (target: any) => void;
57
57
  };
58
- export declare const into: <Doc, T extends string, Input, Full, Light, Insight, Summary, Filter extends FilterType>(docRef: Type<Doc>, cnst: ConstantModel<T, Input, Full, Light, Insight, Filter, Summary>) => Type<DatabaseModel<T, DocumentModel<Input>, Doc, Full, Insight, Filter, Summary>>;
59
- export declare const inside: <Doc, T extends string, Input, Full, Light, Insight, Filter extends FilterType, Summary>(modelRef: Type, cnst: ConstantModel<T, Input, Full, Light, Insight, Filter, Summary>) => Type<DatabaseModel<T, DocumentModel<Input>, Doc, Full, Insight, Filter, Summary>>;
58
+ export declare const into: <Doc, T extends string, Input, Full, Light, Insight, Summary, Filter extends FilterType, _CapitalizedT extends string, _Default, _DefaultInput, _DefaultState, _DefaultStateInput, _Doc, _DocInput, _QueryOfDoc, _Query, _Sort>(docRef: Type<Doc>, cnst: ConstantModel<T, Input, Full, Light, Insight, Filter, Summary, _CapitalizedT, _Default, _DefaultInput, _DefaultState, _DefaultStateInput, _Doc, _DocInput, _QueryOfDoc, _Query, _Sort>) => Type<DatabaseModel<T, _DocInput, Doc, Full, Insight, Filter, Summary, _CapitalizedT, _QueryOfDoc, _Query, _Sort>>;
59
+ export declare const inside: <Doc, T extends string, Input, Full, Light, Insight, Filter extends FilterType, Summary, _CapitalizedT extends string, _Default, _DefaultInput, _DefaultState, _DefaultStateInput, _Doc, _DocInput, _QueryOfDoc, _Query, _Sort>(modelRef: Type, cnst: ConstantModel<T, Input, Full, Light, Insight, Filter, Summary, _CapitalizedT, _Default, _DefaultInput, _DefaultState, _DefaultStateInput, _Doc, _DocInput, _QueryOfDoc, _Query, _Sort>) => Type<DatabaseModel<T, _DocInput, Doc, Full, Insight, Filter, Summary, _CapitalizedT, _QueryOfDoc, _Query, _Sort>>;
60
60
  export declare const by: <Model, AddModel>(modelRef: Type<Model>, addRef?: Type<AddModel>) => AddModel extends {
61
61
  [key: string]: any;
62
62
  } ? Type<Model & (AddModel extends BaseObject ? Doc<AddModel> : DocumentModel<AddModel>)> : Type<Model extends BaseObject ? Doc<Model> : DocumentModel<Model>>;
package/src/types.d.ts CHANGED
@@ -1,5 +1,3 @@
1
1
  export type DataInputOf<Input, Obj> = {
2
- [K in keyof Input as Input[K] extends any[] ? never : K]: Input[K];
3
- } & {
4
- [K in keyof Input as Input[K] extends any[] ? K : never]?: Input[K];
2
+ [K in keyof Input as K extends K ? K : never]: Input[K] extends any[] ? Input[K] | undefined : Input[K];
5
3
  } & Partial<Obj>;