bradb 1.0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Thorque Software
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,133 @@
1
+ # Brad API Utilities
2
+
3
+ **Brad** is a utility library designed to dramatically speed up the development of RESTful APIs in Node.js. It natively integrates [Express](https://expressjs.com/), [Drizzle ORM](https://orm.drizzle.team/), and [Zod](https://zod.dev/) to provide a solid and efficient foundation for your projects.
4
+
5
+ The philosophy of `brad` is simple: reduce repetitive boilerplate code to a minimum, allowing you to focus on business logic.
6
+
7
+ ## Core Features
8
+
9
+ - **Base CRUD Controller:** Automatically generates endpoints for `getAll`, `getById`, `create`, `update`, and `delete`.
10
+ - **Standardized Services:** Factory functions to create database access logic (`findAll`, `findOne`, `create`, etc.).
11
+ - **Automatic Pagination and Filtering:** Extracts pagination (`page`, `pageSize`) and filter parameters from the URL.
12
+ - **Validation with Zod:** Uses Zod schemas to validate input data.
13
+ - **Integrated Error Handling:** An Express `errorHandler` that catches and formats errors from `Zod`, `Drizzle`, and custom service errors.
14
+
15
+ ## Installation
16
+
17
+ ```bash
18
+ npm install brad
19
+ ```
20
+
21
+ Make sure to also install the `peer` dependencies:
22
+
23
+ ```bash
24
+ npm install express drizzle-orm pg zod
25
+ npm install -D @types/express @types/pg
26
+ ```
27
+
28
+ ## Quickstart Guide
29
+
30
+ Here is an example of how to structure an API with `brad`.
31
+
32
+ ### 1. Define Your Drizzle Schema
33
+
34
+ Create your database schema as you normally would with Drizzle.
35
+
36
+ `src/schema.ts`:
37
+ ```typescript
38
+ import { pgTable, serial, text, timestamp } from "drizzle-orm/pg-core";
39
+
40
+ export const serviceTable = pgTable("services", {
41
+ id: serial("id").primaryKey(),
42
+ name: text("name").notNull(),
43
+ // ... other fields
44
+ deletedAt: timestamp("deleted_at", { withTimezone: true, mode: "date" })
45
+ });
46
+ ```
47
+
48
+ ### 2. Create the Service
49
+
50
+ Use `brad`'s factory functions to create a CRUD service for your table.
51
+
52
+ `src/services.ts`:
53
+ ```typescript
54
+ import { db } from "./db"; // Your Drizzle instance
55
+ import { serviceTable } from "./schema";
56
+ import { create, findAll, findOne, softDelete, update, count } from "brad";
57
+ import { eq } from "drizzle-orm";
58
+
59
+ // Filter map for queries
60
+ const filterMap = {
61
+ name: (value: string) => eq(serviceTable.name, value)
62
+ };
63
+
64
+ export const serviceService = {
65
+ findAll: findAll(filterMap, db.select().from(serviceTable)),
66
+ findOne: findOne(serviceTable, db.select().from(serviceTable)),
67
+ create: create(db, serviceTable),
68
+ update: update(db, serviceTable),
69
+ delete: softDelete(db, serviceTable),
70
+ count: count(db, serviceTable, filterMap)
71
+ };
72
+ ```
73
+
74
+ ### 3. Create the Controller
75
+
76
+ Extend `brad`'s `BaseController`, passing it the service and Zod validation schemas.
77
+
78
+ `src/controllers.ts`:
79
+ ```typescript
80
+ import { BaseController } from "brad";
81
+ import { serviceService } from "./services";
82
+ import { serviceTable } from "./schema";
83
+ import { z } from "zod";
84
+ import { createSelectSchema } from "drizzle-zod";
85
+
86
+ // Base Zod schema from the Drizzle schema
87
+ const serviceSchema = createSelectSchema(serviceTable);
88
+
89
+ export const serviceController = new BaseController(
90
+ serviceService,
91
+ serviceSchema,
92
+ serviceSchema.pick({ name: true }).partial() // Schema for filters
93
+ );
94
+ ```
95
+
96
+ ### 4. Set up the Router and Server
97
+
98
+ Connect the routes to the controller methods and don't forget to add the `errorHandler`.
99
+
100
+ `src/router.ts`:
101
+ ```typescript
102
+ import { Router } from "express";
103
+ import { serviceController } from "./controllers";
104
+
105
+ const router = Router();
106
+
107
+ router.get("/", serviceController.getAll);
108
+ router.get("/:id", serviceController.getById);
109
+ router.post("/", serviceController.create);
110
+ router.put("/:id", serviceController.update);
111
+ router.delete("/:id", serviceController.delete);
112
+
113
+ export default router;
114
+ ```
115
+
116
+ `src/index.ts`:
117
+ ```typescript
118
+ import express from "express";
119
+ import serviceRouter from "./router";
120
+ import { errorHandler } from "brad";
121
+
122
+ const app = express();
123
+ app.use(express.json());
124
+
125
+ app.use("/services", serviceRouter);
126
+
127
+ // Important! Add the error handler at the end
128
+ app.use(errorHandler);
129
+
130
+ app.listen(3000, () => {
131
+ console.log(`Server is running on port 3000`);
132
+ });
133
+ ```
@@ -0,0 +1,6 @@
1
+ export * from './src/types';
2
+ export * from './src/controller';
3
+ export * from './src/standard';
4
+ export * from './src/relational';
5
+ export * from './src/utils';
6
+ export * from './src/errors';
package/dist/index.js ADDED
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./src/types"), exports);
18
+ __exportStar(require("./src/controller"), exports);
19
+ __exportStar(require("./src/standard"), exports);
20
+ __exportStar(require("./src/relational"), exports);
21
+ __exportStar(require("./src/utils"), exports);
22
+ __exportStar(require("./src/errors"), exports);
package/package.json ADDED
@@ -0,0 +1,30 @@
1
+ {
2
+ "name": "bradb",
3
+ "version": "1.0.0",
4
+ "description": "",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "scripts": {
8
+ "build": "rm -rf dist/ && tsc",
9
+ "test": "echo \"Error: no test specified\" && exit 1"
10
+ },
11
+ "keywords": [
12
+ "api",
13
+ "orm"
14
+ ],
15
+ "author": "Thorque Software",
16
+ "license": "MIT",
17
+ "type": "commonjs",
18
+ "devDependencies": {
19
+ "@types/express": "^5.0.3",
20
+ "@types/pg": "^8.15.5",
21
+ "typescript": "^5.9.2"
22
+ },
23
+ "peerDependencies": {
24
+ "@types/pg": "^8.15.5",
25
+ "drizzle-orm": "^0.44.5",
26
+ "express": "^5.1.0",
27
+ "pg": "^8.16.3",
28
+ "zod": "^4.1.5"
29
+ }
30
+ }