Skip to content

Commit

Permalink
Merge pull request #36 from ridafkih/add-true-programmatic-schemas
Browse files Browse the repository at this point in the history
Add true programmatic schemas
  • Loading branch information
ridafkih authored Oct 15, 2022
2 parents 6ce2022 + 2525b25 commit 16b6d2d
Show file tree
Hide file tree
Showing 24 changed files with 907 additions and 30 deletions.
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

0 comments on commit 16b6d2d

Please sign in to comment.