akanjs 2.3.5-rc.9 → 2.3.5

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 (81) hide show
  1. package/base/base.ts +6 -2
  2. package/base/primitiveRegistry.ts +50 -46
  3. package/base/symbols.ts +16 -11
  4. package/base/types.ts +12 -0
  5. package/constant/constantRegistry.ts +10 -9
  6. package/constant/deserialize.ts +7 -7
  7. package/constant/fieldInfo.ts +173 -31
  8. package/constant/getDefault.ts +2 -2
  9. package/constant/immerify.ts +2 -2
  10. package/constant/purify.ts +19 -4
  11. package/constant/serialize.ts +6 -5
  12. package/constant/types.ts +9 -1
  13. package/constant/via.ts +312 -57
  14. package/dictionary/dictInfo.ts +28 -38
  15. package/dictionary/locale.ts +36 -38
  16. package/document/by.ts +84 -21
  17. package/document/database.ts +53 -18
  18. package/document/databaseRegistry.ts +7 -3
  19. package/document/filterMeta.ts +26 -14
  20. package/document/into.ts +71 -76
  21. package/document/types.ts +4 -2
  22. package/fetch/fetchType/endpointFetch.type.ts +6 -3
  23. package/fetch/fetchType/sliceFetch.type.ts +24 -13
  24. package/package.json +1 -1
  25. package/service/predefinedAdaptor/compress.adaptor.ts +9 -9
  26. package/service/predefinedAdaptor/database.adaptor.ts +3 -7
  27. package/service/serve.ts +17 -52
  28. package/service/types.ts +61 -21
  29. package/signal/endpoint.ts +2 -1
  30. package/signal/endpointInfo.ts +53 -23
  31. package/signal/internal.ts +2 -1
  32. package/signal/internalInfo.ts +71 -23
  33. package/signal/serverSignal.ts +23 -57
  34. package/signal/slice.ts +35 -35
  35. package/signal/sliceInfo.ts +63 -47
  36. package/signal/types.ts +23 -17
  37. package/store/action.ts +57 -44
  38. package/store/state.ts +47 -36
  39. package/store/stateBuilder.ts +3 -3
  40. package/store/store.ts +61 -15
  41. package/store/types.ts +16 -6
  42. package/types/base/base.d.ts +5 -1
  43. package/types/base/primitiveRegistry.d.ts +43 -38
  44. package/types/base/symbols.d.ts +5 -0
  45. package/types/base/types.d.ts +1 -0
  46. package/types/constant/constantRegistry.d.ts +3 -8
  47. package/types/constant/deserialize.d.ts +2 -2
  48. package/types/constant/fieldInfo.d.ts +41 -20
  49. package/types/constant/immerify.d.ts +2 -2
  50. package/types/constant/purify.d.ts +3 -2
  51. package/types/constant/serialize.d.ts +2 -2
  52. package/types/constant/types.d.ts +6 -1
  53. package/types/constant/via.d.ts +143 -22
  54. package/types/dictionary/dictInfo.d.ts +18 -11
  55. package/types/dictionary/locale.d.ts +23 -16
  56. package/types/document/by.d.ts +44 -7
  57. package/types/document/database.d.ts +9 -7
  58. package/types/document/databaseRegistry.d.ts +5 -4
  59. package/types/document/filterMeta.d.ts +18 -11
  60. package/types/document/into.d.ts +46 -19
  61. package/types/document/types.d.ts +3 -2
  62. package/types/fetch/fetchType/endpointFetch.type.d.ts +2 -2
  63. package/types/fetch/fetchType/sliceFetch.type.d.ts +11 -11
  64. package/types/service/serve.d.ts +4 -5
  65. package/types/service/serviceModule.d.ts +5 -5
  66. package/types/service/types.d.ts +23 -5
  67. package/types/signal/endpoint.d.ts +2 -1
  68. package/types/signal/endpointInfo.d.ts +40 -15
  69. package/types/signal/internal.d.ts +2 -1
  70. package/types/signal/internalInfo.d.ts +46 -12
  71. package/types/signal/serverSignal.d.ts +15 -12
  72. package/types/signal/slice.d.ts +8 -6
  73. package/types/signal/sliceInfo.d.ts +41 -20
  74. package/types/signal/types.d.ts +23 -17
  75. package/types/store/action.d.ts +25 -22
  76. package/types/store/baseSt.d.ts +42 -13
  77. package/types/store/state.d.ts +23 -26
  78. package/types/store/stateBuilder.d.ts +2 -2
  79. package/types/store/store.d.ts +17 -5
  80. package/types/store/types.d.ts +9 -3
  81. package/ui/Signal/makeExample.ts +3 -3
package/document/into.ts CHANGED
@@ -1,10 +1,12 @@
1
- import { type Cls, LOADER_META, type MergeAllActionTypes, type PromiseOrObject } from "akanjs/base";
1
+ import { type Cls, LOADER_META, type PromiseOrObject } from "akanjs/base";
2
2
  import { applyMixins } from "akanjs/common";
3
- import type { ConstantModel, DocumentModel, FieldObject, QueryOf } from "akanjs/constant";
4
- import type { ExtractQuery, ExtractSort, FilterCls, FilterInstance, SchemaOf } from ".";
5
- import type { DatabaseInstance } from "./database";
3
+ import type { DocumentModel, QueryOf } from "akanjs/constant";
4
+ import type { FilterCls, FilterQueryOf, FilterSortOf, SchemaOf } from ".";
5
+ import type { CacheDatabase, QueryMethodPart } from "./database";
6
+ import type { DataLoader } from "./dataLoader";
6
7
  import type { DocumentUpdate, DocumentUpdateOptions } from "./documentQuery";
7
8
  import { type LoaderBuilder, type ModelCls, makeLoaderBuilder } from "./loaderInfo";
9
+ import type { DocumentProjection } from "./types";
8
10
 
9
11
  export type CRUDEventType = "create" | "update" | "remove";
10
12
  export type SaveEventType = "save" | CRUDEventType;
@@ -14,7 +16,7 @@ interface DefaultMdlStats<
14
16
  TSchema,
15
17
  _Partial extends Partial<TSchema> = Partial<TSchema>,
16
18
  _FilterQuery extends QueryOf<TSchema> = QueryOf<TSchema>,
17
- _Projection extends Partial<Record<keyof TSchema, boolean>> = Partial<Record<keyof TSchema, boolean>>,
19
+ _Projection = DocumentProjection<TSchema>,
18
20
  > {
19
21
  pickOneAndWrite: (query: _FilterQuery, rawData: _Partial) => Promise<TDocument>;
20
22
  pickAndWrite: (docId: string, rawData: _Partial) => Promise<TDocument>;
@@ -45,9 +47,9 @@ export interface UpdateResult {
45
47
  modifiedCount: number;
46
48
  upsertedId?: string | null;
47
49
  }
48
- export interface BulkWriteOperation<Raw> {
50
+ export interface BulkWriteOperation<Raw, _RawDoc = DocumentModel<Raw>, _RawQuery = QueryOf<_RawDoc>> {
49
51
  updateOne: {
50
- filter: QueryOf<DocumentModel<Raw>>;
52
+ filter: _RawQuery;
51
53
  update: DocumentUpdate;
52
54
  upsert?: boolean;
53
55
  };
@@ -63,89 +65,82 @@ type FindOneChain<Doc> = Promise<Doc | null> & {
63
65
  skip(skip: number): FindOneChain<Doc>;
64
66
  select(projection?: unknown): FindOneChain<Doc>;
65
67
  };
66
- export type Mdl<Doc, Raw> = DefaultMdlStats<Doc, DocumentModel<Raw>> & {
68
+ export type Mdl<
69
+ Doc,
70
+ Raw,
71
+ _RawDoc = DocumentModel<Raw>,
72
+ _RawQuery = QueryOf<_RawDoc>,
73
+ _Projection extends DocumentProjection<Raw> = DocumentProjection<Raw>,
74
+ > = DefaultMdlStats<Doc, _RawDoc, Partial<_RawDoc>, _RawQuery, _Projection> & {
67
75
  refName: string;
68
- new (data: Partial<DocumentModel<Raw>>): Doc;
69
- find(query: QueryOf<DocumentModel<Raw>>, projection?: Partial<Record<keyof Raw, boolean>>): FindManyChain<Doc>;
70
- findOne(query: QueryOf<DocumentModel<Raw>>, projection?: Partial<Record<keyof Raw, boolean>>): FindOneChain<Doc>;
71
- findById(id: string | undefined, projection?: Partial<Record<keyof Raw, boolean>>): Promise<Doc | null>;
72
- countDocuments(query: QueryOf<DocumentModel<Raw>>): Promise<number>;
73
- exists(query: QueryOf<DocumentModel<Raw>>): Promise<string | null>;
74
- updateOne(
75
- query: QueryOf<DocumentModel<Raw>>,
76
- update: DocumentUpdate,
77
- options?: DocumentUpdateOptions,
78
- ): Promise<UpdateResult>;
79
- updateMany(query: QueryOf<DocumentModel<Raw>>, update: DocumentUpdate): Promise<UpdateResult>;
80
- deleteMany(query: QueryOf<DocumentModel<Raw>>): Promise<UpdateResult>;
81
- bulkWrite(operations: BulkWriteOperation<Raw>[]): Promise<UpdateResult>;
76
+ new (data: Partial<_RawDoc> | Partial<Doc>): Doc;
77
+ find(query: _RawQuery, projection?: _Projection): FindManyChain<Doc>;
78
+ findOne(query: _RawQuery, projection?: _Projection): FindOneChain<Doc>;
79
+ findById(id: string | undefined, projection?: _Projection): Promise<Doc | null>;
80
+ countDocuments(query: _RawQuery): Promise<number>;
81
+ exists(query: _RawQuery): Promise<string | null>;
82
+ updateOne(query: _RawQuery, update: DocumentUpdate, options?: DocumentUpdateOptions): Promise<UpdateResult>;
83
+ updateMany(query: _RawQuery, update: DocumentUpdate): Promise<UpdateResult>;
84
+ deleteMany(query: _RawQuery): Promise<UpdateResult>;
85
+ bulkWrite(operations: BulkWriteOperation<Raw, _RawDoc, _RawQuery>[]): Promise<UpdateResult>;
82
86
  };
83
87
 
88
+ interface IntoConstantModel<T extends string, _CapitalizedRefName extends string, Raw> {
89
+ refName: T;
90
+ _CapitalizedRefName: _CapitalizedRefName;
91
+ _Full: Raw;
92
+ }
93
+ type NoInferType<T> = [T][T extends unknown ? 0 : never];
94
+ type IntoModelActions<
95
+ T extends string,
96
+ _CapitalizedRefName extends string,
97
+ Doc,
98
+ Raw,
99
+ _Query,
100
+ _Sort,
101
+ _QueryOfDoc = QueryOf<Doc>,
102
+ > = {
103
+ [key in _CapitalizedRefName]: Mdl<Doc, Raw>;
104
+ } & {
105
+ [key in `${Uncapitalize<_CapitalizedRefName>}Loader`]: DataLoader<string, Doc, string>;
106
+ } & {
107
+ [key in `${Uncapitalize<_CapitalizedRefName>}Cache`]: CacheDatabase<T>;
108
+ } & {
109
+ [K in `get${_CapitalizedRefName}`]: (id: string) => Promise<Doc>;
110
+ } & {
111
+ [K in `load${_CapitalizedRefName}`]: (id?: string) => Promise<Doc | null>;
112
+ } & {
113
+ [K in `load${_CapitalizedRefName}Many`]: (ids: string[]) => Promise<Doc[]>;
114
+ } & {
115
+ [K in `create${_CapitalizedRefName}`]: (data: Partial<Doc>) => Promise<Doc>;
116
+ } & {
117
+ [K in `update${_CapitalizedRefName}`]: (id: string, data: Partial<Doc>) => Promise<Doc>;
118
+ } & {
119
+ [K in `remove${_CapitalizedRefName}`]: (id: string) => Promise<Doc>;
120
+ } & QueryMethodPart<_Query, _Sort, Raw, Doc, unknown, unknown, unknown, _QueryOfDoc>;
121
+
84
122
  export const into = <
85
123
  Doc,
86
- Filter extends FilterInstance,
124
+ FilterRef extends FilterCls,
87
125
  T extends string,
88
- Input,
89
- Obj,
90
- Full,
91
- Light,
92
- Insight,
93
- FullFieldObj extends FieldObject,
126
+ Raw,
94
127
  AddDbModels extends ModelCls[],
95
128
  _CapitalizedRefName extends string,
96
- _Default,
97
- _DefaultInput,
98
- _DefaultState,
99
- _DefaultStateInput,
100
- _DefaultInsight,
101
- _PurifiedInput,
102
- _Doc,
103
- _DocInput,
104
- _QueryOfDoc,
105
- _Query = ExtractQuery<Filter>,
106
- _Sort = ExtractSort<Filter>,
107
- _DatabaseModel = DatabaseInstance<
108
- T,
109
- _DocInput,
110
- Doc,
111
- Full,
112
- Insight,
113
- Filter,
114
- _CapitalizedRefName,
115
- _QueryOfDoc,
116
- _Query,
117
- _Sort
118
- >,
119
- _LoaderBuilder extends LoaderBuilder<_Doc> = LoaderBuilder<_Doc>,
129
+ _QueryOfDoc = QueryOf<Doc>,
130
+ _Query = FilterQueryOf<FilterRef>,
131
+ _Sort = FilterSortOf<FilterRef>,
132
+ _LoaderBuilder extends LoaderBuilder<NoInferType<Doc>> = LoaderBuilder<Doc>,
120
133
  >(
121
134
  docRef: Cls<Doc>,
122
- filterRef: FilterCls<Filter>,
123
- cnst: ConstantModel<
124
- T,
125
- Input,
126
- Obj,
127
- Full,
128
- Light,
129
- Insight,
130
- FullFieldObj,
131
- _CapitalizedRefName,
132
- _Default,
133
- _DefaultInput,
134
- _DefaultState,
135
- _DefaultStateInput,
136
- _DefaultInsight,
137
- _PurifiedInput,
138
- _Doc,
139
- _DocInput,
140
- _QueryOfDoc
141
- >,
135
+ filterRef: FilterRef,
136
+ cnst: IntoConstantModel<T, _CapitalizedRefName, Raw>,
142
137
  loaderBuilder: _LoaderBuilder,
143
138
  ...addMdls: [...AddDbModels]
144
139
  ): ModelCls<
145
- MergeAllActionTypes<AddDbModels, keyof _DatabaseModel & string> & _DatabaseModel,
140
+ IntoModelActions<T, _CapitalizedRefName, Doc, Raw, _Query, _Sort, _QueryOfDoc>,
146
141
  ReturnType<_LoaderBuilder>
147
142
  > => {
148
- const loaderInfoMap = loaderBuilder(makeLoaderBuilder());
143
+ const loaderInfoMap = loaderBuilder(makeLoaderBuilder<Doc>());
149
144
  const libsOnSchemaFns = addMdls.map((mdl) => mdl._onSchema);
150
145
  const DefaultModel = Object.assign(class DefaultModel {}, {
151
146
  [LOADER_META]: Object.assign({}, ...addMdls.map((mdl) => mdl[LOADER_META]), loaderInfoMap),
@@ -163,7 +158,7 @@ export const into = <
163
158
  });
164
159
  });
165
160
  return DefaultModel as unknown as ModelCls<
166
- MergeAllActionTypes<AddDbModels, keyof _DatabaseModel & string> & _DatabaseModel,
161
+ IntoModelActions<T, _CapitalizedRefName, Doc, Raw, _Query, _Sort, _QueryOfDoc>,
167
162
  ReturnType<_LoaderBuilder>
168
163
  >;
169
164
  };
package/document/types.ts CHANGED
@@ -25,17 +25,19 @@ export interface FilterArgProps {
25
25
  enum?: EnumInstance;
26
26
  }
27
27
 
28
+ export type DocumentProjection<T> = Partial<Record<keyof T, boolean>>;
29
+
28
30
  export interface ListQueryOption<Sort = never, Obj = any> {
29
31
  skip?: number | null;
30
32
  limit?: number | null;
31
33
  sort?: Sort | null;
32
34
  sample?: number;
33
- select?: Partial<Record<keyof Obj, boolean>>;
35
+ select?: DocumentProjection<Obj>;
34
36
  }
35
37
  export interface FindQueryOption<Sort = never, Obj = any> {
36
38
  skip?: number | null;
37
39
  sort?: Sort | null;
38
40
  sample?: boolean;
39
- select?: Partial<Record<keyof Obj, boolean>>;
41
+ select?: DocumentProjection<Obj>;
40
42
  }
41
43
  export type { SchemaOf } from "./documentSchema";
@@ -7,6 +7,9 @@ import type {
7
7
  EndpInfoReqType,
8
8
  EndpointCls,
9
9
  EndpointInfo,
10
+ SlceCnstFull,
11
+ SlceCnstInsight,
12
+ SlceCnstLight,
10
13
  SliceCls,
11
14
  } from "akanjs/signal";
12
15
 
@@ -24,9 +27,9 @@ type EndpointClientReturns<E, SlceCls extends SliceCls | never> = [SlceCls] exte
24
27
  ? EndpInfoClientReturns<E>
25
28
  : ExtendedEndpointReturn<
26
29
  EndpInfoClientReturns<E>,
27
- SlceCls["srv"]["cnst"]["_Full"],
28
- SlceCls["srv"]["cnst"]["_Light"],
29
- SlceCls["srv"]["cnst"]["_Insight"]
30
+ SlceCnstFull<SlceCls>,
31
+ SlceCnstLight<SlceCls>,
32
+ SlceCnstInsight<SlceCls>
30
33
  >;
31
34
 
32
35
  type EndpInfoReturns<E, SlceCls extends SliceCls | never> =
@@ -2,22 +2,33 @@ import type { GetStateObject, SLICE_META } from "akanjs/base";
2
2
  import type { FetchPolicy } from "akanjs/common";
3
3
  import type { ConstantModel, DefaultOf, ProtoFile, PurifiedModel } from "akanjs/constant";
4
4
  import type { DatabaseModel, ExtractSort, FilterInstance } from "akanjs/document";
5
- import type { SliceCls, SliceInfoArgs } from "akanjs/signal";
5
+ import type {
6
+ SlceCnstCapitalizedRefName,
7
+ SlceCnstDefaultInput,
8
+ SlceCnstFull,
9
+ SlceCnstInput,
10
+ SlceCnstInsight,
11
+ SlceCnstLight,
12
+ SlceCnstPurifiedInput,
13
+ SlceCnstRefName,
14
+ SlceDbFilter,
15
+ SlceDbSort,
16
+ SliceCls,
17
+ SliceInfoArgs,
18
+ } from "akanjs/signal";
6
19
  import type { ClientEdit, ClientInit, ClientView, EditReturn, InitReturn, ViewReturn } from "./appliedReturn.type";
7
20
 
8
21
  type _SliceMap<S extends SliceCls> = S[typeof SLICE_META];
9
- type _RefName<S extends SliceCls> = S["srv"]["cnst"]["refName"];
10
- type _Cap<S extends SliceCls> = S["srv"]["cnst"]["_CapitalizedRefName"];
11
- type _Input<S extends SliceCls> = S["srv"]["cnst"]["_Input"];
12
- type _Full<S extends SliceCls> = S["srv"]["cnst"]["_Full"];
13
- type _Light<S extends SliceCls> = S["srv"]["cnst"]["_Light"];
14
- type _Insight<S extends SliceCls> = S["srv"]["cnst"]["_Insight"];
15
- type _PurifiedInput<S extends SliceCls> = S["srv"]["cnst"]["_PurifiedInput"];
16
- type _DefaultInput<S extends SliceCls> = S["srv"]["cnst"]["_DefaultInput"];
17
- type _StateLight<S extends SliceCls> = S["srv"]["cnst"]["_StateLight"];
18
- type _StateInsight<S extends SliceCls> = S["srv"]["cnst"]["_StateInsight"];
19
- type _Filter<S extends SliceCls> = S["srv"]["db"]["_Filter"];
20
- type _Sort<S extends SliceCls> = S["srv"]["db"]["_Sort"];
22
+ type _RefName<S extends SliceCls> = SlceCnstRefName<S>;
23
+ type _Cap<S extends SliceCls> = SlceCnstCapitalizedRefName<S>;
24
+ type _Input<S extends SliceCls> = SlceCnstInput<S>;
25
+ type _Full<S extends SliceCls> = SlceCnstFull<S>;
26
+ type _Light<S extends SliceCls> = SlceCnstLight<S>;
27
+ type _Insight<S extends SliceCls> = SlceCnstInsight<S>;
28
+ type _PurifiedInput<S extends SliceCls> = SlceCnstPurifiedInput<S>;
29
+ type _DefaultInput<S extends SliceCls> = SlceCnstDefaultInput<S>;
30
+ type _Filter<S extends SliceCls> = SlceDbFilter<S>;
31
+ type _Sort<S extends SliceCls> = SlceDbSort<S>;
21
32
  type _LightWithId<S extends SliceCls> = _Light<S> extends { id: string } ? _Light<S> : { id: string };
22
33
  type _SliceFetchInitOption<S extends SliceCls> = FetchInitOption<_Input<S>, _Filter<S>, _DefaultInput<S>, _Sort<S>>;
23
34
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "akanjs",
3
- "version": "2.3.5-rc.9",
3
+ "version": "2.3.5",
4
4
  "sourceType": "module",
5
5
  "type": "module",
6
6
  "publishConfig": {
@@ -13,7 +13,7 @@ import {
13
13
  type PrimitiveScalar,
14
14
  } from "akanjs/base";
15
15
  import { Logger } from "akanjs/common";
16
- import { type ConstantCls, type ConstantField, ConstantRegistry } from "akanjs/constant";
16
+ import { type ConstantCls, type ConstantField, type ConstantModelRef, ConstantRegistry } from "akanjs/constant";
17
17
  import { adapt } from "../adapt";
18
18
 
19
19
  type ProtoType = {
@@ -94,7 +94,7 @@ export class ProtobufCompressor extends adapt("protobufCompressor", () => ({}))
94
94
  return this.#proto;
95
95
  }
96
96
 
97
- #getProtoModel(modelRef: ConstantCls<unknown>) {
97
+ #getProtoModel(modelRef: ConstantModelRef<unknown>) {
98
98
  const refName = ConstantRegistry.getRefName(modelRef);
99
99
  const predefinedProtoModel = this.#protoModelMap.get(modelRef);
100
100
  if (predefinedProtoModel) return predefinedProtoModel;
@@ -140,13 +140,13 @@ export class ProtobufCompressor extends adapt("protobufCompressor", () => ({}))
140
140
  const [valueRef] = getNonArrayModel(modelRef);
141
141
  return this.#primitiveProtoEncodeMap.get(valueRef) ?? ((value) => value as object);
142
142
  }
143
- #getRelatedProtoModelRefs(modelRef: ConstantCls, baseModelRefs: ConstantCls[] = []) {
143
+ #getRelatedProtoModelRefs(modelRef: ConstantModelRef, baseModelRefs: ConstantModelRef[] = []) {
144
144
  const allModelRefs = [modelRef, ...baseModelRefs];
145
145
  allModelRefs.forEach((mRef) => {
146
146
  Object.values(mRef[FIELD_META]).forEach((field) => {
147
147
  const relatedRef = field.isMap && field.of ? (field.of as Cls) : field.modelRef;
148
- if (!PrimitiveRegistry.has(relatedRef) && !allModelRefs.includes(relatedRef as ConstantCls)) {
149
- allModelRefs.push(relatedRef as ConstantCls);
148
+ if (!PrimitiveRegistry.has(relatedRef) && !allModelRefs.includes(relatedRef as ConstantModelRef)) {
149
+ allModelRefs.push(relatedRef as ConstantModelRef);
150
150
  }
151
151
  });
152
152
  });
@@ -188,7 +188,7 @@ export class ProtobufCompressor extends adapt("protobufCompressor", () => ({}))
188
188
  | boolean
189
189
  | null;
190
190
  }
191
- #makeProtoEncode = <T>(modelRef: ConstantCls<T>): ((value: never) => object) => {
191
+ #makeProtoEncode = <T>(modelRef: ConstantModelRef<T>): ((value: never) => object) => {
192
192
  const predefinedProtoEncode = this.#protoEncodeMap.get(modelRef);
193
193
  if (predefinedProtoEncode) return predefinedProtoEncode;
194
194
  const protoEncodeFn = (value: T) => {
@@ -286,7 +286,7 @@ export class ProtobufCompressor extends adapt("protobufCompressor", () => ({}))
286
286
  | null
287
287
  | undefined;
288
288
  }
289
- #makeProtoDecode<T>(modelRef: ConstantCls<T>): (value: never) => object {
289
+ #makeProtoDecode<T>(modelRef: ConstantModelRef<T>): (value: never) => object {
290
290
  const predefinedProtoDecode = this.#protoDecodeMap.get(modelRef);
291
291
  if (predefinedProtoDecode) return predefinedProtoDecode;
292
292
  const protoDecodeFn = (value: T) => {
@@ -300,7 +300,7 @@ export class ProtobufCompressor extends adapt("protobufCompressor", () => ({}))
300
300
  return protoDecodeFn as (value: never) => object;
301
301
  }
302
302
  #decode<T>(modelRef: ConstantCls<T>, buffer: Buffer, { raw = false }: { raw?: boolean } = {}): T {
303
- const protoModel = this.#getProtoModel(modelRef as unknown as ConstantCls<unknown>);
303
+ const protoModel = this.#getProtoModel(modelRef as unknown as ConstantModelRef<unknown>);
304
304
  const message = protoModel.decode(Buffer.from(buffer));
305
305
  const data = protoModel.toObject(message);
306
306
  const protoDecode = this.#makeProtoDecode(modelRef);
@@ -326,7 +326,7 @@ export class ProtobufCompressor extends adapt("protobufCompressor", () => ({}))
326
326
  const fieldType = isPrimitive ? (this.#protobufTypeMap.get(ref) ?? "string") : refName;
327
327
 
328
328
  if (!isPrimitive) {
329
- const modelRef = ref as ConstantCls;
329
+ const modelRef = ref as ConstantModelRef;
330
330
  const allModelRefs = this.#getRelatedProtoModelRefs(modelRef, [...modelRef.children]);
331
331
 
332
332
  for (const mRef of allModelRefs) {
@@ -3,7 +3,7 @@ import { AsyncLocalStorage } from "node:async_hooks";
3
3
  import { mkdir } from "node:fs/promises";
4
4
  import path from "node:path";
5
5
  import type { InArgs, InValue, Client as LibsqlClient } from "@libsql/client";
6
- import { type BaseEnv, dayjs, FIELD_META, PRIMITIVE_DEFAULT_VALUE, type PromiseOrObject } from "akanjs/base";
6
+ import { type BaseEnv, DEFAULT_VALUE, dayjs, FIELD_META, type PromiseOrObject } from "akanjs/base";
7
7
  import { type ConstantModel, getDefault } from "akanjs/constant";
8
8
  import {
9
9
  createDocumentId,
@@ -957,9 +957,7 @@ export class SqliteDocumentStore {
957
957
  typeof props.default === "function" ? (props.default as (data: unknown) => unknown)(doc) : props.default;
958
958
  } else {
959
959
  doc[field] =
960
- ((props as Record<string, unknown>).modelRef as { [PRIMITIVE_DEFAULT_VALUE]?: unknown })?.[
961
- PRIMITIVE_DEFAULT_VALUE
962
- ] ?? null;
960
+ ((props as Record<string, unknown>).modelRef as { [DEFAULT_VALUE]?: unknown })?.[DEFAULT_VALUE] ?? null;
963
961
  }
964
962
  } else {
965
963
  doc[field] = props ? this.decodeFieldValue(value, props) : value;
@@ -1035,9 +1033,7 @@ export class SqliteDocumentStore {
1035
1033
  result[key] = null;
1036
1034
  } else {
1037
1035
  result[key] =
1038
- ((props as Record<string, unknown>).modelRef as { [PRIMITIVE_DEFAULT_VALUE]?: unknown })?.[
1039
- PRIMITIVE_DEFAULT_VALUE
1040
- ] ?? null;
1036
+ ((props as Record<string, unknown>).modelRef as { [DEFAULT_VALUE]?: unknown })?.[DEFAULT_VALUE] ?? null;
1041
1037
  }
1042
1038
  } else {
1043
1039
  result[key] = this.decodeFieldValue(value, props);
package/service/serve.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { type Cls, INJECT_META } from "akanjs/base";
2
2
  import { applyMixins, capitalize, Logger, lowerlize } from "akanjs/common";
3
- import type { DatabaseModel, FilterInstance } from "akanjs/document";
3
+ import type { DatabaseModel } from "akanjs/document";
4
4
 
5
5
  import {
6
6
  type ExtractInjectInfoObject,
@@ -9,7 +9,7 @@ import {
9
9
  type InjectType,
10
10
  injectionBuilder,
11
11
  } from "./injectInfo";
12
- import type { DatabaseService } from "./types";
12
+ import type { DatabaseService, DatabaseServiceForModel } from "./types";
13
13
 
14
14
  interface ServiceOptions {
15
15
  enabled?: boolean;
@@ -17,6 +17,10 @@ interface ServiceOptions {
17
17
  }
18
18
  export type ServiceType = "database" | "plain";
19
19
 
20
+ type DatabaseServiceData = Record<string, unknown>;
21
+ type DatabaseServiceDoc = Record<string, unknown>;
22
+ type DatabaseServicePatch = Partial<DatabaseServiceDoc>;
23
+
20
24
  const avoidKeys = new Set([
21
25
  "onInit",
22
26
  "onDestroy",
@@ -72,52 +76,17 @@ export function serve<RefName extends string, Injection extends InjectBuilder>(
72
76
  injectBuilder: Injection,
73
77
  ...extendSrvs: Cls[]
74
78
  ): ServiceCls<RefName, {}, ReturnType<Injection>>;
75
- export function serve<
76
- T extends string,
77
- Input,
78
- Doc,
79
- Model,
80
- Obj,
81
- Insight,
82
- Filter extends FilterInstance,
83
- Injection extends InjectBuilder,
84
- LibSrvs extends Cls[] = [],
85
- >(
86
- db: DatabaseModel<T, Input, Doc, Model, Obj, Insight, Filter>,
87
- injectBuilder: Injection,
88
- ...extendSrvs: LibSrvs
89
- ): ServiceCls<T, DatabaseService<T, Input, Doc, Obj, Model, Insight, Filter, LibSrvs>, ReturnType<Injection>>;
90
- export function serve<
91
- T extends string,
92
- Input,
93
- Doc,
94
- Model,
95
- Obj,
96
- Insight,
97
- Filter extends FilterInstance,
98
- Injection extends InjectBuilder,
99
- LibSrvs extends Cls[] = [],
100
- >(
101
- db: DatabaseModel<T, Input, Doc, Model, Obj, Insight, Filter>,
79
+ export function serve<Db extends DatabaseModel, Injection extends InjectBuilder, LibSrvs extends Cls[] = []>(
80
+ db: Db,
102
81
  injectBuilder: Injection,
103
82
  ...extendSrvs: LibSrvs
104
- ): ServiceCls<T, DatabaseService<T, Input, Doc, Obj, Model, Insight, Filter, LibSrvs>, ReturnType<Injection>>;
105
- export function serve<
106
- T extends string,
107
- Input,
108
- Doc,
109
- Model,
110
- Obj,
111
- Insight,
112
- Filter extends FilterInstance,
113
- Injection extends InjectBuilder,
114
- LibSrvs extends ServiceCls[] = [],
115
- >(
116
- db: DatabaseModel<T, Input, Doc, Model, Obj, Insight, Filter>,
83
+ ): ServiceCls<Db["refName"], DatabaseServiceForModel<Db, LibSrvs>, ReturnType<Injection>>;
84
+ export function serve<Db extends DatabaseModel, Injection extends InjectBuilder, LibSrvs extends Cls[] = []>(
85
+ db: Db,
117
86
  option: ServiceOptions,
118
87
  injectBuilder: Injection,
119
88
  ...extendSrvs: LibSrvs
120
- ): ServiceCls<T, DatabaseService<T, Input, Doc, Obj, Model, Insight, Filter, LibSrvs>, ReturnType<Injection>>;
89
+ ): ServiceCls<Db["refName"], DatabaseServiceForModel<Db, LibSrvs>, ReturnType<Injection>>;
121
90
 
122
91
  export function serve(
123
92
  refNameOrDb: string | DatabaseModel,
@@ -178,29 +147,25 @@ export function serve(
178
147
  const preRemoveFns = extSrvs.map((srv) => srv.prototype._preRemove);
179
148
  const postRemoveFns = extSrvs.map((srv) => srv.prototype._postRemove);
180
149
  Object.assign(srvRef.prototype, {
181
- async __libsPreCreate(this: DatabaseService, data: Parameters<DatabaseService["__libsPreCreate"]>[0]) {
150
+ async __libsPreCreate(this: DatabaseService, data: DatabaseServiceData) {
182
151
  let result = data;
183
152
  for (const preCreate of preCreateFns) if (preCreate) result = await preCreate.call(this, result);
184
153
  if (this._preCreate) result = await this._preCreate(result);
185
154
  return result;
186
155
  },
187
- async __libsPostCreate(this: DatabaseService, doc: Parameters<DatabaseService["__libsPostCreate"]>[0]) {
156
+ async __libsPostCreate(this: DatabaseService, doc: DatabaseServiceDoc) {
188
157
  let result = doc;
189
158
  for (const postCreate of postCreateFns) if (postCreate) result = await postCreate.call(this, result);
190
159
  if (this._postCreate) result = await this._postCreate(result);
191
160
  return result;
192
161
  },
193
- async __libsPreUpdate(
194
- this: DatabaseService,
195
- id: string,
196
- data: Parameters<DatabaseService["__libsPreUpdate"]>[1],
197
- ) {
162
+ async __libsPreUpdate(this: DatabaseService, id: string, data: DatabaseServicePatch) {
198
163
  let result = data;
199
164
  for (const preUpdate of preUpdateFns) if (preUpdate) result = await preUpdate.call(this, id, result);
200
165
  if (this._preUpdate) result = await this._preUpdate(id, result);
201
166
  return result;
202
167
  },
203
- async __libsPostUpdate(this: DatabaseService, doc: Parameters<DatabaseService["__libsPostUpdate"]>[0]) {
168
+ async __libsPostUpdate(this: DatabaseService, doc: DatabaseServiceDoc) {
204
169
  let result = doc;
205
170
  for (const postUpdate of postUpdateFns) if (postUpdate) result = await postUpdate.call(this, result);
206
171
  if (this._postUpdate) result = await this._postUpdate(result);
@@ -210,7 +175,7 @@ export function serve(
210
175
  for (const preRemove of preRemoveFns) await preRemove?.call(this, id);
211
176
  if (this._preRemove) await this._preRemove(id);
212
177
  },
213
- async __libsPostRemove(this: DatabaseService, doc: Parameters<DatabaseService["__libsPostRemove"]>[0]) {
178
+ async __libsPostRemove(this: DatabaseService, doc: DatabaseServiceDoc) {
214
179
  let result = doc;
215
180
  for (const postRemove of postRemoveFns) if (postRemove) result = await postRemove.call(this, result);
216
181
  if (this._postRemove) result = await this._postRemove(result);
package/service/types.ts CHANGED
@@ -3,11 +3,8 @@ import type { Logger } from "akanjs/common";
3
3
  import type { QueryOf } from "akanjs/constant";
4
4
  import type {
5
5
  CRUDEventType,
6
- DatabaseInstance,
6
+ DatabaseModel,
7
7
  DataInputOf,
8
- Doc as DbDoc,
9
- ExtractQuery,
10
- ExtractSort,
11
8
  FilterInstance,
12
9
  FindQueryOption,
13
10
  GetDocObject,
@@ -34,6 +31,49 @@ type ServiceMixinOmitKey =
34
31
  | "_libsPreRemove"
35
32
  | "_libsPostRemove";
36
33
 
34
+ type DatabaseQueryMethods<Query, Sort, Obj, Doc, Insight, FindQueryOption, ListQueryOption, DocQuery> = QueryMethodPart<
35
+ Query,
36
+ Sort,
37
+ Obj,
38
+ Doc,
39
+ Insight,
40
+ FindQueryOption,
41
+ ListQueryOption,
42
+ DocQuery
43
+ >;
44
+
45
+ type DocumentLike = {
46
+ id: string;
47
+ set: (...args: never[]) => unknown;
48
+ save: (...args: never[]) => unknown;
49
+ };
50
+ type MixedLibServiceReturn<Value, Doc, DocObjectOfDoc> = Value extends (infer SingleValue)[]
51
+ ? SingleValue extends DocumentLike
52
+ ? DocObjectOfDoc extends GetDocObject<SingleValue>
53
+ ? Doc[]
54
+ : Value
55
+ : Value
56
+ : Value extends DocumentLike
57
+ ? DocObjectOfDoc extends GetDocObject<Value>
58
+ ? Doc
59
+ : Value
60
+ : Value;
61
+ type MixedLibServiceMethods<MixedLibSrv, Doc, DocObjectOfDoc> = {
62
+ [K in keyof MixedLibSrv]: MixedLibSrv[K] extends (...args: infer Args) => Promise<infer Value>
63
+ ? (...args: Args) => Promise<MixedLibServiceReturn<Value, Doc, DocObjectOfDoc>>
64
+ : MixedLibSrv[K];
65
+ };
66
+
67
+ export type DbRefName<Db extends DatabaseModel> = Db["refName"];
68
+ export type DbInput<Db extends DatabaseModel> = Db["_Input"];
69
+ export type DbDoc<Db extends DatabaseModel> = Db["_Doc"];
70
+ export type DbObj<Db extends DatabaseModel> = Db["_Obj"];
71
+ export type DbModel<Db extends DatabaseModel> = Db["_Model"];
72
+ export type DbInsight<Db extends DatabaseModel> = Db["_Insight"];
73
+ export type DbFilter<Db extends DatabaseModel> = Db["_Filter"];
74
+ export type DbQuery<Db extends DatabaseModel> = Db["_Query"];
75
+ export type DbSort<Db extends DatabaseModel> = Db["_Sort"];
76
+
37
77
  export type DatabaseService<
38
78
  T extends string = string,
39
79
  Input = any,
@@ -43,8 +83,8 @@ export type DatabaseService<
43
83
  Insight = any,
44
84
  Filter extends FilterInstance = FilterInstance,
45
85
  LibSrvs extends Cls[] = [],
46
- _Query extends ExtractQuery<Filter> = ExtractQuery<Filter>,
47
- _Sort extends ExtractSort<Filter> = ExtractSort<Filter>,
86
+ _Query = Filter extends FilterInstance ? any : never,
87
+ _Sort = Filter extends FilterInstance ? any : never,
48
88
  _CapitalizedRefName extends Capitalize<T> = Capitalize<T>,
49
89
  _DataInputOfDoc extends DataInputOf<Input, Doc> = DataInputOf<Input, Doc>,
50
90
  _QueryOfDoc extends QueryOf<Doc> = QueryOf<Doc>,
@@ -96,18 +136,18 @@ export type DatabaseService<
96
136
  [K in `update${_CapitalizedRefName}`]: (id: string, data: Partial<Doc>) => Promise<Doc>;
97
137
  } & {
98
138
  [K in `remove${_CapitalizedRefName}`]: (id: string) => Promise<Doc>;
99
- } & QueryMethodPart<_Query, _Sort, Obj, Doc, Insight, _FindQueryOption, _ListQueryOption, _QueryOfDoc> & {
100
- [K in keyof _MixedLibSrv]: _MixedLibSrv[K] extends (...args: infer Args) => Promise<infer Value>
101
- ? Value extends (infer SingleValue)[]
102
- ? SingleValue extends DatabaseInstance
103
- ? _DocObjectOfDoc extends GetDocObject<SingleValue>
104
- ? (...args: Args) => Promise<Doc[]>
105
- : _MixedLibSrv[K]
106
- : _MixedLibSrv[K]
107
- : Value extends DbDoc<any>
108
- ? _DocObjectOfDoc extends GetDocObject<Value>
109
- ? (...args: Args) => Promise<Doc>
110
- : _MixedLibSrv[K]
111
- : _MixedLibSrv[K]
112
- : _MixedLibSrv[K];
113
- };
139
+ } & DatabaseQueryMethods<_Query, _Sort, Obj, Doc, Insight, _FindQueryOption, _ListQueryOption, _QueryOfDoc> &
140
+ MixedLibServiceMethods<_MixedLibSrv, Doc, _DocObjectOfDoc>;
141
+
142
+ export type DatabaseServiceForModel<Db extends DatabaseModel, LibSrvs extends Cls[] = []> = DatabaseService<
143
+ DbRefName<Db>,
144
+ DbInput<Db>,
145
+ DbDoc<Db>,
146
+ DbObj<Db>,
147
+ DbModel<Db>,
148
+ DbInsight<Db>,
149
+ DbFilter<Db>,
150
+ LibSrvs,
151
+ DbQuery<Db>,
152
+ DbSort<Db>
153
+ >;
@@ -3,6 +3,7 @@ import { ENDPOINT_META } from "akanjs/base";
3
3
  import { applyMixins } from "akanjs/common";
4
4
  import { type Adaptor, type AdaptorCls, dangerouslyAdapt, type ServiceModel } from "akanjs/service";
5
5
  import { buildEndpoint, type EndpointBuilder, type EndpointInfo } from "./endpointInfo";
6
+ import type { SrvRefName } from "./types";
6
7
 
7
8
  export interface Endpoint extends Adaptor {}
8
9
 
@@ -10,7 +11,7 @@ export interface EndpointCls<
10
11
  SrvModule extends ServiceModel = ServiceModel,
11
12
  EndpointInfoObj extends { [key: string]: EndpointInfo } = { [key: string]: EndpointInfo },
12
13
  > extends AdaptorCls {
13
- baseName: SrvModule["srv"]["refName"];
14
+ baseName: SrvRefName<SrvModule>;
14
15
  srv: SrvModule;
15
16
  [ENDPOINT_META]: EndpointInfoObj;
16
17
  }