firestore-db-orm
is a simple ORM library for Firestore, designed to provide easy-to-use, TypeScript-typed CRUD
operations.
Install firebase-db-orm with bun
bun add firestore-db-orm
import { FirestoreORM } from "firestore-db-orm";
import { db } from "./firestore";
import type { IUser } from "./user.interface";
const userORM = new FirestoreORM<IUser>(db, "users");
export default userORM;
import userORM from "./user.orm";
import { v4 } from "uuid";
(async () => {
const newUser = await userORM.add({
id: v4(),
email: "test@example.com",
password: "securepassword",
});
console.log("New user created:", newUser);
const user = await userORM.get(newUser.id);
if (user) {
console.log("User found:", user);
}
await userORM.update(newUser.id, { email: "newemail@example.com" });
await userORM.delete(newUser.id);
const otherUser = await userORM.findOne({
email: { where: "==", value: "email@asd.com" },
});
})();
Find documents where age
is equal to 25.
const searchParams1: SearchParams = {
age: 25,
};
const result1 = await userORM.finds(searchParams1);
console.log(result1);
Find documents where age
is greater than 18.
const searchParams2: SearchParams = {
age: {
value: 18,
where: ">",
},
};
const result2 = await userORM.finds(searchParams2);
console.log(result2);
Find documents where age
is greater than 10 and less than 30.
const searchParams3: SearchParams = {
age: [
{
value: 10,
where: ">",
},
{
value: 30,
where: "<",
},
],
};
const result3 = await userORM.finds(searchParams3);
console.log(result3);
Find documents where age
is greater than 18 and name
is equal to "Juan".
const searchParams4: SearchParams = {
age: {
value: 18,
where: ">",
},
name: "Juan",
};
const result4 = await userORM.finds(searchParams4);
console.log(result4);
Find documents where age
is within certain ranges and name
is equal to "Ana".
const searchParams5: SearchParams = {
age: [
{
value: 10,
where: ">",
},
{
value: 30,
where: "<",
},
],
name: "Ana",
};
const result5 = await userORM.finds(searchParams5);
console.log(result5);
Find documents where age
is greater than 10 and less than 30, name
is "Pedro", and active
is true.
const searchParams6: SearchParams = {
age: [
{
value: 10,
where: ">",
},
{
value: 30,
where: "<",
},
],
name: "Pedro",
active: true,
};
const result6 = await userORM.finds(searchParams6);
console.log(result6);