@aromix/core 0.3.2 → 0.4.0

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 +55 -67
  2. package/dist/index.js +117 -82
  3. package/package.json +10 -3
package/dist/index.d.ts CHANGED
@@ -1,82 +1,70 @@
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
3
 
3
- interface KvAdapter {
4
- get(key: string): Promise<unknown>;
5
- set(key: string, value: unknown): Promise<void>;
6
- delete(key: string): Promise<void>;
7
- has(key: string): Promise<boolean>;
8
- }
9
- declare function KvAdapter(adapter: KvAdapter): KvAdapter;
10
-
11
- interface KvEntityInput<Schema extends AnySchema> {
4
+ interface KvEntityUserInput<Schema extends AnySchema> {
12
5
  name: string;
13
- adapter: KvAdapter;
14
6
  model: Schema;
15
7
  }
16
- interface KvEntityOutput<Schema extends AnySchema> {
17
- get(key: string): Promise<Schema['$infer']>;
18
- set(key: string, value: Schema['$infer']): Promise<void>;
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>;
19
14
  delete(key: string): Promise<void>;
20
15
  has(key: string): Promise<boolean>;
21
- state: {
22
- name: string;
23
- adapter: KvAdapter;
24
- model: Schema;
25
- };
26
16
  }
27
- declare function KvEntity<Schema extends AnySchema>(input: KvEntityInput<Schema>): KvEntityOutput<Schema>;
28
17
 
29
- interface MongoCollectionAdapter {
30
- insertOne(doc: any): Promise<any>;
31
- insertMany(docs: any): Promise<any>;
32
- findOne(filter: any): Promise<any>;
33
- find(filter: any): Promise<any>;
34
- updateOne(filter: any, update: any): Promise<any>;
35
- updateMany(filter: any, update: any): Promise<any>;
36
- deleteOne(filter: any): Promise<any>;
37
- deleteMany(filter: any): Promise<any>;
38
- countDocuments(filter?: any): Promise<any>;
39
- }
40
- interface MongoAdapter {
41
- collection(name: string): MongoCollectionAdapter;
42
- }
43
- declare function MongoAdapter(adapter: MongoAdapter): MongoAdapter;
44
-
45
- interface MongoEntityInput<Schema extends AnySchema> {
18
+ interface MongoEntityUserInput<Schema extends AnySchema> {
46
19
  name: string;
47
- adapter: MongoAdapter;
48
20
  model: Schema;
49
21
  }
50
- interface MongoEntityOutput<Schema extends AnySchema> {
51
- insertOne(doc: Schema['$infer']): Promise<{
52
- insertedId: unknown;
53
- }>;
54
- insertMany(docs: Schema['$infer'][]): Promise<{
55
- insertedIds: unknown[];
56
- }>;
57
- findOne(filter: Record<string, unknown>): Promise<Schema['$infer'] | null>;
58
- find(filter: Record<string, unknown>): Promise<Schema['$infer'][]>;
59
- updateOne(filter: Record<string, unknown>, update: Record<string, unknown>): Promise<{
60
- matchedCount: number;
61
- modifiedCount: number;
62
- }>;
63
- updateMany(filter: Record<string, unknown>, update: Record<string, unknown>): Promise<{
64
- matchedCount: number;
65
- modifiedCount: number;
66
- }>;
67
- deleteOne(filter: Record<string, unknown>): Promise<{
68
- deletedCount: number;
69
- }>;
70
- deleteMany(filter: Record<string, unknown>): Promise<{
71
- deletedCount: number;
72
- }>;
73
- countDocuments(filter?: Record<string, unknown>): Promise<number>;
74
- state: {
75
- name: string;
76
- adapter: MongoAdapter;
77
- model: Schema;
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>;
45
+ }
46
+
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>;
78
67
  };
79
68
  }
80
- declare function MongoEntity<Schema extends AnySchema>(input: MongoEntityInput<Schema>): MongoEntityOutput<Schema>;
81
69
 
82
- export { KvAdapter, KvEntity, type KvEntityInput, type KvEntityOutput, MongoAdapter, type MongoCollectionAdapter, MongoEntity, type MongoEntityInput, type MongoEntityOutput };
70
+ export { EntityBuilder, KvEntity, type KvEntityUserInput, MongoEntity, type MongoEntityUserInput };
package/dist/index.js CHANGED
@@ -1,88 +1,123 @@
1
- // src/kv/adapter.ts
2
- function KvAdapter(adapter) {
3
- return adapter;
4
- }
5
-
6
- // src/kv/entity.ts
7
- function KvEntity(input) {
8
- const adapter = input.adapter;
9
- return {
10
- async get(key) {
11
- const formattedKey = `${input.name}:${key}`;
12
- const raw = await adapter.get(formattedKey);
13
- return input.model.parse(raw);
14
- },
15
- async set(key, value) {
16
- const formattedKey = `${input.name}:${key}`;
17
- const validated = input.model.parse(value);
18
- await adapter.set(formattedKey, validated);
19
- },
20
- async delete(key) {
21
- const formattedKey = `${input.name}:${key}`;
22
- await adapter.delete(formattedKey);
23
- },
24
- async has(key) {
25
- const formattedKey = `${input.name}:${key}`;
26
- return await adapter.has(formattedKey);
27
- },
28
- state: {
29
- adapter,
30
- model: input.model,
31
- name: input.name
32
- }
33
- };
34
- }
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
+ };
35
27
 
36
- // src/mongo/adapter.ts
37
- function MongoAdapter(adapter) {
38
- return adapter;
39
- }
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);
86
+ }
87
+ async createIndex(indexSpec, options) {
88
+ return this.collection.createIndex(indexSpec, options);
89
+ }
90
+ async dropIndex(indexName, options) {
91
+ return this.collection.dropIndex(indexName, options);
92
+ }
93
+ };
40
94
 
41
- // src/mongo/entity.ts
42
- function MongoEntity(input) {
43
- const collection = input.adapter.collection(input.name);
44
- return {
45
- async insertOne(doc) {
46
- const validated = input.model.parse(doc);
47
- const result = await collection.insertOne(validated);
48
- return { insertedId: result.insertedId };
49
- },
50
- async insertMany(docs) {
51
- const validated = docs.map((d) => input.model.parse(d));
52
- const result = await collection.insertMany(validated);
53
- return { insertedIds: Object.values(result.insertedIds) };
54
- },
55
- async findOne(filter) {
56
- const raw = await collection.findOne(filter);
57
- return raw === null ? null : input.model.parse(raw);
58
- },
59
- async find(filter) {
60
- const raw = await collection.find(filter);
61
- return raw.map((r) => input.model.parse(r));
62
- },
63
- async updateOne(filter, update) {
64
- const result = await collection.updateOne(filter, update);
65
- return { matchedCount: result.matchedCount, modifiedCount: result.modifiedCount };
66
- },
67
- async updateMany(filter, update) {
68
- const result = await collection.updateMany(filter, update);
69
- return { matchedCount: result.matchedCount, modifiedCount: result.modifiedCount };
70
- },
71
- async deleteOne(filter) {
72
- const result = await collection.deleteOne(filter);
73
- return { deletedCount: result.deletedCount };
74
- },
75
- async deleteMany(filter) {
76
- const result = await collection.deleteMany(filter);
77
- return { deletedCount: result.deletedCount };
78
- },
79
- countDocuments: (filter) => collection.countDocuments(filter),
80
- state: { name: input.name, adapter: input.adapter, model: input.model }
81
- };
82
- }
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);
103
+ }
104
+ };
105
+ return result;
106
+ }
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);
113
+ }
114
+ };
115
+ return result;
116
+ }
117
+ EntityBuilder2.mongo = mongo;
118
+ })(EntityBuilder || (EntityBuilder = {}));
83
119
  export {
84
- KvAdapter,
120
+ EntityBuilder,
85
121
  KvEntity,
86
- MongoAdapter,
87
122
  MongoEntity
88
123
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aromix/core",
3
- "version": "0.3.2",
3
+ "version": "0.4.0",
4
4
  "description": "The Core Package For Aromix",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -41,12 +41,19 @@
41
41
  "typecheck": "tsc --noEmit"
42
42
  },
43
43
  "peerDependencies": {
44
- "@aromix/validator": "^0.2.0"
44
+ "@aromix/validator": "^0.3.0",
45
+ "mongodb": "^7.3.0"
45
46
  },
46
47
  "devDependencies": {
47
- "@aromix/validator": "^0.2.0",
48
+ "@aromix/validator": "^0.3.0",
48
49
  "@types/node": "^22.10.2",
50
+ "mongodb": "^7.3.0",
49
51
  "tsup": "^8.3.5",
50
52
  "typescript": "^5.7.2"
53
+ },
54
+ "peerDependenciesMeta": {
55
+ "mongodb": {
56
+ "optional": true
57
+ }
51
58
  }
52
59
  }