@lbu/code-gen
Advanced tools
Comparing version 0.0.78 to 0.0.79
@@ -601,6 +601,9 @@ import { Logger } from "@lbu/insight"; | ||
* Posibbly adding createdAt and updatedAt fields. | ||
* When withHistory is true, it automatically enables withDates. | ||
* When withSoftDeletes is true, it automatically enables withDates. | ||
* Added by the 'sql' plugin | ||
*/ | ||
enableQueries(options?: { withHistory?: boolean; withDates?: boolean }): this; | ||
enableQueries(options?: { | ||
withSoftDeletes?: boolean; | ||
withDates?: boolean; | ||
}): this; | ||
} | ||
@@ -607,0 +610,0 @@ |
{ | ||
"name": "@lbu/code-gen", | ||
"version": "0.0.78", | ||
"version": "0.0.79", | ||
"description": "Generate various boring parts of your server", | ||
@@ -19,5 +19,5 @@ "main": "./index.js", | ||
"dependencies": { | ||
"@lbu/cli": "0.0.78", | ||
"@lbu/insight": "0.0.78", | ||
"@lbu/stdlib": "0.0.78" | ||
"@lbu/cli": "0.0.79", | ||
"@lbu/insight": "0.0.79", | ||
"@lbu/stdlib": "0.0.79" | ||
}, | ||
@@ -45,3 +45,3 @@ "maintainers": [ | ||
}, | ||
"gitHead": "9ea11a8ce0e17e7823ff11cac7e06db2960f33e2" | ||
"gitHead": "c8f13508e33887b1c9ac6586c4cc1aa906c0a121" | ||
} |
@@ -147,3 +147,2 @@ import { newLogger, printProcessMemoryUsage } from "@lbu/insight"; | ||
if (errors) { | ||
this.logger.error(errors); | ||
throw errors[0]; | ||
@@ -169,3 +168,2 @@ } | ||
if (errors) { | ||
this.logger.error(errors); | ||
throw errors[0]; | ||
@@ -172,0 +170,0 @@ } |
@@ -24,10 +24,17 @@ import { existsSync } from "fs"; | ||
let stringifyInput; | ||
// validators may not be present, fallback to just stringify | ||
const stringifyInput = (!isNil(codeGenValidators.structure) | ||
? JSON.stringify(codeGenValidators.structure(generatorInput).data) | ||
: JSON.stringify(generatorInput) | ||
) | ||
.replace(/\\/g, "\\\\") | ||
.replace("'", "\\'"); | ||
if (!isNil(codeGenValidators.structure)) { | ||
const { data, errors } = codeGenValidators.structure(generatorInput); | ||
if (errors) { | ||
app.logger.error(errors); | ||
} | ||
stringifyInput = JSON.stringify(data); | ||
} else { | ||
stringifyInput = JSON.stringify(generatorInput); | ||
} | ||
stringifyInput = stringifyInput.replace(/\\/g, "\\\\").replace("'", "\\'"); | ||
recursiveLinkupReferences(generatorInput, generatorInput); | ||
@@ -34,0 +41,0 @@ addFieldsOfRelations(generatorInput); |
@@ -103,3 +103,3 @@ // Generated by @lbu/code-gen | ||
* | ||
* @typedef { { "type":"object", "docString":string, "isOptional":boolean, "defaultValue"?:( string|boolean|number ) , "uniqueName"?:string , "group"?:string , "name"?:string , "sql"?:{ "primary":boolean, "searchable":boolean, } , "validator":{ "strict":boolean, }, "keys":Object< string , CodeGenType >, "enableQueries":boolean, "queryOptions"?:{ "withHistory":boolean, "withDates":boolean, } , }} | ||
* @typedef { { "type":"object", "docString":string, "isOptional":boolean, "defaultValue"?:( string|boolean|number ) , "uniqueName"?:string , "group"?:string , "name"?:string , "sql"?:{ "primary":boolean, "searchable":boolean, } , "validator":{ "strict":boolean, }, "keys":Object< string , CodeGenType >, "enableQueries":boolean, "queryOptions"?:{ "withSoftDeletes":boolean, "withDates":boolean, } , }} | ||
*/ | ||
@@ -110,3 +110,3 @@ | ||
* | ||
* @typedef { { "type":"object", "docString"?:string , "isOptional"?:boolean , "defaultValue"?:( string|boolean|number ) , "uniqueName"?:string , "group"?:string , "name"?:string , "sql"?:{ "primary"?:boolean , "searchable"?:boolean , } , "validator":{ "strict"?:boolean , }, "keys":Object< string , CodeGenType_Input >, "enableQueries"?:boolean , "queryOptions"?:{ "withHistory"?:boolean , "withDates"?:boolean , } , }} | ||
* @typedef { { "type":"object", "docString"?:string , "isOptional"?:boolean , "defaultValue"?:( string|boolean|number ) , "uniqueName"?:string , "group"?:string , "name"?:string , "sql"?:{ "primary"?:boolean , "searchable"?:boolean , } , "validator":{ "strict"?:boolean , }, "keys":Object< string , CodeGenType_Input >, "enableQueries"?:boolean , "queryOptions"?:{ "withSoftDeletes"?:boolean , "withDates"?:boolean , } , }} | ||
*/ | ||
@@ -113,0 +113,0 @@ |
@@ -1,2 +0,1 @@ | ||
import { isNil } from "@lbu/stdlib"; | ||
import { addToData } from "../../generate.js"; | ||
@@ -33,4 +32,4 @@ import { TypeBuilder, TypeCreator } from "../../types/index.js"; | ||
const { group, name } = item; | ||
// withHistory implies withDates | ||
if (item?.queryOptions?.withDates || item?.queryOptions?.withHistory) { | ||
// withSoftDeletes implies withDates | ||
if (item?.queryOptions?.withDates || item?.queryOptions?.withSoftDeletes) { | ||
addDateFields(item); | ||
@@ -224,3 +223,2 @@ } | ||
function addDateFields(item) { | ||
item.queryOptions.dateFields = true; | ||
item.keys.createdAt = { | ||
@@ -246,2 +244,14 @@ ...TypeBuilder.getBaseData(), | ||
}; | ||
if (item.queryOptions?.withSoftDeletes) { | ||
item.keys.deletedAt = { | ||
...TypeBuilder.getBaseData(), | ||
...TypeCreator.types.get("date").class.getBaseData(), | ||
type: "date", | ||
isOptional: true, | ||
sql: { | ||
searchable: true, | ||
}, | ||
}; | ||
} | ||
} | ||
@@ -268,7 +278,4 @@ | ||
const it = getItem(item.keys[key]); | ||
// We don't support optional field searching, since it will break the way we do the | ||
// query generation e.g. NULL IS NULL is always true and thus the search results are | ||
// invalid. However if a default value is set, we expect that this will be honored | ||
// throughout all of the application. | ||
if ((it.isOptional && isNil(it.defaultValue)) || !it?.sql?.searchable) { | ||
if (!it?.sql?.searchable) { | ||
continue; | ||
@@ -284,4 +291,24 @@ } | ||
isOptional: true, | ||
defaultValue: undefined, | ||
}; | ||
if ( | ||
key === "deletedAt" && | ||
item.queryOptions?.withSoftDeletes && | ||
type === "date" | ||
) { | ||
fieldsArray.push({ | ||
key, | ||
name: "deletedAtInclude", | ||
type: "includeNotNull", | ||
}); | ||
resultType.keys["deletedAtInclude"] = { | ||
...TypeBuilder.getBaseData(), | ||
...TypeCreator.types.get("boolean").class.getBaseData(), | ||
...settings, | ||
}; | ||
resultType.docString += `By default 'where.deletedAtInclude' will only include 'null' values. To use the other generated variants like 'deletedAtGreaterThan', set this value to 'true'.`; | ||
} | ||
if (type === "number" || type === "date") { | ||
@@ -288,0 +315,0 @@ // Generate =, > and < queries |
@@ -11,3 +11,3 @@ import { dirnameForModule, pathJoin } from "@lbu/stdlib"; | ||
* @param {object} [options={}] | ||
* @param {boolean} [options.withHistory] | ||
* @param {boolean} [options.withSoftDeletes] | ||
* @param {boolean} [options.withDates] | ||
@@ -14,0 +14,0 @@ * @returns {ObjectType} |
@@ -55,3 +55,3 @@ import { log } from "@lbu/insight"; | ||
name: `ref${upperCaseFirst(ref.name)}`, | ||
references: { | ||
reference: { | ||
...ref, | ||
@@ -66,3 +66,3 @@ uniqueName: upperCaseFirst(ref.group) + upperCaseFirst(ref.name), | ||
for (const ref of context.openAPIReferences) { | ||
const refValue = resolveReference(context, ref); | ||
const refValue = resolveReferenceAndConvert(context, ref); | ||
if (refValue) { | ||
@@ -91,7 +91,7 @@ context.result[context.defaultGroup][refValue.name] = refValue; | ||
type: "route", | ||
group: lowerCaseFirst(item.tags?.[0] ?? context.defaultGroup), | ||
name: transformRouteName(context, item.operationId), | ||
group: transformRouteName(item.tags?.[0] ?? context.defaultGroup), | ||
name: transformRouteName(item.operationId ?? upperCaseFirst(method) + path), | ||
docString: item.description ?? "", | ||
method: method.toUpperCase(), | ||
path: transformRoutePath(context, path), | ||
path: transformRoutePath(path), | ||
tags: [], | ||
@@ -135,9 +135,9 @@ }; | ||
* | ||
* @param context | ||
* @param operationId | ||
* @returns {string} | ||
*/ | ||
function transformRouteName(context, operationId) { | ||
function transformRouteName(operationId) { | ||
return operationId | ||
.split(/(?:[_\s-])/g) | ||
.replace(/[{}]/g, "/") | ||
.split(/(?:[_\s-/])/g) | ||
.map((it, idx) => (idx === 0 ? lowerCaseFirst(it) : upperCaseFirst(it))) | ||
@@ -150,7 +150,6 @@ .join(""); | ||
* | ||
* @param context | ||
* @param path | ||
* @returns {string} | ||
*/ | ||
function transformRoutePath(context, path) { | ||
function transformRoutePath(path) { | ||
return `${path | ||
@@ -179,3 +178,9 @@ .split("/") | ||
for (const input of inputList) { | ||
for (let input of inputList) { | ||
// resolve param/query references | ||
if (input.$ref) { | ||
input = resolveReference(context, input.$ref); | ||
} | ||
// ensure filter type align | ||
if (input.in !== filter) { | ||
@@ -253,3 +258,3 @@ continue; | ||
*/ | ||
function resolveReference(context, refString) { | ||
function resolveReferenceAndConvert(context, refString) { | ||
const path = refString.split("/").slice(1); | ||
@@ -259,3 +264,2 @@ const name = path[path.length - 1]; | ||
let currentItem = context.data; | ||
while (path.length > 0) { | ||
@@ -277,2 +281,19 @@ currentItem = currentItem?.[path.shift()]; | ||
/** | ||
* Naively try to find the referenced item in the OpenAPI doc | ||
* | ||
* @param context | ||
* @param refString | ||
*/ | ||
function resolveReference(context, refString) { | ||
const path = refString.split("/").slice(1); | ||
let currentItem = context.data; | ||
while (path.length > 0) { | ||
currentItem = currentItem?.[path.shift()]; | ||
} | ||
return currentItem; | ||
} | ||
/** | ||
* Make an effort ot convert to native lbu types. | ||
@@ -279,0 +300,0 @@ * LBU and OpenAPI offer flexibility in different places: |
@@ -100,3 +100,3 @@ import { readFileSync } from "fs"; | ||
dir, | ||
async (file) => { | ||
(file) => { | ||
if (!file.endsWith(ext)) { | ||
@@ -103,0 +103,0 @@ return; |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
274028
8554
+ Added@lbu/cli@0.0.79(transitive)
+ Added@lbu/insight@0.0.79(transitive)
+ Added@lbu/stdlib@0.0.79(transitive)
- Removed@lbu/cli@0.0.78(transitive)
- Removed@lbu/insight@0.0.78(transitive)
- Removed@lbu/stdlib@0.0.78(transitive)
Updated@lbu/cli@0.0.79
Updated@lbu/insight@0.0.79
Updated@lbu/stdlib@0.0.79