npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

atlas2ts

v0.0.10

Published

Autogenerate typescript interfaces & Zod typespecs from Atlas DDL spec

Downloads

15

Readme

Atlas2TS

Generates typescript interfaces or zod typespecs from atlas DDL specification.

Usage

atlas2ts: Generate typescript interfaces and zod typespecs from atlas HCL files

Parameters:
-h, --help Print help message
-i, --input Path of one or more input files to process
-o, --output Path of output file for generated types (defaults to db-types.ts)
-g, --generator Generator to be used, can be ts (default) or zod
-t, --template name of liquid template file (resolved relative to template root) to feed the generated content into
--template-root Root relative to which templates will be resolved
--naming-strategy Strategy used to derive field & type names from column names and tables. Can be unmodified or camel-case (default)
--type-names Name & type mapping (Can be repeated) eg. -n users.name:Person.handle
--field-names Field name mapping
--field-types Field type mapping
--verbose Enable verbose output (Useful for bug reports)

Example

  1. Given an input hcl file, generate typescript interfaces:
atlas2ts -i fixtures/dvdrental.hcl -o fixtures/dvdrental.ts
  1. Given an input hcl file, generate zod typespecs:
atlas2ts -i fixtures/dvdrental.hcl -o fixtures/dvdrental.zod.ts -g zod

Customizing the generated code:

Casing of generated type/field names:

By default atlas2ts will camel-case the names of types and fields as per common convention in js ecosystem. You can also pass --naming-strategy=unmodified to prevent this conversion. The type names will always have first character capitalized.

In addition, atlas2ts enables you to selectively override the names of types or fields in generated code.

Customizing the type names

atlas2ts -i fixtures/dvdrental.hcl -o fixtures/dvdrental.ts --type-names user:Person

In above example the interface generated for user table will be Person (rather than User).

Customizing field names

Similarly, we can configure the name of fields for specific columns:

atlas2ts -i fixtures/dvdrental.hcl -o fixtures/dvdrental.ts --field-names user.name:handle

Here the field for name column of user table will be named as handle (rather than name).

Please note that atlas2ts does not know how you will use these mapped fields in your code. It is your responsibility to ensure that at runtime the generated SQL uses the correct table/column names.

Customizing types

Atlas2TS usually makes a sane guess around what db types should be mapped to what TS types.

However, it may need help when custom types or json fields are used.

For these scenarios, you can specify the types to be used in generated through type-mapping or field-types argument.

--type-mapping allows you to specify what type should be used for a specific db level type.

atlas2ts -i fixtures/dvdrental.hcl -o fixtures/dvdrental.ts --type-mapping decimal:string

--field-types allows you to specify what type should be used for a specific field:

atlas2ts -i fixtures/dvdrental.hcl -o fixtures/dvdrental.ts --field-types User.email:string

Note that the key here is typename.fieldname combination which can be different from tablename.columnname:

atlas2ts -i fixtures/dvdrental.hcl -o fixtures/dvdrental.ts --field-names user.name:handle --field-types User.handle:string

The typename in the key is optional, so we can use just the fieldname to override the type of a field in all tables:

atlas2ts -i fixtures/dvdrental.hcl -o fixtures/dvdrental.ts --field-types createdAt:Date

If these types need to be imported from elsewhere you can also define a liquid template containing relevant imports into which the output will be injected.

atlas2ts -i fixtures/dvdrental.hcl -o fixtures/dvdrental.ts --field-names user.name:handle --field-types User.handle:Str --template-root fixtures/templates --template base.ts.liquid

--template-root should point to a directory where template(s) will be found, and template is the specific template to be used. If this template uses Partials or layouts those files will also be found from the template-root.

This template must contain a {{output}} block where the output will be injected.

For multiple mappings, --type-names, --field-names or --field-types parameter can be passed multiple times.

License

MIT