Skip to content

Commit

Permalink
feat: implements object type in hapify (#316)
Browse files Browse the repository at this point in the history
* feat: implements object type in hapify

* fix: cleaning code

* fix: remove unnecessary line

Co-authored-by: Killian Challeau <killian@tractr.net>
  • Loading branch information
kilalo and Killian Challeau committed Dec 2, 2021
1 parent f6e6ac8 commit 7dfa584
Show file tree
Hide file tree
Showing 13 changed files with 175 additions and 10 deletions.
12 changes: 11 additions & 1 deletion hapify-models.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
"backRelation": "messages"
},
"name": "questions",
"properties": ["multiple"],
"properties": ["multiple", "searchable", "sortable"],
"subtype": "manyMany",
"type": "entity",
"value": "6894a782-2e83-cd6a-52cc-f62967165636"
Expand Down Expand Up @@ -271,11 +271,21 @@
"subtype": "password",
"type": "string"
},
{
"name": "listObject",
"properties": ["multiple", "searchable", "sortable"],
"type": "object"
},
{
"name": "roles",
"properties": ["multiple", "searchable", "sortable"],
"type": "enum",
"value": ["admin", "user", "guest"]
},
{
"name": "Object",
"properties": [],
"type": "object"
}
],
"id": "a7d0308a-49f0-3458-0975-1dce106136a1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import {
} from '../enums';
<<endif>>

<<if Fields object>>
import { Prisma } from '@prisma/client';
<<endif>>

import {

<<if Fields primary or entity or number or boolean>>
Expand All @@ -24,7 +28,7 @@ import {
internet,
<<endif>>

<<if Fields string and not (primary or email or password or url)>>
<<if Fields object or (string and not (primary or email or password or url))>>
lorem,
<<endif>>

Expand Down Expand Up @@ -80,6 +84,7 @@ function getBaseFaker(f) {
if (f.type === 'number' && (f.subtype === 'float' || f.subtype === 'latitude' || f.subtype === 'longitude')) return 'datatype.float()';
if (f.type === 'datetime') return 'date.past()';
if (f.type === 'entity') return 'datatype.uuid()';
if (f.type === 'object') return 'generateFakeObject()';
if (f.type === 'enum') return `random.arrayElement(Object.values(${root.names.pascal}${f.names.pascal}))`;
}
>>>
Expand All @@ -105,3 +110,14 @@ function getMockReturnType(f) {
}
>>>

<<if Fields object>>
function generateFakeObject() {
const countKeys = Math.floor(Math.random() * 10);
const obj : Prisma.JsonObject = {};
for (let i=0; i<=countKeys; i++) {
obj[`key${i}`] = lorem.word();
}
return obj;
}
<<endif>>

Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,23 @@ import {
ValidateNested,
<<<}>>>

<<if Fields object>>
IsObject,
<<< if (root.f.multiple.some(e => e.type ==='object')) { >>>
IsArray,
<<< } >>>
<<endif>>

<<if Fields password>>
Length,
<<endif>>

} from 'class-validator';

<<if Fields object>>
import { Prisma } from '@prisma/client';
<<endif>>

<<<for (const relation of getRelations()) {>>>
import {
<<relation pascal>>,
Expand Down Expand Up @@ -125,6 +136,13 @@ export class <<Model pascal>> implements Partial<Prisma<<Model pascal>>> {
@Type(() => <<=field.model.names.pascal>>)
<<endif>>

<<if field object>>
@IsObject(<<=listModifier(field)>>)
<<< if (field.multiple) { >>>
@IsArray()
<<< } >>>
<<endif>>

<<if field enum>>
@IsIn(Object.values(<<Model pascal>><<field pascal>>),<<=listModifier(field)>>)
<<endif>>
Expand Down Expand Up @@ -196,6 +214,7 @@ function typedProperty(f) {
else if (f.type === "enum") out += `${root.names.pascal}${f.names.pascal}`;
else if (f.type === "number") out += "number";
else if (f.type === "datetime") out += "Date";
else if (f.type === "object") out += "Prisma.JsonObject";
else if (f.type === "entity") {
out += f.model.names.pascal + (f.model.name !== root.name ? 'T' : '');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,11 @@ export class <<Model pascal>>RestDtoService {
const where = {
<<for Fields searchable and (multiple or entity) field>>
<<if field multiple and not entity>>
<<field camel>>: <<field camel>> ? { hasSome: <<field camel>> } : undefined,
<<if field object>>
<<field camel>>: <<field camel>> ? { equals: <<field camel>> } : undefined,
<<else>>
<<field camel>>: <<field camel>> ? { hasSome: <<field camel>> } : undefined,
<<endif>>
<<elseif field oneOne or oneMany>>
<<field camel>>: { id: <<field camel>> },
<<else>>
Expand Down Expand Up @@ -177,7 +181,11 @@ export class <<Model pascal>>RestDtoService {
const where: Prisma.<<Model pascal>>WhereInput = {
<<for Fields searchable and (multiple or entity) field>>
<<if field multiple and not entity>>
<<field camel>>: <<field camel>> ? { hasSome: <<field camel>> } : undefined,
<<if field object>>
<<field camel>>: <<field camel>> ? { equals: <<field camel>> } : undefined,
<<else>>
<<field camel>>: <<field camel>> ? { hasSome: <<field camel>> } : undefined,
<<endif>>
<<elseif field oneOne or oneMany>>
<<field camel>>: { id: <<field camel>> },
<<else>>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,11 @@ describe('<<Model pascal>>DatabaseService', () => {
...values,
<<for Fields searchable and (multiple or entity) field>>
<<if field multiple and not entity>>
<<field camel>>: { hasSome: <<field camel>> },
<<if field object>>
<<field camel>>: { equals: <<field camel>> },
<<else>>
<<field camel>>: { hasSome: <<field camel>> },
<<endif>>
<<elseif field oneOne or oneMany>>
<<field camel>>: { id: <<field camel>> },
<<else>>
Expand Down Expand Up @@ -169,7 +173,11 @@ describe('<<Model pascal>>DatabaseService', () => {
const where = {
<<for Fields searchable and (multiple or entity) field>>
<<if field multiple and not entity>>
<<field camel>>: { hasSome: <<field camel>> },
<<if field object>>
<<field camel>>: { equals: <<field camel>> },
<<else>>
<<field camel>>: { hasSome: <<field camel>> },
<<endif>>
<<elseif field oneOne or oneMany>>
<<field camel>>: { id: <<field camel>> },
<<else>>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,13 @@ function prismaType(f, m) {
if(f.subtype === "float" || f.subtype === "latitude" || f.subtype === "longitude") out += "Float";
else out += "Int";
} else if (f.type === "datetime") out += "DateTime";
else if (f.type === "object") out += "Json";
else if (f.type === "entity") {
out += f.m.names.pascal;
}

// Handle nullable and multiple fields
if (f.multiple) out += "[]";
if (f.multiple && f.type !== "object") out += "[]";
else if (f.nullable) out += "?";

switch(f.type) {
Expand All @@ -114,6 +115,11 @@ function prismaType(f, m) {
case 'date': return out += " @db.Date";
case 'time': return out += " @db.Time(3)";
}
case 'datetime':
switch(f.subtype) {
case 'date': return out += " @db.Date";
case 'time': return out += " @db.Time(3)";
}
default: return out;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,18 @@ import {
IsUUID,
<<endif>>

<<if Fields object and not internal>>
IsObject,
<<< if (root.f.multiple.some(e => e.type ==='object')) { >>>
IsArray,
<<< } >>>
<<endif>>

} from 'class-validator';

<<if Fields object>>
import { Prisma } from '@prisma/client';
<<endif>>

<<if Fields enum>>
import {
Expand Down Expand Up @@ -67,6 +78,7 @@ import {

export class <<Model pascal>>CountQueryDto {
<<for Fields searchable and not password field>>

<<if field boolean>>
@IsBoolean(<<=listModifier(field)>>)
@TransformStringToBoolean()
Expand Down Expand Up @@ -108,6 +120,13 @@ export class <<Model pascal>>CountQueryDto {
@TransformStringToDate()
<<endif>>

<<if field object>>
@IsObject(<<=listModifier(field)>>)
<<< if (field.multiple) { >>>
@IsArray()
<<< } >>>
<<endif>>

@IsOptional()
<<=optionalTypedProperty(field)>>

Expand All @@ -123,6 +142,7 @@ function optionalTypedProperty(f) {
else if (f.type === "enum") out += `${root.names.pascal}${f.names.pascal}`;
else if (f.type === "number") out += "number";
else if (f.type === "datetime") out += "Date";
else if (f.type === "object") out += "Prisma.JsonObject";
else if (f.type === "entity") {
out += "string"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,20 @@ import {
Matches,
<<endif>>

<<if Fields object and not internal>>
IsObject,
<<< if (root.f.multiple.some(e => e.type ==='object')) { >>>
IsArray,
<<< } >>>
<<endif>>

} from 'class-validator';
<<endif>>


<<if Fields object>>
import { Prisma } from '@prisma/client';
<<endif>>

<<if Fields enum>>
import {
<<for Fields enum enumField>>
Expand All @@ -73,6 +84,7 @@ import {

export class <<Model pascal>>CreateBodyDto {
<<for Fields not internal field>>

<<if field boolean>>
@IsBoolean(<<=listModifier(field)>>)
@TransformStringToBoolean()
Expand Down Expand Up @@ -122,6 +134,13 @@ export class <<Model pascal>>CreateBodyDto {
@IsOptional()
<<endif>>

<<if field object>>
@IsObject(<<=listModifier(field)>>)
<<< if (field.multiple) { >>>
@IsArray()
<<< } >>>
<<endif>>

<<=typedProperty(field)>>

<<endfor>>
Expand All @@ -137,6 +156,7 @@ function typedProperty(f) {
else if (f.type === "enum") out += `${root.names.pascal}${f.names.pascal}`;
else if (f.type === "number") out += "number";
else if (f.type === "datetime") out += "Date";
else if (f.type === "object") out += "Prisma.JsonObject";
else if (f.type === "entity") {
out += "string";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,18 @@ import {
<<if Fields entity and searchable>>
IsUUID,
<<endif>>

<<if Fields object and not internal>>
IsObject,
<<endif>>

Min,

} from 'class-validator';

<<if Fields object>>
import { Prisma } from '@prisma/client';
<<endif>>

import {
<<for Fields enum enumField>>
Expand Down Expand Up @@ -105,6 +113,13 @@ export class <<Model pascal>>FindManyQueryDto {
@TransformStringToDate()
<<endif>>

<<if field object>>
@IsObject(<<=listModifier(field)>>)
<<< if (field.multiple) { >>>
@IsArray()
<<< } >>>
<<endif>>

@IsOptional()
<<=optionalTypedProperty(field)>>

Expand Down Expand Up @@ -156,6 +171,7 @@ function optionalTypedProperty(f) {
else if (f.type === "enum") out += `${root.names.pascal}${f.names.pascal}`;
else if (f.type === "number") out += "number";
else if (f.type === "datetime") out += "Date";
else if (f.type === "object") out += "Prisma.JsonObject";
else if (f.type === "entity") {
out += "string"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,19 @@ import {
Matches,
<<endif>>

<<if Fields object and not internal>>
IsObject,
<<< if (root.f.multiple.some(e => e.type ==='object')) { >>>
IsArray,
<<< } >>>
<<endif>>

} from 'class-validator';
<<endif>>

<<if Fields object>>
import { Prisma } from '@prisma/client';
<<endif>>

<<if Fields enum>>
import {
Expand Down Expand Up @@ -116,6 +127,13 @@ export class <<Model pascal>>UpdateBodyDto {
@TransformStringToDate()
<<endif>>

<<if field object>>
@IsObject(<<=listModifier(field)>>)
<<< if (field.multiple) { >>>
@IsArray()
<<< } >>>
<<endif>>

@IsOptional()
<<=optionalTypedProperty(field)>>

Expand All @@ -131,6 +149,7 @@ function optionalTypedProperty(f) {
else if (f.type === "enum") out += `${root.names.pascal}${f.names.pascal}`;
else if (f.type === "number") out += "number";
else if (f.type === "datetime") out += "Date";
else if (f.type === "object") out += "Prisma.JsonObject";
else if (f.type === "entity") {
out += "string";
}
Expand Down
Loading

0 comments on commit 7dfa584

Please sign in to comment.