Skip to content

Commit

Permalink
Merge pull request #549 from sirctseb/main
Browse files Browse the repository at this point in the history
Improve convenience types for clients to derive DBDataSource
  • Loading branch information
rintaun authored Feb 6, 2023
2 parents 042cbb3 + ff849b4 commit 16ec39e
Show file tree
Hide file tree
Showing 9 changed files with 360 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"typecheck": "tsc --noEmit --project .",
"lint": "eslint '**/*.{j,t}s{,on{,c,5}}'",
"lint:fix": "yarn -s lint --fix",
"test": "jest",
"test": "jest --runInBand",
"fullvalidate": "yarn install && yarn typecheck && yarn lint && yarn test",
"prepack": "yarn build"
},
Expand Down
43 changes: 43 additions & 0 deletions src/generator/Generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,16 @@ import { CaseFunction, Transformations } from './types'
import UtilityTypesBuilder from './builders/UtilityTypesBuilder'
import ZodSchemaBuilder from './builders/ZodSchemaBuilder'
import SingleNamedImportBuilder from './builders/SingleNamedImportBuilder'
import InfoBuilder from './builders/InfoBuilder'
import UpdateSchemaBuilder from './builders/UpdateSchemaBuilder'

export interface GeneratorOptions {
schema: SchemaInfo
genSelectSchemas?: boolean
genInsertSchemas?: boolean
genUpdateSchemas?: boolean
genTableMetadata?: boolean
genInfos?: boolean
disableEslint?: boolean
/** @deprecated */
genEnums?: boolean
Expand All @@ -57,7 +61,9 @@ export default class Generator {
public readonly generate: {
selectSchemas: boolean
insertSchemas: boolean
updateSchemas: boolean
tableMetadata: boolean
infos: boolean
disableEslint: boolean
/** @deprecated */
enums: boolean
Expand All @@ -77,7 +83,9 @@ export default class Generator {
schema,
genSelectSchemas: selectSchemas = true,
genInsertSchemas: insertSchemas = true,
genUpdateSchemas: updateSchemas = true,
genTableMetadata: tableMetadata = true,
genInfos: infos = true,
disableEslint = true,
genEnums = false,
genInsertTypes = false,
Expand All @@ -88,6 +96,17 @@ export default class Generator {
transformEnumMembers = 'pascal',
transformTypeNames = 'pascal',
}: GeneratorOptions) {
if (
infos &&
(!selectSchemas || !insertSchemas || !updateSchemas || !tableMetadata)
) {
const message =
'Cannot generate Info without insert, select, and update schemas \
and table metadata'
console.error(message)
throw new Error(message)
}

this.printer = createPrinter({
newLine: NewLineKind.LineFeed,
removeComments: false,
Expand All @@ -99,7 +118,9 @@ export default class Generator {
this.generate = Object.freeze({
selectSchemas,
insertSchemas,
updateSchemas,
tableMetadata,
infos,
disableEslint,
enums: genEnums,
insertTypes: genInsertTypes,
Expand Down Expand Up @@ -231,6 +252,28 @@ export default class Generator {
builders.push(builder)
}

if (this.generate.updateSchemas) {
const builder = new UpdateSchemaBuilder(
tableInfo,
this.types,
this.transform
)
builders.push(builder)
}

if (this.generate.infos) {
const builder = new InfoBuilder(
tableInfo,
this.types,
this.transform,
new InsertSchemaBuilder(tableInfo, this.types, this.transform),
new SelectSchemaBuilder(tableInfo, this.types, this.transform),
new UpdateSchemaBuilder(tableInfo, this.types, this.transform),
new TableMetadataBuilder(tableInfo, this.types, this.transform)
)
builders.push(builder)
}

if (this.generate.tables) {
const builder = new TableBuilder(tableInfo, this.types, this.transform)
this.types.add(builder.name, builder.typename().text, 'table')
Expand Down
12 changes: 12 additions & 0 deletions src/generator/__tests__/Generator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ describe(Generator, () => {
genTableMetadata: false,
genSelectSchemas: false,
genInsertSchemas: false,
genUpdateSchemas: false,
genInfos: false,
genEnums: false,
genInsertTypes: false,
genTables: false,
Expand All @@ -61,6 +63,8 @@ describe(Generator, () => {
genTableMetadata: false,
genSelectSchemas: false,
genInsertSchemas: false,
genUpdateSchemas: false,
genInfos: false,
genEnums: false,
genInsertTypes: false,
genTables: true,
Expand All @@ -80,6 +84,8 @@ describe(Generator, () => {
genTableMetadata: false,
genSelectSchemas: false,
genInsertSchemas: false,
genUpdateSchemas: false,
genInfos: false,
genEnums: true,
genInsertTypes: false,
genTables: false,
Expand All @@ -98,6 +104,8 @@ describe(Generator, () => {
genTableMetadata: false,
genSelectSchemas: false,
genInsertSchemas: false,
genUpdateSchemas: false,
genInfos: false,
genEnums: false,
genInsertTypes: true,
genTables: false,
Expand All @@ -117,6 +125,8 @@ describe(Generator, () => {
genTableMetadata: false,
genSelectSchemas: false,
genInsertSchemas: false,
genUpdateSchemas: false,
genInfos: false,
genEnums: false,
genInsertTypes: false,
genTables: false,
Expand All @@ -136,6 +146,8 @@ describe(Generator, () => {
genTableMetadata: false,
genSelectSchemas: false,
genInsertSchemas: false,
genUpdateSchemas: false,
genInfos: false,
genEnums: false,
genInsertTypes: false,
genTables: false,
Expand Down
130 changes: 130 additions & 0 deletions src/generator/__tests__/__snapshots__/Generator.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ export const DEFAULT = Symbol("DEFAULT");
export const SomehowDuplicatedTypeName$Metadata = {} as const;
export const SomehowDuplicatedTypeName$SelectSchema = z.object({});
export const SomehowDuplicatedTypeName$InsertSchema = z.object({});
export const SomehowDuplicatedTypeName$UpdateSchema = SomehowDuplicatedTypeName$InsertSchema.partial();
export const SomehowDuplicatedTypeNameInfo = {
metadata: SomehowDuplicatedTypeName$Metadata,
name: "somehow_duplicated_type_name",
schemas: {
insert: SomehowDuplicatedTypeName$InsertSchema,
select: SomehowDuplicatedTypeName$SelectSchema,
update: SomehowDuplicatedTypeName$UpdateSchema
}
};
export interface SomehowDuplicatedTypeName {
}
"
Expand Down Expand Up @@ -51,6 +61,16 @@ export const DEFAULT = Symbol("DEFAULT");
export const TableWithNoColumns$Metadata = {} as const;
export const TableWithNoColumns$SelectSchema = z.object({});
export const TableWithNoColumns$InsertSchema = z.object({});
export const TableWithNoColumns$UpdateSchema = TableWithNoColumns$InsertSchema.partial();
export const TableWithNoColumnsInfo = {
metadata: TableWithNoColumns$Metadata,
name: "table_with_no_columns",
schemas: {
insert: TableWithNoColumns$InsertSchema,
select: TableWithNoColumns$SelectSchema,
update: TableWithNoColumns$UpdateSchema
}
};
export const TableWithNumericId$Metadata = {
id: {
nativeName: "id",
Expand All @@ -63,6 +83,16 @@ export const TableWithNumericId$SelectSchema = z.object({
export const TableWithNumericId$InsertSchema = z.object({
id: z.number().optional().or(z.literal(DEFAULT))
});
export const TableWithNumericId$UpdateSchema = TableWithNumericId$InsertSchema.partial();
export const TableWithNumericIdInfo = {
metadata: TableWithNumericId$Metadata,
name: "table_with_numeric_id",
schemas: {
insert: TableWithNumericId$InsertSchema,
select: TableWithNumericId$SelectSchema,
update: TableWithNumericId$UpdateSchema
}
};
export const TableWithCustomTypes$Metadata = {
enum_type: {
nativeName: "enum_type",
Expand Down Expand Up @@ -93,6 +123,16 @@ export const TableWithCustomTypes$InsertSchema = z.object({
table_type: z.unknown(),
table_array_type: z.array(z.unknown())
});
export const TableWithCustomTypes$UpdateSchema = TableWithCustomTypes$InsertSchema.partial();
export const TableWithCustomTypesInfo = {
metadata: TableWithCustomTypes$Metadata,
name: "table_with_custom_types",
schemas: {
insert: TableWithCustomTypes$InsertSchema,
select: TableWithCustomTypes$SelectSchema,
update: TableWithCustomTypes$UpdateSchema
}
};
export const TableWithUuidId$Metadata = {
id: {
nativeName: "id",
Expand All @@ -105,6 +145,16 @@ export const TableWithUuidId$SelectSchema = z.object({
export const TableWithUuidId$InsertSchema = z.object({
id: z.string().optional().or(z.literal(DEFAULT))
});
export const TableWithUuidId$UpdateSchema = TableWithUuidId$InsertSchema.partial();
export const TableWithUuidIdInfo = {
metadata: TableWithUuidId$Metadata,
name: "table_with_uuid_id",
schemas: {
insert: TableWithUuidId$InsertSchema,
select: TableWithUuidId$SelectSchema,
update: TableWithUuidId$UpdateSchema
}
};
export const TableWithNullableFields$Metadata = {
nullable: {
nativeName: "nullable",
Expand Down Expand Up @@ -135,6 +185,16 @@ export const TableWithNullableFields$InsertSchema = z.object({
nullable_array: z.array(z.string()).nullable().optional(),
nullable_array_with_default: z.array(z.string()).nullable().optional().or(z.literal(DEFAULT))
});
export const TableWithNullableFields$UpdateSchema = TableWithNullableFields$InsertSchema.partial();
export const TableWithNullableFieldsInfo = {
metadata: TableWithNullableFields$Metadata,
name: "table_with_nullable_fields",
schemas: {
insert: TableWithNullableFields$InsertSchema,
select: TableWithNullableFields$SelectSchema,
update: TableWithNullableFields$UpdateSchema
}
};
export const TableWithJsonJsonb$Metadata = {
json: {
nativeName: "json",
Expand All @@ -153,6 +213,16 @@ export const TableWithJsonJsonb$InsertSchema = z.object({
json: z.unknown(),
jsonb: z.unknown().nullable().optional().or(z.literal(DEFAULT))
});
export const TableWithJsonJsonb$UpdateSchema = TableWithJsonJsonb$InsertSchema.partial();
export const TableWithJsonJsonbInfo = {
metadata: TableWithJsonJsonb$Metadata,
name: "table_with_json_jsonb",
schemas: {
insert: TableWithJsonJsonb$InsertSchema,
select: TableWithJsonJsonb$SelectSchema,
update: TableWithJsonJsonb$UpdateSchema
}
};
"
`;
Expand All @@ -171,6 +241,16 @@ export const DEFAULT = Symbol("DEFAULT");
export const TableWithNoColumns$Metadata = {} as const;
export const TableWithNoColumns$SelectSchema = z.object({});
export const TableWithNoColumns$InsertSchema = z.object({});
export const TableWithNoColumns$UpdateSchema = TableWithNoColumns$InsertSchema.partial();
export const TableWithNoColumnsInfo = {
metadata: TableWithNoColumns$Metadata,
name: "table_with_no_columns",
schemas: {
insert: TableWithNoColumns$InsertSchema,
select: TableWithNoColumns$SelectSchema,
update: TableWithNoColumns$UpdateSchema
}
};
export const TableWithNumericId$Metadata = {
id: {
nativeName: "id",
Expand All @@ -183,6 +263,16 @@ export const TableWithNumericId$SelectSchema = z.object({
export const TableWithNumericId$InsertSchema = z.object({
id: z.number().optional().or(z.literal(DEFAULT))
});
export const TableWithNumericId$UpdateSchema = TableWithNumericId$InsertSchema.partial();
export const TableWithNumericIdInfo = {
metadata: TableWithNumericId$Metadata,
name: "table_with_numeric_id",
schemas: {
insert: TableWithNumericId$InsertSchema,
select: TableWithNumericId$SelectSchema,
update: TableWithNumericId$UpdateSchema
}
};
export const TableWithCustomTypes$Metadata = {
enumType: {
nativeName: "enum_type",
Expand Down Expand Up @@ -213,6 +303,16 @@ export const TableWithCustomTypes$InsertSchema = z.object({
tableType: z.unknown(),
tableArrayType: z.array(z.unknown())
});
export const TableWithCustomTypes$UpdateSchema = TableWithCustomTypes$InsertSchema.partial();
export const TableWithCustomTypesInfo = {
metadata: TableWithCustomTypes$Metadata,
name: "table_with_custom_types",
schemas: {
insert: TableWithCustomTypes$InsertSchema,
select: TableWithCustomTypes$SelectSchema,
update: TableWithCustomTypes$UpdateSchema
}
};
export const TableWithUuidId$Metadata = {
id: {
nativeName: "id",
Expand All @@ -225,6 +325,16 @@ export const TableWithUuidId$SelectSchema = z.object({
export const TableWithUuidId$InsertSchema = z.object({
id: z.string().optional().or(z.literal(DEFAULT))
});
export const TableWithUuidId$UpdateSchema = TableWithUuidId$InsertSchema.partial();
export const TableWithUuidIdInfo = {
metadata: TableWithUuidId$Metadata,
name: "table_with_uuid_id",
schemas: {
insert: TableWithUuidId$InsertSchema,
select: TableWithUuidId$SelectSchema,
update: TableWithUuidId$UpdateSchema
}
};
export const TableWithNullableFields$Metadata = {
nullable: {
nativeName: "nullable",
Expand Down Expand Up @@ -255,6 +365,16 @@ export const TableWithNullableFields$InsertSchema = z.object({
nullableArray: z.array(z.string()).nullable().optional(),
nullableArrayWithDefault: z.array(z.string()).nullable().optional().or(z.literal(DEFAULT))
});
export const TableWithNullableFields$UpdateSchema = TableWithNullableFields$InsertSchema.partial();
export const TableWithNullableFieldsInfo = {
metadata: TableWithNullableFields$Metadata,
name: "table_with_nullable_fields",
schemas: {
insert: TableWithNullableFields$InsertSchema,
select: TableWithNullableFields$SelectSchema,
update: TableWithNullableFields$UpdateSchema
}
};
export const TableWithJsonJsonb$Metadata = {
json: {
nativeName: "json",
Expand All @@ -273,6 +393,16 @@ export const TableWithJsonJsonb$InsertSchema = z.object({
json: z.unknown(),
jsonb: z.unknown().nullable().optional().or(z.literal(DEFAULT))
});
export const TableWithJsonJsonb$UpdateSchema = TableWithJsonJsonb$InsertSchema.partial();
export const TableWithJsonJsonbInfo = {
metadata: TableWithJsonJsonb$Metadata,
name: "table_with_json_jsonb",
schemas: {
insert: TableWithJsonJsonb$InsertSchema,
select: TableWithJsonJsonb$SelectSchema,
update: TableWithJsonJsonb$UpdateSchema
}
};
"
`;
Expand Down
Loading

0 comments on commit 16ec39e

Please sign in to comment.