vsrepo 1.3.3 → 1.3.5
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 +142 -3
- package/README.pt-BR.md +1590 -0
- package/dist/DynamicRepository.d.ts +96 -22
- package/dist/DynamicRepository.js +3 -31
- package/dist/VSRepository.d.ts +182 -84
- package/dist/VSRepository.js +22 -0
- package/dist/internal/decorators/dynamic-method.decorator.js +1 -1
- package/dist/internal/decorators/query-method.decorator.js +20 -0
- package/dist/internal/entities/dynamic-method-metadata.entity.js +3 -1
- package/dist/internal/resolvers/dbAndPrismaArgs.resolve.js +3 -0
- package/dist/internal/utils/schemas.util.js +6 -1
- package/dist/internal/validation/method-options.validate.js +10 -3
- package/dist/internal/validation/query-method-arg.validate.js +22 -0
- package/dist/internal/validation/query-method-options.validate.js +24 -0
- package/dist/internal/validation/types/query-method-arg.type.js +2 -0
- package/dist/internal/validation/types/query-method-options.type.js +2 -0
- package/package.json +11 -2
- package/scripts/configure-prisma-import.mjs +26 -19
|
@@ -15,6 +15,7 @@ import {
|
|
|
15
15
|
PrismaModelName,
|
|
16
16
|
RepositoryRelations,
|
|
17
17
|
SeeMode,
|
|
18
|
+
SelectModel,
|
|
18
19
|
WhereModel,
|
|
19
20
|
} from "./VSRepository.d";
|
|
20
21
|
|
|
@@ -91,7 +92,9 @@ type DynamicTransformCreatePayload<U, T, M extends PrismaModelName, TRelations>
|
|
|
91
92
|
export type DynamicSaveInput<TEntity, UName extends PrismaModelName, WRelations = undefined> =
|
|
92
93
|
ModelUpsertInput<UName> extends infer U
|
|
93
94
|
? U extends any
|
|
94
|
-
? Simplify<
|
|
95
|
+
? Simplify<
|
|
96
|
+
DynamicTransformCreatePayload<U, TEntity, UName, ResolveRelations<WRelations>>
|
|
97
|
+
>
|
|
95
98
|
: never
|
|
96
99
|
: never;
|
|
97
100
|
|
|
@@ -108,12 +111,14 @@ export type DynamicPatchInput<
|
|
|
108
111
|
UName extends PrismaModelName,
|
|
109
112
|
WRelations = undefined,
|
|
110
113
|
> = Simplify<
|
|
111
|
-
DistributiveOmit<
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
114
|
+
DistributiveOmit<
|
|
115
|
+
PrismaModelInputs<UName>["updateInput"],
|
|
116
|
+
keyof ResolveRelations<WRelations>
|
|
117
|
+
> & {
|
|
118
|
+
[K in Extract<
|
|
119
|
+
keyof ResolveRelations<WRelations>,
|
|
120
|
+
keyof TEntity
|
|
121
|
+
>]?: DynamicRelationCreatePayload<TEntity[K], UName, K>;
|
|
117
122
|
}
|
|
118
123
|
>;
|
|
119
124
|
|
|
@@ -129,12 +134,14 @@ export type DynamicMergeInput<
|
|
|
129
134
|
UName extends PrismaModelName,
|
|
130
135
|
WRelations = undefined,
|
|
131
136
|
> = Simplify<
|
|
132
|
-
DistributiveOmit<
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
137
|
+
DistributiveOmit<
|
|
138
|
+
PrismaModelInputs<UName>["updateInput"],
|
|
139
|
+
keyof ResolveRelations<WRelations>
|
|
140
|
+
> & {
|
|
141
|
+
[K in Extract<
|
|
142
|
+
keyof ResolveRelations<WRelations>,
|
|
143
|
+
keyof TEntity
|
|
144
|
+
>]?: DynamicRelationUpdatePayload<TEntity[K], UName, K>;
|
|
138
145
|
}
|
|
139
146
|
>;
|
|
140
147
|
|
|
@@ -151,8 +158,11 @@ export type DynamicMethodOptions<TName extends PrismaModelName = PrismaModelName
|
|
|
151
158
|
*/
|
|
152
159
|
see?: SeeMode;
|
|
153
160
|
|
|
154
|
-
/** Raw Prisma `include`. */
|
|
161
|
+
/** Raw Prisma `include`. Cannot be used together with `select`. */
|
|
155
162
|
include?: IncludeModel<TName>;
|
|
163
|
+
|
|
164
|
+
/** Raw Prisma `select`. Cannot be used together with `include`. */
|
|
165
|
+
select?: SelectModel<TName>;
|
|
156
166
|
};
|
|
157
167
|
|
|
158
168
|
/**
|
|
@@ -284,7 +294,7 @@ export declare abstract class DynamicRepository<
|
|
|
284
294
|
/** Saves an array of objects in a single automatic transaction. */
|
|
285
295
|
saveList(
|
|
286
296
|
objs: DynamicSaveInput<TEntity, UName, WRelations>[],
|
|
287
|
-
options?: Omit<DynamicMethodOptions<UName>, "include"> & {
|
|
297
|
+
options?: Omit<DynamicMethodOptions<UName>, "include" | "select"> & {
|
|
288
298
|
/** Transaction client to use for this operation. */
|
|
289
299
|
db?: DbTransaction;
|
|
290
300
|
},
|
|
@@ -300,7 +310,7 @@ export declare abstract class DynamicRepository<
|
|
|
300
310
|
/** Partially updates multiple records via `[pk, obj]` tuples in an automatic transaction. */
|
|
301
311
|
patchList(
|
|
302
312
|
tuples: [pk: VPKType, obj: DynamicPatchInput<TEntity, UName, WRelations>][],
|
|
303
|
-
options?: Omit<DynamicMethodOptions<UName>, "include"> & {
|
|
313
|
+
options?: Omit<DynamicMethodOptions<UName>, "include" | "select"> & {
|
|
304
314
|
/** Transaction client to use for this operation. */
|
|
305
315
|
db?: DbTransaction;
|
|
306
316
|
},
|
|
@@ -316,7 +326,7 @@ export declare abstract class DynamicRepository<
|
|
|
316
326
|
/** Deletes multiple records by their primary keys. */
|
|
317
327
|
removeList(
|
|
318
328
|
pks: VPKType[],
|
|
319
|
-
options?: Omit<DynamicMethodOptions<UName>, "include">,
|
|
329
|
+
options?: Omit<DynamicMethodOptions<UName>, "include" | "select">,
|
|
320
330
|
): Promise<{ count: number }>;
|
|
321
331
|
|
|
322
332
|
/** Fetches all records (respects `requiredWhere` when set). */
|
|
@@ -334,10 +344,10 @@ export declare abstract class DynamicRepository<
|
|
|
334
344
|
): Promise<TEntity[]>;
|
|
335
345
|
|
|
336
346
|
/** Returns the total number of records. */
|
|
337
|
-
total(options?: Omit<DynamicMethodOptions<UName>, "include">): Promise<number>;
|
|
347
|
+
total(options?: Omit<DynamicMethodOptions<UName>, "include" | "select">): Promise<number>;
|
|
338
348
|
|
|
339
349
|
/** Checks whether a record exists by its primary key (PK). */
|
|
340
|
-
has(pk: VPKType, options?: Omit<DynamicMethodOptions<UName>, "include">): Promise<boolean>;
|
|
350
|
+
has(pk: VPKType, options?: Omit<DynamicMethodOptions<UName>, "include" | "select">): Promise<boolean>;
|
|
341
351
|
|
|
342
352
|
/** Marks a record as deleted (soft-delete). */
|
|
343
353
|
softRemove(pk: VPKType, options?: Omit<DynamicMethodOptions<UName>, "see">): Promise<TEntity>;
|
|
@@ -345,7 +355,7 @@ export declare abstract class DynamicRepository<
|
|
|
345
355
|
/** Marks multiple records as deleted (soft-delete) in batch. */
|
|
346
356
|
softRemoveList(
|
|
347
357
|
pks: VPKType[],
|
|
348
|
-
options?: Omit<DynamicMethodOptions<UName>, "see" | "include">,
|
|
358
|
+
options?: Omit<DynamicMethodOptions<UName>, "see" | "include" | "select">,
|
|
349
359
|
): Promise<{ count: number }>;
|
|
350
360
|
|
|
351
361
|
/** Restores a record previously marked as deleted (soft-delete). */
|
|
@@ -354,7 +364,7 @@ export declare abstract class DynamicRepository<
|
|
|
354
364
|
/** Restores multiple records previously marked as deleted (soft-delete) in batch. */
|
|
355
365
|
restoreList(
|
|
356
366
|
pks: VPKType[],
|
|
357
|
-
options?: Omit<DynamicMethodOptions<UName>, "see" | "include">,
|
|
367
|
+
options?: Omit<DynamicMethodOptions<UName>, "see" | "include" | "select">,
|
|
358
368
|
): Promise<{ count: number }>;
|
|
359
369
|
}
|
|
360
370
|
|
|
@@ -395,7 +405,7 @@ export type DynamicMethodConfig<M extends PrismaModelName = PrismaModelName> = {
|
|
|
395
405
|
* class UserRepository extends DynamicRepository<User, "User", string> {
|
|
396
406
|
* `@DynamicMethod()`
|
|
397
407
|
* declare findOneByEmail: (email: string) => Promise<User | null>;
|
|
398
|
-
*
|
|
408
|
+
*
|
|
399
409
|
* @DynamicMethod<"User">({ injectOrdenation: { createdAt: "desc" } })
|
|
400
410
|
* declare findByAge: (email: number) => Promise<User[]>;
|
|
401
411
|
* }
|
|
@@ -404,3 +414,67 @@ export type DynamicMethodConfig<M extends PrismaModelName = PrismaModelName> = {
|
|
|
404
414
|
export declare function DynamicMethod<M extends PrismaModelName = PrismaModelName>(
|
|
405
415
|
config?: DynamicMethodConfig<M>,
|
|
406
416
|
): PropertyDecorator;
|
|
417
|
+
|
|
418
|
+
/**
|
|
419
|
+
* Single argument accepted by a method declared with `@QueryMethod`.
|
|
420
|
+
*
|
|
421
|
+
* `args` are injected positionally into the raw SQL statement (`$1`, `$2`, ...),
|
|
422
|
+
* exactly like Prisma's `$queryRawUnsafe`/`$executeRawUnsafe` parameters — this is
|
|
423
|
+
* what allows safe parameter injection instead of string-concatenating values into
|
|
424
|
+
* the SQL text.
|
|
425
|
+
*
|
|
426
|
+
* @template T Tuple type of the positional SQL parameters, e.g. `[email: string]`.
|
|
427
|
+
*
|
|
428
|
+
* @example
|
|
429
|
+
* ```typescript
|
|
430
|
+
* declare findByEmail: (arg: QueryMethodArg<[email: string]>) => Promise<User[]>;
|
|
431
|
+
* ```
|
|
432
|
+
*/
|
|
433
|
+
export type QueryMethodArg<T extends Array<any>> = {
|
|
434
|
+
/** Positional parameters injected into the SQL placeholders (`$1`, `$2`, ...). */
|
|
435
|
+
args: T;
|
|
436
|
+
/** Transaction client to run this query in, instead of the repository's default client. */
|
|
437
|
+
db?: ClientOrTransaction;
|
|
438
|
+
};
|
|
439
|
+
|
|
440
|
+
/**
|
|
441
|
+
* Options accepted by the `@QueryMethod` decorator.
|
|
442
|
+
*/
|
|
443
|
+
export type QueryMethodOptions = {
|
|
444
|
+
/**
|
|
445
|
+
* When `true`, the SQL is executed with `$executeRawUnsafe` (INSERT/UPDATE/DELETE)
|
|
446
|
+
* and the decorated method must resolve to `number` (the count of affected rows).
|
|
447
|
+
* When `false`/omitted, the SQL is executed with `$queryRawUnsafe` and the method
|
|
448
|
+
* resolves to whatever return type you declare on the field.
|
|
449
|
+
*
|
|
450
|
+
* @default false
|
|
451
|
+
*/
|
|
452
|
+
modifying?: boolean;
|
|
453
|
+
};
|
|
454
|
+
|
|
455
|
+
/**
|
|
456
|
+
* Property decorator used to declare a **raw SQL query method** on a `DynamicRepository`
|
|
457
|
+
* subclass, bypassing name-based method parsing entirely.
|
|
458
|
+
*
|
|
459
|
+
* Applied to a `declare` class field, it executes `value` directly through Prisma
|
|
460
|
+
* (`$queryRawUnsafe` or `$executeRawUnsafe`, depending on `options.modifying`), with
|
|
461
|
+
* parameters injected positionally via `QueryMethodArg["args"]`. Since the SQL result
|
|
462
|
+
* isn't statically known, the field's own declared function type is what determines
|
|
463
|
+
* the method's return type — for `modifying: true` it must always resolve to `number`.
|
|
464
|
+
*
|
|
465
|
+
* @param value Raw SQL statement to execute. Use `$1`, `$2`, ... placeholders for the
|
|
466
|
+
* values that will be passed via `args` — never interpolate values directly into `value`.
|
|
467
|
+
* @param options Optional configuration; set `modifying: true` for `INSERT`/`UPDATE`/`DELETE` statements.
|
|
468
|
+
*
|
|
469
|
+
* @example
|
|
470
|
+
* ```typescript
|
|
471
|
+
* class UserRepository extends DynamicRepository<User, "User", string> {
|
|
472
|
+
* @QueryMethod('SELECT * FROM "user" WHERE email = $1')
|
|
473
|
+
* declare findByEmailRaw: (arg: QueryMethodArg<[email: string]>) => Promise<User[]>;
|
|
474
|
+
*
|
|
475
|
+
* @QueryMethod('UPDATE "user" SET active = true WHERE id = $1', { modifying: true })
|
|
476
|
+
* declare activateUser: (arg: QueryMethodArg<[id: string]>) => Promise<number>;
|
|
477
|
+
* }
|
|
478
|
+
* ```
|
|
479
|
+
*/
|
|
480
|
+
export declare function QueryMethod(value: string, options?: QueryMethodOptions): PropertyDecorator;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.DynamicRepository = exports.DynamicMethod = void 0;
|
|
3
|
+
exports.DynamicRepository = exports.QueryMethod = exports.DynamicMethod = void 0;
|
|
4
4
|
const dynamic_methods_metadata_resolve_1 = require("./internal/resolvers/dynamic-methods-metadata.resolve");
|
|
5
5
|
const build_config_validate_1 = require("./internal/validation/build-config.validate");
|
|
6
6
|
const is_object_validate_1 = require("./internal/validation/is-object.validate");
|
|
@@ -8,6 +8,8 @@ const VSRepository_1 = require("./VSRepository");
|
|
|
8
8
|
require("reflect-metadata");
|
|
9
9
|
var dynamic_method_decorator_1 = require("./internal/decorators/dynamic-method.decorator");
|
|
10
10
|
Object.defineProperty(exports, "DynamicMethod", { enumerable: true, get: function () { return dynamic_method_decorator_1.DynamicMethod; } });
|
|
11
|
+
var query_method_decorator_1 = require("./internal/decorators/query-method.decorator");
|
|
12
|
+
Object.defineProperty(exports, "QueryMethod", { enumerable: true, get: function () { return query_method_decorator_1.QueryMethod; } });
|
|
11
13
|
class DynamicRepository extends VSRepository_1.VSRepository {
|
|
12
14
|
constructor(prisma, config) {
|
|
13
15
|
if (!(0, is_object_validate_1.isObject)(config)) {
|
|
@@ -22,33 +24,3 @@ class DynamicRepository extends VSRepository_1.VSRepository {
|
|
|
22
24
|
}
|
|
23
25
|
}
|
|
24
26
|
exports.DynamicRepository = DynamicRepository;
|
|
25
|
-
// export class UserRepository extends DynamicRepository {
|
|
26
|
-
// constructor() {
|
|
27
|
-
// super(
|
|
28
|
-
// { $transaction: {} },
|
|
29
|
-
// {
|
|
30
|
-
// tableName: "user",
|
|
31
|
-
// pkName: "id",
|
|
32
|
-
// requiredWhere: { active: true },
|
|
33
|
-
// softRemovekName: "createdAt",
|
|
34
|
-
// build: { showWorking: true },
|
|
35
|
-
// },
|
|
36
|
-
// );
|
|
37
|
-
// }
|
|
38
|
-
// @DynamicMethod()
|
|
39
|
-
// declare findByAge: (age: number) => Promise<object[]>;
|
|
40
|
-
// @DynamicMethod()
|
|
41
|
-
// declare findOneByEmail: (email: number) => Promise<object[]>;
|
|
42
|
-
// @DynamicMethod()
|
|
43
|
-
// declare findByNameStartsWithInsensitive: (email: number) => Promise<object[]>;
|
|
44
|
-
// @DynamicMethod()
|
|
45
|
-
// declare findByDescriptionIsNull: (email: number) => Promise<object[]>;
|
|
46
|
-
// @DynamicMethod({ whereType: "overwrite" })
|
|
47
|
-
// declare findByNameContainsInsensitiveOrderedAndPaginated: (email: number) => Promise<object[]>;
|
|
48
|
-
// @DynamicMethod({
|
|
49
|
-
// injectOrdenation: [{ address: { state: "asc" } }, { address: { city: "asc" } }],
|
|
50
|
-
// })
|
|
51
|
-
// declare findByAddressWithCountry: (email: number) => Promise<object[]>;
|
|
52
|
-
// }
|
|
53
|
-
// const userRepository = new UserRepository();
|
|
54
|
-
// console.log(userRepository);
|