@kanjijs/store 0.2.0-beta.14 → 0.2.0-beta.15
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/dist/contracts/src/index.d.ts +27 -0
- package/dist/core/src/context.d.ts +7 -0
- package/dist/core/src/decorators.d.ts +34 -0
- package/dist/core/src/di/container.d.ts +26 -0
- package/dist/core/src/di/module-compiler.d.ts +11 -0
- package/dist/core/src/exceptions/exception.filter.d.ts +3 -0
- package/dist/core/src/exceptions/http.exception.d.ts +7 -0
- package/dist/core/src/index.d.ts +9 -0
- package/dist/core/src/metadata.d.ts +64 -0
- package/dist/index.js +111 -66
- package/dist/store/src/index.d.ts +12 -0
- package/package.json +3 -3
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export type SchemaLike = unknown;
|
|
2
|
+
export interface ContractRequestSpec<T = SchemaLike> {
|
|
3
|
+
params?: T;
|
|
4
|
+
query?: T;
|
|
5
|
+
headers?: T;
|
|
6
|
+
cookies?: T;
|
|
7
|
+
body?: T;
|
|
8
|
+
}
|
|
9
|
+
export interface ContractResponseSpec<T = SchemaLike> {
|
|
10
|
+
[status: number]: T;
|
|
11
|
+
}
|
|
12
|
+
export interface ContractSpec<T = SchemaLike> {
|
|
13
|
+
request?: ContractRequestSpec<T>;
|
|
14
|
+
response?: ContractResponseSpec<T>;
|
|
15
|
+
}
|
|
16
|
+
export interface ValidatorAdapter<S = SchemaLike> {
|
|
17
|
+
parse<O = unknown>(schema: S, data: unknown): Promise<{
|
|
18
|
+
success: true;
|
|
19
|
+
data: O;
|
|
20
|
+
} | {
|
|
21
|
+
success: false;
|
|
22
|
+
issues: unknown[];
|
|
23
|
+
}>;
|
|
24
|
+
}
|
|
25
|
+
export declare class Contract {
|
|
26
|
+
static json<T = SchemaLike>(spec: ContractRequestSpec<T>): ContractSpec<T>;
|
|
27
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
2
|
+
export interface RequestContext {
|
|
3
|
+
requestId: string;
|
|
4
|
+
[key: string]: any;
|
|
5
|
+
}
|
|
6
|
+
export declare const kanjijsContext: AsyncLocalStorage<RequestContext>;
|
|
7
|
+
export declare function getRequestId(): string | undefined;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { ContractSpec } from "@kanjijs/contracts";
|
|
2
|
+
import { type ModuleMetadata, type Token } from "./metadata";
|
|
3
|
+
/**
|
|
4
|
+
* @Module({ controllers: [...] })
|
|
5
|
+
*/
|
|
6
|
+
export declare function Module(metadata: ModuleMetadata): ClassDecorator;
|
|
7
|
+
/**
|
|
8
|
+
* @Contract({ ... })
|
|
9
|
+
*/
|
|
10
|
+
export declare function Contract(spec: ContractSpec): MethodDecorator;
|
|
11
|
+
/**
|
|
12
|
+
* @Controller('/users')
|
|
13
|
+
*/
|
|
14
|
+
export declare function Controller(prefix?: string): ClassDecorator;
|
|
15
|
+
export declare function Injectable(): ClassDecorator;
|
|
16
|
+
export declare const Get: (path?: string) => MethodDecorator;
|
|
17
|
+
export declare const Post: (path?: string) => MethodDecorator;
|
|
18
|
+
export declare const Put: (path?: string) => MethodDecorator;
|
|
19
|
+
export declare const Delete: (path?: string) => MethodDecorator;
|
|
20
|
+
export declare const Patch: (path?: string) => MethodDecorator;
|
|
21
|
+
/**
|
|
22
|
+
* @Inject("DATABASE_CLIENT")
|
|
23
|
+
*/
|
|
24
|
+
export declare function Inject(token: Token<unknown>): ParameterDecorator;
|
|
25
|
+
/**
|
|
26
|
+
* @Use(middleware1, middleware2)
|
|
27
|
+
* Attaches middlewares to a controller or method.
|
|
28
|
+
*/
|
|
29
|
+
export declare function Use(...middlewares: unknown[]): MethodDecorator & ClassDecorator;
|
|
30
|
+
export declare const Body: (data?: string) => ParameterDecorator;
|
|
31
|
+
export declare const Query: (data?: string) => ParameterDecorator;
|
|
32
|
+
export declare const Param: (data?: string) => ParameterDecorator;
|
|
33
|
+
export declare const Headers: (data?: string) => ParameterDecorator;
|
|
34
|
+
export declare const Ctx: (data?: string) => ParameterDecorator;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { type Constructor, type Token } from "../metadata";
|
|
2
|
+
import "reflect-metadata";
|
|
3
|
+
export declare class KanjijsIoC {
|
|
4
|
+
private static providers;
|
|
5
|
+
static register<T>(target: Constructor<T>): void;
|
|
6
|
+
static register<T>(token: Token<T>, provider: {
|
|
7
|
+
useValue?: T;
|
|
8
|
+
useClass?: Constructor<T>;
|
|
9
|
+
}): void;
|
|
10
|
+
static resolve<T>(target: Token<T>): T;
|
|
11
|
+
static clear(): void;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* V2 STRICT CONTAINER
|
|
15
|
+
* Instance-based, no auto-registration, explicit visibility.
|
|
16
|
+
*/
|
|
17
|
+
export declare class Container {
|
|
18
|
+
private providers;
|
|
19
|
+
register<T>(token: Token<T>, provider: {
|
|
20
|
+
useValue?: T;
|
|
21
|
+
useClass?: Constructor<T>;
|
|
22
|
+
useFactory?: (...args: unknown[]) => T;
|
|
23
|
+
inject?: Array<Token<unknown>>;
|
|
24
|
+
}): void;
|
|
25
|
+
resolve<T>(token: Token<T>): T;
|
|
26
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { type Constructor } from "../metadata";
|
|
2
|
+
import { Container } from "./container";
|
|
3
|
+
export declare class ModuleCompiler {
|
|
4
|
+
private nodes;
|
|
5
|
+
private globalExportedTokens;
|
|
6
|
+
compile(rootModule: Constructor): Container;
|
|
7
|
+
private scan;
|
|
8
|
+
private processProviders;
|
|
9
|
+
private validate;
|
|
10
|
+
private checkDependencies;
|
|
11
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export type { ContractRequestSpec, ContractResponseSpec, ContractSpec, SchemaLike, ValidatorAdapter, } from "@kanjijs/contracts";
|
|
2
|
+
export * from "./context";
|
|
3
|
+
export * from "./decorators";
|
|
4
|
+
export * from "./di/container";
|
|
5
|
+
export * from "./di/module-compiler";
|
|
6
|
+
export * from "./exceptions/exception.filter";
|
|
7
|
+
export * from "./exceptions/http.exception";
|
|
8
|
+
export * from "./metadata";
|
|
9
|
+
export declare const GLOBAL_MIDDLEWARE_TOKEN: unique symbol;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import type { ContractSpec } from "@kanjijs/contracts";
|
|
2
|
+
import "reflect-metadata";
|
|
3
|
+
export type Constructor<T = unknown> = new (...args: unknown[]) => T;
|
|
4
|
+
export type HttpMethod = "GET" | "POST" | "PUT" | "DELETE" | "PATCH";
|
|
5
|
+
export type RouteParamType = "BODY" | "QUERY" | "PARAM" | "HEADERS" | "CONTEXT";
|
|
6
|
+
export interface RouteParamMetadata {
|
|
7
|
+
index: number;
|
|
8
|
+
type: RouteParamType;
|
|
9
|
+
data?: string;
|
|
10
|
+
}
|
|
11
|
+
export interface RouteMetadata {
|
|
12
|
+
method: HttpMethod;
|
|
13
|
+
path: string;
|
|
14
|
+
contract?: ContractSpec;
|
|
15
|
+
middlewares?: unknown[];
|
|
16
|
+
params?: RouteParamMetadata[];
|
|
17
|
+
}
|
|
18
|
+
export interface ControllerMetadata {
|
|
19
|
+
prefix: string;
|
|
20
|
+
middlewares?: unknown[];
|
|
21
|
+
}
|
|
22
|
+
export type Token<T = unknown> = string | symbol | Constructor<T>;
|
|
23
|
+
export type Provider<T = unknown> = Constructor<T> | {
|
|
24
|
+
provide: Token<T>;
|
|
25
|
+
useValue: T;
|
|
26
|
+
} | {
|
|
27
|
+
provide: Token<T>;
|
|
28
|
+
useClass: Constructor<T>;
|
|
29
|
+
} | {
|
|
30
|
+
provide: Token<T>;
|
|
31
|
+
useFactory: (...args: unknown[]) => T | Promise<T>;
|
|
32
|
+
inject?: Token[];
|
|
33
|
+
};
|
|
34
|
+
export interface DynamicModule {
|
|
35
|
+
module: Constructor;
|
|
36
|
+
providers?: Provider[];
|
|
37
|
+
imports?: Array<Constructor | DynamicModule>;
|
|
38
|
+
exports?: Token[];
|
|
39
|
+
global?: boolean;
|
|
40
|
+
}
|
|
41
|
+
export interface ModuleMetadata {
|
|
42
|
+
controllers?: Constructor[];
|
|
43
|
+
providers?: Provider[];
|
|
44
|
+
imports?: Array<Constructor | DynamicModule>;
|
|
45
|
+
exports?: Token[];
|
|
46
|
+
global?: boolean;
|
|
47
|
+
}
|
|
48
|
+
export interface IMetadataStorage {
|
|
49
|
+
addRoute(target: object, methodName: string, meta: RouteMetadata): void;
|
|
50
|
+
addContract(target: object, methodName: string, contract: ContractSpec): void;
|
|
51
|
+
addMiddleware(target: object, middleware: unknown, methodName?: string): void;
|
|
52
|
+
getRoutes(target: object): Map<string, RouteMetadata> | undefined;
|
|
53
|
+
setController(target: object, meta: ControllerMetadata): void;
|
|
54
|
+
getController(target: object): ControllerMetadata | undefined;
|
|
55
|
+
defineModule(target: object, meta: ModuleMetadata): void;
|
|
56
|
+
getModule(target: object): ModuleMetadata | undefined;
|
|
57
|
+
addInjection(target: object, index: number, token: Token<unknown>): void;
|
|
58
|
+
getInjections(target: object): Map<number, Token<unknown>> | undefined;
|
|
59
|
+
addRouteParam(target: object, methodName: string, param: RouteParamMetadata): void;
|
|
60
|
+
}
|
|
61
|
+
declare global {
|
|
62
|
+
var KANJI_METADATA_STORAGE: IMetadataStorage | undefined;
|
|
63
|
+
}
|
|
64
|
+
export declare const MetadataStorage: IMetadataStorage;
|
package/dist/index.js
CHANGED
|
@@ -1,17 +1,4 @@
|
|
|
1
1
|
// @bun
|
|
2
|
-
var __legacyDecorateClassTS = function(decorators, target, key, desc) {
|
|
3
|
-
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
-
if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
|
|
5
|
-
r = Reflect.decorate(decorators, target, key, desc);
|
|
6
|
-
else
|
|
7
|
-
for (var i = decorators.length - 1;i >= 0; i--)
|
|
8
|
-
if (d = decorators[i])
|
|
9
|
-
r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
10
|
-
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
// ../core/dist/index.js
|
|
14
|
-
import { AsyncLocalStorage } from "async_hooks";
|
|
15
2
|
var __create = Object.create;
|
|
16
3
|
var __getProtoOf = Object.getPrototypeOf;
|
|
17
4
|
var __defProp = Object.defineProperty;
|
|
@@ -29,21 +16,33 @@ var __toESM = (mod, isNodeMode, target) => {
|
|
|
29
16
|
return to;
|
|
30
17
|
};
|
|
31
18
|
var __commonJS = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports);
|
|
19
|
+
var __legacyDecorateClassTS = function(decorators, target, key, desc) {
|
|
20
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
21
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
|
|
22
|
+
r = Reflect.decorate(decorators, target, key, desc);
|
|
23
|
+
else
|
|
24
|
+
for (var i = decorators.length - 1;i >= 0; i--)
|
|
25
|
+
if (d = decorators[i])
|
|
26
|
+
r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
27
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
// ../../node_modules/.bun/reflect-metadata@0.2.2/node_modules/reflect-metadata/Reflect.js
|
|
32
31
|
var require_Reflect = __commonJS(() => {
|
|
33
32
|
/*! *****************************************************************************
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
33
|
+
Copyright (C) Microsoft. All rights reserved.
|
|
34
|
+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
|
|
35
|
+
this file except in compliance with the License. You may obtain a copy of the
|
|
36
|
+
License at http://www.apache.org/licenses/LICENSE-2.0
|
|
37
|
+
|
|
38
|
+
THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
39
|
+
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
|
|
40
|
+
WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
|
|
41
|
+
MERCHANTABLITY OR NON-INFRINGEMENT.
|
|
42
|
+
|
|
43
|
+
See the Apache Version 2.0 License for specific language governing permissions
|
|
44
|
+
and limitations under the License.
|
|
45
|
+
***************************************************************************** */
|
|
47
46
|
var Reflect2;
|
|
48
47
|
(function(Reflect3) {
|
|
49
48
|
(function(factory) {
|
|
@@ -1023,6 +1022,11 @@ var require_Reflect = __commonJS(() => {
|
|
|
1023
1022
|
});
|
|
1024
1023
|
})(Reflect2 || (Reflect2 = {}));
|
|
1025
1024
|
});
|
|
1025
|
+
|
|
1026
|
+
// ../core/src/context.ts
|
|
1027
|
+
import { AsyncLocalStorage } from "async_hooks";
|
|
1028
|
+
var kanjijsContext = new AsyncLocalStorage;
|
|
1029
|
+
// ../core/src/metadata.ts
|
|
1026
1030
|
var import_reflect_metadata = __toESM(require_Reflect(), 1);
|
|
1027
1031
|
var methodMetadataStore = new WeakMap;
|
|
1028
1032
|
var controllerMetadataStore = new WeakMap;
|
|
@@ -1050,6 +1054,16 @@ if (!globalStore.KANJI_METADATA_STORAGE) {
|
|
|
1050
1054
|
existing.contract = contract;
|
|
1051
1055
|
methods.set(methodName, existing);
|
|
1052
1056
|
},
|
|
1057
|
+
addRouteParam(target, methodName, param) {
|
|
1058
|
+
let methods = methodMetadataStore.get(target);
|
|
1059
|
+
if (!methods) {
|
|
1060
|
+
methods = new Map;
|
|
1061
|
+
methodMetadataStore.set(target, methods);
|
|
1062
|
+
}
|
|
1063
|
+
const existing = methods.get(methodName) || {};
|
|
1064
|
+
existing.params = [...existing.params || [], param];
|
|
1065
|
+
methods.set(methodName, existing);
|
|
1066
|
+
},
|
|
1053
1067
|
addMiddleware(target, middleware, methodName) {
|
|
1054
1068
|
if (methodName) {
|
|
1055
1069
|
let methods = methodMetadataStore.get(target);
|
|
@@ -1095,6 +1109,8 @@ if (!globalStore.KANJI_METADATA_STORAGE) {
|
|
|
1095
1109
|
};
|
|
1096
1110
|
}
|
|
1097
1111
|
var MetadataStorage = globalStore.KANJI_METADATA_STORAGE;
|
|
1112
|
+
|
|
1113
|
+
// ../core/src/decorators.ts
|
|
1098
1114
|
function Module(metadata) {
|
|
1099
1115
|
return (target) => {
|
|
1100
1116
|
MetadataStorage.defineModule(target, metadata);
|
|
@@ -1116,10 +1132,29 @@ var Put = createMethodDecorator("PUT");
|
|
|
1116
1132
|
var Delete = createMethodDecorator("DELETE");
|
|
1117
1133
|
var Patch = createMethodDecorator("PATCH");
|
|
1118
1134
|
function Inject(token) {
|
|
1119
|
-
return (target,
|
|
1135
|
+
return (target, _propertyKey, parameterIndex) => {
|
|
1120
1136
|
MetadataStorage.addInjection(target, parameterIndex, token);
|
|
1121
1137
|
};
|
|
1122
1138
|
}
|
|
1139
|
+
function createParamDecorator(type) {
|
|
1140
|
+
return (data) => {
|
|
1141
|
+
return (target, propertyKey, parameterIndex) => {
|
|
1142
|
+
if (propertyKey) {
|
|
1143
|
+
MetadataStorage.addRouteParam(target, propertyKey, {
|
|
1144
|
+
index: parameterIndex,
|
|
1145
|
+
type,
|
|
1146
|
+
data
|
|
1147
|
+
});
|
|
1148
|
+
}
|
|
1149
|
+
};
|
|
1150
|
+
};
|
|
1151
|
+
}
|
|
1152
|
+
var Body = createParamDecorator("BODY");
|
|
1153
|
+
var Query = createParamDecorator("QUERY");
|
|
1154
|
+
var Param = createParamDecorator("PARAM");
|
|
1155
|
+
var Headers = createParamDecorator("HEADERS");
|
|
1156
|
+
var Ctx = createParamDecorator("CONTEXT");
|
|
1157
|
+
// ../core/src/di/container.ts
|
|
1123
1158
|
var import_reflect_metadata2 = __toESM(require_Reflect(), 1);
|
|
1124
1159
|
|
|
1125
1160
|
class KanjijsIoC {
|
|
@@ -1127,43 +1162,46 @@ class KanjijsIoC {
|
|
|
1127
1162
|
static register(tokenOrTarget, provider) {
|
|
1128
1163
|
if (provider) {
|
|
1129
1164
|
if ("useValue" in provider) {
|
|
1130
|
-
|
|
1165
|
+
KanjijsIoC.providers.set(tokenOrTarget, { useValue: provider.useValue });
|
|
1131
1166
|
} else if ("useClass" in provider) {
|
|
1132
|
-
|
|
1167
|
+
KanjijsIoC.providers.set(tokenOrTarget, { useClass: provider.useClass });
|
|
1133
1168
|
}
|
|
1134
1169
|
} else {
|
|
1135
|
-
|
|
1170
|
+
KanjijsIoC.providers.set(tokenOrTarget, {
|
|
1171
|
+
useClass: tokenOrTarget
|
|
1172
|
+
});
|
|
1136
1173
|
}
|
|
1137
1174
|
}
|
|
1138
1175
|
static resolve(target) {
|
|
1139
|
-
let provider =
|
|
1176
|
+
let provider = KanjijsIoC.providers.get(target);
|
|
1140
1177
|
if (!provider && typeof target === "function") {
|
|
1141
1178
|
provider = { useClass: target };
|
|
1142
|
-
|
|
1179
|
+
KanjijsIoC.providers.set(target, provider);
|
|
1143
1180
|
}
|
|
1144
1181
|
if (!provider) {
|
|
1145
|
-
throw new Error(`Provider not found for token: ${target
|
|
1182
|
+
throw new Error(`Provider not found for token: ${typeof target === "function" ? target.name : String(target)}`);
|
|
1146
1183
|
}
|
|
1147
1184
|
if (provider.instance) {
|
|
1148
1185
|
return provider.instance;
|
|
1149
1186
|
}
|
|
1150
|
-
console.log(`[DI] Creating NEW instance for ${target
|
|
1187
|
+
console.log(`[DI] Creating NEW instance for ${typeof target === "function" ? target.name : String(target)}`);
|
|
1151
1188
|
if (provider.useValue !== undefined) {
|
|
1152
1189
|
provider.instance = provider.useValue;
|
|
1153
1190
|
} else if (provider.useClass) {
|
|
1154
1191
|
const ConcreteClass = provider.useClass;
|
|
1155
1192
|
const paramTypes = Reflect.getMetadata("design:paramtypes", ConcreteClass) || [];
|
|
1156
1193
|
const injectionTokens = MetadataStorage.getInjections(ConcreteClass) || new Map;
|
|
1157
|
-
const injections = paramTypes.map((
|
|
1194
|
+
const injections = paramTypes.map((paramToken, index) => {
|
|
1158
1195
|
const overrideToken = injectionTokens.get(index);
|
|
1159
|
-
|
|
1196
|
+
const resolvedToken = overrideToken || paramToken;
|
|
1197
|
+
return KanjijsIoC.resolve(resolvedToken);
|
|
1160
1198
|
});
|
|
1161
1199
|
provider.instance = new ConcreteClass(...injections);
|
|
1162
1200
|
}
|
|
1163
1201
|
return provider.instance;
|
|
1164
1202
|
}
|
|
1165
1203
|
static clear() {
|
|
1166
|
-
|
|
1204
|
+
KanjijsIoC.providers.clear();
|
|
1167
1205
|
}
|
|
1168
1206
|
}
|
|
1169
1207
|
|
|
@@ -1175,7 +1213,7 @@ class Container {
|
|
|
1175
1213
|
resolve(token) {
|
|
1176
1214
|
const provider = this.providers.get(token);
|
|
1177
1215
|
if (!provider) {
|
|
1178
|
-
throw new Error(`[DI] Provider not found for token: ${token
|
|
1216
|
+
throw new Error(`[DI] Provider not found for token: ${typeof token === "function" ? token.name : String(token)}`);
|
|
1179
1217
|
}
|
|
1180
1218
|
if (provider.instance) {
|
|
1181
1219
|
return provider.instance;
|
|
@@ -1188,7 +1226,8 @@ class Container {
|
|
|
1188
1226
|
const injectionTokens = MetadataStorage.getInjections(ConcreteClass) || new Map;
|
|
1189
1227
|
const injections = paramTypes.map((paramToken, index) => {
|
|
1190
1228
|
const overrideToken = injectionTokens.get(index);
|
|
1191
|
-
|
|
1229
|
+
const resolvedToken = overrideToken || paramToken;
|
|
1230
|
+
return this.resolve(resolvedToken);
|
|
1192
1231
|
});
|
|
1193
1232
|
provider.instance = new ConcreteClass(...injections);
|
|
1194
1233
|
} else if (provider.useFactory) {
|
|
@@ -1198,7 +1237,7 @@ class Container {
|
|
|
1198
1237
|
return provider.instance;
|
|
1199
1238
|
}
|
|
1200
1239
|
}
|
|
1201
|
-
|
|
1240
|
+
// ../core/src/di/module-compiler.ts
|
|
1202
1241
|
class ModuleCompiler {
|
|
1203
1242
|
nodes = new Map;
|
|
1204
1243
|
globalExportedTokens = new Set;
|
|
@@ -1208,7 +1247,8 @@ class ModuleCompiler {
|
|
|
1208
1247
|
const container = new Container;
|
|
1209
1248
|
for (const node of this.nodes.values()) {
|
|
1210
1249
|
for (const [token, provider] of node.providers) {
|
|
1211
|
-
|
|
1250
|
+
const { provide: _provide, ...definition } = provider;
|
|
1251
|
+
container.register(token, definition);
|
|
1212
1252
|
}
|
|
1213
1253
|
}
|
|
1214
1254
|
return container;
|
|
@@ -1227,20 +1267,20 @@ class ModuleCompiler {
|
|
|
1227
1267
|
};
|
|
1228
1268
|
this.nodes.set(moduleClass, node);
|
|
1229
1269
|
const meta = MetadataStorage.getModule(moduleClass) || {};
|
|
1230
|
-
const dynamicMeta = "module" in target ? target :
|
|
1231
|
-
const allProviders = [...meta.providers || [], ...dynamicMeta
|
|
1270
|
+
const dynamicMeta = "module" in target ? target : undefined;
|
|
1271
|
+
const allProviders = [...meta.providers || [], ...dynamicMeta?.providers || []];
|
|
1232
1272
|
this.processProviders(node, allProviders);
|
|
1233
|
-
const allExports = [...meta.exports || [], ...dynamicMeta
|
|
1273
|
+
const allExports = [...meta.exports || [], ...dynamicMeta?.exports || []];
|
|
1234
1274
|
for (const token of allExports) {
|
|
1235
1275
|
node.exports.add(token);
|
|
1236
1276
|
}
|
|
1237
|
-
if (meta.global || dynamicMeta
|
|
1277
|
+
if (meta.global || dynamicMeta?.global) {
|
|
1238
1278
|
node.isGlobal = true;
|
|
1239
1279
|
for (const token of node.exports) {
|
|
1240
1280
|
this.globalExportedTokens.add(token);
|
|
1241
1281
|
}
|
|
1242
1282
|
}
|
|
1243
|
-
const allImports = [...meta.imports || [], ...dynamicMeta
|
|
1283
|
+
const allImports = [...meta.imports || [], ...dynamicMeta?.imports || []];
|
|
1244
1284
|
for (const imp of allImports) {
|
|
1245
1285
|
const importedNode = this.scan(imp);
|
|
1246
1286
|
node.imports.add(importedNode);
|
|
@@ -1279,7 +1319,7 @@ class ModuleCompiler {
|
|
|
1279
1319
|
for (const globalToken of this.globalExportedTokens) {
|
|
1280
1320
|
visibleTokens.add(globalToken);
|
|
1281
1321
|
}
|
|
1282
|
-
for (const [
|
|
1322
|
+
for (const [_token, provider] of node.providers) {
|
|
1283
1323
|
this.checkDependencies(provider, visibleTokens, node.module.name);
|
|
1284
1324
|
}
|
|
1285
1325
|
}
|
|
@@ -1292,19 +1332,24 @@ class ModuleCompiler {
|
|
|
1292
1332
|
targetName = clazz.name;
|
|
1293
1333
|
const paramTypes = Reflect.getMetadata("design:paramtypes", clazz) || [];
|
|
1294
1334
|
const injectionTokens = MetadataStorage.getInjections(clazz) || new Map;
|
|
1295
|
-
dependencies = paramTypes.map((
|
|
1335
|
+
dependencies = paramTypes.map((paramType, index) => {
|
|
1336
|
+
const overrideToken = injectionTokens.get(index);
|
|
1337
|
+
return overrideToken || paramType;
|
|
1338
|
+
});
|
|
1296
1339
|
} else if ("useFactory" in provider) {
|
|
1297
|
-
targetName = provider.provide
|
|
1340
|
+
targetName = typeof provider.provide === "function" ? provider.provide.name : String(provider.provide);
|
|
1298
1341
|
dependencies = provider.inject || [];
|
|
1299
1342
|
}
|
|
1300
1343
|
for (const dep of dependencies) {
|
|
1301
1344
|
if (!visibleTokens.has(dep)) {
|
|
1302
|
-
const depName = dep
|
|
1345
|
+
const depName = typeof dep === "function" ? dep.name : String(dep);
|
|
1303
1346
|
throw new Error(`[Kanjijs] strict-di-error: Provider '${targetName}' in Module '${moduleName}' ` + `depends on '${depName}', but it is not visible. ` + `Make sure it is imported and exported by the source module.`);
|
|
1304
1347
|
}
|
|
1305
1348
|
}
|
|
1306
1349
|
}
|
|
1307
1350
|
}
|
|
1351
|
+
|
|
1352
|
+
// ../core/src/index.ts
|
|
1308
1353
|
var GLOBAL_MIDDLEWARE_TOKEN = Symbol("GLOBAL_MIDDLEWARE_TOKEN");
|
|
1309
1354
|
|
|
1310
1355
|
// ../../node_modules/.bun/drizzle-orm@0.30.10+45912b99425c1f32/node_modules/drizzle-orm/entity.js
|
|
@@ -1680,7 +1725,7 @@ class SQL {
|
|
|
1680
1725
|
params: []
|
|
1681
1726
|
};
|
|
1682
1727
|
}
|
|
1683
|
-
if (is(chunk,
|
|
1728
|
+
if (is(chunk, Param2)) {
|
|
1684
1729
|
const mappedValue = chunk.value === null ? null : chunk.encoder.mapToDriverValue(chunk.value);
|
|
1685
1730
|
if (is(mappedValue, SQL)) {
|
|
1686
1731
|
return this.buildQueryFromSourceParams([mappedValue], config);
|
|
@@ -1795,7 +1840,7 @@ var noopMapper = {
|
|
|
1795
1840
|
...noopEncoder
|
|
1796
1841
|
};
|
|
1797
1842
|
|
|
1798
|
-
class
|
|
1843
|
+
class Param2 {
|
|
1799
1844
|
constructor(value, encoder = noopEncoder) {
|
|
1800
1845
|
this.value = value;
|
|
1801
1846
|
this.encoder = encoder;
|
|
@@ -1849,7 +1894,7 @@ function sql(strings, ...params) {
|
|
|
1849
1894
|
}
|
|
1850
1895
|
sql2.placeholder = placeholder2;
|
|
1851
1896
|
function param2(value, encoder) {
|
|
1852
|
-
return new
|
|
1897
|
+
return new Param2(value, encoder);
|
|
1853
1898
|
}
|
|
1854
1899
|
sql2.param = param2;
|
|
1855
1900
|
})(sql || (sql = {}));
|
|
@@ -1998,7 +2043,7 @@ function mapUpdateSet(table, values) {
|
|
|
1998
2043
|
if (is(value, SQL)) {
|
|
1999
2044
|
return [key, value];
|
|
2000
2045
|
} else {
|
|
2001
|
-
return [key, new
|
|
2046
|
+
return [key, new Param2(value, table[Table.Symbol.Columns][key])];
|
|
2002
2047
|
}
|
|
2003
2048
|
});
|
|
2004
2049
|
if (entries.length === 0) {
|
|
@@ -2084,7 +2129,7 @@ class PgInsertBuilder {
|
|
|
2084
2129
|
const cols = this.table[Table.Symbol.Columns];
|
|
2085
2130
|
for (const colKey of Object.keys(entry)) {
|
|
2086
2131
|
const colValue = entry[colKey];
|
|
2087
|
-
result[colKey] = is(colValue, SQL) ? colValue : new
|
|
2132
|
+
result[colKey] = is(colValue, SQL) ? colValue : new Param2(colValue, cols[colKey]);
|
|
2088
2133
|
}
|
|
2089
2134
|
return result;
|
|
2090
2135
|
});
|
|
@@ -2429,8 +2474,8 @@ class PrimaryKey {
|
|
|
2429
2474
|
|
|
2430
2475
|
// ../../node_modules/.bun/drizzle-orm@0.30.10+45912b99425c1f32/node_modules/drizzle-orm/sql/expressions/conditions.js
|
|
2431
2476
|
function bindIfParam(value, column) {
|
|
2432
|
-
if (isDriverValueEncoder(column) && !isSQLWrapper(value) && !is(value,
|
|
2433
|
-
return new
|
|
2477
|
+
if (isDriverValueEncoder(column) && !isSQLWrapper(value) && !is(value, Param2) && !is(value, Placeholder) && !is(value, Column) && !is(value, Table) && !is(value, View)) {
|
|
2478
|
+
return new Param2(value, column);
|
|
2434
2479
|
}
|
|
2435
2480
|
return value;
|
|
2436
2481
|
}
|
|
@@ -3027,7 +3072,7 @@ class PgDialect {
|
|
|
3027
3072
|
const valueList = [];
|
|
3028
3073
|
for (const [fieldName, col] of colEntries) {
|
|
3029
3074
|
const colValue = value[fieldName];
|
|
3030
|
-
if (colValue === undefined || is(colValue,
|
|
3075
|
+
if (colValue === undefined || is(colValue, Param2) && colValue.value === undefined) {
|
|
3031
3076
|
if (col.defaultFn !== undefined) {
|
|
3032
3077
|
const defaultFnResult = col.defaultFn();
|
|
3033
3078
|
const defaultValue = is(defaultFnResult, SQL) ? defaultFnResult : sql.param(defaultFnResult, col);
|
|
@@ -4233,7 +4278,7 @@ var originStackCache = new Map;
|
|
|
4233
4278
|
var originError = Symbol("OriginError");
|
|
4234
4279
|
var CLOSE = {};
|
|
4235
4280
|
|
|
4236
|
-
class
|
|
4281
|
+
class Query2 extends Promise {
|
|
4237
4282
|
constructor(strings, args, handler, canceller, options = {}) {
|
|
4238
4283
|
let resolve, reject;
|
|
4239
4284
|
super((a, b) => {
|
|
@@ -4504,7 +4549,7 @@ function stringify(q, string, value, parameters, types2, options) {
|
|
|
4504
4549
|
return string;
|
|
4505
4550
|
}
|
|
4506
4551
|
function stringifyValue(string, value, parameters, types2, o) {
|
|
4507
|
-
return value instanceof Builder ? value.build(string, parameters, types2, o) : value instanceof
|
|
4552
|
+
return value instanceof Builder ? value.build(string, parameters, types2, o) : value instanceof Query2 ? fragment(value, parameters, types2, o) : value instanceof Identifier ? value.value : value && value[0] instanceof Query2 ? value.reduce((acc, x) => acc + " " + fragment(x, parameters, types2, o), "") : handleValue(value, parameters, types2, o);
|
|
4508
4553
|
}
|
|
4509
4554
|
function fragment(q, parameters, types2, options) {
|
|
4510
4555
|
q.fragment = true;
|
|
@@ -4526,7 +4571,7 @@ function select(first, rest, parameters, types2, options) {
|
|
|
4526
4571
|
const columns = rest.length ? rest.flat() : Object.keys(first);
|
|
4527
4572
|
return columns.map((x) => {
|
|
4528
4573
|
value = first[x];
|
|
4529
|
-
return (value instanceof
|
|
4574
|
+
return (value instanceof Query2 ? fragment(value, parameters, types2, options) : value instanceof Identifier ? value.value : handleValue(value, parameters, types2, options)) + " as " + escapeIdentifier(options.transform.column.to ? options.transform.column.to(x) : x);
|
|
4530
4575
|
}).join(",");
|
|
4531
4576
|
}
|
|
4532
4577
|
var builders = Object.entries({
|
|
@@ -5308,7 +5353,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
|
|
|
5308
5353
|
}
|
|
5309
5354
|
async function fetchArrayTypes() {
|
|
5310
5355
|
needsTypes = false;
|
|
5311
|
-
const types2 = await new
|
|
5356
|
+
const types2 = await new Query2([`
|
|
5312
5357
|
select b.oid, b.typarray
|
|
5313
5358
|
from pg_catalog.pg_type a
|
|
5314
5359
|
left join pg_catalog.pg_type b on b.oid = a.typelem
|
|
@@ -5331,7 +5376,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
|
|
|
5331
5376
|
return x === "read-write" && xs.default_transaction_read_only === "on" || x === "read-only" && xs.default_transaction_read_only === "off" || x === "primary" && xs.in_hot_standby === "on" || x === "standby" && xs.in_hot_standby === "off" || x === "prefer-standby" && xs.in_hot_standby === "off" && options.host[retries];
|
|
5332
5377
|
}
|
|
5333
5378
|
function fetchState() {
|
|
5334
|
-
const query2 = new
|
|
5379
|
+
const query2 = new Query2([`
|
|
5335
5380
|
show transaction_read_only;
|
|
5336
5381
|
select pg_catalog.pg_is_in_recovery()
|
|
5337
5382
|
`], [], execute, null, { simple: true });
|
|
@@ -5861,12 +5906,12 @@ function Postgres(a, b2) {
|
|
|
5861
5906
|
return new Parameter(value, type);
|
|
5862
5907
|
}
|
|
5863
5908
|
function sql3(strings, ...args) {
|
|
5864
|
-
const query = strings && Array.isArray(strings.raw) ? new
|
|
5909
|
+
const query = strings && Array.isArray(strings.raw) ? new Query2(strings, args, handler2, cancel) : typeof strings === "string" && !args.length ? new Identifier(options.transform.column.to ? options.transform.column.to(strings) : strings) : new Builder(strings, args);
|
|
5865
5910
|
return query;
|
|
5866
5911
|
}
|
|
5867
5912
|
function unsafe(string, args = [], options2 = {}) {
|
|
5868
5913
|
arguments.length === 2 && !Array.isArray(args) && (options2 = args, args = []);
|
|
5869
|
-
const query = new
|
|
5914
|
+
const query = new Query2([string], args, handler2, cancel, {
|
|
5870
5915
|
prepare: false,
|
|
5871
5916
|
...options2,
|
|
5872
5917
|
simple: "simple" in options2 ? options2.simple : args.length === 0
|
|
@@ -5875,7 +5920,7 @@ function Postgres(a, b2) {
|
|
|
5875
5920
|
}
|
|
5876
5921
|
function file(path, args = [], options2 = {}) {
|
|
5877
5922
|
arguments.length === 2 && !Array.isArray(args) && (options2 = args, args = []);
|
|
5878
|
-
const query = new
|
|
5923
|
+
const query = new Query2([], args, (query2) => {
|
|
5879
5924
|
fs.readFile(path, "utf8", (err, string) => {
|
|
5880
5925
|
if (err)
|
|
5881
5926
|
return query2.reject(err);
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { type DynamicModule } from "@kanjijs/core";
|
|
2
|
+
import { drizzle } from "drizzle-orm/postgres-js";
|
|
3
|
+
export declare const DATABASE_CLIENT: unique symbol;
|
|
4
|
+
export interface StoreModuleOptions {
|
|
5
|
+
url: string;
|
|
6
|
+
schema?: Record<string, unknown>;
|
|
7
|
+
}
|
|
8
|
+
export declare const InjectDb: () => ParameterDecorator;
|
|
9
|
+
export type DrizzleDb = ReturnType<typeof drizzle>;
|
|
10
|
+
export declare class StoreModule {
|
|
11
|
+
static forRoot(options: StoreModuleOptions): DynamicModule;
|
|
12
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kanjijs/store",
|
|
3
|
-
"version": "0.2.0-beta.
|
|
3
|
+
"version": "0.2.0-beta.15",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -17,9 +17,9 @@
|
|
|
17
17
|
"postgres": "^3.4.3"
|
|
18
18
|
},
|
|
19
19
|
"peerDependencies": {
|
|
20
|
-
"@kanjijs/core": "^0.2.0-beta.
|
|
20
|
+
"@kanjijs/core": "^0.2.0-beta.15"
|
|
21
21
|
},
|
|
22
22
|
"devDependencies": {
|
|
23
23
|
"bun-types": "latest"
|
|
24
24
|
}
|
|
25
|
-
}
|
|
25
|
+
}
|