vsrepo 1.2.6 → 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.
- package/README.md +296 -437
- package/{VSRepository → dist}/VSRepoError.d.ts +1 -1
- package/dist/VSRepoError.js +17 -0
- package/{VSRepository → dist}/VSRepository.d.ts +937 -907
- package/dist/VSRepository.js +204 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +17 -0
- package/dist/internal/errors/types/vs-repo-error-type.type.js +2 -0
- package/dist/internal/errors/vs-repo.error.js +27 -0
- package/dist/internal/resolvers/base-methods.resolve.js +535 -0
- package/dist/internal/resolvers/create-update-payloads-with-relations.resolve.js +143 -0
- package/dist/internal/resolvers/data-payload-with-relations.resolve.js +60 -0
- package/dist/internal/resolvers/dbAndPrismaArgs.resolve.js +46 -0
- package/dist/internal/resolvers/dinamic-method-customization.resolve.js +45 -0
- package/dist/internal/resolvers/dinamic-method-info.resolve.js +265 -0
- package/dist/internal/resolvers/merge-wheres.resolve.js +22 -0
- package/dist/internal/resolvers/pretty-wheres.resolve.js +87 -0
- package/dist/internal/resolvers/select.resolve.js +7 -0
- package/dist/internal/resolvers/specific-where.resolve.js +77 -0
- package/dist/internal/resolvers/types/base-method-function.type.js +2 -0
- package/dist/internal/resolvers/types/dinamic-method-customization.type.js +2 -0
- package/dist/internal/resolvers/types/dinamic-method-info.type.js +2 -0
- package/dist/internal/resolvers/types/dinamic-method-where-ops.type.js +2 -0
- package/dist/internal/resolvers/types/pretty-where.type.js +2 -0
- package/dist/internal/resolvers/types/prisma-args.type.js +2 -0
- package/dist/internal/resolvers/types/repository-build-instance.type.js +2 -0
- package/dist/internal/resolvers/types/resolve-db-and-prisma-args-data.type.js +2 -0
- package/dist/internal/resolvers/types/ugly-where.type.js +2 -0
- package/dist/internal/resolvers/ugly-where.resolve.js +178 -0
- package/dist/internal/utils/logger.util.js +21 -0
- package/dist/internal/utils/schemas.util.js +10 -0
- package/dist/internal/utils/uncapitalize.util.js +6 -0
- package/dist/internal/validation/build-config.validate.js +84 -0
- package/dist/internal/validation/constructor-config.validate.js +68 -0
- package/dist/internal/validation/extension.validate.js +15 -0
- package/dist/internal/validation/is-object.validate.js +6 -0
- package/dist/internal/validation/method-options.validate.js +26 -0
- package/dist/internal/validation/obj-with-relations.validate.js +37 -0
- package/dist/internal/validation/prisma-client.validate.js +10 -0
- package/dist/internal/validation/types/base-methods.type.js +2 -0
- package/dist/internal/validation/types/build-config.type.js +2 -0
- package/dist/internal/validation/types/constructor-config.type.js +2 -0
- package/dist/internal/validation/types/method-options.type.js +2 -0
- package/dist/internal/validation/types/method.type.js +2 -0
- package/dist/internal/validation/types/pagination.type.js +2 -0
- package/dist/internal/validation/types/relation.type.js +2 -0
- package/dist/internal/validation/types/see-mode.type.js +2 -0
- package/package.json +72 -70
- package/scripts/configure-prisma-import.mjs +3 -3
- package/scripts/copy-types.mjs +24 -0
- package/VSRepository/VSRepoError.js +0 -33
- package/VSRepository/VSRepoUtils.js +0 -155
- package/VSRepository/VSRepository.js +0 -1445
- package/VSRepository/index.d.ts +0 -2
- package/VSRepository/index.js +0 -2
|
@@ -69,10 +69,10 @@ const packageRoot = installedPackageExists
|
|
|
69
69
|
? installedNodeModulesPath
|
|
70
70
|
: path.resolve(scriptDirectory, '..');
|
|
71
71
|
|
|
72
|
-
const sourceDir = path.join(packageRoot, '
|
|
72
|
+
const sourceDir = path.join(packageRoot, 'dist');
|
|
73
73
|
|
|
74
74
|
if (!fs.existsSync(sourceDir)) {
|
|
75
|
-
console.error(`Nao foi possivel localizar a pasta
|
|
75
|
+
console.error(`Nao foi possivel localizar a pasta dist em: ${sourceDir}`);
|
|
76
76
|
process.exit(1);
|
|
77
77
|
}
|
|
78
78
|
|
|
@@ -99,7 +99,7 @@ for (const fileName of typeFiles) {
|
|
|
99
99
|
let contents = fs.readFileSync(sourceFile, 'utf8');
|
|
100
100
|
|
|
101
101
|
contents = contents.replace(
|
|
102
|
-
/from\s+(['"])
|
|
102
|
+
/from\s+(['"])\.\.\/generated\/prisma\/client\1/g,
|
|
103
103
|
`from '${prismaTargetPath === CLASSIC_PRISMA_GENERATED_PATH ? prismaTargetPath : relativeImportPath(path.dirname(targetFile), prismaTargetPath)}'`
|
|
104
104
|
);
|
|
105
105
|
|
|
@@ -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,155 +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.tableName}: 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
|
-
patch: z.strictObject({
|
|
127
|
-
active: activeSchema,
|
|
128
|
-
defaultSelect: defaultSelectSchema,
|
|
129
|
-
ignoreRequiredWhere: ignoreRequiredWhereSchema
|
|
130
|
-
}).optional(),
|
|
131
|
-
removeList: z.strictObject({
|
|
132
|
-
active: activeSchema,
|
|
133
|
-
ignoreRequiredWhere: ignoreRequiredWhereSchema
|
|
134
|
-
}).optional(),
|
|
135
|
-
total: z.strictObject({
|
|
136
|
-
active: activeSchema,
|
|
137
|
-
ignoreRequiredWhere: ignoreRequiredWhereSchema
|
|
138
|
-
}).optional(),
|
|
139
|
-
has: z.strictObject({
|
|
140
|
-
active: activeSchema,
|
|
141
|
-
ignoreRequiredWhere: ignoreRequiredWhereSchema
|
|
142
|
-
}).optional(),
|
|
143
|
-
}).optional()
|
|
144
|
-
});
|
|
145
|
-
|
|
146
|
-
const buildConfigParsed = buildConfigSchema.safeParse(buildConfig);
|
|
147
|
-
|
|
148
|
-
if(!buildConfigParsed.success) {
|
|
149
|
-
const firstIssue = buildConfigParsed.error.issues[0];
|
|
150
|
-
const path = firstIssue.path.length ? firstIssue.path.join(".") : "config";
|
|
151
|
-
throw new VSRepoBuildError(`[VSRepository] (${config.tableName}: build) ${path}: ${firstIssue.message}`)
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
return buildConfigParsed.data;
|
|
155
|
-
}
|