0.1.1 • Published 11 months ago
typescript-pocketbase-orm v0.1.1
TypeScript ORM for PocketBase
The main goal of this project is to make it easier to use PocketBase with TypeScript. I created this to make it easier to use PocketBase with NestJS and to get an ORM similar to TypeORM or Mongoose.
Usage
import { PocketBaseORM } from "typescript-pocketbase-orm";
import { SortSide, findQuery } from 'typescript-pocketbase-orm/dist/interfaces';
const pb = new PocketBaseORM("http://localhost:8000");
await pb.authAdmin("admin", "admin");
const res = await pb.findAll({
collection: "users",
where: "email='test@test.com'",
sortBy: {
field: "email",
side: SortSide.ASC
},
relations: ["posts"]
});
console.log(res);
const res2 = await pb.find({
collection: "users",
where: "email='test@test.com'",
sortBy: {
field: "email",
side: SortSide.ASC
},
page: 1,
limit: 10,
relations: ["posts"]
});
console.log(res2);
Types
All types are in 'typescript-pocketbase-orm/dist/interfaces'
Errors
All errors are in 'typescript-pocketbase-orm/dist/errors'
import { UnauthenticatedError, UnauthorizedError, SomethingWentWrongError, ResourceNotFoundError } from "typescript-pocketbase-orm/dist/errors";
try {
await pb.authAdmin("admin", "admin");
} catch (e) {
if (e instanceof UnauthenticatedError) {
console.log("Unauthenticated");
}
}
try {
await pb.findAll({
collection: "users",
where: "email='test@test.com'",
sortBy: {
field: "email",
side: SortSide.ASC
},
relations: ["posts"]
});
} catch (e) {
if (e instanceof SomethingWentWrongError) {
console.log("Something went wrong");
}
if (e instanceof UnauthenticatedError) {
console.log("Unauthenticated");
}
if (e instanceof UnauthorizedError) {
console.log("Unauthorized");
}
if (e instanceof ResourceNotFoundError) {
console.log("Resource not found");
}
}
Available methods
authAdmin
async authAdmin(username: string, password: string);
authUser
async authUser(username: string, password: string, userCollectionName: string = 'users');
logout
async logout();
findAll
async findAll(query: findAllQuery);
find
async find(query: findQuery);
findFirst
async findFirst(query: findFirstQuery);
findById
async findById(query: findByIdQuery);
create
async create(collection: string, data: any);
update
async update(collection: string, id: string, data: any);
delete
async delete(collection: string, id: string);
verifyToken
async verifyToken(token: string, userCollectionName: string = 'users');
loadToken
loadToken(token: string);
requestPasswordReset
async requestPasswordReset(email: string, userCollectionName: string = 'users');
confirmPasswordReset
async confirmPasswordReset(resetPasswordData: ResetPasswordQuery, userCollectionName: string = 'users');