vsrepo 2.0.0 → 2.2.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.
- package/README.md +180 -16
- package/README.pt-BR.md +180 -16
- package/dist/VSRepoAdapter.d.ts +38 -0
- package/dist/VSRepository.d.ts +45 -5
- package/dist/VSRepository.js +77 -11
- package/dist/decorators/query-method.decorator.d.ts +26 -3
- package/dist/decorators/query-method.decorator.js +25 -2
- package/dist/index.d.ts +8 -1
- package/dist/index.js +6 -1
- package/dist/internal/enums/vsrepo-error-type.enum.d.ts +1 -1
- package/dist/internal/enums/vsrepo-error-type.enum.js +1 -1
- package/dist/internal/resolvers/dynamic-methods.resolver.js +32 -6
- package/dist/internal/utils/db-arg.util.d.ts +15 -0
- package/dist/internal/utils/db-arg.util.js +22 -0
- package/dist/internal/utils/with-db.util.d.ts +19 -0
- package/dist/internal/utils/with-db.util.js +23 -0
- package/dist/internal/validators/decorators.validator.js +2 -0
- package/dist/internal/validators/vsrepo.validator.d.ts +8 -1
- package/dist/internal/validators/vsrepo.validator.js +23 -0
- package/dist/types/decorators/query-method-options.type.d.ts +35 -1
- package/dist/types/utils/decimal-like.type.d.ts +23 -0
- package/dist/types/utils/decimal-like.type.js +2 -0
- package/dist/types/utils/numeric-keys.type.d.ts +24 -0
- package/dist/types/utils/numeric-keys.type.js +2 -0
- package/dist/types/utils/numeric-like.type.d.ts +10 -0
- package/dist/types/utils/numeric-like.type.js +2 -0
- package/dist/types/utils/primitive.type.d.ts +2 -1
- package/dist/types/utils/query-args.type.d.ts +30 -0
- package/dist/types/utils/query-args.type.js +2 -0
- package/dist/types/utils/query-method-arg.type.d.ts +3 -2
- package/dist/types/utils/restrict-method-options.type.d.ts +14 -0
- package/dist/types/utils/restrict-method-options.type.js +2 -0
- package/dist/types/vsrepo/vsrepo-query-options.type.d.ts +14 -0
- package/package.json +1 -1
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { NumericLike } from "./numeric-like.type";
|
|
2
|
+
/**
|
|
3
|
+
* Extracts the keys of `T` whose (non-nullable) value type is assignable to
|
|
4
|
+
* {@link NumericLike} — i.e. the fields eligible as the `field` argument of
|
|
5
|
+
* `increment`/`decrement`/`multiply`/`divide`/`sum`/`average`/`min`/`max`.
|
|
6
|
+
*
|
|
7
|
+
* Nullable/optional numeric fields (e.g. `number | null`) ARE included —
|
|
8
|
+
* the `null`/`undefined` part is stripped before the check, it isn't a
|
|
9
|
+
* reason to exclude the field. This means a field that is currently `NULL`
|
|
10
|
+
* in the database can be targeted; be aware that in standard SQL, arithmetic
|
|
11
|
+
* against a `NULL` value (`NULL + 5`) itself stays `NULL` — this type only
|
|
12
|
+
* governs what compiles, not the row's runtime value.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* type Product = { id: string; price: Decimal; stock: number | null; name: string };
|
|
17
|
+
* type Numeric = NumericKeys<Product>; // "price" | "stock"
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* @publicApi
|
|
21
|
+
*/
|
|
22
|
+
export type NumericKeys<T> = {
|
|
23
|
+
[P in keyof T]: NonNullable<T[P]> extends NumericLike ? P : never;
|
|
24
|
+
}[keyof T];
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { DecimalLike } from "./decimal-like.type";
|
|
2
|
+
/**
|
|
3
|
+
* Union of value types accepted as "numeric" by the atomic
|
|
4
|
+
* (`increment`/`decrement`/`multiply`/`divide`) and aggregate
|
|
5
|
+
* (`sum`/`average`/`min`/`max`) operations: a native `number`, a native
|
|
6
|
+
* `bigint`, or a {@link DecimalLike} object.
|
|
7
|
+
*
|
|
8
|
+
* @publicApi
|
|
9
|
+
*/
|
|
10
|
+
export type NumericLike = number | bigint | DecimalLike;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
+
import { DecimalLike } from "./decimal-like.type";
|
|
1
2
|
/**
|
|
2
3
|
* Types treated as scalar (non-relation) values when walking an entity's shape.
|
|
3
4
|
*
|
|
4
5
|
* @publicApi
|
|
5
6
|
*/
|
|
6
|
-
export type Primitive = string | number | boolean | bigint | symbol | undefined | null | Date;
|
|
7
|
+
export type Primitive = string | number | boolean | bigint | symbol | undefined | null | Date | DecimalLike;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { DbArg } from "../../internal/utils/db-arg.util";
|
|
2
|
+
import { VSRepoOrmTypes } from "../vsrepo/vsrepo-orm-types.type";
|
|
3
|
+
/**
|
|
4
|
+
* Types the parameter list of a `@QueryMethod` declared with
|
|
5
|
+
* `{ spreadArgs: true }`: the SQL placeholder values (`T`), in order,
|
|
6
|
+
* followed by an optional trailing {@link DbArg} — built via {@link withDb} —
|
|
7
|
+
* to run the query against a specific client or transaction instead of the
|
|
8
|
+
* repository's default one.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* class UserRepository extends VSRepository<User, string> {
|
|
13
|
+
* @QueryMethod('SELECT * FROM "user" WHERE email = $1 AND "userType" = $2', {
|
|
14
|
+
* spreadArgs: true,
|
|
15
|
+
* })
|
|
16
|
+
* declare findByEmailAndType: (
|
|
17
|
+
* ...args: QueryArgs<[email: string, userType: string]>
|
|
18
|
+
* ) => Promise<User[]>;
|
|
19
|
+
* }
|
|
20
|
+
*
|
|
21
|
+
* await userRepository.findByEmailAndType("joao@email.com", "admin");
|
|
22
|
+
* await userRepository.findByEmailAndType("joao@email.com", "admin", withDb(tx));
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* @publicApi
|
|
26
|
+
*/
|
|
27
|
+
export type QueryArgs<T extends Array<any> = [], O extends VSRepoOrmTypes = VSRepoOrmTypes> = [
|
|
28
|
+
...T,
|
|
29
|
+
db?: DbArg<O>
|
|
30
|
+
];
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { VSRepoOrmTypes } from "../vsrepo/vsrepo-orm-types.type";
|
|
1
2
|
/**
|
|
2
3
|
* Single argument accepted by a method declared with `@QueryMethod`.
|
|
3
4
|
*
|
|
@@ -19,9 +20,9 @@
|
|
|
19
20
|
*
|
|
20
21
|
* @publicApi
|
|
21
22
|
*/
|
|
22
|
-
export type QueryMethodArg<T extends Array<any
|
|
23
|
+
export type QueryMethodArg<T extends Array<any> = [], O extends VSRepoOrmTypes = VSRepoOrmTypes> = {
|
|
23
24
|
/** Positional parameters injected into the SQL placeholders (`$1`, `$2`, ...). */
|
|
24
25
|
args?: T;
|
|
25
26
|
/** Database client or transaction to run this query in, instead of the repository's default client. */
|
|
26
|
-
db?:
|
|
27
|
+
db?: O["dbClient"] | O["dbTransaction"];
|
|
27
28
|
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { VSRepoOrmTypes } from "../vsrepo/vsrepo-orm-types.type";
|
|
2
|
+
import { MethodOptions } from "./methods-options.type";
|
|
3
|
+
/**
|
|
4
|
+
* Narrowed variant of {@link MethodOptions} exposing only `db` and `see`.
|
|
5
|
+
*
|
|
6
|
+
* Used by base methods that don't shape/return an `Entity` — count-like
|
|
7
|
+
* operations (`total`, `has`, `sum`, `average`, `min`, `max`) and
|
|
8
|
+
* batch-delete-like operations (`removeList`, `softRemoveList`,
|
|
9
|
+
* `restoreList`) — where `select`/`relations` (which only make sense when
|
|
10
|
+
* an `Entity` is being returned) don't apply.
|
|
11
|
+
*
|
|
12
|
+
* @publicApi
|
|
13
|
+
*/
|
|
14
|
+
export type RestrictMethodOptions<T, O extends VSRepoOrmTypes = VSRepoOrmTypes> = Pick<MethodOptions<T, O>, "db" | "see">;
|
|
@@ -14,4 +14,18 @@ export type VSRepoQueryOptions<T extends VSRepoOrmTypes = VSRepoOrmTypes> = {
|
|
|
14
14
|
* @default false
|
|
15
15
|
*/
|
|
16
16
|
modifying?: boolean;
|
|
17
|
+
/**
|
|
18
|
+
* When `true`, the array returned by the underlying query is collapsed
|
|
19
|
+
* into its first element (`null` if the array is empty) before
|
|
20
|
+
* being resolved to the caller. Has no effect when the query resolves
|
|
21
|
+
* to something other than an array (e.g. a `modifying` query's
|
|
22
|
+
* affected-row count).
|
|
23
|
+
*
|
|
24
|
+
* Useful for queries you already know return at most one row (e.g. a
|
|
25
|
+
* `SELECT ... LIMIT 1` or a lookup by a unique column), where declaring
|
|
26
|
+
* the return type as an array would be misleading.
|
|
27
|
+
*
|
|
28
|
+
* @default false
|
|
29
|
+
*/
|
|
30
|
+
singleResult?: boolean;
|
|
17
31
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vsrepo",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.2.0",
|
|
4
4
|
"description": "ORM-agnostic repository pattern library with full TypeScript support and automatic type inference.",
|
|
5
5
|
"homepage": "https://github.com/jaobrabo123/VSRepository#readme",
|
|
6
6
|
"repository": {
|