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.
- package/base/base.ts +6 -2
- package/base/primitiveRegistry.ts +50 -46
- package/base/symbols.ts +16 -11
- package/base/types.ts +12 -0
- package/constant/constantRegistry.ts +10 -9
- package/constant/deserialize.ts +7 -7
- package/constant/fieldInfo.ts +173 -31
- package/constant/getDefault.ts +2 -2
- package/constant/immerify.ts +2 -2
- package/constant/purify.ts +19 -4
- package/constant/serialize.ts +6 -5
- package/constant/types.ts +9 -1
- package/constant/via.ts +312 -57
- package/dictionary/dictInfo.ts +28 -38
- package/dictionary/locale.ts +36 -38
- package/document/by.ts +84 -21
- package/document/database.ts +53 -18
- package/document/databaseRegistry.ts +7 -3
- package/document/filterMeta.ts +26 -14
- package/document/into.ts +71 -76
- package/document/types.ts +4 -2
- package/fetch/fetchType/endpointFetch.type.ts +6 -3
- package/fetch/fetchType/sliceFetch.type.ts +24 -13
- package/package.json +1 -1
- package/service/predefinedAdaptor/compress.adaptor.ts +9 -9
- package/service/predefinedAdaptor/database.adaptor.ts +3 -7
- package/service/serve.ts +17 -52
- package/service/types.ts +61 -21
- package/signal/endpoint.ts +2 -1
- package/signal/endpointInfo.ts +53 -23
- package/signal/internal.ts +2 -1
- package/signal/internalInfo.ts +71 -23
- package/signal/serverSignal.ts +23 -57
- package/signal/slice.ts +35 -35
- package/signal/sliceInfo.ts +63 -47
- package/signal/types.ts +23 -17
- package/store/action.ts +57 -44
- package/store/state.ts +47 -36
- package/store/stateBuilder.ts +3 -3
- package/store/store.ts +61 -15
- package/store/types.ts +16 -6
- package/types/base/base.d.ts +5 -1
- package/types/base/primitiveRegistry.d.ts +43 -38
- package/types/base/symbols.d.ts +5 -0
- package/types/base/types.d.ts +1 -0
- package/types/constant/constantRegistry.d.ts +3 -8
- package/types/constant/deserialize.d.ts +2 -2
- package/types/constant/fieldInfo.d.ts +41 -20
- package/types/constant/immerify.d.ts +2 -2
- package/types/constant/purify.d.ts +3 -2
- package/types/constant/serialize.d.ts +2 -2
- package/types/constant/types.d.ts +6 -1
- package/types/constant/via.d.ts +143 -22
- package/types/dictionary/dictInfo.d.ts +18 -11
- package/types/dictionary/locale.d.ts +23 -16
- package/types/document/by.d.ts +44 -7
- package/types/document/database.d.ts +9 -7
- package/types/document/databaseRegistry.d.ts +5 -4
- package/types/document/filterMeta.d.ts +18 -11
- package/types/document/into.d.ts +46 -19
- package/types/document/types.d.ts +3 -2
- package/types/fetch/fetchType/endpointFetch.type.d.ts +2 -2
- package/types/fetch/fetchType/sliceFetch.type.d.ts +11 -11
- package/types/service/serve.d.ts +4 -5
- package/types/service/serviceModule.d.ts +5 -5
- package/types/service/types.d.ts +23 -5
- package/types/signal/endpoint.d.ts +2 -1
- package/types/signal/endpointInfo.d.ts +40 -15
- package/types/signal/internal.d.ts +2 -1
- package/types/signal/internalInfo.d.ts +46 -12
- package/types/signal/serverSignal.d.ts +15 -12
- package/types/signal/slice.d.ts +8 -6
- package/types/signal/sliceInfo.d.ts +41 -20
- package/types/signal/types.d.ts +23 -17
- package/types/store/action.d.ts +25 -22
- package/types/store/baseSt.d.ts +42 -13
- package/types/store/state.d.ts +23 -26
- package/types/store/stateBuilder.d.ts +2 -2
- package/types/store/store.d.ts +17 -5
- package/types/store/types.d.ts +9 -3
- package/ui/Signal/makeExample.ts +3 -3
package/document/into.ts
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
|
-
import { type Cls, LOADER_META, type
|
|
1
|
+
import { type Cls, LOADER_META, type PromiseOrObject } from "akanjs/base";
|
|
2
2
|
import { applyMixins } from "akanjs/common";
|
|
3
|
-
import type {
|
|
4
|
-
import type {
|
|
5
|
-
import type {
|
|
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
|
|
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:
|
|
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<
|
|
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<
|
|
69
|
-
find(query:
|
|
70
|
-
findOne(query:
|
|
71
|
-
findById(id: string | undefined, projection?:
|
|
72
|
-
countDocuments(query:
|
|
73
|
-
exists(query:
|
|
74
|
-
updateOne(
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
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
|
-
|
|
124
|
+
FilterRef extends FilterCls,
|
|
87
125
|
T extends string,
|
|
88
|
-
|
|
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
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
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:
|
|
123
|
-
cnst:
|
|
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
|
-
|
|
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
|
-
|
|
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?:
|
|
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?:
|
|
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
|
|
28
|
-
SlceCls
|
|
29
|
-
SlceCls
|
|
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 {
|
|
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
|
|
10
|
-
type _Cap<S extends SliceCls> = S
|
|
11
|
-
type _Input<S extends SliceCls> = S
|
|
12
|
-
type _Full<S extends SliceCls> = S
|
|
13
|
-
type _Light<S extends SliceCls> = S
|
|
14
|
-
type _Insight<S extends SliceCls> = S
|
|
15
|
-
type _PurifiedInput<S extends SliceCls> = S
|
|
16
|
-
type _DefaultInput<S extends SliceCls> = S
|
|
17
|
-
type
|
|
18
|
-
type
|
|
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
|
@@ -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:
|
|
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:
|
|
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
|
|
149
|
-
allModelRefs.push(relatedRef as
|
|
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:
|
|
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:
|
|
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
|
|
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
|
|
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,
|
|
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 { [
|
|
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 { [
|
|
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
|
|
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
|
-
|
|
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<
|
|
105
|
-
export function serve<
|
|
106
|
-
|
|
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<
|
|
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:
|
|
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:
|
|
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:
|
|
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:
|
|
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
|
-
|
|
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
|
|
47
|
-
_Sort
|
|
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
|
-
} &
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
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
|
+
>;
|
package/signal/endpoint.ts
CHANGED
|
@@ -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
|
|
14
|
+
baseName: SrvRefName<SrvModule>;
|
|
14
15
|
srv: SrvModule;
|
|
15
16
|
[ENDPOINT_META]: EndpointInfoObj;
|
|
16
17
|
}
|