@mongoosejs/studio 0.2.13 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (92) hide show
  1. package/backend/actions/ChatMessage/executeScript.js +5 -1
  2. package/backend/actions/ChatThread/createChatMessage.js +4 -1
  3. package/backend/actions/ChatThread/streamChatMessage.js +4 -2
  4. package/backend/actions/Dashboard/updateDashboard.js +2 -2
  5. package/backend/actions/Task/getTaskOverview.js +102 -0
  6. package/backend/actions/Task/getTasks.js +85 -45
  7. package/backend/actions/Task/index.js +1 -0
  8. package/eslint.config.js +4 -1
  9. package/frontend/public/app.js +25025 -762
  10. package/frontend/public/dark-theme.css +365 -0
  11. package/frontend/public/images/mongoose-studio.svg +4 -0
  12. package/frontend/public/index.html +21 -1
  13. package/frontend/public/style.css +5 -7
  14. package/frontend/public/theme-variables.css +294 -0
  15. package/frontend/public/tw.css +348 -213
  16. package/frontend/src/_util/dateRange.js +82 -0
  17. package/frontend/src/ace-editor/ace-editor.html +4 -0
  18. package/frontend/src/ace-editor/ace-editor.js +95 -0
  19. package/frontend/src/aceEditor.js +69 -0
  20. package/frontend/src/api.js +6 -0
  21. package/frontend/src/chat/chat-message/chat-message.html +1 -1
  22. package/frontend/src/chat/chat-message/chat-message.js +1 -1
  23. package/frontend/src/chat/chat-message-script/chat-message-script.html +54 -42
  24. package/frontend/src/chat/chat-message-script/chat-message-script.js +6 -55
  25. package/frontend/src/chat/chat.html +68 -39
  26. package/frontend/src/chat/chat.js +26 -2
  27. package/frontend/src/clone-document/clone-document.html +7 -2
  28. package/frontend/src/clone-document/clone-document.js +1 -8
  29. package/frontend/src/create-dashboard/create-dashboard.html +11 -6
  30. package/frontend/src/create-dashboard/create-dashboard.js +0 -7
  31. package/frontend/src/create-document/create-document.html +15 -9
  32. package/frontend/src/create-document/create-document.js +5 -12
  33. package/frontend/src/dashboard/dashboard.html +14 -12
  34. package/frontend/src/dashboard/dashboard.js +21 -4
  35. package/frontend/src/dashboard/edit-dashboard/edit-dashboard.html +13 -7
  36. package/frontend/src/dashboard/edit-dashboard/edit-dashboard.js +16 -23
  37. package/frontend/src/dashboard-result/dashboard-chart/dashboard-chart.html +19 -17
  38. package/frontend/src/dashboard-result/dashboard-chart/dashboard-chart.js +97 -2
  39. package/frontend/src/dashboard-result/dashboard-map/dashboard-map.js +27 -3
  40. package/frontend/src/dashboard-result/dashboard-result.html +3 -3
  41. package/frontend/src/dashboard-result/dashboard-result.js +3 -0
  42. package/frontend/src/dashboard-result/dashboard-table/dashboard-table.html +34 -0
  43. package/frontend/src/dashboard-result/dashboard-table/dashboard-table.js +37 -0
  44. package/frontend/src/dashboards/dashboards.html +101 -109
  45. package/frontend/src/dashboards/dashboards.js +25 -1
  46. package/frontend/src/detail-default/detail-default.html +2 -2
  47. package/frontend/src/detail-default/detail-default.js +24 -3
  48. package/frontend/src/document/confirm-changes/confirm-changes.html +1 -1
  49. package/frontend/src/document/confirm-delete/confirm-delete.html +1 -1
  50. package/frontend/src/document/document.css +1 -1
  51. package/frontend/src/document/document.html +28 -28
  52. package/frontend/src/document/execute-script/execute-script.html +20 -21
  53. package/frontend/src/document/execute-script/execute-script.js +1 -43
  54. package/frontend/src/document-details/document-details.css +4 -9
  55. package/frontend/src/document-details/document-details.html +34 -33
  56. package/frontend/src/document-details/document-details.js +2 -53
  57. package/frontend/src/document-details/document-property/document-property.html +12 -12
  58. package/frontend/src/edit-array/edit-array.html +7 -6
  59. package/frontend/src/edit-array/edit-array.js +10 -50
  60. package/frontend/src/edit-boolean/edit-boolean.html +12 -12
  61. package/frontend/src/edit-date/edit-date.html +2 -2
  62. package/frontend/src/edit-default/edit-default.html +1 -1
  63. package/frontend/src/edit-string/edit-string.html +3 -3
  64. package/frontend/src/edit-subdocument/edit-subdocument.html +5 -3
  65. package/frontend/src/edit-subdocument/edit-subdocument.js +1 -15
  66. package/frontend/src/export-query-results/export-query-results.html +3 -3
  67. package/frontend/src/json-node/json-node.html +3 -3
  68. package/frontend/src/list-json/json-node.html +1 -1
  69. package/frontend/src/models/document-search/document-search.html +3 -3
  70. package/frontend/src/models/model-switcher/model-switcher.html +53 -0
  71. package/frontend/src/models/model-switcher/model-switcher.js +123 -0
  72. package/frontend/src/models/models.css +3 -10
  73. package/frontend/src/models/models.html +146 -80
  74. package/frontend/src/models/models.js +116 -7
  75. package/frontend/src/navbar/navbar.html +157 -97
  76. package/frontend/src/navbar/navbar.js +31 -12
  77. package/frontend/src/routes.js +1 -1
  78. package/frontend/src/splash/splash.html +5 -5
  79. package/frontend/src/task-by-name/task-by-name.html +77 -7
  80. package/frontend/src/task-by-name/task-by-name.js +84 -9
  81. package/frontend/src/task-single/task-single.html +29 -29
  82. package/frontend/src/task-single/task-single.js +10 -10
  83. package/frontend/src/tasks/task-details/task-details.html +43 -43
  84. package/frontend/src/tasks/task-details/task-details.js +9 -3
  85. package/frontend/src/tasks/tasks.html +36 -35
  86. package/frontend/src/tasks/tasks.js +27 -143
  87. package/frontend/src/team/new-invitation/new-invitation.html +8 -8
  88. package/frontend/src/team/team.html +27 -27
  89. package/frontend/src/update-document/update-document.html +7 -2
  90. package/frontend/src/update-document/update-document.js +2 -11
  91. package/package.json +3 -1
  92. package/tailwind.config.js +75 -11
@@ -43,7 +43,11 @@ module.exports = ({ db, studioConnection }) => async function executeScript(para
43
43
  if (!db.Types) {
44
44
  db.Types = mongoose.Types;
45
45
  }
46
- const sandbox = { db, mongoose, console: {}, ObjectId: mongoose.Types.ObjectId };
46
+ const MongooseStudioChartColors = [
47
+ '#4e79a7', '#e15759', '#59a14f', '#9c755f',
48
+ '#f28e2b', '#b07aa1', '#76b7b2', '#edc948'
49
+ ];
50
+ const sandbox = { db, mongoose, console: {}, ObjectId: mongoose.Types.ObjectId, MongooseStudioChartColors };
47
51
 
48
52
  // Capture console logs
49
53
  sandbox.console.log = function() {
@@ -108,6 +108,7 @@ The following globals are available. Assume no other globals exist.
108
108
  - mongoose: the output of require('mongoose').
109
109
  - ObjectId: MongoDB ObjectId class from mongoose.Types.ObjectId
110
110
  - console: has a stubbed log() function that logs to the console and is accessible in the output.
111
+ - MongooseStudioChartColors: an array of 8 hex color strings for use as chart dataset colors.
111
112
 
112
113
  Keep scripts concise. Avoid unnecessary comments, error handling, and temporary variables.
113
114
 
@@ -129,10 +130,12 @@ Format output as Markdown, including code fences for any scripts the user reques
129
130
 
130
131
  Add a brief text description of what the script does.
131
132
 
132
- If the user's query is best answered with a chart, return a Chart.js 4 configuration as \`return { $chart: chartJSConfig };\`. Disable ChartJS animation by default unless user asks for it. Set responsive: true, maintainAspectRatio: false options unless the user explicitly asks.
133
+ If the user's query is best answered with a chart, return a Chart.js 4 configuration as \`return { $chart: chartJSConfig };\`. Disable ChartJS animation by default unless user asks for it. Set responsive: true, maintainAspectRatio: false options unless the user explicitly asks. Use MongooseStudioChartColors for dataset backgroundColor and borderColor by default. For line/bar charts, use MongooseStudioChartColors[i] as borderColor and MongooseStudioChartColors[i] + '33' as backgroundColor for each dataset. For pie/doughnut charts, use MongooseStudioChartColors.slice(0, data.length) as backgroundColor. Only use custom colors if the user explicitly requests specific colors.
133
134
 
134
135
  If the user\'s query is best answered by a map, return an object { $featureCollection } which contains a GeoJSON FeatureCollection
135
136
 
137
+ If the user's query is best answered by a table, return an object { $table: { columns: string[], rows: any[][] } }
138
+
136
139
  Example output:
137
140
 
138
141
  The following script counts the number of users which are not deleted.
@@ -123,7 +123,7 @@ const systemPrompt = `
123
123
 
124
124
  Do not write any imports or require() statements, that will cause the script to break.
125
125
 
126
- If the user approves the script, the script will run in the Node.js server in a sandboxed vm.createContext() call with only 1 global variable: db, which contains the Mongoose connection. The script return value will then send the response via JSON to the client. Be aware that the result of the query will be serialized to JSON before being displayed to the user. MAKE SURE TO RETURN A VALUE FROM THE SCRIPT.
126
+ If the user approves the script, the script will run in the Node.js server in a sandboxed vm.createContext() call with the following globals: db (the Mongoose connection), mongoose, ObjectId (mongoose.Types.ObjectId), console, and MongooseStudioChartColors (an array of 8 hex color strings for chart dataset colors). The script return value will then send the response via JSON to the client. Be aware that the result of the query will be serialized to JSON before being displayed to the user. MAKE SURE TO RETURN A VALUE FROM THE SCRIPT.
127
127
 
128
128
  Optimize scripts for readability first, followed by reliability, followed by performance. Avoid using the aggregation framework unless explicitly requested by the user. Use indexed fields in queries where possible.
129
129
 
@@ -139,10 +139,12 @@ const systemPrompt = `
139
139
 
140
140
  Add a brief text description of what the script does.
141
141
 
142
- If the user's query is best answered with a chart, return a Chart.js 4 configuration as \`return { $chart: chartJSConfig };\`. Disable ChartJS animation by default unless user asks for it. Set responsive: true, maintainAspectRatio: false options unless the user explicitly asks.
142
+ If the user's query is best answered with a chart, return a Chart.js 4 configuration as \`return { $chart: chartJSConfig };\`. Disable ChartJS animation by default unless user asks for it. Set responsive: true, maintainAspectRatio: false options unless the user explicitly asks. Use MongooseStudioChartColors for dataset backgroundColor and borderColor by default. For line/bar charts, use MongooseStudioChartColors[i] as borderColor and MongooseStudioChartColors[i] + '33' as backgroundColor for each dataset. For pie/doughnut charts, use MongooseStudioChartColors.slice(0, data.length) as backgroundColor. Only use custom colors if the user explicitly requests specific colors.
143
143
 
144
144
  If the user\'s query is best answered by a map, return an object { $featureCollection } which contains a GeoJSON FeatureCollection
145
145
 
146
+ If the user's query is best answered by a table, return an object { $table: { columns: string[], rows: any[][] } }
147
+
146
148
  Example output:
147
149
 
148
150
  The following script counts the number of users which are not deleted.
@@ -32,11 +32,11 @@ module.exports = ({ db }) => async function updateDashboard(params) {
32
32
 
33
33
  const updateObj = { code };
34
34
 
35
- if (title) {
35
+ if (title != null) {
36
36
  updateObj.title = title;
37
37
  }
38
38
 
39
- if (description) {
39
+ if (description != null) {
40
40
  updateObj.description = description;
41
41
  }
42
42
 
@@ -0,0 +1,102 @@
1
+ 'use strict';
2
+
3
+ const Archetype = require('archetype');
4
+ const escape = require('regexp.escape');
5
+
6
+ const GetTaskOverviewParams = new Archetype({
7
+ start: { $type: Date },
8
+ end: { $type: Date },
9
+ status: { $type: 'string' },
10
+ name: { $type: 'string' }
11
+ }).compile('GetTaskOverviewParams');
12
+
13
+ /** Statuses shown on the Task overview page. */
14
+ const OVERVIEW_STATUSES = ['pending', 'succeeded', 'failed', 'cancelled'];
15
+
16
+ function buildMatch(params) {
17
+ const { start, end, status, name } = params;
18
+ const match = {};
19
+ if (start != null && end != null) {
20
+ match.scheduledAt = { $gte: start, $lt: end };
21
+ } else if (start != null) {
22
+ match.scheduledAt = { $gte: start };
23
+ }
24
+ const statusVal = typeof status === 'string' ? status.trim() : status;
25
+ if (statusVal != null && statusVal !== '') {
26
+ match.status = statusVal;
27
+ } else {
28
+ match.status = { $in: ['pending', 'in_progress', 'succeeded', 'failed', 'cancelled', 'unknown'] };
29
+ }
30
+ if (name != null && name !== '') {
31
+ const nameStr = typeof name === 'string' ? name.trim() : String(name);
32
+ match.name = { $regex: escape(nameStr), $options: 'i' };
33
+ }
34
+ return match;
35
+ }
36
+
37
+ module.exports = ({ db }) => async function getTaskOverview(params) {
38
+ params = new GetTaskOverviewParams(params);
39
+ if (typeof params.status === 'string') params.status = params.status.trim();
40
+ if (typeof params.name === 'string') params.name = params.name.trim();
41
+ const { Task } = db.models;
42
+ const match = buildMatch(params);
43
+
44
+ const defaultCounts = OVERVIEW_STATUSES.map(s => ({ k: s, v: 0 }));
45
+
46
+ const pipeline = [
47
+ { $match: match },
48
+ {
49
+ $facet: {
50
+ statusCounts: [
51
+ { $group: { _id: { $ifNull: ['$status', 'unknown'] }, count: { $sum: 1 } } },
52
+ { $group: { _id: null, counts: { $push: { k: '$_id', v: '$count' } } } },
53
+ {
54
+ $project: {
55
+ statusCounts: {
56
+ $arrayToObject: {
57
+ $concatArrays: [{ $literal: defaultCounts }, '$counts']
58
+ }
59
+ }
60
+ }
61
+ },
62
+ { $replaceRoot: { newRoot: '$statusCounts' } }
63
+ ],
64
+ tasksByName: [
65
+ {
66
+ $group: {
67
+ _id: '$name',
68
+ totalCount: { $sum: 1 },
69
+ lastRun: { $max: '$scheduledAt' },
70
+ pending: { $sum: { $cond: [{ $eq: ['$status', 'pending'] }, 1, 0] } },
71
+ succeeded: { $sum: { $cond: [{ $eq: ['$status', 'succeeded'] }, 1, 0] } },
72
+ failed: { $sum: { $cond: [{ $eq: ['$status', 'failed'] }, 1, 0] } },
73
+ cancelled: { $sum: { $cond: [{ $eq: ['$status', 'cancelled'] }, 1, 0] } }
74
+ }
75
+ },
76
+ {
77
+ $project: {
78
+ _id: 0,
79
+ name: '$_id',
80
+ totalCount: 1,
81
+ lastRun: 1,
82
+ statusCounts: {
83
+ pending: '$pending',
84
+ succeeded: '$succeeded',
85
+ failed: '$failed',
86
+ cancelled: '$cancelled'
87
+ }
88
+ }
89
+ },
90
+ { $sort: { name: 1 } }
91
+ ]
92
+ }
93
+ }
94
+ ];
95
+
96
+ const [result] = await Task.aggregate(pipeline);
97
+
98
+ return {
99
+ statusCounts: result.statusCounts?.[0] ?? {},
100
+ tasksByName: result.tasksByName || []
101
+ };
102
+ };
@@ -1,65 +1,105 @@
1
1
  'use strict';
2
2
 
3
3
  const Archetype = require('archetype');
4
+ const escape = require('regexp.escape');
4
5
 
5
6
  const GetTasksParams = new Archetype({
6
- start: {
7
- $type: Date,
8
- $required: true
9
- },
10
- end: {
11
- $type: Date
12
- },
13
- status: {
14
- $type: 'string'
15
- },
16
- name: {
17
- $type: 'string'
18
- }
7
+ start: { $type: Date },
8
+ end: { $type: Date },
9
+ status: { $type: 'string' },
10
+ name: { $type: 'string' },
11
+ skip: { $type: 'number', $default: 0 },
12
+ limit: { $type: 'number', $default: 100 }
19
13
  }).compile('GetTasksParams');
20
14
 
21
- module.exports = ({ db }) => async function getTasks(params) {
22
- params = new GetTasksParams(params);
23
- const { start, end, status, name } = params;
24
- const { Task } = db.models;
15
+ const ALL_STATUSES = ['pending', 'in_progress', 'succeeded', 'failed', 'cancelled', 'unknown'];
16
+
17
+ /** Status keys for statusCounts (same shape as getTaskOverview). */
18
+ const STATUS_COUNT_KEYS = ['pending', 'succeeded', 'failed', 'cancelled'];
25
19
 
26
- const filter = {};
20
+ /** Max documents per request to avoid excessive memory and response size. */
21
+ const MAX_LIMIT = 2000;
27
22
 
28
- if (start && end) {
29
- filter.scheduledAt = { $gte: start, $lt: end };
30
- } else if (start) {
31
- filter.scheduledAt = { $gte: start };
23
+ function buildMatch(params) {
24
+ const { start, end, status, name } = params;
25
+ const match = {};
26
+ if (start != null && end != null) {
27
+ match.scheduledAt = { $gte: start, $lt: end };
28
+ } else if (start != null) {
29
+ match.scheduledAt = { $gte: start };
32
30
  }
33
- if (status) {
34
- filter.status = status;
31
+ const statusVal = typeof status === 'string' ? status.trim() : status;
32
+ if (statusVal != null && statusVal !== '') {
33
+ match.status = statusVal;
35
34
  } else {
36
- filter.status = { $in: ['pending', 'in_progress', 'succeeded', 'failed', 'cancelled', 'unknown'] };
35
+ match.status = { $in: ALL_STATUSES };
37
36
  }
38
- if (name) {
39
- filter.name = { $regex: name, $options: 'i' };
37
+ if (name != null && name !== '') {
38
+ const nameStr = typeof name === 'string' ? name.trim() : String(name);
39
+ match.name = { $regex: escape(nameStr), $options: 'i' };
40
40
  }
41
+ return match;
42
+ }
41
43
 
42
- const tasks = await Task.find(filter);
44
+ /** Projection done in aggregation: only fields needed by frontend, payload → parameters, _id → id. */
45
+ const TASK_PROJECT_STAGE = {
46
+ _id: 1,
47
+ id: '$_id',
48
+ name: 1,
49
+ status: 1,
50
+ scheduledAt: 1,
51
+ createdAt: 1,
52
+ startedAt: 1,
53
+ completedAt: 1,
54
+ error: 1,
55
+ parameters: '$payload'
56
+ };
57
+
58
+ module.exports = ({ db }) => async function getTasks(params) {
59
+ params = new GetTasksParams(params);
60
+ if (typeof params.status === 'string') params.status = params.status.trim();
61
+ if (typeof params.name === 'string') params.name = params.name.trim();
43
62
 
44
- // Define all possible statuses
45
- const allStatuses = ['pending', 'in_progress', 'succeeded', 'failed', 'cancelled', 'unknown'];
63
+ const skip = Math.max(0, Number(params.skip) || 0);
64
+ const limit = Math.min(MAX_LIMIT, Math.max(1, Number(params.limit) || 100));
65
+ const { Task } = db.models;
66
+ const match = buildMatch(params);
46
67
 
47
- // Initialize groupedTasks with all statuses
48
- const groupedTasks = allStatuses.reduce((groups, status) => {
49
- groups[status] = [];
50
- return groups;
51
- }, {});
68
+ const defaultCounts = STATUS_COUNT_KEYS.map(s => ({ k: s, v: 0 }));
52
69
 
53
- // Group tasks by status
54
- tasks.forEach(task => {
55
- const taskStatus = task.status || 'unknown';
56
- if (groupedTasks.hasOwnProperty(taskStatus)) {
57
- groupedTasks[taskStatus].push(task);
70
+ const pipeline = [
71
+ { $match: match },
72
+ {
73
+ $facet: {
74
+ tasks: [
75
+ { $sort: { scheduledAt: -1 } },
76
+ { $skip: skip },
77
+ { $limit: limit },
78
+ { $project: TASK_PROJECT_STAGE }
79
+ ],
80
+ count: [{ $count: 'total' }],
81
+ statusCounts: [
82
+ { $group: { _id: { $ifNull: ['$status', 'unknown'] }, count: { $sum: 1 } } },
83
+ { $group: { _id: null, counts: { $push: { k: '$_id', v: '$count' } } } },
84
+ {
85
+ $project: {
86
+ statusCounts: {
87
+ $arrayToObject: {
88
+ $concatArrays: [{ $literal: defaultCounts }, '$counts']
89
+ }
90
+ }
91
+ }
92
+ },
93
+ { $replaceRoot: { newRoot: '$statusCounts' } }
94
+ ]
95
+ }
58
96
  }
59
- });
97
+ ];
98
+
99
+ const [result] = await Task.aggregate(pipeline);
100
+ const tasks = result.tasks || [];
101
+ const numDocs = (result.count && result.count[0] && result.count[0].total) || 0;
102
+ const statusCounts = result.statusCounts?.[0] ?? {};
60
103
 
61
- return {
62
- tasks,
63
- groupedTasks
64
- };
104
+ return { tasks, numDocs, statusCounts };
65
105
  };
@@ -3,5 +3,6 @@
3
3
  exports.cancelTask = require('./cancelTask');
4
4
  exports.createTask = require('./createTask');
5
5
  exports.getTasks = require('./getTasks');
6
+ exports.getTaskOverview = require('./getTaskOverview');
6
7
  exports.rescheduleTask = require('./rescheduleTask');
7
8
  exports.runTask = require('./runTask');
package/eslint.config.js CHANGED
@@ -43,7 +43,10 @@ module.exports = defineConfig([
43
43
  TextDecoder: true,
44
44
  AbortController: true,
45
45
  clearTimeout: true,
46
- requestAnimationFrame: true
46
+ requestAnimationFrame: true,
47
+ localStorage: true,
48
+ getComputedStyle: true,
49
+ CustomEvent: true
47
50
  },
48
51
  sourceType: 'commonjs'
49
52
  },