feat: enhance database management with new migration and database node functionalities, including CRUD operations and test cases
This commit is contained in:
16
tests/base/createDbNode.ts
Normal file
16
tests/base/createDbNode.ts
Normal file
@ -0,0 +1,16 @@
|
||||
import createDatabaseNode from "../functions/createDatabaseNode";
|
||||
|
||||
(async () => {
|
||||
try {
|
||||
const node = await createDatabaseNode(
|
||||
"localhost",
|
||||
3306,
|
||||
"root",
|
||||
"password"
|
||||
);
|
||||
|
||||
console.log("node", node);
|
||||
} catch (error) {
|
||||
console.error("Error during test execution:", error);
|
||||
}
|
||||
})();
|
||||
@ -1,21 +1,42 @@
|
||||
import createDatabase from "../functions/createDatabase";
|
||||
import createDatabaseNode from "../functions/createDatabaseNode";
|
||||
import createMigration from "../functions/createMigration";
|
||||
import createProject from "../functions/createProject";
|
||||
import databaseMigrationUp from "..//functions/databaseMigrationUp";
|
||||
|
||||
(async () => {
|
||||
try {
|
||||
const project = await createProject("Test Project");
|
||||
const node = await createDatabaseNode("localhost", 3306, "root", "root");
|
||||
|
||||
const result_1 = await createMigration(project.token, [
|
||||
{
|
||||
name: "users",
|
||||
fields: {
|
||||
name: { type: "string", isNullable: false },
|
||||
age: { type: "int", isNullable: true },
|
||||
},
|
||||
},
|
||||
]);
|
||||
console.log("Database node created:", node);
|
||||
|
||||
console.log("Migration 1:", result_1.data);
|
||||
const project = await createProject("Test Migrations Project");
|
||||
|
||||
console.log("Project created:", project);
|
||||
|
||||
const db = await createDatabase(project.id, node.id);
|
||||
|
||||
console.log("Database created:", db);
|
||||
|
||||
const migration = await createMigration(
|
||||
db.id,
|
||||
"CREATE TABLE `test` (id INT)",
|
||||
"DROP TABLE `test`"
|
||||
);
|
||||
|
||||
console.log("Migration created:", migration);
|
||||
|
||||
const migration2 = await createMigration(
|
||||
db.id,
|
||||
"ALTER TABLE `test` ADD COLUMN name VARCHAR(255)",
|
||||
"ALTER TABLE `test` DROP COLUMN name"
|
||||
);
|
||||
|
||||
console.log("Second Migration created:", migration2);
|
||||
|
||||
const migrationsUp = await databaseMigrationUp(db.id);
|
||||
|
||||
console.log("Migrations applied:", migrationsUp);
|
||||
} catch (error) {
|
||||
console.error("Error during test execution:", error);
|
||||
}
|
||||
|
||||
15
tests/functions/createDatabase.ts
Normal file
15
tests/functions/createDatabase.ts
Normal file
@ -0,0 +1,15 @@
|
||||
import axios from "axios";
|
||||
import { config } from "../config";
|
||||
|
||||
export default async (projectId: string, databaseNodeId: string) => {
|
||||
try {
|
||||
const response = await axios.post(`${config.url}/database/create`, {
|
||||
projectId,
|
||||
databaseNodeId,
|
||||
});
|
||||
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
console.error("Error in creating database:", error);
|
||||
}
|
||||
};
|
||||
22
tests/functions/createDatabaseNode.ts
Normal file
22
tests/functions/createDatabaseNode.ts
Normal file
@ -0,0 +1,22 @@
|
||||
import { config } from "../config";
|
||||
import axios from "axios";
|
||||
|
||||
export default async (
|
||||
host: string,
|
||||
port: number,
|
||||
username: string,
|
||||
password: string
|
||||
) => {
|
||||
try {
|
||||
const response = await axios.post(`${config.url}/database/node/create`, {
|
||||
host,
|
||||
port,
|
||||
username,
|
||||
password,
|
||||
});
|
||||
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
console.error("Error in creating database node:", error);
|
||||
}
|
||||
};
|
||||
@ -1,12 +1,20 @@
|
||||
import axios from "axios";
|
||||
import { config } from "../config";
|
||||
|
||||
export default async (token: string, tables: any) => {
|
||||
export default async (
|
||||
databaseId: string,
|
||||
upQuery: string,
|
||||
downQuery: string
|
||||
) => {
|
||||
try {
|
||||
const response = await axios.post(`${config.url}/migrations/create`, {
|
||||
token,
|
||||
tables,
|
||||
});
|
||||
const response = await axios.post(
|
||||
`${config.url}/database/migration/create`,
|
||||
{
|
||||
databaseId,
|
||||
up: upQuery,
|
||||
down: downQuery,
|
||||
}
|
||||
);
|
||||
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
|
||||
14
tests/functions/databaseMigrationUp.ts
Normal file
14
tests/functions/databaseMigrationUp.ts
Normal file
@ -0,0 +1,14 @@
|
||||
import { config } from "../config";
|
||||
import axios from "axios";
|
||||
|
||||
export default async (databaseId: string) => {
|
||||
try {
|
||||
const response = await axios.get(
|
||||
`${config.url}/database/migration/up/${databaseId}`
|
||||
);
|
||||
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
console.error("Error in migrating database up:", error);
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user