Created
April 5, 2023 12:52
-
-
Save galek/2e1969d0a3e61a4e0f3c07b0d4e78319 to your computer and use it in GitHub Desktop.
PseudoORMInterface for microservices
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export abstract class PseudoORMInterface { | |
getDataFromTable<T1, T2>(TableName: string, valuesKeysForGetting: string[], conditions?: string[] | null): T1; | |
insertDataToTable(TableName: string); | |
updateDataToTable<T2>(Data:T2, TableName: string, conditions?: string[] | null): boolean; | |
deleteDataFromTable(TableName: string, conditions?: string[] | null); | |
} | |
export class PseudoORMImpl implements PseudoORMInterface { | |
getDataFromTable<T1, T2>(TableName: string, valuesKeysForGetting: string[], conditions?: string[] | null): T1 { | |
let conditionsStr: string = ''; | |
if (conditions && conditions.length > 0) { | |
conditionsStr = ` WHERE ${conditions.join(',')}` | |
} | |
const request: string = `SELECT ${valuesKeysForGetting.join(',')} FROM ${TableName}${conditionsStr}`; | |
return {} as T1; | |
} | |
// TODO: надо защитить как-то, что бы в Data не было SQL запроса в ключе - тут как раз orm и выходит как queryBuilder | |
insertDataToTable<T2>(TableName: string, Data:T2) { | |
// INSERT INTO table_name(column1, column2, column3, ...) VALUES (value1, value2, value3, ...); | |
const request: string = `INSERT INTO ${TableName}(${Object.keys(Data)}), VALUES (${Object.values(Data)})`; | |
}; | |
updateDataToTable<T2>(Data:T2, TableName: string, conditions?: string[] | null) { | |
let conditionsStr: string = ''; | |
if (conditions && conditions.length > 0) { | |
conditionsStr = ` WHERE ${conditions.join(',')}` | |
} | |
// https://goo.su/gjOUJc | |
let it: number = 0; | |
let strData: string = ''; | |
for (const key in Data) { | |
//console.warn(key) | |
strData += `${it===0 ? '': ', '}${key}=${(Data as any)[key]}`; | |
it++; | |
} | |
const request: string = `UPDATE ${TableName} SET ${strData} FROM ${TableName}${conditionsStr}`; | |
} | |
deleteDataFromTable(TableName: string, conditions?: string[] | null) { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment