hola-server 1.0.10 → 2.0.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 (83) hide show
  1. package/README.md +196 -1
  2. package/core/array.js +79 -142
  3. package/core/bash.js +208 -259
  4. package/core/chart.js +26 -16
  5. package/core/cron.js +14 -3
  6. package/core/date.js +15 -44
  7. package/core/encrypt.js +19 -9
  8. package/core/file.js +42 -29
  9. package/core/lhs.js +32 -6
  10. package/core/meta.js +213 -289
  11. package/core/msg.js +20 -7
  12. package/core/number.js +105 -103
  13. package/core/obj.js +15 -12
  14. package/core/random.js +9 -6
  15. package/core/role.js +69 -77
  16. package/core/thread.js +12 -2
  17. package/core/type.js +300 -261
  18. package/core/url.js +20 -12
  19. package/core/validate.js +29 -26
  20. package/db/db.js +297 -227
  21. package/db/entity.js +631 -963
  22. package/db/gridfs.js +120 -166
  23. package/design/add_default_field_attr.md +56 -0
  24. package/http/context.js +22 -8
  25. package/http/cors.js +25 -8
  26. package/http/error.js +27 -9
  27. package/http/express.js +70 -41
  28. package/http/params.js +70 -42
  29. package/http/router.js +51 -40
  30. package/http/session.js +59 -36
  31. package/index.js +85 -9
  32. package/package.json +2 -2
  33. package/router/clone.js +28 -36
  34. package/router/create.js +21 -26
  35. package/router/delete.js +24 -28
  36. package/router/read.js +137 -123
  37. package/router/update.js +38 -56
  38. package/setting.js +22 -6
  39. package/skills/array.md +155 -0
  40. package/skills/bash.md +91 -0
  41. package/skills/chart.md +54 -0
  42. package/skills/code.md +422 -0
  43. package/skills/context.md +177 -0
  44. package/skills/date.md +58 -0
  45. package/skills/express.md +255 -0
  46. package/skills/file.md +60 -0
  47. package/skills/lhs.md +54 -0
  48. package/skills/meta.md +1023 -0
  49. package/skills/msg.md +30 -0
  50. package/skills/number.md +88 -0
  51. package/skills/obj.md +36 -0
  52. package/skills/params.md +206 -0
  53. package/skills/random.md +22 -0
  54. package/skills/role.md +59 -0
  55. package/skills/session.md +281 -0
  56. package/skills/storage.md +743 -0
  57. package/skills/thread.md +22 -0
  58. package/skills/type.md +547 -0
  59. package/skills/url.md +34 -0
  60. package/skills/validate.md +48 -0
  61. package/test/cleanup/close-db.js +5 -0
  62. package/test/core/array.js +226 -0
  63. package/test/core/chart.js +51 -0
  64. package/test/core/file.js +59 -0
  65. package/test/core/lhs.js +44 -0
  66. package/test/core/number.js +167 -12
  67. package/test/core/obj.js +47 -0
  68. package/test/core/random.js +24 -0
  69. package/test/core/thread.js +20 -0
  70. package/test/core/type.js +216 -0
  71. package/test/core/validate.js +67 -0
  72. package/test/db/db-ops.js +99 -0
  73. package/test/db/pipe_test.txt +0 -0
  74. package/test/db/test_case_design.md +528 -0
  75. package/test/db/test_db_class.js +613 -0
  76. package/test/db/test_entity_class.js +414 -0
  77. package/test/db/test_gridfs_class.js +234 -0
  78. package/test/entity/create.js +1 -1
  79. package/test/entity/delete-mixed.js +156 -0
  80. package/test/entity/ref-filter.js +63 -0
  81. package/tool/gen_i18n.js +55 -21
  82. package/test/crud/router.js +0 -99
  83. package/test/router/user.js +0 -17
@@ -0,0 +1,155 @@
1
+ # Array Utilities Skill
2
+
3
+ ## Overview
4
+
5
+ The `hola-server/core/array.js` module provides a set of utility functions for common array manipulations. This skill guide explains how to use these helpers effectively.
6
+
7
+ ## Importing
8
+
9
+ ```javascript
10
+ const {
11
+ shuffle, remove_element, pop_n, shift_n,
12
+ sum, avg, map_array_to_obj,
13
+ sort_desc, sort_asc, sort_by_key_seq,
14
+ combine, unique
15
+ } = require("hola-server/core/array");
16
+ ```
17
+
18
+ ## API Reference
19
+
20
+ ### 1. Modification & Extraction
21
+
22
+ #### `shuffle(arr)`
23
+ Randomly shuffles array elements in place.
24
+ - **param**: `arr` (Array) - The array to shuffle.
25
+ - **returns**: `void` (Modifies array in place).
26
+
27
+ ```javascript
28
+ const list = [1, 2, 3, 4, 5];
29
+ shuffle(list);
30
+ // list is now randomized, e.g., [3, 1, 5, 2, 4]
31
+ ```
32
+
33
+ #### `remove_element(array, field, value)`
34
+ Removes elements from an array of objects where the specified field matches the value.
35
+ - **param**: `array` (Object[]) - Array of objects.
36
+ - **param**: `field` (string) - Property name to match against.
37
+ - **param**: `value` (*) - Value to look for.
38
+ - **returns**: `void` (Modifies array in place).
39
+
40
+ ```javascript
41
+ const users = [{id: 1, name: 'Alice'}, {id: 2, name: 'Bob'}];
42
+ remove_element(users, 'id', 1);
43
+ // users is now [{id: 2, name: 'Bob'}]
44
+ ```
45
+
46
+ #### `pop_n(array, n)` / `shift_n(array, n)`
47
+ Extracts (removes and returns) `n` elements from the end (`pop_n`) or start (`shift_n`) of an array.
48
+ - **param**: `array` (Array) - Source array (modified).
49
+ - **param**: `n` (number) - Number of elements to extract.
50
+ - **returns**: `Array` or `undefined` - Array of extracted elements, or undefined if array became empty before extracting `n` items? N.B. actually it tries to remove up to n. If returns undefined if result is empty.
51
+
52
+ ```javascript
53
+ const nums = [1, 2, 3, 4, 5];
54
+ const lastTwo = pop_n(nums, 2); // [5, 4] (Note: items come off one by one)
55
+ // nums is now [1, 2, 3]
56
+
57
+ const firstTwo = shift_n(nums, 2); // [1, 2]
58
+ // nums is now [3]
59
+ ```
60
+
61
+ ### 2. Calculation
62
+
63
+ #### `sum(arr)`
64
+ Calculates the sum of an array of numbers.
65
+ - **param**: `arr` (number[]) - Array of numbers.
66
+ - **returns**: `number` - Sum rounded to 2 decimal places.
67
+
68
+ ```javascript
69
+ sum([10.5, 20.3, 5.111]); // Returns 35.91
70
+ ```
71
+
72
+ #### `avg(arr)`
73
+ Calculates the average of an array of numbers.
74
+ - **param**: `arr` (number[]) - Array of numbers.
75
+ - **returns**: `number` - Average rounded to 2 decimal places.
76
+
77
+ ```javascript
78
+ avg([10, 20, 30]); // Returns 20
79
+ ```
80
+
81
+ ### 3. Transformation & Combining
82
+
83
+ #### `map_array_to_obj(arr, key_attr, value_attr)`
84
+ Converts an array of objects into a single object (map) using specified properties for keys and values.
85
+ - **param**: `arr` (Object[]) - Array of objects.
86
+ - **param**: `key_attr` (string) - Property to use as object key.
87
+ - **param**: `value_attr` (string) - Property to use as object value.
88
+ - **returns**: `Object` - Key-value map.
89
+
90
+ ```javascript
91
+ const users = [
92
+ {id: 1, name: 'Alice', role: 'admin'},
93
+ {id: 2, name: 'Bob', role: 'user'}
94
+ ];
95
+ const userMap = map_array_to_obj(users, 'id', 'name');
96
+ // Returns: { 1: 'Alice', 2: 'Bob' }
97
+ ```
98
+
99
+ #### `combine(arr1, arr2)`
100
+ Creates a Cartesian product of two arrays of objects, merging properties.
101
+ - **param**: `arr1` (Object[]) - First array.
102
+ - **param**: `arr2` (Object[]) - Second array.
103
+ - **returns**: `Object[]` - Array of merged objects (length = arr1.length * arr2.length).
104
+
105
+ ```javascript
106
+ const colors = [{ color: 'red' }, { color: 'blue' }];
107
+ const sizes = [{ size: 'S' }, { size: 'M' }];
108
+ const products = combine(colors, sizes);
109
+ // Returns:
110
+ // [
111
+ // { color: 'red', size: 'S' }, { color: 'red', size: 'M' },
112
+ // { color: 'blue', size: 'S' }, { color: 'blue', size: 'M' }
113
+ // ]
114
+ ```
115
+
116
+ #### `unique(array)`
117
+ Removes duplicate values from an array. Handles primitives and objects (via stringification).
118
+ - **param**: `array` (Array) - Source array.
119
+ - **returns**: `Array` - New array with duplicates removed.
120
+
121
+ ```javascript
122
+ unique([1, 2, 2, 3]); // [1, 2, 3]
123
+ unique([{id:1}, {id:1}, {id:2}]); // [{id:1}, {id:2}]
124
+ ```
125
+
126
+ ### 4. Sorting
127
+
128
+ #### `sort_asc(arr, attr)` / `sort_desc(arr, attr)`
129
+ Sorts an array of objects by a numeric attribute in ascending or descending order.
130
+ - **param**: `arr` (Object[]) - Array to sort.
131
+ - **param**: `attr` (string) - Numeric property to sort by.
132
+ - **returns**: `Object[]` - Sorted array (same reference).
133
+
134
+ ```javascript
135
+ const items = [{ price: 10 }, { price: 5 }, { price: 20 }];
136
+ sort_asc(items, 'price'); // [{price: 5}, {price: 10}, {price: 20}]
137
+ ```
138
+
139
+ #### `sort_by_key_seq(arr, attr, keys)`
140
+ Sorts an array of objects based on a specific sequence of values for an attribute.
141
+ - **param**: `arr` (Object[]) - Array to sort.
142
+ - **param**: `attr` (string) - Property to inspect.
143
+ - **param**: `keys` (Array) - Ordered list of values defining the sort order.
144
+ - **returns**: `Object[]` - Sorted array.
145
+
146
+ ```javascript
147
+ const tasks = [
148
+ { status: 'done' },
149
+ { status: 'todo' },
150
+ { status: 'doing' }
151
+ ];
152
+ const order = ['todo', 'doing', 'done'];
153
+ sort_by_key_seq(tasks, 'status', order);
154
+ // Result: [{status: 'todo'}, {status: 'doing'}, {status: 'done'}]
155
+ ```
package/skills/bash.md ADDED
@@ -0,0 +1,91 @@
1
+ # Bash Utilities Skill
2
+
3
+ ## Overview
4
+
5
+ The `hola-server/core/bash.js` module provides comprehensive utilities for executing local and remote (SSH) commands, handling file transfers (SCP), and managing processes. It includes built-in logging and error handling.
6
+
7
+ ## Importing
8
+
9
+ ```javascript
10
+ const {
11
+ stop_process, scp, scpr,
12
+ run_script, run_script_extra, run_script_file,
13
+ run_simple_cmd, run_local_cmd, run_simple_local_cmd,
14
+ get_info, get_system_attributes,
15
+ read_key_value_line, read_obj_line
16
+ } = require("hola-server/core/bash");
17
+ ```
18
+
19
+ ## API Reference
20
+
21
+ ### 1. Remote Execution (SSH)
22
+
23
+ #### `run_script(host, script, log_extra)`
24
+ Executes a script/command on a remote host via SSH.
25
+ - **param**: `host` (Object) - Host info `{name, user, ip, port, auth}`.
26
+ - **param**: `script` (string) - Shell commands to run.
27
+ - **param**: `log_extra` (Object) - Optional context for logging.
28
+ - **returns**: `Promise<{stdout, err}>`
29
+
30
+ ```javascript
31
+ const host = { name: "web1", user: "root", ip: "10.0.0.1", port: 22, auth: "-i ~/.ssh/id_rsa" };
32
+ const { stdout, err } = await run_script(host, "ls -la /var/www");
33
+ ```
34
+
35
+ #### `run_simple_cmd(host, cmd, log_extra)`
36
+ Wrapper around `run_script` that returns trimmed stdout or null on error.
37
+ - **returns**: `Promise<string|null>`
38
+
39
+ ```javascript
40
+ const uptime = await run_simple_cmd(host, "uptime");
41
+ ```
42
+
43
+ #### `run_script_file(host, script_file, log_extra)`
44
+ Reads a local script file and executes it on the remote host.
45
+ - **param**: `script_file` (string) - Path to local script.
46
+ - **returns**: `Promise<{stdout, err}>`
47
+
48
+ #### `stop_process(host, process_name, stop_cmd, using_full, log_extra)`
49
+ Checks if a process is running on remote host and stops it if found.
50
+ - **param**: `process_name` (string) - Name to grep for.
51
+ - **param**: `stop_cmd` (string) - Command to kill the process (e.g. `pkill node`).
52
+ - **param**: `using_full` (boolean) - If true, uses `pgrep -f`.
53
+ - **returns**: `Promise<boolean>` - True if process was running.
54
+
55
+ ### 2. File Transfer (SCP)
56
+
57
+ #### `scp(host, remote_file, local_file, log_extra)`
58
+ Downloads a file from remote to local.
59
+ - **returns**: `Promise<{stdout, err}>`
60
+
61
+ #### `scpr(host, local_file, remote_file, log_extra)`
62
+ Uploads a file from local to remote.
63
+ - **returns**: `Promise<{stdout, err}>`
64
+
65
+ ### 3. Local Execution
66
+
67
+ #### `run_local_cmd(cmd, log_extra)`
68
+ Executes a command on the local machine.
69
+ - **returns**: `Promise<{stdout, err}>`
70
+
71
+ #### `run_simple_local_cmd(cmd, log_extra)`
72
+ Executes local command and returns trimmed stdout or null on error.
73
+ - **returns**: `Promise<string|null>`
74
+
75
+ ### 4. Output Parsing
76
+
77
+ #### `get_info(stdout, key, log_extra)`
78
+ Extracts values from stdout using regex pattern `key: value`.
79
+ - **returns**: `string[]` - Array of matched values.
80
+
81
+ #### `read_key_value_line(stdout, delimiter, lines, config, exclude_mode)`
82
+ Parses output lines into a key-value object.
83
+ - **param**: `delimiter` (string) - Separator (default ":").
84
+ - **param**: `lines` (number[]) - Specific line indices to read.
85
+ - **returns**: `Object`
86
+
87
+ #### `read_obj_line(stdout, keys, ignore, delimiter)`
88
+ Parses columnar output into array of objects.
89
+ - **param**: `keys` (string[]) - Property names for columns.
90
+ - **param**: `ignore` (number) - Number of header lines to skip.
91
+ - **returns**: `Object[]`
@@ -0,0 +1,54 @@
1
+ # Chart Utilities Skill
2
+
3
+ ## Overview
4
+
5
+ The `hola-server/core/chart.js` module provides helpers for manipulating chart data structures, typically represented as 2D arrays (array of rows, where the first row is headers).
6
+
7
+ ## Importing
8
+
9
+ ```javascript
10
+ const { set_chart_header, merge_chart_data } = require("hola-server/core/chart");
11
+ ```
12
+
13
+ ## API Reference
14
+
15
+ ### `set_chart_header(arr, prefix)`
16
+ Modifies chart headers in-place by prepending a prefix to all columns except the first one (usually the x-axis/category).
17
+ - **param**: `arr` (Array[]) - 2D array [header_row, data_row1, ...].
18
+ - **param**: `prefix` (string) - String to prepend to headers.
19
+ - **returns**: `void` (Modifies array in place).
20
+
21
+ ```javascript
22
+ const data = [
23
+ ["Date", "Value", "Cost"],
24
+ ["2023-01-01", 100, 50]
25
+ ];
26
+ set_chart_header(data, "US_");
27
+ // data[0] becomes ["Date", "US_Value", "US_Cost"]
28
+ ```
29
+
30
+ ### `merge_chart_data(arr1, arr2)`
31
+ Merges two chart data sets (2D arrays) by appending columns from `arr2` (excluding its first ID column) to `arr1`. Handles mismatched lengths by padding with empty strings.
32
+ - **param**: `arr1` (Array[]) - Base dataset (modified in place).
33
+ - **param**: `arr2` (Array[]) - Dataset to merge.
34
+ - **returns**: `void`.
35
+
36
+ ```javascript
37
+ const chart1 = [
38
+ ["Date", "A"],
39
+ ["Jan", 10]
40
+ ];
41
+ const chart2 = [
42
+ ["Date", "B"],
43
+ ["Jan", 20],
44
+ ["Feb", 25]
45
+ ];
46
+
47
+ merge_chart_data(chart1, chart2);
48
+ // chart1 becomes:
49
+ // [
50
+ // ["Date", "A", "B"],
51
+ // ["Jan", 10, 20],
52
+ // ["Feb", "", 25] <-- padded row for chart1
53
+ // ]
54
+ ```