Skip to content

Commit

Permalink
Schema for Subobject
Browse files Browse the repository at this point in the history
  • Loading branch information
kaihaase committed Jan 16, 2024
1 parent f08f6c1 commit f9c7ed0
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 16 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lenne.tech/cli",
"version": "0.0.99",
"version": "0.0.100",
"description": "lenne.Tech CLI: lt",
"keywords": [
"lenne.Tech",
Expand Down
15 changes: 10 additions & 5 deletions src/commands/server/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ const NewCommand: GluegunCommand = {
const props: Record<string, ServerProps> = {};
const setProps = true;
let refsSet = false;
let schemaSet = false;
while (setProps) {
const name = (
await ask({
Expand Down Expand Up @@ -92,14 +93,18 @@ const NewCommand: GluegunCommand = {
type = 'JSON';
}

if (type === 'Use own')
let schema: string;
if (type === 'Subobject') {
type = (
await ask({
type: 'input',
name: 'input',
message: `Enter property type (e.g. MyClass or MyClass[])`,
message: `Enter property type (e.g. MyClass)`,
})
).input;
schema = type;
schemaSet = true;
}

let reference: string;
let enumRef: string;
Expand Down Expand Up @@ -135,7 +140,7 @@ const NewCommand: GluegunCommand = {

const nullable = await confirm(`Nullable?`, true);

props[name] = { name, nullable, isArray, type, reference, enumRef };
props[name] = { name, nullable, isArray, type, reference, enumRef, schema };
}

const generateSpinner = spin('Generate files');
Expand Down Expand Up @@ -239,8 +244,8 @@ const NewCommand: GluegunCommand = {
info(``);

// We're done, so show what to do next
if (refsSet) {
success(`HINT: References have been added, so it is necessary to add the corresponding imports!`);
if (refsSet || schemaSet) {
success(`HINT: References / Schemata have been added, so it is necessary to add the corresponding imports!`);
}

if (!toolbox.parameters.options.fromGluegunMenu) {
Expand Down
23 changes: 15 additions & 8 deletions src/commands/server/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ const NewCommand: GluegunCommand = {
const props: Record<string, ServerProps> = {};
const setProps = true;
let refsSet = false;
let schemaSet = false;
while (setProps) {
const name = (
await ask({
Expand Down Expand Up @@ -91,14 +92,20 @@ const NewCommand: GluegunCommand = {
type = 'JSON';
}

if (type === 'Use own')
let schema: string;
if (type === 'Subobject')
{
type = (
await ask({
type: 'input',
name: 'input',
message: `Enter property type (e.g. MyClass or MyClass[])`,
message: `Enter property type (e.g. MyClass)`,
})
).input;
schema = type;
schemaSet = true;
}


let reference: string;
let enumRef: string;
Expand Down Expand Up @@ -134,7 +141,7 @@ const NewCommand: GluegunCommand = {

const nullable = await confirm(`Nullable?`, true);

props[name] = { name, nullable, isArray, type, reference, enumRef };
props[name] = { name, nullable, isArray, type, reference, enumRef, schema };
}

const generateSpinner = spin('Generate files');
Expand Down Expand Up @@ -173,18 +180,18 @@ const NewCommand: GluegunCommand = {
generateSpinner.succeed('Files generated');

// Linting
if (await confirm('Run lint?', true)) {
await system.run('npm run lint');
}
// if (await confirm('Run lint?', true)) {
// await system.run('npm run lint');
// }

// We're done, so show what to do next
info(``);
success(`Generated ${namePascal}Object in ${helper.msToMinutesAndSeconds(timer())}m.`);
info(``);

// We're done, so show what to do next
if (refsSet) {
success(`HINT: References have been added, so it is necessary to add the corresponding imports!`);
if (refsSet || schemaSet) {
success(`HINT: References / Schemata have been added, so it is necessary to add the corresponding imports!`);
}

if (!toolbox.parameters.options.fromGluegunMenu) {
Expand Down
3 changes: 3 additions & 0 deletions src/extensions/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ export class Server {
for (const [name, item] of Object.entries<ServerProps>(props)) {
const propName = this.camelCase(name);
const reference = item.reference?.trim() ? this.pascalCase(item.reference.trim()) : '';
const schema = item.schema?.trim() ? this.pascalCase(item.schema.trim()) : '';
const enumRef = item.enumRef?.trim() ? this.pascalCase(item.enumRef.trim()) : '';
const modelFieldType = enumRef
? 'String'
Expand Down Expand Up @@ -183,6 +184,8 @@ export class Server {
@Prop(${
reference
? (isArray ? '[' : '') + `{ ref: '${reference}', type: Schema.Types.ObjectId }` + (isArray ? ']' : '')
: schema
? (isArray ? '[' : '') + `{ type: ${schema}Schema }` + (isArray ? ']' : '')
: enumRef
? (isArray ? '[' : '') + `{ enum: ${item.nullable ? `Object.values(${enumRef}).concat([null])` : enumRef}, type: String }` + (isArray ? ']' : '')
: type === 'Json'
Expand Down
1 change: 1 addition & 0 deletions src/interfaces/ServerProps.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ export interface ServerProps {
nullable: boolean;
reference: string;
enumRef: string;
schema: string;
type: string;
}

0 comments on commit f9c7ed0

Please sign in to comment.