@ragable/sdk 0.6.20 → 0.6.21

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
@@ -627,6 +627,17 @@ async function parseTransportResponse(response) {
627
627
  }
628
628
 
629
629
  // src/browser-postgrest.ts
630
+ function toRagableResult(r) {
631
+ if (r.error) return { ok: false, error: r.error };
632
+ return { ok: true, value: r.data };
633
+ }
634
+ function assertPostgrestSuccess(r) {
635
+ if (r.error) throw r.error;
636
+ }
637
+ function unwrapPostgrest(r) {
638
+ if (r.error) throw r.error;
639
+ return r.data;
640
+ }
630
641
  async function asPostgrestResponse(fn) {
631
642
  try {
632
643
  const data = await fn();
@@ -2212,24 +2223,71 @@ var RagableBrowserAuthClient = class {
2212
2223
  return this.auth.getSession();
2213
2224
  }
2214
2225
  };
2226
+ function collectionRecordToRowWithMeta(record) {
2227
+ const { data, id, createdAt, updatedAt } = record;
2228
+ return { ...data, meta: { id, createdAt, updatedAt } };
2229
+ }
2230
+ function collectionRecordsToRowWithMeta(records) {
2231
+ return records.map(collectionRecordToRowWithMeta);
2232
+ }
2233
+ var FIND_QUERY_KEYS = [
2234
+ "where",
2235
+ "filters",
2236
+ "limit",
2237
+ "offset",
2238
+ "orderBy",
2239
+ "orderDirection",
2240
+ "return"
2241
+ ];
2215
2242
  var BrowserCollectionApi = class {
2216
2243
  constructor(database, name, databaseInstanceId) {
2217
2244
  this.database = database;
2218
2245
  this.name = name;
2219
2246
  this.databaseInstanceId = databaseInstanceId;
2220
- __publicField(this, "find", (whereOrParams = {}) => {
2221
- const hasQueryKeys = ["where", "filters", "limit", "offset", "orderBy", "orderDirection"].some(
2222
- (key) => Object.prototype.hasOwnProperty.call(whereOrParams, key)
2223
- );
2224
- const body = hasQueryKeys ? whereOrParams : { where: whereOrParams };
2225
- return asPostgrestResponse(
2226
- () => this.database._requestCollection(
2227
- "POST",
2228
- `/${encodeURIComponent(this.name)}/find`,
2229
- body,
2230
- this.databaseInstanceId
2231
- )
2232
- );
2247
+ __publicField(this, "requestFind", (body) => asPostgrestResponse(
2248
+ () => this.database._requestCollection(
2249
+ "POST",
2250
+ `/${encodeURIComponent(this.name)}/find`,
2251
+ body,
2252
+ this.databaseInstanceId
2253
+ )
2254
+ ));
2255
+ /**
2256
+ * Query collection rows. Prefer this over the deprecated `find` alias.
2257
+ * Use `return: "flat"` to get {@link CollectionRowWithMeta} without nested `.data`.
2258
+ */
2259
+ __publicField(this, "findMany", async (whereOrParams = {}) => {
2260
+ const { returnMode, body } = this.normalizeFindArgs(whereOrParams);
2261
+ const res = await this.requestFind(body);
2262
+ if (res.error) return res;
2263
+ if (returnMode === "flat") {
2264
+ return {
2265
+ data: collectionRecordsToRowWithMeta(res.data),
2266
+ error: null
2267
+ };
2268
+ }
2269
+ return res;
2270
+ });
2271
+ /**
2272
+ * @deprecated Use {@link BrowserCollectionApi.findMany} — same behavior.
2273
+ */
2274
+ __publicField(this, "find", (whereOrParams = {}) => this.findMany(whereOrParams));
2275
+ /**
2276
+ * At most one row, `data` is the record or `null` if none match (not an error).
2277
+ */
2278
+ __publicField(this, "findFirst", async (whereOrParams = {}) => {
2279
+ const { body } = this.normalizeFindArgs(whereOrParams);
2280
+ const withCap = { ...body, limit: 1, offset: body["offset"] ?? 0 };
2281
+ const res = await this.requestFind(withCap);
2282
+ if (res.error) return res;
2283
+ return { data: res.data[0] ?? null, error: null };
2284
+ });
2285
+ /**
2286
+ * Lookup by primary key `id` (envelope). Equivalent to
2287
+ * `findFirst({ where: { id }, limit: 1 })` with a typed `where.id`.
2288
+ */
2289
+ __publicField(this, "findUnique", async (args) => {
2290
+ return this.findFirst({ where: args.where });
2233
2291
  });
2234
2292
  __publicField(this, "insert", (data) => asPostgrestResponse(
2235
2293
  () => this.database._requestCollection(
@@ -2239,6 +2297,9 @@ var BrowserCollectionApi = class {
2239
2297
  this.databaseInstanceId
2240
2298
  )
2241
2299
  ));
2300
+ /**
2301
+ * Update rows matching `where` (JSON fields, plus envelope `id` / `createdAt` / `updatedAt`).
2302
+ */
2242
2303
  __publicField(this, "update", (where, patch, options) => asPostgrestResponse(
2243
2304
  () => this.database._requestCollection(
2244
2305
  "PATCH",
@@ -2247,6 +2308,15 @@ var BrowserCollectionApi = class {
2247
2308
  this.databaseInstanceId
2248
2309
  )
2249
2310
  ));
2311
+ /**
2312
+ * Like {@link BrowserCollectionApi.update} but the success payload includes
2313
+ * `meta.count` (number of rows returned from the update, bounded by `limit`).
2314
+ */
2315
+ __publicField(this, "updateMany", async (where, patch, options) => {
2316
+ const r = await this.update(where, patch, options);
2317
+ if (r.error) return r;
2318
+ return { data: { records: r.data, meta: { count: r.data.length } }, error: null };
2319
+ });
2250
2320
  __publicField(this, "delete", (where, options) => asPostgrestResponse(
2251
2321
  () => this.database._requestCollection(
2252
2322
  "DELETE",
@@ -2256,6 +2326,15 @@ var BrowserCollectionApi = class {
2256
2326
  )
2257
2327
  ));
2258
2328
  }
2329
+ normalizeFindArgs(whereOrParams) {
2330
+ const hasQueryKeys = typeof whereOrParams === "object" && whereOrParams !== null && FIND_QUERY_KEYS.some(
2331
+ (key) => Object.prototype.hasOwnProperty.call(whereOrParams, key)
2332
+ );
2333
+ const raw = hasQueryKeys ? { ...whereOrParams } : { where: whereOrParams };
2334
+ const returnMode = raw["return"] === "flat" ? "flat" : "envelope";
2335
+ delete raw["return"];
2336
+ return { returnMode, body: raw };
2337
+ }
2259
2338
  };
2260
2339
  var RagableBrowserDatabaseClient = class {
2261
2340
  constructor(options, ragableAuth = null) {
@@ -2848,7 +2927,10 @@ export {
2848
2927
  ShiftClient,
2849
2928
  Transport,
2850
2929
  asPostgrestResponse,
2930
+ assertPostgrestSuccess,
2851
2931
  bindFetch,
2932
+ collectionRecordToRowWithMeta,
2933
+ collectionRecordsToRowWithMeta,
2852
2934
  createBrowserClient,
2853
2935
  createClient,
2854
2936
  createRagPipeline,
@@ -2864,6 +2946,8 @@ export {
2864
2946
  normalizeBrowserApiBase,
2865
2947
  parseSseDataLine,
2866
2948
  parseTransportResponse,
2867
- readSseStream
2949
+ readSseStream,
2950
+ toRagableResult,
2951
+ unwrapPostgrest
2868
2952
  };
2869
2953
  //# sourceMappingURL=index.mjs.map