@compas/code-gen
Advanced tools
Comparing version 0.0.116 to 0.0.117
{ | ||
"name": "@compas/code-gen", | ||
"version": "0.0.116", | ||
"version": "0.0.117", | ||
"description": "Generate various boring parts of your server", | ||
@@ -18,5 +18,5 @@ "main": "./index.js", | ||
"dependencies": { | ||
"@compas/cli": "0.0.116", | ||
"@compas/insight": "0.0.116", | ||
"@compas/stdlib": "0.0.116" | ||
"@compas/cli": "0.0.117", | ||
"@compas/insight": "0.0.117", | ||
"@compas/stdlib": "0.0.117" | ||
}, | ||
@@ -44,3 +44,3 @@ "maintainers": [ | ||
}, | ||
"gitHead": "240f978002efe57c446650a5de9213961e2c06cb" | ||
"gitHead": "c24054692e907354ddaa42afc6f472599c0b7c4d" | ||
} |
@@ -74,3 +74,4 @@ import { newLogger, printProcessMemoryUsage } from "@compas/insight"; | ||
* Create a new App. | ||
* @param {AppOpts=} options | ||
* | ||
* @param {AppOpts} [options={}] | ||
*/ | ||
@@ -77,0 +78,0 @@ constructor({ verbose } = {}) { |
@@ -30,3 +30,3 @@ import { TypeBuilder } from "./TypeBuilder.js"; | ||
* @param {string} value | ||
* @param {{ javaScript?: string, typeScript?: string }=} importValue | ||
* @param {{ javaScript?: string, typeScript?: string }} [importValue={}] | ||
* @returns {AnyType} | ||
@@ -46,3 +46,3 @@ */ | ||
* @param {string} value | ||
* @param {{ javaScript?: string, typeScript?: string }=} importValue | ||
* @param {{ javaScript?: string, typeScript?: string }} [importValue={}] | ||
* @returns {AnyType} | ||
@@ -49,0 +49,0 @@ */ |
@@ -31,2 +31,3 @@ import { isPlainObject } from "@compas/stdlib"; | ||
* - object | ||
* | ||
* @param {TypeBuilderLike|undefined} value | ||
@@ -33,0 +34,0 @@ * @returns {*} |
@@ -16,3 +16,3 @@ // Generated by @compas/code-gen | ||
/** | ||
* @typedef {{"type": "anyOf", "docString": string, "isOptional": boolean, "defaultValue"?: string|boolean|number, "uniqueName"?: string, "group"?: string, "name"?: string, "sql"?: {"primary": boolean, "searchable": boolean, }, "validator": {}, "values": (CodeGenType)[], }} CodeGenAnyOfType | ||
* @typedef {{"type": "anyOf", "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": {}, "values": (CodeGenType)[], }} CodeGenAnyOfType | ||
*/ | ||
@@ -23,39 +23,39 @@ /** | ||
/** | ||
* @typedef {{"type": "any", "docString": string, "isOptional": boolean, "defaultValue"?: string|boolean|number, "uniqueName"?: string, "group"?: string, "name"?: string, "sql"?: {"primary": boolean, "searchable": boolean, }, "validator": {"allowNull": boolean, }, "rawValue"?: string, "rawValueImport": {"javaScript"?: string, "typeScript"?: string, }, "rawValidator"?: string, "rawValidatorImport": {"javaScript"?: string, "typeScript"?: string, }, }} CodeGenAnyType | ||
* @typedef {{"type": "any", "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": {"allowNull": boolean, }, "rawValue"?: undefined|string, "rawValueImport": {"javaScript"?: undefined|string, "typeScript"?: undefined|string, }, "rawValidator"?: undefined|string, "rawValidatorImport": {"javaScript"?: undefined|string, "typeScript"?: undefined|string, }, }} CodeGenAnyType | ||
*/ | ||
/** | ||
* @typedef {{"type": "array", "docString": string, "isOptional": boolean, "defaultValue"?: string|boolean|number, "uniqueName"?: string, "group"?: string, "name"?: string, "sql"?: {"primary": boolean, "searchable": boolean, }, "validator": {"convert": boolean, "min"?: number, "max"?: number, }, "values": CodeGenType, }} CodeGenArrayType | ||
* @typedef {{"type": "array", "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": {"convert": boolean, "min"?: undefined|number, "max"?: undefined|number, }, "values": CodeGenType, }} CodeGenArrayType | ||
*/ | ||
/** | ||
* @typedef {{"type": "boolean", "docString": string, "isOptional": boolean, "defaultValue"?: string|boolean|number, "uniqueName"?: string, "group"?: string, "name"?: string, "sql"?: {"primary": boolean, "searchable": boolean, }, "validator": {"convert": boolean, "allowNull": boolean, }, "oneOf"?: boolean, }} CodeGenBooleanType | ||
* @typedef {{"type": "boolean", "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": {"convert": boolean, "allowNull": boolean, }, "oneOf"?: undefined|boolean, }} CodeGenBooleanType | ||
*/ | ||
/** | ||
* @typedef {{"type": "date", "docString": string, "isOptional": boolean, "defaultValue"?: string|boolean|number, "uniqueName"?: string, "group"?: string, "name"?: string, "sql"?: {"primary": boolean, "searchable": boolean, }, "validator": {"allowNull": boolean, }, }} CodeGenDateType | ||
* @typedef {{"type": "date", "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": {"allowNull": boolean, }, }} CodeGenDateType | ||
*/ | ||
/** | ||
* @typedef {{"type": "file", "docString": string, "isOptional": boolean, "defaultValue"?: string|boolean|number, "uniqueName"?: string, "group"?: string, "name"?: string, "sql"?: {"primary": boolean, "searchable": boolean, }, "validator": {}, }} CodeGenFileType | ||
* @typedef {{"type": "file", "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": {}, }} CodeGenFileType | ||
*/ | ||
/** | ||
* @typedef {{"type": "generic", "docString": string, "isOptional": boolean, "defaultValue"?: string|boolean|number, "uniqueName"?: string, "group"?: string, "name"?: string, "sql"?: {"primary": boolean, "searchable": boolean, }, "validator": {}, "keys": CodeGenType, "values": CodeGenType, }} CodeGenGenericType | ||
* @typedef {{"type": "generic", "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": {}, "keys": CodeGenType, "values": CodeGenType, }} CodeGenGenericType | ||
*/ | ||
/** | ||
* @typedef {{"type": "number", "docString": string, "isOptional": boolean, "defaultValue"?: string|boolean|number, "uniqueName"?: string, "group"?: string, "name"?: string, "sql"?: {"primary": boolean, "searchable": boolean, }, "validator": {"convert": boolean, "floatingPoint": boolean, "min"?: number, "max"?: number, "allowNull": boolean, }, "oneOf"?: (number)[], }} CodeGenNumberType | ||
* @typedef {{"type": "number", "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": {"convert": boolean, "floatingPoint": boolean, "min"?: undefined|number, "max"?: undefined|number, "allowNull": boolean, }, "oneOf"?: undefined|(number)[], }} CodeGenNumberType | ||
*/ | ||
/** | ||
* @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, }, "shortName"?: string, "keys": Object<string, CodeGenType>, "enableQueries": boolean, "queryOptions"?: {"withSoftDeletes": boolean, "withDates": boolean, "withPrimaryKey": boolean, "isView": boolean, }, "relations": (CodeGenRelationType)[], "where"?: {"type": string, "fields": ({"key": string, "name": string, "variant": "equal"|"notEqual"|"in"|"notIn"|"greaterThan"|"lowerThan"|"isNull"|"isNotNull"|"includeNotNull"|"like"|"iLike"|"notLike", })[], }, "partial"?: {"insertType": string, "updateType": string, "fields": ({"key": string, "defaultValue"?: 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", })[], }, "partial"?: undefined|{"insertType": string, "updateType": string, "fields": ({"key": string, "defaultValue"?: undefined|string, "isJsonb": boolean, })[], }, }} CodeGenObjectType | ||
*/ | ||
/** | ||
* @typedef {{"type": "relation", "subType": "manyToOne"|"oneToMany"|"oneToOne"|"oneToOneReverse", "reference": CodeGenReferenceType, "ownKey": string, "referencedKey"?: string, "isOptional": boolean, }} CodeGenRelationType | ||
* @typedef {{"type": "relation", "subType": "manyToOne"|"oneToMany"|"oneToOne"|"oneToOneReverse", "reference": CodeGenReferenceType, "ownKey": string, "referencedKey"?: undefined|string, "isOptional": boolean, }} CodeGenRelationType | ||
*/ | ||
/** | ||
* @typedef {{"type": "reference", "docString": string, "isOptional": boolean, "defaultValue"?: string|boolean|number, "uniqueName"?: string, "group"?: string, "name"?: string, "sql"?: {"primary": boolean, "searchable": boolean, }, "validator": {}, "reference": CodeGenType|{"uniqueName"?: string, "group"?: string, "name"?: string, }, }} CodeGenReferenceType | ||
* @typedef {{"type": "reference", "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": {}, "reference": CodeGenType|{"uniqueName"?: undefined|string, "group"?: undefined|string, "name"?: undefined|string, }, }} CodeGenReferenceType | ||
*/ | ||
/** | ||
* @typedef {{"type": "string", "docString": string, "isOptional": boolean, "defaultValue"?: string|boolean|number, "uniqueName"?: string, "group"?: string, "name"?: string, "sql"?: {"primary": boolean, "searchable": boolean, }, "validator": {"convert": boolean, "trim": boolean, "lowerCase": boolean, "upperCase": boolean, "min": number, "max"?: number, "pattern"?: string, "allowNull": boolean, }, "oneOf"?: (string)[], }} CodeGenStringType | ||
* @typedef {{"type": "string", "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": {"convert": boolean, "trim": boolean, "lowerCase": boolean, "upperCase": boolean, "min": number, "max"?: undefined|number, "pattern"?: undefined|string, "allowNull": boolean, }, "oneOf"?: undefined|(string)[], }} CodeGenStringType | ||
*/ | ||
/** | ||
* @typedef {{"type": "uuid", "docString": string, "isOptional": boolean, "defaultValue"?: string|boolean|number, "uniqueName"?: string, "group"?: string, "name"?: string, "sql"?: {"primary": boolean, "searchable": boolean, }, "validator": {"allowNull": boolean, }, }} CodeGenUuidType | ||
* @typedef {{"type": "uuid", "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": {"allowNull": boolean, }, }} CodeGenUuidType | ||
*/ | ||
/** | ||
* @typedef {{"type": "route", "docString": string, "isOptional": boolean, "defaultValue"?: string|boolean|number, "uniqueName"?: string, "group"?: string, "name"?: string, "sql"?: {"primary": boolean, "searchable": boolean, }, "validator": {}, "method": "GET"|"POST"|"PUT"|"DELETE"|"HEAD"|"PATCH", "idempotent": boolean, "path": string, "tags": (string)[], "query"?: CodeGenType, "params"?: CodeGenType, "body"?: CodeGenType, "files"?: CodeGenType, "response"?: CodeGenType, }} CodeGenRouteType | ||
* @typedef {{"type": "route", "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": {}, "method": "GET"|"POST"|"PUT"|"DELETE"|"HEAD"|"PATCH", "idempotent": boolean, "path": string, "tags": (string)[], "query"?: undefined|CodeGenType, "params"?: undefined|CodeGenType, "body"?: undefined|CodeGenType, "files"?: undefined|CodeGenType, "response"?: undefined|CodeGenType, }} CodeGenRouteType | ||
*/ | ||
@@ -78,3 +78,3 @@ /** | ||
/** | ||
* @typedef {{"isJSON"?: boolean, "nestedIsJSON"?: boolean, "useDefaults"?: boolean, "useTypescript"?: boolean, "isNode"?: boolean, "isBrowser"?: boolean, "suffix"?: string, "fileTypeIO"?: "input"|"outputRouter"|"outputClient", }} CodeGenTypeSettings | ||
* @typedef {{"isJSON"?: undefined|boolean, "nestedIsJSON"?: undefined|boolean, "useDefaults"?: undefined|boolean, "useTypescript"?: undefined|boolean, "isNode"?: undefined|boolean, "isBrowser"?: undefined|boolean, "suffix"?: undefined|string, "fileTypeIO"?: undefined|"input"|"outputRouter"|"outputClient", }} CodeGenTypeSettings | ||
*/ |
@@ -166,2 +166,3 @@ import { mkdirSync, readFileSync, writeFileSync } from "fs"; | ||
* Join all root exports in to a single index.js file | ||
* | ||
* @param {CodeGenContext} context | ||
@@ -186,2 +187,3 @@ */ | ||
* Use the fileHeader from options, and prefix all file contents with it | ||
* | ||
* @param {CodeGenContext} context | ||
@@ -203,2 +205,3 @@ */ | ||
* Write out all files | ||
* | ||
* @param {CodeGenContext} context | ||
@@ -221,2 +224,3 @@ */ | ||
* Check if we should generate ES Modules based on the package.json | ||
* | ||
* @param {Logger} logger | ||
@@ -223,0 +227,0 @@ * @returns {boolean} |
@@ -10,2 +10,3 @@ import { js } from "../tag/tag.js"; | ||
* Generate model files with query basic, partials and builder | ||
* | ||
* @param {CodeGenContext} context | ||
@@ -12,0 +13,0 @@ */ |
@@ -102,2 +102,3 @@ import { isNil } from "@compas/stdlib"; | ||
* Adds builder to reuse inserts | ||
* | ||
* @param {CodeGenContext} context | ||
@@ -154,2 +155,3 @@ * @param {CodeGenObjectType} type | ||
* Adds builder to reuse updates | ||
* | ||
* @param {CodeGenContext} context | ||
@@ -156,0 +158,0 @@ * @param {CodeGenObjectType} type |
@@ -196,3 +196,3 @@ import { upperCaseFirst } from "../../utils.js"; | ||
* @param {${type.partial.insertType}|(${type.partial.insertType}[])} insert | ||
* @param {{ withPrimaryKey: boolean }=} options | ||
* @param {{ withPrimaryKey: boolean }} [options={}] | ||
* @returns {Promise<${type.uniqueName}[]>} | ||
@@ -199,0 +199,0 @@ */ |
@@ -31,2 +31,3 @@ import { isNil } from "@compas/stdlib"; | ||
* Generate the necessary query builder types | ||
* | ||
* @param {CodeGenContext} context | ||
@@ -386,3 +387,9 @@ */ | ||
${Object.entries(otherSide.queryBuilder.relations).map( | ||
([key, { joinKey: otherJoinKey, subType: otherSubType }]) => { | ||
([ | ||
key, | ||
{ | ||
joinKey: otherJoinKey, | ||
relation: { subType: otherSubType }, | ||
}, | ||
]) => { | ||
const coalescedValue = | ||
@@ -394,3 +401,3 @@ otherSubType === "oneToMany" | ||
if (builder.${relationKey}.${key}) { | ||
joinedKeys.push("'" + (builder.${relationKey}.${key}?.as ?? "${key}") + "'", '${coalescedValue}'); | ||
joinedKeys.push("'" + (builder.${relationKey}.${key}?.as ?? "${key}") + "'", \`${coalescedValue}\`); | ||
} | ||
@@ -551,3 +558,3 @@ `; | ||
* @param {*[]} values | ||
* @param {${type.uniqueName}QueryBuilder=} builder | ||
* @param {${type.uniqueName}QueryBuilder} [builder={}] | ||
*/ | ||
@@ -554,0 +561,0 @@ export function transform${upperCaseFirst( |
@@ -85,2 +85,3 @@ import { js } from "../tag/index.js"; | ||
* A list of fields for the provided type, with dynamic tableName | ||
* | ||
* @property {CodeGenContext} context | ||
@@ -87,0 +88,0 @@ * @property {CodeGenObjectType} type |
@@ -33,2 +33,3 @@ import { isNil } from "@compas/stdlib"; | ||
* Generates the sql structure, this can be used to create migration files from | ||
* | ||
* @param {CodeGenContext} context | ||
@@ -35,0 +36,0 @@ */ |
@@ -6,2 +6,3 @@ import { isNil } from "@compas/stdlib"; | ||
* other queries | ||
* | ||
* @param {CodeGenContext} context | ||
@@ -97,2 +98,3 @@ */ | ||
* - createdAt, updatedAt, deletedAt | ||
* | ||
* @param {CodeGenObjectType} type | ||
@@ -164,2 +166,3 @@ * @returns {string[]} | ||
* Statically check if objects are correctly setup do have queries enabled. | ||
* | ||
* @param {CodeGenContext} context | ||
@@ -166,0 +169,0 @@ */ |
@@ -299,3 +299,3 @@ import { isNil } from "@compas/stdlib"; | ||
* @param {string} [tableName="${type.shortName}."] | ||
* @param {{ skipValidator?: boolean }=} options | ||
* @param {{ skipValidator?: boolean|undefined }} [options={}] | ||
* @returns {QueryPart} | ||
@@ -325,2 +325,3 @@ */ | ||
* Returns an object with only the searchable fields | ||
* | ||
* @param {CodeGenObjectType} type | ||
@@ -327,0 +328,0 @@ * @returns {Object<string, CodeGenType>} |
@@ -20,2 +20,3 @@ import { isNil } from "@compas/stdlib"; | ||
* Setup stores for memoized types, so we can reuse types if necessary | ||
* | ||
* @param {CodeGenContext} context | ||
@@ -51,2 +52,3 @@ */ | ||
* Use the memoized types and the provided settings to create a new type | ||
* | ||
* @param {CodeGenContext} context | ||
@@ -295,3 +297,3 @@ * @param {CodeGenType} type | ||
for (const key of Object.keys(type.keys)) { | ||
let right = generateTypeDefinition( | ||
const right = generateTypeDefinition( | ||
context, | ||
@@ -305,3 +307,2 @@ type.keys[key], | ||
separator = "?:"; | ||
right = right.substring(10); | ||
} | ||
@@ -308,0 +309,0 @@ |
@@ -15,2 +15,3 @@ import { isNil } from "@compas/stdlib"; | ||
* Clean template output by removing redundant new lines | ||
* | ||
* @param {string} str | ||
@@ -17,0 +18,0 @@ * @returns {string} |
@@ -257,3 +257,3 @@ import { isNil } from "@compas/stdlib"; | ||
* @param {string} prefix | ||
* @param {string=} parentType | ||
* @param {string} [parentType] | ||
* @returns {string} | ||
@@ -395,3 +395,3 @@ */ | ||
case "date": | ||
return anonymousValidatorDate(context, imports); | ||
return anonymousValidatorDate(context, imports, type); | ||
case "file": | ||
@@ -608,8 +608,11 @@ return anonymousValidatorFile(context); | ||
* @param {ImportCreator} imports | ||
* @param {CodeGenDateType} type | ||
*/ | ||
function anonymousValidatorDate(context, imports) { | ||
function anonymousValidatorDate(context, imports, type) { | ||
const stringType = { | ||
...TypeBuilder.getBaseData(), | ||
type: "string", | ||
isOptional: type.isOptional, | ||
validator: { | ||
allowNull: type.validator.allowNull, | ||
min: 24, | ||
@@ -634,2 +637,9 @@ max: 29, | ||
)} | ||
${ | ||
type.isOptional && type.defaultValue | ||
? `if (!value) { return ${type.defaultValue}; }` | ||
: "" | ||
} | ||
${type.isOptional ? `if (!value) { return value; }` : ""} | ||
} | ||
@@ -636,0 +646,0 @@ try { |
@@ -8,3 +8,3 @@ /** | ||
* @param {CodeGenType} type | ||
* @param {boolean=} includeSqlRelated | ||
* @param {boolean} [includeSqlRelated=false] | ||
* @returns {string} | ||
@@ -11,0 +11,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
384365
12302
+ Added@compas/cli@0.0.117(transitive)
+ Added@compas/insight@0.0.117(transitive)
+ Added@compas/stdlib@0.0.117(transitive)
+ Added@types/node@14.14.31(transitive)
+ Addedc8@7.6.0(transitive)
- Removed@compas/cli@0.0.116(transitive)
- Removed@compas/insight@0.0.116(transitive)
- Removed@compas/stdlib@0.0.116(transitive)
- Removed@types/node@14.14.27(transitive)
- Removedc8@7.5.0(transitive)
Updated@compas/cli@0.0.117
Updated@compas/insight@0.0.117
Updated@compas/stdlib@0.0.117