@thinkingdifferently/core 1.1.0 → 1.2.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.
- package/README.md +321 -0
- package/dist/index.d.mts +11 -1
- package/dist/index.d.ts +11 -1
- package/dist/index.js +146 -25
- package/dist/index.mjs +146 -25
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,321 @@
|
|
|
1
|
+
# `@thinkingdifferently/core`
|
|
2
|
+
|
|
3
|
+
Official TypeScript SDK for the Thinking Differently Backend-as-a-Service platform.
|
|
4
|
+
|
|
5
|
+
The SDK provides a simple interface for interacting with Thinking Differently collections without manually managing HTTP requests, authentication headers, request formatting, or query serialization.
|
|
6
|
+
|
|
7
|
+
## Features
|
|
8
|
+
|
|
9
|
+
- TypeScript-first SDK
|
|
10
|
+
- Collection querying with chained builders
|
|
11
|
+
- Automatic API key authentication via `x-api-key`
|
|
12
|
+
- Axios-based HTTP layer
|
|
13
|
+
- Built-in validation for query inputs
|
|
14
|
+
- `FormData` support for inserts
|
|
15
|
+
- Query inspection with `toJSON()`
|
|
16
|
+
|
|
17
|
+
## Installation
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
npm install @thinkingdifferently/core
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Quick Start
|
|
24
|
+
|
|
25
|
+
```ts
|
|
26
|
+
import { ThinkingDifferently } from "@thinkingdifferently/core";
|
|
27
|
+
|
|
28
|
+
const sdk = new ThinkingDifferently({
|
|
29
|
+
apiKey: "YOUR_API_KEY"
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
const animals = await sdk
|
|
33
|
+
.collection("animals")
|
|
34
|
+
.where("age", ">", 10)
|
|
35
|
+
.limit(10)
|
|
36
|
+
.sort("createdAt", "desc")
|
|
37
|
+
.get();
|
|
38
|
+
|
|
39
|
+
console.log(animals);
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## TypeScript Usage
|
|
43
|
+
|
|
44
|
+
The SDK is designed for TypeScript developers and supports generic response typing:
|
|
45
|
+
|
|
46
|
+
```ts
|
|
47
|
+
type Animal = {
|
|
48
|
+
name: string;
|
|
49
|
+
age: number;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
const animals = await sdk
|
|
53
|
+
.collection("animals")
|
|
54
|
+
.get<Animal>();
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
You can also inspect the generated query before sending it:
|
|
58
|
+
|
|
59
|
+
```ts
|
|
60
|
+
const query = sdk
|
|
61
|
+
.collection("animals")
|
|
62
|
+
.where("age", ">", 10);
|
|
63
|
+
|
|
64
|
+
console.log(query.toJSON());
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## API Reference
|
|
68
|
+
|
|
69
|
+
### `new ThinkingDifferently(config)`
|
|
70
|
+
|
|
71
|
+
Creates an SDK instance.
|
|
72
|
+
|
|
73
|
+
#### Parameters
|
|
74
|
+
|
|
75
|
+
- `config.apiKey: string` — API key used for authenticated requests
|
|
76
|
+
|
|
77
|
+
#### Example
|
|
78
|
+
|
|
79
|
+
```ts
|
|
80
|
+
const sdk = new ThinkingDifferently({
|
|
81
|
+
apiKey: "YOUR_API_KEY"
|
|
82
|
+
});
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### `sdk.collection(name)`
|
|
86
|
+
|
|
87
|
+
Creates a query builder for a collection.
|
|
88
|
+
|
|
89
|
+
#### Parameters
|
|
90
|
+
|
|
91
|
+
- `name: string` — collection name
|
|
92
|
+
|
|
93
|
+
#### Returns
|
|
94
|
+
|
|
95
|
+
A `QueryBuilder` instance.
|
|
96
|
+
|
|
97
|
+
### `QueryBuilder.where(field, operator, value)`
|
|
98
|
+
|
|
99
|
+
Adds a filter to the query.
|
|
100
|
+
|
|
101
|
+
#### Supported operators
|
|
102
|
+
|
|
103
|
+
- `=`
|
|
104
|
+
- `!=`
|
|
105
|
+
- `>`
|
|
106
|
+
- `<`
|
|
107
|
+
- `>=`
|
|
108
|
+
- `<=`
|
|
109
|
+
- `in`
|
|
110
|
+
- `contains`
|
|
111
|
+
|
|
112
|
+
#### Notes
|
|
113
|
+
|
|
114
|
+
- Empty field names are rejected
|
|
115
|
+
- The `in` operator requires an array value
|
|
116
|
+
- Multiple `where()` calls append filters
|
|
117
|
+
|
|
118
|
+
#### Example
|
|
119
|
+
|
|
120
|
+
```ts
|
|
121
|
+
const query = sdk
|
|
122
|
+
.collection("animals")
|
|
123
|
+
.where("age", ">", 10)
|
|
124
|
+
.where("status", "=", "active");
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
### `QueryBuilder.limit(count)`
|
|
128
|
+
|
|
129
|
+
Sets the maximum number of returned documents.
|
|
130
|
+
|
|
131
|
+
#### Validation
|
|
132
|
+
|
|
133
|
+
- Must be a positive integer
|
|
134
|
+
- Subsequent calls overwrite the previous limit
|
|
135
|
+
|
|
136
|
+
#### Example
|
|
137
|
+
|
|
138
|
+
```ts
|
|
139
|
+
const query = sdk.collection("animals").limit(20);
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
### `QueryBuilder.sort(field, direction)`
|
|
143
|
+
|
|
144
|
+
Adds a sort clause to the query.
|
|
145
|
+
|
|
146
|
+
#### Parameters
|
|
147
|
+
|
|
148
|
+
- `field: string` — field to sort by
|
|
149
|
+
- `direction: "asc" | "desc"` — sort direction
|
|
150
|
+
|
|
151
|
+
#### Notes
|
|
152
|
+
|
|
153
|
+
- Empty field names are rejected
|
|
154
|
+
- Subsequent calls overwrite the previous sort
|
|
155
|
+
|
|
156
|
+
#### Example
|
|
157
|
+
|
|
158
|
+
```ts
|
|
159
|
+
const query = sdk.collection("animals").sort("createdAt", "desc");
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
### `QueryBuilder.get<T>()`
|
|
163
|
+
|
|
164
|
+
Executes the query and returns an array of typed results.
|
|
165
|
+
|
|
166
|
+
#### Signature
|
|
167
|
+
|
|
168
|
+
```ts
|
|
169
|
+
get<T = any>(): Promise<T[]>
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
#### Example
|
|
173
|
+
|
|
174
|
+
```ts
|
|
175
|
+
const animals = await sdk
|
|
176
|
+
.collection("animals")
|
|
177
|
+
.where("age", ">", 10)
|
|
178
|
+
.get<{ name: string; age: number }>();
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
### `QueryBuilder.count()`
|
|
182
|
+
|
|
183
|
+
Returns the number of documents matching the current query.
|
|
184
|
+
|
|
185
|
+
#### Signature
|
|
186
|
+
|
|
187
|
+
```ts
|
|
188
|
+
count(): Promise<number>
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
#### Example
|
|
192
|
+
|
|
193
|
+
```ts
|
|
194
|
+
const total = await sdk
|
|
195
|
+
.collection("animals")
|
|
196
|
+
.where("age", ">", 10)
|
|
197
|
+
.count();
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
### `QueryBuilder.toJSON()`
|
|
201
|
+
|
|
202
|
+
Returns a deep copy of the generated query object.
|
|
203
|
+
|
|
204
|
+
#### Example
|
|
205
|
+
|
|
206
|
+
```ts
|
|
207
|
+
const query = sdk.collection("animals").where("age", ">", 10);
|
|
208
|
+
console.log(query.toJSON());
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
### `types `
|
|
212
|
+
|
|
213
|
+
```ts
|
|
214
|
+
interface SDKConfig {
|
|
215
|
+
apiKey: string;
|
|
216
|
+
}
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
## HTTP Details
|
|
220
|
+
|
|
221
|
+
- Base URL: `https://www.thinkingdifferently.dev/api/v1`
|
|
222
|
+
- Authentication header: `x-api-key: YOUR_API_KEY`
|
|
223
|
+
- HTTP client: Axios
|
|
224
|
+
- Supported methods: `GET`, `POST`, `PATCH`, `DELETE`
|
|
225
|
+
|
|
226
|
+
## Error Handling
|
|
227
|
+
|
|
228
|
+
The SDK throws JavaScript `Error` objects when requests fail or when query validation fails.
|
|
229
|
+
|
|
230
|
+
### Request errors
|
|
231
|
+
|
|
232
|
+
Backend errors are converted into `Error` messages.
|
|
233
|
+
|
|
234
|
+
```ts
|
|
235
|
+
try {
|
|
236
|
+
const animals = await sdk
|
|
237
|
+
.collection("animals")
|
|
238
|
+
.get();
|
|
239
|
+
|
|
240
|
+
console.log(animals);
|
|
241
|
+
} catch (error) {
|
|
242
|
+
console.error(error);
|
|
243
|
+
}
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
### Validation errors
|
|
247
|
+
|
|
248
|
+
The SDK validates query input before sending requests:
|
|
249
|
+
|
|
250
|
+
- Empty field names are rejected
|
|
251
|
+
- Invalid limit values are rejected
|
|
252
|
+
- The `in` operator requires an array
|
|
253
|
+
- Query responses are validated before parsing
|
|
254
|
+
|
|
255
|
+
## Examples
|
|
256
|
+
|
|
257
|
+
### Filter, sort, and limit
|
|
258
|
+
|
|
259
|
+
```ts
|
|
260
|
+
const animals = await sdk
|
|
261
|
+
.collection("animals")
|
|
262
|
+
.where("age", ">", 10)
|
|
263
|
+
.where("price", "<", 50000)
|
|
264
|
+
.limit(10)
|
|
265
|
+
.sort("createdAt", "desc")
|
|
266
|
+
.get();
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
### Count matching documents
|
|
270
|
+
|
|
271
|
+
```ts
|
|
272
|
+
const total = await sdk
|
|
273
|
+
.collection("animals")
|
|
274
|
+
.where("age", ">", 10)
|
|
275
|
+
.count();
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
### Inspect the generated query
|
|
279
|
+
|
|
280
|
+
```ts
|
|
281
|
+
const query = sdk
|
|
282
|
+
.collection("animals")
|
|
283
|
+
.where("age", ">", 10);
|
|
284
|
+
|
|
285
|
+
console.log(JSON.stringify(query.toJSON(), null, 2));
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
## FAQ
|
|
289
|
+
|
|
290
|
+
### How do I authenticate?
|
|
291
|
+
|
|
292
|
+
Pass your API key to `new ThinkingDifferently({ apiKey })`. The SDK automatically sends it using the `x-api-key` header.
|
|
293
|
+
|
|
294
|
+
### What does `collection()` return?
|
|
295
|
+
|
|
296
|
+
It returns a query builder that lets you chain `where()`, `limit()`, `sort()`, `get()`, `count()`, and `toJSON()`.
|
|
297
|
+
|
|
298
|
+
### Can I inspect a query before executing it?
|
|
299
|
+
|
|
300
|
+
Yes. Call `toJSON()` on the query builder to get a deep copy of the query object.
|
|
301
|
+
|
|
302
|
+
### Does the SDK validate input?
|
|
303
|
+
|
|
304
|
+
Yes. Empty field names, invalid limit values, and invalid `in` operator values are rejected.
|
|
305
|
+
|
|
306
|
+
### What transport does the SDK use?
|
|
307
|
+
|
|
308
|
+
The SDK uses Axios for network requests.
|
|
309
|
+
|
|
310
|
+
## Roadmap
|
|
311
|
+
|
|
312
|
+
The following items are listed in the current design notes as not yet implemented:
|
|
313
|
+
|
|
314
|
+
- `update()`
|
|
315
|
+
- `delete()`
|
|
316
|
+
- `first()`
|
|
317
|
+
- pagination helpers
|
|
318
|
+
- aggregation queries
|
|
319
|
+
- `OR` conditions
|
|
320
|
+
- joins
|
|
321
|
+
|
package/dist/index.d.mts
CHANGED
|
@@ -7,17 +7,20 @@ interface GetOptions {
|
|
|
7
7
|
|
|
8
8
|
declare class TDClient {
|
|
9
9
|
private api;
|
|
10
|
+
private apikey;
|
|
10
11
|
constructor(apiKey: string);
|
|
11
12
|
request(method: "POST" | "GET" | "PATCH" | "DELETE", body?: any): Promise<any>;
|
|
12
13
|
}
|
|
13
14
|
|
|
14
15
|
type Operator = "=" | "!=" | ">" | "<" | ">=" | "<=" | "in" | "contains";
|
|
16
|
+
type Operation = "find" | "count" | "insert" | "update" | "delete" | "updateMany" | "deleteMany";
|
|
15
17
|
type Filter = {
|
|
16
18
|
field: string;
|
|
17
19
|
operator: Operator;
|
|
18
20
|
value: unknown;
|
|
19
21
|
};
|
|
20
22
|
type Query = {
|
|
23
|
+
operation: Operation | null;
|
|
21
24
|
collection: string;
|
|
22
25
|
filters: Filter[];
|
|
23
26
|
limit: number | null;
|
|
@@ -25,6 +28,8 @@ type Query = {
|
|
|
25
28
|
field: string;
|
|
26
29
|
direction: "asc" | "desc";
|
|
27
30
|
} | null;
|
|
31
|
+
id?: string;
|
|
32
|
+
data?: unknown;
|
|
28
33
|
};
|
|
29
34
|
declare class QueryBuilder {
|
|
30
35
|
private query;
|
|
@@ -33,9 +38,14 @@ declare class QueryBuilder {
|
|
|
33
38
|
where(field: string, operator: Operator, value: unknown): this;
|
|
34
39
|
limit(count: number): this;
|
|
35
40
|
sort(field: string, direction: "asc" | "desc"): this;
|
|
36
|
-
get<T = any>(): Promise<T[]>;
|
|
37
41
|
count(): Promise<number>;
|
|
38
42
|
toJSON(): Query;
|
|
43
|
+
get<T = any>(): Promise<T[]>;
|
|
44
|
+
insert(data: unknown): Promise<any>;
|
|
45
|
+
UpdateById(id: string, data: Record<string, any>): Promise<any>;
|
|
46
|
+
updateMany(data: Record<string, any>): Promise<any>;
|
|
47
|
+
DeleteById(id: string): Promise<any>;
|
|
48
|
+
deleteMany(): Promise<any>;
|
|
39
49
|
}
|
|
40
50
|
|
|
41
51
|
declare class ThinkingDifferently {
|
package/dist/index.d.ts
CHANGED
|
@@ -7,17 +7,20 @@ interface GetOptions {
|
|
|
7
7
|
|
|
8
8
|
declare class TDClient {
|
|
9
9
|
private api;
|
|
10
|
+
private apikey;
|
|
10
11
|
constructor(apiKey: string);
|
|
11
12
|
request(method: "POST" | "GET" | "PATCH" | "DELETE", body?: any): Promise<any>;
|
|
12
13
|
}
|
|
13
14
|
|
|
14
15
|
type Operator = "=" | "!=" | ">" | "<" | ">=" | "<=" | "in" | "contains";
|
|
16
|
+
type Operation = "find" | "count" | "insert" | "update" | "delete" | "updateMany" | "deleteMany";
|
|
15
17
|
type Filter = {
|
|
16
18
|
field: string;
|
|
17
19
|
operator: Operator;
|
|
18
20
|
value: unknown;
|
|
19
21
|
};
|
|
20
22
|
type Query = {
|
|
23
|
+
operation: Operation | null;
|
|
21
24
|
collection: string;
|
|
22
25
|
filters: Filter[];
|
|
23
26
|
limit: number | null;
|
|
@@ -25,6 +28,8 @@ type Query = {
|
|
|
25
28
|
field: string;
|
|
26
29
|
direction: "asc" | "desc";
|
|
27
30
|
} | null;
|
|
31
|
+
id?: string;
|
|
32
|
+
data?: unknown;
|
|
28
33
|
};
|
|
29
34
|
declare class QueryBuilder {
|
|
30
35
|
private query;
|
|
@@ -33,9 +38,14 @@ declare class QueryBuilder {
|
|
|
33
38
|
where(field: string, operator: Operator, value: unknown): this;
|
|
34
39
|
limit(count: number): this;
|
|
35
40
|
sort(field: string, direction: "asc" | "desc"): this;
|
|
36
|
-
get<T = any>(): Promise<T[]>;
|
|
37
41
|
count(): Promise<number>;
|
|
38
42
|
toJSON(): Query;
|
|
43
|
+
get<T = any>(): Promise<T[]>;
|
|
44
|
+
insert(data: unknown): Promise<any>;
|
|
45
|
+
UpdateById(id: string, data: Record<string, any>): Promise<any>;
|
|
46
|
+
updateMany(data: Record<string, any>): Promise<any>;
|
|
47
|
+
DeleteById(id: string): Promise<any>;
|
|
48
|
+
deleteMany(): Promise<any>;
|
|
39
49
|
}
|
|
40
50
|
|
|
41
51
|
declare class ThinkingDifferently {
|
package/dist/index.js
CHANGED
|
@@ -39,6 +39,7 @@ module.exports = __toCommonJS(index_exports);
|
|
|
39
39
|
var import_axios = __toESM(require("axios"));
|
|
40
40
|
var TDClient = class {
|
|
41
41
|
constructor(apiKey) {
|
|
42
|
+
this.apikey = apiKey;
|
|
42
43
|
this.api = import_axios.default.create({
|
|
43
44
|
baseURL: "https://www.thinkingdifferently.dev/api/v1",
|
|
44
45
|
headers: {
|
|
@@ -50,6 +51,14 @@ var TDClient = class {
|
|
|
50
51
|
async request(method, body) {
|
|
51
52
|
try {
|
|
52
53
|
const isFormData = body instanceof FormData;
|
|
54
|
+
if (isFormData) {
|
|
55
|
+
body.append("key", this.apikey);
|
|
56
|
+
} else {
|
|
57
|
+
body = {
|
|
58
|
+
key: this.apikey,
|
|
59
|
+
query: body
|
|
60
|
+
};
|
|
61
|
+
}
|
|
53
62
|
const response = await this.api.request({
|
|
54
63
|
url: "/data",
|
|
55
64
|
method,
|
|
@@ -70,6 +79,7 @@ var QueryBuilder = class {
|
|
|
70
79
|
constructor(collection, client) {
|
|
71
80
|
this.client = client;
|
|
72
81
|
this.query = {
|
|
82
|
+
operation: null,
|
|
73
83
|
collection,
|
|
74
84
|
filters: [],
|
|
75
85
|
limit: null,
|
|
@@ -103,7 +113,30 @@ var QueryBuilder = class {
|
|
|
103
113
|
// build() {
|
|
104
114
|
// return this.query;
|
|
105
115
|
// }
|
|
116
|
+
//to do writing the count method for the query builder
|
|
117
|
+
//the conditions are specified , so now the count method should return the number of documents that match the specified conditions in the query builder
|
|
118
|
+
//means count will first get the data based on the filters and then return the length of the data array as the count of matching documents
|
|
119
|
+
async count() {
|
|
120
|
+
this.query.operation = "count";
|
|
121
|
+
console.log("\n================ COUNT REQUEST ================");
|
|
122
|
+
console.log("[SDK] Final Query:", this.query);
|
|
123
|
+
try {
|
|
124
|
+
const response = await this.client.request(
|
|
125
|
+
"POST",
|
|
126
|
+
this.query
|
|
127
|
+
);
|
|
128
|
+
return response.count;
|
|
129
|
+
} catch (error) {
|
|
130
|
+
console.error("[SDK] COUNT ERROR");
|
|
131
|
+
console.error(error);
|
|
132
|
+
throw error;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
toJSON() {
|
|
136
|
+
return structuredClone(this.query);
|
|
137
|
+
}
|
|
106
138
|
async get() {
|
|
139
|
+
this.query.operation = "find";
|
|
107
140
|
console.log("\n================ GET REQUEST ================");
|
|
108
141
|
console.log("[SDK] Final Query:", this.query);
|
|
109
142
|
try {
|
|
@@ -115,45 +148,133 @@ var QueryBuilder = class {
|
|
|
115
148
|
if (!Array.isArray(response.data)) {
|
|
116
149
|
throw new Error("Invalid response format");
|
|
117
150
|
}
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
console.log("[SDK] Parsed Data:", parsed);
|
|
122
|
-
return parsed;
|
|
151
|
+
console.log("sdk response ", response);
|
|
152
|
+
console.log("[SDK] Data:", response.data);
|
|
153
|
+
return response.data;
|
|
123
154
|
} catch (error) {
|
|
124
155
|
console.error("[SDK] GET ERROR");
|
|
125
156
|
console.error(error);
|
|
126
157
|
throw error;
|
|
127
158
|
}
|
|
128
159
|
}
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
async count() {
|
|
133
|
-
console.log("\n================ COUNT REQUEST ================");
|
|
134
|
-
console.log("[SDK] Final Query for Count:", this.query);
|
|
160
|
+
async insert(data) {
|
|
161
|
+
this.query.operation = "insert";
|
|
162
|
+
console.log("\n================ INSERT REQUEST ================");
|
|
135
163
|
try {
|
|
136
|
-
|
|
137
|
-
"
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
164
|
+
if (data instanceof FormData) {
|
|
165
|
+
console.log("[SDK] FormData detected");
|
|
166
|
+
const payload = new FormData();
|
|
167
|
+
const extractedData = {};
|
|
168
|
+
for (const [key, value] of data.entries()) {
|
|
169
|
+
if (value instanceof File) {
|
|
170
|
+
payload.append(
|
|
171
|
+
key,
|
|
172
|
+
value
|
|
173
|
+
);
|
|
174
|
+
} else {
|
|
175
|
+
extractedData[key] = value;
|
|
176
|
+
}
|
|
142
177
|
}
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
178
|
+
this.query.data = extractedData;
|
|
179
|
+
payload.append(
|
|
180
|
+
"query",
|
|
181
|
+
JSON.stringify(this.query)
|
|
182
|
+
);
|
|
183
|
+
console.log("[SDK] Query:", this.query);
|
|
184
|
+
return await this.client.request(
|
|
185
|
+
"POST",
|
|
186
|
+
payload
|
|
187
|
+
);
|
|
147
188
|
}
|
|
148
|
-
|
|
189
|
+
this.query.data = data;
|
|
190
|
+
console.log("[SDK] Final Query:", this.query);
|
|
191
|
+
return await this.client.request(
|
|
192
|
+
"POST",
|
|
193
|
+
this.query
|
|
194
|
+
);
|
|
149
195
|
} catch (error) {
|
|
150
|
-
console.error("[SDK]
|
|
196
|
+
console.error("[SDK] INSERT ERROR");
|
|
151
197
|
console.error(error);
|
|
152
198
|
throw error;
|
|
153
199
|
}
|
|
154
200
|
}
|
|
155
|
-
|
|
156
|
-
|
|
201
|
+
async UpdateById(id, data) {
|
|
202
|
+
this.query.operation = "update";
|
|
203
|
+
this.query.id = id;
|
|
204
|
+
this.query.data = data;
|
|
205
|
+
console.log("\n================ UPDATE REQUEST ================");
|
|
206
|
+
console.log("[SDK] Final Query:", this.query);
|
|
207
|
+
try {
|
|
208
|
+
const response = await this.client.request(
|
|
209
|
+
"PATCH",
|
|
210
|
+
this.query
|
|
211
|
+
);
|
|
212
|
+
console.log("[SDK] Update Response:", response);
|
|
213
|
+
return response;
|
|
214
|
+
} catch (error) {
|
|
215
|
+
console.error("[SDK] UPDATE ERROR");
|
|
216
|
+
console.error(error);
|
|
217
|
+
throw error;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
//updateMany
|
|
221
|
+
// its just like the get() it will call hte client
|
|
222
|
+
//example sdk.collection("animals").where("price", ">", 1000).updateMany({status : "expensive"}) it will update all the animals whose price is greater than 1000 and set their status to expensive
|
|
223
|
+
async updateMany(data) {
|
|
224
|
+
this.query.operation = "updateMany";
|
|
225
|
+
this.query.data = data;
|
|
226
|
+
console.log("\n================ UPDATE MANY REQUEST ================");
|
|
227
|
+
console.log("[SDK] Final Query:", this.query);
|
|
228
|
+
try {
|
|
229
|
+
const response = await this.client.request(
|
|
230
|
+
"PATCH",
|
|
231
|
+
this.query
|
|
232
|
+
);
|
|
233
|
+
console.log("[SDK] Update Many Response:", response);
|
|
234
|
+
return response;
|
|
235
|
+
} catch (error) {
|
|
236
|
+
console.error("[SDK] UPDATE MANY ERROR");
|
|
237
|
+
console.error(error);
|
|
238
|
+
throw error;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
async DeleteById(id) {
|
|
242
|
+
this.query.operation = "delete";
|
|
243
|
+
this.query.id = id;
|
|
244
|
+
console.log("\n================ DELETE REQUEST ================");
|
|
245
|
+
console.log("[SDK] Final Query:", this.query);
|
|
246
|
+
try {
|
|
247
|
+
const response = await this.client.request(
|
|
248
|
+
"DELETE",
|
|
249
|
+
this.query
|
|
250
|
+
);
|
|
251
|
+
console.log("[SDK] Delete Response:", response);
|
|
252
|
+
return response;
|
|
253
|
+
} catch (error) {
|
|
254
|
+
console.error("[SDK] DELETE ERROR");
|
|
255
|
+
console.error(error);
|
|
256
|
+
throw error;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
//deleteMany
|
|
260
|
+
// its just like the get() it will call hte client
|
|
261
|
+
//example sdk.collection("animals").where("price", ">", 1000).deleteMany() it will delete all the animals whose price is greater than 1000
|
|
262
|
+
async deleteMany() {
|
|
263
|
+
this.query.operation = "deleteMany";
|
|
264
|
+
console.log("\n================ DELETE MANY REQUEST ================");
|
|
265
|
+
console.log("[SDK] Final Query:", this.query);
|
|
266
|
+
try {
|
|
267
|
+
const response = await this.client.request(
|
|
268
|
+
"DELETE",
|
|
269
|
+
this.query
|
|
270
|
+
);
|
|
271
|
+
console.log("[SDK] Delete Many Response:", response);
|
|
272
|
+
return response;
|
|
273
|
+
} catch (error) {
|
|
274
|
+
console.error("[SDK] DELETE MANY ERROR");
|
|
275
|
+
console.error(error);
|
|
276
|
+
throw error;
|
|
277
|
+
}
|
|
157
278
|
}
|
|
158
279
|
};
|
|
159
280
|
|
package/dist/index.mjs
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
import axios from "axios";
|
|
3
3
|
var TDClient = class {
|
|
4
4
|
constructor(apiKey) {
|
|
5
|
+
this.apikey = apiKey;
|
|
5
6
|
this.api = axios.create({
|
|
6
7
|
baseURL: "https://www.thinkingdifferently.dev/api/v1",
|
|
7
8
|
headers: {
|
|
@@ -13,6 +14,14 @@ var TDClient = class {
|
|
|
13
14
|
async request(method, body) {
|
|
14
15
|
try {
|
|
15
16
|
const isFormData = body instanceof FormData;
|
|
17
|
+
if (isFormData) {
|
|
18
|
+
body.append("key", this.apikey);
|
|
19
|
+
} else {
|
|
20
|
+
body = {
|
|
21
|
+
key: this.apikey,
|
|
22
|
+
query: body
|
|
23
|
+
};
|
|
24
|
+
}
|
|
16
25
|
const response = await this.api.request({
|
|
17
26
|
url: "/data",
|
|
18
27
|
method,
|
|
@@ -33,6 +42,7 @@ var QueryBuilder = class {
|
|
|
33
42
|
constructor(collection, client) {
|
|
34
43
|
this.client = client;
|
|
35
44
|
this.query = {
|
|
45
|
+
operation: null,
|
|
36
46
|
collection,
|
|
37
47
|
filters: [],
|
|
38
48
|
limit: null,
|
|
@@ -66,7 +76,30 @@ var QueryBuilder = class {
|
|
|
66
76
|
// build() {
|
|
67
77
|
// return this.query;
|
|
68
78
|
// }
|
|
79
|
+
//to do writing the count method for the query builder
|
|
80
|
+
//the conditions are specified , so now the count method should return the number of documents that match the specified conditions in the query builder
|
|
81
|
+
//means count will first get the data based on the filters and then return the length of the data array as the count of matching documents
|
|
82
|
+
async count() {
|
|
83
|
+
this.query.operation = "count";
|
|
84
|
+
console.log("\n================ COUNT REQUEST ================");
|
|
85
|
+
console.log("[SDK] Final Query:", this.query);
|
|
86
|
+
try {
|
|
87
|
+
const response = await this.client.request(
|
|
88
|
+
"POST",
|
|
89
|
+
this.query
|
|
90
|
+
);
|
|
91
|
+
return response.count;
|
|
92
|
+
} catch (error) {
|
|
93
|
+
console.error("[SDK] COUNT ERROR");
|
|
94
|
+
console.error(error);
|
|
95
|
+
throw error;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
toJSON() {
|
|
99
|
+
return structuredClone(this.query);
|
|
100
|
+
}
|
|
69
101
|
async get() {
|
|
102
|
+
this.query.operation = "find";
|
|
70
103
|
console.log("\n================ GET REQUEST ================");
|
|
71
104
|
console.log("[SDK] Final Query:", this.query);
|
|
72
105
|
try {
|
|
@@ -78,45 +111,133 @@ var QueryBuilder = class {
|
|
|
78
111
|
if (!Array.isArray(response.data)) {
|
|
79
112
|
throw new Error("Invalid response format");
|
|
80
113
|
}
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
console.log("[SDK] Parsed Data:", parsed);
|
|
85
|
-
return parsed;
|
|
114
|
+
console.log("sdk response ", response);
|
|
115
|
+
console.log("[SDK] Data:", response.data);
|
|
116
|
+
return response.data;
|
|
86
117
|
} catch (error) {
|
|
87
118
|
console.error("[SDK] GET ERROR");
|
|
88
119
|
console.error(error);
|
|
89
120
|
throw error;
|
|
90
121
|
}
|
|
91
122
|
}
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
async count() {
|
|
96
|
-
console.log("\n================ COUNT REQUEST ================");
|
|
97
|
-
console.log("[SDK] Final Query for Count:", this.query);
|
|
123
|
+
async insert(data) {
|
|
124
|
+
this.query.operation = "insert";
|
|
125
|
+
console.log("\n================ INSERT REQUEST ================");
|
|
98
126
|
try {
|
|
99
|
-
|
|
100
|
-
"
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
127
|
+
if (data instanceof FormData) {
|
|
128
|
+
console.log("[SDK] FormData detected");
|
|
129
|
+
const payload = new FormData();
|
|
130
|
+
const extractedData = {};
|
|
131
|
+
for (const [key, value] of data.entries()) {
|
|
132
|
+
if (value instanceof File) {
|
|
133
|
+
payload.append(
|
|
134
|
+
key,
|
|
135
|
+
value
|
|
136
|
+
);
|
|
137
|
+
} else {
|
|
138
|
+
extractedData[key] = value;
|
|
139
|
+
}
|
|
105
140
|
}
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
141
|
+
this.query.data = extractedData;
|
|
142
|
+
payload.append(
|
|
143
|
+
"query",
|
|
144
|
+
JSON.stringify(this.query)
|
|
145
|
+
);
|
|
146
|
+
console.log("[SDK] Query:", this.query);
|
|
147
|
+
return await this.client.request(
|
|
148
|
+
"POST",
|
|
149
|
+
payload
|
|
150
|
+
);
|
|
110
151
|
}
|
|
111
|
-
|
|
152
|
+
this.query.data = data;
|
|
153
|
+
console.log("[SDK] Final Query:", this.query);
|
|
154
|
+
return await this.client.request(
|
|
155
|
+
"POST",
|
|
156
|
+
this.query
|
|
157
|
+
);
|
|
112
158
|
} catch (error) {
|
|
113
|
-
console.error("[SDK]
|
|
159
|
+
console.error("[SDK] INSERT ERROR");
|
|
114
160
|
console.error(error);
|
|
115
161
|
throw error;
|
|
116
162
|
}
|
|
117
163
|
}
|
|
118
|
-
|
|
119
|
-
|
|
164
|
+
async UpdateById(id, data) {
|
|
165
|
+
this.query.operation = "update";
|
|
166
|
+
this.query.id = id;
|
|
167
|
+
this.query.data = data;
|
|
168
|
+
console.log("\n================ UPDATE REQUEST ================");
|
|
169
|
+
console.log("[SDK] Final Query:", this.query);
|
|
170
|
+
try {
|
|
171
|
+
const response = await this.client.request(
|
|
172
|
+
"PATCH",
|
|
173
|
+
this.query
|
|
174
|
+
);
|
|
175
|
+
console.log("[SDK] Update Response:", response);
|
|
176
|
+
return response;
|
|
177
|
+
} catch (error) {
|
|
178
|
+
console.error("[SDK] UPDATE ERROR");
|
|
179
|
+
console.error(error);
|
|
180
|
+
throw error;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
//updateMany
|
|
184
|
+
// its just like the get() it will call hte client
|
|
185
|
+
//example sdk.collection("animals").where("price", ">", 1000).updateMany({status : "expensive"}) it will update all the animals whose price is greater than 1000 and set their status to expensive
|
|
186
|
+
async updateMany(data) {
|
|
187
|
+
this.query.operation = "updateMany";
|
|
188
|
+
this.query.data = data;
|
|
189
|
+
console.log("\n================ UPDATE MANY REQUEST ================");
|
|
190
|
+
console.log("[SDK] Final Query:", this.query);
|
|
191
|
+
try {
|
|
192
|
+
const response = await this.client.request(
|
|
193
|
+
"PATCH",
|
|
194
|
+
this.query
|
|
195
|
+
);
|
|
196
|
+
console.log("[SDK] Update Many Response:", response);
|
|
197
|
+
return response;
|
|
198
|
+
} catch (error) {
|
|
199
|
+
console.error("[SDK] UPDATE MANY ERROR");
|
|
200
|
+
console.error(error);
|
|
201
|
+
throw error;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
async DeleteById(id) {
|
|
205
|
+
this.query.operation = "delete";
|
|
206
|
+
this.query.id = id;
|
|
207
|
+
console.log("\n================ DELETE REQUEST ================");
|
|
208
|
+
console.log("[SDK] Final Query:", this.query);
|
|
209
|
+
try {
|
|
210
|
+
const response = await this.client.request(
|
|
211
|
+
"DELETE",
|
|
212
|
+
this.query
|
|
213
|
+
);
|
|
214
|
+
console.log("[SDK] Delete Response:", response);
|
|
215
|
+
return response;
|
|
216
|
+
} catch (error) {
|
|
217
|
+
console.error("[SDK] DELETE ERROR");
|
|
218
|
+
console.error(error);
|
|
219
|
+
throw error;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
//deleteMany
|
|
223
|
+
// its just like the get() it will call hte client
|
|
224
|
+
//example sdk.collection("animals").where("price", ">", 1000).deleteMany() it will delete all the animals whose price is greater than 1000
|
|
225
|
+
async deleteMany() {
|
|
226
|
+
this.query.operation = "deleteMany";
|
|
227
|
+
console.log("\n================ DELETE MANY REQUEST ================");
|
|
228
|
+
console.log("[SDK] Final Query:", this.query);
|
|
229
|
+
try {
|
|
230
|
+
const response = await this.client.request(
|
|
231
|
+
"DELETE",
|
|
232
|
+
this.query
|
|
233
|
+
);
|
|
234
|
+
console.log("[SDK] Delete Many Response:", response);
|
|
235
|
+
return response;
|
|
236
|
+
} catch (error) {
|
|
237
|
+
console.error("[SDK] DELETE MANY ERROR");
|
|
238
|
+
console.error(error);
|
|
239
|
+
throw error;
|
|
240
|
+
}
|
|
120
241
|
}
|
|
121
242
|
};
|
|
122
243
|
|