@tspvivek/baasix-sdk 0.1.0-alpha.1 → 0.1.0-alpha.2

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/index.js CHANGED
@@ -1605,38 +1605,23 @@ var ItemsModule = class {
1605
1605
  * const fileInput = document.querySelector('input[type="file"]');
1606
1606
  * const file = fileInput.files[0];
1607
1607
  *
1608
- * const result = await baasix.items('products').importCSV(file, {
1609
- * delimiter: ',',
1610
- * skipFirstRow: true
1611
- * });
1608
+ * const result = await baasix.items('products').importCSV(file);
1612
1609
  *
1613
- * console.log(`Imported ${result.created} items`);
1610
+ * console.log(`Imported ${result.imported} items`);
1614
1611
  * ```
1615
1612
  */
1616
- async importCSV(file, options) {
1613
+ async importCSV(file) {
1617
1614
  const formData = new FormData();
1618
1615
  if (file instanceof File) {
1619
- formData.append("file", file);
1616
+ formData.append("csvFile", file);
1620
1617
  } else {
1621
- formData.append("file", file);
1622
- }
1623
- if (options?.delimiter) {
1624
- formData.append("delimiter", options.delimiter);
1625
- }
1626
- if (options?.skipFirstRow !== void 0) {
1627
- formData.append("skipFirstRow", String(options.skipFirstRow));
1628
- }
1629
- if (options?.dateFormat) {
1630
- formData.append("dateFormat", options.dateFormat);
1631
- }
1632
- if (options?.fieldMapping) {
1633
- formData.append("fieldMapping", JSON.stringify(options.fieldMapping));
1618
+ formData.append("csvFile", file);
1634
1619
  }
1635
1620
  const response = await this.client.post(
1636
- `/items/${this.collection}/import/csv`,
1621
+ `/items/${this.collection}/import-csv`,
1637
1622
  formData
1638
1623
  );
1639
- return response.data;
1624
+ return response.results;
1640
1625
  }
1641
1626
  /**
1642
1627
  * Import items from a JSON file
@@ -1646,24 +1631,21 @@ var ItemsModule = class {
1646
1631
  * const file = fileInput.files[0]; // JSON file
1647
1632
  * const result = await baasix.items('products').importJSON(file);
1648
1633
  *
1649
- * console.log(`Imported ${result.created} items`);
1634
+ * console.log(`Imported ${result.imported} items`);
1650
1635
  * ```
1651
1636
  */
1652
- async importJSON(file, options) {
1637
+ async importJSON(file) {
1653
1638
  const formData = new FormData();
1654
1639
  if (file instanceof File) {
1655
- formData.append("file", file);
1640
+ formData.append("jsonFile", file);
1656
1641
  } else {
1657
- formData.append("file", file);
1658
- }
1659
- if (options?.fieldMapping) {
1660
- formData.append("fieldMapping", JSON.stringify(options.fieldMapping));
1642
+ formData.append("jsonFile", file);
1661
1643
  }
1662
1644
  const response = await this.client.post(
1663
- `/items/${this.collection}/import/json`,
1645
+ `/items/${this.collection}/import-json`,
1664
1646
  formData
1665
1647
  );
1666
- return response.data;
1648
+ return response.results;
1667
1649
  }
1668
1650
  /**
1669
1651
  * Import items from an array of objects
@@ -1689,19 +1671,22 @@ var ItemsModule = class {
1689
1671
  // Sort Operations
1690
1672
  // ===================
1691
1673
  /**
1692
- * Sort/reorder items (move item before another)
1674
+ * Sort/reorder items (move item before or after another)
1693
1675
  *
1694
1676
  * @example
1695
1677
  * ```typescript
1696
1678
  * // Move item1 before item2
1697
1679
  * await baasix.items('products').sortItem('item1-uuid', 'item2-uuid');
1680
+ *
1681
+ * // Move item1 after item2
1682
+ * await baasix.items('products').sortItem('item1-uuid', 'item2-uuid', 'after');
1698
1683
  * ```
1699
1684
  */
1700
- async sortItem(itemId, beforeItemId) {
1701
- await this.client.post("/utils/sort", {
1702
- collection: this.collection,
1685
+ async sortItem(itemId, targetItemId, mode = "before") {
1686
+ await this.client.post(`/utils/sort/${this.collection}`, {
1703
1687
  item: itemId,
1704
- to: beforeItemId
1688
+ to: targetItemId,
1689
+ mode
1705
1690
  });
1706
1691
  }
1707
1692
  /**
@@ -1719,10 +1704,10 @@ var ItemsModule = class {
1719
1704
  */
1720
1705
  async reorder(orderedIds) {
1721
1706
  for (let i = 1; i < orderedIds.length; i++) {
1722
- await this.client.post("/utils/sort", {
1723
- collection: this.collection,
1707
+ await this.client.post(`/utils/sort/${this.collection}`, {
1724
1708
  item: orderedIds[i],
1725
- to: orderedIds[i - 1]
1709
+ to: orderedIds[i - 1],
1710
+ mode: "after"
1726
1711
  });
1727
1712
  }
1728
1713
  }
@@ -2002,10 +1987,13 @@ var SchemasModule = class {
2002
1987
  * ```typescript
2003
1988
  * const { data } = await baasix.schemas.find();
2004
1989
  * console.log(data.map(s => s.collectionName));
1990
+ *
1991
+ * // With pagination
1992
+ * const { data } = await baasix.schemas.find({ page: 1, limit: 50 });
2005
1993
  * ```
2006
1994
  */
2007
- async find() {
2008
- return this.client.get("/schemas");
1995
+ async find(params) {
1996
+ return this.client.get("/schemas", { params });
2009
1997
  }
2010
1998
  /**
2011
1999
  * Get schema for a specific collection
@@ -2148,6 +2136,23 @@ var SchemasModule = class {
2148
2136
  `/schemas/${collection}/relationships/${relationshipName}`
2149
2137
  );
2150
2138
  }
2139
+ /**
2140
+ * Update a relationship
2141
+ *
2142
+ * @example
2143
+ * ```typescript
2144
+ * await baasix.schemas.updateRelationship('posts', 'author', {
2145
+ * alias: 'authoredPosts',
2146
+ * onDelete: 'CASCADE'
2147
+ * });
2148
+ * ```
2149
+ */
2150
+ async updateRelationship(collection, relationshipName, data) {
2151
+ await this.client.patch(
2152
+ `/schemas/${collection}/relationships/${relationshipName}`,
2153
+ data
2154
+ );
2155
+ }
2151
2156
  /**
2152
2157
  * Create an index on a collection
2153
2158
  *
@@ -2276,71 +2281,56 @@ var NotificationsModule = class {
2276
2281
  });
2277
2282
  }
2278
2283
  /**
2279
- * Get a single notification by ID
2280
- */
2281
- async findOne(id) {
2282
- const response = await this.client.get(
2283
- `/notifications/${id}`
2284
- );
2285
- return response.data;
2286
- }
2287
- /**
2288
- * Mark a notification as seen
2289
- *
2290
- * @example
2291
- * ```typescript
2292
- * await baasix.notifications.markAsSeen('notification-uuid');
2293
- * ```
2294
- */
2295
- async markAsSeen(id) {
2296
- await this.client.patch(`/notifications/${id}/seen`, { seen: true });
2297
- }
2298
- /**
2299
- * Mark multiple notifications as seen
2300
- *
2301
- * @example
2302
- * ```typescript
2303
- * await baasix.notifications.markManySeen(['id1', 'id2', 'id3']);
2304
- * ```
2305
- */
2306
- async markManySeen(ids) {
2307
- await Promise.all(ids.map((id) => this.markAsSeen(id)));
2308
- }
2309
- /**
2310
- * Mark all notifications as seen
2284
+ * Get unread notification count
2311
2285
  *
2312
2286
  * @example
2313
2287
  * ```typescript
2314
- * await baasix.notifications.markAllSeen();
2288
+ * const count = await baasix.notifications.getUnreadCount();
2315
2289
  * ```
2316
2290
  */
2317
- async markAllSeen() {
2318
- await this.client.post("/notifications/seen-all");
2291
+ async getUnreadCount() {
2292
+ const response = await this.client.get(
2293
+ "/notifications/unread/count"
2294
+ );
2295
+ return response.count;
2319
2296
  }
2320
2297
  /**
2321
- * Get unread notification count
2298
+ * Mark notifications as seen
2322
2299
  *
2323
2300
  * @example
2324
2301
  * ```typescript
2325
- * const count = await baasix.notifications.getUnreadCount();
2302
+ * // Mark specific notifications as seen
2303
+ * await baasix.notifications.markAsSeen(['id1', 'id2']);
2304
+ *
2305
+ * // Mark all notifications as seen
2306
+ * await baasix.notifications.markAsSeen();
2326
2307
  * ```
2327
2308
  */
2328
- async getUnreadCount() {
2329
- const response = await this.client.get(
2330
- "/notifications/unread-count"
2309
+ async markAsSeen(notificationIds) {
2310
+ const response = await this.client.post(
2311
+ "/notifications/mark-seen",
2312
+ { notificationIds }
2331
2313
  );
2332
- return response.data.count;
2314
+ return { count: response.count };
2333
2315
  }
2334
2316
  /**
2335
- * Delete a notification
2317
+ * Delete notifications for the current user
2336
2318
  *
2337
2319
  * @example
2338
2320
  * ```typescript
2339
- * await baasix.notifications.delete('notification-uuid');
2321
+ * // Delete specific notifications
2322
+ * await baasix.notifications.delete(['id1', 'id2']);
2323
+ *
2324
+ * // Delete all notifications
2325
+ * await baasix.notifications.delete();
2340
2326
  * ```
2341
2327
  */
2342
- async delete(id) {
2343
- await this.client.delete(`/notifications/${id}`);
2328
+ async delete(notificationIds) {
2329
+ const response = await this.client.delete(
2330
+ "/notifications",
2331
+ { params: notificationIds ? { notificationIds } : void 0 }
2332
+ );
2333
+ return { count: response.count };
2344
2334
  }
2345
2335
  /**
2346
2336
  * Send a notification to users (requires admin permissions)
@@ -2357,7 +2347,30 @@ var NotificationsModule = class {
2357
2347
  * ```
2358
2348
  */
2359
2349
  async send(data) {
2360
- await this.client.post("/notifications/send", data);
2350
+ const response = await this.client.post(
2351
+ "/notifications/send",
2352
+ data
2353
+ );
2354
+ return { notificationIds: response.notificationIds };
2355
+ }
2356
+ /**
2357
+ * Cleanup old notifications (requires admin permissions)
2358
+ *
2359
+ * @example
2360
+ * ```typescript
2361
+ * // Clean up notifications older than 30 days (default)
2362
+ * await baasix.notifications.cleanup();
2363
+ *
2364
+ * // Clean up notifications older than 7 days
2365
+ * await baasix.notifications.cleanup(7);
2366
+ * ```
2367
+ */
2368
+ async cleanup(days = 30) {
2369
+ const response = await this.client.post(
2370
+ "/notifications/cleanup",
2371
+ { days }
2372
+ );
2373
+ return { count: response.count };
2361
2374
  }
2362
2375
  };
2363
2376
 
@@ -3714,15 +3727,21 @@ var MigrationsModule = class {
3714
3727
  return response.data;
3715
3728
  }
3716
3729
  /**
3717
- * Get all completed migrations
3730
+ * Get all migrations with optional filtering
3718
3731
  *
3719
3732
  * @example
3720
3733
  * ```typescript
3734
+ * // Get all migrations
3721
3735
  * const migrations = await baasix.migrations.list();
3736
+ *
3737
+ * // Get completed migrations
3738
+ * const completed = await baasix.migrations.list({ status: 'completed' });
3722
3739
  * ```
3723
3740
  */
3724
- async list() {
3725
- const response = await this.client.get("/migrations");
3741
+ async list(options) {
3742
+ const response = await this.client.get("/migrations", {
3743
+ params: options
3744
+ });
3726
3745
  return response.data;
3727
3746
  }
3728
3747
  /**
@@ -3740,31 +3759,34 @@ var MigrationsModule = class {
3740
3759
  return response.data;
3741
3760
  }
3742
3761
  /**
3743
- * Check if there are pending migrations
3762
+ * Check if migrations are needed
3744
3763
  *
3745
3764
  * @example
3746
3765
  * ```typescript
3747
- * const needsMigration = await baasix.migrations.hasPending();
3766
+ * const check = await baasix.migrations.check();
3767
+ * if (check.hasPending) {
3768
+ * console.log('Migrations needed');
3769
+ * }
3748
3770
  * ```
3749
3771
  */
3750
- async hasPending() {
3772
+ async check() {
3751
3773
  const response = await this.client.get(
3752
3774
  "/migrations/check"
3753
3775
  );
3754
- return response.data.hasPending;
3776
+ return response.data;
3755
3777
  }
3756
3778
  /**
3757
- * Get a specific migration by name
3779
+ * Get a specific migration by version
3758
3780
  *
3759
3781
  * @example
3760
3782
  * ```typescript
3761
- * const migration = await baasix.migrations.get('20231201_create_users');
3783
+ * const migration = await baasix.migrations.get('20231201000000');
3762
3784
  * ```
3763
3785
  */
3764
- async get(name) {
3786
+ async get(version) {
3765
3787
  try {
3766
3788
  const response = await this.client.get(
3767
- `/migrations/${encodeURIComponent(name)}`
3789
+ `/migrations/${encodeURIComponent(version)}`
3768
3790
  );
3769
3791
  return response.data;
3770
3792
  } catch {
@@ -3776,14 +3798,20 @@ var MigrationsModule = class {
3776
3798
  *
3777
3799
  * @example
3778
3800
  * ```typescript
3801
+ * // Run all pending migrations
3779
3802
  * const result = await baasix.migrations.run();
3780
- * console.log(`Ran ${result.migrationsRun.length} migrations`);
3803
+ *
3804
+ * // Run with options
3805
+ * const result = await baasix.migrations.run({
3806
+ * step: 1, // Run only 1 migration
3807
+ * dryRun: true // Preview without executing
3808
+ * });
3781
3809
  * ```
3782
3810
  */
3783
- async run() {
3811
+ async run(options) {
3784
3812
  const response = await this.client.post(
3785
3813
  "/migrations/run",
3786
- {}
3814
+ options || {}
3787
3815
  );
3788
3816
  return response.data;
3789
3817
  }
@@ -3792,13 +3820,13 @@ var MigrationsModule = class {
3792
3820
  *
3793
3821
  * @example
3794
3822
  * ```typescript
3795
- * const result = await baasix.migrations.rollback('20231201_create_users');
3823
+ * const result = await baasix.migrations.rollback('20231201000000');
3796
3824
  * ```
3797
3825
  */
3798
- async rollback(name) {
3826
+ async rollback(version) {
3799
3827
  const response = await this.client.post(
3800
- "/migrations/rollback",
3801
- { name }
3828
+ `/migrations/rollback/${encodeURIComponent(version)}`,
3829
+ {}
3802
3830
  );
3803
3831
  return response.data;
3804
3832
  }
@@ -3807,12 +3835,12 @@ var MigrationsModule = class {
3807
3835
  *
3808
3836
  * @example
3809
3837
  * ```typescript
3810
- * const result = await baasix.migrations.rollbackLast();
3838
+ * const result = await baasix.migrations.rollbackBatch();
3811
3839
  * ```
3812
3840
  */
3813
- async rollbackLast() {
3841
+ async rollbackBatch() {
3814
3842
  const response = await this.client.post(
3815
- "/migrations/rollback-last",
3843
+ "/migrations/rollback-batch",
3816
3844
  {}
3817
3845
  );
3818
3846
  return response.data;
@@ -3822,37 +3850,91 @@ var MigrationsModule = class {
3822
3850
  *
3823
3851
  * @example
3824
3852
  * ```typescript
3825
- * const migrationName = await baasix.migrations.create('add_status_column');
3853
+ * const { filepath } = await baasix.migrations.create('add_status_column', {
3854
+ * type: 'schema',
3855
+ * description: 'Add status column to orders'
3856
+ * });
3826
3857
  * ```
3827
3858
  */
3828
- async create(name) {
3859
+ async create(name, options) {
3829
3860
  const response = await this.client.post(
3830
3861
  "/migrations/create",
3831
- { name }
3862
+ { name, ...options }
3832
3863
  );
3833
- return response.data.name;
3864
+ return response.data;
3834
3865
  }
3835
3866
  /**
3836
- * Mark a migration as complete (without running it)
3867
+ * Mark a specific migration as completed without running it
3868
+ * Useful for existing installations that already have the changes
3837
3869
  *
3838
3870
  * @example
3839
3871
  * ```typescript
3840
- * await baasix.migrations.markComplete('20231201_create_users');
3872
+ * await baasix.migrations.markCompleted('20231201000000');
3841
3873
  * ```
3842
3874
  */
3843
- async markComplete(name) {
3844
- await this.client.post("/migrations/mark-complete", { name });
3875
+ async markCompleted(version, metadata) {
3876
+ const response = await this.client.post(
3877
+ `/migrations/mark-completed/${encodeURIComponent(version)}`,
3878
+ { metadata }
3879
+ );
3880
+ return response.data;
3845
3881
  }
3846
3882
  /**
3847
- * Mark all pending migrations as complete
3883
+ * Mark all pending migrations as completed
3884
+ * Useful for bringing an existing database up to date without running migrations
3848
3885
  *
3849
3886
  * @example
3850
3887
  * ```typescript
3851
- * await baasix.migrations.markAllComplete();
3888
+ * // Mark all pending
3889
+ * await baasix.migrations.markAllCompleted();
3890
+ *
3891
+ * // Mark up to a specific version
3892
+ * await baasix.migrations.markAllCompleted('20231201000000');
3893
+ * ```
3894
+ */
3895
+ async markAllCompleted(toVersion) {
3896
+ const response = await this.client.post(
3897
+ "/migrations/mark-all-completed",
3898
+ { toVersion }
3899
+ );
3900
+ return response.data;
3901
+ }
3902
+ };
3903
+
3904
+ // src/modules/server.ts
3905
+ var ServerModule = class {
3906
+ client;
3907
+ constructor(config) {
3908
+ this.client = config.client;
3909
+ }
3910
+ /**
3911
+ * Get server information including project settings
3912
+ *
3913
+ * @example
3914
+ * ```typescript
3915
+ * const info = await baasix.server.info();
3916
+ * console.log('Project:', info.project?.name);
3917
+ * console.log('Version:', info.version);
3918
+ * ```
3919
+ */
3920
+ async info() {
3921
+ return this.client.get("/");
3922
+ }
3923
+ /**
3924
+ * Check server health
3925
+ *
3926
+ * @example
3927
+ * ```typescript
3928
+ * const isHealthy = await baasix.server.health();
3852
3929
  * ```
3853
3930
  */
3854
- async markAllComplete() {
3855
- await this.client.post("/migrations/mark-all-complete", {});
3931
+ async health() {
3932
+ try {
3933
+ await this.client.get("/health");
3934
+ return true;
3935
+ } catch {
3936
+ return false;
3937
+ }
3856
3938
  }
3857
3939
  };
3858
3940
 
@@ -3931,6 +4013,7 @@ var Baasix = class {
3931
4013
  roles;
3932
4014
  users;
3933
4015
  migrations;
4016
+ server;
3934
4017
  // Items module factory cache
3935
4018
  itemsModules = /* @__PURE__ */ new Map();
3936
4019
  constructor(config) {
@@ -3979,6 +4062,7 @@ var Baasix = class {
3979
4062
  this.roles = new RolesModule({ client: this.httpClient });
3980
4063
  this.users = new UsersModule({ client: this.httpClient });
3981
4064
  this.migrations = new MigrationsModule({ client: this.httpClient });
4065
+ this.server = new ServerModule({ client: this.httpClient });
3982
4066
  this.realtime = new RealtimeModule({
3983
4067
  client: this.httpClient,
3984
4068
  storage: this.storage,
@@ -4107,6 +4191,23 @@ var Baasix = class {
4107
4191
  this.httpClient.updateConfig({ tenantId: void 0 });
4108
4192
  await this.storage.remove(STORAGE_KEYS.TENANT);
4109
4193
  }
4194
+ /**
4195
+ * Set a static token (convenience method that delegates to auth.setToken)
4196
+ *
4197
+ * @example
4198
+ * ```typescript
4199
+ * baasix.setToken('your-api-token');
4200
+ * ```
4201
+ */
4202
+ async setToken(token) {
4203
+ return this.auth.setToken(token);
4204
+ }
4205
+ /**
4206
+ * Get the current auth token
4207
+ */
4208
+ async getToken() {
4209
+ return this.storage.get(STORAGE_KEYS.ACCESS_TOKEN);
4210
+ }
4110
4211
  /**
4111
4212
  * Get the base URL
4112
4213
  */