Skip to content

Commit

Permalink
Add support for restricting exported types further through use of Wra…
Browse files Browse the repository at this point in the history
…pper types
  • Loading branch information
lorefnon committed Jul 3, 2023
1 parent 08e6af2 commit 9186750
Show file tree
Hide file tree
Showing 12 changed files with 415 additions and 166 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ const options = {
tables: {
// Include only whitelisted tables
include: [/authors/, "books"],
// Similar exclude can be used to blacklist
// Similarly exclude can be used to blacklist
},
fieldMappings: [
{
Expand Down
4 changes: 2 additions & 2 deletions src/field-mappings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const ImportedItemSchema = z.object({
/**
* Whether this is a relative import
*
* @default true
* @default true
*/
isRelative: z.boolean().nullish()
});
Expand All @@ -37,7 +37,7 @@ export const ImportedItemSchema = z.object({
* resolved relative to the cwd from where generator is invoked and
* then converted to a relative path relative to the generated file
*
* Examples:
* Examples:
* When generated file is located at src/db/tables/some-table.ts and generator
* is run from project root
*
Expand Down
17 changes: 16 additions & 1 deletion src/generator-options.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as z from "zod";
import { FieldMappingSchema, StrOrRegExpSchema } from "./field-mappings";
import { FieldMappingSchema, ImportedItemSchema, StrOrRegExpSchema } from "./field-mappings";

export const TableInclusionSchema = z.object({
/**
Expand Down Expand Up @@ -292,6 +292,11 @@ export const RawContentSchema = z.object({

export interface RawContent extends z.TypeOf<typeof RawContentSchema> {}

export const TypeWrapperSchema = z.object({
typeName: StrOrRegExpSchema,
wrapper: ImportedItemSchema
});

export const GeneratorOptsSchema = z.object({
/** Simulate the generation and print the outcome without actually modifying any files */
dryRun: z.boolean().nullish(),
Expand Down Expand Up @@ -386,6 +391,16 @@ export const GeneratorOptsSchema = z.object({
* @see RawContent
*/
rawContent: RawContentSchema.nullish(),

/**
* Wrap inferred types before exporting - this is useful to restrict
* the types used for insert/update etc. beyond what the database permits.
*
* Eg. We can hint that updatedAt must be set whenever record is updated
*
* @see TypeWapper
*/
typeWrappers: TypeWrapperSchema.array().nullish(),
});

/**
Expand Down
Loading

0 comments on commit 9186750

Please sign in to comment.