@peers-app/peers-sdk 0.1.4

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 (234) hide show
  1. package/README.md +1 -0
  2. package/dist/context/data-context.d.ts +31 -0
  3. package/dist/context/data-context.js +56 -0
  4. package/dist/context/index.d.ts +3 -0
  5. package/dist/context/index.js +19 -0
  6. package/dist/context/user-context-singleton.d.ts +11 -0
  7. package/dist/context/user-context-singleton.js +121 -0
  8. package/dist/context/user-context.d.ts +55 -0
  9. package/dist/context/user-context.js +205 -0
  10. package/dist/data/assistants.d.ts +68 -0
  11. package/dist/data/assistants.js +64 -0
  12. package/dist/data/change-tracking.d.ts +219 -0
  13. package/dist/data/change-tracking.js +119 -0
  14. package/dist/data/channels.d.ts +29 -0
  15. package/dist/data/channels.js +25 -0
  16. package/dist/data/data-locks.d.ts +37 -0
  17. package/dist/data/data-locks.js +180 -0
  18. package/dist/data/data-locks.test.d.ts +1 -0
  19. package/dist/data/data-locks.test.js +456 -0
  20. package/dist/data/device-sync-info.d.ts +19 -0
  21. package/dist/data/device-sync-info.js +24 -0
  22. package/dist/data/devices.d.ts +51 -0
  23. package/dist/data/devices.js +36 -0
  24. package/dist/data/embeddings.d.ts +47 -0
  25. package/dist/data/embeddings.js +36 -0
  26. package/dist/data/files/file-read-stream.d.ts +27 -0
  27. package/dist/data/files/file-read-stream.js +195 -0
  28. package/dist/data/files/file-write-stream.d.ts +20 -0
  29. package/dist/data/files/file-write-stream.js +113 -0
  30. package/dist/data/files/file.types.d.ts +47 -0
  31. package/dist/data/files/file.types.js +55 -0
  32. package/dist/data/files/files.d.ts +28 -0
  33. package/dist/data/files/files.js +127 -0
  34. package/dist/data/files/files.test.d.ts +1 -0
  35. package/dist/data/files/files.test.js +728 -0
  36. package/dist/data/files/index.d.ts +4 -0
  37. package/dist/data/files/index.js +23 -0
  38. package/dist/data/group-member-roles.d.ts +9 -0
  39. package/dist/data/group-member-roles.js +25 -0
  40. package/dist/data/group-members.d.ts +39 -0
  41. package/dist/data/group-members.js +68 -0
  42. package/dist/data/group-members.test.d.ts +1 -0
  43. package/dist/data/group-members.test.js +287 -0
  44. package/dist/data/group-permissions.d.ts +8 -0
  45. package/dist/data/group-permissions.js +73 -0
  46. package/dist/data/group-share.d.ts +50 -0
  47. package/dist/data/group-share.js +196 -0
  48. package/dist/data/groups.d.ts +50 -0
  49. package/dist/data/groups.js +73 -0
  50. package/dist/data/groups.test.d.ts +1 -0
  51. package/dist/data/groups.test.js +153 -0
  52. package/dist/data/index.d.ts +31 -0
  53. package/dist/data/index.js +47 -0
  54. package/dist/data/knowledge/knowledge-frames.d.ts +34 -0
  55. package/dist/data/knowledge/knowledge-frames.js +34 -0
  56. package/dist/data/knowledge/knowledge-links.d.ts +30 -0
  57. package/dist/data/knowledge/knowledge-links.js +25 -0
  58. package/dist/data/knowledge/knowledge-values.d.ts +35 -0
  59. package/dist/data/knowledge/knowledge-values.js +35 -0
  60. package/dist/data/knowledge/peer-types.d.ts +112 -0
  61. package/dist/data/knowledge/peer-types.js +27 -0
  62. package/dist/data/knowledge/predicates.d.ts +34 -0
  63. package/dist/data/knowledge/predicates.js +27 -0
  64. package/dist/data/messages.d.ts +57 -0
  65. package/dist/data/messages.js +97 -0
  66. package/dist/data/orm/client-proxy.data-source.d.ts +27 -0
  67. package/dist/data/orm/client-proxy.data-source.js +65 -0
  68. package/dist/data/orm/cursor.d.ts +25 -0
  69. package/dist/data/orm/cursor.js +47 -0
  70. package/dist/data/orm/cursor.test.d.ts +1 -0
  71. package/dist/data/orm/cursor.test.js +315 -0
  72. package/dist/data/orm/data-query.d.ts +96 -0
  73. package/dist/data/orm/data-query.js +208 -0
  74. package/dist/data/orm/data-query.mongo.d.ts +17 -0
  75. package/dist/data/orm/data-query.mongo.js +267 -0
  76. package/dist/data/orm/data-query.mongo.test.d.ts +1 -0
  77. package/dist/data/orm/data-query.mongo.test.js +398 -0
  78. package/dist/data/orm/data-query.sqlite.d.ts +14 -0
  79. package/dist/data/orm/data-query.sqlite.js +297 -0
  80. package/dist/data/orm/data-query.sqlite.test.d.ts +1 -0
  81. package/dist/data/orm/data-query.sqlite.test.js +377 -0
  82. package/dist/data/orm/data-query.test.d.ts +1 -0
  83. package/dist/data/orm/data-query.test.js +553 -0
  84. package/dist/data/orm/decorators.d.ts +6 -0
  85. package/dist/data/orm/decorators.js +21 -0
  86. package/dist/data/orm/dependency-injection.test.d.ts +1 -0
  87. package/dist/data/orm/dependency-injection.test.js +171 -0
  88. package/dist/data/orm/doc.d.ts +26 -0
  89. package/dist/data/orm/doc.js +124 -0
  90. package/dist/data/orm/event-registry.d.ts +24 -0
  91. package/dist/data/orm/event-registry.js +40 -0
  92. package/dist/data/orm/event-registry.test.d.ts +1 -0
  93. package/dist/data/orm/event-registry.test.js +44 -0
  94. package/dist/data/orm/factory.d.ts +8 -0
  95. package/dist/data/orm/factory.js +147 -0
  96. package/dist/data/orm/index.d.ts +16 -0
  97. package/dist/data/orm/index.js +32 -0
  98. package/dist/data/orm/multi-cursors.d.ts +11 -0
  99. package/dist/data/orm/multi-cursors.js +146 -0
  100. package/dist/data/orm/multi-cursors.test.d.ts +1 -0
  101. package/dist/data/orm/multi-cursors.test.js +455 -0
  102. package/dist/data/orm/sql-db.d.ts +6 -0
  103. package/dist/data/orm/sql-db.js +2 -0
  104. package/dist/data/orm/sql.data-source.d.ts +38 -0
  105. package/dist/data/orm/sql.data-source.js +379 -0
  106. package/dist/data/orm/sql.data-source.test.d.ts +1 -0
  107. package/dist/data/orm/sql.data-source.test.js +406 -0
  108. package/dist/data/orm/subscribable.data-source.d.ts +25 -0
  109. package/dist/data/orm/subscribable.data-source.js +72 -0
  110. package/dist/data/orm/table-container-events.test.d.ts +1 -0
  111. package/dist/data/orm/table-container-events.test.js +93 -0
  112. package/dist/data/orm/table-container.d.ts +39 -0
  113. package/dist/data/orm/table-container.js +96 -0
  114. package/dist/data/orm/table-definitions.system.d.ts +9 -0
  115. package/dist/data/orm/table-definitions.system.js +29 -0
  116. package/dist/data/orm/table-definitions.type.d.ts +19 -0
  117. package/dist/data/orm/table-definitions.type.js +2 -0
  118. package/dist/data/orm/table-dependencies.d.ts +32 -0
  119. package/dist/data/orm/table-dependencies.js +2 -0
  120. package/dist/data/orm/table.d.ts +42 -0
  121. package/dist/data/orm/table.event-source.test.d.ts +1 -0
  122. package/dist/data/orm/table.event-source.test.js +341 -0
  123. package/dist/data/orm/table.js +244 -0
  124. package/dist/data/orm/types.d.ts +20 -0
  125. package/dist/data/orm/types.js +115 -0
  126. package/dist/data/orm/types.test.d.ts +1 -0
  127. package/dist/data/orm/types.test.js +71 -0
  128. package/dist/data/package-permissions.d.ts +7 -0
  129. package/dist/data/package-permissions.js +18 -0
  130. package/dist/data/packages.d.ts +92 -0
  131. package/dist/data/packages.js +90 -0
  132. package/dist/data/peer-events/peer-event-handlers.d.ts +21 -0
  133. package/dist/data/peer-events/peer-event-handlers.js +28 -0
  134. package/dist/data/peer-events/peer-event-types.d.ts +119 -0
  135. package/dist/data/peer-events/peer-event-types.js +29 -0
  136. package/dist/data/peer-events/peer-events.d.ts +41 -0
  137. package/dist/data/peer-events/peer-events.js +102 -0
  138. package/dist/data/persistent-vars.d.ts +87 -0
  139. package/dist/data/persistent-vars.js +230 -0
  140. package/dist/data/tool-tests.d.ts +37 -0
  141. package/dist/data/tool-tests.js +27 -0
  142. package/dist/data/tools.d.ts +358 -0
  143. package/dist/data/tools.js +48 -0
  144. package/dist/data/user-permissions.d.ts +15 -0
  145. package/dist/data/user-permissions.js +39 -0
  146. package/dist/data/user-permissions.test.d.ts +1 -0
  147. package/dist/data/user-permissions.test.js +252 -0
  148. package/dist/data/users.d.ts +38 -0
  149. package/dist/data/users.js +73 -0
  150. package/dist/data/workflow-logs.d.ts +106 -0
  151. package/dist/data/workflow-logs.js +67 -0
  152. package/dist/data/workflow-runs.d.ts +103 -0
  153. package/dist/data/workflow-runs.js +313 -0
  154. package/dist/data/workflows.d.ts +16 -0
  155. package/dist/data/workflows.js +21 -0
  156. package/dist/device/connection.d.ts +41 -0
  157. package/dist/device/connection.js +249 -0
  158. package/dist/device/connection.test.d.ts +1 -0
  159. package/dist/device/connection.test.js +292 -0
  160. package/dist/device/device-election.d.ts +36 -0
  161. package/dist/device/device-election.js +137 -0
  162. package/dist/device/device.d.ts +22 -0
  163. package/dist/device/device.js +110 -0
  164. package/dist/device/device.test.d.ts +1 -0
  165. package/dist/device/device.test.js +203 -0
  166. package/dist/device/get-trust-level.d.ts +3 -0
  167. package/dist/device/get-trust-level.js +87 -0
  168. package/dist/device/socket.type.d.ts +20 -0
  169. package/dist/device/socket.type.js +15 -0
  170. package/dist/device/streamed-socket.d.ts +27 -0
  171. package/dist/device/streamed-socket.js +154 -0
  172. package/dist/device/streamed-socket.test.d.ts +1 -0
  173. package/dist/device/streamed-socket.test.js +44 -0
  174. package/dist/events.d.ts +35 -0
  175. package/dist/events.js +128 -0
  176. package/dist/index.d.ts +33 -0
  177. package/dist/index.js +50 -0
  178. package/dist/keys.d.ts +51 -0
  179. package/dist/keys.js +234 -0
  180. package/dist/keys.test.d.ts +1 -0
  181. package/dist/keys.test.js +215 -0
  182. package/dist/mentions.d.ts +9 -0
  183. package/dist/mentions.js +46 -0
  184. package/dist/observable.d.ts +19 -0
  185. package/dist/observable.js +112 -0
  186. package/dist/observable.test.d.ts +1 -0
  187. package/dist/observable.test.js +183 -0
  188. package/dist/package-loader/get-require.d.ts +10 -0
  189. package/dist/package-loader/get-require.js +31 -0
  190. package/dist/package-loader/index.d.ts +1 -0
  191. package/dist/package-loader/index.js +17 -0
  192. package/dist/package-loader/package-loader.d.ts +16 -0
  193. package/dist/package-loader/package-loader.js +102 -0
  194. package/dist/peers-ui/peers-ui.d.ts +15 -0
  195. package/dist/peers-ui/peers-ui.js +23 -0
  196. package/dist/peers-ui/peers-ui.types.d.ts +35 -0
  197. package/dist/peers-ui/peers-ui.types.js +3 -0
  198. package/dist/rpc-types.d.ts +45 -0
  199. package/dist/rpc-types.js +47 -0
  200. package/dist/serial-json.d.ts +5 -0
  201. package/dist/serial-json.js +186 -0
  202. package/dist/serial-json.test.d.ts +1 -0
  203. package/dist/serial-json.test.js +86 -0
  204. package/dist/system-ids.d.ts +6 -0
  205. package/dist/system-ids.js +10 -0
  206. package/dist/tools/index.d.ts +1 -0
  207. package/dist/tools/index.js +17 -0
  208. package/dist/tools/tools-factory.d.ts +5 -0
  209. package/dist/tools/tools-factory.js +34 -0
  210. package/dist/types/app-nav.d.ts +18 -0
  211. package/dist/types/app-nav.js +10 -0
  212. package/dist/types/assistant-runner-args.d.ts +9 -0
  213. package/dist/types/assistant-runner-args.js +2 -0
  214. package/dist/types/field-type.d.ts +37 -0
  215. package/dist/types/field-type.js +26 -0
  216. package/dist/types/peer-device.d.ts +40 -0
  217. package/dist/types/peer-device.js +14 -0
  218. package/dist/types/peers-package.d.ts +23 -0
  219. package/dist/types/peers-package.js +2 -0
  220. package/dist/types/workflow-logger.d.ts +2 -0
  221. package/dist/types/workflow-logger.js +2 -0
  222. package/dist/types/workflow-run-context.d.ts +12 -0
  223. package/dist/types/workflow-run-context.js +2 -0
  224. package/dist/types/workflow.d.ts +72 -0
  225. package/dist/types/workflow.js +24 -0
  226. package/dist/types/zod-types.d.ts +7 -0
  227. package/dist/types/zod-types.js +12 -0
  228. package/dist/users.query.d.ts +13 -0
  229. package/dist/users.query.js +134 -0
  230. package/dist/utils.d.ts +39 -0
  231. package/dist/utils.js +240 -0
  232. package/dist/utils.test.d.ts +1 -0
  233. package/dist/utils.test.js +140 -0
  234. package/package.json +50 -0
@@ -0,0 +1,267 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.dataFilterToMongoQuery = dataFilterToMongoQuery;
4
+ exports.sortByToMongoSort = sortByToMongoSort;
5
+ exports.paginationToMongoSkipLimit = paginationToMongoSkipLimit;
6
+ const lodash_1 = require("lodash");
7
+ /**
8
+ * Converts a DataFilter to a MongoDB query object
9
+ */
10
+ function dataFilterToMongoQuery(filter) {
11
+ if (isScalarValue(filter)) {
12
+ return filter;
13
+ }
14
+ if ((0, lodash_1.isArray)(filter)) {
15
+ return handleOrArray(filter);
16
+ }
17
+ return handleFilterObject(filter);
18
+ }
19
+ /**
20
+ * Handles OR arrays - converts to MongoDB $or syntax
21
+ */
22
+ function handleOrArray(filters) {
23
+ if (filters.length === 0) {
24
+ return {}; // Empty filter matches everything in MongoDB
25
+ }
26
+ if (filters.length === 1) {
27
+ return dataFilterToMongoQuery(filters[0]);
28
+ }
29
+ return { $or: filters.map(f => dataFilterToMongoQuery(f)) };
30
+ }
31
+ /**
32
+ * Handles filter objects - processes each field
33
+ */
34
+ function handleFilterObject(filter) {
35
+ const mongoFilter = {};
36
+ for (const [fieldName, value] of Object.entries(filter)) {
37
+ const result = handleFilterField(fieldName, value);
38
+ // Merge the result into mongoFilter
39
+ if (typeof result === 'object' && result !== null && !(0, lodash_1.isArray)(result)) {
40
+ Object.assign(mongoFilter, result);
41
+ }
42
+ else {
43
+ mongoFilter[fieldName] = result;
44
+ }
45
+ }
46
+ return mongoFilter;
47
+ }
48
+ /**
49
+ * Handles individual filter fields
50
+ */
51
+ function handleFilterField(fieldName, value) {
52
+ if (fieldName === '$or') {
53
+ return { $or: value.map((v) => dataFilterToMongoQuery(v)) };
54
+ }
55
+ if (fieldName === '$and') {
56
+ if (value.length === 0) {
57
+ return {}; // Empty $and matches everything
58
+ }
59
+ return { $and: value.map((v) => dataFilterToMongoQuery(v)) };
60
+ }
61
+ if (fieldName === '$matchWords') {
62
+ return handleMatchWordsOperator(value);
63
+ }
64
+ if (fieldName === '$substr') {
65
+ return handleSubstrOperator(value);
66
+ }
67
+ if (fieldName === '$expr') {
68
+ return handleExprOperator(value);
69
+ }
70
+ return handleFieldCondition(fieldName, value);
71
+ }
72
+ /**
73
+ * Handles regular field conditions
74
+ */
75
+ function handleFieldCondition(fieldName, value) {
76
+ if (value === null || value === undefined) {
77
+ return { [fieldName]: null };
78
+ }
79
+ if ((0, lodash_1.isArray)(value)) {
80
+ throw new Error(`Invalid filter: Arrays are not allowed as direct field values. Use $in instead: { ${fieldName}: { $in: [...] } }`);
81
+ }
82
+ if (typeof value === 'object' && !(0, lodash_1.isDate)(value)) {
83
+ return { [fieldName]: handleObjectValue(value) };
84
+ }
85
+ return { [fieldName]: value };
86
+ }
87
+ /**
88
+ * Handles object values with operators
89
+ */
90
+ function handleObjectValue(value) {
91
+ const mongoValue = {};
92
+ for (const [operator, operatorValue] of Object.entries(value)) {
93
+ if (operator.startsWith('$')) {
94
+ mongoValue[operator] = handleOperatorValue(operator, operatorValue);
95
+ }
96
+ else {
97
+ throw new Error(`Invalid operator: ${operator}. Operators must start with $`);
98
+ }
99
+ }
100
+ return mongoValue;
101
+ }
102
+ /**
103
+ * Handles specific operator values
104
+ */
105
+ function handleOperatorValue(operator, value) {
106
+ switch (operator) {
107
+ case '$eq':
108
+ case '$ne':
109
+ case '$gt':
110
+ case '$gte':
111
+ case '$lt':
112
+ case '$lte':
113
+ case '$in':
114
+ case '$nin':
115
+ case '$exists':
116
+ // These operators work directly in MongoDB
117
+ return value;
118
+ case '$matchWords':
119
+ throw new Error('$matchWords should be handled at field level, not as nested operator');
120
+ case '$substr':
121
+ throw new Error('$substr should be handled at field level, not as nested operator');
122
+ case '$expr':
123
+ throw new Error('$expr should be handled at field level, not as nested operator');
124
+ default:
125
+ throw new Error(`Unknown operator: ${operator}`);
126
+ }
127
+ }
128
+ /**
129
+ * Converts $matchWords to MongoDB text search or regex
130
+ */
131
+ function handleMatchWordsOperator(searchText) {
132
+ if (typeof searchText !== 'string') {
133
+ throw new Error('$matchWords value must be a string');
134
+ }
135
+ const words = searchText.split(' ').filter(word => word.trim());
136
+ if (words.length === 0) {
137
+ return {}; // Empty search matches everything
138
+ }
139
+ // Option 1: Use MongoDB text search (requires text index)
140
+ // return { $text: { $search: searchText } };
141
+ // Option 2: Use regex for each word (more compatible, works without indexes)
142
+ if (words.length === 1) {
143
+ return { $regex: escapeRegex(words[0]), $options: 'i' };
144
+ }
145
+ // For multiple words, create AND condition with regex for each word
146
+ const regexConditions = words.map(word => ({
147
+ $regex: escapeRegex(word),
148
+ $options: 'i'
149
+ }));
150
+ // This creates a field-level $and, which isn't standard MongoDB
151
+ // We need to return this to be handled at a higher level
152
+ throw new Error('$matchWords with multiple words requires field context - should be handled differently');
153
+ }
154
+ /**
155
+ * Converts $substr to MongoDB $expr with $substr
156
+ */
157
+ function handleSubstrOperator(args) {
158
+ if (!(0, lodash_1.isArray)(args) || args.length !== 3) {
159
+ throw new Error('$substr requires exactly 3 arguments: [string, start, length]');
160
+ }
161
+ const [str, start, length] = args;
162
+ // Convert field references (strings starting with $)
163
+ const mongoStr = typeof str === 'string' && str.startsWith('$')
164
+ ? str // Keep MongoDB field reference format
165
+ : str;
166
+ return {
167
+ $expr: {
168
+ $substr: [mongoStr, start, length]
169
+ }
170
+ };
171
+ }
172
+ /**
173
+ * Converts SQL-style $expr to MongoDB $expr format
174
+ */
175
+ function handleExprOperator(exprArray) {
176
+ if (!(0, lodash_1.isArray)(exprArray) || exprArray.length !== 2) {
177
+ throw new Error('$expr requires exactly 2 arguments: [leftSide, rightSide]');
178
+ }
179
+ const [leftSide, rightSide] = exprArray;
180
+ // Convert left side (usually a function like $substr)
181
+ const mongoLeft = convertExpressionPart(leftSide);
182
+ // Convert right side (usually a comparison)
183
+ const mongoRight = convertExpressionPart(rightSide);
184
+ // Determine the operation from rightSide
185
+ if (typeof rightSide === 'object' && rightSide !== null) {
186
+ const operators = Object.keys(rightSide).filter(k => k.startsWith('$'));
187
+ if (operators.length === 1) {
188
+ const op = operators[0];
189
+ return {
190
+ $expr: {
191
+ [op]: [mongoLeft, rightSide[op]]
192
+ }
193
+ };
194
+ }
195
+ }
196
+ // Default to equality comparison
197
+ return {
198
+ $expr: {
199
+ $eq: [mongoLeft, mongoRight]
200
+ }
201
+ };
202
+ }
203
+ /**
204
+ * Converts parts of expressions to MongoDB format
205
+ */
206
+ function convertExpressionPart(part) {
207
+ if (typeof part === 'object' && part !== null && !(0, lodash_1.isArray)(part)) {
208
+ // Handle function calls like { $substr: [...] }
209
+ const keys = Object.keys(part);
210
+ if (keys.length === 1 && keys[0].startsWith('$')) {
211
+ const func = keys[0];
212
+ const args = part[func];
213
+ if (func === '$substr' && (0, lodash_1.isArray)(args) && args.length === 3) {
214
+ const [str, start, length] = args;
215
+ const mongoStr = typeof str === 'string' && str.startsWith('$') ? str : str;
216
+ return { $substr: [mongoStr, start, length] };
217
+ }
218
+ }
219
+ }
220
+ return part;
221
+ }
222
+ /**
223
+ * Escapes special regex characters
224
+ */
225
+ function escapeRegex(text) {
226
+ return text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
227
+ }
228
+ /**
229
+ * Checks if a value is a scalar (primitive) value
230
+ */
231
+ function isScalarValue(value) {
232
+ return (typeof value !== 'object' ||
233
+ (0, lodash_1.isDate)(value) ||
234
+ value === null);
235
+ }
236
+ /**
237
+ * Converts DataQuery sort format to MongoDB sort format
238
+ */
239
+ function sortByToMongoSort(sortBy) {
240
+ if (!sortBy || sortBy.length === 0) {
241
+ return {};
242
+ }
243
+ const mongoSort = {};
244
+ for (const field of sortBy) {
245
+ if (field.startsWith('-')) {
246
+ mongoSort[field.substring(1)] = -1; // Descending
247
+ }
248
+ else {
249
+ mongoSort[field] = 1; // Ascending
250
+ }
251
+ }
252
+ return mongoSort;
253
+ }
254
+ function paginationToMongoSkipLimit(page, pageSize) {
255
+ const result = {};
256
+ if (pageSize) {
257
+ result.limit = Math.round(pageSize);
258
+ }
259
+ if (page && pageSize) {
260
+ const pageNum = Math.round(page);
261
+ const skip = Math.round(pageSize * (pageNum - 1));
262
+ if (skip > 0) {
263
+ result.skip = skip;
264
+ }
265
+ }
266
+ return result;
267
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,398 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const data_query_mongo_1 = require("./data-query.mongo");
4
+ describe('dataFilterToMongoQuery', () => {
5
+ describe('Scalar values', () => {
6
+ it('should handle null values', () => {
7
+ expect((0, data_query_mongo_1.dataFilterToMongoQuery)(null)).toBe(null);
8
+ });
9
+ it('should handle undefined values', () => {
10
+ expect((0, data_query_mongo_1.dataFilterToMongoQuery)(undefined)).toBe(undefined);
11
+ });
12
+ it('should handle primitive values', () => {
13
+ expect((0, data_query_mongo_1.dataFilterToMongoQuery)(42)).toBe(42);
14
+ expect((0, data_query_mongo_1.dataFilterToMongoQuery)('test')).toBe('test');
15
+ expect((0, data_query_mongo_1.dataFilterToMongoQuery)(true)).toBe(true);
16
+ });
17
+ it('should handle Date objects', () => {
18
+ const date = new Date('2023-01-01');
19
+ expect((0, data_query_mongo_1.dataFilterToMongoQuery)(date)).toBe(date);
20
+ });
21
+ });
22
+ describe('Empty filters', () => {
23
+ it('should handle empty object filters', () => {
24
+ expect((0, data_query_mongo_1.dataFilterToMongoQuery)({})).toEqual({});
25
+ });
26
+ it('should handle empty array filters', () => {
27
+ expect((0, data_query_mongo_1.dataFilterToMongoQuery)([])).toEqual({});
28
+ });
29
+ });
30
+ describe('Simple field conditions', () => {
31
+ it('should handle basic field equality', () => {
32
+ const filter = { name: 'John' };
33
+ expect((0, data_query_mongo_1.dataFilterToMongoQuery)(filter)).toEqual({ name: 'John' });
34
+ });
35
+ it('should handle multiple fields', () => {
36
+ const filter = { name: 'John', age: 30 };
37
+ expect((0, data_query_mongo_1.dataFilterToMongoQuery)(filter)).toEqual({ name: 'John', age: 30 });
38
+ });
39
+ it('should handle null field values', () => {
40
+ const filter = { name: null };
41
+ expect((0, data_query_mongo_1.dataFilterToMongoQuery)(filter)).toEqual({ name: null });
42
+ });
43
+ it('should handle undefined field values', () => {
44
+ const filter = { name: undefined };
45
+ expect((0, data_query_mongo_1.dataFilterToMongoQuery)(filter)).toEqual({ name: null });
46
+ });
47
+ it('should throw error for direct array usage', () => {
48
+ const filter = { names: ['John', 'Jane'] };
49
+ expect(() => (0, data_query_mongo_1.dataFilterToMongoQuery)(filter)).toThrow('Invalid filter: Arrays are not allowed as direct field values. Use $in instead');
50
+ });
51
+ });
52
+ describe('MongoDB operators (direct mapping)', () => {
53
+ describe('$eq operator', () => {
54
+ it('should handle $eq operator', () => {
55
+ const filter = { age: { $eq: 30 } };
56
+ expect((0, data_query_mongo_1.dataFilterToMongoQuery)(filter)).toEqual({ age: { $eq: 30 } });
57
+ });
58
+ });
59
+ describe('$ne operator', () => {
60
+ it('should handle $ne operator', () => {
61
+ const filter = { age: { $ne: 30 } };
62
+ expect((0, data_query_mongo_1.dataFilterToMongoQuery)(filter)).toEqual({ age: { $ne: 30 } });
63
+ });
64
+ });
65
+ describe('Comparison operators', () => {
66
+ it('should handle $gt operator', () => {
67
+ const filter = { age: { $gt: 18 } };
68
+ expect((0, data_query_mongo_1.dataFilterToMongoQuery)(filter)).toEqual({ age: { $gt: 18 } });
69
+ });
70
+ it('should handle $gte operator', () => {
71
+ const filter = { age: { $gte: 18 } };
72
+ expect((0, data_query_mongo_1.dataFilterToMongoQuery)(filter)).toEqual({ age: { $gte: 18 } });
73
+ });
74
+ it('should handle $lt operator', () => {
75
+ const filter = { age: { $lt: 65 } };
76
+ expect((0, data_query_mongo_1.dataFilterToMongoQuery)(filter)).toEqual({ age: { $lt: 65 } });
77
+ });
78
+ it('should handle $lte operator', () => {
79
+ const filter = { age: { $lte: 65 } };
80
+ expect((0, data_query_mongo_1.dataFilterToMongoQuery)(filter)).toEqual({ age: { $lte: 65 } });
81
+ });
82
+ it('should handle multiple comparison operators', () => {
83
+ const filter = { age: { $gte: 18, $lt: 65 } };
84
+ expect((0, data_query_mongo_1.dataFilterToMongoQuery)(filter)).toEqual({ age: { $gte: 18, $lt: 65 } });
85
+ });
86
+ });
87
+ describe('$in and $nin operators', () => {
88
+ it('should handle $in operator', () => {
89
+ const filter = { status: { $in: ['active', 'pending'] } };
90
+ expect((0, data_query_mongo_1.dataFilterToMongoQuery)(filter)).toEqual({ status: { $in: ['active', 'pending'] } });
91
+ });
92
+ it('should handle $nin operator', () => {
93
+ const filter = { status: { $nin: ['inactive', 'deleted'] } };
94
+ expect((0, data_query_mongo_1.dataFilterToMongoQuery)(filter)).toEqual({ status: { $nin: ['inactive', 'deleted'] } });
95
+ });
96
+ it('should handle empty arrays', () => {
97
+ const filter = { status: { $in: [] } };
98
+ expect((0, data_query_mongo_1.dataFilterToMongoQuery)(filter)).toEqual({ status: { $in: [] } });
99
+ });
100
+ });
101
+ describe('$exists operator', () => {
102
+ it('should handle $exists: true', () => {
103
+ const filter = { email: { $exists: true } };
104
+ expect((0, data_query_mongo_1.dataFilterToMongoQuery)(filter)).toEqual({ email: { $exists: true } });
105
+ });
106
+ it('should handle $exists: false', () => {
107
+ const filter = { email: { $exists: false } };
108
+ expect((0, data_query_mongo_1.dataFilterToMongoQuery)(filter)).toEqual({ email: { $exists: false } });
109
+ });
110
+ });
111
+ });
112
+ describe('Logical operators', () => {
113
+ describe('$or operator', () => {
114
+ it('should handle $or with multiple conditions', () => {
115
+ const filter = { $or: [{ name: 'John' }, { age: 30 }] };
116
+ expect((0, data_query_mongo_1.dataFilterToMongoQuery)(filter)).toEqual({
117
+ $or: [{ name: 'John' }, { age: 30 }]
118
+ });
119
+ });
120
+ it('should handle empty $or array', () => {
121
+ const filter = { $or: [] };
122
+ expect((0, data_query_mongo_1.dataFilterToMongoQuery)(filter)).toEqual({ $or: [] });
123
+ });
124
+ it('should handle $or with complex conditions', () => {
125
+ const filter = {
126
+ $or: [
127
+ { name: 'John', age: { $gte: 18 } },
128
+ { status: { $in: ['active', 'pending'] } }
129
+ ]
130
+ };
131
+ expect((0, data_query_mongo_1.dataFilterToMongoQuery)(filter)).toEqual({
132
+ $or: [
133
+ { name: 'John', age: { $gte: 18 } },
134
+ { status: { $in: ['active', 'pending'] } }
135
+ ]
136
+ });
137
+ });
138
+ });
139
+ describe('$and operator', () => {
140
+ it('should handle $and with multiple conditions', () => {
141
+ const filter = { $and: [{ name: 'John' }, { age: 30 }] };
142
+ expect((0, data_query_mongo_1.dataFilterToMongoQuery)(filter)).toEqual({
143
+ $and: [{ name: 'John' }, { age: 30 }]
144
+ });
145
+ });
146
+ it('should handle empty $and array', () => {
147
+ const filter = { $and: [] };
148
+ expect((0, data_query_mongo_1.dataFilterToMongoQuery)(filter)).toEqual({});
149
+ });
150
+ it('should handle nested $and and $or', () => {
151
+ const filter = {
152
+ $and: [
153
+ { status: 'active' },
154
+ {
155
+ $or: [
156
+ { age: { $gte: 18 } },
157
+ { verified: true }
158
+ ]
159
+ }
160
+ ]
161
+ };
162
+ expect((0, data_query_mongo_1.dataFilterToMongoQuery)(filter)).toEqual({
163
+ $and: [
164
+ { status: 'active' },
165
+ {
166
+ $or: [
167
+ { age: { $gte: 18 } },
168
+ { verified: true }
169
+ ]
170
+ }
171
+ ]
172
+ });
173
+ });
174
+ });
175
+ describe('Array filters (OR logic)', () => {
176
+ it('should handle single condition arrays', () => {
177
+ const filter = [{ name: 'John' }];
178
+ expect((0, data_query_mongo_1.dataFilterToMongoQuery)(filter)).toEqual({ name: 'John' });
179
+ });
180
+ it('should handle multiple condition arrays', () => {
181
+ const filter = [{ name: 'John' }, { age: 30 }, { city: 'NYC' }];
182
+ expect((0, data_query_mongo_1.dataFilterToMongoQuery)(filter)).toEqual({
183
+ $or: [{ name: 'John' }, { age: 30 }, { city: 'NYC' }]
184
+ });
185
+ });
186
+ it('should handle complex array conditions', () => {
187
+ const filter = [
188
+ { name: 'John', age: { $gte: 18 } },
189
+ { status: { $in: ['active', 'pending'] } }
190
+ ];
191
+ expect((0, data_query_mongo_1.dataFilterToMongoQuery)(filter)).toEqual({
192
+ $or: [
193
+ { name: 'John', age: { $gte: 18 } },
194
+ { status: { $in: ['active', 'pending'] } }
195
+ ]
196
+ });
197
+ });
198
+ });
199
+ });
200
+ describe('Custom operators', () => {
201
+ describe('$matchWords operator', () => {
202
+ it('should throw error for $matchWords (needs field context)', () => {
203
+ const filter = { $matchWords: 'hello world' };
204
+ expect(() => (0, data_query_mongo_1.dataFilterToMongoQuery)(filter)).toThrow('$matchWords with multiple words requires field context');
205
+ });
206
+ it('should throw error for non-string values', () => {
207
+ const filter = { $matchWords: 123 };
208
+ expect(() => (0, data_query_mongo_1.dataFilterToMongoQuery)(filter)).toThrow('$matchWords value must be a string');
209
+ });
210
+ });
211
+ describe('$substr operator', () => {
212
+ it('should convert $substr to MongoDB $expr format', () => {
213
+ const filter = { $substr: ['$fieldName', 1, 3] };
214
+ const result = (0, data_query_mongo_1.dataFilterToMongoQuery)(filter);
215
+ expect(result).toEqual({
216
+ $expr: {
217
+ $substr: ['$fieldName', 1, 3]
218
+ }
219
+ });
220
+ });
221
+ it('should handle string literals in $substr', () => {
222
+ const filter = { $substr: ['literal text', 0, 5] };
223
+ const result = (0, data_query_mongo_1.dataFilterToMongoQuery)(filter);
224
+ expect(result).toEqual({
225
+ $expr: {
226
+ $substr: ['literal text', 0, 5]
227
+ }
228
+ });
229
+ });
230
+ it('should throw error for invalid $substr arguments', () => {
231
+ const filter = { $substr: ['$field'] }; // Missing arguments
232
+ expect(() => (0, data_query_mongo_1.dataFilterToMongoQuery)(filter)).toThrow('$substr requires exactly 3 arguments');
233
+ });
234
+ it('should throw error for non-array $substr', () => {
235
+ const filter = { $substr: 'invalid' };
236
+ expect(() => (0, data_query_mongo_1.dataFilterToMongoQuery)(filter)).toThrow('$substr requires exactly 3 arguments');
237
+ });
238
+ });
239
+ describe('$expr operator', () => {
240
+ it('should convert simple $expr to MongoDB format', () => {
241
+ const filter = { $expr: [{ $substr: ['$id', 0, 1] }, { $eq: 'A' }] };
242
+ const result = (0, data_query_mongo_1.dataFilterToMongoQuery)(filter);
243
+ expect(result).toEqual({
244
+ $expr: {
245
+ $eq: [
246
+ { $substr: ['$id', 0, 1] },
247
+ 'A'
248
+ ]
249
+ }
250
+ });
251
+ });
252
+ it('should handle $expr with different operators', () => {
253
+ const filter = { $expr: [{ $substr: ['$name', 0, 1] }, { $ne: 'Z' }] };
254
+ const result = (0, data_query_mongo_1.dataFilterToMongoQuery)(filter);
255
+ expect(result).toEqual({
256
+ $expr: {
257
+ $ne: [
258
+ { $substr: ['$name', 0, 1] },
259
+ 'Z'
260
+ ]
261
+ }
262
+ });
263
+ });
264
+ it('should default to $eq for simple expressions', () => {
265
+ const filter = { $expr: ['$field1', '$field2'] };
266
+ const result = (0, data_query_mongo_1.dataFilterToMongoQuery)(filter);
267
+ expect(result).toEqual({
268
+ $expr: {
269
+ $eq: ['$field1', '$field2']
270
+ }
271
+ });
272
+ });
273
+ it('should throw error for invalid $expr format', () => {
274
+ const filter = { $expr: ['single_argument'] };
275
+ expect(() => (0, data_query_mongo_1.dataFilterToMongoQuery)(filter)).toThrow('$expr requires exactly 2 arguments');
276
+ });
277
+ });
278
+ });
279
+ describe('Error cases', () => {
280
+ it('should throw error for unknown operators', () => {
281
+ const filter = { field: { $unknown: 'value' } };
282
+ expect(() => (0, data_query_mongo_1.dataFilterToMongoQuery)(filter)).toThrow('Unknown operator: $unknown');
283
+ });
284
+ it('should throw error for operators without $ prefix', () => {
285
+ const filter = { field: { invalidOp: 'value' } };
286
+ expect(() => (0, data_query_mongo_1.dataFilterToMongoQuery)(filter)).toThrow('Invalid operator: invalidOp. Operators must start with $');
287
+ });
288
+ it('should throw error for nested $matchWords', () => {
289
+ const filter = { field: { $matchWords: 'test' } };
290
+ expect(() => (0, data_query_mongo_1.dataFilterToMongoQuery)(filter)).toThrow('$matchWords should be handled at field level');
291
+ });
292
+ it('should throw error for nested $substr', () => {
293
+ const filter = { field: { $substr: ['$other', 0, 1] } };
294
+ expect(() => (0, data_query_mongo_1.dataFilterToMongoQuery)(filter)).toThrow('$substr should be handled at field level');
295
+ });
296
+ it('should throw error for nested $expr', () => {
297
+ const filter = { field: { $expr: ['$a', '$b'] } };
298
+ expect(() => (0, data_query_mongo_1.dataFilterToMongoQuery)(filter)).toThrow('$expr should be handled at field level');
299
+ });
300
+ });
301
+ describe('Complex nested scenarios', () => {
302
+ it('should handle deeply nested conditions', () => {
303
+ const filter = {
304
+ $and: [
305
+ { status: 'active' },
306
+ {
307
+ $or: [
308
+ { age: { $gte: 18, $lt: 65 } },
309
+ { verified: true }
310
+ ]
311
+ },
312
+ { role: { $in: ['user', 'admin'] } }
313
+ ]
314
+ };
315
+ expect((0, data_query_mongo_1.dataFilterToMongoQuery)(filter)).toEqual({
316
+ $and: [
317
+ { status: 'active' },
318
+ {
319
+ $or: [
320
+ { age: { $gte: 18, $lt: 65 } },
321
+ { verified: true }
322
+ ]
323
+ },
324
+ { role: { $in: ['user', 'admin'] } }
325
+ ]
326
+ });
327
+ });
328
+ it('should handle mixed array and object filters', () => {
329
+ const filter = [
330
+ { name: 'John', age: { $gte: 18 } },
331
+ {
332
+ $and: [
333
+ { status: 'active' },
334
+ { verified: true }
335
+ ]
336
+ }
337
+ ];
338
+ expect((0, data_query_mongo_1.dataFilterToMongoQuery)(filter)).toEqual({
339
+ $or: [
340
+ { name: 'John', age: { $gte: 18 } },
341
+ {
342
+ $and: [
343
+ { status: 'active' },
344
+ { verified: true }
345
+ ]
346
+ }
347
+ ]
348
+ });
349
+ });
350
+ });
351
+ });
352
+ describe('sortByToMongoSort', () => {
353
+ it('should handle empty sort', () => {
354
+ expect((0, data_query_mongo_1.sortByToMongoSort)()).toEqual({});
355
+ expect((0, data_query_mongo_1.sortByToMongoSort)([])).toEqual({});
356
+ });
357
+ it('should handle ascending sort', () => {
358
+ expect((0, data_query_mongo_1.sortByToMongoSort)(['name'])).toEqual({ name: 1 });
359
+ });
360
+ it('should handle descending sort', () => {
361
+ expect((0, data_query_mongo_1.sortByToMongoSort)(['-name'])).toEqual({ name: -1 });
362
+ });
363
+ it('should handle multiple sort fields', () => {
364
+ expect((0, data_query_mongo_1.sortByToMongoSort)(['name', '-age', 'created'])).toEqual({
365
+ name: 1,
366
+ age: -1,
367
+ created: 1
368
+ });
369
+ });
370
+ it('should handle complex field names', () => {
371
+ expect((0, data_query_mongo_1.sortByToMongoSort)(['-user.name', 'profile.age'])).toEqual({
372
+ 'user.name': -1,
373
+ 'profile.age': 1
374
+ });
375
+ });
376
+ });
377
+ describe('paginationToMongoSkipLimit', () => {
378
+ it('should handle no pagination', () => {
379
+ expect((0, data_query_mongo_1.paginationToMongoSkipLimit)()).toEqual({});
380
+ });
381
+ it('should handle only pageSize', () => {
382
+ expect((0, data_query_mongo_1.paginationToMongoSkipLimit)(undefined, 10)).toEqual({ limit: 10 });
383
+ });
384
+ it('should handle only page (no effect without pageSize)', () => {
385
+ expect((0, data_query_mongo_1.paginationToMongoSkipLimit)(2)).toEqual({});
386
+ });
387
+ it('should handle page and pageSize', () => {
388
+ expect((0, data_query_mongo_1.paginationToMongoSkipLimit)(1, 10)).toEqual({ limit: 10 });
389
+ expect((0, data_query_mongo_1.paginationToMongoSkipLimit)(2, 10)).toEqual({ limit: 10, skip: 10 });
390
+ expect((0, data_query_mongo_1.paginationToMongoSkipLimit)(3, 20)).toEqual({ limit: 20, skip: 40 });
391
+ });
392
+ it('should handle fractional numbers', () => {
393
+ expect((0, data_query_mongo_1.paginationToMongoSkipLimit)(2.7, 10.3)).toEqual({ limit: 10, skip: 21 });
394
+ });
395
+ it('should handle zero page', () => {
396
+ expect((0, data_query_mongo_1.paginationToMongoSkipLimit)(0, 10)).toEqual({ limit: 10 });
397
+ });
398
+ });
@@ -0,0 +1,14 @@
1
+ import { DataFilter, DataQuery } from './data-query';
2
+ export declare function dataFilterToSqlWhere(filter: DataFilter<any>): string;
3
+ /**
4
+ * Converts a DataQuery to a complete SQLite query string
5
+ */
6
+ export declare function dataQueryToSqliteQuery(dataQuery: DataQuery<any>): string;
7
+ /**
8
+ * Converts DataQuery text search to SQLite LIKE clauses
9
+ */
10
+ export declare function dataFilterToSqliteTextSearch(query: DataQuery<any>): string;
11
+ /**
12
+ * Converts $substr directive to SQLite substr function
13
+ */
14
+ export declare function substrDirectiveToSqlite(substrArgs: any): string;