@koolbase/react-native 12.5.1 → 12.6.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.
@@ -29,3 +29,15 @@ export type { UseCollectionResult } from './use-collection.js';
29
29
  * ```
30
30
  */
31
31
  export declare const useCollection: (collection: string, query?: import("@koolbase/core").CollectionQuery) => import("./use-collection.js").UseCollectionResult;
32
+ export type { UseRecordResult } from './use-record.js';
33
+ /**
34
+ * One record, by id, as React state: loading, loaded, notFound or error,
35
+ * with refresh. notFound covers a missing id, a record that does not exist,
36
+ * one this user may not read (the API does not tell them apart) and a record
37
+ * from a different collection. Use it after Koolbase.initialize().
38
+ *
39
+ * ```tsx
40
+ * const song = useRecord('songs', songId);
41
+ * ```
42
+ */
43
+ export declare const useRecord: (collection: string, id: string | null | undefined) => import("./use-record.js").UseRecordResult;
package/dist/cjs/index.js CHANGED
@@ -20,13 +20,14 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
20
20
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
21
21
  };
22
22
  Object.defineProperty(exports, "__esModule", { value: true });
23
- exports.useCollection = exports.SecureAuthStorage = exports.Koolbase = exports.reactNativePlatform = void 0;
23
+ exports.useRecord = exports.useCollection = exports.SecureAuthStorage = exports.Koolbase = exports.reactNativePlatform = void 0;
24
24
  __exportStar(require("@koolbase/core"), exports);
25
25
  var platform_js_1 = require("./platform.js");
26
26
  Object.defineProperty(exports, "reactNativePlatform", { enumerable: true, get: function () { return platform_js_1.reactNativePlatform; } });
27
27
  const core_1 = require("@koolbase/core");
28
28
  const platform_js_2 = require("./platform.js");
29
29
  const use_collection_js_1 = require("./use-collection.js");
30
+ const use_record_js_1 = require("./use-record.js");
30
31
  let _auth = null;
31
32
  let _db = null;
32
33
  let _storage = null;
@@ -164,3 +165,14 @@ Object.defineProperty(exports, "SecureAuthStorage", { enumerable: true, get: fun
164
165
  * ```
165
166
  */
166
167
  exports.useCollection = (0, use_collection_js_1.createUseCollection)(() => exports.Koolbase.db);
168
+ /**
169
+ * One record, by id, as React state: loading, loaded, notFound or error,
170
+ * with refresh. notFound covers a missing id, a record that does not exist,
171
+ * one this user may not read (the API does not tell them apart) and a record
172
+ * from a different collection. Use it after Koolbase.initialize().
173
+ *
174
+ * ```tsx
175
+ * const song = useRecord('songs', songId);
176
+ * ```
177
+ */
178
+ exports.useRecord = (0, use_record_js_1.createUseRecord)(() => exports.Koolbase.db);
@@ -0,0 +1,6 @@
1
+ import { type KoolbaseDatabase, type RecordState } from '@koolbase/core';
2
+ export interface UseRecordResult extends RecordState {
3
+ /** The record again, from the server. What is shown stays while it runs, and stays if it fails. */
4
+ refresh: () => Promise<void>;
5
+ }
6
+ export declare function createUseRecord(getDb: () => Pick<KoolbaseDatabase, 'get'>): (collection: string, id: string | null | undefined) => UseRecordResult;
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ // useRecord: one Koolbase record, by id, as React state.
3
+ //
4
+ // A thin wrapper, like useCollection: everything about getting the record
5
+ // right lives in @koolbase/core's KoolbaseRecordController, tested there;
6
+ // this file only ties one controller's lifetime to a component's.
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ exports.createUseRecord = createUseRecord;
9
+ const react_1 = require("react");
10
+ const core_1 = require("@koolbase/core");
11
+ const noop = () => { };
12
+ const done = () => Promise.resolve();
13
+ function createUseRecord(getDb) {
14
+ return function useRecord(collection, id) {
15
+ const key = JSON.stringify([collection, id ?? '']);
16
+ const [current, setCurrent] = (0, react_1.useState)(null);
17
+ (0, react_1.useEffect)(() => {
18
+ // Created here, not during render: under React strict mode an effect
19
+ // runs, cleans up and runs again, and each run gets its own controller.
20
+ const controller = new core_1.KoolbaseRecordController(getDb(), collection, id ?? '');
21
+ setCurrent({ key, controller });
22
+ void controller.load();
23
+ return () => controller.dispose();
24
+ // Depends on key alone: key is collection + id, as a value.
25
+ }, [key]);
26
+ // A controller from the previous key reads as a fresh load, never as the
27
+ // previous record.
28
+ const controller = current?.key === key ? current.controller : null;
29
+ const subscribe = (0, react_1.useCallback)((onChange) => (controller ? controller.subscribe(onChange) : noop), [controller]);
30
+ const getSnapshot = (0, react_1.useCallback)(() => (controller ? controller.getState() : core_1.initialRecordState), [controller]);
31
+ const state = (0, react_1.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
32
+ const refresh = (0, react_1.useCallback)(() => (controller ? controller.refresh() : done()), [controller]);
33
+ return { ...state, refresh };
34
+ };
35
+ }
@@ -29,3 +29,15 @@ export type { UseCollectionResult } from './use-collection.js';
29
29
  * ```
30
30
  */
31
31
  export declare const useCollection: (collection: string, query?: import("@koolbase/core").CollectionQuery) => import("./use-collection.js").UseCollectionResult;
32
+ export type { UseRecordResult } from './use-record.js';
33
+ /**
34
+ * One record, by id, as React state: loading, loaded, notFound or error,
35
+ * with refresh. notFound covers a missing id, a record that does not exist,
36
+ * one this user may not read (the API does not tell them apart) and a record
37
+ * from a different collection. Use it after Koolbase.initialize().
38
+ *
39
+ * ```tsx
40
+ * const song = useRecord('songs', songId);
41
+ * ```
42
+ */
43
+ export declare const useRecord: (collection: string, id: string | null | undefined) => import("./use-record.js").UseRecordResult;
package/dist/esm/index.js CHANGED
@@ -9,6 +9,7 @@ export { reactNativePlatform } from './platform.js';
9
9
  import { KoolbaseAuth, KoolbaseDatabase, KoolbaseFiscal, KoolbaseStorage, KoolbaseRealtime, KoolbaseFunctions, KoolbaseFlags, KoolbaseAnalytics, disabledAnalytics, KoolbaseMessaging, getOrCreateDeviceId, setPlatform, } from '@koolbase/core';
10
10
  import { reactNativePlatform } from './platform.js';
11
11
  import { createUseCollection } from './use-collection.js';
12
+ import { createUseRecord } from './use-record.js';
12
13
  let _auth = null;
13
14
  let _db = null;
14
15
  let _storage = null;
@@ -145,3 +146,14 @@ export { SecureAuthStorage } from './auth-storage.js';
145
146
  * ```
146
147
  */
147
148
  export const useCollection = createUseCollection(() => Koolbase.db);
149
+ /**
150
+ * One record, by id, as React state: loading, loaded, notFound or error,
151
+ * with refresh. notFound covers a missing id, a record that does not exist,
152
+ * one this user may not read (the API does not tell them apart) and a record
153
+ * from a different collection. Use it after Koolbase.initialize().
154
+ *
155
+ * ```tsx
156
+ * const song = useRecord('songs', songId);
157
+ * ```
158
+ */
159
+ export const useRecord = createUseRecord(() => Koolbase.db);
@@ -0,0 +1,6 @@
1
+ import { type KoolbaseDatabase, type RecordState } from '@koolbase/core';
2
+ export interface UseRecordResult extends RecordState {
3
+ /** The record again, from the server. What is shown stays while it runs, and stays if it fails. */
4
+ refresh: () => Promise<void>;
5
+ }
6
+ export declare function createUseRecord(getDb: () => Pick<KoolbaseDatabase, 'get'>): (collection: string, id: string | null | undefined) => UseRecordResult;
@@ -0,0 +1,32 @@
1
+ // useRecord: one Koolbase record, by id, as React state.
2
+ //
3
+ // A thin wrapper, like useCollection: everything about getting the record
4
+ // right lives in @koolbase/core's KoolbaseRecordController, tested there;
5
+ // this file only ties one controller's lifetime to a component's.
6
+ import { useCallback, useEffect, useState, useSyncExternalStore } from 'react';
7
+ import { KoolbaseRecordController, initialRecordState, } from '@koolbase/core';
8
+ const noop = () => { };
9
+ const done = () => Promise.resolve();
10
+ export function createUseRecord(getDb) {
11
+ return function useRecord(collection, id) {
12
+ const key = JSON.stringify([collection, id ?? '']);
13
+ const [current, setCurrent] = useState(null);
14
+ useEffect(() => {
15
+ // Created here, not during render: under React strict mode an effect
16
+ // runs, cleans up and runs again, and each run gets its own controller.
17
+ const controller = new KoolbaseRecordController(getDb(), collection, id ?? '');
18
+ setCurrent({ key, controller });
19
+ void controller.load();
20
+ return () => controller.dispose();
21
+ // Depends on key alone: key is collection + id, as a value.
22
+ }, [key]);
23
+ // A controller from the previous key reads as a fresh load, never as the
24
+ // previous record.
25
+ const controller = current?.key === key ? current.controller : null;
26
+ const subscribe = useCallback((onChange) => (controller ? controller.subscribe(onChange) : noop), [controller]);
27
+ const getSnapshot = useCallback(() => (controller ? controller.getState() : initialRecordState), [controller]);
28
+ const state = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
29
+ const refresh = useCallback(() => (controller ? controller.refresh() : done()), [controller]);
30
+ return { ...state, refresh };
31
+ };
32
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@koolbase/react-native",
3
- "version": "12.5.1",
3
+ "version": "12.6.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/cjs/index.js",
6
6
  "types": "./dist/esm/index.d.ts",
@@ -24,7 +24,7 @@
24
24
  "url": "https://github.com/koolbase/koolbase-react-native"
25
25
  },
26
26
  "dependencies": {
27
- "@koolbase/core": "12.5.1"
27
+ "@koolbase/core": "12.6.0"
28
28
  },
29
29
  "peerDependencies": {
30
30
  "@react-native-async-storage/async-storage": "^2.2.0 || ^3.0.2",