@techfinityedge/koolbase-react-native 2.1.0 → 2.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 CHANGED
@@ -162,6 +162,20 @@ await Koolbase.db.update('record-id', { title: 'Updated' });
162
162
  await Koolbase.db.delete('record-id');
163
163
  ```
164
164
 
165
+ ### Handling unique-constraint conflicts
166
+
167
+ A write that would violate a unique constraint throws `KoolbaseConflictError`:
168
+
169
+ \`\`\`ts
170
+ try {
171
+ await koolbase.db.upsert('users', { email }, { name });
172
+ } catch (e) {
173
+ if (e instanceof KoolbaseConflictError) {
174
+ showError('That email is already registered.');
175
+ }
176
+ }
177
+ \`\`\`
178
+
165
179
  ### Upsert
166
180
 
167
181
  Insert a record, or update the existing one matching a filter.
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Thrown when a write is rejected because the value would violate a
3
+ * collection's unique constraint — the server responds with 409 Conflict.
4
+ * Catch it to handle duplicates, e.g. an email or username already in use.
5
+ *
6
+ * Currently surfaced by `upsert` (the online-only write). `insert` and
7
+ * `update` are optimistic/offline-first: they accept the write locally and
8
+ * sync in the background, so a constraint conflict on those paths is a
9
+ * sync-time concern rather than a thrown error.
10
+ *
11
+ * @example
12
+ * try {
13
+ * await koolbase.db.upsert('users', { email }, { name });
14
+ * } catch (e) {
15
+ * if (e instanceof KoolbaseConflictError) {
16
+ * showError('That email is already registered.');
17
+ * }
18
+ * }
19
+ */
20
+ export declare class KoolbaseConflictError extends Error {
21
+ code: string;
22
+ constructor(message?: string);
23
+ }
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.KoolbaseConflictError = void 0;
4
+ /**
5
+ * Thrown when a write is rejected because the value would violate a
6
+ * collection's unique constraint — the server responds with 409 Conflict.
7
+ * Catch it to handle duplicates, e.g. an email or username already in use.
8
+ *
9
+ * Currently surfaced by `upsert` (the online-only write). `insert` and
10
+ * `update` are optimistic/offline-first: they accept the write locally and
11
+ * sync in the background, so a constraint conflict on those paths is a
12
+ * sync-time concern rather than a thrown error.
13
+ *
14
+ * @example
15
+ * try {
16
+ * await koolbase.db.upsert('users', { email }, { name });
17
+ * } catch (e) {
18
+ * if (e instanceof KoolbaseConflictError) {
19
+ * showError('That email is already registered.');
20
+ * }
21
+ * }
22
+ */
23
+ class KoolbaseConflictError extends Error {
24
+ constructor(message) {
25
+ super(message ?? 'Value violates a unique constraint');
26
+ this.code = 'unique_violation';
27
+ this.name = 'KoolbaseConflictError';
28
+ Object.setPrototypeOf(this, KoolbaseConflictError.prototype);
29
+ }
30
+ }
31
+ exports.KoolbaseConflictError = KoolbaseConflictError;
package/dist/database.js CHANGED
@@ -4,6 +4,7 @@ exports.KoolbaseDatabase = void 0;
4
4
  const cache_store_1 = require("./cache-store");
5
5
  const sync_engine_1 = require("./sync-engine");
6
6
  const record_1 = require("./record");
7
+ const database_errors_1 = require("./database-errors");
7
8
  function generateId() {
8
9
  return 'local_' + Math.random().toString(36).slice(2) + Date.now().toString(36);
9
10
  }
@@ -121,6 +122,9 @@ class KoolbaseDatabase {
121
122
  });
122
123
  const body = await res.json();
123
124
  if (!res.ok) {
125
+ if (res.status === 409) {
126
+ throw new database_errors_1.KoolbaseConflictError(body.error);
127
+ }
124
128
  throw new Error(body.error ?? `Upsert failed: ${res.status}`);
125
129
  }
126
130
  const created = res.status === 201;
package/dist/index.d.ts CHANGED
@@ -18,6 +18,7 @@ import { KoolbaseStorage } from './storage';
18
18
  import { KoolbaseConfig, VersionCheckResult } from './types';
19
19
  export * from './types';
20
20
  export * from './auth-errors';
21
+ export * from './database-errors';
21
22
  export { KoolbaseAuth, KoolbaseDatabase, KoolbaseFlags, KoolbaseFunctions, KoolbaseRealtime, KoolbaseStorage };
22
23
  export declare const Koolbase: {
23
24
  initialize(config: KoolbaseConfig): Promise<void>;
package/dist/index.js CHANGED
@@ -45,6 +45,7 @@ const storage_1 = require("./storage");
45
45
  Object.defineProperty(exports, "KoolbaseStorage", { enumerable: true, get: function () { return storage_1.KoolbaseStorage; } });
46
46
  __exportStar(require("./types"), exports);
47
47
  __exportStar(require("./auth-errors"), exports);
48
+ __exportStar(require("./database-errors"), exports);
48
49
  let _auth = null;
49
50
  let _db = null;
50
51
  let _storage = null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@techfinityedge/koolbase-react-native",
3
- "version": "2.1.0",
3
+ "version": "2.2.0",
4
4
  "description": "React Native SDK for Koolbase \u2014 auth, database, storage, realtime, feature flags, and functions in one package.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",