@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.
Files changed (3) hide show
  1. package/dist/index.d.ts +71 -57
  2. package/dist/index.js +111 -111
  3. package/package.json +9 -5
package/dist/index.d.ts CHANGED
@@ -1,70 +1,84 @@
1
1
  import { AnySchema } from '@aromix/validator';
2
- import { Db, InsertOneOptions, InsertOneResult, BulkWriteOptions, InsertManyResult, Filter, FindOptions, UpdateFilter, UpdateOptions, UpdateResult, ReplaceOptions, DeleteOptions, DeleteResult, FindOneAndUpdateOptions, FindOneAndDeleteOptions, FindOneAndReplaceOptions, CountDocumentsOptions, DistinctOptions, AggregateOptions, AggregationCursor, BulkWriteResult, CreateIndexesOptions, DropIndexesOptions } from 'mongodb';
2
+ import { Server } from 'http';
3
+ import { Db, MongoClient } from 'mongodb';
4
+ import { RedisClientType, RedisClusterType, RedisClientPoolType, RedisSentinelType } from 'redis';
3
5
 
4
- interface KvEntityUserInput<Schema extends AnySchema> {
6
+ type MaybePromise<T> = T | Promise<T>
7
+
8
+ interface Builder {
5
9
  name: string;
6
- model: Schema;
10
+ onRegister?(state: Record<string, any>): MaybePromise<void>;
11
+ onListen?(server: Server): MaybePromise<void>;
12
+ onShutdown?(): MaybePromise<void>;
7
13
  }
8
- declare class KvEntity<Schema extends AnySchema> {
9
- readonly state: KvEntityUserInput<Schema>;
10
- private adapter;
11
- constructor(userProvided: KvEntityUserInput<Schema>, internal: EntityBuilder.KvAdapter);
12
- get(key: string): Promise<Schema['$select']>;
13
- set(key: string, value: Schema['$insert']): Promise<void>;
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 MongoEntityUserInput<Schema extends AnySchema> {
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
- declare class MongoEntity<Schema extends AnySchema> {
23
- readonly states: MongoEntityUserInput<Schema>;
24
- private readonly collection;
25
- constructor(userInput: MongoEntityUserInput<Schema>, db: Db);
26
- insertOne(doc: Schema['$insert'], options?: InsertOneOptions): Promise<InsertOneResult>;
27
- insertMany(docs: Schema['$insert'][], options?: BulkWriteOptions): Promise<InsertManyResult>;
28
- findOne(filter: Filter<Schema['$select']>, options?: FindOptions): Promise<Schema['$select'] | null>;
29
- find(filter: Filter<Schema['$select']>, options?: FindOptions): Promise<Schema['$select'][]>;
30
- updateOne(filter: Filter<Schema['$select']>, update: UpdateFilter<Schema['$update']>, options?: UpdateOptions): Promise<UpdateResult>;
31
- updateMany(filter: Filter<Schema['$select']>, update: UpdateFilter<Schema['$update']>, options?: UpdateOptions): Promise<UpdateResult>;
32
- replaceOne(filter: Filter<Schema['$select']>, replacement: Schema['$select'], options?: ReplaceOptions): Promise<UpdateResult>;
33
- deleteOne(filter: Filter<Schema['$select']>, options?: DeleteOptions): Promise<DeleteResult>;
34
- deleteMany(filter: Filter<Schema['$select']>, options?: DeleteOptions): Promise<DeleteResult>;
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 namespace EntityBuilder {
48
- interface KvAdapter {
49
- get(key: string): Promise<unknown>;
50
- set(key: string, value: unknown): Promise<void>;
51
- delete(key: string): Promise<void>;
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 { EntityBuilder, KvEntity, type KvEntityUserInput, MongoEntity, type MongoEntityUserInput };
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/entity/platforms/kv.ts
2
- var KvEntity = class {
3
- state;
4
- adapter;
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/entity/platforms/mongo.ts
29
- var MongoEntity = class {
30
- states;
31
- collection;
32
- constructor(userInput, db) {
33
- this.states = userInput;
34
- this.collection = db.collection(userInput.name);
35
- }
36
- async insertOne(doc, options) {
37
- return this.collection.insertOne(doc, options);
38
- }
39
- async insertMany(docs, options) {
40
- return this.collection.insertMany(docs, options);
41
- }
42
- async findOne(filter, options) {
43
- return this.collection.findOne(filter, options);
44
- }
45
- async find(filter, options) {
46
- return this.collection.find(filter, options).toArray();
47
- }
48
- async updateOne(filter, update, options) {
49
- return this.collection.updateOne(filter, update, options);
50
- }
51
- async updateMany(filter, update, options) {
52
- return this.collection.updateMany(filter, update, options);
53
- }
54
- async replaceOne(filter, replacement, options) {
55
- return this.collection.replaceOne(filter, replacement, options);
56
- }
57
- async deleteOne(filter, options) {
58
- return this.collection.deleteOne(filter, options);
59
- }
60
- async deleteMany(filter, options) {
61
- return this.collection.deleteMany(filter, options);
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
- async createIndex(indexSpec, options) {
88
- return this.collection.createIndex(indexSpec, options);
41
+ /** internal attach */
42
+ attach(db) {
43
+ this.db = db;
89
44
  }
90
- async dropIndex(indexName, options) {
91
- return this.collection.dropIndex(indexName, options);
45
+ entity(input) {
92
46
  }
93
47
  };
94
48
 
95
- // src/entity/builder.ts
96
- var EntityBuilder;
97
- ((EntityBuilder2) => {
98
- function kv(builderInput) {
99
- const adapter = builderInput.adapter();
100
- const result = {
101
- entity(entityInput) {
102
- return new KvEntity(entityInput, adapter);
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
- return result;
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
- EntityBuilder2.kv = kv;
108
- function mongo(builderInput) {
109
- const db = builderInput.adapter();
110
- const result = {
111
- entity(entityInput) {
112
- return new MongoEntity(entityInput, db);
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
- EntityBuilder2.mongo = mongo;
118
- })(EntityBuilder || (EntityBuilder = {}));
110
+ };
111
+ function bootstrap() {
112
+ return new Application();
113
+ }
119
114
  export {
120
- EntityBuilder,
121
- KvEntity,
122
- MongoEntity
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.0",
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
- "runtime-agnostic",
22
- "deno",
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
  }