react-firebase-ql 0.1.0 → 0.2.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.
- package/README.md +169 -9
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,15 +1,175 @@
|
|
|
1
|
+
|
|
1
2
|
# react-firebase-ql
|
|
2
3
|
|
|
3
|
-
|
|
4
|
+
[](https://www.npmjs.com/package/react-firebase-ql) [](https://opensource.org/licenses/MIT)
|
|
5
|
+
|
|
6
|
+
React Firebase QL is a lightweight library that **simplifies working with Firebase in React apps**. It wraps Firebase queries and updates inside **React hooks**, giving developers a feel of using Firebase like a **SQL environment**, where you can quickly call methods like `.save()` or `.find()` on your models.
|
|
7
|
+
|
|
8
|
+
It also provides simple utilities for **Firebase Authentication, Firestore, and Storage** — including easy file uploads via the `.doUpload` method.
|
|
9
|
+
|
|
10
|
+
## 🔹 Features
|
|
11
|
+
|
|
12
|
+
- **React hooks for Firebase**: `useFetch`, `useStream`, `useAuth`, `useCount`.
|
|
13
|
+
- **Model-based API**: Create classes extending `BaseModel` to interact with Firestore.
|
|
14
|
+
- **SQL-like operations**: Quickly `save()` or `find()` data without boilerplate.
|
|
15
|
+
- **File uploads**: Simple `.doUpload` on the `Storage` class.
|
|
16
|
+
- Supports **Firebase Authentication, Firestore, and Storage**.
|
|
17
|
+
- Written in **TypeScript** with full type support.
|
|
18
|
+
|
|
19
|
+
## ⚙️ Installation
|
|
20
|
+
|
|
21
|
+
Install via npm or yarn:
|
|
4
22
|
|
|
5
|
-
|
|
6
|
-
npm install firebase
|
|
7
|
-
|
|
23
|
+
```bash
|
|
24
|
+
npm install react-firebase-ql firebase
|
|
25
|
+
# or
|
|
26
|
+
yarn add react-firebase-ql firebase
|
|
27
|
+
```
|
|
8
28
|
|
|
9
|
-
## Usage
|
|
10
|
-
import { useFirebaseAuth } from 'my-react-firebase-lib';
|
|
11
29
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
## 🛠️ Setup
|
|
33
|
+
**Initialize Firebase**
|
|
34
|
+
|
|
35
|
+
Make sure you have Firebase initialized in your project:
|
|
36
|
+
|
|
37
|
+
```ts
|
|
38
|
+
// firebase.config.ts
|
|
39
|
+
import { initializeApp } from "firebase/app";
|
|
40
|
+
import { AppCheck, initializeAppCheck, ReCaptchaEnterpriseProvider } from "firebase/app-check";
|
|
41
|
+
import { connectAuthEmulator, getAuth } from "firebase/auth";
|
|
42
|
+
import { connectFirestoreEmulator, getFirestore } from "firebase/firestore";
|
|
43
|
+
import { connectFunctionsEmulator, getFunctions } from "firebase/functions";
|
|
44
|
+
import { connectStorageEmulator, getStorage } from "firebase/storage"
|
|
45
|
+
|
|
46
|
+
declare global {
|
|
47
|
+
// eslint-disable-next-line no-var
|
|
48
|
+
var FIREBASE_APPCHECK_DEBUG_TOKEN: boolean | string | undefined;
|
|
15
49
|
}
|
|
50
|
+
|
|
51
|
+
const firebaseConfig = {
|
|
52
|
+
apiKey: "...",
|
|
53
|
+
authDomain: "...",
|
|
54
|
+
projectId: "...",
|
|
55
|
+
storageBucket: "...",
|
|
56
|
+
messagingSenderId: "...",
|
|
57
|
+
appId: "..."
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
// Initialize Firebase
|
|
61
|
+
const app = initializeApp(firebaseConfig);
|
|
62
|
+
let appCheck: AppCheck;
|
|
63
|
+
|
|
64
|
+
// for appcheck (recommended)
|
|
65
|
+
if (typeof window !== "undefined") {
|
|
66
|
+
self.FIREBASE_APPCHECK_DEBUG_TOKEN = window.location.hostname==='localhost';
|
|
67
|
+
|
|
68
|
+
appCheck = initializeAppCheck(app, {
|
|
69
|
+
provider: new ReCaptchaEnterpriseProvider('YOUR-APPCHECK-KEY'),
|
|
70
|
+
isTokenAutoRefreshEnabled: true // Set to true to allow auto-refresh.
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// initialize other firebase resources
|
|
75
|
+
export const firebaseAuth = getAuth(app)
|
|
76
|
+
export const firestoreDB = getFirestore(app)
|
|
77
|
+
export const server = getFunctions(app)
|
|
78
|
+
export const storage = getStorage(app)
|
|
79
|
+
|
|
80
|
+
// set emulator for local environment
|
|
81
|
+
if(typeof window !== 'undefined' && window.location.hostname === 'localhost'){
|
|
82
|
+
const host = "127.0.0.1";
|
|
83
|
+
connectAuthEmulator(firebaseAuth, `http://${host}:9099`);
|
|
84
|
+
connectFunctionsEmulator(server, host, 5001);
|
|
85
|
+
connectFirestoreEmulator(firestoreDB, host, 8080);
|
|
86
|
+
connectStorageEmulator(storage, host, 9199);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
// Create and export firestore documents like this
|
|
91
|
+
export enum FIREBASETABLE {
|
|
92
|
+
USERS = 'users',
|
|
93
|
+
---
|
|
94
|
+
}
|
|
95
|
+
```
|
|
96
|
+
## Sample Usage
|
|
97
|
+
|
|
98
|
+
Organize your models in a dedicated folder
|
|
99
|
+
src/ or app/ - for nextjs developers
|
|
100
|
+
|
|
101
|
+
src/
|
|
102
|
+
|
|
103
|
+
└── models/
|
|
104
|
+
|
|
105
|
+
├── Users.model.ts
|
|
106
|
+
|
|
107
|
+
├── Posts.model.ts
|
|
108
|
+
|
|
109
|
+
└── ...other models
|
|
110
|
+
|
|
111
|
+
```
|
|
112
|
+
// src/models/Users.model.ts
|
|
113
|
+
import { firestoreDB } from "../firebase.config";
|
|
114
|
+
import { FIREBASETABLE } from "../firebase.config";
|
|
115
|
+
import { BaseModel, errorLogger } from "react-firebase-ql";
|
|
116
|
+
|
|
117
|
+
export interface YourFirestoreDocumentStructure {
|
|
118
|
+
reference?: string,
|
|
119
|
+
firstName: string;
|
|
120
|
+
---
|
|
121
|
+
}
|
|
122
|
+
export class UsersModel extends BaseModel {
|
|
123
|
+
|
|
124
|
+
data: YourFirestoreDocumentStructure | YourFirestoreDocumentStructure[] | undefined;
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
constructor(){
|
|
128
|
+
super(FIREBASETABLE.users, firestoreDB);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
otherMethods(){
|
|
132
|
+
...
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
Fetch a user record
|
|
141
|
+
|
|
142
|
+
```
|
|
143
|
+
import { useFetch } from "react-firebase-ql";
|
|
144
|
+
import { UsersModel } from "../models/Users.model";
|
|
145
|
+
|
|
146
|
+
export default function UserProfile() {
|
|
147
|
+
const [user, loading] = useFetch({
|
|
148
|
+
model: new UsersModel(),
|
|
149
|
+
reference: reference! --document reference
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
if (loading) return <p>Loading...</p>;
|
|
153
|
+
if (error) return <p>Error: {error.message}</p>;
|
|
154
|
+
|
|
155
|
+
return (
|
|
156
|
+
<p>
|
|
157
|
+
{user.firstName}
|
|
158
|
+
</p>
|
|
159
|
+
);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
## Acknowledgements
|
|
165
|
+
|
|
166
|
+
- [Awesome Readme Templates](https://awesomeopensource.com/project/elangosundar/awesome-README-templates)
|
|
167
|
+
- [Awesome README](https://github.com/matiassingers/awesome-readme)
|
|
168
|
+
- [How to write a Good readme](https://bulldogjob.com/news/449-how-to-write-a-good-readme-for-your-github-project)
|
|
169
|
+
|
|
170
|
+
Breedware Limited
|
|
171
|
+
## Authors
|
|
172
|
+
|
|
173
|
+
- [@breedware](https://www.github.com/breedware)
|
|
174
|
+
|
|
175
|
+
Breedware Limited
|
package/package.json
CHANGED