@smartive/graphql-magic 19.1.0 → 19.1.1
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/CHANGELOG.md +3 -3
- package/dist/bin/gqm.cjs +129 -131
- package/dist/cjs/index.cjs +0 -69
- package/dist/esm/permissions/index.d.ts +0 -1
- package/dist/esm/permissions/index.js +0 -1
- package/dist/esm/permissions/index.js.map +1 -1
- package/package.json +1 -1
- package/src/bin/gqm/gqm.ts +1 -1
- package/src/{permissions/generate-types.ts → bin/gqm/permissions.ts} +3 -3
- package/src/permissions/index.ts +0 -1
- package/dist/esm/permissions/generate-types.d.ts +0 -2
- package/dist/esm/permissions/generate-types.js +0 -61
- package/dist/esm/permissions/generate-types.js.map +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
## [19.1.1](https://github.com/smartive/graphql-magic/compare/v19.1.0...v19.1.1) (2025-06-04)
|
|
2
2
|
|
|
3
3
|
|
|
4
|
-
###
|
|
4
|
+
### Bug Fixes
|
|
5
5
|
|
|
6
|
-
*
|
|
6
|
+
* Move permission generation to bin ([#304](https://github.com/smartive/graphql-magic/issues/304)) ([e7368ec](https://github.com/smartive/graphql-magic/commit/e7368eca9a61a8b0000c37ef3e7563c83e65aba5))
|
package/dist/bin/gqm.cjs
CHANGED
|
@@ -1253,76 +1253,9 @@ var import_graphql3 = require("graphql");
|
|
|
1253
1253
|
var import_isEqual = __toESM(require("lodash/isEqual"), 1);
|
|
1254
1254
|
var getColumnName = (field) => field.kind === "relation" ? field.foreignKey || `${field.name}Id` : field.name;
|
|
1255
1255
|
|
|
1256
|
-
// src/permissions/generate-types.ts
|
|
1257
|
-
var import_ts_morph = require("ts-morph");
|
|
1258
|
-
|
|
1259
1256
|
// src/permissions/generate.ts
|
|
1260
1257
|
var ACTIONS = ["READ", "CREATE", "UPDATE", "DELETE", "RESTORE", "LINK"];
|
|
1261
1258
|
|
|
1262
|
-
// src/permissions/generate-types.ts
|
|
1263
|
-
var generatePermissionTypes = (models) => {
|
|
1264
|
-
const project = new import_ts_morph.Project({
|
|
1265
|
-
manipulationSettings: {
|
|
1266
|
-
indentationText: import_ts_morph.IndentationText.TwoSpaces
|
|
1267
|
-
}
|
|
1268
|
-
});
|
|
1269
|
-
const sourceFile = project.createSourceFile("permissions.ts", "", {
|
|
1270
|
-
overwrite: true
|
|
1271
|
-
});
|
|
1272
|
-
sourceFile.addStatements(`export type PermissionsConfig = Record<Role, PermissionsBlock>;`);
|
|
1273
|
-
sourceFile.addStatements(
|
|
1274
|
-
(writer) => writer.write(`export type PermissionsBlock = true | `).inlineBlock(() => {
|
|
1275
|
-
writer.writeLine(`me?: UserPermissions,`);
|
|
1276
|
-
for (const model of models.entities) {
|
|
1277
|
-
writer.writeLine(`${model.name}?: ${model.name}Permissions,`);
|
|
1278
|
-
}
|
|
1279
|
-
})
|
|
1280
|
-
);
|
|
1281
|
-
const usedEnums = /* @__PURE__ */ new Set(["Role"]);
|
|
1282
|
-
for (const model of models.entities) {
|
|
1283
|
-
sourceFile.addStatements(
|
|
1284
|
-
(writer) => writer.write(`export type ${model.name}Where = `).inlineBlock(() => {
|
|
1285
|
-
for (const field of model.fields.filter((field2) => field2.filterable)) {
|
|
1286
|
-
if (field.kind === "relation") {
|
|
1287
|
-
writer.writeLine(`${field.name}?: ${field.type}Where,`);
|
|
1288
|
-
} else if (!field.kind || field.kind === "primitive") {
|
|
1289
|
-
writer.writeLine(`${field.name}?: ${PRIMITIVE_TYPES[field.type]} | ${PRIMITIVE_TYPES[field.type]}[],`);
|
|
1290
|
-
} else {
|
|
1291
|
-
if (field.kind === "enum") {
|
|
1292
|
-
usedEnums.add(field.type);
|
|
1293
|
-
}
|
|
1294
|
-
writer.writeLine(`${field.name}?: ${field.type} | ${field.type}[],`);
|
|
1295
|
-
}
|
|
1296
|
-
}
|
|
1297
|
-
})
|
|
1298
|
-
);
|
|
1299
|
-
sourceFile.addStatements(
|
|
1300
|
-
(writer) => writer.write(`export type ${model.name}Permissions = `).inlineBlock(() => {
|
|
1301
|
-
for (const action of ACTIONS) {
|
|
1302
|
-
writer.writeLine(`${action}?: true,`);
|
|
1303
|
-
}
|
|
1304
|
-
writer.writeLine(`WHERE?: ${model.name}Where,`);
|
|
1305
|
-
const relations = [...model.relations, ...model.reverseRelations];
|
|
1306
|
-
if (relations.length > 0) {
|
|
1307
|
-
writer.write(`RELATIONS?: `).inlineBlock(() => {
|
|
1308
|
-
for (const relation of relations) {
|
|
1309
|
-
writer.writeLine(`${relation.name}?: ${relation.targetModel.name}Permissions,`);
|
|
1310
|
-
}
|
|
1311
|
-
});
|
|
1312
|
-
}
|
|
1313
|
-
})
|
|
1314
|
-
);
|
|
1315
|
-
}
|
|
1316
|
-
for (const name2 of usedEnums) {
|
|
1317
|
-
sourceFile.addStatements(
|
|
1318
|
-
(writer) => writer.write(
|
|
1319
|
-
`type ${name2} = ${models.getModel(name2, "enum").values.map((v) => `'${v}'`).join(" | ")};`
|
|
1320
|
-
)
|
|
1321
|
-
);
|
|
1322
|
-
}
|
|
1323
|
-
return sourceFile.getFullText();
|
|
1324
|
-
};
|
|
1325
|
-
|
|
1326
1259
|
// src/resolvers/arguments.ts
|
|
1327
1260
|
var import_graphql4 = require("graphql");
|
|
1328
1261
|
|
|
@@ -2070,14 +2003,14 @@ var generateGraphqlClientTypes = async () => {
|
|
|
2070
2003
|
};
|
|
2071
2004
|
|
|
2072
2005
|
// src/bin/gqm/parse-knexfile.ts
|
|
2073
|
-
var
|
|
2006
|
+
var import_ts_morph4 = require("ts-morph");
|
|
2074
2007
|
|
|
2075
2008
|
// src/bin/gqm/static-eval.ts
|
|
2076
2009
|
var import_lodash = require("lodash");
|
|
2077
|
-
var
|
|
2010
|
+
var import_ts_morph2 = require("ts-morph");
|
|
2078
2011
|
|
|
2079
2012
|
// src/bin/gqm/visitor.ts
|
|
2080
|
-
var
|
|
2013
|
+
var import_ts_morph = require("ts-morph");
|
|
2081
2014
|
var visit = (node, context, visitor) => {
|
|
2082
2015
|
if (!node) {
|
|
2083
2016
|
if (visitor.undefined) {
|
|
@@ -2097,7 +2030,7 @@ var visit = (node, context, visitor) => {
|
|
|
2097
2030
|
console.error(node.getText());
|
|
2098
2031
|
throw new Error(
|
|
2099
2032
|
`Cannot handle kind ${get(
|
|
2100
|
-
Object.entries(
|
|
2033
|
+
Object.entries(import_ts_morph.SyntaxKind).find(([, val]) => val === kind),
|
|
2101
2034
|
0
|
|
2102
2035
|
)}`
|
|
2103
2036
|
);
|
|
@@ -2133,11 +2066,11 @@ var KNOWN_IDENTIFIERS = {
|
|
|
2133
2066
|
var staticEval = (node, context) => visit(node, context, VISITOR);
|
|
2134
2067
|
var VISITOR = {
|
|
2135
2068
|
undefined: () => void 0,
|
|
2136
|
-
[
|
|
2137
|
-
[
|
|
2069
|
+
[import_ts_morph2.SyntaxKind.VariableDeclaration]: (node, context) => staticEval(node.getInitializer(), context),
|
|
2070
|
+
[import_ts_morph2.SyntaxKind.ArrayLiteralExpression]: (node, context) => {
|
|
2138
2071
|
const values = [];
|
|
2139
2072
|
for (const value2 of node.getElements()) {
|
|
2140
|
-
if (value2.isKind(
|
|
2073
|
+
if (value2.isKind(import_ts_morph2.SyntaxKind.SpreadElement)) {
|
|
2141
2074
|
values.push(...staticEval(value2, context));
|
|
2142
2075
|
} else {
|
|
2143
2076
|
values.push(staticEval(value2, context));
|
|
@@ -2145,23 +2078,23 @@ var VISITOR = {
|
|
|
2145
2078
|
}
|
|
2146
2079
|
return values;
|
|
2147
2080
|
},
|
|
2148
|
-
[
|
|
2081
|
+
[import_ts_morph2.SyntaxKind.ObjectLiteralExpression]: (node, context) => {
|
|
2149
2082
|
const result = {};
|
|
2150
2083
|
for (const property of node.getProperties()) {
|
|
2151
2084
|
Object.assign(result, staticEval(property, context));
|
|
2152
2085
|
}
|
|
2153
2086
|
return result;
|
|
2154
2087
|
},
|
|
2155
|
-
[
|
|
2156
|
-
[
|
|
2088
|
+
[import_ts_morph2.SyntaxKind.StringLiteral]: (node) => node.getLiteralValue(),
|
|
2089
|
+
[import_ts_morph2.SyntaxKind.PropertyAssignment]: (node, context) => ({
|
|
2157
2090
|
[node.getName()]: staticEval(node.getInitializer(), context)
|
|
2158
2091
|
}),
|
|
2159
|
-
[
|
|
2092
|
+
[import_ts_morph2.SyntaxKind.ShorthandPropertyAssignment]: (node, context) => ({
|
|
2160
2093
|
[node.getName()]: staticEval(node.getNameNode(), context)
|
|
2161
2094
|
}),
|
|
2162
|
-
[
|
|
2163
|
-
[
|
|
2164
|
-
[
|
|
2095
|
+
[import_ts_morph2.SyntaxKind.SpreadElement]: (node, context) => staticEval(node.getExpression(), context),
|
|
2096
|
+
[import_ts_morph2.SyntaxKind.SpreadAssignment]: (node, context) => staticEval(node.getExpression(), context),
|
|
2097
|
+
[import_ts_morph2.SyntaxKind.Identifier]: (node, context) => {
|
|
2165
2098
|
const identifierName = node.getText();
|
|
2166
2099
|
if (identifierName in KNOWN_IDENTIFIERS) {
|
|
2167
2100
|
return KNOWN_IDENTIFIERS[identifierName];
|
|
@@ -2172,19 +2105,19 @@ var VISITOR = {
|
|
|
2172
2105
|
}
|
|
2173
2106
|
return staticEval(definitionNodes[0], context);
|
|
2174
2107
|
},
|
|
2175
|
-
[
|
|
2176
|
-
[
|
|
2177
|
-
[
|
|
2178
|
-
[
|
|
2179
|
-
[
|
|
2180
|
-
[
|
|
2181
|
-
[
|
|
2182
|
-
[
|
|
2108
|
+
[import_ts_morph2.SyntaxKind.ParenthesizedExpression]: (node, context) => staticEval(node.getExpression(), context),
|
|
2109
|
+
[import_ts_morph2.SyntaxKind.AsExpression]: (node, context) => staticEval(node.getExpression(), context),
|
|
2110
|
+
[import_ts_morph2.SyntaxKind.ConditionalExpression]: (node, context) => staticEval(node.getCondition(), context) ? staticEval(node.getWhenTrue(), context) : staticEval(node.getWhenFalse(), context),
|
|
2111
|
+
[import_ts_morph2.SyntaxKind.TrueKeyword]: () => true,
|
|
2112
|
+
[import_ts_morph2.SyntaxKind.FalseKeyword]: () => false,
|
|
2113
|
+
[import_ts_morph2.SyntaxKind.NumericLiteral]: (node) => node.getLiteralValue(),
|
|
2114
|
+
[import_ts_morph2.SyntaxKind.BigIntLiteral]: (node) => node.getLiteralValue(),
|
|
2115
|
+
[import_ts_morph2.SyntaxKind.CallExpression]: (node, context) => {
|
|
2183
2116
|
const method = staticEval(node.getExpression(), context);
|
|
2184
2117
|
const args2 = node.getArguments().map((arg) => staticEval(arg, context));
|
|
2185
2118
|
return method(...args2);
|
|
2186
2119
|
},
|
|
2187
|
-
[
|
|
2120
|
+
[import_ts_morph2.SyntaxKind.PropertyAccessExpression]: (node, context) => {
|
|
2188
2121
|
const target = staticEval(node.getExpression(), context);
|
|
2189
2122
|
const property = target[node.getName()];
|
|
2190
2123
|
if (typeof property === "function") {
|
|
@@ -2223,7 +2156,7 @@ var VISITOR = {
|
|
|
2223
2156
|
}
|
|
2224
2157
|
return property;
|
|
2225
2158
|
},
|
|
2226
|
-
[
|
|
2159
|
+
[import_ts_morph2.SyntaxKind.ArrowFunction]: (node, context) => {
|
|
2227
2160
|
return (...args2) => {
|
|
2228
2161
|
const parameters = {};
|
|
2229
2162
|
let i = 0;
|
|
@@ -2234,12 +2167,12 @@ var VISITOR = {
|
|
|
2234
2167
|
return staticEval(node.getBody(), { ...context, ...parameters });
|
|
2235
2168
|
};
|
|
2236
2169
|
},
|
|
2237
|
-
[
|
|
2170
|
+
[import_ts_morph2.SyntaxKind.Block]: (node, context) => {
|
|
2238
2171
|
for (const statement of node.getStatements()) {
|
|
2239
2172
|
return staticEval(statement, context);
|
|
2240
2173
|
}
|
|
2241
2174
|
},
|
|
2242
|
-
[
|
|
2175
|
+
[import_ts_morph2.SyntaxKind.CaseClause]: (node, context) => {
|
|
2243
2176
|
const statements = node.getStatements();
|
|
2244
2177
|
if (statements.length !== 1) {
|
|
2245
2178
|
console.error(node.getText());
|
|
@@ -2247,7 +2180,7 @@ var VISITOR = {
|
|
|
2247
2180
|
}
|
|
2248
2181
|
return staticEval(statements[0], context);
|
|
2249
2182
|
},
|
|
2250
|
-
[
|
|
2183
|
+
[import_ts_morph2.SyntaxKind.DefaultClause]: (node, context) => {
|
|
2251
2184
|
const statements = node.getStatements();
|
|
2252
2185
|
if (statements.length !== 1) {
|
|
2253
2186
|
console.error(node.getText());
|
|
@@ -2255,18 +2188,18 @@ var VISITOR = {
|
|
|
2255
2188
|
}
|
|
2256
2189
|
return staticEval(statements[0], context);
|
|
2257
2190
|
},
|
|
2258
|
-
[
|
|
2191
|
+
[import_ts_morph2.SyntaxKind.ReturnStatement]: (node, context) => {
|
|
2259
2192
|
return staticEval(node.getExpression(), context);
|
|
2260
2193
|
},
|
|
2261
|
-
[
|
|
2194
|
+
[import_ts_morph2.SyntaxKind.SwitchStatement]: (node, context) => {
|
|
2262
2195
|
const value2 = staticEval(node.getExpression(), context);
|
|
2263
2196
|
let active = false;
|
|
2264
2197
|
for (const clause of node.getCaseBlock().getClauses()) {
|
|
2265
2198
|
switch (clause.getKind()) {
|
|
2266
|
-
case
|
|
2199
|
+
case import_ts_morph2.SyntaxKind.DefaultClause:
|
|
2267
2200
|
return staticEval(clause, context);
|
|
2268
|
-
case
|
|
2269
|
-
const caseClause = clause.asKindOrThrow(
|
|
2201
|
+
case import_ts_morph2.SyntaxKind.CaseClause: {
|
|
2202
|
+
const caseClause = clause.asKindOrThrow(import_ts_morph2.SyntaxKind.CaseClause);
|
|
2270
2203
|
if (caseClause.getStatements().length && active) {
|
|
2271
2204
|
return staticEval(clause, context);
|
|
2272
2205
|
}
|
|
@@ -2281,15 +2214,15 @@ var VISITOR = {
|
|
|
2281
2214
|
}
|
|
2282
2215
|
}
|
|
2283
2216
|
},
|
|
2284
|
-
[
|
|
2285
|
-
[
|
|
2217
|
+
[import_ts_morph2.SyntaxKind.Parameter]: (node, context) => context[node.getName()],
|
|
2218
|
+
[import_ts_morph2.SyntaxKind.BinaryExpression]: (node, context) => {
|
|
2286
2219
|
const mapping = {
|
|
2287
|
-
[
|
|
2288
|
-
[
|
|
2289
|
-
[
|
|
2290
|
-
[
|
|
2291
|
-
[
|
|
2292
|
-
[
|
|
2220
|
+
[import_ts_morph2.SyntaxKind.EqualsEqualsEqualsToken]: (left, right) => left === right(),
|
|
2221
|
+
[import_ts_morph2.SyntaxKind.ExclamationEqualsEqualsToken]: (left, right) => left !== right(),
|
|
2222
|
+
[import_ts_morph2.SyntaxKind.BarBarToken]: (left, right) => left || right(),
|
|
2223
|
+
[import_ts_morph2.SyntaxKind.AmpersandAmpersandToken]: (left, right) => left && right(),
|
|
2224
|
+
[import_ts_morph2.SyntaxKind.EqualsEqualsToken]: (left, right) => left == right(),
|
|
2225
|
+
[import_ts_morph2.SyntaxKind.ExclamationEqualsToken]: (left, right) => left != right()
|
|
2293
2226
|
};
|
|
2294
2227
|
if (node.getOperatorToken().getKind() in mapping) {
|
|
2295
2228
|
return mapping[node.getOperatorToken().getKind()](
|
|
@@ -2299,40 +2232,40 @@ var VISITOR = {
|
|
|
2299
2232
|
}
|
|
2300
2233
|
throw new Error(`Cannot handle operator of kind ${node.getOperatorToken().getKindName()}`);
|
|
2301
2234
|
},
|
|
2302
|
-
[
|
|
2303
|
-
[
|
|
2304
|
-
[
|
|
2305
|
-
[
|
|
2306
|
-
[
|
|
2235
|
+
[import_ts_morph2.SyntaxKind.SatisfiesExpression]: (node, context) => staticEval(node.getExpression(), context),
|
|
2236
|
+
[import_ts_morph2.SyntaxKind.TemplateExpression]: (node, context) => node.getHead().getLiteralText() + node.getTemplateSpans().map((span) => staticEval(span.getExpression(), context) + staticEval(span.getLiteral(), context)).join(""),
|
|
2237
|
+
[import_ts_morph2.SyntaxKind.TemplateTail]: (node) => node.getLiteralText(),
|
|
2238
|
+
[import_ts_morph2.SyntaxKind.TemplateMiddle]: (node) => node.getLiteralText(),
|
|
2239
|
+
[import_ts_morph2.SyntaxKind.PrefixUnaryExpression]: (node, context) => {
|
|
2307
2240
|
switch (node.getOperatorToken()) {
|
|
2308
|
-
case
|
|
2241
|
+
case import_ts_morph2.SyntaxKind.PlusToken:
|
|
2309
2242
|
return +staticEval(node.getOperand(), context);
|
|
2310
|
-
case
|
|
2243
|
+
case import_ts_morph2.SyntaxKind.MinusToken:
|
|
2311
2244
|
return -staticEval(node.getOperand(), context);
|
|
2312
|
-
case
|
|
2245
|
+
case import_ts_morph2.SyntaxKind.TildeToken:
|
|
2313
2246
|
return ~staticEval(node.getOperand(), context);
|
|
2314
|
-
case
|
|
2247
|
+
case import_ts_morph2.SyntaxKind.ExclamationToken:
|
|
2315
2248
|
return !staticEval(node.getOperand(), context);
|
|
2316
|
-
case
|
|
2317
|
-
case
|
|
2249
|
+
case import_ts_morph2.SyntaxKind.PlusPlusToken:
|
|
2250
|
+
case import_ts_morph2.SyntaxKind.MinusMinusToken:
|
|
2318
2251
|
throw new Error(`Cannot handle assignments.`);
|
|
2319
2252
|
}
|
|
2320
2253
|
},
|
|
2321
|
-
[
|
|
2254
|
+
[import_ts_morph2.SyntaxKind.ElementAccessExpression]: (node, context) => {
|
|
2322
2255
|
const target = staticEval(node.getExpression(), context);
|
|
2323
2256
|
const argument = staticEval(node.getArgumentExpression(), context);
|
|
2324
2257
|
return target[argument];
|
|
2325
2258
|
},
|
|
2326
|
-
[
|
|
2327
|
-
[
|
|
2328
|
-
[
|
|
2329
|
-
[
|
|
2259
|
+
[import_ts_morph2.SyntaxKind.NoSubstitutionTemplateLiteral]: (node) => node.getLiteralValue(),
|
|
2260
|
+
[import_ts_morph2.SyntaxKind.NullKeyword]: () => null,
|
|
2261
|
+
[import_ts_morph2.SyntaxKind.NewExpression]: (node, context) => new (staticEval(node.getExpression(), context))(...node.getArguments().map((arg) => staticEval(arg, context))),
|
|
2262
|
+
[import_ts_morph2.SyntaxKind.TypeOfExpression]: (node, context) => typeof staticEval(node.getExpression(), context)
|
|
2330
2263
|
};
|
|
2331
2264
|
|
|
2332
2265
|
// src/bin/gqm/utils.ts
|
|
2333
|
-
var
|
|
2266
|
+
var import_ts_morph3 = require("ts-morph");
|
|
2334
2267
|
var findDeclarationInFile = (sourceFile, name2) => {
|
|
2335
|
-
const syntaxList = sourceFile.getChildrenOfKind(
|
|
2268
|
+
const syntaxList = sourceFile.getChildrenOfKind(import_ts_morph3.SyntaxKind.SyntaxList)[0];
|
|
2336
2269
|
if (!syntaxList) {
|
|
2337
2270
|
throw new Error("No SyntaxList");
|
|
2338
2271
|
}
|
|
@@ -2343,7 +2276,7 @@ var findDeclarationInFile = (sourceFile, name2) => {
|
|
|
2343
2276
|
return declaration;
|
|
2344
2277
|
};
|
|
2345
2278
|
var findDeclaration = (syntaxList, name2) => {
|
|
2346
|
-
for (const variableStatement of syntaxList.getChildrenOfKind(
|
|
2279
|
+
for (const variableStatement of syntaxList.getChildrenOfKind(import_ts_morph3.SyntaxKind.VariableStatement)) {
|
|
2347
2280
|
for (const declaration of variableStatement.getDeclarationList().getDeclarations()) {
|
|
2348
2281
|
if (declaration.getName() === name2) {
|
|
2349
2282
|
return declaration;
|
|
@@ -2354,9 +2287,9 @@ var findDeclaration = (syntaxList, name2) => {
|
|
|
2354
2287
|
|
|
2355
2288
|
// src/bin/gqm/parse-knexfile.ts
|
|
2356
2289
|
var parseKnexfile = async () => {
|
|
2357
|
-
const project = new
|
|
2290
|
+
const project = new import_ts_morph4.Project({
|
|
2358
2291
|
manipulationSettings: {
|
|
2359
|
-
indentationText:
|
|
2292
|
+
indentationText: import_ts_morph4.IndentationText.TwoSpaces
|
|
2360
2293
|
}
|
|
2361
2294
|
});
|
|
2362
2295
|
const knexfilePath = await getSetting("knexfilePath");
|
|
@@ -2368,11 +2301,11 @@ var parseKnexfile = async () => {
|
|
|
2368
2301
|
};
|
|
2369
2302
|
|
|
2370
2303
|
// src/bin/gqm/parse-models.ts
|
|
2371
|
-
var
|
|
2304
|
+
var import_ts_morph5 = require("ts-morph");
|
|
2372
2305
|
var parseModels = async () => {
|
|
2373
|
-
const project = new
|
|
2306
|
+
const project = new import_ts_morph5.Project({
|
|
2374
2307
|
manipulationSettings: {
|
|
2375
|
-
indentationText:
|
|
2308
|
+
indentationText: import_ts_morph5.IndentationText.TwoSpaces
|
|
2376
2309
|
}
|
|
2377
2310
|
});
|
|
2378
2311
|
const modelsPath = await getSetting("modelsPath");
|
|
@@ -2384,6 +2317,71 @@ var parseModels = async () => {
|
|
|
2384
2317
|
return models;
|
|
2385
2318
|
};
|
|
2386
2319
|
|
|
2320
|
+
// src/bin/gqm/permissions.ts
|
|
2321
|
+
var import_ts_morph6 = require("ts-morph");
|
|
2322
|
+
var generatePermissionTypes = (models) => {
|
|
2323
|
+
const project = new import_ts_morph6.Project({
|
|
2324
|
+
manipulationSettings: {
|
|
2325
|
+
indentationText: import_ts_morph6.IndentationText.TwoSpaces
|
|
2326
|
+
}
|
|
2327
|
+
});
|
|
2328
|
+
const sourceFile = project.createSourceFile("permissions.ts", "", {
|
|
2329
|
+
overwrite: true
|
|
2330
|
+
});
|
|
2331
|
+
sourceFile.addStatements(`export type PermissionsConfig = Record<Role, PermissionsBlock>;`);
|
|
2332
|
+
sourceFile.addStatements(
|
|
2333
|
+
(writer) => writer.write(`export type PermissionsBlock = true | `).inlineBlock(() => {
|
|
2334
|
+
writer.writeLine(`me?: UserPermissions,`);
|
|
2335
|
+
for (const model of models.entities) {
|
|
2336
|
+
writer.writeLine(`${model.name}?: ${model.name}Permissions,`);
|
|
2337
|
+
}
|
|
2338
|
+
})
|
|
2339
|
+
);
|
|
2340
|
+
const usedEnums = /* @__PURE__ */ new Set(["Role"]);
|
|
2341
|
+
for (const model of models.entities) {
|
|
2342
|
+
sourceFile.addStatements(
|
|
2343
|
+
(writer) => writer.write(`export type ${model.name}Where = `).inlineBlock(() => {
|
|
2344
|
+
for (const field of model.fields.filter((field2) => field2.filterable)) {
|
|
2345
|
+
if (field.kind === "relation") {
|
|
2346
|
+
writer.writeLine(`${field.name}?: ${field.type}Where,`);
|
|
2347
|
+
} else if (!field.kind || field.kind === "primitive") {
|
|
2348
|
+
writer.writeLine(`${field.name}?: ${PRIMITIVE_TYPES[field.type]} | ${PRIMITIVE_TYPES[field.type]}[],`);
|
|
2349
|
+
} else {
|
|
2350
|
+
if (field.kind === "enum") {
|
|
2351
|
+
usedEnums.add(field.type);
|
|
2352
|
+
}
|
|
2353
|
+
writer.writeLine(`${field.name}?: ${field.type} | ${field.type}[],`);
|
|
2354
|
+
}
|
|
2355
|
+
}
|
|
2356
|
+
})
|
|
2357
|
+
);
|
|
2358
|
+
sourceFile.addStatements(
|
|
2359
|
+
(writer) => writer.write(`export type ${model.name}Permissions = `).inlineBlock(() => {
|
|
2360
|
+
for (const action of ACTIONS) {
|
|
2361
|
+
writer.writeLine(`${action}?: true,`);
|
|
2362
|
+
}
|
|
2363
|
+
writer.writeLine(`WHERE?: ${model.name}Where,`);
|
|
2364
|
+
const relations = [...model.relations, ...model.reverseRelations];
|
|
2365
|
+
if (relations.length > 0) {
|
|
2366
|
+
writer.write(`RELATIONS?: `).inlineBlock(() => {
|
|
2367
|
+
for (const relation of relations) {
|
|
2368
|
+
writer.writeLine(`${relation.name}?: ${relation.targetModel.name}Permissions,`);
|
|
2369
|
+
}
|
|
2370
|
+
});
|
|
2371
|
+
}
|
|
2372
|
+
})
|
|
2373
|
+
);
|
|
2374
|
+
}
|
|
2375
|
+
for (const name2 of usedEnums) {
|
|
2376
|
+
sourceFile.addStatements(
|
|
2377
|
+
(writer) => writer.write(
|
|
2378
|
+
`type ${name2} = ${models.getModel(name2, "enum").values.map((v) => `'${v}'`).join(" | ")};`
|
|
2379
|
+
)
|
|
2380
|
+
);
|
|
2381
|
+
}
|
|
2382
|
+
return sourceFile.getFullText();
|
|
2383
|
+
};
|
|
2384
|
+
|
|
2387
2385
|
// src/bin/gqm/gqm.ts
|
|
2388
2386
|
(0, import_dotenv.config)({
|
|
2389
2387
|
path: ".env"
|
package/dist/cjs/index.cjs
CHANGED
|
@@ -84,7 +84,6 @@ __export(index_exports, {
|
|
|
84
84
|
generateDefinitions: () => generateDefinitions,
|
|
85
85
|
generateKnexTables: () => generateKnexTables,
|
|
86
86
|
generateMutations: () => generateMutations,
|
|
87
|
-
generatePermissionTypes: () => generatePermissionTypes,
|
|
88
87
|
generatePermissions: () => generatePermissions,
|
|
89
88
|
get: () => get,
|
|
90
89
|
getActionableRelations: () => getActionableRelations,
|
|
@@ -1989,9 +1988,6 @@ var applyWhere = (model, query, alias, where, aliases) => {
|
|
|
1989
1988
|
}
|
|
1990
1989
|
};
|
|
1991
1990
|
|
|
1992
|
-
// src/permissions/generate-types.ts
|
|
1993
|
-
var import_ts_morph = require("ts-morph");
|
|
1994
|
-
|
|
1995
1991
|
// src/permissions/generate.ts
|
|
1996
1992
|
var ACTIONS = ["READ", "CREATE", "UPDATE", "DELETE", "RESTORE", "LINK"];
|
|
1997
1993
|
var generatePermissions = (models, config) => {
|
|
@@ -2073,70 +2069,6 @@ var addPermissions = (models, permissions, links, block) => {
|
|
|
2073
2069
|
}
|
|
2074
2070
|
};
|
|
2075
2071
|
|
|
2076
|
-
// src/permissions/generate-types.ts
|
|
2077
|
-
var generatePermissionTypes = (models) => {
|
|
2078
|
-
const project = new import_ts_morph.Project({
|
|
2079
|
-
manipulationSettings: {
|
|
2080
|
-
indentationText: import_ts_morph.IndentationText.TwoSpaces
|
|
2081
|
-
}
|
|
2082
|
-
});
|
|
2083
|
-
const sourceFile = project.createSourceFile("permissions.ts", "", {
|
|
2084
|
-
overwrite: true
|
|
2085
|
-
});
|
|
2086
|
-
sourceFile.addStatements(`export type PermissionsConfig = Record<Role, PermissionsBlock>;`);
|
|
2087
|
-
sourceFile.addStatements(
|
|
2088
|
-
(writer) => writer.write(`export type PermissionsBlock = true | `).inlineBlock(() => {
|
|
2089
|
-
writer.writeLine(`me?: UserPermissions,`);
|
|
2090
|
-
for (const model of models.entities) {
|
|
2091
|
-
writer.writeLine(`${model.name}?: ${model.name}Permissions,`);
|
|
2092
|
-
}
|
|
2093
|
-
})
|
|
2094
|
-
);
|
|
2095
|
-
const usedEnums = /* @__PURE__ */ new Set(["Role"]);
|
|
2096
|
-
for (const model of models.entities) {
|
|
2097
|
-
sourceFile.addStatements(
|
|
2098
|
-
(writer) => writer.write(`export type ${model.name}Where = `).inlineBlock(() => {
|
|
2099
|
-
for (const field of model.fields.filter((field2) => field2.filterable)) {
|
|
2100
|
-
if (field.kind === "relation") {
|
|
2101
|
-
writer.writeLine(`${field.name}?: ${field.type}Where,`);
|
|
2102
|
-
} else if (!field.kind || field.kind === "primitive") {
|
|
2103
|
-
writer.writeLine(`${field.name}?: ${PRIMITIVE_TYPES[field.type]} | ${PRIMITIVE_TYPES[field.type]}[],`);
|
|
2104
|
-
} else {
|
|
2105
|
-
if (field.kind === "enum") {
|
|
2106
|
-
usedEnums.add(field.type);
|
|
2107
|
-
}
|
|
2108
|
-
writer.writeLine(`${field.name}?: ${field.type} | ${field.type}[],`);
|
|
2109
|
-
}
|
|
2110
|
-
}
|
|
2111
|
-
})
|
|
2112
|
-
);
|
|
2113
|
-
sourceFile.addStatements(
|
|
2114
|
-
(writer) => writer.write(`export type ${model.name}Permissions = `).inlineBlock(() => {
|
|
2115
|
-
for (const action of ACTIONS) {
|
|
2116
|
-
writer.writeLine(`${action}?: true,`);
|
|
2117
|
-
}
|
|
2118
|
-
writer.writeLine(`WHERE?: ${model.name}Where,`);
|
|
2119
|
-
const relations = [...model.relations, ...model.reverseRelations];
|
|
2120
|
-
if (relations.length > 0) {
|
|
2121
|
-
writer.write(`RELATIONS?: `).inlineBlock(() => {
|
|
2122
|
-
for (const relation of relations) {
|
|
2123
|
-
writer.writeLine(`${relation.name}?: ${relation.targetModel.name}Permissions,`);
|
|
2124
|
-
}
|
|
2125
|
-
});
|
|
2126
|
-
}
|
|
2127
|
-
})
|
|
2128
|
-
);
|
|
2129
|
-
}
|
|
2130
|
-
for (const name2 of usedEnums) {
|
|
2131
|
-
sourceFile.addStatements(
|
|
2132
|
-
(writer) => writer.write(
|
|
2133
|
-
`type ${name2} = ${models.getModel(name2, "enum").values.map((v) => `'${v}'`).join(" | ")};`
|
|
2134
|
-
)
|
|
2135
|
-
);
|
|
2136
|
-
}
|
|
2137
|
-
return sourceFile.getFullText();
|
|
2138
|
-
};
|
|
2139
|
-
|
|
2140
2072
|
// src/resolvers/arguments.ts
|
|
2141
2073
|
var import_graphql4 = require("graphql");
|
|
2142
2074
|
function getRawValue(value2, values) {
|
|
@@ -3627,7 +3559,6 @@ var printSchemaFromModels = (models) => printSchema((0, import_graphql6.buildAST
|
|
|
3627
3559
|
generateDefinitions,
|
|
3628
3560
|
generateKnexTables,
|
|
3629
3561
|
generateMutations,
|
|
3630
|
-
generatePermissionTypes,
|
|
3631
3562
|
generatePermissions,
|
|
3632
3563
|
get,
|
|
3633
3564
|
getActionableRelations,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/permissions/index.ts"],"names":[],"mappings":"AAAA,iCAAiC;AAEjC,cAAc,SAAS,CAAC;AACxB,cAAc,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/permissions/index.ts"],"names":[],"mappings":"AAAA,iCAAiC;AAEjC,cAAc,SAAS,CAAC;AACxB,cAAc,YAAY,CAAC"}
|
package/package.json
CHANGED
package/src/bin/gqm/gqm.ts
CHANGED
|
@@ -12,11 +12,11 @@ import {
|
|
|
12
12
|
getMigrationDate,
|
|
13
13
|
printSchemaFromModels,
|
|
14
14
|
} from '../..';
|
|
15
|
-
import { generatePermissionTypes } from '../../permissions/generate-types';
|
|
16
15
|
import { DateLibrary } from '../../utils/dates';
|
|
17
16
|
import { generateGraphqlApiTypes, generateGraphqlClientTypes } from './codegen';
|
|
18
17
|
import { parseKnexfile } from './parse-knexfile';
|
|
19
18
|
import { parseModels } from './parse-models';
|
|
19
|
+
import { generatePermissionTypes } from './permissions';
|
|
20
20
|
import { readLine } from './readline';
|
|
21
21
|
import { getSetting, writeToFile } from './settings';
|
|
22
22
|
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { IndentationText, Project } from 'ts-morph';
|
|
2
|
-
import { PRIMITIVE_TYPES } from '
|
|
3
|
-
import { Models } from '
|
|
4
|
-
import { ACTIONS } from '
|
|
2
|
+
import { PRIMITIVE_TYPES } from '../../db/generate';
|
|
3
|
+
import { Models } from '../../models';
|
|
4
|
+
import { ACTIONS } from '../../permissions';
|
|
5
5
|
|
|
6
6
|
export const generatePermissionTypes = (models: Models) => {
|
|
7
7
|
const project = new Project({
|
package/src/permissions/index.ts
CHANGED
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
import { IndentationText, Project } from 'ts-morph';
|
|
2
|
-
import { PRIMITIVE_TYPES } from '../db/generate';
|
|
3
|
-
import { ACTIONS } from './generate';
|
|
4
|
-
export const generatePermissionTypes = (models) => {
|
|
5
|
-
const project = new Project({
|
|
6
|
-
manipulationSettings: {
|
|
7
|
-
indentationText: IndentationText.TwoSpaces,
|
|
8
|
-
},
|
|
9
|
-
});
|
|
10
|
-
const sourceFile = project.createSourceFile('permissions.ts', '', {
|
|
11
|
-
overwrite: true,
|
|
12
|
-
});
|
|
13
|
-
sourceFile.addStatements(`export type PermissionsConfig = Record<Role, PermissionsBlock>;`);
|
|
14
|
-
sourceFile.addStatements((writer) => writer.write(`export type PermissionsBlock = true | `).inlineBlock(() => {
|
|
15
|
-
writer.writeLine(`me?: UserPermissions,`);
|
|
16
|
-
for (const model of models.entities) {
|
|
17
|
-
writer.writeLine(`${model.name}?: ${model.name}Permissions,`);
|
|
18
|
-
}
|
|
19
|
-
}));
|
|
20
|
-
const usedEnums = new Set(['Role']);
|
|
21
|
-
for (const model of models.entities) {
|
|
22
|
-
sourceFile.addStatements((writer) => writer.write(`export type ${model.name}Where = `).inlineBlock(() => {
|
|
23
|
-
for (const field of model.fields.filter((field) => field.filterable)) {
|
|
24
|
-
if (field.kind === 'relation') {
|
|
25
|
-
writer.writeLine(`${field.name}?: ${field.type}Where,`);
|
|
26
|
-
}
|
|
27
|
-
else if (!field.kind || field.kind === 'primitive') {
|
|
28
|
-
writer.writeLine(`${field.name}?: ${PRIMITIVE_TYPES[field.type]} | ${PRIMITIVE_TYPES[field.type]}[],`);
|
|
29
|
-
}
|
|
30
|
-
else {
|
|
31
|
-
if (field.kind === 'enum') {
|
|
32
|
-
usedEnums.add(field.type);
|
|
33
|
-
}
|
|
34
|
-
writer.writeLine(`${field.name}?: ${field.type} | ${field.type}[],`);
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
}));
|
|
38
|
-
sourceFile.addStatements((writer) => writer.write(`export type ${model.name}Permissions = `).inlineBlock(() => {
|
|
39
|
-
for (const action of ACTIONS) {
|
|
40
|
-
writer.writeLine(`${action}?: true,`);
|
|
41
|
-
}
|
|
42
|
-
writer.writeLine(`WHERE?: ${model.name}Where,`);
|
|
43
|
-
const relations = [...model.relations, ...model.reverseRelations];
|
|
44
|
-
if (relations.length > 0) {
|
|
45
|
-
writer.write(`RELATIONS?: `).inlineBlock(() => {
|
|
46
|
-
for (const relation of relations) {
|
|
47
|
-
writer.writeLine(`${relation.name}?: ${relation.targetModel.name}Permissions,`);
|
|
48
|
-
}
|
|
49
|
-
});
|
|
50
|
-
}
|
|
51
|
-
}));
|
|
52
|
-
}
|
|
53
|
-
for (const name of usedEnums) {
|
|
54
|
-
sourceFile.addStatements((writer) => writer.write(`type ${name} = ${models
|
|
55
|
-
.getModel(name, 'enum')
|
|
56
|
-
.values.map((v) => `'${v}'`)
|
|
57
|
-
.join(' | ')};`));
|
|
58
|
-
}
|
|
59
|
-
return sourceFile.getFullText();
|
|
60
|
-
};
|
|
61
|
-
//# sourceMappingURL=generate-types.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"generate-types.js","sourceRoot":"","sources":["../../../src/permissions/generate-types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,OAAO,EAAE,MAAM,UAAU,CAAC;AACpD,OAAO,EAAE,e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