Open sunw31 opened 2 years ago
oh i use pdman generator
{{ var today=new Date();
var fullYear=today.getFullYear();
var month=today.getMonth() + 1;
var days=today.getDate();
var pkVarName = "undefinedId";
var pkDataType = "String";
it.entity.fields.map(function(field){
if(field["type"].indexOf("int")!==-1){
field["type"] = "number";
}else if(field["type"].indexOf("varchar")!==-1){
field["type"] = "string";
}
if(field.primaryKey){
pkVarName = it.func.camel(field.defKey,false);
pkDataType = field["type"];
return;
}
});
var pkgName = it.entity.env.base.nameSpace;
var beanClass = it.entity.env.base.codeRoot;
var beanVarName = beanClass.charAt(0).toLowerCase()+beanClass.slice(1);
var serviceClass = beanClass+'Service';
var serviceVarName= beanVarName+'Service';
}}import BaseModel from "./base_model.ts";
$blankline
export type {{=beanClass}}Entity = {
{{~it.entity.fields:field:index}}
{{=it.func.camel(field.defKey,false)}}: {{=field.type}};
{{~}}
};
$blankline
/**
* @description
* Creates a instance of the {{=beanClass}} model with the properties populated
*
* @param object obj
{{~it.entity.fields:field:index}}
* @param {{=field.type}} obj.{{=it.func.camel(field.defKey,false)}}: {{=field.type}},
{{~}}
*
* @return {{=beanClass}}Model
*/
export function create{{=beanClass}}ModelObject(obj: {
{{~it.entity.fields:field:index}}
{{=it.func.camel(field.defKey,false)}}: {{=field.type}},
{{~}}
}): {{=beanClass}}Model {
return new {{=beanClass}}Model(
{{~it.entity.fields:field:index}}
obj.{{=it.func.camel(field.defKey,false)}},
{{~}}
);
}
$blankline
//@ts-ignore {{=beanClass}}Model defines a where method that has different params than base models
// where method. Might need to investigate the naming usage
export class {{=beanClass}}Model extends BaseModel {
//////////////////////////////////////////////////////////////////////////////
// FILE MARKER - PROPERTIES //////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
{{~it.entity.fields:field:index}}
/** {{=it.func.join(field.defName,field.comment,';')}} */
public {{=it.func.camel(field.defKey,false)}}: {{=field.type}};
{{~}}
$blankline
//////////////////////////////////////////////////////////////////////////////
// FILE MARKER - CONSTRCUTOR /////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
$blankline
constructor(
{{~it.entity.fields:field:index}}
{{=it.func.camel(field.defKey,false)}}: {{=field.type}},
{{~}}
) {
super();
{{~it.entity.fields:field:index}}
this.{{=it.func.camel(field.defKey,false)}} = {{=it.func.camel(field.defKey,false)}};
{{~}}
}
$blankline
//////////////////////////////////////////////////////////////////////////////
// FILE MARKER - METHODS - CRUD //////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
$blankline
/**
* Delete this model.
*
* @return Promise<boolean> False if the query failed to delete
*/
public async delete(): Promise<boolean> {
const query = `DELETE FROM {{=it.entity.defKey}} WHERE id = $1`;
const dbResult = await BaseModel.query(query, this.id);
if (dbResult.rowCount! < 1) {
return false;
}
return true;
}
$blankline
/**
* Save this model.
*
* @return Promise<{{=beanClass}}Model|null> Empty array if no data was found
*/
public async save(): Promise<{{=beanClass}}Model | null> {
// If this model already has an ID, then that means we're updating the model
if (this.id != -1) {
return this.update();
}
const query =
`INSERT INTO {{=it.entity.defKey}} ({{~it.entity.fields:field:index}}
{{=field.defKey}},
{{~}}
) VALUES (
{{~it.entity.fields:field:index}}
${{=index+1}},
{{~}});`;
const dbResult = await BaseModel.query(
query,
{{~it.entity.fields:field:index}}
this.{{=it.func.camel(field.defKey,false)}},
{{~}}
);
if (dbResult.rowCount < 1) {
return null;
}
// (crookse) We ignore this because this will never return null.
const savedResult = await {{=beanClass}}Model.where({ {{=it.func.camel(it.entity.fields[0].defKey,false)}}: this.{{=it.func.camel(it.entity.fields[0].defKey,false)}} });
if (savedResult.length === 0) {
return null;
}
return savedResult[0];
}
$blankline
/**
* Update this model.
*
* @return Promise<{{=beanClass}}Model|null> False if no results were found
*/
public async update(): Promise<{{=beanClass}}Model | null> {
const query = `UPDATE {{=it.entity.defKey}} SET
{{~it.entity.fields:field:index}}
{{=it.func.camel(field.defKey,false)}} = ${{=index+1}},
{{~}} WHERE id = $100;`;
const dbResult = await BaseModel.query(
query,
{{~it.entity.fields:field:index}}
this.{{=it.func.camel(field.defKey,false)}},
{{~}}
);
if (dbResult.rowCount! < 1) {
return null;
}
const updatedResult = await {{=beanClass}}Model.where({ {{=it.func.camel(it.entity.fields[0].defKey,false)}}: this.{{=it.func.camel(it.entity.fields[0].defKey,false)}} });
if (updatedResult.length === 0) {
return null;
}
return updatedResult[0];
}
$blankline
//////////////////////////////////////////////////////////////////////////////
// FILE MARKER - METHODS - STATIC ////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
$blankline
/**
* @description
* See BaseModel.Where()
*
* @param {[key: string]: string} fields
*
* @return Promise<{{=beanClass}}Model[]|[]>
*/
static async where(
fields: { [key: string]: string | number },
): Promise<{{=beanClass}}Model[] | []> {
const results = await BaseModel.Where("{{=it.entity.defKey}}", fields);
if (results.length <= 0) {
return [];
}
//@ts-ignore Nothing we can do about this.. the create{{=beanClass}}ModelObject expect
// a {{=beanClass}} object type, but there's no way to type it like that the return type of whereIn can't be {{=beanClass}}
return results.map((result) => {
return create{{=beanClass}}ModelObject(
result as {
{{~it.entity.fields:field:index}}
{{=it.func.camel(field.defKey,false)}}: {{=field.type}} ;
{{~}}
},
);
});
}
$blankline
/**
* @description
* See BaseModel.WhereIn()
*
* @param string column
* @param any values
*
* @return Promise<{{=beanClass}}Model[]> | []
*/
static async whereIn(
column: string,
values: string[] | number[],
): Promise<{{=beanClass}}Model[] | []> {
const results = await BaseModel.WhereIn("{{=it.entity.defKey}}", {
column,
values,
});
if (results.length <= 0) {
return [];
}
//@ts-ignore Nothing we can do about this.. the create{{=beanClass}}ModelObject expect
// a {{=beanClass}} object type, but there's no way to type it like that the return type of whereIn can't be {{=beanClass}}
return results.map((result) => {
return create{{=beanClass}}ModelObject(
result as {
{{~it.entity.fields:field:index}}
{{=it.func.camel(field.defKey,false)}}: {{=field.type}} ;
{{~}}
},
);
});
}
$blankline
//////////////////////////////////////////////////////////////////////////////
// FILE MARKER - METHODS - PUBLIC ////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
$blankline
/**
* @return {{=beanClass}}Entity
*/
public toEntity(): {{=beanClass}}Entity {
return {
{{~it.entity.fields:field:index}}
{{=it.func.camel(field.defKey,false)}}: this.{{=it.func.camel(field.defKey,false)}},
{{~}}
};
}
}
$blankline
export default {{=beanClass}}Model;
hi @sunw31, im not sure what your question is? is its regading the docblocks, we write those ourselves and is consistent across each drashland project
Summary
What is your question?
Can you give us code generator?
////////////////////////////////////////////////////////////////////////////// // FILE MARKER - METHODS - STATIC //////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////