cocobase 1.1.3 → 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/dist/cjs/core/core.d.ts +238 -1
- package/dist/cjs/core/core.d.ts.map +1 -1
- package/dist/cjs/core/core.js +256 -7
- package/dist/cjs/core/core.js.map +1 -1
- package/dist/cjs/core/file.d.ts +26 -0
- package/dist/cjs/core/file.d.ts.map +1 -1
- package/dist/cjs/core/file.js +22 -0
- package/dist/cjs/core/file.js.map +1 -1
- package/dist/cjs/core/functions.d.ts +70 -0
- package/dist/cjs/core/functions.d.ts.map +1 -1
- package/dist/cjs/core/functions.js +58 -1
- package/dist/cjs/core/functions.js.map +1 -1
- package/dist/cjs/types/types.d.ts +98 -0
- package/dist/cjs/types/types.d.ts.map +1 -1
- package/dist/core/core.d.ts +238 -1
- package/dist/core/core.d.ts.map +1 -1
- package/dist/core/core.js +256 -7
- package/dist/core/core.js.map +1 -1
- package/dist/core/file.d.ts +26 -0
- package/dist/core/file.d.ts.map +1 -1
- package/dist/core/file.js +22 -0
- package/dist/core/file.js.map +1 -1
- package/dist/core/functions.d.ts +70 -0
- package/dist/core/functions.d.ts.map +1 -1
- package/dist/core/functions.js +58 -1
- package/dist/core/functions.js.map +1 -1
- package/dist/types/types.d.ts +98 -0
- package/dist/types/types.d.ts.map +1 -1
- package/package.json +1 -1
package/dist/cjs/core/core.d.ts
CHANGED
|
@@ -1,5 +1,35 @@
|
|
|
1
|
-
import { CocobaseConfig, Document, AppUser, Query, Connection } from "../types/types";
|
|
1
|
+
import { CocobaseConfig, Document, AppUser, Query, AppUserList, GoogleLoginResponse, Connection, AggregateResults, AggregateParams } from "../types/types";
|
|
2
2
|
import { CloudFunction } from "./functions";
|
|
3
|
+
/**
|
|
4
|
+
* Main Cocobase client for interacting with the Cocobase backend API.
|
|
5
|
+
*
|
|
6
|
+
* Provides methods for:
|
|
7
|
+
* - Document CRUD operations (Create, Read, Update, Delete)
|
|
8
|
+
* - User authentication and management
|
|
9
|
+
* - File uploads
|
|
10
|
+
* - Real-time data synchronization
|
|
11
|
+
* - Cloud functions execution
|
|
12
|
+
* - Batch operations
|
|
13
|
+
* - Advanced querying and aggregations
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* // Initialize the client
|
|
18
|
+
* const db = new Cocobase({
|
|
19
|
+
* apiKey: 'your-api-key',
|
|
20
|
+
* projectId: 'your-project-id'
|
|
21
|
+
* });
|
|
22
|
+
*
|
|
23
|
+
* // Create a document
|
|
24
|
+
* await db.createDocument('users', { name: 'John Doe' });
|
|
25
|
+
*
|
|
26
|
+
* // Query documents
|
|
27
|
+
* const users = await db.listDocuments('users', {
|
|
28
|
+
* filters: { status: 'active' },
|
|
29
|
+
* limit: 10
|
|
30
|
+
* });
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
3
33
|
export declare class Cocobase {
|
|
4
34
|
private baseURL;
|
|
5
35
|
apiKey?: string;
|
|
@@ -7,11 +37,63 @@ export declare class Cocobase {
|
|
|
7
37
|
projectId?: string;
|
|
8
38
|
user?: AppUser;
|
|
9
39
|
functions: CloudFunction;
|
|
40
|
+
/**
|
|
41
|
+
* Creates a new Cocobase client instance.
|
|
42
|
+
*
|
|
43
|
+
* @param config - Configuration object for the client
|
|
44
|
+
* @param config.apiKey - Your Cocobase API key (required for most operations)
|
|
45
|
+
* @param config.projectId - Your Cocobase project ID (required for cloud functions)
|
|
46
|
+
* @param config.baseURL - Optional custom base URL (defaults to https://api.cocobase.buzz)
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```typescript
|
|
50
|
+
* const db = new Cocobase({
|
|
51
|
+
* apiKey: 'your-api-key',
|
|
52
|
+
* projectId: 'your-project-id'
|
|
53
|
+
* });
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
10
56
|
constructor(config: CocobaseConfig);
|
|
57
|
+
/**
|
|
58
|
+
* Gets the current authentication token.
|
|
59
|
+
*
|
|
60
|
+
* @returns The current JWT token, or undefined if not authenticated
|
|
61
|
+
*/
|
|
11
62
|
getToken(): string | undefined;
|
|
12
63
|
private request;
|
|
13
64
|
private getErrorSuggestion;
|
|
65
|
+
/**
|
|
66
|
+
* Retrieves a single document by ID from a collection.
|
|
67
|
+
*
|
|
68
|
+
* @template T - The type of the document data
|
|
69
|
+
* @param collection - Name of the collection
|
|
70
|
+
* @param docId - Unique ID of the document
|
|
71
|
+
* @returns Promise resolving to the document with metadata
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* ```typescript
|
|
75
|
+
* const user = await db.getDocument('users', 'user-123');
|
|
76
|
+
* console.log(user.data.name);
|
|
77
|
+
* ```
|
|
78
|
+
*/
|
|
14
79
|
getDocument<T = any>(collection: string, docId: string): Promise<Document<T>>;
|
|
80
|
+
/**
|
|
81
|
+
* Creates a new document in a collection.
|
|
82
|
+
*
|
|
83
|
+
* @template T - The type of the document data
|
|
84
|
+
* @param collection - Name of the collection
|
|
85
|
+
* @param data - Document data to store
|
|
86
|
+
* @returns Promise resolving to the created document with metadata
|
|
87
|
+
*
|
|
88
|
+
* @example
|
|
89
|
+
* ```typescript
|
|
90
|
+
* const newUser = await db.createDocument('users', {
|
|
91
|
+
* name: 'John Doe',
|
|
92
|
+
* email: 'john@example.com',
|
|
93
|
+
* age: 30
|
|
94
|
+
* });
|
|
95
|
+
* ```
|
|
96
|
+
*/
|
|
15
97
|
createDocument<T = any>(collection: string, data: T): Promise<Document<T>>;
|
|
16
98
|
/**
|
|
17
99
|
* Create a document with file uploads
|
|
@@ -42,6 +124,23 @@ export declare class Cocobase {
|
|
|
42
124
|
* ```
|
|
43
125
|
*/
|
|
44
126
|
createDocumentWithFiles<T = any>(collection: string, data: T, files: Record<string, File | File[]>): Promise<Document<T>>;
|
|
127
|
+
/**
|
|
128
|
+
* Updates an existing document in a collection.
|
|
129
|
+
*
|
|
130
|
+
* @template T - The type of the document data
|
|
131
|
+
* @param collection - Name of the collection
|
|
132
|
+
* @param docId - Unique ID of the document to update
|
|
133
|
+
* @param data - Partial document data to update (only specified fields are updated)
|
|
134
|
+
* @returns Promise resolving to the updated document with metadata
|
|
135
|
+
*
|
|
136
|
+
* @example
|
|
137
|
+
* ```typescript
|
|
138
|
+
* await db.updateDocument('users', 'user-123', {
|
|
139
|
+
* age: 31,
|
|
140
|
+
* status: 'active'
|
|
141
|
+
* });
|
|
142
|
+
* ```
|
|
143
|
+
*/
|
|
45
144
|
updateDocument<T = any>(collection: string, docId: string, data: Partial<T>): Promise<Document<T>>;
|
|
46
145
|
/**
|
|
47
146
|
* Update a document with file uploads
|
|
@@ -67,14 +166,125 @@ export declare class Cocobase {
|
|
|
67
166
|
* ```
|
|
68
167
|
*/
|
|
69
168
|
updateDocumentWithFiles<T = any>(collection: string, docId: string, data?: Partial<T>, files?: Record<string, File | File[]>): Promise<Document<T>>;
|
|
169
|
+
/**
|
|
170
|
+
* Deletes a document from a collection.
|
|
171
|
+
*
|
|
172
|
+
* @param collection - Name of the collection
|
|
173
|
+
* @param docId - Unique ID of the document to delete
|
|
174
|
+
* @returns Promise resolving to a success status object
|
|
175
|
+
*
|
|
176
|
+
* @example
|
|
177
|
+
* ```typescript
|
|
178
|
+
* await db.deleteDocument('users', 'user-123');
|
|
179
|
+
* ```
|
|
180
|
+
*/
|
|
70
181
|
deleteDocument(collection: string, docId: string): Promise<{
|
|
71
182
|
success: boolean;
|
|
72
183
|
}>;
|
|
184
|
+
/**
|
|
185
|
+
* Lists documents from a collection with optional filtering and pagination.
|
|
186
|
+
*
|
|
187
|
+
* @template T - The type of the document data
|
|
188
|
+
* @param collection - Name of the collection
|
|
189
|
+
* @param query - Optional query parameters for filtering, sorting, and pagination
|
|
190
|
+
* @returns Promise resolving to an array of documents
|
|
191
|
+
*
|
|
192
|
+
* @example
|
|
193
|
+
* ```typescript
|
|
194
|
+
* // Simple query
|
|
195
|
+
* const users = await db.listDocuments('users', {
|
|
196
|
+
* filters: { status: 'active' },
|
|
197
|
+
* limit: 10
|
|
198
|
+
* });
|
|
199
|
+
*
|
|
200
|
+
* // Advanced query with sorting
|
|
201
|
+
* const posts = await db.listDocuments('posts', {
|
|
202
|
+
* filters: { published: true },
|
|
203
|
+
* sort: 'createdAt',
|
|
204
|
+
* order: 'desc',
|
|
205
|
+
* limit: 20
|
|
206
|
+
* });
|
|
207
|
+
* ```
|
|
208
|
+
*/
|
|
73
209
|
listDocuments<T = any>(collection: string, query?: Query): Promise<Document<T>[]>;
|
|
210
|
+
/**
|
|
211
|
+
* Initializes authentication by restoring the session from local storage.
|
|
212
|
+
* Call this method when your application loads to restore user sessions.
|
|
213
|
+
*
|
|
214
|
+
* @example
|
|
215
|
+
* ```typescript
|
|
216
|
+
* await db.initAuth();
|
|
217
|
+
* if (db.isAuthenticated()) {
|
|
218
|
+
* console.log('User is logged in:', db.user);
|
|
219
|
+
* }
|
|
220
|
+
* ```
|
|
221
|
+
*/
|
|
74
222
|
initAuth(): Promise<void>;
|
|
223
|
+
/**
|
|
224
|
+
* Sets the authentication token and stores it in local storage.
|
|
225
|
+
*
|
|
226
|
+
* @param token - JWT authentication token
|
|
227
|
+
*/
|
|
75
228
|
setToken(token: string): void;
|
|
229
|
+
/**
|
|
230
|
+
* Authenticates a user with email and password.
|
|
231
|
+
*
|
|
232
|
+
* @param email - User's email address
|
|
233
|
+
* @param password - User's password
|
|
234
|
+
* @returns Promise that resolves when login is complete
|
|
235
|
+
*
|
|
236
|
+
* @example
|
|
237
|
+
* ```typescript
|
|
238
|
+
* await db.login('user@example.com', 'password123');
|
|
239
|
+
* console.log('Logged in as:', db.user.email);
|
|
240
|
+
* ```
|
|
241
|
+
*/
|
|
76
242
|
login(email: string, password: string): Promise<void>;
|
|
243
|
+
/**
|
|
244
|
+
* Registers a new user with email, password, and optional additional data.
|
|
245
|
+
*
|
|
246
|
+
* @param email - User's email address
|
|
247
|
+
* @param password - User's password
|
|
248
|
+
* @param data - Optional additional user data
|
|
249
|
+
* @returns Promise that resolves when registration is complete
|
|
250
|
+
*
|
|
251
|
+
* @example
|
|
252
|
+
* ```typescript
|
|
253
|
+
* await db.register('user@example.com', 'password123', {
|
|
254
|
+
* username: 'johndoe',
|
|
255
|
+
* fullName: 'John Doe'
|
|
256
|
+
* });
|
|
257
|
+
* ```
|
|
258
|
+
*/
|
|
77
259
|
register(email: string, password: string, data?: Record<string, any>): Promise<void>;
|
|
260
|
+
/**
|
|
261
|
+
* Initiates Google OAuth login flow.
|
|
262
|
+
*
|
|
263
|
+
* @returns Promise resolving to an object with the Google OAuth URL
|
|
264
|
+
*
|
|
265
|
+
* @example
|
|
266
|
+
* ```typescript
|
|
267
|
+
* const { url } = await db.loginWithGoogle();
|
|
268
|
+
* window.location.href = url; // Redirect to Google login
|
|
269
|
+
* ```
|
|
270
|
+
*/
|
|
271
|
+
loginWithGoogle(): Promise<GoogleLoginResponse>;
|
|
272
|
+
/**
|
|
273
|
+
* Completes the Google OAuth login flow after redirect.
|
|
274
|
+
*
|
|
275
|
+
* @param token - JWT token received from OAuth callback
|
|
276
|
+
* @returns Promise that resolves when login is complete
|
|
277
|
+
*
|
|
278
|
+
* @example
|
|
279
|
+
* ```typescript
|
|
280
|
+
* // After Google redirects back to your app with a token
|
|
281
|
+
* const token = new URLSearchParams(window.location.search).get('token');
|
|
282
|
+
* if (token) {
|
|
283
|
+
* await db.completeGoogleLogin(token);
|
|
284
|
+
* }
|
|
285
|
+
* ```
|
|
286
|
+
*/
|
|
287
|
+
completeGoogleLogin(token: string): Promise<void>;
|
|
78
288
|
/**
|
|
79
289
|
* Register a new user with file uploads (avatar, cover photo, etc.)
|
|
80
290
|
*
|
|
@@ -146,5 +356,32 @@ export declare class Cocobase {
|
|
|
146
356
|
}) => void, connectionName?: string, onOpen?: () => void, onError?: () => void): Connection;
|
|
147
357
|
hasRole(role: string): boolean;
|
|
148
358
|
closeConnection(name: string): void;
|
|
359
|
+
listUsers<T = any>(query?: Query): Promise<AppUserList>;
|
|
360
|
+
getUserById<T = any>(userId: string): Promise<AppUser>;
|
|
361
|
+
deleteDocuments(collection: string, docIds: string[]): Promise<{
|
|
362
|
+
status: string;
|
|
363
|
+
message: string;
|
|
364
|
+
count: number;
|
|
365
|
+
}>;
|
|
366
|
+
createDocuments<T = any>(collection: string, documents: T[]): Promise<Document<T>[]>;
|
|
367
|
+
/**
|
|
368
|
+
* Batch update documents
|
|
369
|
+
*
|
|
370
|
+
* @param collection - Collection name
|
|
371
|
+
* @param updates - Object mapping document IDs to partial update objects.
|
|
372
|
+
* Example: { "docId1": { fieldA: "value" }, "docId2": { fieldB: 2 } }
|
|
373
|
+
*/
|
|
374
|
+
updateDocuments<T = any>(collection: string, updates: Record<string, Partial<T>>): Promise<Document<T>[]>;
|
|
375
|
+
/**
|
|
376
|
+
* Count documents matching filters without returning the documents.
|
|
377
|
+
*
|
|
378
|
+
* Example:
|
|
379
|
+
* await db.countDocuments('users', { status: 'active', age_gte: 18 })
|
|
380
|
+
* // returns { count: 42 }
|
|
381
|
+
*/
|
|
382
|
+
countDocuments(collection: string, query?: Query): Promise<{
|
|
383
|
+
count: number;
|
|
384
|
+
}>;
|
|
385
|
+
aggregateDocuments(collection: string, params: AggregateParams): Promise<AggregateResults>;
|
|
149
386
|
}
|
|
150
387
|
//# sourceMappingURL=core.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"core.d.ts","sourceRoot":"","sources":["../../../src/core/core.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,EACd,QAAQ,EAER,OAAO,EACP,KAAK,EACL,
|
|
1
|
+
{"version":3,"file":"core.d.ts","sourceRoot":"","sources":["../../../src/core/core.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,EACd,QAAQ,EAER,OAAO,EACP,KAAK,EACL,WAAW,EACX,mBAAmB,EACnB,UAAU,EACV,gBAAgB,EAChB,eAAe,EAChB,MAAM,gBAAgB,CAAC;AASxB,OAAO,EAAE,aAAa,EAAE,MAAM,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CAAC,OAAO,CAAC;IAKhD,eAAe,CACnB,UAAU,EAAE,MAAM,EAClB,MAAM,EAAE,MAAM,EAAE,GACf,OAAO,CAAC;QAAE,MAAM,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE,CAAC;IASxD,eAAe,CAAC,CAAC,GAAG,GAAG,EAC3B,UAAU,EAAE,MAAM,EAClB,SAAS,EAAE,CAAC,EAAE,GACb,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAC,EAAE,CAAC;IAQzB;;;;;;OAMG;IACG,eAAe,CAAC,CAAC,GAAG,GAAG,EAC3B,UAAU,EAAE,MAAM,EAClB,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC,GAClC,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAC,EAAE,CAAC;IAQzB;;;;;;OAMG;IACG,cAAc,CAClB,UAAU,EAAE,MAAM,EAClB,KAAK,CAAC,EAAE,KAAK,GACZ,OAAO,CAAC;QAAE,KAAK,EAAE,MAAM,CAAA;KAAE,CAAC;IAUvB,kBAAkB,CAAC,UAAU,EAAE,MAAM,EAAE,MAAM,EAAC,eAAe,GAAE,OAAO,CAAC,gBAAgB,CAAC;CAS/F"}
|
package/dist/cjs/core/core.js
CHANGED
|
@@ -4,13 +4,64 @@ exports.Cocobase = void 0;
|
|
|
4
4
|
const utils_1 = require("../utils/utils");
|
|
5
5
|
const socket_1 = require("../utils/socket");
|
|
6
6
|
const functions_1 = require("./functions");
|
|
7
|
+
/**
|
|
8
|
+
* Main Cocobase client for interacting with the Cocobase backend API.
|
|
9
|
+
*
|
|
10
|
+
* Provides methods for:
|
|
11
|
+
* - Document CRUD operations (Create, Read, Update, Delete)
|
|
12
|
+
* - User authentication and management
|
|
13
|
+
* - File uploads
|
|
14
|
+
* - Real-time data synchronization
|
|
15
|
+
* - Cloud functions execution
|
|
16
|
+
* - Batch operations
|
|
17
|
+
* - Advanced querying and aggregations
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```typescript
|
|
21
|
+
* // Initialize the client
|
|
22
|
+
* const db = new Cocobase({
|
|
23
|
+
* apiKey: 'your-api-key',
|
|
24
|
+
* projectId: 'your-project-id'
|
|
25
|
+
* });
|
|
26
|
+
*
|
|
27
|
+
* // Create a document
|
|
28
|
+
* await db.createDocument('users', { name: 'John Doe' });
|
|
29
|
+
*
|
|
30
|
+
* // Query documents
|
|
31
|
+
* const users = await db.listDocuments('users', {
|
|
32
|
+
* filters: { status: 'active' },
|
|
33
|
+
* limit: 10
|
|
34
|
+
* });
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
7
37
|
class Cocobase {
|
|
38
|
+
/**
|
|
39
|
+
* Creates a new Cocobase client instance.
|
|
40
|
+
*
|
|
41
|
+
* @param config - Configuration object for the client
|
|
42
|
+
* @param config.apiKey - Your Cocobase API key (required for most operations)
|
|
43
|
+
* @param config.projectId - Your Cocobase project ID (required for cloud functions)
|
|
44
|
+
* @param config.baseURL - Optional custom base URL (defaults to https://api.cocobase.buzz)
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```typescript
|
|
48
|
+
* const db = new Cocobase({
|
|
49
|
+
* apiKey: 'your-api-key',
|
|
50
|
+
* projectId: 'your-project-id'
|
|
51
|
+
* });
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
8
54
|
constructor(config) {
|
|
9
55
|
this.baseURL = config.baseURL ?? utils_1.BASEURL;
|
|
10
56
|
this.apiKey = config.apiKey;
|
|
11
57
|
this.projectId = config.projectId;
|
|
12
58
|
this.functions = new functions_1.CloudFunction(config.projectId || "project id required", () => this.token);
|
|
13
59
|
}
|
|
60
|
+
/**
|
|
61
|
+
* Gets the current authentication token.
|
|
62
|
+
*
|
|
63
|
+
* @returns The current JWT token, or undefined if not authenticated
|
|
64
|
+
*/
|
|
14
65
|
getToken() {
|
|
15
66
|
return this.token;
|
|
16
67
|
}
|
|
@@ -70,11 +121,40 @@ class Cocobase {
|
|
|
70
121
|
return "Check the API documentation and verify your request format";
|
|
71
122
|
}
|
|
72
123
|
}
|
|
73
|
-
|
|
124
|
+
/**
|
|
125
|
+
* Retrieves a single document by ID from a collection.
|
|
126
|
+
*
|
|
127
|
+
* @template T - The type of the document data
|
|
128
|
+
* @param collection - Name of the collection
|
|
129
|
+
* @param docId - Unique ID of the document
|
|
130
|
+
* @returns Promise resolving to the document with metadata
|
|
131
|
+
*
|
|
132
|
+
* @example
|
|
133
|
+
* ```typescript
|
|
134
|
+
* const user = await db.getDocument('users', 'user-123');
|
|
135
|
+
* console.log(user.data.name);
|
|
136
|
+
* ```
|
|
137
|
+
*/
|
|
74
138
|
async getDocument(collection, docId) {
|
|
75
139
|
return this.request("GET", `/collections/${collection}/documents/${docId}`);
|
|
76
140
|
}
|
|
77
|
-
|
|
141
|
+
/**
|
|
142
|
+
* Creates a new document in a collection.
|
|
143
|
+
*
|
|
144
|
+
* @template T - The type of the document data
|
|
145
|
+
* @param collection - Name of the collection
|
|
146
|
+
* @param data - Document data to store
|
|
147
|
+
* @returns Promise resolving to the created document with metadata
|
|
148
|
+
*
|
|
149
|
+
* @example
|
|
150
|
+
* ```typescript
|
|
151
|
+
* const newUser = await db.createDocument('users', {
|
|
152
|
+
* name: 'John Doe',
|
|
153
|
+
* email: 'john@example.com',
|
|
154
|
+
* age: 30
|
|
155
|
+
* });
|
|
156
|
+
* ```
|
|
157
|
+
*/
|
|
78
158
|
async createDocument(collection, data) {
|
|
79
159
|
return this.request("POST", `/collections/documents?collection=${collection}`, data);
|
|
80
160
|
}
|
|
@@ -138,7 +218,23 @@ class Cocobase {
|
|
|
138
218
|
}
|
|
139
219
|
return res.json();
|
|
140
220
|
}
|
|
141
|
-
|
|
221
|
+
/**
|
|
222
|
+
* Updates an existing document in a collection.
|
|
223
|
+
*
|
|
224
|
+
* @template T - The type of the document data
|
|
225
|
+
* @param collection - Name of the collection
|
|
226
|
+
* @param docId - Unique ID of the document to update
|
|
227
|
+
* @param data - Partial document data to update (only specified fields are updated)
|
|
228
|
+
* @returns Promise resolving to the updated document with metadata
|
|
229
|
+
*
|
|
230
|
+
* @example
|
|
231
|
+
* ```typescript
|
|
232
|
+
* await db.updateDocument('users', 'user-123', {
|
|
233
|
+
* age: 31,
|
|
234
|
+
* status: 'active'
|
|
235
|
+
* });
|
|
236
|
+
* ```
|
|
237
|
+
*/
|
|
142
238
|
async updateDocument(collection, docId, data) {
|
|
143
239
|
return this.request("PATCH", `/collections/${collection}/documents/${docId}`, data);
|
|
144
240
|
}
|
|
@@ -199,16 +295,62 @@ class Cocobase {
|
|
|
199
295
|
}
|
|
200
296
|
return res.json();
|
|
201
297
|
}
|
|
202
|
-
|
|
298
|
+
/**
|
|
299
|
+
* Deletes a document from a collection.
|
|
300
|
+
*
|
|
301
|
+
* @param collection - Name of the collection
|
|
302
|
+
* @param docId - Unique ID of the document to delete
|
|
303
|
+
* @returns Promise resolving to a success status object
|
|
304
|
+
*
|
|
305
|
+
* @example
|
|
306
|
+
* ```typescript
|
|
307
|
+
* await db.deleteDocument('users', 'user-123');
|
|
308
|
+
* ```
|
|
309
|
+
*/
|
|
203
310
|
async deleteDocument(collection, docId) {
|
|
204
311
|
return this.request("DELETE", `/collections/${collection}/documents/${docId}`);
|
|
205
312
|
}
|
|
206
|
-
|
|
313
|
+
/**
|
|
314
|
+
* Lists documents from a collection with optional filtering and pagination.
|
|
315
|
+
*
|
|
316
|
+
* @template T - The type of the document data
|
|
317
|
+
* @param collection - Name of the collection
|
|
318
|
+
* @param query - Optional query parameters for filtering, sorting, and pagination
|
|
319
|
+
* @returns Promise resolving to an array of documents
|
|
320
|
+
*
|
|
321
|
+
* @example
|
|
322
|
+
* ```typescript
|
|
323
|
+
* // Simple query
|
|
324
|
+
* const users = await db.listDocuments('users', {
|
|
325
|
+
* filters: { status: 'active' },
|
|
326
|
+
* limit: 10
|
|
327
|
+
* });
|
|
328
|
+
*
|
|
329
|
+
* // Advanced query with sorting
|
|
330
|
+
* const posts = await db.listDocuments('posts', {
|
|
331
|
+
* filters: { published: true },
|
|
332
|
+
* sort: 'createdAt',
|
|
333
|
+
* order: 'desc',
|
|
334
|
+
* limit: 20
|
|
335
|
+
* });
|
|
336
|
+
* ```
|
|
337
|
+
*/
|
|
207
338
|
async listDocuments(collection, query) {
|
|
208
339
|
const query_str = (0, utils_1.buildFilterQuery)(query);
|
|
209
340
|
return this.request("GET", `/collections/${collection}/documents${query_str ? `?${query_str}` : ""}`);
|
|
210
341
|
}
|
|
211
|
-
|
|
342
|
+
/**
|
|
343
|
+
* Initializes authentication by restoring the session from local storage.
|
|
344
|
+
* Call this method when your application loads to restore user sessions.
|
|
345
|
+
*
|
|
346
|
+
* @example
|
|
347
|
+
* ```typescript
|
|
348
|
+
* await db.initAuth();
|
|
349
|
+
* if (db.isAuthenticated()) {
|
|
350
|
+
* console.log('User is logged in:', db.user);
|
|
351
|
+
* }
|
|
352
|
+
* ```
|
|
353
|
+
*/
|
|
212
354
|
async initAuth() {
|
|
213
355
|
const token = (0, utils_1.getFromLocalStorage)("cocobase-token");
|
|
214
356
|
const user = (0, utils_1.getFromLocalStorage)("cocobase-user");
|
|
@@ -226,17 +368,51 @@ class Cocobase {
|
|
|
226
368
|
this.token = undefined;
|
|
227
369
|
}
|
|
228
370
|
}
|
|
371
|
+
/**
|
|
372
|
+
* Sets the authentication token and stores it in local storage.
|
|
373
|
+
*
|
|
374
|
+
* @param token - JWT authentication token
|
|
375
|
+
*/
|
|
229
376
|
setToken(token) {
|
|
230
377
|
this.token = token;
|
|
231
378
|
(0, utils_1.setToLocalStorage)("cocobase-token", token);
|
|
232
379
|
}
|
|
380
|
+
/**
|
|
381
|
+
* Authenticates a user with email and password.
|
|
382
|
+
*
|
|
383
|
+
* @param email - User's email address
|
|
384
|
+
* @param password - User's password
|
|
385
|
+
* @returns Promise that resolves when login is complete
|
|
386
|
+
*
|
|
387
|
+
* @example
|
|
388
|
+
* ```typescript
|
|
389
|
+
* await db.login('user@example.com', 'password123');
|
|
390
|
+
* console.log('Logged in as:', db.user.email);
|
|
391
|
+
* ```
|
|
392
|
+
*/
|
|
233
393
|
async login(email, password) {
|
|
234
394
|
const response = this.request(`POST`, `/auth-collections/login`, { email, password }, false // Do not use data key for auth endpoints
|
|
235
395
|
);
|
|
236
396
|
this.token = (await response).access_token;
|
|
237
397
|
this.setToken(this.token);
|
|
238
|
-
|
|
398
|
+
await this.getCurrentUser();
|
|
239
399
|
}
|
|
400
|
+
/**
|
|
401
|
+
* Registers a new user with email, password, and optional additional data.
|
|
402
|
+
*
|
|
403
|
+
* @param email - User's email address
|
|
404
|
+
* @param password - User's password
|
|
405
|
+
* @param data - Optional additional user data
|
|
406
|
+
* @returns Promise that resolves when registration is complete
|
|
407
|
+
*
|
|
408
|
+
* @example
|
|
409
|
+
* ```typescript
|
|
410
|
+
* await db.register('user@example.com', 'password123', {
|
|
411
|
+
* username: 'johndoe',
|
|
412
|
+
* fullName: 'John Doe'
|
|
413
|
+
* });
|
|
414
|
+
* ```
|
|
415
|
+
*/
|
|
240
416
|
async register(email, password, data) {
|
|
241
417
|
const response = this.request(`POST`, `/auth-collections/signup`, { email, password, data }, false // Do not use data key for auth endpoints
|
|
242
418
|
);
|
|
@@ -244,6 +420,39 @@ class Cocobase {
|
|
|
244
420
|
this.setToken(this.token);
|
|
245
421
|
await this.getCurrentUser();
|
|
246
422
|
}
|
|
423
|
+
/**
|
|
424
|
+
* Initiates Google OAuth login flow.
|
|
425
|
+
*
|
|
426
|
+
* @returns Promise resolving to an object with the Google OAuth URL
|
|
427
|
+
*
|
|
428
|
+
* @example
|
|
429
|
+
* ```typescript
|
|
430
|
+
* const { url } = await db.loginWithGoogle();
|
|
431
|
+
* window.location.href = url; // Redirect to Google login
|
|
432
|
+
* ```
|
|
433
|
+
*/
|
|
434
|
+
async loginWithGoogle() {
|
|
435
|
+
return (await this.request(`GET`, "/auth-collections/login-google"));
|
|
436
|
+
}
|
|
437
|
+
/**
|
|
438
|
+
* Completes the Google OAuth login flow after redirect.
|
|
439
|
+
*
|
|
440
|
+
* @param token - JWT token received from OAuth callback
|
|
441
|
+
* @returns Promise that resolves when login is complete
|
|
442
|
+
*
|
|
443
|
+
* @example
|
|
444
|
+
* ```typescript
|
|
445
|
+
* // After Google redirects back to your app with a token
|
|
446
|
+
* const token = new URLSearchParams(window.location.search).get('token');
|
|
447
|
+
* if (token) {
|
|
448
|
+
* await db.completeGoogleLogin(token);
|
|
449
|
+
* }
|
|
450
|
+
* ```
|
|
451
|
+
*/
|
|
452
|
+
async completeGoogleLogin(token) {
|
|
453
|
+
this.setToken(token);
|
|
454
|
+
await this.getCurrentUser();
|
|
455
|
+
}
|
|
247
456
|
/**
|
|
248
457
|
* Register a new user with file uploads (avatar, cover photo, etc.)
|
|
249
458
|
*
|
|
@@ -458,6 +667,46 @@ class Cocobase {
|
|
|
458
667
|
closeConnection(name) {
|
|
459
668
|
(0, socket_1.closeConnection)(name);
|
|
460
669
|
}
|
|
670
|
+
// AUTH COLLECTION ROUTES
|
|
671
|
+
listUsers(query) {
|
|
672
|
+
const query_str = (0, utils_1.buildFilterQuery)(query);
|
|
673
|
+
return this.request("GET", `/auth-collections/users${query_str ? `?${query_str}` : ""}`);
|
|
674
|
+
}
|
|
675
|
+
getUserById(userId) {
|
|
676
|
+
return this.request("GET", `/auth-collections/users/${userId}`);
|
|
677
|
+
}
|
|
678
|
+
// BATCH OPERATIONS
|
|
679
|
+
async deleteDocuments(collection, docIds) {
|
|
680
|
+
return this.request("POST", `/collections/${collection}/batch/documents/delete`, { document_ids: docIds }, false);
|
|
681
|
+
}
|
|
682
|
+
async createDocuments(collection, documents) {
|
|
683
|
+
return this.request("POST", `/collections/${collection}/batch/documents/create`, { documents }, false);
|
|
684
|
+
}
|
|
685
|
+
/**
|
|
686
|
+
* Batch update documents
|
|
687
|
+
*
|
|
688
|
+
* @param collection - Collection name
|
|
689
|
+
* @param updates - Object mapping document IDs to partial update objects.
|
|
690
|
+
* Example: { "docId1": { fieldA: "value" }, "docId2": { fieldB: 2 } }
|
|
691
|
+
*/
|
|
692
|
+
async updateDocuments(collection, updates) {
|
|
693
|
+
return this.request("POST", `/collections/${collection}/batch/documents/update`, { updates }, false);
|
|
694
|
+
}
|
|
695
|
+
/**
|
|
696
|
+
* Count documents matching filters without returning the documents.
|
|
697
|
+
*
|
|
698
|
+
* Example:
|
|
699
|
+
* await db.countDocuments('users', { status: 'active', age_gte: 18 })
|
|
700
|
+
* // returns { count: 42 }
|
|
701
|
+
*/
|
|
702
|
+
async countDocuments(collection, query) {
|
|
703
|
+
const query_str = (0, utils_1.buildFilterQuery)(query);
|
|
704
|
+
return this.request("GET", `/collections/${collection}/query/documents/count${query_str ? `?${query_str}` : ""}`);
|
|
705
|
+
}
|
|
706
|
+
async aggregateDocuments(collection, params) {
|
|
707
|
+
const query_str = (0, utils_1.buildFilterQuery)(params.query);
|
|
708
|
+
return (await this.request("GET", `/collections/${collection}/query/documents/aggregate?field=${params.field}&operation=${params.operation}&${query_str ? `${query_str}` : ""}`));
|
|
709
|
+
}
|
|
461
710
|
}
|
|
462
711
|
exports.Cocobase = Cocobase;
|
|
463
712
|
//# sourceMappingURL=core.js.map
|