vsrepo 1.3.2 → 1.3.3

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.
@@ -63,9 +63,9 @@ class VSRepository {
63
63
  }
64
64
  return extended;
65
65
  }
66
- build(prisma, config) {
66
+ build(prisma, config, useInstance) {
67
67
  (0, prisma_client_validate_1.validatePrismaClient)(prisma, this);
68
- const buildInstance = Object.create(this);
68
+ const buildInstance = useInstance ?? Object.create(this);
69
69
  buildInstance.prisma = prisma;
70
70
  if (buildInstance.softRemovekName &&
71
71
  prisma[buildInstance.tableName]["fields"][buildInstance.softRemovekName]["typeName"] !==
@@ -93,19 +93,27 @@ class VSRepository {
93
93
  };
94
94
  if (!dinamicMethodInfo.ignoreWhere) {
95
95
  (0, pretty_wheres_resolve_1.resolvePrettyWheres)(dinamicMethodInfo, dinamicMethodWhereOps);
96
- if (showWorking) {
97
- const argsSimulation = [];
98
- for (let x = 0; x < dinamicMethodInfo.argsCount; x++) {
99
- argsSimulation[x] = "00";
100
- }
101
- (0, logger_util_1.logger)(`Where object resolved to ${methodToMap}:`, "build", buildInstance.tableName, (0, specific_where_resolve_1.resolveSpecificWhere)(argsSimulation, dinamicMethodWhereOps.prettyWheres));
102
- // logger(
103
- // `Where object resolved to ${methodToMap}:`,
104
- // "build",
105
- // buildInstance.tableName,
106
- // dinamicMethodWhereOps.prettyWheres,
107
- // );
108
- }
96
+ // if (showWorking) {
97
+ // const argsSimulation: any[] = [];
98
+ // for (let x = 0; x < dinamicMethodInfo.argsCount; x++) {
99
+ // argsSimulation[x] = "00";
100
+ // }
101
+ // logger(
102
+ // `Where object resolved to ${methodToMap}:`,
103
+ // "build",
104
+ // buildInstance.tableName,
105
+ // resolveSpecificWhere(
106
+ // argsSimulation,
107
+ // dinamicMethodWhereOps.prettyWheres,
108
+ // ),
109
+ // );
110
+ // // logger(
111
+ // // `Where object resolved to ${methodToMap}:`,
112
+ // // "build",
113
+ // // buildInstance.tableName,
114
+ // // dinamicMethodWhereOps.prettyWheres,
115
+ // // );
116
+ // }
109
117
  }
110
118
  let select = undefined;
111
119
  if (dinamicMethodInfo.existsMode) {
@@ -123,7 +131,7 @@ class VSRepository {
123
131
  defaultSelect: methods[originalKey]?.selectModel,
124
132
  ignoreRequiredWhere: dinamicMethodWhereOps.whereType === "overwrite",
125
133
  },
126
- withoutWhere: dinamicMethodInfo.ignoreWhere,
134
+ withoutWhere: dinamicMethodInfo.ignoreWhere && !dinamicMethodInfo.onlyBaseWheres,
127
135
  specificSelect: select,
128
136
  pushWhere: dinamicMethodWhereOps.pushWhere,
129
137
  withoutSelect: dinamicMethodInfo.ignoreSelect,
@@ -158,6 +166,11 @@ class VSRepository {
158
166
  const { prismaArgs } = (0, dbAndPrismaArgs_resolve_1.resolveDbAndPrismaArgs)(resolveDbAndPrismaArgsData);
159
167
  return prismaArgs;
160
168
  });
169
+ if (showWorking) {
170
+ const argsSimulation = new Array(dinamicMethodInfo.argsCount).fill("00");
171
+ const prismaArgs = buildInstance.vsrepocache.get(originalKey)(argsSimulation);
172
+ (0, logger_util_1.logger)(`PrismaArgs preview for ${methodToMap}:`, "build", buildInstance.tableName, prismaArgs);
173
+ }
161
174
  buildInstance[originalKey] = async (...args) => {
162
175
  let db = buildInstance.prisma;
163
176
  let methodOptions = undefined;
@@ -0,0 +1,4 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DINAMIC_METHODS_KEY = void 0;
4
+ exports.DINAMIC_METHODS_KEY = Symbol("dynamic-methods-key");
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DynamicMethod = DynamicMethod;
4
+ const dinamic_methods_key_constant_1 = require("../constants/dinamic-methods-key.constant");
5
+ const dynamic_method_metadata_entity_1 = require("../entities/dynamic-method-metadata.entity");
6
+ const dynamic_method_config_validate_1 = require("../validation/dynamic-method-config.validate");
7
+ function DynamicMethod(config) {
8
+ const validatedConfig = config ? (0, dynamic_method_config_validate_1.validateDynamicMethodConfig)(config) : undefined;
9
+ return (target, propertyKey) => {
10
+ const methods = Reflect.getMetadata(dinamic_methods_key_constant_1.DINAMIC_METHODS_KEY, target) ?? [];
11
+ methods.push(new dynamic_method_metadata_entity_1.DynamicMethodMetadata(propertyKey, validatedConfig?.map ?? true, validatedConfig?.proxyTo, validatedConfig?.whereType, validatedConfig?.pushWhere, validatedConfig?.fbMode, validatedConfig?.injectOrdenation, validatedConfig?.injectPagination));
12
+ Reflect.defineMetadata(dinamic_methods_key_constant_1.DINAMIC_METHODS_KEY, methods, target);
13
+ };
14
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DynamicMethodMetadata = void 0;
4
+ class DynamicMethodMetadata {
5
+ propertyKey;
6
+ map;
7
+ proxyTo;
8
+ whereType;
9
+ pushWhere;
10
+ fbMode;
11
+ injectOrdenation;
12
+ injectPagination;
13
+ constructor(propertyKey, map, proxyTo, whereType, pushWhere, fbMode, injectOrdenation, injectPagination) {
14
+ this.propertyKey = propertyKey;
15
+ this.map = map;
16
+ this.proxyTo = proxyTo;
17
+ this.whereType = whereType;
18
+ this.pushWhere = pushWhere;
19
+ this.fbMode = fbMode;
20
+ this.injectOrdenation = injectOrdenation;
21
+ this.injectPagination = injectPagination;
22
+ }
23
+ }
24
+ exports.DynamicMethodMetadata = DynamicMethodMetadata;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.VSRepoRuntimeError = exports.VSRepoExtendError = exports.VSRepoBuildError = exports.VSRepoConfigError = exports.VSRepoError = void 0;
3
+ exports.VSRepoRuntimeError = exports.VSRepoDecoratorError = exports.VSRepoExtendError = exports.VSRepoBuildError = exports.VSRepoConfigError = exports.VSRepoError = void 0;
4
4
  class VSRepoError extends Error {
5
5
  }
6
6
  exports.VSRepoError = VSRepoError;
@@ -16,6 +16,10 @@ class VSRepoExtendError extends VSRepoError {
16
16
  type = "VSREPO_EXTEND";
17
17
  }
18
18
  exports.VSRepoExtendError = VSRepoExtendError;
19
+ class VSRepoDecoratorError extends VSRepoError {
20
+ type = "VSREPO_DECORATOR";
21
+ }
22
+ exports.VSRepoDecoratorError = VSRepoDecoratorError;
19
23
  class VSRepoRuntimeError extends VSRepoError {
20
24
  type = "VSREPO_RUNTIME";
21
25
  code;
@@ -24,6 +24,9 @@ function resolveDbAndPrismaArgs(data) {
24
24
  else if (validatedOptions.includeModel) {
25
25
  prismaArgs.include = instance.includeModels?.[validatedOptions.includeModel];
26
26
  }
27
+ else if (validatedOptions.include) {
28
+ prismaArgs.include = validatedOptions.include;
29
+ }
27
30
  else {
28
31
  prismaArgs.select = (0, select_resolve_1.resolveSelect)(instance, validatedOptions.selectModel, baseConfig.defaultSelect);
29
32
  }
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.resolveDynamicMethodsMetadata = resolveDynamicMethodsMetadata;
4
+ const dinamic_methods_key_constant_1 = require("../constants/dinamic-methods-key.constant");
5
+ function resolveDynamicMethodsMetadata(prototype) {
6
+ const methods = Reflect.getMetadata(dinamic_methods_key_constant_1.DINAMIC_METHODS_KEY, prototype);
7
+ const methodsRecord = {};
8
+ if (!methods)
9
+ return methodsRecord;
10
+ for (const method of methods) {
11
+ const { propertyKey, ...rest } = method;
12
+ methodsRecord[propertyKey] = rest;
13
+ }
14
+ return methodsRecord;
15
+ }
@@ -4,7 +4,7 @@ exports.logger = logger;
4
4
  exports.performanceLoggerStart = performanceLoggerStart;
5
5
  exports.performanceLoggerEnd = performanceLoggerEnd;
6
6
  function logger(message, moment, tableName, obj) {
7
- console.log(`[VSRepository] (${tableName ?? "unknown"}: ${moment}) ${message}`);
7
+ console.log(`\n[VSRepository] (${tableName ?? "unknown"}: ${moment}) ${message}`);
8
8
  if (obj)
9
9
  console.log(JSON.stringify(obj, null, 2));
10
10
  }
@@ -3,8 +3,18 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.booleanSchema = exports.objectSchema = exports.stringSchema = void 0;
6
+ exports.methodSchema = exports.booleanSchema = exports.objectSchema = exports.stringSchema = void 0;
7
7
  const zod_1 = __importDefault(require("zod"));
8
8
  exports.stringSchema = zod_1.default.string().trim().nonempty();
9
9
  exports.objectSchema = zod_1.default.looseObject({});
10
10
  exports.booleanSchema = zod_1.default.boolean();
11
+ exports.methodSchema = zod_1.default.strictObject({
12
+ map: exports.booleanSchema,
13
+ selectModel: exports.stringSchema.or(zod_1.default.literal(false)).optional(),
14
+ whereType: zod_1.default.enum(["overwrite", "extending"]).optional(),
15
+ proxyTo: exports.stringSchema.optional(),
16
+ pushWhere: exports.objectSchema.optional(),
17
+ fbMode: zod_1.default.enum(["one", "list"]).optional(),
18
+ injectOrdenation: exports.objectSchema.or(zod_1.default.array(exports.objectSchema)).optional(),
19
+ injectPagination: exports.objectSchema.optional(),
20
+ });
@@ -27,16 +27,7 @@ function validateConstructorConfig(config) {
27
27
  }))
28
28
  .optional(),
29
29
  methods: zod_1.default
30
- .record(schemas_util_1.stringSchema, zod_1.default.strictObject({
31
- map: schemas_util_1.booleanSchema,
32
- selectModel: schemas_util_1.stringSchema.or(zod_1.default.literal(false)).optional(),
33
- whereType: zod_1.default.enum(["overwrite", "extending"]).optional(),
34
- proxyTo: schemas_util_1.stringSchema.optional(),
35
- pushWhere: schemas_util_1.objectSchema.optional(),
36
- fbMode: zod_1.default.enum(["one", "list"]).optional(),
37
- injectOrdenation: schemas_util_1.objectSchema.or(zod_1.default.array(schemas_util_1.objectSchema)).optional(),
38
- injectPagination: schemas_util_1.objectSchema.optional(),
39
- }))
30
+ .record(schemas_util_1.stringSchema, schemas_util_1.methodSchema)
40
31
  .optional(),
41
32
  defaultOrdenation: schemas_util_1.objectSchema.or(zod_1.default.array(schemas_util_1.objectSchema)).optional(),
42
33
  })
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.validateDynamicMethodConfig = validateDynamicMethodConfig;
4
+ const vs_repo_error_1 = require("../errors/vs-repo.error");
5
+ const schemas_util_1 = require("../utils/schemas.util");
6
+ const is_object_validate_1 = require("./is-object.validate");
7
+ function validateDynamicMethodConfig(config) {
8
+ if (!(0, is_object_validate_1.isObject)(config)) {
9
+ throw new vs_repo_error_1.VSRepoDecoratorError(`[VSRepository] (unknown: decorator) 'config' must be a valid object`);
10
+ }
11
+ config["map"] = true;
12
+ const configParsed = schemas_util_1.methodSchema.safeParse(config);
13
+ if (!configParsed.success) {
14
+ const firstIssue = configParsed.error.issues[0];
15
+ const path = firstIssue?.path.length ? firstIssue.path.join(".") : "config";
16
+ throw new vs_repo_error_1.VSRepoDecoratorError(`[VSRepository] (unknown: decorator) ${path}: ${firstIssue?.message}`);
17
+ }
18
+ return configParsed.data;
19
+ }
@@ -17,6 +17,7 @@ function validateMethodOptions(options, instance) {
17
17
  .optional(),
18
18
  includeModel: zod_1.default.enum(Object.keys(instance.includeModels ?? {})).optional(),
19
19
  see: zod_1.default.enum(["active", "removed", "all"]).default("active"),
20
+ include: schemas_util_1.objectSchema.optional(),
20
21
  })
21
22
  .refine(data => {
22
23
  return !(data.selectModel !== undefined && data.includeModel !== undefined);
package/package.json CHANGED
@@ -1,72 +1,78 @@
1
- {
2
- "name": "vsrepo",
3
- "version": "1.3.2",
4
- "description": "Uma biblioteca de repository pattern para Prisma",
5
- "homepage": "https://github.com/jaobrabo123/VSRepository#readme",
6
- "repository": {
7
- "type": "git",
8
- "url": "git+https://github.com/jaobrabo123/VSRepository.git"
9
- },
10
- "main": "./dist/index.js",
11
- "types": "./dist/index.d.ts",
12
- "exports": {
13
- ".": {
14
- "types": "./dist/index.d.ts",
15
- "import": "./dist/index.js",
16
- "require": "./dist/index.js"
17
- },
18
- "./VSRepository": {
19
- "types": "./dist/VSRepository.d.ts",
20
- "import": "./dist/VSRepository.js",
21
- "require": "./dist/VSRepository.js"
22
- },
23
- "./VSRepoError": {
24
- "types": "./dist/VSRepoError.d.ts",
25
- "import": "./dist/VSRepoError.js",
26
- "require": "./dist/VSRepoError.js"
27
- }
28
- },
29
- "type": "commonjs",
30
- "files": [
31
- "dist",
32
- "scripts"
33
- ],
34
- "bin": {
35
- "vsrepo": "scripts/configure-prisma-import.mjs"
36
- },
37
- "engines": {
38
- "node": ">=18"
39
- },
40
- "scripts": {
41
- "vsrepo": "node ./scripts/configure-prisma-import.mjs",
42
- "build": "tsc -p tsconfig.build.json && node ./scripts/copy-types.mjs",
43
- "prepack": "pnpm build"
44
- },
45
- "keywords": [
46
- "prisma",
47
- "repository",
48
- "orm",
49
- "typescript",
50
- "vsrepo",
51
- "vsrepository",
52
- "jparepository"
53
- ],
54
- "author": "João Pedro Azevedo Freire Mecenas",
55
- "license": "MIT",
56
- "peerDependencies": {
57
- "@prisma/client": "^7.8.0"
58
- },
59
- "devDependencies": {
60
- "@prisma/adapter-pg": "^7.8.0",
61
- "@prisma/client": "^7.8.0",
62
- "@types/node": "^25.6.0",
63
- "dotenv": "^17.4.2",
64
- "prisma": "^7.8.0",
65
- "tsx": "^4.22.3",
66
- "typescript": "^6.0.3"
67
- },
68
- "dependencies": {
69
- "deepmerge": "^4.3.1",
70
- "zod": "^4.4.3"
71
- }
72
- }
1
+ {
2
+ "name": "vsrepo",
3
+ "version": "1.3.3",
4
+ "description": "A repository pattern library for Prisma",
5
+ "homepage": "https://github.com/jaobrabo123/VSRepository#readme",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/jaobrabo123/VSRepository.git"
9
+ },
10
+ "main": "./dist/index.js",
11
+ "types": "./dist/index.d.ts",
12
+ "exports": {
13
+ ".": {
14
+ "types": "./dist/index.d.ts",
15
+ "import": "./dist/index.js",
16
+ "require": "./dist/index.js"
17
+ },
18
+ "./VSRepository": {
19
+ "types": "./dist/VSRepository.d.ts",
20
+ "import": "./dist/VSRepository.js",
21
+ "require": "./dist/VSRepository.js"
22
+ },
23
+ "./DynamicRepository": {
24
+ "types": "./dist/DynamicRepository.d.ts",
25
+ "import": "./dist/DynamicRepository.js",
26
+ "require": "./dist/DynamicRepository.js"
27
+ },
28
+ "./VSRepoError": {
29
+ "types": "./dist/VSRepoError.d.ts",
30
+ "import": "./dist/VSRepoError.js",
31
+ "require": "./dist/VSRepoError.js"
32
+ }
33
+ },
34
+ "type": "commonjs",
35
+ "files": [
36
+ "dist",
37
+ "scripts"
38
+ ],
39
+ "bin": {
40
+ "vsrepo": "scripts/configure-prisma-import.mjs"
41
+ },
42
+ "engines": {
43
+ "node": ">=18"
44
+ },
45
+ "scripts": {
46
+ "vsrepo": "node ./scripts/configure-prisma-import.mjs",
47
+ "build": "tsc -p tsconfig.build.json && node ./scripts/copy-types.mjs",
48
+ "prepack": "pnpm build"
49
+ },
50
+ "keywords": [
51
+ "prisma",
52
+ "repository",
53
+ "orm",
54
+ "typescript",
55
+ "vsrepo",
56
+ "vsrepository",
57
+ "jparepository"
58
+ ],
59
+ "author": "João Pedro Azevedo Freire Mecenas",
60
+ "license": "MIT",
61
+ "peerDependencies": {
62
+ "@prisma/client": "^7.8.0"
63
+ },
64
+ "devDependencies": {
65
+ "@prisma/adapter-pg": "^7.8.0",
66
+ "@prisma/client": "^7.8.0",
67
+ "@types/node": "^25.6.0",
68
+ "dotenv": "^17.4.2",
69
+ "prisma": "^7.8.0",
70
+ "tsx": "^4.22.3",
71
+ "typescript": "^6.0.3"
72
+ },
73
+ "dependencies": {
74
+ "deepmerge": "^4.3.1",
75
+ "reflect-metadata": "^0.2.2",
76
+ "zod": "^4.4.3"
77
+ }
78
+ }
@@ -81,8 +81,25 @@ fs.mkdirSync(outputDir, { recursive: true });
81
81
  const typeFiles = [
82
82
  'VSRepoError.d.ts',
83
83
  'VSRepository.d.ts',
84
+ 'DynamicRepository.d.ts'
84
85
  ];
85
86
 
87
+ // Mapa de basename original (com e sem extensao) -> basename final `.types`,
88
+ // usado para corrigir os imports relativos entre os proprios arquivos gerados
89
+ // (ex: DynamicRepository.d.ts importando de "./VSRepository.d" ou
90
+ // VSRepository.d.ts importando de "./VSRepoError").
91
+ const crossFileReplacements = typeFiles.flatMap((fileName) => {
92
+ const originalBase = fileName.replace(/\.d\.ts$/, '');
93
+ const finalBase = `${originalBase}.types`;
94
+
95
+ return [
96
+ // from "./VSRepository.d" -> from "./VSRepository.types"
97
+ { pattern: new RegExp(`from(\\s+)(['"])\\./${originalBase}\\.d\\2`, 'g'), replacement: `from$1$2./${finalBase}$2` },
98
+ // from "./VSRepoError" -> from "./VSRepoError.types"
99
+ { pattern: new RegExp(`from(\\s+)(['"])\\./${originalBase}\\2`, 'g'), replacement: `from$1$2./${finalBase}$2` },
100
+ ];
101
+ });
102
+
86
103
  for (const fileName of typeFiles) {
87
104
  const sourceFile = path.join(sourceDir, fileName);
88
105
 
@@ -103,6 +120,10 @@ for (const fileName of typeFiles) {
103
120
  `from '${prismaTargetPath === CLASSIC_PRISMA_GENERATED_PATH ? prismaTargetPath : relativeImportPath(path.dirname(targetFile), prismaTargetPath)}'`
104
121
  );
105
122
 
123
+ for (const { pattern, replacement } of crossFileReplacements) {
124
+ contents = contents.replace(pattern, replacement);
125
+ }
126
+
106
127
  fs.writeFileSync(targetFile, GENERATED_HEADER + contents, 'utf8');
107
128
 
108
129
  console.log(`Gerado: ${path.relative(workspaceRoot, targetFile)}`);
@@ -152,6 +173,24 @@ export const VSRepository = VSRepositoryRuntime as typeof VSRepositoryType;
152
173
  export const setupVSRepo = setupVSRepoRuntime as typeof setupVSRepoType;
153
174
 
154
175
  export type * from './VSRepository.types';
176
+ `,
177
+ },
178
+ {
179
+ fileName: 'DynamicRepository.ts',
180
+ content: `import {
181
+ DynamicRepository as DynamicRepositoryRuntime,
182
+ DynamicMethod as DynamicMethodRuntime
183
+ } from 'vsrepo/DynamicRepository';
184
+
185
+ import type {
186
+ DynamicRepository as DynamicRepositoryType,
187
+ DynamicMethod as DynamicMethodType
188
+ } from './DynamicRepository.types';
189
+
190
+ export const DynamicRepository = DynamicRepositoryRuntime as typeof DynamicRepositoryType;
191
+ export const DynamicMethod = DynamicMethodRuntime as typeof DynamicMethodType;
192
+
193
+ export type * from './DynamicRepository.types';
155
194
  `,
156
195
  },
157
196
  {
@@ -169,8 +208,14 @@ export {
169
208
  setupVSRepo
170
209
  } from './VSRepository.js';
171
210
 
211
+ export {
212
+ DynamicRepository,
213
+ DynamicMethod
214
+ } from './DynamicRepository.js';
215
+
172
216
  export type * from './VSRepoError.types';
173
217
  export type * from './VSRepository.types';
218
+ export type * from './DynamicRepository.types';
174
219
  `,
175
220
  },
176
221
  ];