Skip to content

Commit a74eac2

Browse files
authored
always include generic argument for expand (#67)
* always include generic argument for expand * update snapshots
1 parent edb4d7d commit a74eac2

8 files changed

+43
-62
lines changed

dist/index.js

+26-29
Original file line numberDiff line numberDiff line change
@@ -83,33 +83,6 @@ var AUTH_SYSTEM_FIELDS_DEFINITION = `export type AuthSystemFields<T = never> = {
8383
verified: boolean
8484
} & BaseSystemFields<T>`;
8585

86-
// src/generics.ts
87-
function fieldNameToGeneric(name) {
88-
return `T${name}`;
89-
}
90-
function getGenericArgList(schema) {
91-
const jsonFields = schema.filter((field) => field.type === "json").map((field) => fieldNameToGeneric(field.name)).sort();
92-
return jsonFields;
93-
}
94-
function getGenericArgStringForRecord(schema) {
95-
const argList = getGenericArgList(schema);
96-
if (argList.length === 0)
97-
return "";
98-
return `<${argList.map((name) => `${name}`).join(", ")}>`;
99-
}
100-
function getGenericArgStringWithDefault(schema, opts) {
101-
const argList = getGenericArgList(schema);
102-
if (opts.includeExpand && canExpand(schema)) {
103-
argList.push(fieldNameToGeneric(EXPAND_GENERIC_NAME));
104-
}
105-
if (argList.length === 0)
106-
return "";
107-
return `<${argList.map((name) => `${name} = unknown`).join(", ")}>`;
108-
}
109-
function canExpand(schema) {
110-
return !!schema.find((field) => field.type === "relation");
111-
}
112-
11386
// src/utils.ts
11487
import { promises as fs2 } from "fs";
11588
function toPascalCase(str) {
@@ -167,6 +140,30 @@ ${nameRecordMap}
167140
}`;
168141
}
169142

143+
// src/generics.ts
144+
function fieldNameToGeneric(name) {
145+
return `T${name}`;
146+
}
147+
function getGenericArgList(schema) {
148+
const jsonFields = schema.filter((field) => field.type === "json").map((field) => fieldNameToGeneric(field.name)).sort();
149+
return jsonFields;
150+
}
151+
function getGenericArgStringForRecord(schema) {
152+
const argList = getGenericArgList(schema);
153+
if (argList.length === 0)
154+
return "";
155+
return `<${argList.map((name) => `${name}`).join(", ")}>`;
156+
}
157+
function getGenericArgStringWithDefault(schema, opts) {
158+
const argList = getGenericArgList(schema);
159+
if (opts.includeExpand) {
160+
argList.push(fieldNameToGeneric(EXPAND_GENERIC_NAME));
161+
}
162+
if (argList.length === 0)
163+
return "";
164+
return `<${argList.map((name) => `${name} = unknown`).join(", ")}>`;
165+
}
166+
170167
// src/fields.ts
171168
var pbSchemaTypescriptMap = {
172169
bool: "boolean",
@@ -257,7 +254,7 @@ function createResponseType(collectionSchemaEntry) {
257254
});
258255
const genericArgsForRecord = getGenericArgStringForRecord(schema);
259256
const systemFields = getSystemFields(type);
260-
const expandArgString = canExpand(schema) ? `<T${EXPAND_GENERIC_NAME}>` : "";
257+
const expandArgString = `<T${EXPAND_GENERIC_NAME}>`;
261258
return `export type ${pascaleName}Response${genericArgsWithDefaults} = Required<${pascaleName}Record${genericArgsForRecord}> & ${systemFields}${expandArgString}`;
262259
}
263260

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

286283
// package.json
287-
var version = "1.1.9";
284+
var version = "1.1.10";
288285

289286
// src/index.ts
290287
program.name("Pocketbase Typegen").version(version).description(

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "pocketbase-typegen",
3-
"version": "1.1.9",
3+
"version": "1.1.10",
44
"description": "Generate pocketbase record types from your database",
55
"main": "dist/index.js",
66
"bin": {

src/generics.ts

+1-6
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,10 @@ export function getGenericArgStringWithDefault(
2525
): string {
2626
const argList = getGenericArgList(schema)
2727

28-
if (opts.includeExpand && canExpand(schema)) {
28+
if (opts.includeExpand) {
2929
argList.push(fieldNameToGeneric(EXPAND_GENERIC_NAME))
3030
}
3131

3232
if (argList.length === 0) return ""
3333
return `<${argList.map((name) => `${name} = unknown`).join(", ")}>`
3434
}
35-
36-
// Does the collection have relation fields that can be expanded
37-
export function canExpand(schema: FieldSchema[]) {
38-
return !!schema.find((field) => field.type === "relation")
39-
}

src/lib.ts

+5-6
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,16 @@ import {
99
RESPONSE_TYPE_COMMENT,
1010
} from "./constants"
1111
import { CollectionRecord, FieldSchema } from "./types"
12-
import {
13-
canExpand,
14-
getGenericArgStringForRecord,
15-
getGenericArgStringWithDefault,
16-
} from "./generics"
1712
import {
1813
createCollectionEnum,
1914
createCollectionRecords,
2015
createCollectionResponses,
2116
} from "./collections"
2217
import { createSelectOptions, createTypeField } from "./fields"
18+
import {
19+
getGenericArgStringForRecord,
20+
getGenericArgStringWithDefault,
21+
} from "./generics"
2322
import { getSystemFields, toPascalCase } from "./utils"
2423

2524
export function generate(results: Array<CollectionRecord>): string {
@@ -87,7 +86,7 @@ export function createResponseType(
8786
})
8887
const genericArgsForRecord = getGenericArgStringForRecord(schema)
8988
const systemFields = getSystemFields(type)
90-
const expandArgString = canExpand(schema) ? `<T${EXPAND_GENERIC_NAME}>` : ""
89+
const expandArgString = `<T${EXPAND_GENERIC_NAME}>`
9190

9291
return `export type ${pascaleName}Response${genericArgsWithDefaults} = Required<${pascaleName}Record${genericArgsForRecord}> & ${systemFields}${expandArgString}`
9392
}

test/__snapshots__/fromJSON.test.ts.snap

+4-4
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,12 @@ export type UsersRecord = {
9292
}
9393
9494
// Response types include system fields and match responses from the PocketBase API
95-
export type BaseResponse = Required<BaseRecord> & BaseSystemFields
96-
export type CustomAuthResponse = Required<CustomAuthRecord> & AuthSystemFields
95+
export type BaseResponse<Texpand = unknown> = Required<BaseRecord> & BaseSystemFields<Texpand>
96+
export type CustomAuthResponse<Texpand = unknown> = Required<CustomAuthRecord> & AuthSystemFields<Texpand>
9797
export type EverythingResponse<Tanother_json_field = unknown, Tjson_field = unknown, Texpand = unknown> = Required<EverythingRecord<Tanother_json_field, Tjson_field>> & BaseSystemFields<Texpand>
9898
export type MyViewResponse<Tjson_field = unknown, Texpand = unknown> = Required<MyViewRecord<Tjson_field>> & BaseSystemFields<Texpand>
99-
export type PostsResponse = Required<PostsRecord> & BaseSystemFields
100-
export type UsersResponse = Required<UsersRecord> & AuthSystemFields
99+
export type PostsResponse<Texpand = unknown> = Required<PostsRecord> & BaseSystemFields<Texpand>
100+
export type UsersResponse<Texpand = unknown> = Required<UsersRecord> & AuthSystemFields<Texpand>
101101
102102
// Types containing all Records and Responses, useful for creating typing helper functions
103103

test/__snapshots__/lib.test.ts.snap

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ exports[`createRecordType handles file fields with multiple files 1`] = `
1212
}"
1313
`;
1414

15-
exports[`createResponseType creates type definition for a response 1`] = `"export type BooksResponse = Required<BooksRecord> & BaseSystemFields"`;
15+
exports[`createResponseType creates type definition for a response 1`] = `"export type BooksResponse<Texpand = unknown> = Required<BooksRecord> & BaseSystemFields<Texpand>"`;
1616
1717
exports[`createResponseType handles file fields with multiple files 1`] = `
1818
"export type BooksRecord = {
@@ -58,7 +58,7 @@ export type BooksRecord = {
5858
}
5959
6060
// Response types include system fields and match responses from the PocketBase API
61-
export type BooksResponse = Required<BooksRecord> & BaseSystemFields
61+
export type BooksResponse<Texpand = unknown> = Required<BooksRecord> & BaseSystemFields<Texpand>
6262
6363
// Types containing all Records and Responses, useful for creating typing helper functions
6464

test/generics.test.ts

-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import {
2-
canExpand,
32
getGenericArgList,
43
getGenericArgStringForRecord,
54
getGenericArgStringWithDefault,
@@ -127,12 +126,3 @@ describe("getGenericArgStringForRecord", () => {
127126
).toEqual("<Tdata1, Tdata2>")
128127
})
129128
})
130-
131-
describe("canExpand", () => {
132-
it("detects collections that can be expanded", () => {
133-
expect(canExpand([textField, jsonField1, expandField])).toEqual(true)
134-
})
135-
it("detects collections that cannot be expanded", () => {
136-
expect(canExpand([textField, jsonField1])).toEqual(false)
137-
})
138-
})

test/pocketbase-types-example.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,12 @@ export type UsersRecord = {
8989
}
9090

9191
// Response types include system fields and match responses from the PocketBase API
92-
export type BaseResponse = Required<BaseRecord> & BaseSystemFields
93-
export type CustomAuthResponse = Required<CustomAuthRecord> & AuthSystemFields
92+
export type BaseResponse<Texpand = unknown> = Required<BaseRecord> & BaseSystemFields<Texpand>
93+
export type CustomAuthResponse<Texpand = unknown> = Required<CustomAuthRecord> & AuthSystemFields<Texpand>
9494
export type EverythingResponse<Tanother_json_field = unknown, Tjson_field = unknown, Texpand = unknown> = Required<EverythingRecord<Tanother_json_field, Tjson_field>> & BaseSystemFields<Texpand>
9595
export type MyViewResponse<Tjson_field = unknown, Texpand = unknown> = Required<MyViewRecord<Tjson_field>> & BaseSystemFields<Texpand>
96-
export type PostsResponse = Required<PostsRecord> & BaseSystemFields
97-
export type UsersResponse = Required<UsersRecord> & AuthSystemFields
96+
export type PostsResponse<Texpand = unknown> = Required<PostsRecord> & BaseSystemFields<Texpand>
97+
export type UsersResponse<Texpand = unknown> = Required<UsersRecord> & AuthSystemFields<Texpand>
9898

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

0 commit comments

Comments
 (0)