Skip to content

Commit

Permalink
fix(QuickMongo)^Cextracting the value from V if in array when returni…
Browse files Browse the repository at this point in the history
…ng an array value
  • Loading branch information
shadowplay1 committed Jan 14, 2024
1 parent 47496a9 commit d7161ca
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 70 deletions.
4 changes: 2 additions & 2 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

## 🕘 | Changelog

**v2.0.3**
- Typings bugfixes.
**v2.0.5**
- Typings bug fixes.

**v2.0.2**
- Removed the `TValue` type parameter from all database operations methods due to it breaking the type of the specifying value.
Expand Down
17 changes: 8 additions & 9 deletions docs/classes/QuickMongo.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,24 +237,24 @@ Determines whether the specified target is a number.
```


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

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

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

- **Returns:** `Promise<V[]>` - Updated target array from the database.
- **Returns:** `Promise<ExtractFromArray<V>[]>` - Updated target array from the database.
- **Example:**
```ts
const membersPushResult = await quickMongo.push('members', 'William');
console.log(membersPushResult); // -> ['John', 'William']
```


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

**[!!!] The type of target value must be an array.**
Expand All @@ -264,31 +264,30 @@ Pushes the specified value into the target array in the database.
- `targetArrayElementIndex` (`number`): The index to find the element in target array.
- `value` (`V`): The value to be pushed into the target array.

- **Returns:** `Promise<V[]>` - Updated target array from the database.
- **Returns:** `Promise<ExtractFromArray<V>[]>` - Updated target array from the database.
- **Example:**
```ts
const membersPullResult = await quickMongo.pull('members', 1, 'James');
console.log(membersPullResult); // -> ['John', 'James', 'Tom']
```


## `pop(key: K, targetArrayElementIndex: number, value: V): Promise<V[]>`
## `pop(key: K, ...targetArrayElementIndexes: RestOrArray<ExtractFromArray<number>>): Promise<ExtractFromArray<V>[]>`
Removes the specified element(s) from the target array in the database.

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

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

- **Returns:** `Promise<V[]>` - Updated target array from the database.
- **Returns:** `Promise<ExtractFromArray<V>[]>` - Updated target array from the database.
- **Example:**
```ts
const membersPopResult = await quickMongo.pop('members', 1);
console.log(membersPopResult); // -> ['John', 'Tom']
```


## `keys(key?: K): string[]`
Returns an array of object keys by specified database key.

Expand Down
11 changes: 11 additions & 0 deletions docs/types/ExtractFromArray.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# **`ExtractFromArray<T>` Type**

Extracts the type from the `Array<T>` type.

## Implemenatation
```ts
export type ExtractFromArray<A> = A extends Array<infer T> ? T : A
```
- **Type Parameters:**
- `A` (`any[]`): The array type to extract the type 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": "2.0.4",
"version": "2.0.5",
"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
68 changes: 10 additions & 58 deletions src/lib/QuickMongo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { typeOf } from './utils/functions/typeOf.function'
import { isNumber } from './utils/functions/isNumber.function'

import { QuickMongoError } from './utils/QuickMongoError'
import { If, IsObject, Maybe, RestOrArray } from '../types/utils'
import { ExtractFromArray, If, IsObject, Maybe, RestOrArray } from '../types/utils'

import { createTypesArray } from '../structures/errors'

Expand Down Expand Up @@ -582,39 +582,14 @@ export class QuickMongo<K extends string = string, V = any> {
return isNumber(target)
}

/**
* Pushes the specified value into the target array in database.
*
* [!!!] The type of target value must be an array.
*
* @param {K} key The key to access the target in database by.
* @param {V} value The value to be pushed into the target array in database.
* @returns {Promise<V[]>} Updated target array from database.
*
* @example
* const membersPushResult = await quickMongo.push('members', 'William')
* console.log(membersPushResult) // -> ['John', 'William']
*
* // You can also pass in multiple values to push into the target array:
* const currenciesPushResult = await quickMongo.push('currencies', 'Euro', 'Rupee')
* console.log(currenciesPushResult) // -> ['Dollar', 'Euro', 'Rupee']
*
* // ^ Assuming that the initial database object for this example is:
* // {
* // members: ['John'],
* // currencies: ['Dollar']
* // }
*/
public async push(key: K, value: V): Promise<V[]>

/**
* Pushes the specified value(s) into the target array in database.
*
* [!!!] The type of target value must be an array.
*
* @param {K} key The key to access the target in database by.
* @param {RestOrArray<V>} values The value(s) to be pushed into the target array in database.
* @returns {Promise<V[]>} Updated target array from database.
* @param {RestOrArray<ExtractFromArray<V>>} values The value(s) to be pushed into the target array in database.
* @returns {Promise<ExtractFromArray<V>[]>} Updated target array from database.
*
* @example
* const membersPushResult = await quickMongo.push('members', 'William')
Expand All @@ -630,7 +605,8 @@ export class QuickMongo<K extends string = string, V = any> {
* // currencies: ['Dollar']
* // }
*/
public async push(key: K, ...values: RestOrArray<V>): Promise<V[]> {
public async push(key: K, ...values: RestOrArray<ExtractFromArray<V>>): Promise<ExtractFromArray<V>[]> {
this.push('asd' as any, null as any, null as any)
const targetArray = this.get(key) || []

if (!Array.isArray(targetArray)) {
Expand All @@ -655,7 +631,7 @@ export class QuickMongo<K extends string = string, V = any> {
* @param {K} key The key to access the target in database by.
* @param {number} targetArrayElementIndex The index to find the element in target array by.
* @param {V} value The value to be pushed into the target array in database.
* @returns {Promise<V[]>} Updated target array from database.
* @returns {Promise<ExtractFromArray<V>[]>} Updated target array from database.
*
* @example
* const membersPullResult = await quickMongo.pull('members', 1, 'James')
Expand All @@ -666,7 +642,7 @@ export class QuickMongo<K extends string = string, V = any> {
* // members: ['John', 'William', 'Tom']
* // }
*/
public async pull(key: K, targetArrayElementIndex: number, value: V): Promise<V[]> {
public async pull(key: K, targetArrayElementIndex: number, value: V): Promise<ExtractFromArray<V>[]> {
const targetArray = this.get(key) ?? []

if (!Array.isArray(targetArray)) {
Expand Down Expand Up @@ -695,38 +671,14 @@ export class QuickMongo<K extends string = string, V = any> {
return targetArray
}

/**
* Removes the specified element from the target array in database.
*
* [!!!] The type of target value must be an array.
*
* @param {K} key The key to access the target in database by.
* @param {number} targetArrayElementIndex The index to find the element in target array by.
* @returns {Promise<V[]>} Updated target array from database.
*
* @example
* const membersPopResult = await quickMongo.pop('members', 1)
* console.log(membersPopResult) // -> ['John', 'Tom']
*
* const currenciesPopResult = await quickMongo.pop('currencies', 1)
* console.log(currenciesPopResult) // -> ['Dollar', 'Euro']
*
* // ^ Assuming that the initial database object for this example is:
* // {
* // members: ['John', 'William', 'Tom'],
* // currencies: ['Dollar', 'Rupee', 'Euro']
* // }
*/
public async pop(key: K, targetArrayElementIndex: number): Promise<V[]>

/**
* Removes the specified element(s) from the target array in database.
*
* [!!!] The type of target value must be an array.
*
* @param {K} key The key to access the target in database by.
* @param {RestOrArray<number>} targetArrayElementIndexes The index(es) to find the element(s) in target array by.
* @returns {Promise<V[]>} Updated target array from database.
* @param {RestOrArray<ExtractFromArray<number>>} targetArrayElementIndexes The index(es) to find the element(s) in target array by.
* @returns {Promise<ExtractFromArray<V>[]>} Updated target array from database.
*
* @example
* const membersPopResult = await quickMongo.pop('members', 1)
Expand All @@ -741,7 +693,7 @@ export class QuickMongo<K extends string = string, V = any> {
* // currencies: ['Dollar', 'Rupee', 'Euro']
* // }
*/
public async pop(key: K, ...targetArrayElementIndexes: RestOrArray<number>): Promise<V[]> {
public async pop(key: K, ...targetArrayElementIndexes: RestOrArray<ExtractFromArray<number>>): Promise<ExtractFromArray<V>[]> {
const targetArray = this.get(key) ?? []

if (!Array.isArray(targetArray)) {
Expand Down
6 changes: 6 additions & 0 deletions src/types/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,9 @@ export type IsObject<T> = T extends null
* @template T The type to convert into rest-or-array type.
*/
export type RestOrArray<T> = T[] | [T[]]

/**
* Extracts the type from the `Array<T>` type.
* @template T The array type to extract the type from.
*/
export type ExtractFromArray<A> = A extends Array<infer T> ? T : A

0 comments on commit d7161ca

Please sign in to comment.