-
Notifications
You must be signed in to change notification settings - Fork 0
dml-operation #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
dml-operation #10
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,4 +19,5 @@ const sequelize = new Sequelize( | |
| }, | ||
| ); | ||
|
|
||
| import '../models/relations'; | ||
| export { sequelize }; | ||
This file contains hidden or 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
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| import { Request, Response } from 'express'; | ||
| import { sequelize } from '../config/database'; | ||
| import { successResponse, errorResponse } from '../utils/response'; | ||
| import { DMLOperations } from '../types/dml'; | ||
| import { DMLExecutor } from '../operations/execute'; | ||
|
|
||
| export const execute = async (req: Request, res: Response) => { | ||
| const { operations }: { operations: DMLOperations[] } = req.body; | ||
| if (!operations || !Array.isArray(operations)) | ||
| return errorResponse(res, 'Invalid payload structure', 400); | ||
|
|
||
| const transaction = await sequelize.transaction(); | ||
|
|
||
| try { | ||
| const result = await DMLExecutor.execute(operations, transaction); | ||
| await transaction.commit(); | ||
| return successResponse( | ||
| res, | ||
| result, | ||
| 'DML operations completed successfully', | ||
| ); | ||
| } catch (error: any) { | ||
| await transaction.rollback(); | ||
| return errorResponse(res, error.message, 500); | ||
| } | ||
| }; |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import { Request, Response } from 'express'; | ||
| import SchemaRepository from '../repositories/schema-repository'; | ||
|
|
||
| export const schema = async (req: Request, res: Response) => { | ||
| try { | ||
| const tables = await SchemaRepository.getSchemas(); | ||
|
|
||
| return res.json({ success: true, tables }); | ||
| } catch (error) { | ||
| const errorMessage = | ||
| error instanceof Error ? error.message : 'Unknown error'; | ||
| res.status(500).json({ error: errorMessage }); | ||
| } | ||
| }; |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import MetadataTable from './metadata-table'; | ||
| import MetadataColumn from './metadata-column'; | ||
|
|
||
| MetadataTable.hasMany(MetadataColumn, { | ||
| foreignKey: 'table_id', | ||
| as: 'columns', | ||
| onDelete: 'CASCADE', | ||
| }); | ||
|
|
||
| MetadataColumn.belongsTo(MetadataTable, { | ||
| foreignKey: 'table_id', | ||
| as: 'table', | ||
| onDelete: 'CASCADE', | ||
| }); |
This file contains hidden or 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
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| import { Transaction } from 'sequelize'; | ||
| import { DeleteInstruction } from '../../types/dml'; | ||
| import { DMLRepository } from '../../repositories/dml-repository'; | ||
| import MetadataTableRepository from '../../repositories/metadata-table-repository'; | ||
| import MetadataColumnRepository from '../../repositories/metadata-column-repository'; | ||
| import { | ||
| parseAndValidateCondition, | ||
| validIdentifier, | ||
| } from '../../utils/validation'; | ||
|
|
||
| export class DeleteOperation { | ||
| static async execute( | ||
| instruction: DeleteInstruction, | ||
| transaction: Transaction, | ||
| ) { | ||
| const { table, condition, params } = instruction; | ||
|
|
||
| if (!validIdentifier(table)) | ||
| throw new Error(`Invalid table name: ${table}`); | ||
|
|
||
| const metadataTable = await MetadataTableRepository.findOne( | ||
| { table_name: table }, | ||
| transaction, | ||
| ); | ||
| if (!metadataTable) throw new Error(`Table ${table} does not exist`); | ||
|
|
||
| const metadataColumns = await MetadataColumnRepository.findAll( | ||
| { table_id: metadataTable.id }, | ||
| transaction, | ||
| ); | ||
|
|
||
| const parsedCondition = condition | ||
| ? parseAndValidateCondition(condition, metadataColumns) | ||
| : {}; | ||
| const result = await DMLRepository.delete( | ||
| table, | ||
| parsedCondition, | ||
| params, | ||
| transaction, | ||
| ); | ||
|
|
||
| await transaction.afterCommit(() => { | ||
| console.log(`Data deleted from ${table} successfully`); | ||
| }); | ||
|
|
||
| return result; | ||
| } | ||
| } |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| export { SelectOperation } from './select'; | ||
| export { InsertOperation } from './insert'; | ||
| export { UpdateOperation } from './update'; | ||
| export { DeleteOperation } from './delete'; |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import { Transaction } from 'sequelize'; | ||
| import { InsertInstruction } from '../../types/dml'; | ||
| import { DMLRepository } from '../../repositories/dml-repository'; | ||
| import { parseAndValidateData, validIdentifier } from '../../utils/validation'; | ||
|
|
||
| export class InsertOperation { | ||
| static async execute( | ||
| instruction: InsertInstruction, | ||
| transaction: Transaction, | ||
| ) { | ||
| const { table, data } = instruction; | ||
|
|
||
| if (!validIdentifier(table)) { | ||
| throw new Error(`Invalid table name: ${table}`); | ||
| } | ||
|
|
||
| if (!data || Object.keys(data).length === 0) { | ||
| throw new Error('Insert data cannot be empty'); | ||
| } | ||
|
|
||
| const parsedData = await parseAndValidateData(table, data, transaction); | ||
| const result = await DMLRepository.insert(table, parsedData, transaction); | ||
|
|
||
| await transaction.afterCommit(() => { | ||
| console.log(`Data inserted into ${table} successfully`); | ||
| }); | ||
|
|
||
| return result[0][0].id; | ||
| } | ||
| } |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| import { Transaction } from 'sequelize'; | ||
| import { SelectInstruction } from '../../types/dml'; | ||
| import { DMLRepository } from '../../repositories/dml-repository'; | ||
| import MetadataTableRepository from '../../repositories/metadata-table-repository'; | ||
| import { | ||
| parseAndValidateCondition, | ||
| validIdentifier, | ||
| } from '../../utils/validation'; | ||
|
|
||
| export class SelectOperation { | ||
| static async execute( | ||
| instruction: SelectInstruction, | ||
| transaction: Transaction, | ||
| ) { | ||
| const { table, condition, orderBy, limit, offset, params } = instruction; | ||
|
|
||
| if (!validIdentifier(table)) | ||
| throw new Error(`Invalid table name: ${table}`); | ||
|
|
||
| const metadataTable = await MetadataTableRepository.findOne( | ||
| { table_name: table }, | ||
| transaction, | ||
| ); | ||
| if (!metadataTable) { | ||
| throw new Error(`Table ${table} does not exist`); | ||
| } | ||
|
|
||
| const parsedCondition = condition | ||
| ? parseAndValidateCondition(condition) | ||
| : {}; | ||
| const result = await DMLRepository.select( | ||
| table, | ||
| parsedCondition, | ||
| orderBy, | ||
| limit, | ||
| offset, | ||
| params, | ||
| transaction, | ||
| ); | ||
|
|
||
| return result; | ||
| } | ||
| } |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| import { Transaction } from 'sequelize'; | ||
| import { UpdateInstruction } from '../../types/dml'; | ||
| import { DMLRepository } from '../../repositories/dml-repository'; | ||
| import { | ||
| parseAndValidateCondition, | ||
| parseAndValidateData, | ||
| validIdentifier, | ||
| } from '../../utils/validation'; | ||
|
|
||
| export class UpdateOperation { | ||
| static async execute( | ||
| instruction: UpdateInstruction, | ||
| transaction: Transaction, | ||
| ) { | ||
| const { table, condition, set, params } = instruction; | ||
|
|
||
| if (!validIdentifier(table)) | ||
| throw new Error(`Invalid table name: ${table}`); | ||
|
|
||
| if (!set || Object.keys(set).length === 0) | ||
| throw new Error('Update set cannot be empty'); | ||
|
|
||
| const parsedSet = await parseAndValidateData(table, set, transaction); | ||
| const parsedCondition = condition | ||
| ? parseAndValidateCondition(condition) | ||
| : {}; | ||
|
|
||
| const result = await DMLRepository.update( | ||
| table, | ||
| parsedSet, | ||
| parsedCondition, | ||
| params, | ||
| transaction, | ||
| ); | ||
|
|
||
| await transaction.afterCommit(() => { | ||
| console.log(`Data updated in ${table} successfully`); | ||
| }); | ||
|
|
||
| return result; | ||
| } | ||
| } |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| import { Transaction } from 'sequelize'; | ||
| import { DMLOperations } from '../types/dml'; | ||
| import { | ||
| SelectOperation, | ||
| InsertOperation, | ||
| UpdateOperation, | ||
| DeleteOperation, | ||
| } from '../operations/dml'; | ||
|
|
||
| export class DMLExecutor { | ||
| static async execute(operations: DMLOperations[], transaction: Transaction) { | ||
| const results: Record<string, any>[] = []; | ||
|
|
||
| for (const { operation, instruction } of operations) { | ||
| switch (operation) { | ||
| case 'Select': { | ||
| const selectResult = await SelectOperation.execute( | ||
| instruction, | ||
| transaction, | ||
| ); | ||
| results.push(selectResult); | ||
| break; | ||
| } | ||
| case 'Insert': { | ||
| const insertResult = await InsertOperation.execute( | ||
| instruction, | ||
| transaction, | ||
| ); | ||
| results.push(insertResult); | ||
| break; | ||
| } | ||
| case 'Update': { | ||
| const updateResult = await UpdateOperation.execute( | ||
| instruction, | ||
| transaction, | ||
| ); | ||
| if (updateResult) { | ||
| results.push(updateResult); | ||
| } | ||
| break; | ||
| } | ||
| case 'Delete': { | ||
| const deleteResult = await DeleteOperation.execute( | ||
| instruction, | ||
| transaction, | ||
| ); | ||
| results.push(deleteResult); | ||
| break; | ||
| } | ||
| default: { | ||
| throw new Error(`Unsupported operation: ${operation}`); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return results; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.