@lbu/code-gen
Advanced tools
Comparing version
{ | ||
"name": "@lbu/code-gen", | ||
"version": "0.0.87", | ||
"version": "0.0.88", | ||
"description": "Generate various boring parts of your server", | ||
@@ -19,5 +19,5 @@ "main": "./index.js", | ||
"dependencies": { | ||
"@lbu/cli": "0.0.87", | ||
"@lbu/insight": "0.0.87", | ||
"@lbu/stdlib": "0.0.87" | ||
"@lbu/cli": "0.0.88", | ||
"@lbu/insight": "0.0.88", | ||
"@lbu/stdlib": "0.0.88" | ||
}, | ||
@@ -45,3 +45,3 @@ "maintainers": [ | ||
}, | ||
"gitHead": "021df7496d95137e21351c5686674de431f7bb01" | ||
"gitHead": "f95775597389435d11e6ef713651a2d1d4816067" | ||
} |
@@ -29,3 +29,3 @@ import { js } from "../tag/index.js"; | ||
const file = js` | ||
import { query } from "@lbu/store"; | ||
import { query, isQueryObject } from "@lbu/store"; | ||
@@ -32,0 +32,0 @@ ${partials} |
@@ -83,8 +83,8 @@ import { upperCaseFirst } from "../../utils.js"; | ||
get${upperCaseFirst(relation.ownKey)}(where = {}) { | ||
return traverse${upperCaseFirst(otherSide.name)}(where, query\` | ||
AND ${otherSide.shortName}."${referencedKey}" = ANY( | ||
where.${referencedKey}In = query\` | ||
SELECT ${type.shortName}."${ownKey}" | ||
$\{q} | ||
) | ||
\`); | ||
FROM "${type.name}" ${type.shortName} | ||
WHERE $\{${type.name}Where(thisWhere)} | ||
\`; | ||
return traverse${upperCaseFirst(otherSide.name)}(where); | ||
}, | ||
@@ -113,15 +113,6 @@ `; | ||
/** | ||
* @param {${type.uniqueName}Where} [where={}] | ||
* @param {QueryPart|undefined} [queryPart] | ||
* @param {${type.uniqueName}Where} [thisWhere={}] | ||
* @returns {Traverse${upperCaseFirst(type.name)}} | ||
*/ | ||
export function traverse${upperCaseFirst( | ||
type.name, | ||
)}(where = {}, queryPart) { | ||
const q = query\` | ||
FROM "${type.name}" ${type.shortName} | ||
WHERE $\{${type.name}Where(where)} | ||
$\{queryPart} | ||
\`; | ||
export function traverse${upperCaseFirst(type.name)}(thisWhere = {}) { | ||
return { | ||
@@ -132,3 +123,4 @@ ${partials} | ||
SELECT $\{${type.name}Fields()} | ||
$\{q} | ||
FROM "${type.name}" ${type.shortName} | ||
WHERE $\{${type.name}Where(thisWhere)} | ||
ORDER BY $\{${type.name}OrderBy()} | ||
@@ -139,3 +131,4 @@ \`; | ||
SELECT $\{${type.name}Fields()} | ||
$\{q} | ||
FROM "${type.name}" ${type.shortName} | ||
WHERE $\{${type.name}Where(thisWhere)} | ||
ORDER BY $\{${type.name}OrderBy()} | ||
@@ -142,0 +135,0 @@ \`.exec(sql); |
@@ -142,10 +142,15 @@ import { isNil } from "@lbu/stdlib"; | ||
str += ` | ||
strings.push(\` AND $\{tableName}"${field.key}" = ANY(ARRAY[\`); | ||
for (let i = 0; i < where.${field.name}.length; ++i) { | ||
values.push(where.${field.name}[i]); | ||
if (i === where.${field.name}.length - 1) { | ||
strings.push("]::${fieldType}[])"); | ||
values.push(undefined); | ||
} else { | ||
strings.push(", "); | ||
if (isQueryObject(where.${field.name})) { | ||
strings.push(\` AND $\{tableName}"${field.key}" = ANY(\`, ")"); | ||
values.push(where.${field.name}, undefined); | ||
} else { | ||
strings.push(\` AND $\{tableName}"${field.key}" = ANY(ARRAY[\`); | ||
for (let i = 0; i < where.${field.name}.length; ++i) { | ||
values.push(where.${field.name}[i]); | ||
if (i === where.${field.name}.length - 1) { | ||
strings.push("]::${fieldType}[])"); | ||
values.push(undefined); | ||
} else { | ||
strings.push(", "); | ||
} | ||
} | ||
@@ -157,10 +162,15 @@ } | ||
str += ` | ||
strings.push(\` AND $\{tableName}"${field.key}" != ANY(ARRAY[\`); | ||
for (let i = 0; i < where.${field.name}.length; ++i) { | ||
values.push(where.${field.name}[i]); | ||
if (i === where.${field.name}.length - 1) { | ||
strings.push("]::${fieldType}[])"); | ||
values.push(undefined); | ||
} else { | ||
strings.push(", "); | ||
if (isQueryObject(where.${field.name})) { | ||
strings.push(\` AND $\{tableName}"${field.key}" != ANY(\`, ")"); | ||
values.push(where.${field.name}, undefined); | ||
} else { | ||
strings.push(\` AND $\{tableName}"${field.key}" != ANY(ARRAY[\`); | ||
for (let i = 0; i < where.${field.name}.length; ++i) { | ||
values.push(where.${field.name}[i]); | ||
if (i === where.${field.name}.length - 1) { | ||
strings.push("]::${fieldType}[])"); | ||
values.push(undefined); | ||
} else { | ||
strings.push(", "); | ||
} | ||
} | ||
@@ -167,0 +177,0 @@ } |
@@ -57,2 +57,14 @@ import { inspect } from "util"; | ||
/** | ||
* @param {ValidatorContext} context | ||
* @param {string} output | ||
* @returns {string} | ||
*/ | ||
function withTypescript(context, output) { | ||
if (context.context.options.useTypescript) { | ||
return output ?? ""; | ||
} | ||
return ""; | ||
} | ||
/** | ||
* | ||
@@ -112,4 +124,15 @@ * @param {ValidatorContext} context | ||
*/ | ||
export function ${it}(value, propertyPath = "$") { | ||
const errors = []; | ||
export function ${it}(value${withTypescript( | ||
context, | ||
": any", | ||
)}, propertyPath = "$") ${withTypescript( | ||
context, | ||
`: { data: ${getTypeNameForType( | ||
context.context, | ||
data[it], | ||
"", | ||
{}, | ||
)} } | { data: undefined, errors: any[] }`, | ||
)} { | ||
const errors${withTypescript(context, ": any[]")} = []; | ||
const data = ${mapping[it]}(value, propertyPath, errors); | ||
@@ -151,3 +174,7 @@ | ||
*/ | ||
export function isNil(value) { | ||
export function isNil(value | ||
${withTypescript(context, ": any")} | ||
) | ||
{ | ||
return value === undefined || value === null; | ||
@@ -157,15 +184,21 @@ } | ||
/** | ||
* @name {ValidationErrorFn} | ||
* This function should not throw as the corresponding validator will do that | ||
* @callback ValidationErrorFn | ||
* @param {string} key | ||
* @param {Object} info | ||
* @typedef {function(string,any): Error} | ||
*/ | ||
${withTypescript( | ||
context, | ||
"type ValidationErrorFn = (key: string, info: any) => any", | ||
)} | ||
/** | ||
* @type {ValidationErrorFn} | ||
*/ | ||
let errorFn = (key, info) => { | ||
/** @type {ValidationErrorFn} */ | ||
let errorFn = ( | ||
key${withTypescript(context, ": string")}, | ||
info${withTypescript(context, ": any")} | ||
) => { | ||
const err | ||
${context.context.options.useTypescript ? ": any" : ""} = | ||
new Error(\`ValidationError: $\{key}\`); | ||
${withTypescript( | ||
context, | ||
": any", | ||
)} = new Error(\`ValidationError: $\{key}\`); | ||
err.key = key; | ||
@@ -181,3 +214,10 @@ err.info = info; | ||
*/ | ||
export function buildError(type, key, info) { | ||
export function buildError( | ||
type${withTypescript(context, ": string")}, | ||
key${withTypescript(context, ": string")}, | ||
info | ||
${withTypescript(context, ": any")} | ||
) | ||
{ | ||
return errorFn(\`validator.$\{type}.$\{key}\`, info); | ||
@@ -190,3 +230,6 @@ } | ||
*/ | ||
export function validatorSetError(fn) { | ||
export function validatorSetError(fn${withTypescript( | ||
context, | ||
": ValidationErrorFn", | ||
)}) { | ||
errorFn = fn; | ||
@@ -250,6 +293,18 @@ } | ||
*/ | ||
export function anonymousValidator${idx}(value, | ||
propertyPath, | ||
errors = [], | ||
parentType = "${type.type}", | ||
export function anonymousValidator${idx}(value${withTypescript( | ||
context, | ||
": any", | ||
)}, | ||
propertyPath${withTypescript( | ||
context, | ||
": string", | ||
)}, | ||
errors${withTypescript( | ||
context, | ||
": any[]", | ||
)} = [], | ||
parentType${withTypescript( | ||
context, | ||
": string", | ||
)} = "${type.type}", | ||
) { | ||
@@ -256,0 +311,0 @@ if (isNil(value)) { |
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
320023
0.73%10552
0.5%+ Added
+ Added
+ Added
+ Added
+ Added
- Removed
- Removed
- Removed
- Removed
- Removed
Updated
Updated
Updated