Skip to content

Commit c59ac74

Browse files
authored
dml-operation (#10)
* dml-operation * fixed1:dml-operation * fixed2:dml-operation * fixed3:dml-operation * fixbgt unused-var * fixed code
1 parent d25fb58 commit c59ac74

31 files changed

Lines changed: 1862 additions & 32 deletions

.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ module.exports = {
1818
rules: {
1919
"prettier/prettier": "error",
2020
"@typescript-eslint/no-explicit-any": "warn",
21+
"@typescript-eslint/no-unused-vars": "off"
2122
},
2223
};
2324

eslint.config.mjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export default [
2020
"@typescript-eslint/no-explicit-any": "off",
2121
"@typescript-eslint/no-var-requires": "off",
2222
"no-undef": "off",
23+
"@typescript-eslint/no-unused-vars": "off",
2324
},
2425
},
2526
{
@@ -30,6 +31,7 @@ export default [
3031
rules: {
3132
"no-undef": "off",
3233
"@typescript-eslint/no-var-requires": "off",
34+
"@typescript-eslint/no-unused-vars": "off",
3335
},
3436
},
3537
];

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"jsonwebtoken": "8.5.1",
2222
"nanoid": "3",
2323
"pg": "^8.13.3",
24+
"pg-format": "^1.0.4",
2425
"pg-hstore": "^2.3.4",
2526
"sequelize": "^6.37.5",
2627
"uuid": "^11.0.5"

src/config/database.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ const sequelize = new Sequelize(
1919
},
2020
);
2121

22+
import '../models/relations';
2223
export { sequelize };

src/controllers/ddl-controller.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { Request, Response } from 'express';
22
import { sequelize } from '../config/database';
33
import { successResponse, errorResponse } from '../utils/response';
4-
import { Operations } from '../types/ddl';
4+
import { DDLOperations } from '../types/ddl';
55
import { DDLExecutor } from '../operations/migrate';
66

77
export const migrate = async (req: Request, res: Response) => {
8-
const { operations }: { operations: Operations[] } = req.body;
8+
const { operations }: { operations: DDLOperations[] } = req.body;
99
if (!operations || !Array.isArray(operations))
1010
return errorResponse(res, 'Invalid payload structure', 400);
1111

src/controllers/dml-controller.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { Request, Response } from 'express';
2+
import { sequelize } from '../config/database';
3+
import { successResponse, errorResponse } from '../utils/response';
4+
import { DMLOperations } from '../types/dml';
5+
import { DMLExecutor } from '../operations/execute';
6+
7+
export const execute = async (req: Request, res: Response) => {
8+
const { operations }: { operations: DMLOperations[] } = req.body;
9+
if (!operations || !Array.isArray(operations))
10+
return errorResponse(res, 'Invalid payload structure', 400);
11+
12+
const transaction = await sequelize.transaction();
13+
14+
try {
15+
const result = await DMLExecutor.execute(operations, transaction);
16+
await transaction.commit();
17+
return successResponse(
18+
res,
19+
result,
20+
'DML operations completed successfully',
21+
);
22+
} catch (error: any) {
23+
await transaction.rollback();
24+
return errorResponse(res, error.message, 500);
25+
}
26+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { Request, Response } from 'express';
2+
import SchemaRepository from '../repositories/schema-repository';
3+
4+
export const schema = async (req: Request, res: Response) => {
5+
try {
6+
const tables = await SchemaRepository.getSchemas();
7+
8+
return res.json({ success: true, tables });
9+
} catch (error) {
10+
const errorMessage =
11+
error instanceof Error ? error.message : 'Unknown error';
12+
res.status(500).json({ error: errorMessage });
13+
}
14+
};

src/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ import userRoutes from './routes/user-routes';
44
import authRoutes from './routes/auth-routes';
55
import ddlRoutes from './routes/ddl-routes';
66
import { sequelize } from './config/database';
7+
import schemaRoutes from './routes/schema-routes';
8+
import dmlRoutes from './routes/dml-routes';
79

810
sequelize
9-
.sync({ force: true })
11+
.sync({ alter: true })
1012
.then(async () => {
1113
console.log('Database synchronized successfully.');
1214
})
@@ -26,6 +28,8 @@ const apiRouter = express.Router();
2628
apiRouter.use('/auth', authRoutes);
2729
apiRouter.use('/users', userRoutes);
2830
apiRouter.use('/migrate', ddlRoutes);
31+
apiRouter.use('/schemas', schemaRoutes);
32+
apiRouter.use('/execute', dmlRoutes);
2933

3034
app.use('/api', apiRouter);
3135

src/models/metadata-column.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,4 @@ MetadataColumn.init(
6868
},
6969
);
7070

71-
MetadataColumn.belongsTo(MetadataTable, {
72-
foreignKey: 'table_id',
73-
onDelete: 'CASCADE',
74-
});
75-
7671
export default MetadataColumn;

src/models/metadata-table.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ class MetadataTable extends Model {
66
public table_name!: string;
77
public readonly createdAt!: Date;
88
public readonly updatedAt!: Date;
9-
primaryKey: any;
109
}
1110

1211
MetadataTable.init(

0 commit comments

Comments
 (0)