model-blueprint 0.1.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.md +21 -0
- package/README.md +82 -0
- package/dist/createId/createId.d.ts +17 -0
- package/dist/createId/createId.js +1724 -0
- package/dist/createId/createId.js.map +1 -0
- package/dist/dropQuery/dropQuery.d.ts +16 -0
- package/dist/dropQuery/dropQuery.js +50 -0
- package/dist/dropQuery/dropQuery.js.map +1 -0
- package/dist/index.d.ts +100 -0
- package/dist/index.js +148 -0
- package/dist/index.js.map +1 -0
- package/dist/withCursor/withCursor.d.ts +6 -0
- package/dist/withCursor/withCursor.js +13 -0
- package/dist/withCursor/withCursor.js.map +1 -0
- package/package.json +80 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Roman Jankowski
|
|
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,82 @@
|
|
|
1
|
+
# db-model
|
|
2
|
+
|
|
3
|
+
A type-safe database agnostic model factory and procedure builder for constructing per-request, strongly-typed surfaces from a static blueprint.
|
|
4
|
+
|
|
5
|
+
This project provides:
|
|
6
|
+
|
|
7
|
+
- A blueprint-driven model factory that hydrates a nested API surface per-request.
|
|
8
|
+
- A fluent ProcedureBuilder for input validation, middleware composition, and final query handlers.
|
|
9
|
+
- Lightweight utilities for safe object checks, schema parsing, and error dropping.
|
|
10
|
+
- A helper for cursor-based pagination.
|
|
11
|
+
|
|
12
|
+
## Installation
|
|
13
|
+
|
|
14
|
+
- npm install model-blueprint
|
|
15
|
+
- yarn add model-blueprint
|
|
16
|
+
- pnpm add model-blueprint
|
|
17
|
+
|
|
18
|
+
## Quick Start
|
|
19
|
+
|
|
20
|
+
```typescript
|
|
21
|
+
// 1. Setup
|
|
22
|
+
import { createModelFactory, initProcedure } from "model-blueprint";
|
|
23
|
+
import { dropQuery } from "model-blueprint/dropQuery";
|
|
24
|
+
import { createId } from "model-blueprint/createId";
|
|
25
|
+
import { withCursor } from "model-blueprint/withCursor";
|
|
26
|
+
|
|
27
|
+
type Context = {
|
|
28
|
+
db: DBDriver,
|
|
29
|
+
authHeader: string
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const dropMessages = {
|
|
33
|
+
notAuthenticated: 'not authenticated',
|
|
34
|
+
notFound: 'not found'
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const proc = initProcedure<Context>()
|
|
38
|
+
.use(dropQuery(dropMessages)) // populates ctx with drop function which throws HTTPException
|
|
39
|
+
|
|
40
|
+
const userGetQuery = proc
|
|
41
|
+
.input(z.object({ id: z.string() }))
|
|
42
|
+
.query(async ({ ctx, input }) => {
|
|
43
|
+
const user = await ctx.db.users.findUnique({ where: { id: input.id } });
|
|
44
|
+
if (!user) throw ctx.drop('notFound', 404);
|
|
45
|
+
return user;
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
const usersQuery = proc
|
|
49
|
+
.use(async ({ ctx, input }) => { // Auth middleware example
|
|
50
|
+
if (!ctx.authHeader) throw ctx.drop('notAuthenticated', 401);
|
|
51
|
+
return { ...ctx, input };
|
|
52
|
+
})
|
|
53
|
+
.input(z.object({ cursor: z.string().optional(), limit: z.number().optional() }))
|
|
54
|
+
.query(async ({ ctx, { cursor, limit } }) => {
|
|
55
|
+
const users = await ctx.db.users.all({ cursor, limit });
|
|
56
|
+
return withCursor(users, 'id', limit); // returns { entries, cursor }
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
const modelFactory = createModelFactory<Context>({
|
|
60
|
+
users: {
|
|
61
|
+
get: userGetQuery,
|
|
62
|
+
list: usersQuery
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
// 2. Hydrate the model
|
|
67
|
+
const model = modelFactory({ db: myDbInstance, authHeader });
|
|
68
|
+
|
|
69
|
+
// 3. Use model
|
|
70
|
+
const user = await model.users.get({ id: 1 });
|
|
71
|
+
const { entries, cursor } = await mode.users.list({ limit: 20})
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
What happens here:
|
|
75
|
+
|
|
76
|
+
- The blueprint is walked recursively.
|
|
77
|
+
- When a leaf value is a QueryBuilder, it is invoked with the provided context to produce a concrete API method for that leaf.
|
|
78
|
+
- Non-object leaves are copied through; nested objects are hydrated recursively.
|
|
79
|
+
|
|
80
|
+
## API Reference
|
|
81
|
+
|
|
82
|
+
This project is organized into a few focused modules. Below is a concise reference for each public API.
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { createId as createId$1, init } from '@paralleldrive/cuid2';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Middleware factory that adds a `createId` and `init` function to the context.
|
|
5
|
+
* This function allows procedures to create CUID2 IDs and initialize CUID2.
|
|
6
|
+
* @template TContext - The context type.
|
|
7
|
+
* @param ctx - The context object.
|
|
8
|
+
* @returns A middleware function that injects the `createId` and `init` helpers into the context.
|
|
9
|
+
*/
|
|
10
|
+
declare const createId: <TContext>({ ctx }: {
|
|
11
|
+
ctx: TContext;
|
|
12
|
+
}) => TContext & {
|
|
13
|
+
createId: typeof createId$1;
|
|
14
|
+
init: typeof init;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export { createId };
|