-
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from ridafkih/add-true-programmatic-schemas
Add true programmatic schemas
- Loading branch information
Showing
24 changed files
with
907 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
dist | ||
node_modules | ||
example | ||
examples/ | ||
babel.config.cjs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Schemix Example | ||
|
||
Welcome to Schemix. | ||
|
||
This example creates an API at 0.0.0.0:8000 with a route in which you can send GET requests to a `/model` endpoint. | ||
It accepts the following `zod` schema. | ||
|
||
```ts | ||
validation.object({ | ||
modelName: validation.string(), | ||
fields: validation.array(validation.object({ | ||
type: validation.enum(["string", "int"]), | ||
name: validation.string(), | ||
})), | ||
}); | ||
``` | ||
|
||
When a request is sent with the following body to `0.0.0.0:8000/model`, it will respond with the subsequent string. | ||
|
||
```json | ||
{ | ||
"modelName": "ExampleModel", | ||
"fields": [ | ||
{ | ||
"type": "int", | ||
"name": "number" | ||
}, | ||
{ | ||
"type": "string", | ||
"name": "name" | ||
} | ||
] | ||
} | ||
``` | ||
|
||
``` | ||
model ExampleModel { | ||
id String @id @default(uuid()) | ||
number Int | ||
name String | ||
} | ||
``` | ||
|
||
This example simply serves to prove that a purely programmatic approach to schema generation can be taken as of the release dated 15-10-2022. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { router } from "18h"; | ||
import path from "path"; | ||
|
||
/** | ||
* This creates a router at 0.0.0.0:8000 and installs | ||
* all the routes at the `routes/` sub-folder. | ||
*/ | ||
router({ | ||
port: 8000, | ||
routesFolder: path.join(__dirname, "routes") | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"name": "schemix-example", | ||
"version": "0.0.1", | ||
"description": "", | ||
"scripts": { | ||
"start": "ts-node ./" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"18h": "3.0.16", | ||
"schemix": "^1.1.4", | ||
"ts-node": "^10.7.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { route, validation } from "18h"; | ||
import { PrismaModel } from "../../../dist/modules/PrismaModel"; | ||
|
||
export default route({ | ||
get: { | ||
schema: { | ||
request: validation.object({ | ||
modelName: validation.string(), | ||
fields: validation.array( | ||
validation.object({ | ||
type: validation.enum(["string", "int"]), | ||
name: validation.string(), | ||
}) | ||
), | ||
}), | ||
response: validation.string(), | ||
}, | ||
accepts: ["json"], | ||
async handler(context) { | ||
const { body } = context.request; | ||
const model = new PrismaModel(body.modelName); | ||
model.string("id", { id: true, default: { uuid: true } }); | ||
|
||
for (const { type, name } of body.fields) { | ||
model[type](name); | ||
} | ||
|
||
return { | ||
status: 200, | ||
body: model.toString(), | ||
}; | ||
}, | ||
}, | ||
}); |
File renamed without changes.
Oops, something went wrong.