vsrepo 1.2.5 → 1.2.7

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.
Files changed (55) hide show
  1. package/README.md +310 -441
  2. package/{VSRepository → dist}/VSRepoError.d.ts +1 -1
  3. package/dist/VSRepoError.js +17 -0
  4. package/{VSRepository → dist}/VSRepository.d.ts +937 -887
  5. package/dist/VSRepository.js +204 -0
  6. package/dist/index.d.ts +1 -0
  7. package/dist/index.js +17 -0
  8. package/dist/internal/errors/types/vs-repo-error-type.type.js +2 -0
  9. package/dist/internal/errors/vs-repo.error.js +27 -0
  10. package/dist/internal/resolvers/base-methods.resolve.js +535 -0
  11. package/dist/internal/resolvers/create-update-payloads-with-relations.resolve.js +143 -0
  12. package/dist/internal/resolvers/data-payload-with-relations.resolve.js +60 -0
  13. package/dist/internal/resolvers/dbAndPrismaArgs.resolve.js +46 -0
  14. package/dist/internal/resolvers/dinamic-method-customization.resolve.js +45 -0
  15. package/dist/internal/resolvers/dinamic-method-info.resolve.js +265 -0
  16. package/dist/internal/resolvers/merge-wheres.resolve.js +22 -0
  17. package/dist/internal/resolvers/pretty-wheres.resolve.js +87 -0
  18. package/dist/internal/resolvers/select.resolve.js +7 -0
  19. package/dist/internal/resolvers/specific-where.resolve.js +77 -0
  20. package/dist/internal/resolvers/types/base-method-function.type.js +2 -0
  21. package/dist/internal/resolvers/types/dinamic-method-customization.type.js +2 -0
  22. package/dist/internal/resolvers/types/dinamic-method-info.type.js +2 -0
  23. package/dist/internal/resolvers/types/dinamic-method-where-ops.type.js +2 -0
  24. package/dist/internal/resolvers/types/pretty-where.type.js +2 -0
  25. package/dist/internal/resolvers/types/prisma-args.type.js +2 -0
  26. package/dist/internal/resolvers/types/repository-build-instance.type.js +2 -0
  27. package/dist/internal/resolvers/types/resolve-db-and-prisma-args-data.type.js +2 -0
  28. package/dist/internal/resolvers/types/ugly-where.type.js +2 -0
  29. package/dist/internal/resolvers/ugly-where.resolve.js +178 -0
  30. package/dist/internal/utils/logger.util.js +21 -0
  31. package/dist/internal/utils/schemas.util.js +10 -0
  32. package/dist/internal/utils/uncapitalize.util.js +6 -0
  33. package/dist/internal/validation/build-config.validate.js +84 -0
  34. package/dist/internal/validation/constructor-config.validate.js +68 -0
  35. package/dist/internal/validation/extension.validate.js +15 -0
  36. package/dist/internal/validation/is-object.validate.js +6 -0
  37. package/dist/internal/validation/method-options.validate.js +26 -0
  38. package/dist/internal/validation/obj-with-relations.validate.js +37 -0
  39. package/dist/internal/validation/prisma-client.validate.js +10 -0
  40. package/dist/internal/validation/types/base-methods.type.js +2 -0
  41. package/dist/internal/validation/types/build-config.type.js +2 -0
  42. package/dist/internal/validation/types/constructor-config.type.js +2 -0
  43. package/dist/internal/validation/types/method-options.type.js +2 -0
  44. package/dist/internal/validation/types/method.type.js +2 -0
  45. package/dist/internal/validation/types/pagination.type.js +2 -0
  46. package/dist/internal/validation/types/relation.type.js +2 -0
  47. package/dist/internal/validation/types/see-mode.type.js +2 -0
  48. package/package.json +72 -70
  49. package/scripts/configure-prisma-import.mjs +9 -8
  50. package/scripts/copy-types.mjs +24 -0
  51. package/VSRepository/VSRepoError.js +0 -33
  52. package/VSRepository/VSRepoUtils.js +0 -150
  53. package/VSRepository/VSRepository.js +0 -1394
  54. package/VSRepository/index.d.ts +0 -2
  55. package/VSRepository/index.js +0 -2
@@ -20,15 +20,16 @@ const GENERATED_HEADER = `/**
20
20
 
21
21
  `;
22
22
 
23
- const command = args[0]?.startsWith('-') ? 'generate' : args[0] ?? 'generate';
24
- const commandArgs = command === 'generate' && args[0] !== command ? args : args.slice(1);
23
+ const command = args[0] || 'generate';
25
24
 
26
25
  if (command !== 'generate') {
27
26
  console.error(`Comando desconhecido: ${command}`);
28
- console.error('Uso: vsrepo generate --output <dir-output> --prisma <path-prisma>');
27
+ console.error('Use: vsrepo generate --output <dir-output> --prisma <path-prisma>');
29
28
  process.exit(1);
30
29
  }
31
30
 
31
+ const commandArgs = command === args[0] ? args.slice(1) : args;
32
+
32
33
  const outputFlagIndex = commandArgs.findIndex((arg) => arg === '--output' || arg === '-o');
33
34
  const prismaFlagIndex = commandArgs.findIndex((arg) => arg === '--prisma' || arg === '-p');
34
35
 
@@ -68,10 +69,10 @@ const packageRoot = installedPackageExists
68
69
  ? installedNodeModulesPath
69
70
  : path.resolve(scriptDirectory, '..');
70
71
 
71
- const sourceDir = path.join(packageRoot, 'VSRepository');
72
+ const sourceDir = path.join(packageRoot, 'dist');
72
73
 
73
74
  if (!fs.existsSync(sourceDir)) {
74
- console.error(`Nao foi possivel localizar a pasta VSRepository em: ${sourceDir}`);
75
+ console.error(`Nao foi possivel localizar a pasta dist em: ${sourceDir}`);
75
76
  process.exit(1);
76
77
  }
77
78
 
@@ -98,7 +99,7 @@ for (const fileName of typeFiles) {
98
99
  let contents = fs.readFileSync(sourceFile, 'utf8');
99
100
 
100
101
  contents = contents.replace(
101
- /from\s+(['"])@vsrepo\/prisma\/types\1/g,
102
+ /from\s+(['"])\.\.\/generated\/prisma\/client\1/g,
102
103
  `from '${prismaTargetPath === CLASSIC_PRISMA_GENERATED_PATH ? prismaTargetPath : relativeImportPath(path.dirname(targetFile), prismaTargetPath)}'`
103
104
  );
104
105
 
@@ -194,8 +195,8 @@ if (fs.existsSync(readmeSource)) {
194
195
  }
195
196
 
196
197
  console.log('\nVSRepository gerado com tipagem do Prisma.');
197
- console.log(`Output: ${outputDir}`);
198
- console.log(`Prisma usado em: ${prismaTargetPath}`);
198
+ console.log(`Output: ${path.relative(workspaceRoot, outputDir)}`);
199
+ console.log(`Prisma usado em: ${path.relative(workspaceRoot, prismaTargetPath)}`);
199
200
 
200
201
  function stripKnownExtension(inputPath) {
201
202
  return inputPath.replace(/\.(?:d\.ts|ts|js|mjs|cjs)$/i, '');
@@ -0,0 +1,24 @@
1
+ #!/usr/bin/env node
2
+ import fs from 'fs';
3
+ import path from 'path';
4
+
5
+ const srcDir = './src';
6
+ const distDir = './dist';
7
+
8
+ function copyDefs(dir) {
9
+ const files = fs.readdirSync(dir);
10
+ for (const file of files) {
11
+ const fullPath = path.join(dir, file);
12
+ if (fs.statSync(fullPath).isDirectory()) {
13
+ copyDefs(fullPath);
14
+ } else if (file.endsWith('.d.ts')) {
15
+ const relativePath = path.relative(srcDir, fullPath);
16
+ const destPath = path.join(distDir, relativePath);
17
+ fs.mkdirSync(path.dirname(destPath), { recursive: true });
18
+ fs.copyFileSync(fullPath, destPath);
19
+ }
20
+ }
21
+ }
22
+
23
+ copyDefs(srcDir);
24
+ console.log('Arquivos .d.ts copiados para a pasta dist.');
@@ -1,33 +0,0 @@
1
- // * Essa é a classe de erro base, pode ser usada em debug para saber se um erro lançado veio do VSRepository
2
- export class VSRepoError extends Error {
3
- constructor(message, type){
4
- super(message);
5
- this.name = this.constructor.name;
6
- this.type = type; // * Tipo do erro para debug
7
- }
8
- }
9
-
10
- // * Dessa em diante são classes de erros específicas que estendem da base para saber em qual estágio do VSRepository foi lançado o erro, não vou detalhar cada uma pq o nome é auto explicativo
11
- export class VSRepoConfigError extends VSRepoError {
12
- constructor(message){
13
- super(message, "VSREPO_CONFIG");
14
- }
15
- }
16
-
17
- export class VSRepoBuildError extends VSRepoError {
18
- constructor(message){
19
- super(message, "VSREPO_BUILD");
20
- }
21
- }
22
-
23
- export class VSRepoExtendError extends VSRepoError {
24
- constructor(message){
25
- super(message, "VSREPO_EXTEND");
26
- }
27
- }
28
-
29
- export class VSRepoRuntimeError extends VSRepoError {
30
- constructor(message){
31
- super(message, "VSREPO_RUNTIME");
32
- }
33
- }
@@ -1,150 +0,0 @@
1
- import z from "zod";
2
- import { VSRepoConfigError, VSRepoBuildError } from "./VSRepoError.js";
3
-
4
- export function mergeWheres(currentWhere, restrictWhere) {
5
- const restrictWhereKeys = Object.keys(restrictWhere);
6
- for (let i = 0; i < restrictWhereKeys.length; i++) {
7
- const restrictWhereKey = restrictWhereKeys[i];
8
- const restrictWhereField = restrictWhere[restrictWhereKey];
9
-
10
- if (restrictWhereField === null || typeof restrictWhereField !== "object") {
11
- currentWhere[restrictWhereKey] = restrictWhereField;
12
- } else if (Array.isArray(restrictWhereField)) {
13
- currentWhere[restrictWhereKey] = Array.isArray(currentWhere[restrictWhereKey]) ?
14
- restrictWhereField.concat(currentWhere[restrictWhereKey]) :
15
- restrictWhereField;
16
- } else {
17
- currentWhere[restrictWhereKey] =
18
- currentWhere[restrictWhereKey] != null &&
19
- typeof currentWhere[restrictWhereKey] === "object" &&
20
- !Array.isArray(currentWhere[restrictWhereKey]) ?
21
- Object.assign(currentWhere[restrictWhereKey], restrictWhereField) :
22
- restrictWhereField;
23
- }
24
- }
25
- }
26
-
27
- const stringSchema = z.string().trim().nonempty();
28
- const objectSchema = z.looseObject({});
29
- const booleanSchema = z.boolean();
30
-
31
- export function validateConfig(config) {
32
- const configSchema = z.strictObject({
33
- tableName: stringSchema,
34
- pkName: stringSchema,
35
- selectModels: z.record(stringSchema, objectSchema).optional(),
36
- defaultSelectModel: stringSchema.optional(),
37
- requiredWhere: objectSchema.optional(),
38
- relations: z.record(
39
- stringSchema,
40
- z.strictObject({
41
- mode: z.enum(['otm', 'mtm', 'mto', 'oto']),
42
- restriction: z.enum(['set', 'add']),
43
- pk: stringSchema,
44
- nullAble: booleanSchema.optional(),
45
- })
46
- ).optional(),
47
- methods: z.record(stringSchema, z.strictObject({
48
- map: booleanSchema,
49
- selectModel: stringSchema.or(z.literal(false)).optional(),
50
- whereType: z.enum(["overwrite", "extending"]).optional(),
51
- proxyTo: stringSchema.optional(),
52
- pushWhere: objectSchema.optional(),
53
- fbMode: z.enum(["one", "list"]).optional(),
54
- injectOrdenation: objectSchema.or(z.array(objectSchema)).optional(),
55
- injectPagination: objectSchema.optional()
56
- })).optional()
57
- }).superRefine((config, ctx)=>{
58
- if (config.defaultSelectModel && !config.selectModels?.[config.defaultSelectModel]) {
59
- ctx.addIssue({
60
- code: "vs_custom",
61
- path: ["defaultSelectModel"],
62
- message: "must be a valid key of 'selectModels'",
63
- });
64
- }
65
-
66
- if (config.methods) {
67
- for (const [key, method] of Object.entries(config.methods)) {
68
- if (method.selectModel && !config.selectModels?.[method.selectModel]) {
69
- ctx.addIssue({
70
- code: "vs_custom",
71
- path: ["methods", key, "selectModel"],
72
- message: "must be a valid key of 'selectModels'",
73
- });
74
- }
75
- }
76
- }
77
- });
78
-
79
- const configParsed = configSchema.safeParse(config);
80
-
81
- if(!configParsed.success) {
82
- const firstIssue = configParsed.error.issues[0];
83
- const path = firstIssue.path.length ? firstIssue.path.join(".") : "config";
84
- throw new VSRepoConfigError(`[VSRepository] (config) ${path}: ${firstIssue.message}`)
85
- }
86
-
87
- return configParsed.data;
88
- }
89
-
90
- export function validateBuildConfig(buildConfig, buildInstance) {
91
- const activeSchema = booleanSchema.default(true);
92
- const defaultSelectSchema = buildInstance.selectModels ?
93
- z.enum(Object.keys(buildInstance.selectModels)).optional()
94
- : z.never("can only be passed if you have configured 'selectModels'.");
95
- const ignoreRequiredWhereSchema = booleanSchema.default(false);
96
-
97
- const buildConfigSchema = z.strictObject({
98
- freeze: booleanSchema.default(true),
99
- showWorking: booleanSchema.default(false),
100
- baseMethods: z.strictObject({
101
- get: z.strictObject({
102
- active: activeSchema,
103
- defaultSelect: defaultSelectSchema,
104
- ignoreRequiredWhere: ignoreRequiredWhereSchema
105
- }).optional(),
106
- getOrThrow: z.strictObject({
107
- active: activeSchema,
108
- defaultSelect: defaultSelectSchema,
109
- ignoreRequiredWhere: ignoreRequiredWhereSchema
110
- }).optional(),
111
- getAll: z.strictObject({
112
- active: activeSchema,
113
- defaultSelect: defaultSelectSchema,
114
- ignoreRequiredWhere: ignoreRequiredWhereSchema
115
- }).optional(),
116
- remove: z.strictObject({
117
- active: activeSchema,
118
- defaultSelect: defaultSelectSchema,
119
- ignoreRequiredWhere: ignoreRequiredWhereSchema
120
- }).optional(),
121
- save: z.strictObject({
122
- active: activeSchema,
123
- defaultSelect: defaultSelectSchema,
124
- ignoreRequiredWhere: ignoreRequiredWhereSchema
125
- }).optional(),
126
- removeList: z.strictObject({
127
- active: activeSchema,
128
- ignoreRequiredWhere: ignoreRequiredWhereSchema
129
- }).optional(),
130
- total: z.strictObject({
131
- active: activeSchema,
132
- ignoreRequiredWhere: ignoreRequiredWhereSchema
133
- }).optional(),
134
- has: z.strictObject({
135
- active: activeSchema,
136
- ignoreRequiredWhere: ignoreRequiredWhereSchema
137
- }).optional(),
138
- }).optional()
139
- });
140
-
141
- const buildConfigParsed = buildConfigSchema.safeParse(buildConfig);
142
-
143
- if(!buildConfigParsed.success) {
144
- const firstIssue = buildConfigParsed.error.issues[0];
145
- const path = firstIssue.path.length ? firstIssue.path.join(".") : "config";
146
- throw new VSRepoBuildError(`[VSRepository] (build) ${path}: ${firstIssue.message}`)
147
- }
148
-
149
- return buildConfigParsed.data;
150
- }