What?
I'm writing my first custom node (not a script node). While I'm navigating using the documentation here, I'm not seeing any guidance on how to use other external npm libraries, and also most of them use ts
.
In my case I'm trying to use firebase, for example.
What I have so far?
My super early WIP is here.
What it has so far is some basic inputs for firebase RTDB config with which I'm beginning to test.
In future these will be in a configuration node.
Notes:
- I do not intent to not push it to npm BTW (only for my learning purposes).
- I will have configuration node and everything setup later.
- There will be many more features like status green when successfully connected to DB etc.
What help I'm looking for?
I'm not the JS/TS ninja unfortunately
So I was wondering if there could be someone who can show how to implement the simple functions (in my WIP firebase-rtdb-connect.js) like the one below (as those functions would are intended to be implemented):
Note:
- I'm currently facing struggles with these import styles and examples for pure js in firebase's documentation doesn't exist.
- I also came across a tutorial here, which helped a lot in my custom node creation's mental model but I'm getting stuck while trying/testing to importing ext modules.
click test.mjs (some function in here will are intended for implmentation)
import { initializeApp } from 'firebase/app';
import { getDatabase, ref, child, set, get, onValue, onChildAdded, onChildChanged, onChildRemoved} from "firebase/database";
const firebaseConfig = {
apiKey: "xxx",
authDomain: "xxx",
databaseURL: "xxx",
projectId: "xxx",
storageBucket: "xxx",
messagingSenderId: "xxx",
appId: "xxx"
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
const db = getDatabase();
const dbRef = ref(db);
// -- Listen Always
// onValue(data_struct_var, (snapshot) => {
onValue(ref(db, 'children'), (snapshot) => {
const data = snapshot.val();
console.log("Const Listener->\t\t", data);
});
// -- Child Added
onChildAdded(ref(db, 'children'), (data) => {
console.log("\nOn child added->\t\t", "data.key:", data.key, ", data.val():", data.val());
});
// -- Child Changed
onChildChanged(ref(db, 'children'), (data) => {
console.log("\nOn child changed->\t\t", "data.key:", data.key, ", data.val():", data.val());
});
onChildChanged(ref(db, 'test'), (data) => {
console.log("\nOn child changed->\t\t", "data.key:", data.key, ", data.val():", data.val());
});
// -- Child Removed
onChildRemoved(ref(db, 'children'), (data) => {
console.log("\nOn child removed->\t\t", "data.key:", data.key, ", data.val():", data.val());
});
// -- Get once
get(child(dbRef, 'children')).then((snapshot) => {
if (snapshot.exists()){
const data = snapshot.val();
console.log("One time Get() Listener->\t", data);
}else{
console.log("\nNo data available");
}
}).catch((error) => {
console.error(error);
});
// -- Set/Write/Update
// set(ref(db, 'test'), {
// username: name,
// email: email,
// profile_picture : imageUrl
// });
set(ref(db, 'test'), 6);
// -- Push