Skip to content

Commit

Permalink
always include generic argument for expand (#67)
Browse files Browse the repository at this point in the history
* always include generic argument for expand

* update snapshots
  • Loading branch information
patmood authored Jun 21, 2023
1 parent edb4d7d commit a74eac2
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 62 deletions.
55 changes: 26 additions & 29 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,33 +83,6 @@ var AUTH_SYSTEM_FIELDS_DEFINITION = `export type AuthSystemFields<T = never> = {
verified: boolean
} & BaseSystemFields<T>`;

// src/generics.ts
function fieldNameToGeneric(name) {
return `T${name}`;
}
function getGenericArgList(schema) {
const jsonFields = schema.filter((field) => field.type === "json").map((field) => fieldNameToGeneric(field.name)).sort();
return jsonFields;
}
function getGenericArgStringForRecord(schema) {
const argList = getGenericArgList(schema);
if (argList.length === 0)
return "";
return `<${argList.map((name) => `${name}`).join(", ")}>`;
}
function getGenericArgStringWithDefault(schema, opts) {
const argList = getGenericArgList(schema);
if (opts.includeExpand && canExpand(schema)) {
argList.push(fieldNameToGeneric(EXPAND_GENERIC_NAME));
}
if (argList.length === 0)
return "";
return `<${argList.map((name) => `${name} = unknown`).join(", ")}>`;
}
function canExpand(schema) {
return !!schema.find((field) => field.type === "relation");
}

// src/utils.ts
import { promises as fs2 } from "fs";
function toPascalCase(str) {
Expand Down Expand Up @@ -167,6 +140,30 @@ ${nameRecordMap}
}`;
}

// src/generics.ts
function fieldNameToGeneric(name) {
return `T${name}`;
}
function getGenericArgList(schema) {
const jsonFields = schema.filter((field) => field.type === "json").map((field) => fieldNameToGeneric(field.name)).sort();
return jsonFields;
}
function getGenericArgStringForRecord(schema) {
const argList = getGenericArgList(schema);
if (argList.length === 0)
return "";
return `<${argList.map((name) => `${name}`).join(", ")}>`;
}
function getGenericArgStringWithDefault(schema, opts) {
const argList = getGenericArgList(schema);
if (opts.includeExpand) {
argList.push(fieldNameToGeneric(EXPAND_GENERIC_NAME));
}
if (argList.length === 0)
return "";
return `<${argList.map((name) => `${name} = unknown`).join(", ")}>`;
}

// src/fields.ts
var pbSchemaTypescriptMap = {
bool: "boolean",
Expand Down Expand Up @@ -257,7 +254,7 @@ function createResponseType(collectionSchemaEntry) {
});
const genericArgsForRecord = getGenericArgStringForRecord(schema);
const systemFields = getSystemFields(type);
const expandArgString = canExpand(schema) ? `<T${EXPAND_GENERIC_NAME}>` : "";
const expandArgString = `<T${EXPAND_GENERIC_NAME}>`;
return `export type ${pascaleName}Response${genericArgsWithDefaults} = Required<${pascaleName}Record${genericArgsForRecord}> & ${systemFields}${expandArgString}`;
}

Expand All @@ -284,7 +281,7 @@ async function main(options2) {
import { program } from "commander";

// package.json
var version = "1.1.9";
var version = "1.1.10";

// src/index.ts
program.name("Pocketbase Typegen").version(version).description(
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pocketbase-typegen",
"version": "1.1.9",
"version": "1.1.10",
"description": "Generate pocketbase record types from your database",
"main": "dist/index.js",
"bin": {
Expand Down
7 changes: 1 addition & 6 deletions src/generics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,10 @@ export function getGenericArgStringWithDefault(
): string {
const argList = getGenericArgList(schema)

if (opts.includeExpand && canExpand(schema)) {
if (opts.includeExpand) {
argList.push(fieldNameToGeneric(EXPAND_GENERIC_NAME))
}

if (argList.length === 0) return ""
return `<${argList.map((name) => `${name} = unknown`).join(", ")}>`
}

// Does the collection have relation fields that can be expanded
export function canExpand(schema: FieldSchema[]) {
return !!schema.find((field) => field.type === "relation")
}
11 changes: 5 additions & 6 deletions src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,16 @@ import {
RESPONSE_TYPE_COMMENT,
} from "./constants"
import { CollectionRecord, FieldSchema } from "./types"
import {
canExpand,
getGenericArgStringForRecord,
getGenericArgStringWithDefault,
} from "./generics"
import {
createCollectionEnum,
createCollectionRecords,
createCollectionResponses,
} from "./collections"
import { createSelectOptions, createTypeField } from "./fields"
import {
getGenericArgStringForRecord,
getGenericArgStringWithDefault,
} from "./generics"
import { getSystemFields, toPascalCase } from "./utils"

export function generate(results: Array<CollectionRecord>): string {
Expand Down Expand Up @@ -87,7 +86,7 @@ export function createResponseType(
})
const genericArgsForRecord = getGenericArgStringForRecord(schema)
const systemFields = getSystemFields(type)
const expandArgString = canExpand(schema) ? `<T${EXPAND_GENERIC_NAME}>` : ""
const expandArgString = `<T${EXPAND_GENERIC_NAME}>`

return `export type ${pascaleName}Response${genericArgsWithDefaults} = Required<${pascaleName}Record${genericArgsForRecord}> & ${systemFields}${expandArgString}`
}
8 changes: 4 additions & 4 deletions test/__snapshots__/fromJSON.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,12 @@ export type UsersRecord = {
}
// Response types include system fields and match responses from the PocketBase API
export type BaseResponse = Required<BaseRecord> & BaseSystemFields
export type CustomAuthResponse = Required<CustomAuthRecord> & AuthSystemFields
export type BaseResponse<Texpand = unknown> = Required<BaseRecord> & BaseSystemFields<Texpand>
export type CustomAuthResponse<Texpand = unknown> = Required<CustomAuthRecord> & AuthSystemFields<Texpand>
export type EverythingResponse<Tanother_json_field = unknown, Tjson_field = unknown, Texpand = unknown> = Required<EverythingRecord<Tanother_json_field, Tjson_field>> & BaseSystemFields<Texpand>
export type MyViewResponse<Tjson_field = unknown, Texpand = unknown> = Required<MyViewRecord<Tjson_field>> & BaseSystemFields<Texpand>
export type PostsResponse = Required<PostsRecord> & BaseSystemFields
export type UsersResponse = Required<UsersRecord> & AuthSystemFields
export type PostsResponse<Texpand = unknown> = Required<PostsRecord> & BaseSystemFields<Texpand>
export type UsersResponse<Texpand = unknown> = Required<UsersRecord> & AuthSystemFields<Texpand>
// Types containing all Records and Responses, useful for creating typing helper functions
Expand Down
4 changes: 2 additions & 2 deletions test/__snapshots__/lib.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ exports[`createRecordType handles file fields with multiple files 1`] = `
}"
`;

exports[`createResponseType creates type definition for a response 1`] = `"export type BooksResponse = Required<BooksRecord> & BaseSystemFields"`;
exports[`createResponseType creates type definition for a response 1`] = `"export type BooksResponse<Texpand = unknown> = Required<BooksRecord> & BaseSystemFields<Texpand>"`;
exports[`createResponseType handles file fields with multiple files 1`] = `
"export type BooksRecord = {
Expand Down Expand Up @@ -58,7 +58,7 @@ export type BooksRecord = {
}
// Response types include system fields and match responses from the PocketBase API
export type BooksResponse = Required<BooksRecord> & BaseSystemFields
export type BooksResponse<Texpand = unknown> = Required<BooksRecord> & BaseSystemFields<Texpand>
// Types containing all Records and Responses, useful for creating typing helper functions
Expand Down
10 changes: 0 additions & 10 deletions test/generics.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
canExpand,
getGenericArgList,
getGenericArgStringForRecord,
getGenericArgStringWithDefault,
Expand Down Expand Up @@ -127,12 +126,3 @@ describe("getGenericArgStringForRecord", () => {
).toEqual("<Tdata1, Tdata2>")
})
})

describe("canExpand", () => {
it("detects collections that can be expanded", () => {
expect(canExpand([textField, jsonField1, expandField])).toEqual(true)
})
it("detects collections that cannot be expanded", () => {
expect(canExpand([textField, jsonField1])).toEqual(false)
})
})
8 changes: 4 additions & 4 deletions test/pocketbase-types-example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,12 @@ export type UsersRecord = {
}

// Response types include system fields and match responses from the PocketBase API
export type BaseResponse = Required<BaseRecord> & BaseSystemFields
export type CustomAuthResponse = Required<CustomAuthRecord> & AuthSystemFields
export type BaseResponse<Texpand = unknown> = Required<BaseRecord> & BaseSystemFields<Texpand>
export type CustomAuthResponse<Texpand = unknown> = Required<CustomAuthRecord> & AuthSystemFields<Texpand>
export type EverythingResponse<Tanother_json_field = unknown, Tjson_field = unknown, Texpand = unknown> = Required<EverythingRecord<Tanother_json_field, Tjson_field>> & BaseSystemFields<Texpand>
export type MyViewResponse<Tjson_field = unknown, Texpand = unknown> = Required<MyViewRecord<Tjson_field>> & BaseSystemFields<Texpand>
export type PostsResponse = Required<PostsRecord> & BaseSystemFields
export type UsersResponse = Required<UsersRecord> & AuthSystemFields
export type PostsResponse<Texpand = unknown> = Required<PostsRecord> & BaseSystemFields<Texpand>
export type UsersResponse<Texpand = unknown> = Required<UsersRecord> & AuthSystemFields<Texpand>

// Types containing all Records and Responses, useful for creating typing helper functions

Expand Down

0 comments on commit a74eac2

Please sign in to comment.