Skip to content

Commit

Permalink
fix(QuickMongo): incorrectly inferred type when unexistent keys are i…
Browse files Browse the repository at this point in the history
…nputted
  • Loading branch information
shadowplay1 committed Aug 25, 2024
1 parent b4ce260 commit 3b7c82f
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 60 deletions.
3 changes: 1 addition & 2 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# git & github files
# git files
.git/
.github/

# husky scripts
.husky/
Expand Down
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

## 🕘 | Changelog

**v3.0.2**
- Fixed the type bug when inputting keys that don't exist in the interface would lead to unexpected `value` type.
- Fixed the incorrect return type of `QuickMongo.set()` method.

**v3.0.1**
- Now all the `key` parameters allow to pass in the normal `string` type, alongside the **keys autocomplete** feature released in **v3.0.0**.

Expand Down
2 changes: 1 addition & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# `quick-mongo-super v3.0.1` documentation
# `quick-mongo-super v3.0.2` documentation

## Classes
- [`QuickMongo<K, V>`](classes/QuickMongo.md)
Expand Down
62 changes: 31 additions & 31 deletions docs/classes/QuickMongo.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,11 @@ new QuickMongo<K, V>(quickMongoClient: QuickMongoClient, databaseOptions?: IData

## Methods

## `get<P extends ObjectPath<V>>(key: AutocompletableString<P>): Maybe<ObjectValue<V, P>>`
## `get<P extends AutocompletableString<ObjectPath<V>>>(key: AutocompletableString<P>): Maybe<ObjectValue<V, P>>`
Retrieves a value from database by a key.

- **Parameters:**
- `key` (`P`): The key to access the target in database by.
- `key` (`AutocompletableString<P>`): The key to access the target in database by.

- **Returns:** `Maybe<ObjectValue<V, P>>` - The value of the target in database.
- **Example:**
Expand All @@ -100,11 +100,11 @@ Retrieves a value from database by a key.
console.log(objectPropertyAccessed) // -> []
```

## `getFromDatabase<P extends ObjectPath<V>>(key: AutocompletableString<P>): Promise<Maybe<ObjectValue<V, P>>>`
## `getFromDatabase<P extends AutocompletableString<ObjectPath<V>>>(key: AutocompletableString<P>): Promise<Maybe<ObjectValue<V, P>>>`
Retrieves a value from database by a key via sending a **direct request** to remote cluster, **omitting** the cache.

- **Parameters:**
- `key` (`P`): The key to access the target in database by.
- `key` (`AutocompletableString<P>`): The key to access the target in database by.

- **Returns:** `Promise<Maybe<ObjectValue<V, P>>>` - The value of the target in database.
- **Example:**
Expand All @@ -118,11 +118,11 @@ Retrieves a value from database by a key via sending a **direct request** to rem
```


## `fetch<P extends ObjectPath<V>>(key: AutocompletableString<P>): Maybe<ObjectValue<V, P>>`
## `fetch<P extends AutocompletableString<ObjectPath<V>>>(key: AutocompletableString<P>): Maybe<ObjectValue<V, P>>`
Retrieves a value from database by a key.

- **Parameters:**
- `key` (`P`): The key to access the target in database by.
- `key` (`AutocompletableString<P>`): The key to access the target in database by.

- **Returns:** `Maybe<ObjectValue<V, P>>` - The value of the target in database.
- **Example:**
Expand All @@ -136,11 +136,11 @@ Retrieves a value from database by a key.
```


## `has<P extends ObjectPath<V>>(key: AutocompletableString<P>): boolean`
## `has<P extends AutocompletableString<ObjectPath<V>>>(key: AutocompletableString<P>): boolean`
Determines if the data is stored in database.

- **Parameters:**
- `key` (`P`): The key to access the target in database by.
- `key` (`AutocompletableString<P>`): The key to access the target in database by.

- **Returns:** `boolean` - Whether the data is stored in database.
- **Example:**
Expand All @@ -157,14 +157,14 @@ Determines if the data is stored in database.
```


## `set<P extends ObjectPath<V>>(key: AutocompletableString<P>, value: ObjectValue<V, P>): Promise<If<IsObject<V>, FirstObjectKey<P>, V>>`
## `set<P extends AutocompletableString<ObjectPath<V>>>(key: AutocompletableString<P>, value: ObjectValue<V, P>): Promise<If<IsObject<V>, ObjectValue<V, FirstObjectKey<P>>, V>>`
Writes the specified value into database under the specified key.

- **Parameters:**
- `key` (`P`): The key to write in the target.
- `key` (`AutocompletableString<P>`): The key to write in the target.
- `value` (`ObjectValue<V, P>`): The value to write.

- **Returns:** `Promise<If<IsObject<V>, FirstObjectKey<P>, V>>`:
- **Returns:** `Promise<If<IsObject<V>, ObjectValue<V, FirstObjectKey<P>>, V>>`:
- If the `value` parameter's type is not an object (string, number, boolean, etc), then the specified
`value` parameter (type of `V`) will be returned.

Expand All @@ -191,11 +191,11 @@ Writes the specified value into database under the specified key.
```


## `delete<P extends ObjectPath<V>>(key: AutocompletableString<P>): Promise<boolean>`
## `delete<P extends AutocompletableString<ObjectPath<V>>>(key: AutocompletableString<P>): Promise<boolean>`
Deletes the data from database by key.

- **Parameters:**
- `key` (`P`): The key to access the target in database by.
- `key` (`AutocompletableString<P>`): The key to access the target in database by.

- **Returns:** `Promise<boolean>` - Whether the deletition was successful.
- **Example:**
Expand All @@ -216,13 +216,13 @@ Sends a read, write and delete requests to the remote database and returns the r
```


## `add<P extends ObjectPath<V>>(key: AutocompletableString<P>, numberToAdd: number): Promise<number>`
## `add<P extends AutocompletableString<ObjectPath<V>>>(key: AutocompletableString<P>, numberToAdd: number): Promise<number>`
Performs an arithmetical addition on a target number in the database.

**[!!!] The type of target value must be a number.**

- **Parameters:**
- `key` (`P`): The key to access the target in database by.
- `key` (`AutocompletableString<P>`): The key to access the target in database by.
- `numberToAdd` (`number`): The number to add to the target number in the database.

- **Returns:** `Promise<number>` - Addition operation result.
Expand All @@ -234,23 +234,23 @@ Performs an arithmetical addition on a target number in the database.
```


## `subtract<P extends ObjectPath<V>>(key: AutocompletableString<P>, numberToSubtract: number): Promise<number>`
## `subtract<P extends AutocompletableString<ObjectPath<V>>>(key: AutocompletableString<P>, numberToSubtract: number): Promise<number>`
Performs an arithmetical subtraction on a target number in the database.

**[!!!] The type of target value must be a number.**

- **Parameters:**
- `key` (`P`): The key to access the target in database by.
- `key` (`AutocompletableString<P>`): The key to access the target in database by.
- `numberToSubtract` (`number`): The number to subtract from the target number in the database.

- **Returns:** `Promise<number>` - Subtraction operation result.


## `isTargetArray<P extends ObjectPath<V>>(key: AutocompletableString<P>): boolean`
## `isTargetArray<P extends AutocompletableString<ObjectPath<V>>>(key: AutocompletableString<P>): boolean`
Determines whether the specified target is an array.

- **Parameters:**
- `key` (`P`): The key to access the target in database by.
- `key` (`AutocompletableString<P>`): The key to access the target in database by.

- **Returns:** `boolean` - Whether the target is an array.
- **Example:**
Expand All @@ -260,11 +260,11 @@ Determines whether the specified target is an array.
```


## `isTargetNumber<P extends ObjectPath<V>>(key: AutocompletableString<P>): boolean`
## `isTargetNumber<P extends AutocompletableString<ObjectPath<V>>>(key: AutocompletableString<P>): boolean`
Determines whether the specified target is a number.

- **Parameters:**
- `key` (`P`): The key to access the target in database by.
- `key` (`AutocompletableString<P>`): The key to access the target in database by.

- **Returns:** `boolean` - Whether the target is a number.
- **Example:**
Expand Down Expand Up @@ -335,13 +335,13 @@ Iterates over root database values and checks if the specified condition in the
- **Returns:** `boolean


## `push<P extends ObjectPath<V>>(key: AutocompletableString<P>, ...values: RestOrArray<ExtractFromArray<ObjectValue<V, P>>>): Promise<ExtractFromArray<ObjectValue<V, P>>[]>`
## `push<P extends AutocompletableString<ObjectPath<V>>>(key: AutocompletableString<P>, ...values: RestOrArray<ExtractFromArray<ObjectValue<V, P>>>): Promise<ExtractFromArray<ObjectValue<V, P>>[]>`
Pushes the specified value(s) into the target array in the database.

**[!!!] The type of target value must be an array.**

- **Parameters:**
- `key` (`P`): The key to access the target in database by.
- `key` (`AutocompletableString<P>`): The key to access the target in database by.
- `values` (`RestOrArray<ExtractFromArray<ObjectValue<V, P>>>`): The value(s) to be pushed into the target array.

- **Returns:** `Promise<ExtractFromArray<ObjectValue<V, P>>[]> ` - Updated target array from the database.
Expand All @@ -352,13 +352,13 @@ Pushes the specified value(s) into the target array in the database.
```


## `pull<P extends ObjectPath<V>>(key: AutocompletableString<P>, targetArrayElementIndex: number, value: ObjectValue<V, P>): Promise<ExtractFromArray<ObjectValue<V, P>>[]>`
## `pull<P extends AutocompletableString<ObjectPath<V>>>(key: AutocompletableString<P>, targetArrayElementIndex: number, value: ObjectValue<V, P>): Promise<ExtractFromArray<ObjectValue<V, P>>[]>`
Pushes the specified value into the target array in the database.

**[!!!] The type of target value must be an array.**

- **Parameters:**
- `key` (`P`): The key to access the target in database by.
- `key` (`AutocompletableString<P>`): The key to access the target in database by.
- `targetArrayElementIndex` (`number`): The index to find the element in target array.
- `value` (`V`): The value to be pushed into the target array.

Expand All @@ -370,13 +370,13 @@ Pushes the specified value into the target array in the database.
```


## `pop<P extends ObjectPath<V>>(key: AutocompletableString<P>, ...targetArrayElementIndexes: RestOrArray<ExtractFromArray<number>>): Promise<ExtractFromArray<ObjectValue<V, P>>[]>`
## `pop<P extends AutocompletableString<ObjectPath<V>>>(key: AutocompletableString<P>, ...targetArrayElementIndexes: RestOrArray<ExtractFromArray<number>>): Promise<ExtractFromArray<ObjectValue<V, P>>[]>`
Removes the specified element(s) from the target array in the database.

**[!!!] The type of target value must be an array.**

- **Parameters:**
- `key` (`P`): The key to access the target in database by.
- `key` (`AutocompletableString<P>`): The key to access the target in database by.
- `targetArrayElementIndexes` (`RestOrArray<ExtractFromArray<number>>`): The index(es) to find the element(s) in target array by.

- **Returns:** `Promise<ExtractFromArray<ObjectValue<V, P>>[]>` - Updated target array from the database.
Expand All @@ -386,7 +386,7 @@ Removes the specified element(s) from the target array in the database.
console.log(membersPopResult); // -> ['John', 'Tom']
```

## `keys<P extends ObjectPath<V>>(key?: P): ObjectPath<P>[]`
## `keys<P extends AutocompletableString<ObjectPath<V>>>(key?: P): ObjectPath<P>[]`
Returns an array of object keys by specified database key.

- **Parameters:**
Expand All @@ -399,7 +399,7 @@ Returns an array of object keys by specified database key.
console.log(prop3Keys); // -> ['prop4', 'prop5']
```

## `values<P extends ObjectPath<V>>(key?: P): ObjectValue<V, P>[]`
## `values<P extends AutocompletableString<ObjectPath<V>>>(key?: P): ObjectValue<V, P>[]`
Returns an array of object values by specified database key.

- **Parameters:**
Expand All @@ -413,11 +413,11 @@ Returns an array of object values by specified database key.
```


## `random<P extends ObjectPath<V>>(key: AutocompletableString<P>): Maybe<ObjectValue<V, P>>`
## `random<P extends AutocompletableString<ObjectPath<V>>>(key: AutocompletableString<P>): Maybe<ObjectValue<V, P>>`
Picks a random element of array in the database and returns the picked array element.

- **Parameters:**
- `key` (`P`): The key to access the target in database by.
- `key` (`AutocompletableString<P>`): The key to access the target in database by.

- **Returns:** `Maybe<ObjectValue<V, P>>` - The randomly picked element in the database array.
- **Example:**
Expand Down
2 changes: 2 additions & 0 deletions docs/types/FirstObjectKey.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ Extracts the first key from the specified object path. (for example, in key `mem
export type FirstObjectKey<TKey extends ObjectPath<string, any>> =
TKey extends `${infer Key}.${infer _Rest}`
? Key
: TKey extends string
? TKey
: never
```
Expand Down
8 changes: 4 additions & 4 deletions docs/types/ObjectValue.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ Extracts the value from the specified object path.

## Implementation
```ts
export type ObjectValue<T, P extends ObjectPath<T>> =
T extends string | number | boolean | symbol
export type ObjectValue<T, P extends ObjectPath<T> | AutocompletableString<ObjectPath<T>>> =
T extends AutocompletableString<P> | string | number | boolean | symbol
? T
: P extends `${infer Key}.${infer Rest}`
? Key extends keyof T
Expand All @@ -21,9 +21,9 @@ export type ObjectValue<T, P extends ObjectPath<T>> =
: never
: P extends keyof T
? T[P]
: null
: T
```
- **Type Parameters:**
- `T` (`any`): The object to extract the value from.
- `P` (`ObjectPath<T>`): The object path to extract the value from.
- `P` (`ObjectPath<T>` or `AutocompletableString<ObjectPath<T>>`): The object path to extract the value from.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "quick-mongo-super",
"version": "3.0.1",
"version": "3.0.2",
"description": "Quick Mongo Super is a light-weight and easy-to-use Node.js module written in TypeScript to work with MongoDB.",
"main": "./dist/src/index.js",
"types": "./dist/typings/src/index.d.ts",
Expand Down
Loading

0 comments on commit 3b7c82f

Please sign in to comment.