|
|
@ -18,6 +18,7 @@ export default class NcTemplateParser { |
|
|
|
|
|
|
|
|
|
|
|
private _tables: any[]; |
|
|
|
private _tables: any[]; |
|
|
|
private _relations: any[]; |
|
|
|
private _relations: any[]; |
|
|
|
|
|
|
|
private _m2mRelations: any[]; |
|
|
|
private template: any; |
|
|
|
private template: any; |
|
|
|
|
|
|
|
|
|
|
|
constructor({ client, template }) { |
|
|
|
constructor({ client, template }) { |
|
|
@ -28,12 +29,16 @@ export default class NcTemplateParser { |
|
|
|
public parse(template?: any): any { |
|
|
|
public parse(template?: any): any { |
|
|
|
const tables = []; |
|
|
|
const tables = []; |
|
|
|
this.template = template || this.template; |
|
|
|
this.template = template || this.template; |
|
|
|
for (const tableTemplate of this.template._tables) { |
|
|
|
for (const tableTemplate of this.template.tables) { |
|
|
|
const table = this.extractTable(tableTemplate); |
|
|
|
const table = this.extractTable(tableTemplate); |
|
|
|
tables.push(table); |
|
|
|
tables.push(table); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
this._tables = tables; |
|
|
|
this._tables = tables; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (const tableTemplate of this.template.tables) { |
|
|
|
|
|
|
|
this.extractRelations(tableTemplate); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private extractTable(tableTemplate) { |
|
|
|
private extractTable(tableTemplate) { |
|
|
@ -65,28 +70,97 @@ export default class NcTemplateParser { |
|
|
|
if (!tableColumn?.cn) { |
|
|
|
if (!tableColumn?.cn) { |
|
|
|
throw Error('Missing column name in template'); |
|
|
|
throw Error('Missing column name in template'); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
switch (tableColumn.uidt) { |
|
|
|
if ( |
|
|
|
// case UITypes.ForeignKey:
|
|
|
|
tableColumn.uidt === UITypes.LinkToAnotherRecord || |
|
|
|
// // todo :
|
|
|
|
tableColumn.uidt === UITypes.ForeignKey |
|
|
|
// this.extractRelations(tableColumn, 'bt');
|
|
|
|
) { |
|
|
|
// break;
|
|
|
|
// todo :
|
|
|
|
// case UITypes.LinkToAnotherRecord:
|
|
|
|
} else { |
|
|
|
// // todo :
|
|
|
|
const column = { |
|
|
|
// this.extractRelations(tableColumn, 'hm');
|
|
|
|
...this.sqlUi.getNewColumn(''), |
|
|
|
// // this.extractRelations(tableColumn, 'mm');
|
|
|
|
cn: tableColumn.cn, |
|
|
|
// break;
|
|
|
|
_cn: tableColumn.cn, |
|
|
|
default: |
|
|
|
uidt: tableColumn.uidt, |
|
|
|
columns.push({ |
|
|
|
...this.sqlUi.getDataTypeForUiType(tableColumn) |
|
|
|
...this.sqlUi.getNewColumn(''), |
|
|
|
}; |
|
|
|
cn: tableColumn.cn, |
|
|
|
columns.push(column); |
|
|
|
_cn: tableColumn.cn, |
|
|
|
|
|
|
|
uidt: tableColumn.uidt, |
|
|
|
|
|
|
|
...this.sqlUi.getDataTypeForUiType(tableColumn) |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
return columns; |
|
|
|
return columns; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
protected extractRelations(_columnTemplate) { |
|
|
|
protected extractRelations(tableTemplate) { |
|
|
|
if (!this._relations) this._relations = []; |
|
|
|
if (!this._relations) this._relations = []; |
|
|
|
|
|
|
|
if (!this._m2mRelations) this._m2mRelations = []; |
|
|
|
|
|
|
|
for (const hasMany of tableTemplate.hasMany || []) { |
|
|
|
|
|
|
|
const childTable = this.tables.find(table => table.tn === hasMany.tn); |
|
|
|
|
|
|
|
const partentTable = this.tables.find( |
|
|
|
|
|
|
|
table => table.tn === tableTemplate.tn |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
const parentPrimaryColumn = partentTable.columns.find( |
|
|
|
|
|
|
|
column => column.uidt === UITypes.ID |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// add a column in child table
|
|
|
|
|
|
|
|
const childColumnName = `${tableTemplate.tn}_id`; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
childTable.columns.push({ |
|
|
|
|
|
|
|
cn: childColumnName, |
|
|
|
|
|
|
|
_cn: childColumnName, |
|
|
|
|
|
|
|
rqd: false, |
|
|
|
|
|
|
|
pk: false, |
|
|
|
|
|
|
|
ai: false, |
|
|
|
|
|
|
|
cdf: null, |
|
|
|
|
|
|
|
dt: parentPrimaryColumn.dt, |
|
|
|
|
|
|
|
dtxp: parentPrimaryColumn.dtxp, |
|
|
|
|
|
|
|
dtxs: parentPrimaryColumn.dtxs, |
|
|
|
|
|
|
|
un: parentPrimaryColumn.un, |
|
|
|
|
|
|
|
altered: 1 |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// add relation create entry
|
|
|
|
|
|
|
|
this._relations.push({ |
|
|
|
|
|
|
|
childColumn: childColumnName, |
|
|
|
|
|
|
|
childTable: hasMany.tn, |
|
|
|
|
|
|
|
onDelete: 'NO ACTION', |
|
|
|
|
|
|
|
onUpdate: 'NO ACTION', |
|
|
|
|
|
|
|
parentColumn: parentPrimaryColumn.cn, |
|
|
|
|
|
|
|
parentTable: tableTemplate.tn, |
|
|
|
|
|
|
|
type: 'real', |
|
|
|
|
|
|
|
updateRelation: false |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
for (const manyToMany of tableTemplate.manyToMany || []) { |
|
|
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
|
|
|
const childTable = this.tables.find(table => table.tn === manyToMany.rtn); |
|
|
|
|
|
|
|
const parentTable = this.tables.find( |
|
|
|
|
|
|
|
table => table.tn === tableTemplate.tn |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
const parentPrimaryColumn = parentTable.columns.find( |
|
|
|
|
|
|
|
column => column.uidt === UITypes.ID |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
const childPrimaryColumn = childTable.columns.find( |
|
|
|
|
|
|
|
column => column.uidt === UITypes.ID |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// add many to many relation create entry
|
|
|
|
|
|
|
|
this._m2mRelations.push({ |
|
|
|
|
|
|
|
alias: 'title8', |
|
|
|
|
|
|
|
childColumn: childPrimaryColumn.cn, |
|
|
|
|
|
|
|
childTable: childTable.tn, |
|
|
|
|
|
|
|
onDelete: 'NO ACTION', |
|
|
|
|
|
|
|
onUpdate: 'NO ACTION', |
|
|
|
|
|
|
|
parentColumn: parentPrimaryColumn.cn, |
|
|
|
|
|
|
|
parentTable: parentTable.tn, |
|
|
|
|
|
|
|
type: 'real', |
|
|
|
|
|
|
|
updateRelation: false |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
get tables(): any[] { |
|
|
|
get tables(): any[] { |
|
|
@ -96,4 +170,7 @@ export default class NcTemplateParser { |
|
|
|
get relations(): any[] { |
|
|
|
get relations(): any[] { |
|
|
|
return this._relations; |
|
|
|
return this._relations; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
get m2mRelations(): any[] { |
|
|
|
|
|
|
|
return this._m2mRelations; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|