Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

Split each mirage model api endpoint into their own file

天舟682638919c19
diff --git a/frontend/src/miragejs/routes/activity.js b/frontend/src/miragejs/routes/activity.js
new file mode 100644
index 0000000000..bef2d27295
--- /dev/null
+++ b/frontend/src/miragejs/routes/activity.js
@@ -0,0 +1,55 @@
+import { WORKSPACE_ID } from "./index";
+
+export default function configureActivity(route) {
+ route.get("/activity", function (schema, request) {
+ const {
+ queryParams: { containerid: containerId, type },
+ } = request;
+ return schema.activities.where((activity) => {
+ if (activity.workspaceId != WORKSPACE_ID) {
+ return false;
+ }
+
+ if (containerId && containerId != activity.containerId) {
+ return false;
+ }
+
+ if (type && !activity.actionType.startsWith(type)) {
+ return false;
+ }
+
+ return true;
+ });
+ });
+
+ route.post("/activity", function (schema, request) {
+ const ts = Date.now();
+ const attrs = this.normalizedRequestAttrs("activity");
+ const newActivity = {
+ ...attrs,
+ createdTs: ts,
+ lastUpdatedTs: ts,
+ actionType: "bytebase.task.comment.create",
+ workspaceId: WORKSPACE_ID,
+ };
+ const createdActivity = schema.activities.create(newActivity);
+ return createdActivity;
+ });
+
+ route.patch("/activity/:activityId", function (schema, request) {
+ const attrs = this.normalizedRequestAttrs("activity-patch");
+ const activity = schema.activities.find(request.params.activityId);
+ if (activity) {
+ return activity.update({ ...attrs, lastUpdatedTs: Date.now() });
+ }
+ return new Response(
+ 404,
+ {},
+ { errors: "Activity " + request.params.activityId + " not found" }
+ );
+ });
+
+ route.delete("/activity/:activityId", function (schema, request) {
+ return schema.activities.find(request.params.activityId).destroy();
+ });
+}
diff --git a/frontend/src/miragejs/routes/auth.js b/frontend/src/miragejs/routes/auth.js
new file mode 100644
index 0000000000..449d1a1e37
--- /dev/null
+++ b/frontend/src/miragejs/routes/auth.js
@@ -0,0 +1,76 @@
+import { WORKSPACE_ID } from "./index";
+
+export default function configureAuth(route) {
+ route.post("/auth/login", function (schema, request) {
+ const loginInfo = this.normalizedRequestAttrs("login-info");
+ const user = schema.users.findBy({
+ email: loginInfo.email,
+ passwordHash: loginInfo.password,
+ });
+ if (user) {
+ return user;
+ }
+ return new Response(
+ 401,
+ {},
+ { errors: loginInfo.email + " not found or incorrect password" }
+ );
+ });
+
+ route.post("/auth/signup", function (schema, request) {
+ const signupInfo = this.normalizedRequestAttrs("signup-info");
+ const user = schema.users.findBy({ email: signupInfo.email });
+ if (user) {
+ return new Response(
+ 409,
+ {},
+ { errors: signupInfo.email + " already exists" }
+ );
+ }
+ const ts = Date.now();
+ const createdUser = schema.users.create({
+ createdTs: ts,
+ lastUpdatedTs: ts,
+ status: "ACTIVE",
+ ...signupInfo,
+ });
+
+ const newRoleMapping = {
+ principalId: createdUser.id,
+ email: createdUser.email,
+ createdTs: ts,
+ lastUpdatedTs: ts,
+ role: "DEVELOPER",
+ updaterId: createdUser.id,
+ workspaceId: WORKSPACE_ID,
+ };
+ schema.roleMappings.create(newRoleMapping);
+
+ return createdUser;
+ });
+
+ route.post("/auth/activate", function (schema, request) {
+ const activateInfo = this.normalizedRequestAttrs("activate-info");
+ if (!activateInfo.token) {
+ return new Response(400, {}, { errors: "Missing activation token" });
+ }
+
+ const user = schema.users.findBy({ email: activateInfo.email });
+ if (user) {
+ const ts = Date.now();
+ user.update({
+ name: activateInfo.name,
+ status: "ACTIVE",
+ lastUpdatedTs: ts,
+ passwordHash: activateInfo.password,
+ });
+ return user;
+ }
+
+ return new Response(
+ 400,
+ {},
+ { errors: activateInfo.email + " is not invited" }
+ );
+ });
+}
diff --git a/frontend/src/miragejs/routes/bookmark.js b/frontend/src/miragejs/routes/bookmark.js
new file mode 100644
index 0000000000..e7e570f4b8
--- /dev/null
+++ b/frontend/src/miragejs/routes/bookmark.js
@@ -0,0 +1,31 @@
+import { WORKSPACE_ID } from "./index";
+
+export default function configureBookmark(route) {
+ route.get("/bookmark", function (schema, request) {
+ const {
+ queryParams: { userid: userId },
+ } = request;
+
+ return schema.bookmarks.where({
+ workspaceId: WORKSPACE_ID,
+ creatorId: userId,
+ });
+ });
+
+ route.post("/bookmark", function (schema, request) {
+ const newBookmark = {
+ ...this.normalizedRequestAttrs("bookmark"),
+ workspaceId: WORKSPACE_ID,
+ };
+ return schema.bookmarks.create(newBookmark);
+ });
+
+ route.patch("/bookmark/:bookmarkId", function (schema, request) {
+ const attrs = this.normalizedRequestAttrs("bookmark");
+ return schema.bookmarks.find(request.params.bookmarkId).update(attrs);
+ });
+
+ route.delete("/bookmark/:bookmarkId", function (schema, request) {
+ return schema.bookmarks.find(request.params.bookmarkId).destroy();
+ });
+}
diff --git a/frontend/src/miragejs/routes/dataSource.js b/frontend/src/miragejs/routes/dataSource.js
new file mode 100644
index 0000000000..64ca24ad18
--- /dev/null
+++ b/frontend/src/miragejs/routes/dataSource.js
@@ -0,0 +1,194 @@
+export default function configureDataSource(route) {
+ route.get("/datasource", function (schema, request) {
+ const {
+ queryParams: { instance: instanceId },
+ } = request;
+ return schema.dataSources.where((dataSource) => {
+ return !instanceId || dataSource.instanceId == instanceId;
+ });
+ });
+
+ route.get("/database/:databaseId/datasource", function (schema, request) {
+ const database = schema.databases.find(request.params.databaseId);
+ if (database) {
+ return schema.dataSources.where((dataSource) => {
+ return dataSource.databaseId == database.id;
+ });
+ }
+ return new Response(
+ 404,
+ {},
+ { errors: "Database " + request.params.databaseId + " not found" }
+ );
+ });
+
+ route.get("/database/:databaseId/datasource/:id", function (schema, request) {
+ const database = schema.databases.find(request.params.databaseId);
+ if (database) {
+ const dataSource = schema.dataSources.find(request.params.id);
+ if (dataSource) {
+ return dataSource;
+ }
+ return new Response(
+ 404,
+ {},
+ { errors: "Data Source " + request.params.id + " not found" }
+ );
+ }
+ return new Response(
+ 404,
+ {},
+ { errors: "Database " + request.params.databaseId + " not found" }
+ );
+ });
+
+ route.post("/database/:databaseId/datasource", function (schema, request) {
+ const database = schema.databases.find(request.params.databaseId);
+ if (database) {
+ const newDataSource = {
+ ...this.normalizedRequestAttrs("data-source"),
+ };
+ return schema.dataSources.create(newDataSource);
+ }
+ return new Response(
+ 404,
+ {},
+ { errors: "Dnstance " + request.params.databaseId + " not found" }
+ );
+ });
+
+ route.patch(
+ "/database/:databaseId/datasource/:id",
+ function (schema, request) {
+ const database = schema.databases.find(request.params.databaseId);
+ if (database) {
+ const dataSource = schema.dataSources.find(request.params.id);
+ if (!dataSource) {
+ return new Response(
+ 404,
+ {},
+ {
+ errors: "Data source " + request.params.id + " not found",
+ }
+ );
+ }
+ const attrs = this.normalizedRequestAttrs("data-source");
+ return dataSource.update(attrs);
+ }
+ return new Response(
+ 404,
+ {},
+ { errors: "Database " + request.params.databaseId + " not found" }
+ );
+ }
+ );
+
+ route.delete(
+ "/database/:databaseId/datasource/:id",
+ function (schema, request) {
+ const database = schema.instances.find(request.params.databaseId);
+ if (database) {
+ const dataSource = schema.dataSources.find(request.params.id);
+ if (!dataSource) {
+ return new Response(
+ 404,
+ {},
+ {
+ errors: "Data source " + request.params.id + " not found",
+ }
+ );
+ }
+ const dataSourceMemberList = schema.dataSourceMembers.where(
+ (dataSourceMember) => {
+ return dataSourceMember.dataSourceId == dataSource.id;
+ }
+ );
+ dataSourceMemberList.models.forEach((member) => member.destroy());
+ return dataSource.destroy();
+ }
+ return new Response(
+ 404,
+ {},
+ { errors: "Database " + request.params.databaseId + " not found" }
+ );
+ }
+ );
+
+ // Data Source Member
+ // Be careful to use :dataSourceId instead of :id otherwise this.normalizedRequestAttrs
+ // would de-serialize to id, which would prevent auto increment id logic.
+ route.post(
+ "/database/:databaseId/datasource/:dataSourceId/member",
+ function (schema, request) {
+ const database = schema.databases.find(request.params.databaseId);
+ if (database) {
+ const dataSource = schema.dataSources.find(request.params.dataSourceId);
+ if (dataSource) {
+ const newDataSourceMember = {
+ ...this.normalizedRequestAttrs("data-source-member"),
+ dataSourceId: request.params.dataSourceId,
+ };
+ const newList = dataSource.memberList;
+ const member = newList.find(
+ (item) => item.principalId == newDataSourceMember.principalId
+ );
+ if (!member) {
+ newList.push(newDataSourceMember);
+ return dataSource.update({
+ memberList: newList,
+ });
+ }
+ return dataSource;
+ }
+ return new Response(
+ 404,
+ {},
+ {
+ errors: "Data source " + request.params.dataSourceId + " not found",
+ }
+ );
+ }
+ return new Response(
+ 404,
+ {},
+ { errors: "Database " + request.params.databaseId + " not found" }
+ );
+ }
+ );
+
+ route.delete(
+ "/database/:databaseId/datasource/:dataSourceId/member/:memberId",
+ function (schema, request) {
+ const database = schema.databases.find(request.params.databaseId);
+ if (database) {
+ const dataSource = schema.dataSources.find(request.params.dataSourceId);
+ if (!dataSource) {
+ return new Response(
+ 404,
+ {},
+ {
+ errors:
+ "Data source " + request.params.dataSourceId + " not found",
+ }
+ );
+ }
+ const newList = dataSource.memberList;
+ const index = newList.findIndex(
+ (item) => item.principalId == request.params.memberId
+ );
+ if (index >= 0) {
+ newList.splice(index, 1);
+ return dataSource.update({
+ memberList: newList,
+ });
+ }
+ return dataSource;
+ }
+ return new Response(
+ 404,
+ {},
+ { errors: "Database " + request.params.databaseId + " not found" }
+ );
+ }
+ );
+}
diff --git a/frontend/src/miragejs/routes/database.js b/frontend/src/miragejs/routes/database.js
new file mode 100644
index 0000000000..a08e610db9
--- /dev/null
+++ b/frontend/src/miragejs/routes/database.js
@@ -0,0 +1,100 @@
+import { WORKSPACE_ID } from "./index";
+import { ALL_DATABASE_NAME } from "../../types";
+
+export default function configureDatabase(route) {
+ route.get("/database/:id", function (schema, request) {
+ const database = schema.databases.find(request.params.id);
+ if (database) {
+ return database;
+ }
+ return new Response(
+ 404,
+ {},
+ { errors: "Database " + request.params.id + " not found" }
+ );
+ });
+
+ route.get("/database", function (schema, request) {
+ const {
+ queryParams: { environment: environmentId },
+ } = request;
+ const instanceIdList = schema.instances
+ .where({ workspaceId: WORKSPACE_ID, environmentId })
+ .models.map((instance) => instance.id);
+ if (instanceIdList.length == 0) {
+ return [];
+ }
+ return schema.databases
+ .where((database) => {
+ // If environment is specified, then we don't include the database representing all databases,
+ // since the all databases is per instance.
+ if (environmentId && database.name == ALL_DATABASE_NAME) {
+ return false;
+ }
+ return instanceIdList.includes(database.instanceId);
+ })
+ .sort((a, b) =>
+ a.name.localeCompare(b.name, undefined, { sensitivity: "base" })
+ );
+ });
+
+ route.post("/database", function (schema, request) {
+ const ts = Date.now();
+ const { taskId, creatorId, ...attrs } = this.normalizedRequestAttrs(
+ "database"
+ );
+ const newDatabase = {
+ ...attrs,
+ createdTs: ts,
+ lastUpdatedTs: ts,
+ syncStatus: "OK",
+ lastSuccessfulSyncTs: ts,
+ workspaceId: WORKSPACE_ID,
+ };
+
+ if (taskId) {
+ schema.activities.create({
+ createdTs: ts,
+ lastUpdatedTs: ts,
+ actionType: "bytebase.task",
+ containerId: taskId,
+ creatorId: creatorId,
+ comment: `Created database ${newDatabase.name}`,
+ workspaceId: WORKSPACE_ID,
+ });
+ }
+ const createdDatabase = schema.databases.create(newDatabase);
+ return createdDatabase;
+ });
+
+ route.patch("/database/:databaseId", function (schema, request) {
+ const attrs = this.normalizedRequestAttrs("database-patch");
+ const database = schema.databases.find(request.params.databaseId);
+ if (database) {
+ return database.update({ ...attrs, lastUpdatedTs: Date.now() });
+ }
+ return new Response(
+ 404,
+ {},
+ { errors: "Database " + request.params.databaseId + " not found" }
+ );
+ });
+
+ route.get("/instance/:instanceId/database", function (schema, request) {
+ const instance = schema.instances.find(request.params.instanceId);
+ if (instance) {
+ return schema.databases
+ .where((database) => {
+ return database.instanceId == instance.id;
+ })
+ .sort((a, b) =>
+ a.name.localeCompare(b.name, undefined, { sensitivity: "base" })
+ );
+ }
+ return new Response(
+ 404,
+ {},
+ { errors: "Instance " + request.params.instanceId + " not found" }
+ );
+ });
+}
diff --git a/frontend/src/miragejs/routes/environment.js b/frontend/src/miragejs/routes/environment.js
new file mode 100644
index 0000000000..8cda48249d
--- /dev/null
+++ b/frontend/src/miragejs/routes/environment.js
@@ -0,0 +1,55 @@
+import { WORKSPACE_ID } from "./index";
+
+export default function configureEnvironment(route) {
+ route.get("/environment", function (schema, request) {
+ return schema.environments
+ .where((environment) => {
+ return environment.workspaceId == WORKSPACE_ID;
+ })
+ .sort((a, b) => a.order - b.order);
+ });
+
+ route.post("/environment", function (schema, request) {
+ let order = 0;
+ const list = schema.environments.where((environment) => {
+ return environment.workspaceId == WORKSPACE_ID;
+ });
+ if (list.length > 0) {
+ order = list.sort((a, b) => b.order - a.order).models[0].order + 1;
+ }
+ const newEnvironment = {
+ ...this.normalizedRequestAttrs("environment"),
+ workspaceId: WORKSPACE_ID,
+ order,
+ };
+ return schema.environments.create(newEnvironment);
+ });
+
+ route.patch("/environment/batch", function (schema, request) {
+ const attrs = this.normalizedRequestAttrs("batch-update");
+ for (let i = 0; i < attrs.idList.length; i++) {
+ const env = schema.environments.find(attrs.idList[i]);
+ if (env) {
+ const batch = {};
+ for (let j = 0; j < attrs.fieldMaskList.length; j++) {
+ batch[attrs.fieldMaskList[j]] = attrs.rowValueList[i][j];
+ }
+ env.update(batch);
+ }
+ }
+ return schema.environments
+ .where((environment) => {
+ return environment.workspaceId == WORKSPACE_ID;
+ })
+ .sort((a, b) => a.order - b.order);
+ });
+
+ route.patch("/environment/:environmentId", function (schema, request) {
+ const attrs = this.normalizedRequestAttrs("environment");
+ return schema.environments.find(request.params.environmentId).update(attrs);
+ });
+
+ route.delete("/environment/:environmentId", function (schema, request) {
+ return schema.environments.find(request.params.environmentId).destroy();
+ });
+}
diff --git a/frontend/src/miragejs/routes/index.js b/frontend/src/miragejs/routes/index.js
index 47856ba6e6..9856109dd6 100644
--- a/frontend/src/miragejs/routes/index.js
+++ b/frontend/src/miragejs/routes/index.js
@@ -1,12 +1,18 @@
/*
* Mirage JS guide on Routes: https://miragejs.com/docs/route-handlers/functions
*/
-import { Response } from "miragejs";
-import isEqual from "lodash-es/isEqual";
-import { TaskBuiltinFieldId } from "../../plugins";
-import { ALL_DATABASE_NAME } from "../../types";
-
-const WORKSPACE_ID = 1;
+import configureUser from "./user";
+import configureAuth from "./auth";
+import configureRoleMapping from "./roleMapping";
+import configureActivity from "./activity";
+import configureBookmark from "./bookmark";
+import configureTask from "./task";
+import configureEnvironment from "./environment";
+import configureInstance from "./instance";
+import configureDatabase from "./database";
+import configureDataSource from "./dataSource";
+
+export const WORKSPACE_ID = 1;
export default function routes() {
// Change this value to simulate response delay.
@@ -16,799 +22,32 @@ export default function routes() {
this.namespace = "api";
// User
- this.get("/user/:id");
-
- this.get("/user");
-
- this.post("/user", function (schema, request) {
- const attrs = this.normalizedRequestAttrs("user");
- const user = schema.users.findBy({ email: attrs.email });
- if (user) {
- return new Response(200, {}, user);
- }
- return schema.users.create(attrs);
- });
-
- this.patch("/user/:userId", function (schema, request) {
- const attrs = this.normalizedRequestAttrs("user");
- return schema.users.find(request.params.userId).update(attrs);
- });
-
- this.get("/user/:userId/database", function (schema, request) {
- return schema.databases.where((database) => {
- const dataSourceList = schema.dataSources.where({
- workspaceId: WORKSPACE_ID,
- databaseId: database.id,
- });
-
- for (const dataSource of dataSourceList.models) {
- if (
- dataSource.memberList.find((item) => {
- return item.principalId == request.params.userId;
- })
- ) {
- return true;
- }
- }
- return false;
- });
- });
+ configureUser(this);
// Auth
- this.post("/auth/login", function (schema, request) {
- const loginInfo = this.normalizedRequestAttrs("login-info");
- const user = schema.users.findBy({
- email: loginInfo.email,
- passwordHash: loginInfo.password,
- });
- if (user) {
- return user;
- }
- return new Response(
- 401,
- {},
- { errors: loginInfo.email + " not found or incorrect password" }
- );
- });
-
- this.post("/auth/signup", function (schema, request) {
- const signupInfo = this.normalizedRequestAttrs("signup-info");
- const user = schema.users.findBy({ email: signupInfo.email });
- if (user) {
- return new Response(
- 409,
- {},
- { errors: signupInfo.email + " already exists" }
- );
- }
- const ts = Date.now();
- const createdUser = schema.users.create({
- createdTs: ts,
- lastUpdatedTs: ts,
- status: "ACTIVE",
- ...signupInfo,
- });
-
- const newRoleMapping = {
- principalId: createdUser.id,
- email: createdUser.email,
- createdTs: ts,
- lastUpdatedTs: ts,
- role: "DEVELOPER",
- updaterId: createdUser.id,
- workspaceId: WORKSPACE_ID,
- };
- schema.roleMappings.create(newRoleMapping);
-
- return createdUser;
- });
-
- this.post("/auth/activate", function (schema, request) {
- const activateInfo = this.normalizedRequestAttrs("activate-info");
- if (!activateInfo.token) {
- return new Response(400, {}, { errors: "Missing activation token" });
- }
-
- const user = schema.users.findBy({ email: activateInfo.email });
- if (user) {
- const ts = Date.now();
- user.update({
- name: activateInfo.name,
- status: "ACTIVE",
- lastUpdatedTs: ts,
- passwordHash: activateInfo.password,
- });
- return user;
- }
-
- return new Response(
- 400,
- {},
- { errors: activateInfo.email + " is not invited" }
- );
- });
+ configureAuth(this);
// RoleMapping
- this.get("/rolemapping", function (schema, request) {
- return schema.roleMappings.where((roleMapping) => {
- return roleMapping.workspaceId == WORKSPACE_ID;
- });
- });
+ configureRoleMapping(this);
- this.post("/rolemapping", function (schema, request) {
- const ts = Date.now();
- const attrs = {
- ...this.normalizedRequestAttrs("role-mapping"),
- workspaceId: WORKSPACE_ID,
- };
- const newRoleMapping = {
- ...attrs,
- createdTs: ts,
- lastUpdatedTs: ts,
- role: attrs.role,
- updaterId: attrs.updaterId,
- workspaceId: WORKSPACE_ID,
- };
- return schema.roleMappings.create(newRoleMapping);
- });
-
- this.patch("/rolemapping/:roleMappingId", function (schema, request) {
- const attrs = this.normalizedRequestAttrs("role-mapping");
- return schema.roleMappings.find(request.params.roleMappingId).update(attrs);
- });
+ // Activity
+ configureActivity(this);
- this.delete("/rolemapping/:roleMappingId", function (schema, request) {
- return schema.roleMappings.find(request.params.roleMappingId).destroy();
- });
+ // Bookmark
+ configureBookmark(this);
// Task
- this.get("/task", function (schema, request) {
- const {
- queryParams: { userid: userId },
- } = request;
-
- if (userId) {
- return schema.tasks.where((task) => {
- return (
- task.workspaceId == WORKSPACE_ID &&
- (task.creatorId == userId ||
- task.assigneeId == userId ||
- task.subscriberIdList.includes(userId))
- );
- });
- }
- return schema.tasks.none();
- });
-
- this.get("/task/:id", function (schema, request) {
- const task = schema.tasks.find(request.params.id);
- if (task) {
- return task;
- }
- return new Response(
- 404,
- {},
- { errors: "Task " + request.params.id + " not found" }
- );
- });
-
- this.post("/task", function (schema, request) {
- const ts = Date.now();
- const attrs = this.normalizedRequestAttrs("task");
- const newTask = {
- ...attrs,
- createdTs: ts,
- lastUpdatedTs: ts,
- status: "OPEN",
- workspaceId: WORKSPACE_ID,
- };
- const createdTask = schema.tasks.create(newTask);
-
- schema.activities.create({
- createdTs: ts,
- lastUpdatedTs: ts,
- actionType: "bytebase.task.create",
- containerId: createdTask.id,
- creatorId: attrs.creatorId,
- comment: "",
- workspaceId: WORKSPACE_ID,
- });
-
- return createdTask;
- });
-
- this.patch("/task/:taskId", function (schema, request) {
- const attrs = this.normalizedRequestAttrs("task-patch");
- const task = schema.tasks.find(request.params.taskId);
- if (task) {
- const changeList = [];
-
- if (attrs.name) {
- if (task.name != attrs.name) {
- changeList.push({
- fieldId: TaskBuiltinFieldId.NAME,
- oldValue: task.name,
- newValue: attrs.name,
- });
- }
- }
-
- if (attrs.status) {
- if (task.status != attrs.status) {
- changeList.push({
- fieldId: TaskBuiltinFieldId.STATUS,
- oldValue: task.status,
- newValue: attrs.status,
- });
- }
- }
-
- if (attrs.assigneeId) {
- if (task.assigneeId != attrs.assigneeId) {
- changeList.push({
- fieldId: TaskBuiltinFieldId.ASSIGNEE,
- oldValue: task.assigneeId,
- newValue: attrs.assigneeId,
- });
- }
- }
-
- // Empty string is valid
- if (attrs.description !== undefined) {
- if (task.description != attrs.description) {
- changeList.push({
- fieldId: TaskBuiltinFieldId.DESCRIPTION,
- oldValue: task.description,
- newValue: attrs.description,
- });
- }
- }
-
- if (attrs.stage !== undefined) {
- const stage = task.stageList.find((item) => item.id == attrs.stage.id);
- if (stage) {
- changeList.push({
- fieldId: [TaskBuiltinFieldId.STAGE, stage.id].join("."),
- oldValue: stage.status,
- newValue: attrs.stage.status,
- });
- stage.status = attrs.stage.status;
- attrs.stageList = task.stageList;
- }
- }
-
- if (attrs.sql !== undefined) {
- if (task.sql != attrs.sql) {
- changeList.push({
- fieldId: TaskBuiltinFieldId.SQL,
- oldValue: task.sql,
- newValue: attrs.sql,
- });
- }
- }
-
- if (attrs.rollbackSql !== undefined) {
- if (task.rollbackSql != attrs.rollbackSql) {
- changeList.push({
- fieldId: TaskBuiltinFieldId.ROLLBACK_SQL,
- oldValue: task.rollbackSql,
- newValue: attrs.rollbackSql,
- });
- }
- }
-
- for (const fieldId in attrs.payload) {
- const oldValue = task.payload[fieldId];
- const newValue = attrs.payload[fieldId];
- if (!isEqual(oldValue, newValue)) {
- changeList.push({
- fieldId: fieldId,
- oldValue: task.payload[fieldId],
- newValue: attrs.payload[fieldId],
- });
- }
- }
-
- if (changeList.length) {
- const ts = Date.now();
- const updatedTask = task.update({ ...attrs, lastUpdatedTs: ts });
-
- const payload = {
- changeList,
- };
-
- schema.activities.create({
- createdTs: ts,
- lastUpdatedTs: ts,
- actionType: "bytebase.task.field.update",
- containerId: updatedTask.id,
- creatorId: attrs.updaterId,
- comment: attrs.comment,
- payload,
- workspaceId: WORKSPACE_ID,
- });
- return updatedTask;
- }
- return task;
- }
- return new Response(
- 404,
- {},
- { errors: "Task " + request.params.id + " not found" }
- );
- });
-
- // environment
- this.get("/environment", function (schema, request) {
- return schema.environments
- .where((environment) => {
- return environment.workspaceId == WORKSPACE_ID;
- })
- .sort((a, b) => a.order - b.order);
- });
-
- this.post("/environment", function (schema, request) {
- let order = 0;
- const list = schema.environments.where((environment) => {
- return environment.workspaceId == WORKSPACE_ID;
- });
- if (list.length > 0) {
- order = list.sort((a, b) => b.order - a.order).models[0].order + 1;
- }
- const newEnvironment = {
- ...this.normalizedRequestAttrs("environment"),
- workspaceId: WORKSPACE_ID,
- order,
- };
- return schema.environments.create(newEnvironment);
- });
-
- this.patch("/environment/batch", function (schema, request) {
- const attrs = this.normalizedRequestAttrs("batch-update");
- for (let i = 0; i < attrs.idList.length; i++) {
- const env = schema.environments.find(attrs.idList[i]);
- if (env) {
- const batch = {};
- for (let j = 0; j < attrs.fieldMaskList.length; j++) {
- batch[attrs.fieldMaskList[j]] = attrs.rowValueList[i][j];
- }
- env.update(batch);
- }
- }
- return schema.environments
- .where((environment) => {
- return environment.workspaceId == WORKSPACE_ID;
- })
- .sort((a, b) => a.order - b.order);
- });
+ configureTask(this);
- this.patch("/environment/:environmentId", function (schema, request) {
- const attrs = this.normalizedRequestAttrs("environment");
- return schema.environments.find(request.params.environmentId).update(attrs);
- });
-
- this.delete("/environment/:environmentId", function (schema, request) {
- return schema.environments.find(request.params.environmentId).destroy();
- });
+ // Environment
+ configureEnvironment(this);
// Instance
- this.get("/instance", function (schema, request) {
- return schema.instances.where((instance) => {
- return instance.workspaceId == WORKSPACE_ID;
- });
- });
-
- this.get("/instance/:id", function (schema, request) {
- const instance = schema.instances.find(request.params.id);
- if (instance) {
- return instance;
- }
- return new Response(
- 404,
- {},
- { errors: "Instance " + request.params.id + " not found" }
- );
- });
-
- this.post("/instance", function (schema, request) {
- const newInstance = {
- ...this.normalizedRequestAttrs("instance"),
- workspaceId: WORKSPACE_ID,
- };
- return schema.instances.create(newInstance);
- });
-
- this.patch("/instance/:instanceId", function (schema, request) {
- const attrs = this.normalizedRequestAttrs("instance");
- return schema.instances.find(request.params.instanceId).update(attrs);
- });
-
- this.delete("/instance/:instanceId", function (schema, request) {
- // Delete data source and database before instance itself.
- // Otherwise, the instanceId will be set to null.
- const dataSourceList = schema.dataSources.where({
- instanceId: request.params.instanceId,
- });
- dataSourceList.models.forEach((item) => item.destroy());
-
- const databaseList = schema.databases.where({
- instanceId: request.params.instanceId,
- });
- databaseList.models.forEach((item) => item.destroy());
-
- return schema.instances.find(request.params.instanceId).destroy();
- });
-
- // Data Source
- this.get("/datasource", function (schema, request) {
- const {
- queryParams: { instance: instanceId },
- } = request;
- return schema.dataSources.where((dataSource) => {
- return !instanceId || dataSource.instanceId == instanceId;
- });
- });
-
- this.get("/database/:databaseId/datasource", function (schema, request) {
- const database = schema.databases.find(request.params.databaseId);
- if (database) {
- return schema.dataSources.where((dataSource) => {
- return dataSource.databaseId == database.id;
- });
- }
- return new Response(
- 404,
- {},
- { errors: "Database " + request.params.databaseId + " not found" }
- );
- });
-
- this.get("/database/:databaseId/datasource/:id", function (schema, request) {
- const database = schema.databases.find(request.params.databaseId);
- if (database) {
- const dataSource = schema.dataSources.find(request.params.id);
- if (dataSource) {
- return dataSource;
- }
- return new Response(
- 404,
- {},
- { errors: "Data Source " + request.params.id + " not found" }
- );
- }
- return new Response(
- 404,
- {},
- { errors: "Database " + request.params.databaseId + " not found" }
- );
- });
-
- this.post("/database/:databaseId/datasource", function (schema, request) {
- const database = schema.databases.find(request.params.databaseId);
- if (database) {
- const newDataSource = {
- ...this.normalizedRequestAttrs("data-source"),
- };
- return schema.dataSources.create(newDataSource);
- }
- return new Response(
- 404,
- {},
- { errors: "Dnstance " + request.params.databaseId + " not found" }
- );
- });
-
- this.patch(
- "/database/:databaseId/datasource/:id",
- function (schema, request) {
- const database = schema.databases.find(request.params.databaseId);
- if (database) {
- const dataSource = schema.dataSources.find(request.params.id);
- if (!dataSource) {
- return new Response(
- 404,
- {},
- {
- errors: "Data source " + request.params.id + " not found",
- }
- );
- }
- const attrs = this.normalizedRequestAttrs("data-source");
- return dataSource.update(attrs);
- }
- return new Response(
- 404,
- {},
- { errors: "Database " + request.params.databaseId + " not found" }
- );
- }
- );
-
- this.delete(
- "/database/:databaseId/datasource/:id",
- function (schema, request) {
- const database = schema.instances.find(request.params.databaseId);
- if (database) {
- const dataSource = schema.dataSources.find(request.params.id);
- if (!dataSource) {
- return new Response(
- 404,
- {},
- {
- errors: "Data source " + request.params.id + " not found",
- }
- );
- }
- const dataSourceMemberList = schema.dataSourceMembers.where(
- (dataSourceMember) => {
- return dataSourceMember.dataSourceId == dataSource.id;
- }
- );
- dataSourceMemberList.models.forEach((member) => member.destroy());
- return dataSource.destroy();
- }
- return new Response(
- 404,
- {},
- { errors: "Database " + request.params.databaseId + " not found" }
- );
- }
- );
-
- // Data Source Member
- // Be careful to use :dataSourceId instead of :id otherwise this.normalizedRequestAttrs
- // would de-serialize to id, which would prevent auto increment id logic.
- this.post(
- "/database/:databaseId/datasource/:dataSourceId/member",
- function (schema, request) {
- const database = schema.databases.find(request.params.databaseId);
- if (database) {
- const dataSource = schema.dataSources.find(request.params.dataSourceId);
- if (dataSource) {
- const newDataSourceMember = {
- ...this.normalizedRequestAttrs("data-source-member"),
- dataSourceId: request.params.dataSourceId,
- };
- const newList = dataSource.memberList;
- const member = newList.find(
- (item) => item.principalId == newDataSourceMember.principalId
- );
- if (!member) {
- newList.push(newDataSourceMember);
- return dataSource.update({
- memberList: newList,
- });
- }
- return dataSource;
- }
- return new Response(
- 404,
- {},
- {
- errors: "Data source " + request.params.dataSourceId + " not found",
- }
- );
- }
- return new Response(
- 404,
- {},
- { errors: "Database " + request.params.databaseId + " not found" }
- );
- }
- );
-
- this.delete(
- "/database/:databaseId/datasource/:dataSourceId/member/:memberId",
- function (schema, request) {
- const database = schema.databases.find(request.params.databaseId);
- if (database) {
- const dataSource = schema.dataSources.find(request.params.dataSourceId);
- if (!dataSource) {
- return new Response(
- 404,
- {},
- {
- errors:
- "Data source " + request.params.dataSourceId + " not found",
- }
- );
- }
- const newList = dataSource.memberList;
- const index = newList.findIndex(
- (item) => item.principalId == request.params.memberId
- );
- if (index >= 0) {
- newList.splice(index, 1);
- return dataSource.update({
- memberList: newList,
- });
- }
- return dataSource;
- }
- return new Response(
- 404,
- {},
- { errors: "Database " + request.params.databaseId + " not found" }
- );
- }
- );
+ configureInstance(this);
// Database
- this.get("/database/:id", function (schema, request) {
- const database = schema.databases.find(request.params.id);
- if (database) {
- return database;
- }
- return new Response(
- 404,
- {},
- { errors: "Database " + request.params.id + " not found" }
- );
- });
-
- this.get("/database", function (schema, request) {
- const {
- queryParams: { environment: environmentId },
- } = request;
- const instanceIdList = schema.instances
- .where({ workspaceId: WORKSPACE_ID, environmentId })
- .models.map((instance) => instance.id);
- if (instanceIdList.length == 0) {
- return [];
- }
- return schema.databases
- .where((database) => {
- // If environment is specified, then we don't include the database representing all databases,
- // since the all databases is per instance.
- if (environmentId && database.name == ALL_DATABASE_NAME) {
- return false;
- }
- return instanceIdList.includes(database.instanceId);
- })
- .sort((a, b) =>
- a.name.localeCompare(b.name, undefined, { sensitivity: "base" })
- );
- });
-
- this.post("/database", function (schema, request) {
- const ts = Date.now();
- const { taskId, creatorId, ...attrs } = this.normalizedRequestAttrs(
- "database"
- );
- const newDatabase = {
- ...attrs,
- createdTs: ts,
- lastUpdatedTs: ts,
- syncStatus: "OK",
- lastSuccessfulSyncTs: ts,
- workspaceId: WORKSPACE_ID,
- };
+ configureDatabase(this);
- if (taskId) {
- schema.activities.create({
- createdTs: ts,
- lastUpdatedTs: ts,
- actionType: "bytebase.task",
- containerId: taskId,
- creatorId: creatorId,
- comment: `Created database ${newDatabase.name}`,
- workspaceId: WORKSPACE_ID,
- });
- }
- const createdDatabase = schema.databases.create(newDatabase);
- return createdDatabase;
- });
-
- this.patch("/database/:databaseId", function (schema, request) {
- const attrs = this.normalizedRequestAttrs("database-patch");
- const database = schema.databases.find(request.params.databaseId);
- if (database) {
- return database.update({ ...attrs, lastUpdatedTs: Date.now() });
- }
- return new Response(
- 404,
- {},
- { errors: "Database " + request.params.databaseId + " not found" }
- );
- });
-
- this.get("/instance/:instanceId/database", function (schema, request) {
- const instance = schema.instances.find(request.params.instanceId);
- if (instance) {
- return schema.databases
- .where((database) => {
- return database.instanceId == instance.id;
- })
- .sort((a, b) =>
- a.name.localeCompare(b.name, undefined, { sensitivity: "base" })
- );
- }
- return new Response(
- 404,
- {},
- { errors: "Instance " + request.params.instanceId + " not found" }
- );
- });
-
- // Bookmark
- this.get("/bookmark", function (schema, request) {
- const {
- queryParams: { userid: userId },
- } = request;
-
- return schema.bookmarks.where({
- workspaceId: WORKSPACE_ID,
- creatorId: userId,
- });
- });
-
- this.post("/bookmark", function (schema, request) {
- const newBookmark = {
- ...this.normalizedRequestAttrs("bookmark"),
- workspaceId: WORKSPACE_ID,
- };
- return schema.bookmarks.create(newBookmark);
- });
-
- this.patch("/bookmark/:bookmarkId", function (schema, request) {
- const attrs = this.normalizedRequestAttrs("bookmark");
- return schema.bookmarks.find(request.params.bookmarkId).update(attrs);
- });
-
- this.delete("/bookmark/:bookmarkId", function (schema, request) {
- return schema.bookmarks.find(request.params.bookmarkId).destroy();
- });
-
- // Activity
- this.get("/activity", function (schema, request) {
- const {
- queryParams: { containerid: containerId, type },
- } = request;
- return schema.activities.where((activity) => {
- if (activity.workspaceId != WORKSPACE_ID) {
- return false;
- }
-
- if (containerId && containerId != activity.containerId) {
- return false;
- }
-
- if (type && !activity.actionType.startsWith(type)) {
- return false;
- }
-
- return true;
- });
- });
-
- this.post("/activity", function (schema, request) {
- const ts = Date.now();
- const attrs = this.normalizedRequestAttrs("activity");
- const newActivity = {
- ...attrs,
- createdTs: ts,
- lastUpdatedTs: ts,
- actionType: "bytebase.task.comment.create",
- workspaceId: WORKSPACE_ID,
- };
- const createdActivity = schema.activities.create(newActivity);
- return createdActivity;
- });
-
- this.patch("/activity/:activityId", function (schema, request) {
- const attrs = this.normalizedRequestAttrs("activity-patch");
- const activity = schema.activities.find(request.params.activityId);
- if (activity) {
- return activity.update({ ...attrs, lastUpdatedTs: Date.now() });
- }
- return new Response(
- 404,
- {},
- { errors: "Activity " + request.params.activityId + " not found" }
- );
- });
-
- this.delete("/activity/:activityId", function (schema, request) {
- return schema.activities.find(request.params.activityId).destroy();
- });
+ // Data Source
+ configureDataSource(this);
}
diff --git a/frontend/src/miragejs/routes/instance.js b/frontend/src/miragejs/routes/instance.js
new file mode 100644
index 0000000000..2282234044
--- /dev/null
+++ b/frontend/src/miragejs/routes/instance.js
@@ -0,0 +1,50 @@
+import { WORKSPACE_ID } from "./index";
+
+export default function configurInstance(route) {
+ route.get("/instance", function (schema, request) {
+ return schema.instances.where((instance) => {
+ return instance.workspaceId == WORKSPACE_ID;
+ });
+ });
+
+ route.get("/instance/:id", function (schema, request) {
+ const instance = schema.instances.find(request.params.id);
+ if (instance) {
+ return instance;
+ }
+ return new Response(
+ 404,
+ {},
+ { errors: "Instance " + request.params.id + " not found" }
+ );
+ });
+
+ route.post("/instance", function (schema, request) {
+ const newInstance = {
+ ...this.normalizedRequestAttrs("instance"),
+ workspaceId: WORKSPACE_ID,
+ };
+ return schema.instances.create(newInstance);
+ });
+
+ route.patch("/instance/:instanceId", function (schema, request) {
+ const attrs = this.normalizedRequestAttrs("instance");
+ return schema.instances.find(request.params.instanceId).update(attrs);
+ });
+
+ route.delete("/instance/:instanceId", function (schema, request) {
+ // Delete data source and database before instance itself.
+ // Otherwise, the instanceId will be set to null.
+ const dataSourceList = schema.dataSources.where({
+ instanceId: request.params.instanceId,
+ });
+ dataSourceList.models.forEach((item) => item.destroy());
+
+ const databaseList = schema.databases.where({
+ instanceId: request.params.instanceId,
+ });
+ databaseList.models.forEach((item) => item.destroy());
+
+ return schema.instances.find(request.params.instanceId).destroy();
+ });
+}
diff --git a/frontend/src/miragejs/routes/roleMapping.js b/frontend/src/miragejs/routes/roleMapping.js
new file mode 100644
index 0000000000..de8ab4b43a
--- /dev/null
+++ b/frontend/src/miragejs/routes/roleMapping.js
@@ -0,0 +1,35 @@
+import { WORKSPACE_ID } from "./index";
+
+export default function configureRoleMapping(route) {
+ route.get("/rolemapping", function (schema, request) {
+ return schema.roleMappings.where((roleMapping) => {
+ return roleMapping.workspaceId == WORKSPACE_ID;
+ });
+ });
+
+ route.post("/rolemapping", function (schema, request) {
+ const ts = Date.now();
+ const attrs = {
+ ...this.normalizedRequestAttrs("role-mapping"),
+ workspaceId: WORKSPACE_ID,
+ };
+ const newRoleMapping = {
+ ...attrs,
+ createdTs: ts,
+ lastUpdatedTs: ts,
+ role: attrs.role,
+ updaterId: attrs.updaterId,
+ workspaceId: WORKSPACE_ID,
+ };
+ return schema.roleMappings.create(newRoleMapping);
+ });
+
+ route.patch("/rolemapping/:roleMappingId", function (schema, request) {
+ const attrs = this.normalizedRequestAttrs("role-mapping");
+ return schema.roleMappings.find(request.params.roleMappingId).update(attrs);
+ });
+
+ route.delete("/rolemapping/:roleMappingId", function (schema, request) {
+ return schema.roleMappings.find(request.params.roleMappingId).destroy();
+ });
+}
diff --git a/frontend/src/miragejs/routes/task.js b/frontend/src/miragejs/routes/task.js
new file mode 100644
index 0000000000..884cf728d3
--- /dev/null
+++ b/frontend/src/miragejs/routes/task.js
@@ -0,0 +1,181 @@
+import isEqual from "lodash-es/isEqual";
+import { WORKSPACE_ID } from "./index";
+import { TaskBuiltinFieldId } from "../../plugins";
+
+export default function configureTask(route) {
+ route.get("/task", function (schema, request) {
+ const {
+ queryParams: { userid: userId },
+ } = request;
+
+ if (userId) {
+ return schema.tasks.where((task) => {
+ return (
+ task.workspaceId == WORKSPACE_ID &&
+ (task.creatorId == userId ||
+ task.assigneeId == userId ||
+ task.subscriberIdList.includes(userId))
+ );
+ });
+ }
+ return schema.tasks.none();
+ });
+
+ route.get("/task/:id", function (schema, request) {
+ const task = schema.tasks.find(request.params.id);
+ if (task) {
+ return task;
+ }
+ return new Response(
+ 404,
+ {},
+ { errors: "Task " + request.params.id + " not found" }
+ );
+ });
+
+ route.post("/task", function (schema, request) {
+ const ts = Date.now();
+ const attrs = this.normalizedRequestAttrs("task");
+ const newTask = {
+ ...attrs,
+ createdTs: ts,
+ lastUpdatedTs: ts,
+ status: "OPEN",
+ workspaceId: WORKSPACE_ID,
+ };
+ const createdTask = schema.tasks.create(newTask);
+
+ schema.activities.create({
+ createdTs: ts,
+ lastUpdatedTs: ts,
+ actionType: "bytebase.task.create",
+ containerId: createdTask.id,
+ creatorId: attrs.creatorId,
+ comment: "",
+ workspaceId: WORKSPACE_ID,
+ });
+
+ return createdTask;
+ });
+
+ route.patch("/task/:taskId", function (schema, request) {
+ const attrs = this.normalizedRequestAttrs("task-patch");
+ const task = schema.tasks.find(request.params.taskId);
+ if (task) {
+ const changeList = [];
+
+ if (attrs.name) {
+ if (task.name != attrs.name) {
+ changeList.push({
+ fieldId: TaskBuiltinFieldId.NAME,
+ oldValue: task.name,
+ newValue: attrs.name,
+ });
+ }
+ }
+
+ if (attrs.status) {
+ if (task.status != attrs.status) {
+ changeList.push({
+ fieldId: TaskBuiltinFieldId.STATUS,
+ oldValue: task.status,
+ newValue: attrs.status,
+ });
+ }
+ }
+
+ if (attrs.assigneeId) {
+ if (task.assigneeId != attrs.assigneeId) {
+ changeList.push({
+ fieldId: TaskBuiltinFieldId.ASSIGNEE,
+ oldValue: task.assigneeId,
+ newValue: attrs.assigneeId,
+ });
+ }
+ }
+
+ // Empty string is valid
+ if (attrs.description !== undefined) {
+ if (task.description != attrs.description) {
+ changeList.push({
+ fieldId: TaskBuiltinFieldId.DESCRIPTION,
+ oldValue: task.description,
+ newValue: attrs.description,
+ });
+ }
+ }
+
+ if (attrs.stage !== undefined) {
+ const stage = task.stageList.find((item) => item.id == attrs.stage.id);
+ if (stage) {
+ changeList.push({
+ fieldId: [TaskBuiltinFieldId.STAGE, stage.id].join("."),
+ oldValue: stage.status,
+ newValue: attrs.stage.status,
+ });
+ stage.status = attrs.stage.status;
+ attrs.stageList = task.stageList;
+ }
+ }
+
+ if (attrs.sql !== undefined) {
+ if (task.sql != attrs.sql) {
+ changeList.push({
+ fieldId: TaskBuiltinFieldId.SQL,
+ oldValue: task.sql,
+ newValue: attrs.sql,
+ });
+ }
+ }
+
+ if (attrs.rollbackSql !== undefined) {
+ if (task.rollbackSql != attrs.rollbackSql) {
+ changeList.push({
+ fieldId: TaskBuiltinFieldId.ROLLBACK_SQL,
+ oldValue: task.rollbackSql,
+ newValue: attrs.rollbackSql,
+ });
+ }
+ }
+
+ for (const fieldId in attrs.payload) {
+ const oldValue = task.payload[fieldId];
+ const newValue = attrs.payload[fieldId];
+ if (!isEqual(oldValue, newValue)) {
+ changeList.push({
+ fieldId: fieldId,
+ oldValue: task.payload[fieldId],
+ newValue: attrs.payload[fieldId],
+ });
+ }
+ }
+
+ if (changeList.length) {
+ const ts = Date.now();
+ const updatedTask = task.update({ ...attrs, lastUpdatedTs: ts });
+
+ const payload = {
+ changeList,
+ };
+
+ schema.activities.create({
+ createdTs: ts,
+ lastUpdatedTs: ts,
+ actionType: "bytebase.task.field.update",
+ containerId: updatedTask.id,
+ creatorId: attrs.updaterId,
+ comment: attrs.comment,
+ payload,
+ workspaceId: WORKSPACE_ID,
+ });
+ return updatedTask;
+ }
+ return task;
+ }
+ return new Response(
+ 404,
+ {},
+ { errors: "Task " + request.params.id + " not found" }
+ );
+ });
+}
diff --git a/frontend/src/miragejs/routes/user.js b/frontend/src/miragejs/routes/user.js
new file mode 100644
index 0000000000..17709af1db
--- /dev/null
+++ b/frontend/src/miragejs/routes/user.js
@@ -0,0 +1,41 @@
+import { WORKSPACE_ID } from "./index";
+
+export default function configureUser(route) {
+ route.get("/user/:id");
+
+ route.get("/user");
+
+ route.post("/user", function (schema, request) {
+ const attrs = this.normalizedRequestAttrs("user");
+ const user = schema.users.findBy({ email: attrs.email });
+ if (user) {
+ return user;
+ }
+ return schema.users.create(attrs);
+ });
+
+ route.patch("/user/:userId", function (schema, request) {
+ const attrs = this.normalizedRequestAttrs("user");
+ return schema.users.find(request.params.userId).update(attrs);
+ });
+
+ route.get("/user/:userId/database", function (schema, request) {
+ return schema.databases.where((database) => {
+ const dataSourceList = schema.dataSources.where({
+ workspaceId: WORKSPACE_ID,
+ databaseId: database.id,
+ });
+
+ for (const dataSource of dataSourceList.models) {
+ if (
+ dataSource.memberList.find((item) => {
+ return item.principalId == request.params.userId;
+ })
+ ) {
+ return true;
+ }
+ }
+ return false;
+ });
+ });
+}