@compas/code-gen
Advanced tools
Comparing version 0.0.118 to 0.0.119
{ | ||
"name": "@compas/code-gen", | ||
"version": "0.0.118", | ||
"version": "0.0.119", | ||
"description": "Generate various boring parts of your server", | ||
@@ -18,5 +18,5 @@ "main": "./index.js", | ||
"dependencies": { | ||
"@compas/cli": "0.0.118", | ||
"@compas/insight": "0.0.118", | ||
"@compas/stdlib": "0.0.118" | ||
"@compas/cli": "0.0.119", | ||
"@compas/insight": "0.0.119", | ||
"@compas/stdlib": "0.0.119" | ||
}, | ||
@@ -44,3 +44,3 @@ "maintainers": [ | ||
}, | ||
"gitHead": "1e74482093e2001de5f53adae6850a933399cd36" | ||
"gitHead": "84e343f51fcddbb583e8fbd0fea576e8bc55ffb2" | ||
} |
@@ -1,2 +0,1 @@ | ||
import { isNil } from "@compas/stdlib"; | ||
import { stringifyType } from "../stringify.js"; | ||
@@ -35,2 +34,4 @@ import { TypeBuilder } from "./TypeBuilder.js"; | ||
}; | ||
this.internalValues = []; | ||
} | ||
@@ -43,6 +44,2 @@ | ||
values(...items) { | ||
if (isNil(this.internalValues)) { | ||
this.internalValues = []; | ||
} | ||
this.internalValues.push(...items); | ||
@@ -49,0 +46,0 @@ |
@@ -43,3 +43,3 @@ // Generated by @compas/code-gen | ||
/** | ||
* @typedef {{"type": "object", "docString": string, "isOptional": boolean, "defaultValue"?: undefined|string|boolean|number, "uniqueName"?: undefined|string, "group"?: undefined|string, "name"?: undefined|string, "sql"?: undefined|{"primary": boolean, "searchable": boolean, }, "validator": {"strict": boolean, }, "shortName"?: undefined|string, "keys": Object<string, CodeGenType>, "enableQueries": boolean, "queryOptions"?: undefined|{"withSoftDeletes": boolean, "withDates": boolean, "withPrimaryKey": boolean, "isView": boolean, }, "relations": (CodeGenRelationType)[], "where"?: undefined|{"type": string, "fields": ({"key": string, "name": string, "variant": "equal"|"notEqual"|"in"|"notIn"|"greaterThan"|"lowerThan"|"isNull"|"isNotNull"|"includeNotNull"|"like"|"iLike"|"notLike", })[], }, "partial"?: undefined|{"insertType": string, "updateType": string, "fields": ({"key": string, "defaultValue"?: undefined|string, "isJsonb": boolean, })[], }, }} CodeGenObjectType | ||
* @typedef {{"type": "object", "docString": string, "isOptional": boolean, "defaultValue"?: undefined|string|boolean|number, "uniqueName"?: undefined|string, "group"?: undefined|string, "name"?: undefined|string, "sql"?: undefined|{"primary": boolean, "searchable": boolean, }, "validator": {"strict": boolean, }, "shortName"?: undefined|string, "keys": Object<string, CodeGenType>, "enableQueries": boolean, "queryOptions"?: undefined|{"withSoftDeletes": boolean, "withDates": boolean, "withPrimaryKey": boolean, "isView": boolean, }, "relations": (CodeGenRelationType)[], "where"?: undefined|{"type": string, "fields": ({"key": string, "name": string, "variant": "equal"|"notEqual"|"in"|"notIn"|"greaterThan"|"lowerThan"|"isNull"|"isNotNull"|"includeNotNull"|"like"|"iLike"|"notLike", })[], }, "orderBy"?: undefined|{"type": string, "specType": string, "fields": ({"key": string, "optional": boolean, "options": ("ASC"|"DESC"|"ASC NULLS FIRST"|"ASC NULLS LAST"|"DESC NULLS FIRST"|"DESC NULLS LAST")[], })[], }, "partial"?: undefined|{"insertType": string, "updateType": string, "fields": ({"key": string, "defaultValue"?: undefined|string, "isJsonb": boolean, })[], }, }} CodeGenObjectType | ||
*/ | ||
@@ -46,0 +46,0 @@ /** |
@@ -12,2 +12,3 @@ import { mkdirSync, readFileSync, writeFileSync } from "fs"; | ||
import { generateModelFiles } from "./sql/models.js"; | ||
import { createOrderByTypes } from "./sql/order-by-type.js"; | ||
import { createPartialTypes } from "./sql/partial-type.js"; | ||
@@ -116,2 +117,3 @@ import { createQueryBuilderTypes } from "./sql/query-builder.js"; | ||
createWhereTypes(context); | ||
createOrderByTypes(context); | ||
createPartialTypes(context); | ||
@@ -118,0 +120,0 @@ createQueryBuilderTypes(context); |
@@ -13,3 +13,3 @@ import { isNil } from "@compas/stdlib"; | ||
/** | ||
* Creates a where type and assigns in to the object type | ||
* Creates the partial types for inserts and updates and assigns in to the object type | ||
* | ||
@@ -16,0 +16,0 @@ * @param {CodeGenContext} context |
@@ -55,2 +55,7 @@ import { isNil } from "@compas/stdlib"; | ||
where: T.reference(type.group, `${type.name}Where`).optional(), | ||
orderBy: T.reference(type.group, `${type.name}OrderBy`).optional(), | ||
orderBySpec: T.reference( | ||
type.group, | ||
`${type.name}OrderBySpec`, | ||
).optional(), | ||
as: T.string().optional(), | ||
@@ -81,2 +86,7 @@ limit: T.number().optional(), | ||
context.structure[type.group][`${type.name}Where`]; | ||
queryBuilderType.keys.orderBy.reference = | ||
context.structure[type.group][`${type.name}OrderBy`]; | ||
queryBuilderType.keys.orderBySpec.reference = | ||
context.structure[type.group][`${type.name}OrderBySpec`]; | ||
} | ||
@@ -250,3 +260,3 @@ | ||
$\{internalQuery${upperCaseFirst(type.name)}(builder)} | ||
ORDER BY $\{${type.name}OrderBy()} | ||
ORDER BY $\{${type.name}OrderBy(builder.orderBy, builder.orderBySpec)} | ||
\`; | ||
@@ -374,3 +384,3 @@ | ||
if (relation.subType === "oneToMany") { | ||
select = `array_remove(array_agg(${selectValue} ORDER BY $\{${otherSide.name}OrderBy("${otherShortName}.")}), NULL) as "result"`; | ||
select = `array_remove(array_agg(${selectValue} ORDER BY $\{${otherSide.name}OrderBy(builder.${relationKey}.orderBy, builder.${relationKey}.orderBySpec, "${otherShortName}.")}), NULL) as "result"`; | ||
groupBy = `GROUP BY ${shortName}."${typePrimaryKey}"`; | ||
@@ -380,3 +390,3 @@ orderBy = `ORDER BY ${shortName}."${typePrimaryKey}"`; | ||
select = `${selectValue} as "result"`; | ||
orderBy = `ORDER BY $\{${otherSide.name}OrderBy("${otherShortName}.")}`; | ||
orderBy = `ORDER BY $\{${otherSide.name}OrderBy(builder.${relationKey}.orderBy, builder.${relationKey}.orderBySpec, "${otherShortName}.")}`; | ||
} | ||
@@ -383,0 +393,0 @@ |
import { js } from "../tag/index.js"; | ||
import { getOrderByPartial } from "./order-by-type.js"; | ||
import { getInsertPartial, getUpdatePartial } from "./partial-type.js"; | ||
@@ -25,3 +26,2 @@ import { getPrimaryKeyWithType, getSortedKeysForType } from "./utils.js"; | ||
src.push(getFieldsPartial(context, type)); | ||
src.push(getOrderPartial(context, type)); | ||
@@ -32,3 +32,13 @@ imports.destructureImport( | ||
); | ||
imports.destructureImport( | ||
`validate${type.uniqueName}OrderBy`, | ||
`../validators${context.importExtension}`, | ||
); | ||
imports.destructureImport( | ||
`validate${type.uniqueName}OrderBySpec`, | ||
`../validators${context.importExtension}`, | ||
); | ||
src.push(getWherePartial(context, type)); | ||
src.push(getOrderByPartial(context, type)); | ||
@@ -126,50 +136,1 @@ if (!type.queryOptions.isView) { | ||
} | ||
/** | ||
* A default ordering partial | ||
* Working correctly, with or without dates | ||
* | ||
* @property {CodeGenContext} context | ||
* @property {CodeGenObjectType} type | ||
* @returns {string} | ||
*/ | ||
export function getOrderPartial(context, type) { | ||
const { key: primaryKey } = getPrimaryKeyWithType(type); | ||
if (type.queryOptions.withSoftDeletes || type.queryOptions.withDates) { | ||
return js` | ||
/** | ||
* Get 'ORDER BY ' for ${type.name} | ||
* | ||
* @param {string} [tableName="${type.shortName}."] | ||
* @returns {QueryPart} | ||
*/ | ||
export function ${type.name}OrderBy(tableName = "${type.shortName}.") { | ||
if (tableName.length > 0 && !tableName.endsWith(".")) { | ||
tableName = \`$\{tableName}.\`; | ||
} | ||
const strings = [ \`$\{tableName}"createdAt", $\{tableName}"updatedAt", $\{tableName}"${primaryKey}" \` ]; | ||
return query(strings); | ||
} | ||
`; | ||
} | ||
return js` | ||
/** | ||
* Get 'ORDER BY ' for ${type.name} | ||
* | ||
* @param {string} [tableName="${type.shortName}."] | ||
* @returns {QueryPart} | ||
*/ | ||
export function ${type.name}OrderBy(tableName = "${type.shortName}.") { | ||
if (tableName.length > 0 && !tableName.endsWith(".")) { | ||
tableName = \`$\{tableName}.\`; | ||
} | ||
const strings = [ \`$\{tableName}"id" \` ]; | ||
return query(strings); | ||
} | ||
`; | ||
} |
@@ -212,13 +212,12 @@ import { isNil } from "@compas/stdlib"; | ||
values.push(where.${field.name}, undefined); | ||
} else if (Array.isArray(where.${field.name}) && where.${field.name}.length > 0) { | ||
} else if (Array.isArray(where.${field.name})) { | ||
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 { | ||
if (i !== where.${field.name}.length - 1) { | ||
strings.push(", "); | ||
} | ||
} | ||
strings.push("]::${fieldType}[])"); | ||
values.push(undefined); | ||
} | ||
@@ -232,13 +231,12 @@ `; | ||
values.push(where.${field.name}, undefined); | ||
} else if (Array.isArray(where.${field.name}) && where.${field.name}.length > 0) { | ||
} else if (Array.isArray(where.${field.name})) { | ||
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 { | ||
if (i !== where.${field.name}.length - 1) { | ||
strings.push(", "); | ||
} | ||
} | ||
strings.push("]::${fieldType}[])"); | ||
values.push(undefined); | ||
} | ||
@@ -245,0 +243,0 @@ `; |
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
394676
68
12621
+ Added@compas/cli@0.0.119(transitive)
+ Added@compas/insight@0.0.119(transitive)
+ Added@compas/stdlib@0.0.119(transitive)
- Removed@compas/cli@0.0.118(transitive)
- Removed@compas/insight@0.0.118(transitive)
- Removed@compas/stdlib@0.0.118(transitive)
Updated@compas/cli@0.0.119
Updated@compas/insight@0.0.119
Updated@compas/stdlib@0.0.119