@machhub-dev/sdk-ts 0.0.16 → 1.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.
@@ -1,5 +1,6 @@
1
1
  import { HTTPService } from "../services/http.service.js";
2
2
  import { MQTTService } from "../services/mqtt.service.js";
3
+ import { Operator } from "../types/operator.models.js";
3
4
  export declare class CollectionError extends Error {
4
5
  operation: string;
5
6
  collectionName: string;
@@ -12,7 +13,7 @@ export declare class Collection {
12
13
  protected collectionName: string;
13
14
  protected queryParams: Record<string, any>;
14
15
  constructor(httpService: HTTPService, mqttService: MQTTService | null, collectionName: string);
15
- filter(fieldName: string, operator: "=" | ">" | "<" | "<=" | ">=" | "!=" | "CONTAINS" | "IN", value: any): Collection;
16
+ filter(fieldName: string, operator: Operator, value: any): Collection;
16
17
  sort(field: string, direction?: "asc" | "desc"): Collection;
17
18
  limit(limit: number): Collection;
18
19
  offset(offset: number): Collection;
@@ -21,6 +22,7 @@ export declare class Collection {
21
22
  first(): Promise<any>;
22
23
  getAll(options?: {
23
24
  expand?: string | string[];
25
+ fields?: string | string[];
24
26
  }): Promise<any[]>;
25
27
  count(options?: {
26
28
  filter?: any;
@@ -55,7 +55,10 @@ class Collection {
55
55
  try {
56
56
  this.applyOptions(options);
57
57
  if (options?.expand) {
58
- this.queryParams.expand = Array.isArray(options.expand) ? options.expand.join() : options.expand;
58
+ this.queryParams.expand = Array.isArray(options.expand) ? options.expand.join(",") : options.expand;
59
+ }
60
+ if (options?.fields) {
61
+ this.queryParams.fields = Array.isArray(options.fields) ? options.fields.join(",") : options.fields;
59
62
  }
60
63
  return await this.httpService.request.get(this.collectionName + "/all", this.queryParams);
61
64
  }
@@ -1,5 +1,6 @@
1
1
  export { SDK, type SDKConfig } from './sdk-ts.js';
2
2
  export type { LoginResponse, PermissionResponse, User, Group, Feature, Permission, ActionResponse, Action, Scope } from './types/auth.models.js';
3
+ export type { Operator } from './types/operator.models.js';
3
4
  export type { BaseResponse } from './types/response.models.js';
4
5
  export type { RecordID } from './types/recordID.models.js';
5
6
  export type { HistorizedData } from './types/tag.models.js';
@@ -11,5 +11,6 @@ export declare class MQTTService {
11
11
  clearTopics(): void;
12
12
  publish(topic: string, message: unknown): boolean;
13
13
  private attachMessageListener;
14
+ private matchesTopic;
14
15
  private parseMessage;
15
16
  }
@@ -89,7 +89,7 @@ class MQTTService {
89
89
  });
90
90
  this.client.on('message', (topic, message) => {
91
91
  for (const subscribedTopic of this.subscribedTopics) {
92
- if (topic === subscribedTopic.topic) {
92
+ if (this.matchesTopic(subscribedTopic.topic, topic)) {
93
93
  const parsedMessage = this.parseMessage(message, topic);
94
94
  subscribedTopic.handler(parsedMessage);
95
95
  break;
@@ -97,6 +97,40 @@ class MQTTService {
97
97
  }
98
98
  });
99
99
  }
100
+ // Matches MQTT topic patterns with wildcards (+ for single level, # for multi-level)
101
+ matchesTopic(pattern, topic) {
102
+ // Handle exact match first for performance
103
+ if (pattern === topic) {
104
+ return true;
105
+ }
106
+ const patternParts = pattern.split('/');
107
+ const topicParts = topic.split('/');
108
+ let i = 0;
109
+ let j = 0;
110
+ while (i < patternParts.length && j < topicParts.length) {
111
+ const patternPart = patternParts[i];
112
+ if (patternPart === '#') {
113
+ // Multi-level wildcard matches everything from this point
114
+ return true;
115
+ }
116
+ else if (patternPart === '+') {
117
+ // Single-level wildcard matches any single level
118
+ i++;
119
+ j++;
120
+ }
121
+ else if (patternPart === topicParts[j]) {
122
+ // Exact match
123
+ i++;
124
+ j++;
125
+ }
126
+ else {
127
+ // No match
128
+ return false;
129
+ }
130
+ }
131
+ // Check if we've consumed both pattern and topic completely
132
+ return i === patternParts.length && j === topicParts.length;
133
+ }
100
134
  parseMessage(message, topic) {
101
135
  try {
102
136
  return JSON.parse(message.toString());
@@ -0,0 +1,28 @@
1
+ /**
2
+ * SurrealDB Operators
3
+ * Reference: https://surrealdb.com/docs/surrealql/operators
4
+ */
5
+ /**
6
+ * Comparison operators
7
+ */
8
+ export type ComparisonOperator = "=" | "!=" | "==" | "?=" | "*=" | "~" | "!~" | "?~" | "*~" | "<" | "<=" | ">" | ">=";
9
+ /**
10
+ * Containment operators
11
+ */
12
+ export type ContainmentOperator = "CONTAINS" | "CONTAINSNOT" | "CONTAINSALL" | "CONTAINSANY" | "CONTAINSNONE" | "INSIDE" | "NOTINSIDE" | "ALLINSIDE" | "ANYINSIDE" | "NONEINSIDE";
13
+ /**
14
+ * String operators
15
+ */
16
+ export type StringOperator = "@@" | "@0@" | "@1@" | "@2@" | "@3@";
17
+ /**
18
+ * Array operators
19
+ */
20
+ export type ArrayOperator = "IN" | "NOT IN";
21
+ /**
22
+ * All available operators in SurrealDB
23
+ */
24
+ export type Operator = ComparisonOperator | ContainmentOperator | StringOperator | ArrayOperator;
25
+ /**
26
+ * Common operators for basic queries
27
+ */
28
+ export type BasicOperator = "=" | "!=" | "<" | "<=" | ">" | ">=" | "CONTAINS" | "IN";
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ /**
3
+ * SurrealDB Operators
4
+ * Reference: https://surrealdb.com/docs/surrealql/operators
5
+ */
6
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,5 +1,6 @@
1
1
  import { HTTPService } from "../services/http.service.js";
2
2
  import { MQTTService } from "../services/mqtt.service.js";
3
+ import { Operator } from "../types/operator.models.js";
3
4
  export declare class CollectionError extends Error {
4
5
  operation: string;
5
6
  collectionName: string;
@@ -12,7 +13,7 @@ export declare class Collection {
12
13
  protected collectionName: string;
13
14
  protected queryParams: Record<string, any>;
14
15
  constructor(httpService: HTTPService, mqttService: MQTTService | null, collectionName: string);
15
- filter(fieldName: string, operator: "=" | ">" | "<" | "<=" | ">=" | "!=" | "CONTAINS" | "IN", value: any): Collection;
16
+ filter(fieldName: string, operator: Operator, value: any): Collection;
16
17
  sort(field: string, direction?: "asc" | "desc"): Collection;
17
18
  limit(limit: number): Collection;
18
19
  offset(offset: number): Collection;
@@ -21,6 +22,7 @@ export declare class Collection {
21
22
  first(): Promise<any>;
22
23
  getAll(options?: {
23
24
  expand?: string | string[];
25
+ fields?: string | string[];
24
26
  }): Promise<any[]>;
25
27
  count(options?: {
26
28
  filter?: any;
@@ -51,7 +51,10 @@ export class Collection {
51
51
  try {
52
52
  this.applyOptions(options);
53
53
  if (options?.expand) {
54
- this.queryParams.expand = Array.isArray(options.expand) ? options.expand.join() : options.expand;
54
+ this.queryParams.expand = Array.isArray(options.expand) ? options.expand.join(",") : options.expand;
55
+ }
56
+ if (options?.fields) {
57
+ this.queryParams.fields = Array.isArray(options.fields) ? options.fields.join(",") : options.fields;
55
58
  }
56
59
  return await this.httpService.request.get(this.collectionName + "/all", this.queryParams);
57
60
  }
package/dist/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export { SDK, type SDKConfig } from './sdk-ts.js';
2
2
  export type { LoginResponse, PermissionResponse, User, Group, Feature, Permission, ActionResponse, Action, Scope } from './types/auth.models.js';
3
+ export type { Operator } from './types/operator.models.js';
3
4
  export type { BaseResponse } from './types/response.models.js';
4
5
  export type { RecordID } from './types/recordID.models.js';
5
6
  export type { HistorizedData } from './types/tag.models.js';
@@ -11,5 +11,6 @@ export declare class MQTTService {
11
11
  clearTopics(): void;
12
12
  publish(topic: string, message: unknown): boolean;
13
13
  private attachMessageListener;
14
+ private matchesTopic;
14
15
  private parseMessage;
15
16
  }
@@ -83,7 +83,7 @@ export class MQTTService {
83
83
  });
84
84
  this.client.on('message', (topic, message) => {
85
85
  for (const subscribedTopic of this.subscribedTopics) {
86
- if (topic === subscribedTopic.topic) {
86
+ if (this.matchesTopic(subscribedTopic.topic, topic)) {
87
87
  const parsedMessage = this.parseMessage(message, topic);
88
88
  subscribedTopic.handler(parsedMessage);
89
89
  break;
@@ -91,6 +91,40 @@ export class MQTTService {
91
91
  }
92
92
  });
93
93
  }
94
+ // Matches MQTT topic patterns with wildcards (+ for single level, # for multi-level)
95
+ matchesTopic(pattern, topic) {
96
+ // Handle exact match first for performance
97
+ if (pattern === topic) {
98
+ return true;
99
+ }
100
+ const patternParts = pattern.split('/');
101
+ const topicParts = topic.split('/');
102
+ let i = 0;
103
+ let j = 0;
104
+ while (i < patternParts.length && j < topicParts.length) {
105
+ const patternPart = patternParts[i];
106
+ if (patternPart === '#') {
107
+ // Multi-level wildcard matches everything from this point
108
+ return true;
109
+ }
110
+ else if (patternPart === '+') {
111
+ // Single-level wildcard matches any single level
112
+ i++;
113
+ j++;
114
+ }
115
+ else if (patternPart === topicParts[j]) {
116
+ // Exact match
117
+ i++;
118
+ j++;
119
+ }
120
+ else {
121
+ // No match
122
+ return false;
123
+ }
124
+ }
125
+ // Check if we've consumed both pattern and topic completely
126
+ return i === patternParts.length && j === topicParts.length;
127
+ }
94
128
  parseMessage(message, topic) {
95
129
  try {
96
130
  return JSON.parse(message.toString());
@@ -0,0 +1,28 @@
1
+ /**
2
+ * SurrealDB Operators
3
+ * Reference: https://surrealdb.com/docs/surrealql/operators
4
+ */
5
+ /**
6
+ * Comparison operators
7
+ */
8
+ export type ComparisonOperator = "=" | "!=" | "==" | "?=" | "*=" | "~" | "!~" | "?~" | "*~" | "<" | "<=" | ">" | ">=";
9
+ /**
10
+ * Containment operators
11
+ */
12
+ export type ContainmentOperator = "CONTAINS" | "CONTAINSNOT" | "CONTAINSALL" | "CONTAINSANY" | "CONTAINSNONE" | "INSIDE" | "NOTINSIDE" | "ALLINSIDE" | "ANYINSIDE" | "NONEINSIDE";
13
+ /**
14
+ * String operators
15
+ */
16
+ export type StringOperator = "@@" | "@0@" | "@1@" | "@2@" | "@3@";
17
+ /**
18
+ * Array operators
19
+ */
20
+ export type ArrayOperator = "IN" | "NOT IN";
21
+ /**
22
+ * All available operators in SurrealDB
23
+ */
24
+ export type Operator = ComparisonOperator | ContainmentOperator | StringOperator | ArrayOperator;
25
+ /**
26
+ * Common operators for basic queries
27
+ */
28
+ export type BasicOperator = "=" | "!=" | "<" | "<=" | ">" | ">=" | "CONTAINS" | "IN";
@@ -0,0 +1,5 @@
1
+ /**
2
+ * SurrealDB Operators
3
+ * Reference: https://surrealdb.com/docs/surrealql/operators
4
+ */
5
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@machhub-dev/sdk-ts",
3
- "version": "0.0.16",
3
+ "version": "1.0.1",
4
4
  "description": "MACHHUB TYPESCRIPT SDK",
5
5
  "keywords": [
6
6
  "machhub",
@@ -1,5 +1,6 @@
1
1
  import { HTTPService } from "../services/http.service.js";
2
2
  import { MQTTService } from "../services/mqtt.service.js";
3
+ import { Operator } from "../types/operator.models.js";
3
4
 
4
5
  export class CollectionError extends Error {
5
6
  public operation: string;
@@ -28,7 +29,7 @@ export class Collection {
28
29
  }
29
30
 
30
31
 
31
- filter(fieldName: string, operator: "=" | ">" | "<" | "<=" | ">=" | "!=" | "CONTAINS" | "IN", value: any): Collection {
32
+ filter(fieldName: string, operator: Operator, value: any): Collection {
32
33
  this.queryParams[`filter[${fieldName}][${operator}][${typeof value}]`] = value;
33
34
  return this;
34
35
  }
@@ -68,11 +69,14 @@ export class Collection {
68
69
  return results[0] ?? null
69
70
  }
70
71
 
71
- async getAll(options?: { expand?: string | string[] }): Promise<any[]> {
72
+ async getAll(options?: { expand?: string | string[], fields?: string | string[] }): Promise<any[]> {
72
73
  try {
73
74
  this.applyOptions(options)
74
75
  if (options?.expand) {
75
- this.queryParams.expand = Array.isArray(options.expand) ? options.expand.join() : options.expand
76
+ this.queryParams.expand = Array.isArray(options.expand) ? options.expand.join(",") : options.expand
77
+ }
78
+ if (options?.fields) {
79
+ this.queryParams.fields = Array.isArray(options.fields) ? options.fields.join(",") : options.fields
76
80
  }
77
81
  return await this.httpService.request.get(this.collectionName + "/all", this.queryParams);
78
82
  } catch (error) {
package/src/index.ts CHANGED
@@ -2,6 +2,7 @@ export { SDK, type SDKConfig } from './sdk-ts.js';
2
2
 
3
3
  // Export individual types
4
4
  export type { LoginResponse, PermissionResponse, User, Group, Feature, Permission, ActionResponse, Action, Scope } from './types/auth.models.js';
5
+ export type { Operator } from './types/operator.models.js';
5
6
  export type { BaseResponse } from './types/response.models.js';
6
7
  export type { RecordID } from './types/recordID.models.js';
7
8
  export type { HistorizedData } from './types/tag.models.js';
@@ -101,7 +101,7 @@ export class MQTTService {
101
101
 
102
102
  this.client.on('message', (topic: string, message: Buffer) => {
103
103
  for (const subscribedTopic of this.subscribedTopics) {
104
- if (topic === subscribedTopic.topic) {
104
+ if (this.matchesTopic(subscribedTopic.topic, topic)) {
105
105
  const parsedMessage = this.parseMessage(message, topic);
106
106
  subscribedTopic.handler(parsedMessage);
107
107
  break;
@@ -110,6 +110,43 @@ export class MQTTService {
110
110
  });
111
111
  }
112
112
 
113
+ // Matches MQTT topic patterns with wildcards (+ for single level, # for multi-level)
114
+ private matchesTopic(pattern: string, topic: string): boolean {
115
+ // Handle exact match first for performance
116
+ if (pattern === topic) {
117
+ return true;
118
+ }
119
+
120
+ const patternParts = pattern.split('/');
121
+ const topicParts = topic.split('/');
122
+
123
+ let i = 0;
124
+ let j = 0;
125
+
126
+ while (i < patternParts.length && j < topicParts.length) {
127
+ const patternPart = patternParts[i];
128
+
129
+ if (patternPart === '#') {
130
+ // Multi-level wildcard matches everything from this point
131
+ return true;
132
+ } else if (patternPart === '+') {
133
+ // Single-level wildcard matches any single level
134
+ i++;
135
+ j++;
136
+ } else if (patternPart === topicParts[j]) {
137
+ // Exact match
138
+ i++;
139
+ j++;
140
+ } else {
141
+ // No match
142
+ return false;
143
+ }
144
+ }
145
+
146
+ // Check if we've consumed both pattern and topic completely
147
+ return i === patternParts.length && j === topicParts.length;
148
+ }
149
+
113
150
  private parseMessage(message: Buffer, topic: string): unknown {
114
151
  try {
115
152
  return JSON.parse(message.toString());
@@ -0,0 +1,68 @@
1
+ /**
2
+ * SurrealDB Operators
3
+ * Reference: https://surrealdb.com/docs/surrealql/operators
4
+ */
5
+
6
+ /**
7
+ * Comparison operators
8
+ */
9
+ export type ComparisonOperator =
10
+ | "=" // Equal to
11
+ | "!=" // Not equal to
12
+ | "==" // Exact equality
13
+ | "?=" // Matches a condition
14
+ | "*=" // All match a condition
15
+ | "~" // Contains / matches regex
16
+ | "!~" // Does not contain / match regex
17
+ | "?~" // Matches a condition (fuzzy)
18
+ | "*~" // All match a condition (fuzzy)
19
+ | "<" // Less than
20
+ | "<=" // Less than or equal to
21
+ | ">" // Greater than
22
+ | ">=" // Greater than or equal to;
23
+
24
+ /**
25
+ * Containment operators
26
+ */
27
+ export type ContainmentOperator =
28
+ | "CONTAINS" // Contains a value
29
+ | "CONTAINSNOT" // Does not contain a value
30
+ | "CONTAINSALL" // Contains all values
31
+ | "CONTAINSANY" // Contains any value
32
+ | "CONTAINSNONE" // Contains none of the values
33
+ | "INSIDE" // Value is inside
34
+ | "NOTINSIDE" // Value is not inside
35
+ | "ALLINSIDE" // All values are inside
36
+ | "ANYINSIDE" // Any value is inside
37
+ | "NONEINSIDE"; // No values are inside
38
+
39
+ /**
40
+ * String operators
41
+ */
42
+ export type StringOperator =
43
+ | "@@" // Matches a condition (full-text search)
44
+ | "@0@" // Matches a condition (exact match)
45
+ | "@1@" // Matches a condition (fuzzy match level 1)
46
+ | "@2@" // Matches a condition (fuzzy match level 2)
47
+ | "@3@"; // Matches a condition (fuzzy match level 3)
48
+
49
+ /**
50
+ * Array operators
51
+ */
52
+ export type ArrayOperator =
53
+ | "IN" // Value is in array
54
+ | "NOT IN"; // Value is not in array
55
+
56
+ /**
57
+ * All available operators in SurrealDB
58
+ */
59
+ export type Operator =
60
+ | ComparisonOperator
61
+ | ContainmentOperator
62
+ | StringOperator
63
+ | ArrayOperator;
64
+
65
+ /**
66
+ * Common operators for basic queries
67
+ */
68
+ export type BasicOperator = "=" | "!=" | "<" | "<=" | ">" | ">=" | "CONTAINS" | "IN";