@aromix/core 0.4.0 → 0.4.2
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/index.d.ts +71 -57
- package/dist/index.js +111 -111
- package/package.json +9 -5
package/dist/index.d.ts
CHANGED
|
@@ -1,70 +1,84 @@
|
|
|
1
1
|
import { AnySchema } from '@aromix/validator';
|
|
2
|
-
import {
|
|
2
|
+
import { Server } from 'http';
|
|
3
|
+
import { Db, MongoClient } from 'mongodb';
|
|
4
|
+
import { RedisClientType, RedisClusterType, RedisClientPoolType, RedisSentinelType } from 'redis';
|
|
3
5
|
|
|
4
|
-
|
|
6
|
+
type MaybePromise<T> = T | Promise<T>
|
|
7
|
+
|
|
8
|
+
interface Builder {
|
|
5
9
|
name: string;
|
|
6
|
-
|
|
10
|
+
onRegister?(state: Record<string, any>): MaybePromise<void>;
|
|
11
|
+
onListen?(server: Server): MaybePromise<void>;
|
|
12
|
+
onShutdown?(): MaybePromise<void>;
|
|
7
13
|
}
|
|
8
|
-
declare
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
delete(key: string): Promise<void>;
|
|
15
|
-
has(key: string): Promise<boolean>;
|
|
14
|
+
declare function Builder(def: Builder): Builder;
|
|
15
|
+
|
|
16
|
+
interface ValidateEnvConfig<Schema extends Record<string, AnySchema>> {
|
|
17
|
+
path: string;
|
|
18
|
+
schema: Schema;
|
|
19
|
+
onError?(err: unknown): void;
|
|
16
20
|
}
|
|
21
|
+
declare function ValidateEnv<const Schema extends Record<string, AnySchema>>(config: ValidateEnvConfig<Schema>): readonly [Builder, <Key extends keyof Schema>(key: Key, fallback?: Schema[Key]["$base"]) => Schema[Key]["$base"]];
|
|
17
22
|
|
|
18
|
-
interface
|
|
23
|
+
interface EffectDef {
|
|
24
|
+
name: string;
|
|
25
|
+
run(): void;
|
|
26
|
+
}
|
|
27
|
+
declare function effect(def: EffectDef): EffectDef;
|
|
28
|
+
|
|
29
|
+
interface GuardDef {
|
|
30
|
+
name: string;
|
|
31
|
+
run(): void;
|
|
32
|
+
}
|
|
33
|
+
declare function guard(def: GuardDef): GuardDef;
|
|
34
|
+
|
|
35
|
+
declare class MongoDatabase {
|
|
36
|
+
db: Db;
|
|
37
|
+
constructor();
|
|
38
|
+
/** internal attach */
|
|
39
|
+
attach(db: Db): void;
|
|
40
|
+
entity<Schema extends AnySchema>(input: MongoEntityInput<Schema>): void;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
type ClusterResult<T extends readonly string[]> = {
|
|
44
|
+
builder: Builder;
|
|
45
|
+
client: MongoClient;
|
|
46
|
+
} & {
|
|
47
|
+
[K in T[number]]: MongoDatabase;
|
|
48
|
+
};
|
|
49
|
+
interface MongoClusterInput<Databases extends readonly string[]> {
|
|
50
|
+
name: string;
|
|
51
|
+
uri: string;
|
|
52
|
+
databases: Databases;
|
|
53
|
+
onConnect?(client: MongoClient): MaybePromise<void>;
|
|
54
|
+
onDisconnect?(client: MongoClient): MaybePromise<void>;
|
|
55
|
+
onError?(err: unknown): MaybePromise<void>;
|
|
56
|
+
}
|
|
57
|
+
interface MongoEntityInput<Schema extends AnySchema> {
|
|
19
58
|
name: string;
|
|
20
59
|
model: Schema;
|
|
21
60
|
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
findOneAndUpdate(filter: Filter<Schema['$select']>, update: UpdateFilter<Schema['$update']>, options?: FindOneAndUpdateOptions): Promise<Schema['$select'] | null>;
|
|
36
|
-
findOneAndDelete(filter: Filter<Schema['$select']>, options?: FindOneAndDeleteOptions): Promise<Schema['$select'] | null>;
|
|
37
|
-
findOneAndReplace(filter: Filter<Schema['$select']>, replacement: Schema['$select'], options?: FindOneAndReplaceOptions): Promise<Schema['$select'] | null>;
|
|
38
|
-
countDocuments(filter: Filter<Schema['$select']>, options?: CountDocumentsOptions): Promise<number>;
|
|
39
|
-
estimatedDocumentCount(): Promise<number>;
|
|
40
|
-
distinct<Field extends keyof Schema['$select'] & string>(field: Field, filter: Filter<Schema['$select']>, options?: DistinctOptions): Promise<Array<Schema['$select'][Field]>>;
|
|
41
|
-
aggregate(pipeline: any[], options?: AggregateOptions): AggregationCursor;
|
|
42
|
-
bulkWrite(operations: any[], options?: BulkWriteOptions): Promise<BulkWriteResult>;
|
|
43
|
-
createIndex(indexSpec: any, options?: CreateIndexesOptions): Promise<string>;
|
|
44
|
-
dropIndex(indexName: string, options?: DropIndexesOptions): Promise<void>;
|
|
61
|
+
|
|
62
|
+
declare function MongoCluster<const Databases extends readonly string[]>(options: MongoClusterInput<Databases>): ClusterResult<Databases>;
|
|
63
|
+
|
|
64
|
+
type RedisAdapter = RedisClientType | RedisClusterType | RedisClientPoolType | RedisSentinelType;
|
|
65
|
+
interface RedisInput {
|
|
66
|
+
name: string;
|
|
67
|
+
client(): RedisAdapter;
|
|
68
|
+
onConnect?(client: RedisAdapter): MaybePromise<void>;
|
|
69
|
+
onDisconnect?(client: RedisAdapter): MaybePromise<void>;
|
|
70
|
+
onError?(err: unknown): void;
|
|
71
|
+
}
|
|
72
|
+
declare class Redis {
|
|
73
|
+
constructor(input: RedisInput);
|
|
45
74
|
}
|
|
46
75
|
|
|
47
|
-
declare
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
has(key: string): Promise<boolean>;
|
|
53
|
-
}
|
|
54
|
-
interface KvInput {
|
|
55
|
-
transport: 'http';
|
|
56
|
-
adapter(): KvAdapter;
|
|
57
|
-
}
|
|
58
|
-
function kv(builderInput: KvInput): {
|
|
59
|
-
entity<Schema extends AnySchema>(entityInput: KvEntityUserInput<Schema>): KvEntity<Schema>;
|
|
60
|
-
};
|
|
61
|
-
interface MongoInput {
|
|
62
|
-
adapter(): Db;
|
|
63
|
-
transport: 'http';
|
|
64
|
-
}
|
|
65
|
-
function mongo(builderInput: MongoInput): {
|
|
66
|
-
entity<Schema extends AnySchema>(entityInput: MongoEntityUserInput<Schema>): MongoEntity<Schema>;
|
|
67
|
-
};
|
|
76
|
+
declare class Application {
|
|
77
|
+
private state;
|
|
78
|
+
private builders;
|
|
79
|
+
register(builder: Builder): void;
|
|
80
|
+
listen(port: number): Promise<void>;
|
|
68
81
|
}
|
|
82
|
+
declare function bootstrap(): Application;
|
|
69
83
|
|
|
70
|
-
export {
|
|
84
|
+
export { Builder, type ClusterResult, type EffectDef, type GuardDef, type MaybePromise, MongoCluster, type MongoClusterInput, MongoDatabase, type MongoEntityInput, Redis, type RedisAdapter, type RedisInput, ValidateEnv, type ValidateEnvConfig, bootstrap, effect, guard };
|
package/dist/index.js
CHANGED
|
@@ -1,123 +1,123 @@
|
|
|
1
|
-
// src/
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
constructor(userProvided, internal) {
|
|
6
|
-
this.state = userProvided;
|
|
7
|
-
this.adapter = internal;
|
|
8
|
-
}
|
|
9
|
-
async get(key) {
|
|
10
|
-
const formattedKey = `${this.state.name}:${key}`;
|
|
11
|
-
const raw = await this.adapter.get(formattedKey);
|
|
12
|
-
return raw;
|
|
13
|
-
}
|
|
14
|
-
async set(key, value) {
|
|
15
|
-
const formattedKey = `${this.state.name}:${key}`;
|
|
16
|
-
await this.adapter.set(formattedKey, value);
|
|
17
|
-
}
|
|
18
|
-
async delete(key) {
|
|
19
|
-
const formattedKey = `${this.state.name}:${key}`;
|
|
20
|
-
await this.adapter.delete(formattedKey);
|
|
21
|
-
}
|
|
22
|
-
async has(key) {
|
|
23
|
-
const formattedKey = `${this.state.name}:${key}`;
|
|
24
|
-
return this.adapter.has(formattedKey);
|
|
25
|
-
}
|
|
26
|
-
};
|
|
1
|
+
// src/server/builder.ts
|
|
2
|
+
function Builder(def) {
|
|
3
|
+
return def;
|
|
4
|
+
}
|
|
27
5
|
|
|
28
|
-
// src/
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
return
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
}
|
|
63
|
-
async findOneAndUpdate(filter, update, options) {
|
|
64
|
-
return this.collection.findOneAndUpdate(filter, update, options);
|
|
65
|
-
}
|
|
66
|
-
async findOneAndDelete(filter, options) {
|
|
67
|
-
return this.collection.findOneAndDelete(filter, options);
|
|
68
|
-
}
|
|
69
|
-
async findOneAndReplace(filter, replacement, options) {
|
|
70
|
-
return this.collection.findOneAndReplace(filter, replacement, options);
|
|
71
|
-
}
|
|
72
|
-
async countDocuments(filter, options) {
|
|
73
|
-
return this.collection.countDocuments(filter, options);
|
|
74
|
-
}
|
|
75
|
-
async estimatedDocumentCount() {
|
|
76
|
-
return this.collection.estimatedDocumentCount();
|
|
77
|
-
}
|
|
78
|
-
async distinct(field, filter, options) {
|
|
79
|
-
return this.collection.distinct(field, filter, options);
|
|
80
|
-
}
|
|
81
|
-
aggregate(pipeline, options) {
|
|
82
|
-
return this.collection.aggregate(pipeline, options);
|
|
83
|
-
}
|
|
84
|
-
async bulkWrite(operations, options) {
|
|
85
|
-
return this.collection.bulkWrite(operations, options);
|
|
6
|
+
// src/common/validate.env.ts
|
|
7
|
+
import { loadEnvFile } from "process";
|
|
8
|
+
function ValidateEnv(config) {
|
|
9
|
+
const builder = Builder({
|
|
10
|
+
name: "validate.env",
|
|
11
|
+
onRegister(state) {
|
|
12
|
+
loadEnvFile(config.path);
|
|
13
|
+
console.log(state);
|
|
14
|
+
console.log(process.env);
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
function env(key, fallback) {
|
|
18
|
+
return process.env[key] ?? fallback;
|
|
19
|
+
}
|
|
20
|
+
return [builder, env];
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// src/layer/effect.ts
|
|
24
|
+
function effect(def) {
|
|
25
|
+
return def;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// src/layer/guard.ts
|
|
29
|
+
function guard(def) {
|
|
30
|
+
return def;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// src/mongo/cluster.ts
|
|
34
|
+
import { MongoClient } from "mongodb";
|
|
35
|
+
|
|
36
|
+
// src/mongo/database.ts
|
|
37
|
+
var MongoDatabase = class {
|
|
38
|
+
db;
|
|
39
|
+
constructor() {
|
|
86
40
|
}
|
|
87
|
-
|
|
88
|
-
|
|
41
|
+
/** internal attach */
|
|
42
|
+
attach(db) {
|
|
43
|
+
this.db = db;
|
|
89
44
|
}
|
|
90
|
-
|
|
91
|
-
return this.collection.dropIndex(indexName, options);
|
|
45
|
+
entity(input) {
|
|
92
46
|
}
|
|
93
47
|
};
|
|
94
48
|
|
|
95
|
-
// src/
|
|
96
|
-
|
|
97
|
-
(
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
49
|
+
// src/mongo/cluster.ts
|
|
50
|
+
function MongoCluster(options) {
|
|
51
|
+
const client = new MongoClient(options.uri);
|
|
52
|
+
const databases = {};
|
|
53
|
+
for (const name of options.databases) {
|
|
54
|
+
databases[name] = new MongoDatabase();
|
|
55
|
+
}
|
|
56
|
+
const builder = Builder({
|
|
57
|
+
name: options.name,
|
|
58
|
+
async onListen() {
|
|
59
|
+
try {
|
|
60
|
+
await client.connect();
|
|
61
|
+
for (const name of options.databases) {
|
|
62
|
+
const db = client.db(name);
|
|
63
|
+
databases[name].attach(db);
|
|
64
|
+
}
|
|
65
|
+
await options.onConnect?.(client);
|
|
66
|
+
} catch (err) {
|
|
67
|
+
await options.onError?.(err);
|
|
68
|
+
throw err;
|
|
103
69
|
}
|
|
104
|
-
}
|
|
105
|
-
|
|
70
|
+
},
|
|
71
|
+
async onShutdown() {
|
|
72
|
+
await options.onDisconnect?.(client);
|
|
73
|
+
await client.close();
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
return {
|
|
77
|
+
client,
|
|
78
|
+
builder,
|
|
79
|
+
...databases
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// src/redis/redis.ts
|
|
84
|
+
var Redis = class {
|
|
85
|
+
constructor(input) {
|
|
106
86
|
}
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
// src/server/application.ts
|
|
90
|
+
import { createServer } from "http";
|
|
91
|
+
var Application = class {
|
|
92
|
+
state = {};
|
|
93
|
+
builders = [];
|
|
94
|
+
register(builder) {
|
|
95
|
+
this.builders.push(builder);
|
|
96
|
+
builder.onRegister?.(this.state);
|
|
97
|
+
}
|
|
98
|
+
async listen(port) {
|
|
99
|
+
const server = createServer();
|
|
100
|
+
for (const builder of this.builders) {
|
|
101
|
+
await builder.onListen?.(server);
|
|
102
|
+
}
|
|
103
|
+
server.listen(port);
|
|
104
|
+
server.on("close", async () => {
|
|
105
|
+
for (const builder of this.builders) {
|
|
106
|
+
await builder.onShutdown?.();
|
|
113
107
|
}
|
|
114
|
-
};
|
|
115
|
-
return result;
|
|
108
|
+
});
|
|
116
109
|
}
|
|
117
|
-
|
|
118
|
-
|
|
110
|
+
};
|
|
111
|
+
function bootstrap() {
|
|
112
|
+
return new Application();
|
|
113
|
+
}
|
|
119
114
|
export {
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
115
|
+
Builder,
|
|
116
|
+
MongoCluster,
|
|
117
|
+
MongoDatabase,
|
|
118
|
+
Redis,
|
|
119
|
+
ValidateEnv,
|
|
120
|
+
bootstrap,
|
|
121
|
+
effect,
|
|
122
|
+
guard
|
|
123
123
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aromix/core",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.2",
|
|
4
4
|
"description": "The Core Package For Aromix",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -18,9 +18,8 @@
|
|
|
18
18
|
"core",
|
|
19
19
|
"server",
|
|
20
20
|
"framework",
|
|
21
|
-
"
|
|
22
|
-
"
|
|
23
|
-
"node"
|
|
21
|
+
"node",
|
|
22
|
+
"rpc"
|
|
24
23
|
],
|
|
25
24
|
"files": [
|
|
26
25
|
"dist"
|
|
@@ -42,18 +41,23 @@
|
|
|
42
41
|
},
|
|
43
42
|
"peerDependencies": {
|
|
44
43
|
"@aromix/validator": "^0.3.0",
|
|
45
|
-
"mongodb": "^7.3.0"
|
|
44
|
+
"mongodb": "^7.3.0",
|
|
45
|
+
"redis": "^6.0.1"
|
|
46
46
|
},
|
|
47
47
|
"devDependencies": {
|
|
48
48
|
"@aromix/validator": "^0.3.0",
|
|
49
49
|
"@types/node": "^22.10.2",
|
|
50
50
|
"mongodb": "^7.3.0",
|
|
51
|
+
"redis": "^6.0.1",
|
|
51
52
|
"tsup": "^8.3.5",
|
|
52
53
|
"typescript": "^5.7.2"
|
|
53
54
|
},
|
|
54
55
|
"peerDependenciesMeta": {
|
|
55
56
|
"mongodb": {
|
|
56
57
|
"optional": true
|
|
58
|
+
},
|
|
59
|
+
"redis": {
|
|
60
|
+
"optional": true
|
|
57
61
|
}
|
|
58
62
|
}
|
|
59
63
|
}
|