svelte-firekit 0.0.21 → 0.0.22
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 +218 -55
- package/dist/auth/auth.d.ts +84 -1
- package/dist/auth/auth.js +89 -3
- package/dist/auth/presence.svelte.d.ts +65 -0
- package/dist/auth/presence.svelte.js +42 -0
- package/dist/auth/user.svelte.d.ts +22 -4
- package/dist/auth/user.svelte.js +25 -4
- package/dist/config.d.ts +10 -0
- package/dist/config.js +63 -0
- package/dist/firebase.d.ts +69 -0
- package/dist/firebase.js +71 -3
- package/dist/firestore/awaitable-doc.svelte.d.ts +126 -0
- package/dist/firestore/awaitable-doc.svelte.js +126 -0
- package/dist/firestore/collection.svelte.d.ts +80 -1
- package/dist/firestore/collection.svelte.js +79 -0
- package/dist/firestore/doc.svelte.d.ts +75 -1
- package/dist/firestore/doc.svelte.js +74 -1
- package/dist/firestore/document-mutations.svelte.d.ts +137 -0
- package/dist/firestore/document-mutations.svelte.js +123 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -2
- package/dist/realtime/realtime.svelte.d.ts +145 -0
- package/dist/realtime/realtime.svelte.js +113 -14
- package/dist/storage/download-url.svelte.d.ts +69 -0
- package/dist/storage/download-url.svelte.js +69 -0
- package/dist/storage/storage-list.svelte.d.ts +72 -0
- package/dist/storage/storage-list.svelte.js +72 -0
- package/dist/storage/upload-task.svelte.d.ts +71 -0
- package/dist/storage/upload-task.svelte.js +71 -0
- package/package.json +1 -1
|
@@ -1,11 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module FirekitDoc
|
|
3
|
+
*/
|
|
1
4
|
import { doc, DocumentReference, onSnapshot } from "firebase/firestore";
|
|
2
5
|
import { firebaseService } from "../firebase.js";
|
|
3
6
|
import { browser } from "$app/environment";
|
|
7
|
+
/**
|
|
8
|
+
* Manages real-time Firestore document subscriptions with reactive state
|
|
9
|
+
* @class
|
|
10
|
+
* @template T Document data type
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* interface User {
|
|
15
|
+
* id: string;
|
|
16
|
+
* name: string;
|
|
17
|
+
* email: string;
|
|
18
|
+
* }
|
|
19
|
+
*
|
|
20
|
+
* // Create document subscription
|
|
21
|
+
* const userDoc = firekitDoc<User>('users/123', {
|
|
22
|
+
* id: '123',
|
|
23
|
+
* name: 'Loading...',
|
|
24
|
+
* email: ''
|
|
25
|
+
* });
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
4
28
|
class FirekitDoc {
|
|
29
|
+
/** Current document data */
|
|
5
30
|
_data = $state(null);
|
|
31
|
+
/** Loading state */
|
|
6
32
|
_loading = $state(true);
|
|
33
|
+
/** Error state */
|
|
7
34
|
_error = $state(null);
|
|
35
|
+
/** Document reference */
|
|
8
36
|
docRef = null;
|
|
37
|
+
/**
|
|
38
|
+
* Creates a document subscription
|
|
39
|
+
* @param {string | DocumentReference<T>} ref Document path or reference
|
|
40
|
+
* @param {T} [startWith] Initial data before fetch completes
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```typescript
|
|
44
|
+
* const doc = new FirekitDoc('users/123', defaultUser);
|
|
45
|
+
* // or
|
|
46
|
+
* const doc = new FirekitDoc(docRef, defaultUser);
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
9
49
|
constructor(ref, startWith) {
|
|
10
50
|
this._data = startWith ?? null;
|
|
11
51
|
if (browser) {
|
|
@@ -15,7 +55,6 @@ class FirekitDoc {
|
|
|
15
55
|
? doc(firestore, ref)
|
|
16
56
|
: ref;
|
|
17
57
|
onSnapshot(this.docRef, (snapshot) => {
|
|
18
|
-
// this._data = (snapshot.data() as T) ?? null;
|
|
19
58
|
const data = snapshot.data();
|
|
20
59
|
this._data = data ? { ...data, id: snapshot.id } : null;
|
|
21
60
|
this._loading = false;
|
|
@@ -31,28 +70,62 @@ class FirekitDoc {
|
|
|
31
70
|
}
|
|
32
71
|
}
|
|
33
72
|
}
|
|
73
|
+
/** Gets current document data */
|
|
34
74
|
get data() {
|
|
35
75
|
return this._data;
|
|
36
76
|
}
|
|
77
|
+
/** Gets document ID */
|
|
37
78
|
get id() {
|
|
38
79
|
return this.docRef?.id ?? '';
|
|
39
80
|
}
|
|
81
|
+
/** Gets loading state */
|
|
40
82
|
get loading() {
|
|
41
83
|
return this._loading;
|
|
42
84
|
}
|
|
85
|
+
/** Gets error state */
|
|
43
86
|
get error() {
|
|
44
87
|
return this._error;
|
|
45
88
|
}
|
|
89
|
+
/**
|
|
90
|
+
* Gets document reference
|
|
91
|
+
* @throws {Error} If document reference is not available
|
|
92
|
+
*/
|
|
46
93
|
get ref() {
|
|
47
94
|
if (this.docRef === null) {
|
|
48
95
|
throw new Error("Document reference is not available yet.");
|
|
49
96
|
}
|
|
50
97
|
return this.docRef;
|
|
51
98
|
}
|
|
99
|
+
/** Checks if document exists */
|
|
52
100
|
get exists() {
|
|
53
101
|
return this._data !== null;
|
|
54
102
|
}
|
|
55
103
|
}
|
|
104
|
+
/**
|
|
105
|
+
* Creates a document subscription
|
|
106
|
+
* @template T Document data type
|
|
107
|
+
* @param {string | DocumentReference<T>} ref Document path or reference
|
|
108
|
+
* @param {T} [startWith] Initial data before fetch completes
|
|
109
|
+
* @returns {FirekitDoc<T>} Document subscription instance
|
|
110
|
+
*
|
|
111
|
+
* @example
|
|
112
|
+
* ```typescript
|
|
113
|
+
* const userDoc = firekitDoc<User>('users/123', {
|
|
114
|
+
* id: '123',
|
|
115
|
+
* name: 'Loading...',
|
|
116
|
+
* email: ''
|
|
117
|
+
* });
|
|
118
|
+
*
|
|
119
|
+
* // Access reactive state
|
|
120
|
+
* if (userDoc.loading) {
|
|
121
|
+
* console.log('Loading...');
|
|
122
|
+
* } else if (userDoc.error) {
|
|
123
|
+
* console.error(userDoc.error);
|
|
124
|
+
* } else if (userDoc.exists) {
|
|
125
|
+
* console.log(userDoc.data);
|
|
126
|
+
* }
|
|
127
|
+
* ```
|
|
128
|
+
*/
|
|
56
129
|
export function firekitDoc(ref, startWith) {
|
|
57
130
|
return new FirekitDoc(ref, startWith);
|
|
58
131
|
}
|
|
@@ -1,27 +1,164 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module FirekitDocumentMutations
|
|
3
|
+
*/
|
|
1
4
|
import { type DocumentData, type WithFieldValue, type PartialWithFieldValue } from "firebase/firestore";
|
|
5
|
+
/**
|
|
6
|
+
* Response structure for document mutations
|
|
7
|
+
* @interface MutationResponse
|
|
8
|
+
* @template T Document data type
|
|
9
|
+
*/
|
|
2
10
|
interface MutationResponse<T> {
|
|
11
|
+
/** Operation success status */
|
|
3
12
|
success: boolean;
|
|
13
|
+
/** Document data */
|
|
4
14
|
data?: T;
|
|
15
|
+
/** Document ID */
|
|
5
16
|
id?: string;
|
|
17
|
+
/** Error details if operation failed */
|
|
6
18
|
error?: {
|
|
7
19
|
code: string;
|
|
8
20
|
message: string;
|
|
9
21
|
};
|
|
10
22
|
}
|
|
23
|
+
/**
|
|
24
|
+
* Options for document mutations
|
|
25
|
+
* @interface MutationOptions
|
|
26
|
+
*/
|
|
11
27
|
interface MutationOptions {
|
|
28
|
+
/** Whether to include timestamp fields */
|
|
12
29
|
timestamps?: boolean;
|
|
30
|
+
/** Whether to merge data in set operations */
|
|
13
31
|
merge?: boolean;
|
|
32
|
+
/** Custom document ID for add operations */
|
|
14
33
|
customId?: string;
|
|
15
34
|
}
|
|
35
|
+
/**
|
|
36
|
+
* Manages Firestore document mutations with automatic timestamps and error handling
|
|
37
|
+
* @class
|
|
38
|
+
* @example
|
|
39
|
+
* ```typescript
|
|
40
|
+
* // Add a new document
|
|
41
|
+
* const result = await firekitDocMutations.add('users', {
|
|
42
|
+
* name: 'John Doe',
|
|
43
|
+
* email: 'john@example.com'
|
|
44
|
+
* });
|
|
45
|
+
*
|
|
46
|
+
* // Update a document
|
|
47
|
+
* await firekitDocMutations.update('users/123', {
|
|
48
|
+
* name: 'Jane Doe'
|
|
49
|
+
* });
|
|
50
|
+
*
|
|
51
|
+
* // Delete a document
|
|
52
|
+
* await firekitDocMutations.delete('users/123');
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
16
55
|
declare class FirekitDocumentMutations {
|
|
56
|
+
/**
|
|
57
|
+
* Generates timestamp data for document mutations
|
|
58
|
+
* @private
|
|
59
|
+
* @param {boolean} [isNew=true] Whether this is a new document
|
|
60
|
+
* @returns {Record<string, any>} Timestamp data
|
|
61
|
+
*/
|
|
17
62
|
private getTimestampData;
|
|
63
|
+
/**
|
|
64
|
+
* Handles and formats mutation errors
|
|
65
|
+
* @private
|
|
66
|
+
* @param {any} error Error object
|
|
67
|
+
* @returns {MutationResponse<never>} Formatted error response
|
|
68
|
+
*/
|
|
18
69
|
private handleError;
|
|
70
|
+
/**
|
|
71
|
+
* Adds a new document to a collection
|
|
72
|
+
* @template T Document data type
|
|
73
|
+
* @param {string} collectionPath Collection path
|
|
74
|
+
* @param {WithFieldValue<T>} data Document data
|
|
75
|
+
* @param {MutationOptions} [options] Mutation options
|
|
76
|
+
* @returns {Promise<MutationResponse<T>>} Mutation response
|
|
77
|
+
*
|
|
78
|
+
* @example
|
|
79
|
+
* ```typescript
|
|
80
|
+
* const result = await firekitDocMutations.add('users', {
|
|
81
|
+
* name: 'John Doe',
|
|
82
|
+
* email: 'john@example.com'
|
|
83
|
+
* }, { timestamps: true, customId: 'custom-id' });
|
|
84
|
+
* ```
|
|
85
|
+
*/
|
|
19
86
|
add<T extends DocumentData>(collectionPath: string, data: WithFieldValue<T>, options?: MutationOptions): Promise<MutationResponse<T>>;
|
|
87
|
+
/**
|
|
88
|
+
* Sets document data at specified path
|
|
89
|
+
* @template T Document data type
|
|
90
|
+
* @param {string} path Document path
|
|
91
|
+
* @param {WithFieldValue<T>} data Document data
|
|
92
|
+
* @param {MutationOptions} [options] Mutation options
|
|
93
|
+
* @returns {Promise<MutationResponse<T>>} Mutation response
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
* ```typescript
|
|
97
|
+
* const result = await firekitDocMutations.set('users/123', {
|
|
98
|
+
* name: 'John Doe',
|
|
99
|
+
* email: 'john@example.com'
|
|
100
|
+
* }, { merge: true });
|
|
101
|
+
* ```
|
|
102
|
+
*/
|
|
20
103
|
set<T extends DocumentData>(path: string, data: WithFieldValue<T>, options?: MutationOptions): Promise<MutationResponse<T>>;
|
|
104
|
+
/**
|
|
105
|
+
* Updates a document at specified path
|
|
106
|
+
* @template T Document data type
|
|
107
|
+
* @param {string} path Document path
|
|
108
|
+
* @param {PartialWithFieldValue<T>} data Update data
|
|
109
|
+
* @param {MutationOptions} [options] Mutation options
|
|
110
|
+
* @returns {Promise<MutationResponse<Partial<T>>>} Mutation response
|
|
111
|
+
*
|
|
112
|
+
* @example
|
|
113
|
+
* ```typescript
|
|
114
|
+
* const result = await firekitDocMutations.update('users/123', {
|
|
115
|
+
* name: 'Jane Doe'
|
|
116
|
+
* });
|
|
117
|
+
* ```
|
|
118
|
+
*/
|
|
21
119
|
update<T extends DocumentData>(path: string, data: PartialWithFieldValue<T>, options?: MutationOptions): Promise<MutationResponse<Partial<T>>>;
|
|
120
|
+
/**
|
|
121
|
+
* Deletes a document at specified path
|
|
122
|
+
* @param {string} path Document path
|
|
123
|
+
* @returns {Promise<MutationResponse<void>>} Mutation response
|
|
124
|
+
*
|
|
125
|
+
* @example
|
|
126
|
+
* ```typescript
|
|
127
|
+
* const result = await firekitDocMutations.delete('users/123');
|
|
128
|
+
* ```
|
|
129
|
+
*/
|
|
22
130
|
delete(path: string): Promise<MutationResponse<void>>;
|
|
131
|
+
/**
|
|
132
|
+
* Checks if a document exists at specified path
|
|
133
|
+
* @param {string} path Document path
|
|
134
|
+
* @returns {Promise<boolean>} Whether document exists
|
|
135
|
+
*
|
|
136
|
+
* @example
|
|
137
|
+
* ```typescript
|
|
138
|
+
* const exists = await firekitDocMutations.exists('users/123');
|
|
139
|
+
* ```
|
|
140
|
+
*/
|
|
23
141
|
exists(path: string): Promise<boolean>;
|
|
142
|
+
/**
|
|
143
|
+
* Gets a document at specified path
|
|
144
|
+
* @template T Document data type
|
|
145
|
+
* @param {string} path Document path
|
|
146
|
+
* @returns {Promise<MutationResponse<T>>} Mutation response with document data
|
|
147
|
+
*
|
|
148
|
+
* @example
|
|
149
|
+
* ```typescript
|
|
150
|
+
* const result = await firekitDocMutations.getDoc<UserData>('users/123');
|
|
151
|
+
* if (result.success) {
|
|
152
|
+
* console.log(result.data);
|
|
153
|
+
* }
|
|
154
|
+
* ```
|
|
155
|
+
*/
|
|
24
156
|
getDoc<T extends DocumentData>(path: string): Promise<MutationResponse<T>>;
|
|
25
157
|
}
|
|
158
|
+
/**
|
|
159
|
+
* Pre-initialized instance of FirekitDocumentMutations
|
|
160
|
+
* @const
|
|
161
|
+
* @type {FirekitDocumentMutations}
|
|
162
|
+
*/
|
|
26
163
|
export declare const firekitDocMutations: FirekitDocumentMutations;
|
|
27
164
|
export {};
|
|
@@ -1,7 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module FirekitDocumentMutations
|
|
3
|
+
*/
|
|
1
4
|
import { addDoc, setDoc, updateDoc, deleteDoc, doc, getDoc, collection, serverTimestamp, } from "firebase/firestore";
|
|
2
5
|
import { firebaseService } from "../firebase.js";
|
|
3
6
|
import { firekitUser } from "../auth/user.svelte.js";
|
|
7
|
+
/**
|
|
8
|
+
* Manages Firestore document mutations with automatic timestamps and error handling
|
|
9
|
+
* @class
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* // Add a new document
|
|
13
|
+
* const result = await firekitDocMutations.add('users', {
|
|
14
|
+
* name: 'John Doe',
|
|
15
|
+
* email: 'john@example.com'
|
|
16
|
+
* });
|
|
17
|
+
*
|
|
18
|
+
* // Update a document
|
|
19
|
+
* await firekitDocMutations.update('users/123', {
|
|
20
|
+
* name: 'Jane Doe'
|
|
21
|
+
* });
|
|
22
|
+
*
|
|
23
|
+
* // Delete a document
|
|
24
|
+
* await firekitDocMutations.delete('users/123');
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
4
27
|
class FirekitDocumentMutations {
|
|
28
|
+
/**
|
|
29
|
+
* Generates timestamp data for document mutations
|
|
30
|
+
* @private
|
|
31
|
+
* @param {boolean} [isNew=true] Whether this is a new document
|
|
32
|
+
* @returns {Record<string, any>} Timestamp data
|
|
33
|
+
*/
|
|
5
34
|
getTimestampData(isNew = true) {
|
|
6
35
|
const timestamps = {
|
|
7
36
|
updatedAt: serverTimestamp(),
|
|
@@ -13,6 +42,12 @@ class FirekitDocumentMutations {
|
|
|
13
42
|
}
|
|
14
43
|
return timestamps;
|
|
15
44
|
}
|
|
45
|
+
/**
|
|
46
|
+
* Handles and formats mutation errors
|
|
47
|
+
* @private
|
|
48
|
+
* @param {any} error Error object
|
|
49
|
+
* @returns {MutationResponse<never>} Formatted error response
|
|
50
|
+
*/
|
|
16
51
|
handleError(error) {
|
|
17
52
|
console.error('Firestore mutation error:', error);
|
|
18
53
|
return {
|
|
@@ -23,6 +58,22 @@ class FirekitDocumentMutations {
|
|
|
23
58
|
}
|
|
24
59
|
};
|
|
25
60
|
}
|
|
61
|
+
/**
|
|
62
|
+
* Adds a new document to a collection
|
|
63
|
+
* @template T Document data type
|
|
64
|
+
* @param {string} collectionPath Collection path
|
|
65
|
+
* @param {WithFieldValue<T>} data Document data
|
|
66
|
+
* @param {MutationOptions} [options] Mutation options
|
|
67
|
+
* @returns {Promise<MutationResponse<T>>} Mutation response
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* ```typescript
|
|
71
|
+
* const result = await firekitDocMutations.add('users', {
|
|
72
|
+
* name: 'John Doe',
|
|
73
|
+
* email: 'john@example.com'
|
|
74
|
+
* }, { timestamps: true, customId: 'custom-id' });
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
26
77
|
async add(collectionPath, data, options = { timestamps: true }) {
|
|
27
78
|
try {
|
|
28
79
|
const firestore = firebaseService.getDbInstance();
|
|
@@ -34,12 +85,12 @@ class FirekitDocumentMutations {
|
|
|
34
85
|
let docRef;
|
|
35
86
|
if (options.customId) {
|
|
36
87
|
docRef = doc(colRef, options.customId);
|
|
37
|
-
dataToAdd = { ...dataToAdd, id: docRef.id };
|
|
88
|
+
dataToAdd = { ...dataToAdd, id: docRef.id };
|
|
38
89
|
await setDoc(docRef, dataToAdd);
|
|
39
90
|
}
|
|
40
91
|
else {
|
|
41
92
|
docRef = await addDoc(colRef, dataToAdd);
|
|
42
|
-
dataToAdd = { ...dataToAdd, id: docRef.id };
|
|
93
|
+
dataToAdd = { ...dataToAdd, id: docRef.id };
|
|
43
94
|
await setDoc(docRef, dataToAdd);
|
|
44
95
|
}
|
|
45
96
|
return {
|
|
@@ -52,6 +103,22 @@ class FirekitDocumentMutations {
|
|
|
52
103
|
return this.handleError(error);
|
|
53
104
|
}
|
|
54
105
|
}
|
|
106
|
+
/**
|
|
107
|
+
* Sets document data at specified path
|
|
108
|
+
* @template T Document data type
|
|
109
|
+
* @param {string} path Document path
|
|
110
|
+
* @param {WithFieldValue<T>} data Document data
|
|
111
|
+
* @param {MutationOptions} [options] Mutation options
|
|
112
|
+
* @returns {Promise<MutationResponse<T>>} Mutation response
|
|
113
|
+
*
|
|
114
|
+
* @example
|
|
115
|
+
* ```typescript
|
|
116
|
+
* const result = await firekitDocMutations.set('users/123', {
|
|
117
|
+
* name: 'John Doe',
|
|
118
|
+
* email: 'john@example.com'
|
|
119
|
+
* }, { merge: true });
|
|
120
|
+
* ```
|
|
121
|
+
*/
|
|
55
122
|
async set(path, data, options = { merge: false, timestamps: true }) {
|
|
56
123
|
try {
|
|
57
124
|
const firestore = firebaseService.getDbInstance();
|
|
@@ -79,6 +146,21 @@ class FirekitDocumentMutations {
|
|
|
79
146
|
return this.handleError(error);
|
|
80
147
|
}
|
|
81
148
|
}
|
|
149
|
+
/**
|
|
150
|
+
* Updates a document at specified path
|
|
151
|
+
* @template T Document data type
|
|
152
|
+
* @param {string} path Document path
|
|
153
|
+
* @param {PartialWithFieldValue<T>} data Update data
|
|
154
|
+
* @param {MutationOptions} [options] Mutation options
|
|
155
|
+
* @returns {Promise<MutationResponse<Partial<T>>>} Mutation response
|
|
156
|
+
*
|
|
157
|
+
* @example
|
|
158
|
+
* ```typescript
|
|
159
|
+
* const result = await firekitDocMutations.update('users/123', {
|
|
160
|
+
* name: 'Jane Doe'
|
|
161
|
+
* });
|
|
162
|
+
* ```
|
|
163
|
+
*/
|
|
82
164
|
async update(path, data, options = { timestamps: true }) {
|
|
83
165
|
try {
|
|
84
166
|
const firestore = firebaseService.getDbInstance();
|
|
@@ -98,6 +180,16 @@ class FirekitDocumentMutations {
|
|
|
98
180
|
return this.handleError(error);
|
|
99
181
|
}
|
|
100
182
|
}
|
|
183
|
+
/**
|
|
184
|
+
* Deletes a document at specified path
|
|
185
|
+
* @param {string} path Document path
|
|
186
|
+
* @returns {Promise<MutationResponse<void>>} Mutation response
|
|
187
|
+
*
|
|
188
|
+
* @example
|
|
189
|
+
* ```typescript
|
|
190
|
+
* const result = await firekitDocMutations.delete('users/123');
|
|
191
|
+
* ```
|
|
192
|
+
*/
|
|
101
193
|
async delete(path) {
|
|
102
194
|
try {
|
|
103
195
|
const firestore = firebaseService.getDbInstance();
|
|
@@ -112,6 +204,16 @@ class FirekitDocumentMutations {
|
|
|
112
204
|
return this.handleError(error);
|
|
113
205
|
}
|
|
114
206
|
}
|
|
207
|
+
/**
|
|
208
|
+
* Checks if a document exists at specified path
|
|
209
|
+
* @param {string} path Document path
|
|
210
|
+
* @returns {Promise<boolean>} Whether document exists
|
|
211
|
+
*
|
|
212
|
+
* @example
|
|
213
|
+
* ```typescript
|
|
214
|
+
* const exists = await firekitDocMutations.exists('users/123');
|
|
215
|
+
* ```
|
|
216
|
+
*/
|
|
115
217
|
async exists(path) {
|
|
116
218
|
try {
|
|
117
219
|
const firestore = firebaseService.getDbInstance();
|
|
@@ -124,6 +226,20 @@ class FirekitDocumentMutations {
|
|
|
124
226
|
return false;
|
|
125
227
|
}
|
|
126
228
|
}
|
|
229
|
+
/**
|
|
230
|
+
* Gets a document at specified path
|
|
231
|
+
* @template T Document data type
|
|
232
|
+
* @param {string} path Document path
|
|
233
|
+
* @returns {Promise<MutationResponse<T>>} Mutation response with document data
|
|
234
|
+
*
|
|
235
|
+
* @example
|
|
236
|
+
* ```typescript
|
|
237
|
+
* const result = await firekitDocMutations.getDoc<UserData>('users/123');
|
|
238
|
+
* if (result.success) {
|
|
239
|
+
* console.log(result.data);
|
|
240
|
+
* }
|
|
241
|
+
* ```
|
|
242
|
+
*/
|
|
127
243
|
async getDoc(path) {
|
|
128
244
|
try {
|
|
129
245
|
const firestore = firebaseService.getDbInstance();
|
|
@@ -149,4 +265,9 @@ class FirekitDocumentMutations {
|
|
|
149
265
|
}
|
|
150
266
|
}
|
|
151
267
|
}
|
|
268
|
+
/**
|
|
269
|
+
* Pre-initialized instance of FirekitDocumentMutations
|
|
270
|
+
* @const
|
|
271
|
+
* @type {FirekitDocumentMutations}
|
|
272
|
+
*/
|
|
152
273
|
export const firekitDocMutations = new FirekitDocumentMutations();
|
package/dist/index.d.ts
CHANGED
|
@@ -2,9 +2,9 @@ export { firebaseConfig } from './config.js';
|
|
|
2
2
|
export { firebaseService } from './firebase.js';
|
|
3
3
|
export { firekitUser } from './auth/user.svelte.js';
|
|
4
4
|
export { firekitAuth } from './auth/auth.js';
|
|
5
|
-
export { presenceService } from './auth/presence.svelte';
|
|
5
|
+
export { presenceService } from './auth/presence.svelte.js';
|
|
6
6
|
export { firekitAwaitableDoc } from './firestore/awaitable-doc.svelte.js';
|
|
7
|
-
export { firekitDocMutations } from './firestore/document-mutations.svelte';
|
|
7
|
+
export { firekitDocMutations } from './firestore/document-mutations.svelte.js';
|
|
8
8
|
export { firekitCollection } from './firestore/collection.svelte.js';
|
|
9
9
|
export { firekitDoc } from './firestore/doc.svelte.js';
|
|
10
10
|
export { firekitRealtimeDB } from './realtime/realtime.svelte.js';
|
package/dist/index.js
CHANGED
|
@@ -4,10 +4,10 @@ export { firebaseService } from './firebase.js';
|
|
|
4
4
|
// auth services
|
|
5
5
|
export { firekitUser } from './auth/user.svelte.js';
|
|
6
6
|
export { firekitAuth } from './auth/auth.js';
|
|
7
|
-
export { presenceService } from './auth/presence.svelte';
|
|
7
|
+
export { presenceService } from './auth/presence.svelte.js';
|
|
8
8
|
// firestore services
|
|
9
9
|
export { firekitAwaitableDoc } from './firestore/awaitable-doc.svelte.js';
|
|
10
|
-
export { firekitDocMutations } from './firestore/document-mutations.svelte';
|
|
10
|
+
export { firekitDocMutations } from './firestore/document-mutations.svelte.js';
|
|
11
11
|
export { firekitCollection } from './firestore/collection.svelte.js';
|
|
12
12
|
export { firekitDoc } from './firestore/doc.svelte.js';
|
|
13
13
|
// realtime services
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module FirekitRealtimeDB
|
|
3
|
+
*/
|
|
4
|
+
import { type DatabaseReference } from "firebase/database";
|
|
5
|
+
/**
|
|
6
|
+
* Manages real-time Firebase Realtime Database subscriptions with reactive state
|
|
7
|
+
* @class
|
|
8
|
+
* @template T Data type
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* interface ChatMessage {
|
|
13
|
+
* text: string;
|
|
14
|
+
* userId: string;
|
|
15
|
+
* timestamp: number;
|
|
16
|
+
* }
|
|
17
|
+
*
|
|
18
|
+
* // Create regular reference
|
|
19
|
+
* const chatRef = firekitRealtimeDB<ChatMessage>('chats/123');
|
|
20
|
+
*
|
|
21
|
+
* // Create list reference
|
|
22
|
+
* const messagesList = firekitRealtimeList<ChatMessage>('messages');
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
declare class FirekitRealtimeDB<T> {
|
|
26
|
+
/** Current data */
|
|
27
|
+
private _data;
|
|
28
|
+
/** Loading state */
|
|
29
|
+
private _loading;
|
|
30
|
+
/** Error state */
|
|
31
|
+
private _error;
|
|
32
|
+
/** Database reference */
|
|
33
|
+
private dbRef;
|
|
34
|
+
/** Subscription cleanup function */
|
|
35
|
+
private unsubscribe;
|
|
36
|
+
/**
|
|
37
|
+
* Creates a Realtime Database subscription
|
|
38
|
+
* @param {string} path Database path
|
|
39
|
+
* @param {T} [startWith] Initial data before fetch completes
|
|
40
|
+
*/
|
|
41
|
+
constructor(path: string, startWith?: T);
|
|
42
|
+
/**
|
|
43
|
+
* Initializes database subscription
|
|
44
|
+
* @private
|
|
45
|
+
* @param {string} path Database path
|
|
46
|
+
*/
|
|
47
|
+
private initializeRealtimeDB;
|
|
48
|
+
/**
|
|
49
|
+
* Pushes new data to list
|
|
50
|
+
* @param {T} data Data to push
|
|
51
|
+
* @returns {Promise<string | null>} New item key or null if failed
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```typescript
|
|
55
|
+
* const key = await chatRef.push({
|
|
56
|
+
* text: 'Hello',
|
|
57
|
+
* userId: '123',
|
|
58
|
+
* timestamp: Date.now()
|
|
59
|
+
* });
|
|
60
|
+
* ```
|
|
61
|
+
*/
|
|
62
|
+
push(data: T): Promise<string | null>;
|
|
63
|
+
/**
|
|
64
|
+
* Sets data at reference
|
|
65
|
+
* @param {T} data Data to set
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* ```typescript
|
|
69
|
+
* await chatRef.set({
|
|
70
|
+
* text: 'Updated message',
|
|
71
|
+
* userId: '123',
|
|
72
|
+
* timestamp: Date.now()
|
|
73
|
+
* });
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
76
|
+
set(data: T): Promise<void>;
|
|
77
|
+
/**
|
|
78
|
+
* Updates data at reference
|
|
79
|
+
* @param {Partial<T>} data Data to update
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* ```typescript
|
|
83
|
+
* await chatRef.update({
|
|
84
|
+
* text: 'Edited message'
|
|
85
|
+
* });
|
|
86
|
+
* ```
|
|
87
|
+
*/
|
|
88
|
+
update(data: Partial<T>): Promise<void>;
|
|
89
|
+
/**
|
|
90
|
+
* Removes data at reference
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* ```typescript
|
|
94
|
+
* await chatRef.remove();
|
|
95
|
+
* ```
|
|
96
|
+
*/
|
|
97
|
+
remove(): Promise<void>;
|
|
98
|
+
/** Gets current data */
|
|
99
|
+
get data(): T | null;
|
|
100
|
+
/** Gets loading state */
|
|
101
|
+
get loading(): boolean;
|
|
102
|
+
/** Gets error state */
|
|
103
|
+
get error(): Error | null;
|
|
104
|
+
/**
|
|
105
|
+
* Gets database reference
|
|
106
|
+
* @throws {Error} If reference is not available
|
|
107
|
+
*/
|
|
108
|
+
get ref(): DatabaseReference;
|
|
109
|
+
/** Cleanup subscription */
|
|
110
|
+
dispose(): void;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Creates a reactive Realtime Database reference
|
|
114
|
+
* @template T Data type
|
|
115
|
+
* @param {string} path Database path
|
|
116
|
+
* @param {T} [startWith] Initial data
|
|
117
|
+
* @returns {FirekitRealtimeDB<T>} Database subscription instance
|
|
118
|
+
*
|
|
119
|
+
* @example
|
|
120
|
+
* ```typescript
|
|
121
|
+
* const chatRef = firekitRealtimeDB<ChatMessage>('chats/123');
|
|
122
|
+
* ```
|
|
123
|
+
*/
|
|
124
|
+
export declare function firekitRealtimeDB<T>(path: string, startWith?: T): FirekitRealtimeDB<T>;
|
|
125
|
+
/**
|
|
126
|
+
* Creates a reactive Realtime Database list reference
|
|
127
|
+
* Automatically converts data to array format with IDs
|
|
128
|
+
*
|
|
129
|
+
* @template T List item type
|
|
130
|
+
* @param {string} path Database path
|
|
131
|
+
* @param {T[]} [startWith=[]] Initial array data
|
|
132
|
+
* @returns {FirekitRealtimeDB} Database subscription instance with array support
|
|
133
|
+
*
|
|
134
|
+
* @example
|
|
135
|
+
* ```typescript
|
|
136
|
+
* const messagesList = firekitRealtimeList<ChatMessage>('messages');
|
|
137
|
+
* console.log(messagesList.list); // Array of messages with IDs
|
|
138
|
+
* ```
|
|
139
|
+
*/
|
|
140
|
+
export declare function firekitRealtimeList<T>(path: string, startWith?: T[]): FirekitRealtimeDB<Record<string, T>> & {
|
|
141
|
+
list: Array<T & {
|
|
142
|
+
id: string;
|
|
143
|
+
}>;
|
|
144
|
+
};
|
|
145
|
+
export {};
|