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
Merged

Add true programmatic schemas #36

merged 11 commits into from
Oct 15, 2022

Conversation

ridafkih
Copy link
Owner

As per #35, this re-implements the ability for a purely programmatic approach to schema generation, models and enums can be created independent of a parent schema while retaining full prior functionality.

An example has been added in examples/programmatic-usage, in which an API which can generate basic models is created.

The core logic for this endpoint is as follows.

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(),
  };
}

The README for said example is as follows.


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.

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.

{
	"modelName": "ExampleModel",
	"fields": [
		{
			"type": "int",
			"name": "number"
		},
		{
			"type": "string",
			"name": "name"
		}
	]
}
model ExampleModel {
  id     String @id @default(uuid())
  number Int
  name   String
}

@ridafkih
Copy link
Owner Author

ridafkih commented Oct 15, 2022

@efreila, is this what you had in mind? Let me know before EOD so I can make adjustments and deploy! :)

@ridafkih
Copy link
Owner Author

With this, proper documentation may be needed as the feature usage may require a better knowledge of the constructors and methods.

Copy link

@efreila efreila left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few small comments but looks great overall 🚀

examples/standard-usage/tsconfig.json Show resolved Hide resolved
examples/standard-usage/tsconfig.json Show resolved Hide resolved
examples/programmatic-usage/README.md Outdated Show resolved Hide resolved
lib/index.ts Outdated Show resolved Hide resolved
lib/index.ts Outdated Show resolved Hide resolved
@ridafkih ridafkih requested a review from efreila October 15, 2022 21:52
lib/index.ts Show resolved Hide resolved
@ridafkih ridafkih requested a review from efreila October 15, 2022 22:29
Copy link

@efreila efreila left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LG2M!

Thanks for working on this and addressing comments/feedback so hastily 🚀

@ridafkih
Copy link
Owner Author

@efreila Thank you for the valuable feedback!

@ridafkih ridafkih merged commit 16b6d2d into main Oct 15, 2022
@ridafkih
Copy link
Owner Author

I will mention you once this is deployed!

@ridafkih ridafkih deleted the add-true-programmatic-schemas branch October 16, 2022 00:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants