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
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.setupVSRepo = exports.VSRepository = void 0;
|
|
18
|
+
__exportStar(require("./internal/errors/vs-repo.error"), exports);
|
|
19
|
+
const build_config_validate_1 = require("./internal/validation/build-config.validate");
|
|
20
|
+
const constructor_config_validate_1 = require("./internal/validation/constructor-config.validate");
|
|
21
|
+
const extension_validate_1 = require("./internal/validation/extension.validate");
|
|
22
|
+
const prisma_client_validate_1 = require("./internal/validation/prisma-client.validate");
|
|
23
|
+
const vs_repo_error_1 = require("./internal/errors/vs-repo.error");
|
|
24
|
+
const base_methods_resolve_1 = require("./internal/resolvers/base-methods.resolve");
|
|
25
|
+
const logger_util_1 = require("./internal/utils/logger.util");
|
|
26
|
+
const dinamic_method_info_resolve_1 = require("./internal/resolvers/dinamic-method-info.resolve");
|
|
27
|
+
const dinamic_method_customization_resolve_1 = require("./internal/resolvers/dinamic-method-customization.resolve");
|
|
28
|
+
const pretty_wheres_resolve_1 = require("./internal/resolvers/pretty-wheres.resolve");
|
|
29
|
+
const specific_where_resolve_1 = require("./internal/resolvers/specific-where.resolve");
|
|
30
|
+
const dbAndPrismaArgs_resolve_1 = require("./internal/resolvers/dbAndPrismaArgs.resolve");
|
|
31
|
+
const method_options_validate_1 = require("./internal/validation/method-options.validate");
|
|
32
|
+
class VSRepository {
|
|
33
|
+
vsrepocache;
|
|
34
|
+
tableName;
|
|
35
|
+
pkName;
|
|
36
|
+
softRemovekName;
|
|
37
|
+
selectModels;
|
|
38
|
+
defaultSelectModel;
|
|
39
|
+
requiredWhere;
|
|
40
|
+
relations;
|
|
41
|
+
methods;
|
|
42
|
+
constructor(config) {
|
|
43
|
+
const validatedConfig = (0, constructor_config_validate_1.validateConstructorConfig)(config);
|
|
44
|
+
this.vsrepocache = new Map();
|
|
45
|
+
this.tableName = validatedConfig.tableName;
|
|
46
|
+
this.pkName = validatedConfig.pkName;
|
|
47
|
+
this.softRemovekName = validatedConfig.softRemovekName;
|
|
48
|
+
this.selectModels = validatedConfig.selectModels;
|
|
49
|
+
this.defaultSelectModel = validatedConfig.defaultSelectModel;
|
|
50
|
+
this.relations = validatedConfig.relations;
|
|
51
|
+
this.requiredWhere = validatedConfig.requiredWhere;
|
|
52
|
+
this.methods = validatedConfig.methods;
|
|
53
|
+
}
|
|
54
|
+
extend(extensionFunc) {
|
|
55
|
+
const extension = (0, extension_validate_1.validateExtension)(extensionFunc, this);
|
|
56
|
+
const extended = Object.assign(Object.create(this), extension);
|
|
57
|
+
if (Object.isFrozen(this)) {
|
|
58
|
+
Object.freeze(extended);
|
|
59
|
+
}
|
|
60
|
+
return extended;
|
|
61
|
+
}
|
|
62
|
+
build(prisma, config) {
|
|
63
|
+
(0, prisma_client_validate_1.validatePrismaClient)(prisma, this);
|
|
64
|
+
const buildInstance = Object.create(this);
|
|
65
|
+
buildInstance.prisma = prisma;
|
|
66
|
+
if (buildInstance.softRemovekName &&
|
|
67
|
+
prisma[buildInstance.tableName]["fields"][buildInstance.softRemovekName]["typeName"] !==
|
|
68
|
+
"DateTime") {
|
|
69
|
+
throw new vs_repo_error_1.VSRepoBuildError(`[VSRepository] (${buildInstance.tableName}: build) 'typeName' of 'softRemovekName' must be "DateTime": ${buildInstance.softRemovekName}`);
|
|
70
|
+
}
|
|
71
|
+
const validatedConfig = (0, build_config_validate_1.validateBuildConfig)(config ?? {}, buildInstance);
|
|
72
|
+
(0, base_methods_resolve_1.resolveBaseMethods)(buildInstance, validatedConfig);
|
|
73
|
+
const showWorking = validatedConfig.showWorking;
|
|
74
|
+
const methods = buildInstance.methods;
|
|
75
|
+
if (methods) {
|
|
76
|
+
const methodsToMap = Object.keys(methods).filter(m => methods[m]?.map);
|
|
77
|
+
if (showWorking)
|
|
78
|
+
(0, logger_util_1.logger)("Keys to map:", "build", buildInstance.tableName, methodsToMap);
|
|
79
|
+
for (let methodToMap of methodsToMap) {
|
|
80
|
+
const originalKey = methodToMap;
|
|
81
|
+
methodToMap = methods[originalKey]?.proxyTo ?? methodToMap;
|
|
82
|
+
const dinamicMethodInfo = (0, dinamic_method_info_resolve_1.resolveDinamicMethodInfo)(buildInstance, methodToMap, originalKey);
|
|
83
|
+
const dinamicMethodCustomization = (0, dinamic_method_customization_resolve_1.resolveDinamicMethodCustomization)(buildInstance, dinamicMethodInfo, originalKey);
|
|
84
|
+
const dinamicMethodWhereOps = {
|
|
85
|
+
uglyWheres: [],
|
|
86
|
+
prettyWheres: [],
|
|
87
|
+
whereType: methods[originalKey]?.whereType ?? "extending",
|
|
88
|
+
pushWhere: methods[originalKey]?.pushWhere,
|
|
89
|
+
};
|
|
90
|
+
if (!dinamicMethodInfo.ignoreWhere) {
|
|
91
|
+
(0, pretty_wheres_resolve_1.resolvePrettyWheres)(dinamicMethodInfo, dinamicMethodWhereOps);
|
|
92
|
+
if (showWorking) {
|
|
93
|
+
const argsSimulation = [];
|
|
94
|
+
for (let x = 0; x < dinamicMethodInfo.argsCount; x++) {
|
|
95
|
+
argsSimulation[x] = "00";
|
|
96
|
+
}
|
|
97
|
+
(0, logger_util_1.logger)(`Where object resolved to ${methodToMap}:`, "build", buildInstance.tableName, (0, specific_where_resolve_1.resolveSpecificWhere)(argsSimulation, dinamicMethodWhereOps.prettyWheres));
|
|
98
|
+
// logger(
|
|
99
|
+
// `Where object resolved to ${methodToMap}:`,
|
|
100
|
+
// "build",
|
|
101
|
+
// buildInstance.tableName,
|
|
102
|
+
// dinamicMethodWhereOps.prettyWheres,
|
|
103
|
+
// );
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
let select = undefined;
|
|
107
|
+
if (dinamicMethodInfo.existsMode) {
|
|
108
|
+
select = { [buildInstance.pkName]: true };
|
|
109
|
+
}
|
|
110
|
+
buildInstance.vsrepocache.set(originalKey, (args, methodOptions) => {
|
|
111
|
+
if (dinamicMethodInfo.prismaArgsIndex !== undefined)
|
|
112
|
+
return args.at(dinamicMethodInfo.prismaArgsIndex);
|
|
113
|
+
const resolveDbAndPrismaArgsData = {
|
|
114
|
+
instance: buildInstance,
|
|
115
|
+
options: methodOptions,
|
|
116
|
+
alreadyValidatedOptions: true,
|
|
117
|
+
baseConfig: {
|
|
118
|
+
active: true,
|
|
119
|
+
defaultSelect: methods[originalKey]?.selectModel,
|
|
120
|
+
ignoreRequiredWhere: dinamicMethodWhereOps.whereType === "overwrite",
|
|
121
|
+
},
|
|
122
|
+
withoutWhere: dinamicMethodInfo.ignoreWhere,
|
|
123
|
+
specificSelect: select,
|
|
124
|
+
pushWhere: dinamicMethodWhereOps.pushWhere,
|
|
125
|
+
withoutSelect: dinamicMethodInfo.ignoreSelect,
|
|
126
|
+
skipDuplicates: dinamicMethodCustomization.skipDuplicates,
|
|
127
|
+
ordenation: dinamicMethodCustomization.orderPosition !== undefined
|
|
128
|
+
? args.at(dinamicMethodCustomization.orderPosition)
|
|
129
|
+
: dinamicMethodCustomization.injectOrdenation,
|
|
130
|
+
pagination: dinamicMethodCustomization.paginationPosition !== undefined
|
|
131
|
+
? args.at(dinamicMethodCustomization.paginationPosition)
|
|
132
|
+
: dinamicMethodCustomization.injectPagination,
|
|
133
|
+
dataPayload: dinamicMethodInfo.dataIndex !== undefined
|
|
134
|
+
? args.at(dinamicMethodInfo.dataIndex)
|
|
135
|
+
: undefined,
|
|
136
|
+
createPayload: dinamicMethodInfo.createIndex !== undefined
|
|
137
|
+
? args.at(dinamicMethodInfo.createIndex)
|
|
138
|
+
: undefined,
|
|
139
|
+
updatePayload: dinamicMethodInfo.updateIndex !== undefined
|
|
140
|
+
? args.at(dinamicMethodInfo.updateIndex)
|
|
141
|
+
: undefined,
|
|
142
|
+
};
|
|
143
|
+
if (!dinamicMethodInfo.ignoreWhere) {
|
|
144
|
+
resolveDbAndPrismaArgsData.specificWhere = (0, specific_where_resolve_1.resolveSpecificWhere)(args, dinamicMethodWhereOps.prettyWheres);
|
|
145
|
+
}
|
|
146
|
+
else if (dinamicMethodInfo.onlyBaseWheres) {
|
|
147
|
+
resolveDbAndPrismaArgsData.specificWhere =
|
|
148
|
+
dinamicMethodInfo.whereIndex !== undefined
|
|
149
|
+
? args.at(dinamicMethodInfo.whereIndex)
|
|
150
|
+
: {};
|
|
151
|
+
}
|
|
152
|
+
const { prismaArgs } = (0, dbAndPrismaArgs_resolve_1.resolveDbAndPrismaArgs)(resolveDbAndPrismaArgsData);
|
|
153
|
+
return prismaArgs;
|
|
154
|
+
});
|
|
155
|
+
buildInstance[originalKey] = async (...args) => {
|
|
156
|
+
let db = buildInstance.prisma;
|
|
157
|
+
let methodOptions = undefined;
|
|
158
|
+
if (args.length < dinamicMethodInfo.argsCount) {
|
|
159
|
+
const missingParams = dinamicMethodInfo.whereParams
|
|
160
|
+
.concat(dinamicMethodInfo.otherParams)
|
|
161
|
+
.slice(args.length);
|
|
162
|
+
throw new vs_repo_error_1.VSRepoRuntimeError(`[VSRepository] (${buildInstance.tableName}: runtime) Missing parameters: ${missingParams.join(", ")}`);
|
|
163
|
+
}
|
|
164
|
+
else if (args.length > dinamicMethodInfo.argsCount) {
|
|
165
|
+
const optionsArg = args[args.length - 1];
|
|
166
|
+
methodOptions = (0, method_options_validate_1.validateMethodOptions)(optionsArg, buildInstance);
|
|
167
|
+
db = methodOptions.db ?? db;
|
|
168
|
+
}
|
|
169
|
+
else {
|
|
170
|
+
args.push("1");
|
|
171
|
+
}
|
|
172
|
+
const prismaArgs = buildInstance.vsrepocache.get(originalKey)(args, methodOptions);
|
|
173
|
+
const start = showWorking
|
|
174
|
+
? (0, logger_util_1.performanceLoggerStart)(buildInstance.tableName, dinamicMethodInfo.method, prismaArgs)
|
|
175
|
+
: undefined;
|
|
176
|
+
try {
|
|
177
|
+
const result = await db[buildInstance.tableName][dinamicMethodInfo.method](prismaArgs);
|
|
178
|
+
if (showWorking)
|
|
179
|
+
(0, logger_util_1.performanceLoggerEnd)(buildInstance.tableName, dinamicMethodInfo.method, start);
|
|
180
|
+
if (dinamicMethodInfo.existsMode) {
|
|
181
|
+
return !!result;
|
|
182
|
+
}
|
|
183
|
+
return result;
|
|
184
|
+
}
|
|
185
|
+
catch (err) {
|
|
186
|
+
// logger(
|
|
187
|
+
// `Fatal error when executing ${dinamicMethodInfo.method}:`,
|
|
188
|
+
// "runtime",
|
|
189
|
+
// buildInstance.tableName,
|
|
190
|
+
// { prismaArgs },
|
|
191
|
+
// );
|
|
192
|
+
throw err;
|
|
193
|
+
}
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
if (validatedConfig.freeze)
|
|
198
|
+
Object.freeze(buildInstance);
|
|
199
|
+
return buildInstance;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
exports.VSRepository = VSRepository;
|
|
203
|
+
const setupVSRepo = () => (config) => new VSRepository(config);
|
|
204
|
+
exports.setupVSRepo = setupVSRepo;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./VSRepository";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./VSRepository"), exports);
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.VSRepoRuntimeError = exports.VSRepoExtendError = exports.VSRepoBuildError = exports.VSRepoConfigError = exports.VSRepoError = void 0;
|
|
4
|
+
class VSRepoError extends Error {
|
|
5
|
+
}
|
|
6
|
+
exports.VSRepoError = VSRepoError;
|
|
7
|
+
class VSRepoConfigError extends VSRepoError {
|
|
8
|
+
type = "VSREPO_CONFIG";
|
|
9
|
+
}
|
|
10
|
+
exports.VSRepoConfigError = VSRepoConfigError;
|
|
11
|
+
class VSRepoBuildError extends VSRepoError {
|
|
12
|
+
type = "VSREPO_BUILD";
|
|
13
|
+
}
|
|
14
|
+
exports.VSRepoBuildError = VSRepoBuildError;
|
|
15
|
+
class VSRepoExtendError extends VSRepoError {
|
|
16
|
+
type = "VSREPO_EXTEND";
|
|
17
|
+
}
|
|
18
|
+
exports.VSRepoExtendError = VSRepoExtendError;
|
|
19
|
+
class VSRepoRuntimeError extends VSRepoError {
|
|
20
|
+
type = "VSREPO_RUNTIME";
|
|
21
|
+
code;
|
|
22
|
+
constructor(message, code) {
|
|
23
|
+
super(message);
|
|
24
|
+
this.code = code;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
exports.VSRepoRuntimeError = VSRepoRuntimeError;
|