@ragable/sdk 0.6.15 → 0.6.16

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.mjs CHANGED
@@ -2196,6 +2196,51 @@ var RagableBrowserAuthClient = class {
2196
2196
  return this.auth.getSession();
2197
2197
  }
2198
2198
  };
2199
+ var BrowserCollectionApi = class {
2200
+ constructor(database, name, databaseInstanceId) {
2201
+ this.database = database;
2202
+ this.name = name;
2203
+ this.databaseInstanceId = databaseInstanceId;
2204
+ __publicField(this, "find", (whereOrParams = {}) => {
2205
+ const hasQueryKeys = ["where", "filters", "limit", "offset", "orderBy", "orderDirection"].some(
2206
+ (key) => Object.prototype.hasOwnProperty.call(whereOrParams, key)
2207
+ );
2208
+ const body = hasQueryKeys ? whereOrParams : { where: whereOrParams };
2209
+ return asPostgrestResponse(
2210
+ () => this.database._requestCollection(
2211
+ "POST",
2212
+ `/${encodeURIComponent(this.name)}/find`,
2213
+ body,
2214
+ this.databaseInstanceId
2215
+ )
2216
+ );
2217
+ });
2218
+ __publicField(this, "insert", (data) => asPostgrestResponse(
2219
+ () => this.database._requestCollection(
2220
+ "POST",
2221
+ `/${encodeURIComponent(this.name)}/records`,
2222
+ { data },
2223
+ this.databaseInstanceId
2224
+ )
2225
+ ));
2226
+ __publicField(this, "update", (where, patch, options) => asPostgrestResponse(
2227
+ () => this.database._requestCollection(
2228
+ "PATCH",
2229
+ `/${encodeURIComponent(this.name)}/records`,
2230
+ { where, patch, ...options?.limit ? { limit: options.limit } : {} },
2231
+ this.databaseInstanceId
2232
+ )
2233
+ ));
2234
+ __publicField(this, "delete", (where, options) => asPostgrestResponse(
2235
+ () => this.database._requestCollection(
2236
+ "DELETE",
2237
+ `/${encodeURIComponent(this.name)}/records`,
2238
+ { where, ...options?.limit ? { limit: options.limit } : {} },
2239
+ this.databaseInstanceId
2240
+ )
2241
+ ));
2242
+ }
2243
+ };
2199
2244
  var RagableBrowserDatabaseClient = class {
2200
2245
  constructor(options, ragableAuth = null) {
2201
2246
  this.options = options;
@@ -2255,6 +2300,25 @@ var RagableBrowserDatabaseClient = class {
2255
2300
  };
2256
2301
  return new PostgrestTableApi(pgFetch, id, table);
2257
2302
  });
2303
+ __publicField(this, "collection", (name, databaseInstanceId) => {
2304
+ return new BrowserCollectionApi(this, name, databaseInstanceId);
2305
+ });
2306
+ __publicField(this, "defineCollection", (name, schema, databaseInstanceId) => asPostgrestResponse(
2307
+ () => this._requestCollection(
2308
+ "POST",
2309
+ "/",
2310
+ { name, ...schema ? { schema } : {} },
2311
+ databaseInstanceId
2312
+ )
2313
+ ));
2314
+ __publicField(this, "listCollections", (databaseInstanceId) => asPostgrestResponse(
2315
+ () => this._requestCollection(
2316
+ "GET",
2317
+ "/",
2318
+ void 0,
2319
+ databaseInstanceId
2320
+ )
2321
+ ));
2258
2322
  __publicField(this, "query", async (params) => {
2259
2323
  return asPostgrestResponse(async () => {
2260
2324
  const gid = requireAuthGroupId(this.options);
@@ -2329,6 +2393,36 @@ var RagableBrowserDatabaseClient = class {
2329
2393
  toUrl(path) {
2330
2394
  return `${normalizeBrowserApiBase()}${path.startsWith("/") ? path : `/${path}`}`;
2331
2395
  }
2396
+ async _requestCollection(method, path, body, databaseInstanceId) {
2397
+ const gid = requireAuthGroupId(this.options);
2398
+ const token = await resolveDatabaseAuthBearer(this.options, this.ragableAuth);
2399
+ const id = databaseInstanceId?.trim() || this.options.databaseInstanceId?.trim();
2400
+ if (!id) {
2401
+ throw new RagableError(
2402
+ "db.collection() requires databaseInstanceId in client options or as an argument",
2403
+ 400,
2404
+ { code: "SDK_MISSING_DATABASE_INSTANCE_ID" }
2405
+ );
2406
+ }
2407
+ const headers = this.baseHeaders();
2408
+ headers.set("Authorization", `Bearer ${token}`);
2409
+ headers.set("X-Database-Instance-Id", id);
2410
+ if (body !== void 0) headers.set("Content-Type", "application/json");
2411
+ const response = await this.fetchImpl(
2412
+ this.toUrl(`/auth-groups/${gid}/data/collections${path}`),
2413
+ {
2414
+ method,
2415
+ headers,
2416
+ body: body !== void 0 ? JSON.stringify(body) : void 0
2417
+ }
2418
+ );
2419
+ const payload = await parseMaybeJsonBody(response);
2420
+ if (!response.ok) {
2421
+ const message = extractErrorMessage(payload, response.statusText);
2422
+ throw new RagableError(message, response.status, payload);
2423
+ }
2424
+ return payload;
2425
+ }
2332
2426
  baseHeaders() {
2333
2427
  return new Headers(this.options.headers);
2334
2428
  }
@@ -2561,6 +2655,7 @@ var RagableBrowser = class {
2561
2655
  __publicField(this, "agents");
2562
2656
  __publicField(this, "auth");
2563
2657
  __publicField(this, "database");
2658
+ __publicField(this, "db");
2564
2659
  __publicField(this, "transport");
2565
2660
  __publicField(this, "_ragableAuth");
2566
2661
  /** Delegates to `database.from()`. Kept for back-compat — prefer `database.from()`. */
@@ -2600,6 +2695,7 @@ var RagableBrowser = class {
2600
2695
  this._ragableAuth
2601
2696
  );
2602
2697
  this.database._setTransport(this.transport);
2698
+ this.db = this.database;
2603
2699
  }
2604
2700
  destroy() {
2605
2701
  this._ragableAuth?.destroy();
@@ -2673,7 +2769,7 @@ function createClient(options) {
2673
2769
  if (isServerClientOptions(options)) {
2674
2770
  if (typeof options === "object" && options !== null && "organizationId" in options && typeof options.organizationId === "string") {
2675
2771
  console.warn(
2676
- "[@ragable/sdk] createClient: `apiKey` is set, so the server client is returned. It has no `database` or `auth` \u2014 only `agents` and `shift`. For `database.from()` / `auth.*`, use the browser client without `apiKey` (e.g. createClient({ organizationId, authGroupId, databaseInstanceId, ... }))."
2772
+ "[@ragable/sdk] createClient: `apiKey` is set, so the server client is returned. It has no `database` or `auth` \u2014 only `agents` and `shift`. For `db.collection()` / `database.from()` / `auth.*`, use the browser client without `apiKey` (e.g. createClient({ organizationId, authGroupId, databaseInstanceId, ... }))."
2677
2773
  );
2678
2774
  }
2679
2775
  return new Ragable(options);