@machhub-dev/sdk-ts 0.0.15 → 1.0.0

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.
@@ -12,7 +12,7 @@ export declare class Collection {
12
12
  protected collectionName: string;
13
13
  protected queryParams: Record<string, any>;
14
14
  constructor(httpService: HTTPService, mqttService: MQTTService | null, collectionName: string);
15
- filter(fieldName: string, operator: "=" | ">" | "<" | "<=" | ">=" | "!=" | "CONTAINS", value: any): Collection;
15
+ filter(fieldName: string, operator: "=" | ">" | "<" | "<=" | ">=" | "!=" | "CONTAINS" | "IN", value: any): Collection;
16
16
  sort(field: string, direction?: "asc" | "desc"): Collection;
17
17
  limit(limit: number): Collection;
18
18
  offset(offset: number): Collection;
@@ -25,7 +25,9 @@ export declare class Collection {
25
25
  count(options?: {
26
26
  filter?: any;
27
27
  }): Promise<number>;
28
- getOne(id: string): Promise<any>;
28
+ getOne(id: string, options?: {
29
+ expand?: string | string[];
30
+ }): Promise<any>;
29
31
  create(data: Record<string, any>): Promise<any>;
30
32
  update(id: string, data: Record<string, any>): Promise<any>;
31
33
  delete(id: string): Promise<any>;
@@ -75,12 +75,19 @@ class Collection {
75
75
  throw new CollectionError('count', this.collectionName, error);
76
76
  }
77
77
  }
78
- async getOne(id) {
78
+ async getOne(id, options) {
79
79
  if (!id) {
80
80
  throw new Error("ID must be provided");
81
81
  }
82
82
  try {
83
- return await this.httpService.request.get(id);
83
+ const queryParams = {};
84
+ // Handle expand parameter
85
+ if (options?.expand) {
86
+ queryParams.expand = Array.isArray(options.expand)
87
+ ? options.expand.join(',')
88
+ : options.expand;
89
+ }
90
+ return await this.httpService.request.get(id, queryParams);
84
91
  }
85
92
  catch (error) {
86
93
  throw new CollectionError('getOne', this.collectionName, error);
@@ -12,7 +12,7 @@ export declare class Collection {
12
12
  protected collectionName: string;
13
13
  protected queryParams: Record<string, any>;
14
14
  constructor(httpService: HTTPService, mqttService: MQTTService | null, collectionName: string);
15
- filter(fieldName: string, operator: "=" | ">" | "<" | "<=" | ">=" | "!=" | "CONTAINS", value: any): Collection;
15
+ filter(fieldName: string, operator: "=" | ">" | "<" | "<=" | ">=" | "!=" | "CONTAINS" | "IN", value: any): Collection;
16
16
  sort(field: string, direction?: "asc" | "desc"): Collection;
17
17
  limit(limit: number): Collection;
18
18
  offset(offset: number): Collection;
@@ -25,7 +25,9 @@ export declare class Collection {
25
25
  count(options?: {
26
26
  filter?: any;
27
27
  }): Promise<number>;
28
- getOne(id: string): Promise<any>;
28
+ getOne(id: string, options?: {
29
+ expand?: string | string[];
30
+ }): Promise<any>;
29
31
  create(data: Record<string, any>): Promise<any>;
30
32
  update(id: string, data: Record<string, any>): Promise<any>;
31
33
  delete(id: string): Promise<any>;
@@ -71,12 +71,19 @@ export class Collection {
71
71
  throw new CollectionError('count', this.collectionName, error);
72
72
  }
73
73
  }
74
- async getOne(id) {
74
+ async getOne(id, options) {
75
75
  if (!id) {
76
76
  throw new Error("ID must be provided");
77
77
  }
78
78
  try {
79
- return await this.httpService.request.get(id);
79
+ const queryParams = {};
80
+ // Handle expand parameter
81
+ if (options?.expand) {
82
+ queryParams.expand = Array.isArray(options.expand)
83
+ ? options.expand.join(',')
84
+ : options.expand;
85
+ }
86
+ return await this.httpService.request.get(id, queryParams);
80
87
  }
81
88
  catch (error) {
82
89
  throw new CollectionError('getOne', this.collectionName, error);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@machhub-dev/sdk-ts",
3
- "version": "0.0.15",
3
+ "version": "1.0.0",
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", 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) {
@@ -93,18 +97,26 @@ export class Collection {
93
97
  throw new CollectionError('count', this.collectionName, error as Error);
94
98
  }
95
99
  }
96
-
97
- async getOne(id: string): Promise<any> {
100
+
101
+ async getOne(id: string, options?: { expand?: string | string[] }): Promise<any> {
98
102
  if (!id) {
99
103
  throw new Error("ID must be provided");
100
104
  }
101
105
  try {
102
- return await this.httpService.request.get(id);
106
+ const queryParams: any = {};
107
+
108
+ // Handle expand parameter
109
+ if (options?.expand) {
110
+ queryParams.expand = Array.isArray(options.expand)
111
+ ? options.expand.join(',')
112
+ : options.expand;
113
+ }
114
+
115
+ return await this.httpService.request.get(id, queryParams);
103
116
  } catch (error) {
104
117
  throw new CollectionError('getOne', this.collectionName, error as Error);
105
118
  }
106
119
  }
107
-
108
120
  async create(data: Record<string, any>): Promise<any> {
109
121
  try {
110
122
  const formData = new FormData();
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';
@@ -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";