svelte-firekit 0.0.21 → 0.0.23

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.
@@ -1,15 +1,141 @@
1
+ /**
2
+ * @module FirekitAwaitableDoc
3
+ */
1
4
  import { type DocumentReference, type DocumentData } from "firebase/firestore";
5
+ /**
6
+ * Provides real-time document subscription with state management.
7
+ * Automatically handles document data updates, loading states, and error handling.
8
+ *
9
+ * @class
10
+ * @template T Type of document data
11
+ * @example
12
+ * ```typescript
13
+ * interface UserProfile {
14
+ * id: string;
15
+ * name: string;
16
+ * email: string;
17
+ * }
18
+ *
19
+ * // Create document subscription
20
+ * const userDoc = firekitAwaitableDoc<UserProfile>(
21
+ * 'users/123',
22
+ * { id: '123', name: 'Loading...', email: '' }
23
+ * );
24
+ *
25
+ * // Access reactive state
26
+ * if (userDoc.loading) {
27
+ * console.log('Loading user data...');
28
+ * } else if (userDoc.error) {
29
+ * console.error('Error:', userDoc.error);
30
+ * } else {
31
+ * console.log('User data:', userDoc.data);
32
+ * }
33
+ *
34
+ * // Get fresh data
35
+ * const freshData = await userDoc.getData();
36
+ * ```
37
+ */
2
38
  declare class FirekitAwaitableDoc<T> {
39
+ /**
40
+ * Current document data.
41
+ * Uses SvelteKit's $state for reactivity.
42
+ * @private
43
+ */
3
44
  private _data;
45
+ /**
46
+ * Loading state indicator.
47
+ * Uses SvelteKit's $state for reactivity.
48
+ * @private
49
+ */
4
50
  private _loading;
51
+ /**
52
+ * Error state container.
53
+ * Uses SvelteKit's $state for reactivity.
54
+ * @private
55
+ */
5
56
  private _error;
57
+ /**
58
+ * Firestore document reference.
59
+ * @private
60
+ */
6
61
  private docRef;
62
+ /**
63
+ * Creates a document subscription.
64
+ * Initializes document subscription if in browser environment.
65
+ *
66
+ * @param {string | DocumentReference<T>} ref - Document path or reference
67
+ * @param {T} [startWith] - Initial data before fetch completes
68
+ *
69
+ * @example
70
+ * ```typescript
71
+ * const doc = new FirekitAwaitableDoc('users/123', defaultUser);
72
+ * ```
73
+ */
7
74
  constructor(ref: string | DocumentReference<T>, startWith?: T);
75
+ /**
76
+ * Initializes document subscription.
77
+ * Sets up real-time updates and handles initial data fetch.
78
+ *
79
+ * @private
80
+ * @param {string | DocumentReference<T>} ref - Document path or reference
81
+ * @returns {Promise<void>}
82
+ *
83
+ * @throws {Error} If document initialization fails
84
+ */
8
85
  private initializeDoc;
86
+ /**
87
+ * Fetches fresh document data.
88
+ * Makes a new request to Firestore instead of using cached data.
89
+ *
90
+ * @returns {Promise<T | null>} Document data or null if not found
91
+ *
92
+ * @example
93
+ * ```typescript
94
+ * const freshData = await doc.getData();
95
+ * if (freshData) {
96
+ * console.log('Fresh data:', freshData);
97
+ * }
98
+ * ```
99
+ */
9
100
  getData(): Promise<T | null>;
101
+ /**
102
+ * Gets current document data.
103
+ * @returns {T | null} Current document data or null if not loaded
104
+ */
10
105
  get data(): T | null;
106
+ /**
107
+ * Gets current loading state.
108
+ * @returns {boolean} True if document is loading
109
+ */
11
110
  get loading(): boolean;
111
+ /**
112
+ * Gets current error state.
113
+ * @returns {Error | null} Error object if an error occurred, null otherwise
114
+ */
12
115
  get error(): Error | null;
13
116
  }
117
+ /**
118
+ * Creates a document subscription.
119
+ * Factory function for creating FirekitAwaitableDoc instances.
120
+ *
121
+ * @template T Document data type
122
+ * @param {string} path Document path
123
+ * @param {T} [startWith] Initial data before fetch completes
124
+ * @returns {FirekitAwaitableDoc<T>} Document subscription instance
125
+ *
126
+ * @example
127
+ * ```typescript
128
+ * interface UserProfile {
129
+ * id: string;
130
+ * name: string;
131
+ * email: string;
132
+ * }
133
+ *
134
+ * const userDoc = firekitAwaitableDoc<UserProfile>(
135
+ * 'users/123',
136
+ * { id: '123', name: 'Loading...', email: '' }
137
+ * );
138
+ * ```
139
+ */
14
140
  export declare function firekitAwaitableDoc<T extends DocumentData>(path: string, startWith?: T): FirekitAwaitableDoc<T>;
15
141
  export {};
@@ -1,17 +1,94 @@
1
+ /**
2
+ * @module FirekitAwaitableDoc
3
+ */
1
4
  import { doc, getDoc, onSnapshot } from "firebase/firestore";
2
5
  import { firebaseService } from "../firebase.js";
3
6
  import { browser } from "$app/environment";
7
+ /**
8
+ * Provides real-time document subscription with state management.
9
+ * Automatically handles document data updates, loading states, and error handling.
10
+ *
11
+ * @class
12
+ * @template T Type of document data
13
+ * @example
14
+ * ```typescript
15
+ * interface UserProfile {
16
+ * id: string;
17
+ * name: string;
18
+ * email: string;
19
+ * }
20
+ *
21
+ * // Create document subscription
22
+ * const userDoc = firekitAwaitableDoc<UserProfile>(
23
+ * 'users/123',
24
+ * { id: '123', name: 'Loading...', email: '' }
25
+ * );
26
+ *
27
+ * // Access reactive state
28
+ * if (userDoc.loading) {
29
+ * console.log('Loading user data...');
30
+ * } else if (userDoc.error) {
31
+ * console.error('Error:', userDoc.error);
32
+ * } else {
33
+ * console.log('User data:', userDoc.data);
34
+ * }
35
+ *
36
+ * // Get fresh data
37
+ * const freshData = await userDoc.getData();
38
+ * ```
39
+ */
4
40
  class FirekitAwaitableDoc {
41
+ /**
42
+ * Current document data.
43
+ * Uses SvelteKit's $state for reactivity.
44
+ * @private
45
+ */
5
46
  _data = $state(null);
47
+ /**
48
+ * Loading state indicator.
49
+ * Uses SvelteKit's $state for reactivity.
50
+ * @private
51
+ */
6
52
  _loading = $state(true);
53
+ /**
54
+ * Error state container.
55
+ * Uses SvelteKit's $state for reactivity.
56
+ * @private
57
+ */
7
58
  _error = $state(null);
59
+ /**
60
+ * Firestore document reference.
61
+ * @private
62
+ */
8
63
  docRef = null;
64
+ /**
65
+ * Creates a document subscription.
66
+ * Initializes document subscription if in browser environment.
67
+ *
68
+ * @param {string | DocumentReference<T>} ref - Document path or reference
69
+ * @param {T} [startWith] - Initial data before fetch completes
70
+ *
71
+ * @example
72
+ * ```typescript
73
+ * const doc = new FirekitAwaitableDoc('users/123', defaultUser);
74
+ * ```
75
+ */
9
76
  constructor(ref, startWith) {
10
77
  this._data = startWith ?? null;
11
78
  if (browser) {
12
79
  this.initializeDoc(ref);
13
80
  }
14
81
  }
82
+ /**
83
+ * Initializes document subscription.
84
+ * Sets up real-time updates and handles initial data fetch.
85
+ *
86
+ * @private
87
+ * @param {string | DocumentReference<T>} ref - Document path or reference
88
+ * @returns {Promise<void>}
89
+ *
90
+ * @throws {Error} If document initialization fails
91
+ */
15
92
  async initializeDoc(ref) {
16
93
  try {
17
94
  const firestore = firebaseService.getDbInstance();
@@ -36,22 +113,71 @@ class FirekitAwaitableDoc {
36
113
  this._loading = false;
37
114
  }
38
115
  }
116
+ /**
117
+ * Fetches fresh document data.
118
+ * Makes a new request to Firestore instead of using cached data.
119
+ *
120
+ * @returns {Promise<T | null>} Document data or null if not found
121
+ *
122
+ * @example
123
+ * ```typescript
124
+ * const freshData = await doc.getData();
125
+ * if (freshData) {
126
+ * console.log('Fresh data:', freshData);
127
+ * }
128
+ * ```
129
+ */
39
130
  async getData() {
40
131
  if (!this.docRef)
41
132
  return null;
42
133
  const snapshot = await getDoc(this.docRef);
43
134
  return snapshot.exists() ? { id: snapshot.id, ...snapshot.data() } : null;
44
135
  }
136
+ /**
137
+ * Gets current document data.
138
+ * @returns {T | null} Current document data or null if not loaded
139
+ */
45
140
  get data() {
46
141
  return this._data;
47
142
  }
143
+ /**
144
+ * Gets current loading state.
145
+ * @returns {boolean} True if document is loading
146
+ */
48
147
  get loading() {
49
148
  return this._loading;
50
149
  }
150
+ /**
151
+ * Gets current error state.
152
+ * @returns {Error | null} Error object if an error occurred, null otherwise
153
+ */
51
154
  get error() {
52
155
  return this._error;
53
156
  }
54
157
  }
158
+ /**
159
+ * Creates a document subscription.
160
+ * Factory function for creating FirekitAwaitableDoc instances.
161
+ *
162
+ * @template T Document data type
163
+ * @param {string} path Document path
164
+ * @param {T} [startWith] Initial data before fetch completes
165
+ * @returns {FirekitAwaitableDoc<T>} Document subscription instance
166
+ *
167
+ * @example
168
+ * ```typescript
169
+ * interface UserProfile {
170
+ * id: string;
171
+ * name: string;
172
+ * email: string;
173
+ * }
174
+ *
175
+ * const userDoc = firekitAwaitableDoc<UserProfile>(
176
+ * 'users/123',
177
+ * { id: '123', name: 'Loading...', email: '' }
178
+ * );
179
+ * ```
180
+ */
55
181
  export function firekitAwaitableDoc(path, startWith) {
56
182
  return new FirekitAwaitableDoc(path, startWith);
57
183
  }
@@ -0,0 +1,78 @@
1
+ /**
2
+ * @module FirekitCollectionGroup
3
+ */
4
+ import { type Query, type DocumentData, type QueryConstraint } from "firebase/firestore";
5
+ /**
6
+ * Manages real-time Firestore collection group subscriptions with reactive state
7
+ * @class
8
+ * @template T Collection document type
9
+ *
10
+ * @example
11
+ * ```typescript
12
+ * interface Task {
13
+ * id: string;
14
+ * title: string;
15
+ * status: string;
16
+ * }
17
+ *
18
+ * // Create collection group subscription
19
+ * const allTasks = firekitCollectionGroup<Task>('tasks',
20
+ * where('status', '==', 'active'),
21
+ * orderBy('title')
22
+ * );
23
+ * ```
24
+ */
25
+ declare class FirekitCollectionGroup<T> {
26
+ /** Current collection group data */
27
+ private _data;
28
+ /** Loading state */
29
+ private _loading;
30
+ /** Error state */
31
+ private _error;
32
+ /** Query reference */
33
+ private queryRef;
34
+ /**
35
+ * Creates a collection group subscription
36
+ * @param {string} collectionId Collection ID to query across all documents
37
+ * @param {...QueryConstraint[]} queryConstraints Query constraints (where, orderBy, limit, etc.)
38
+ */
39
+ constructor(collectionId: string, ...queryConstraints: QueryConstraint[]);
40
+ /** Gets current collection group data */
41
+ get data(): T[];
42
+ /** Gets loading state */
43
+ get loading(): boolean;
44
+ /** Gets error state */
45
+ get error(): Error | null;
46
+ /** Checks if collection group is empty */
47
+ get empty(): boolean;
48
+ /** Gets number of documents in collection group */
49
+ get size(): number;
50
+ /**
51
+ * Gets query reference
52
+ * @throws {Error} If query reference is not available
53
+ */
54
+ get ref(): Query<T>;
55
+ }
56
+ /**
57
+ * Creates a collection group subscription
58
+ * @template T Collection document type
59
+ * @param {string} collectionId Collection ID to query across all documents
60
+ * @param {...QueryConstraint[]} queryConstraints Query constraints
61
+ * @returns {FirekitCollectionGroup<T>} Collection group subscription instance
62
+ *
63
+ * @example
64
+ * ```typescript
65
+ * interface Comment {
66
+ * id: string;
67
+ * text: string;
68
+ * userId: string;
69
+ * }
70
+ *
71
+ * const allComments = firekitCollectionGroup<Comment>('comments',
72
+ * where('userId', '==', currentUserId),
73
+ * orderBy('createdAt', 'desc')
74
+ * );
75
+ * ```
76
+ */
77
+ export declare function firekitCollectionGroup<T extends DocumentData>(collectionId: string, ...queryConstraints: QueryConstraint[]): FirekitCollectionGroup<T>;
78
+ export {};
@@ -0,0 +1,120 @@
1
+ /**
2
+ * @module FirekitCollectionGroup
3
+ */
4
+ import { collectionGroup, query, onSnapshot } from "firebase/firestore";
5
+ import { firebaseService } from "../firebase.js";
6
+ import { browser } from "$app/environment";
7
+ /**
8
+ * Manages real-time Firestore collection group subscriptions with reactive state
9
+ * @class
10
+ * @template T Collection document type
11
+ *
12
+ * @example
13
+ * ```typescript
14
+ * interface Task {
15
+ * id: string;
16
+ * title: string;
17
+ * status: string;
18
+ * }
19
+ *
20
+ * // Create collection group subscription
21
+ * const allTasks = firekitCollectionGroup<Task>('tasks',
22
+ * where('status', '==', 'active'),
23
+ * orderBy('title')
24
+ * );
25
+ * ```
26
+ */
27
+ class FirekitCollectionGroup {
28
+ /** Current collection group data */
29
+ _data = $state([]);
30
+ /** Loading state */
31
+ _loading = $state(true);
32
+ /** Error state */
33
+ _error = $state(null);
34
+ /** Query reference */
35
+ queryRef = null;
36
+ /**
37
+ * Creates a collection group subscription
38
+ * @param {string} collectionId Collection ID to query across all documents
39
+ * @param {...QueryConstraint[]} queryConstraints Query constraints (where, orderBy, limit, etc.)
40
+ */
41
+ constructor(collectionId, ...queryConstraints) {
42
+ if (browser) {
43
+ try {
44
+ const firestore = firebaseService.getDbInstance();
45
+ const groupRef = collectionGroup(firestore, collectionId);
46
+ this.queryRef = query(groupRef, ...queryConstraints);
47
+ onSnapshot(this.queryRef, (snapshot) => {
48
+ this._data = snapshot.docs.map(doc => ({
49
+ id: doc.id,
50
+ path: doc.ref.path,
51
+ ...doc.data()
52
+ }));
53
+ this._loading = false;
54
+ this._error = null;
55
+ }, (error) => {
56
+ this._error = error;
57
+ this._loading = false;
58
+ });
59
+ }
60
+ catch (error) {
61
+ this._error = error;
62
+ this._loading = false;
63
+ }
64
+ }
65
+ }
66
+ /** Gets current collection group data */
67
+ get data() {
68
+ return this._data;
69
+ }
70
+ /** Gets loading state */
71
+ get loading() {
72
+ return this._loading;
73
+ }
74
+ /** Gets error state */
75
+ get error() {
76
+ return this._error;
77
+ }
78
+ /** Checks if collection group is empty */
79
+ get empty() {
80
+ return this._data.length === 0;
81
+ }
82
+ /** Gets number of documents in collection group */
83
+ get size() {
84
+ return this._data.length;
85
+ }
86
+ /**
87
+ * Gets query reference
88
+ * @throws {Error} If query reference is not available
89
+ */
90
+ get ref() {
91
+ if (!this.queryRef) {
92
+ throw new Error("Query reference is not available");
93
+ }
94
+ return this.queryRef;
95
+ }
96
+ }
97
+ /**
98
+ * Creates a collection group subscription
99
+ * @template T Collection document type
100
+ * @param {string} collectionId Collection ID to query across all documents
101
+ * @param {...QueryConstraint[]} queryConstraints Query constraints
102
+ * @returns {FirekitCollectionGroup<T>} Collection group subscription instance
103
+ *
104
+ * @example
105
+ * ```typescript
106
+ * interface Comment {
107
+ * id: string;
108
+ * text: string;
109
+ * userId: string;
110
+ * }
111
+ *
112
+ * const allComments = firekitCollectionGroup<Comment>('comments',
113
+ * where('userId', '==', currentUserId),
114
+ * orderBy('createdAt', 'desc')
115
+ * );
116
+ * ```
117
+ */
118
+ export function firekitCollectionGroup(collectionId, ...queryConstraints) {
119
+ return new FirekitCollectionGroup(collectionId, ...queryConstraints);
120
+ }
@@ -1,17 +1,96 @@
1
+ /**
2
+ * @module FirekitCollection
3
+ */
1
4
  import { type CollectionReference, type DocumentData, type QueryConstraint } from "firebase/firestore";
5
+ /**
6
+ * Manages real-time Firestore collection subscriptions with reactive state
7
+ * @class
8
+ * @template T Collection document type
9
+ *
10
+ * @example
11
+ * ```typescript
12
+ * interface User {
13
+ * id: string;
14
+ * name: string;
15
+ * email: string;
16
+ * }
17
+ *
18
+ * // Create collection subscription
19
+ * const users = firekitCollection<User>('users',
20
+ * where('active', '==', true),
21
+ * orderBy('name')
22
+ * );
23
+ *
24
+ * // Access reactive state
25
+ * console.log(users.data); // Array of documents
26
+ * console.log(users.loading); // Loading state
27
+ * console.log(users.error); // Error state
28
+ * console.log(users.empty); // Whether collection is empty
29
+ * console.log(users.size); // Number of documents
30
+ * ```
31
+ */
2
32
  declare class FirekitCollection<T> {
33
+ /** Current collection data */
3
34
  private _data;
35
+ /** Loading state */
4
36
  private _loading;
37
+ /** Error state */
5
38
  private _error;
39
+ /** Collection reference */
6
40
  private colRef;
41
+ /** Query reference */
7
42
  private queryRef;
43
+ /**
44
+ * Creates a collection subscription
45
+ * @param {string} path Collection path
46
+ * @param {...QueryConstraint[]} queryConstraints Query constraints (where, orderBy, limit, etc.)
47
+ *
48
+ * @example
49
+ * ```typescript
50
+ * const collection = new FirekitCollection('users',
51
+ * where('age', '>=', 18),
52
+ * orderBy('name', 'asc'),
53
+ * limit(10)
54
+ * );
55
+ * ```
56
+ */
8
57
  constructor(path: string, ...queryConstraints: QueryConstraint[]);
58
+ /** Gets current collection data */
9
59
  get data(): T[];
60
+ /** Gets loading state */
10
61
  get loading(): boolean;
62
+ /** Gets error state */
11
63
  get error(): Error | null;
64
+ /** Checks if collection is empty */
12
65
  get empty(): boolean;
66
+ /** Gets number of documents in collection */
13
67
  get size(): number;
14
- get ref(): CollectionReference<T, DocumentData>;
68
+ /**
69
+ * Gets collection reference
70
+ * @throws {Error} If collection reference is not available
71
+ */
72
+ get ref(): CollectionReference<T>;
15
73
  }
74
+ /**
75
+ * Creates a collection subscription
76
+ * @template T Collection document type
77
+ * @param {string} path Collection path
78
+ * @param {...QueryConstraint[]} queryConstraints Query constraints
79
+ * @returns {FirekitCollection<T>} Collection subscription instance
80
+ *
81
+ * @example
82
+ * ```typescript
83
+ * interface Post {
84
+ * id: string;
85
+ * title: string;
86
+ * authorId: string;
87
+ * }
88
+ *
89
+ * const posts = firekitCollection<Post>('posts',
90
+ * where('authorId', '==', currentUserId),
91
+ * orderBy('createdAt', 'desc')
92
+ * );
93
+ * ```
94
+ */
16
95
  export declare function firekitCollection<T extends DocumentData>(path: string, ...queryConstraints: QueryConstraint[]): FirekitCollection<T>;
17
96
  export {};