Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add true programmatic schemas #36

Merged
merged 11 commits into from
Oct 15, 2022
2 changes: 1 addition & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
dist
node_modules
example
examples/
babel.config.cjs
44 changes: 44 additions & 0 deletions examples/programmatic-usage/README.md
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.
11 changes: 11 additions & 0 deletions examples/programmatic-usage/index.ts
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")
})
16 changes: 16 additions & 0 deletions examples/programmatic-usage/package.json
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"
}
}
34 changes: 34 additions & 0 deletions examples/programmatic-usage/routes/model.ts
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.
Loading