@compas/code-gen
Advanced tools
Comparing version 0.0.122 to 0.0.123
@@ -227,3 +227,4 @@ import { Logger } from "@compas/insight"; | ||
/** | ||
* Formidable file object, Blob or Stream depending on the usecase | ||
* Formidable file object, Blob or Stream depending on the use case. | ||
* | ||
* @param name | ||
@@ -542,2 +543,10 @@ */ | ||
defaultToNow(): this; | ||
min(value: number | string | Date): this; | ||
max(value: number | string | Date): this; | ||
inTheFuture(): this; | ||
inThePast(): this; | ||
} | ||
@@ -544,0 +553,0 @@ |
{ | ||
"name": "@compas/code-gen", | ||
"version": "0.0.122", | ||
"version": "0.0.123", | ||
"description": "Generate various boring parts of your server", | ||
@@ -18,5 +18,5 @@ "main": "./index.js", | ||
"dependencies": { | ||
"@compas/cli": "0.0.122", | ||
"@compas/insight": "0.0.122", | ||
"@compas/stdlib": "0.0.122" | ||
"@compas/cli": "0.0.123", | ||
"@compas/insight": "0.0.123", | ||
"@compas/stdlib": "0.0.123" | ||
}, | ||
@@ -44,3 +44,3 @@ "maintainers": [ | ||
}, | ||
"gitHead": "22f4e7c17a5d515aaab9b844cd7dc467aab21987" | ||
"gitHead": "d69ba8193d772c412952369512c78de9a81051ef" | ||
} |
@@ -29,3 +29,3 @@ # @compas/code-gen | ||
My work involved doing many small projects. I had a hard time backporting | ||
My work involved doing many small projects. I had a hard time back-porting | ||
incremental fixes to existing projects. To facilitate my needs more and to stop | ||
@@ -32,0 +32,0 @@ copying and pasting things around, this project was born. |
import { newLogger, printProcessMemoryUsage } from "@compas/insight"; | ||
import { AppError, isNil, merge } from "@compas/stdlib"; | ||
import { isNil, merge } from "@compas/stdlib"; | ||
import { ReferenceType } from "./builders/ReferenceType.js"; | ||
@@ -167,3 +167,3 @@ import { buildOrInfer } from "./builders/utils.js"; | ||
if (errors) { | ||
this.logger.error(AppError.format(errors[0])); | ||
this.logger.error(errors[0]); | ||
process.exit(1); | ||
@@ -190,3 +190,3 @@ } | ||
if (errors) { | ||
this.logger.error(AppError.format(errors[0])); | ||
this.logger.error(errors[0]); | ||
process.exit(1); | ||
@@ -308,3 +308,3 @@ } | ||
if (errors) { | ||
this.logger.error(AppError.format(errors[0])); | ||
this.logger.error(errors[0]); | ||
process.exit(1); | ||
@@ -311,0 +311,0 @@ } |
@@ -24,2 +24,80 @@ import { TypeBuilder } from "./TypeBuilder.js"; | ||
} | ||
/** | ||
* Set the minimum date value | ||
* | ||
* @param {number|string|Date} value | ||
* @returns {DateType} | ||
*/ | ||
min(value) { | ||
if (typeof value === "string" || typeof value === "number") { | ||
value = new Date(value); | ||
} | ||
if (!(value instanceof Date) || isNaN(value.getTime())) { | ||
throw new TypeError( | ||
`Expected 'Date', 'string' or 'number', got '${value}'`, | ||
); | ||
} | ||
this.data.validator.min = value; | ||
return this; | ||
} | ||
/** | ||
* Set the max date value | ||
* | ||
* @param {number|string|Date} value | ||
* @returns {DateType} | ||
*/ | ||
max(value) { | ||
if (typeof value === "string" || typeof value === "number") { | ||
value = new Date(value); | ||
} | ||
if (!(value instanceof Date) || isNaN(value.getTime())) { | ||
throw new TypeError( | ||
`Expected 'Date', 'string' or 'number', got '${value}'`, | ||
); | ||
} | ||
this.data.validator.max = value; | ||
return this; | ||
} | ||
/** | ||
* Only allow dates in the future | ||
* | ||
* @returns {DateType} | ||
*/ | ||
inTheFuture() { | ||
this.data.validator.inFuture = true; | ||
if (this.data.validator.inPast) { | ||
throw new TypeError( | ||
"Supports either 'inTheFuture' or 'inThePast', but not both.", | ||
); | ||
} | ||
return this; | ||
} | ||
/** | ||
* Only allow dates in the past | ||
* | ||
* @returns {DateType} | ||
*/ | ||
inThePast() { | ||
this.data.validator.inPast = true; | ||
if (this.data.validator.inFuture) { | ||
throw new TypeError( | ||
"Supports either 'inTheFuture' or 'inThePast', but not both.", | ||
); | ||
} | ||
return this; | ||
} | ||
} |
@@ -43,3 +43,3 @@ import { AppError, isNil, isPlainObject } from "@compas/stdlib"; | ||
/** | ||
* Add item to correct group and nmame | ||
* Add item to correct group and add uniqueName | ||
* | ||
@@ -46,0 +46,0 @@ * @param {CodeGenStructure} dataStructure |
@@ -31,3 +31,3 @@ // Generated by @compas/code-gen | ||
/** | ||
* @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": "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, "min"?: undefined|Date, "max"?: undefined|Date, "inFuture"?: undefined|boolean, "inPast"?: undefined|boolean, }, }} CodeGenDateType | ||
*/ | ||
@@ -44,3 +44,3 @@ /** | ||
/** | ||
* @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, })[], }, "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": {"allowNull": boolean, "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, })[], }, "partial"?: undefined|{"insertType": string, "updateType": string, "fields": ({"key": string, "defaultValue"?: undefined|string, "isJsonb": boolean, })[], }, }} CodeGenObjectType | ||
*/ | ||
@@ -47,0 +47,0 @@ /** |
@@ -292,3 +292,3 @@ import { isNil } from "@compas/stdlib"; | ||
* - Nested joins | ||
* - Query traversal 'via'-quries | ||
* - Query traversal 'via'-queries | ||
* - Self referencing tables, by generating the same function with a different | ||
@@ -295,0 +295,0 @@ * shortName |
@@ -624,2 +624,6 @@ import { isNil } from "@compas/stdlib"; | ||
return js` | ||
if (typeof value !== "string" && typeof value !== "number" && !(value instanceof Date)) { | ||
${buildError("invalid", "{ propertyPath }")} | ||
} | ||
if (typeof value === "string") { | ||
@@ -644,12 +648,57 @@ ${generateAnonymousValidatorCall( | ||
} | ||
try { | ||
const date = new Date(value); | ||
if (!isNaN(date.getTime())) { | ||
return date; | ||
} | ||
} catch { | ||
const date = new Date(value); | ||
if (isNaN(date.getTime())) { | ||
${buildError("invalid", "{ propertyPath }")} | ||
} | ||
${buildError("invalid", "{ propertyPath }")} | ||
${() => { | ||
if (!isNil(type.validator.min)) { | ||
const time = new Date(type.validator.min).getTime(); | ||
return js` | ||
// ${type.validator.min} | ||
if (date.getTime() < ${time}) { | ||
const min = "${type.validator.min}"; | ||
${buildError("dateMin", "{ propertyPath, min }")} | ||
} | ||
`; | ||
} | ||
}} | ||
${() => { | ||
if (!isNil(type.validator.max)) { | ||
const time = new Date(type.validator.max).getTime(); | ||
return js` | ||
// ${type.validator.max} | ||
if (date.getTime() > ${time}) { | ||
const max = "${type.validator.max}"; | ||
${buildError("dateMax", "{ propertyPath, max }")} | ||
} | ||
`; | ||
} | ||
}} | ||
${() => { | ||
if (type.validator.inFuture === true) { | ||
return js` | ||
if (date.getTime() < Date.now()) { | ||
${buildError("future", "{ propertyPath }")} | ||
} | ||
`; | ||
} | ||
}} | ||
${() => { | ||
if (type.validator.inPast === true) { | ||
return js` | ||
if (date.getTime() > Date.now()) { | ||
${buildError("past", "{ propertyPath }")} | ||
} | ||
`; | ||
} | ||
}} | ||
return date; | ||
`; | ||
@@ -656,0 +705,0 @@ } |
Sorry, the diff of this file is too big to display
400854
12854
+ Added@compas/cli@0.0.123(transitive)
+ Added@compas/insight@0.0.123(transitive)
+ Added@compas/stdlib@0.0.123(transitive)
- Removed@compas/cli@0.0.122(transitive)
- Removed@compas/insight@0.0.122(transitive)
- Removed@compas/stdlib@0.0.122(transitive)
Updated@compas/cli@0.0.123
Updated@compas/insight@0.0.123
Updated@compas/stdlib@0.0.123