refactor and add some new endpoint defs

This commit is contained in:
2021-01-18 01:10:10 -07:00
parent 1e81c0ff85
commit 242e83f9b0
2 changed files with 108 additions and 11 deletions

View File

@ -222,7 +222,100 @@ let def: SystemDef = {
param: 'completed' param: 'completed'
} }
], ],
} },
{
component: 'task',
table: 'task',
type: 'update',
columns: [
{
name: 'name',
table: 'task',
param: 'name',
},
{
name: 'description',
table: 'task',
param: 'description'
},
{
name: 'completed',
table: 'task',
param: 'completed'
},
{
name: 'completed_date',
table: 'task',
param: 'completedDate'
},
],
filters: [
{
column: {
name: 'task_id',
table: 'task'
},
comparison: '=',
param: 'task',
required: true
},
]
},
{
component: 'task',
table: 'task',
type: 'delete',
columns:[],
filters: [
{
column: {
name: 'task_id',
table: 'task'
},
comparison: '=',
param: 'task',
required: true
},
]
},
{
component: 'task',
table: 'task',
type: 'item',
columns: [
{
name: 'task_id',
table: 'task'
},
{
name: 'name',
table: 'task'
},
{
name: 'description',
table: 'task'
},
{
name: 'completed',
table: 'task'
},
{
name: 'completed_date',
table: 'task'
},
],
filters: [
{
column: {
name: 'task_id',
table: 'task'
},
comparison: '=',
param: 'task',
required: true
},
]
},
] ]
}, },
{ {

View File

@ -1,9 +1,14 @@
import { import {
BelongsToDef, BelongsToDef,
ColumnDef, ColumnDef,
Filter, JoinDef, Order, ColumnRef,
SystemDef, TableDef, ComponentDef,
EndpointDef, ComponentDef, ColumnRef EndpointDef,
Filter,
JoinDef,
Order,
SystemDef,
TableDef
} from '../systemGenService'; } from '../systemGenService';
import * as path from 'path'; import * as path from 'path';
import * as fs from 'fs'; import * as fs from 'fs';
@ -21,7 +26,7 @@ export function createViews(systemDef: SystemDef) {
} }
function createComponent(component: ComponentDef, systemDef: SystemDef) { function createComponent(component: ComponentDef, systemDef: SystemDef) {
let componentPromise = new Promise<void>((componentResolve, componentReject) => { return new Promise<void>((componentResolve, componentReject) => {
ncp(path.join(process.cwd(), 'frame', 'src', 'components', '{{component}}'), path.join(process.cwd(), systemDef.name, 'src', 'components', component.component), (err: any) => { ncp(path.join(process.cwd(), 'frame', 'src', 'components', '{{component}}'), path.join(process.cwd(), systemDef.name, 'src', 'components', component.component), (err: any) => {
if (err) { if (err) {
console.log(err); console.log(err);
@ -44,11 +49,10 @@ function createComponent(component: ComponentDef, systemDef: SystemDef) {
} }
}); });
}); });
return componentPromise;
} }
function initializeComponentFile(sourceFile: string, destinationFile: string, component: string, outDir: string) { function initializeComponentFile(sourceFile: string, destinationFile: string, component: string, outDir: string) {
let filePromise = new Promise<void>((resolve, reject) => { return new Promise<void>((resolve, reject) => {
fs.rename(sourceFile, destinationFile, (err: any) => { fs.rename(sourceFile, destinationFile, (err: any) => {
let fileContents = fs.readFileSync(destinationFile, 'utf8'); let fileContents = fs.readFileSync(destinationFile, 'utf8');
var uppercaseFirstLetterComponentName = uppercaseFirstLetter(component); var uppercaseFirstLetterComponentName = uppercaseFirstLetter(component);
@ -58,8 +62,7 @@ function initializeComponentFile(sourceFile: string, destinationFile: string, co
fs.writeFileSync(destinationFile, newFileContents, 'utf8'); fs.writeFileSync(destinationFile, newFileContents, 'utf8');
resolve(); resolve();
}); });
}); })
return filePromise
} }
function addInitializeServiceCode(component: string, outDir: string) { function addInitializeServiceCode(component: string, outDir: string) {
@ -91,7 +94,7 @@ function addInitializeRoutesCode(component: string, outDir: string) {
} }
function insertMapperCode(view: EndpointDef, outDir: string): Promise<void> { function insertMapperCode(view: EndpointDef, outDir: string): Promise<void> {
const filePromise: Promise<void> = new Promise<void>((resolve) => { return new Promise<void>((resolve) => {
const separator: string = `// SYSTEM-BUILDER-${view.component}-mapper`; const separator: string = `// SYSTEM-BUILDER-${view.component}-mapper`;
let fileLocation = path.join(process.cwd(), outDir, 'src', 'components', view.component, `${view.component}Mapper.ts`); let fileLocation = path.join(process.cwd(), outDir, 'src', 'components', view.component, `${view.component}Mapper.ts`);
let initMapperFile: string = fs.readFileSync(fileLocation, 'utf8'); let initMapperFile: string = fs.readFileSync(fileLocation, 'utf8');
@ -102,7 +105,6 @@ function insertMapperCode(view: EndpointDef, outDir: string): Promise<void> {
fs.writeFileSync(fileLocation, newMapperFile, 'utf8'); fs.writeFileSync(fileLocation, newMapperFile, 'utf8');
resolve(); resolve();
}); });
return filePromise;
} }
function createMapperFunc(view: EndpointDef): string { function createMapperFunc(view: EndpointDef): string {
@ -116,6 +118,8 @@ function createMapperFunc(view: EndpointDef): string {
funcName = `update${uppercaseFirstLetter(view.component)}`; funcName = `update${uppercaseFirstLetter(view.component)}`;
} else if (view.type.indexOf('create') !== -1) { } else if (view.type.indexOf('create') !== -1) {
func = buildCreateFunc(view); func = buildCreateFunc(view);
} else if (view.type.indexOf('delete') !== -1) {
funcName = `delete${uppercaseFirstLetter(view.component)}`;
} }
return func; return func;
} }