@supawatch/target-orpc 0.5.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) 2026 Omar Dulaimi
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,26 @@
1
+ # @supawatch/target-orpc
2
+
3
+ The oRPC target for
4
+ [supawatch](https://github.com/omar-dulaimi/supawatch). Emits one oRPC
5
+ router per table: `list`, `byId` keyed by the primary key, and `create`
6
+ on tables, with inputs validated by the already generated Zod schemas
7
+ through `os.input()`. Each module exports a factory that takes your
8
+ postgres.js connection and returns the router, ready to serve or to
9
+ call in process with oRPC's `call()`.
10
+
11
+ ```ts
12
+ targets: [
13
+ { kind: "zod", strict: true, emit: { insert: true } },
14
+ { kind: "orpc" },
15
+ ]
16
+ ```
17
+
18
+ Requires the zod target; `schemasImportPath` (default `"../zod"`)
19
+ points the emitted imports at it. API shapes verified empirically
20
+ against `@orpc/server` 1.x: invalid inputs reject with `ORPCError`
21
+ before the handler runs.
22
+
23
+ The repo's suite calls the emitted procedures against a real database,
24
+ valid and invalid inputs both.
25
+
26
+ MIT.
@@ -0,0 +1,13 @@
1
+ import type { Rendered, Snapshot, Table, Target, TargetCapabilities, TargetOptions } from "@supawatch/core";
2
+ export interface OrpcTargetOptions extends TargetOptions {
3
+ schemasImportPath?: string;
4
+ }
5
+ export declare function exportNameFor(table: Table): string;
6
+ export declare class OrpcTarget implements Target<OrpcTargetOptions> {
7
+ readonly name = "orpc";
8
+ readonly fileExtension = ".mjs";
9
+ readonly capabilities: TargetCapabilities;
10
+ renderTable(table: Table, _snapshot: Snapshot, opts: OrpcTargetOptions): Rendered;
11
+ renderTypes(table: Table, _snapshot: Snapshot, _opts: OrpcTargetOptions): string;
12
+ }
13
+ export default OrpcTarget;
package/dist/index.js ADDED
@@ -0,0 +1,62 @@
1
+ function baseNameFor(table) {
2
+ return table.name.replace(/[^a-zA-Z0-9_]/g, "_");
3
+ }
4
+ export function exportNameFor(table) {
5
+ const base = baseNameFor(table);
6
+ return `create${base.charAt(0).toUpperCase() + base.slice(1)}Orpc`;
7
+ }
8
+ function quotedIdent(table) {
9
+ const q = (s) => '"' + s.replace(/"/g, '""') + '"';
10
+ return `${q(table.schema)}.${q(table.name)}`;
11
+ }
12
+ export class OrpcTarget {
13
+ name = "orpc";
14
+ fileExtension = ".mjs";
15
+ capabilities = {
16
+ strictObjects: false,
17
+ brandedTypes: false,
18
+ dateInstances: true,
19
+ };
20
+ renderTable(table, _snapshot, opts) {
21
+ const base = baseNameFor(table);
22
+ const importPath = opts.schemasImportPath ?? "../zod";
23
+ const rowName = `${base}Row`;
24
+ const insertName = `${base}Insert`;
25
+ const ident = quotedIdent(table);
26
+ const singlePk = table.primaryKey.length === 1 ? table.primaryKey[0] : null;
27
+ const lines = [
28
+ `export function ${exportNameFor(table)}(sql) {`,
29
+ " const procedures = {",
30
+ " list: os.handler(async () => {",
31
+ ` const rows = await sql.unsafe('select * from ${ident} limit 100');`,
32
+ ` return rows.map((row) => ${rowName}.parse(row));`,
33
+ " }),",
34
+ ];
35
+ if (singlePk) {
36
+ const pkIdent = '"' + singlePk.replace(/"/g, '""') + '"';
37
+ lines.push(` byId: os.input(${rowName}.pick({ ${JSON.stringify(singlePk)}: true })).handler(async ({ input }) => {`, ` const rows = await sql.unsafe('select * from ${ident} where ${pkIdent} = $1 limit 1', [input[${JSON.stringify(singlePk)}]]);`, ` return rows.length > 0 ? ${rowName}.parse(rows[0]) : null;`, " }),");
38
+ }
39
+ if (table.kind === "table") {
40
+ lines.push(` create: os.input(${insertName}).handler(async ({ input }) => {`, " const keys = Object.keys(input);", ` const cols = keys.map((k) => '"' + k.replace(/"/g, '""') + '"').join(', ');`, " const params = keys.map((_, i) => '$' + (i + 1)).join(', ');", ` const rows = await sql.unsafe('insert into ${ident} (' + cols + ') values (' + params + ') returning *', keys.map((k) => input[k]));`, ` return ${rowName}.parse(rows[0]);`, " }),");
41
+ }
42
+ lines.push(" };", " return os.router(procedures);", "}");
43
+ const names = table.kind === "table" ? `${rowName}, ${insertName}` : rowName;
44
+ return {
45
+ imports: [
46
+ { from: "@orpc/server", names: ["os"] },
47
+ { from: `${importPath}/${table.name}.mjs`, names: [names] },
48
+ ],
49
+ body: lines.join("\n"),
50
+ exportName: exportNameFor(table),
51
+ };
52
+ }
53
+ renderTypes(table, _snapshot, _opts) {
54
+ const name = exportNameFor(table);
55
+ return [
56
+ "// Generated by supawatch. Do not edit.",
57
+ `export declare function ${name}(sql: { unsafe(text: string, params?: unknown[]): Promise<any[]> }): any;`,
58
+ "",
59
+ ].join("\n");
60
+ }
61
+ }
62
+ export default OrpcTarget;
package/package.json ADDED
@@ -0,0 +1,45 @@
1
+ {
2
+ "name": "@supawatch/target-orpc",
3
+ "version": "0.5.0",
4
+ "description": "oRPC routers generated from live Postgres by supawatch, input-validated by the generated Zod schemas.",
5
+ "keywords": [
6
+ "supabase",
7
+ "postgres",
8
+ "postgresql",
9
+ "codegen",
10
+ "schema",
11
+ "typescript",
12
+ "orpc",
13
+ "router",
14
+ "api"
15
+ ],
16
+ "license": "MIT",
17
+ "author": "Omar Dulaimi",
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "git+https://github.com/omar-dulaimi/supawatch.git",
21
+ "directory": "packages/target-orpc"
22
+ },
23
+ "type": "module",
24
+ "main": "./dist/index.js",
25
+ "types": "./dist/index.d.ts",
26
+ "exports": {
27
+ ".": {
28
+ "types": "./dist/index.d.ts",
29
+ "default": "./dist/index.js"
30
+ }
31
+ },
32
+ "files": [
33
+ "dist"
34
+ ],
35
+ "dependencies": {
36
+ "@supawatch/core": "0.5.0"
37
+ },
38
+ "devDependencies": {
39
+ "@types/node": "^22.20.1",
40
+ "typescript": "^5.9.0"
41
+ },
42
+ "scripts": {
43
+ "build": "tsc -p tsconfig.json"
44
+ }
45
+ }