Skip to content

Commit 45a1c1f

Browse files
authored
fix system fields for view collection types (#53)
1 parent b0f1208 commit 45a1c1f

10 files changed

+9
-49
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ Add it to your projects `package.json`:
5252

5353
The output is a typescript file `pocketbase-types.ts` ([example](./test/pocketbase-types-example.ts)) which will contain:
5454

55-
- `Collections` An enum of all collections/
56-
- `[CollectionName]Record` One type for each collection (eg ProfilesRecord)/
55+
- `Collections` An enum of all collections.
56+
- `[CollectionName]Record` One type for each collection (eg ProfilesRecord).
5757
- `[CollectionName]Response` One response type for each collection (eg ProfilesResponse) which includes system fields. This is what is returned from the PocketBase API.
5858
- `[CollectionName][FieldName]Options` If the collection contains a select field with set values, an enum of the options will be generated.
5959
- `CollectionRecords` A type mapping each collection name to the record type.

dist/index.js

+1-10
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,6 @@ var AUTH_SYSTEM_FIELDS_DEFINITION = `export type AuthSystemFields<T = never> = {
6767
username: string
6868
verified: boolean
6969
} & BaseSystemFields<T>`;
70-
var VIEW_SYSTEM_FIELDS_DEFINITION = `export type ViewSystemFields<T = never> = {
71-
id: ${RECORD_ID_STRING_NAME}
72-
collectionId: string
73-
collectionName: Collections
74-
expand?: T
75-
}`;
7670

7771
// src/generics.ts
7872
function fieldNameToGeneric(name) {
@@ -123,8 +117,6 @@ function getSystemFields(type) {
123117
switch (type) {
124118
case "auth":
125119
return "AuthSystemFields";
126-
case "view":
127-
return "ViewSystemFields";
128120
default:
129121
return "BaseSystemFields";
130122
}
@@ -222,7 +214,6 @@ function generate(results) {
222214
ALIAS_TYPE_DEFINITIONS,
223215
BASE_SYSTEM_FIELDS_DEFINITION,
224216
AUTH_SYSTEM_FIELDS_DEFINITION,
225-
VIEW_SYSTEM_FIELDS_DEFINITION,
226217
RECORD_TYPE_COMMENT,
227218
...recordTypes,
228219
responseTypes.join("\n"),
@@ -278,7 +269,7 @@ async function main(options2) {
278269
import { program } from "commander";
279270

280271
// package.json
281-
var version = "1.1.6";
272+
var version = "1.1.7";
282273

283274
// src/index.ts
284275
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.6",
3+
"version": "1.1.7",
44
"description": "Generate pocketbase record types from your database",
55
"main": "dist/index.js",
66
"bin": {

src/constants.ts

-7
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,3 @@ export const AUTH_SYSTEM_FIELDS_DEFINITION = `export type AuthSystemFields<T = n
2929
\tusername: string
3030
\tverified: boolean
3131
} & BaseSystemFields<T>`
32-
33-
export const VIEW_SYSTEM_FIELDS_DEFINITION = `export type ViewSystemFields<T = never> = {
34-
\tid: ${RECORD_ID_STRING_NAME}
35-
\tcollectionId: string
36-
\tcollectionName: Collections
37-
\texpand?: T
38-
}`

src/lib.ts

-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {
77
EXPORT_COMMENT,
88
RECORD_TYPE_COMMENT,
99
RESPONSE_TYPE_COMMENT,
10-
VIEW_SYSTEM_FIELDS_DEFINITION,
1110
} from "./constants"
1211
import { CollectionRecord, FieldSchema } from "./types"
1312
import {
@@ -45,7 +44,6 @@ export function generate(results: Array<CollectionRecord>): string {
4544
ALIAS_TYPE_DEFINITIONS,
4645
BASE_SYSTEM_FIELDS_DEFINITION,
4746
AUTH_SYSTEM_FIELDS_DEFINITION,
48-
VIEW_SYSTEM_FIELDS_DEFINITION,
4947
RECORD_TYPE_COMMENT,
5048
...recordTypes,
5149
responseTypes.join("\n"),

src/utils.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,8 @@ export function getSystemFields(type: CollectionRecord["type"]) {
2929
switch (type) {
3030
case "auth":
3131
return "AuthSystemFields"
32-
case "view":
33-
return "ViewSystemFields"
3432
default:
33+
// `view` and `base` collection types share the same system fields (for now)
3534
return "BaseSystemFields"
3635
}
3736
}

test/__snapshots__/fromJSON.test.ts.snap

+1-8
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,6 @@ export type AuthSystemFields<T = never> = {
3636
verified: boolean
3737
} & BaseSystemFields<T>
3838
39-
export type ViewSystemFields<T = never> = {
40-
id: RecordIdString
41-
collectionId: string
42-
collectionName: Collections
43-
expand?: T
44-
}
45-
4639
// Record types for each collection
4740
4841
export type BaseRecord = {
@@ -102,7 +95,7 @@ export type UsersRecord = {
10295
export type BaseResponse = BaseRecord & BaseSystemFields
10396
export type CustomAuthResponse = CustomAuthRecord & AuthSystemFields
10497
export type EverythingResponse<Tanother_json_field = unknown, Tjson_field = unknown, Texpand = unknown> = EverythingRecord<Tanother_json_field, Tjson_field> & BaseSystemFields<Texpand>
105-
export type MyViewResponse<Tjson_field = unknown, Texpand = unknown> = MyViewRecord<Tjson_field> & ViewSystemFields<Texpand>
98+
export type MyViewResponse<Tjson_field = unknown, Texpand = unknown> = MyViewRecord<Tjson_field> & BaseSystemFields<Texpand>
10699
export type PostsResponse = PostsRecord & BaseSystemFields
107100
export type UsersResponse = UsersRecord & AuthSystemFields
108101

test/__snapshots__/lib.test.ts.snap

-7
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,6 @@ export type AuthSystemFields<T = never> = {
5151
verified: boolean
5252
} & BaseSystemFields<T>
5353
54-
export type ViewSystemFields<T = never> = {
55-
id: RecordIdString
56-
collectionId: string
57-
collectionName: Collections
58-
expand?: T
59-
}
60-
6154
// Record types for each collection
6255
6356
export type BooksRecord = {

test/pocketbase-types-example.ts

+1-8
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,6 @@ export type AuthSystemFields<T = never> = {
3333
verified: boolean
3434
} & BaseSystemFields<T>
3535

36-
export type ViewSystemFields<T = never> = {
37-
id: RecordIdString
38-
collectionId: string
39-
collectionName: Collections
40-
expand?: T
41-
}
42-
4336
// Record types for each collection
4437

4538
export type BaseRecord = {
@@ -99,7 +92,7 @@ export type UsersRecord = {
9992
export type BaseResponse = BaseRecord & BaseSystemFields
10093
export type CustomAuthResponse = CustomAuthRecord & AuthSystemFields
10194
export type EverythingResponse<Tanother_json_field = unknown, Tjson_field = unknown, Texpand = unknown> = EverythingRecord<Tanother_json_field, Tjson_field> & BaseSystemFields<Texpand>
102-
export type MyViewResponse<Tjson_field = unknown, Texpand = unknown> = MyViewRecord<Tjson_field> & ViewSystemFields<Texpand>
95+
export type MyViewResponse<Tjson_field = unknown, Texpand = unknown> = MyViewRecord<Tjson_field> & BaseSystemFields<Texpand>
10396
export type PostsResponse = PostsRecord & BaseSystemFields
10497
export type UsersResponse = UsersRecord & AuthSystemFields
10598

test/utils.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ describe("sanitizeFieldName", () => {
3333

3434
describe("getSystemFields", () => {
3535
it("returns the system field type name for a given collection type", () => {
36-
expect(getSystemFields("base")).toBe("BaseSystemFields")
3736
expect(getSystemFields("auth")).toBe("AuthSystemFields")
38-
expect(getSystemFields("view")).toBe("ViewSystemFields")
37+
expect(getSystemFields("base")).toBe("BaseSystemFields")
38+
expect(getSystemFields("view")).toBe("BaseSystemFields")
3939
})
4040
})
4141

0 commit comments

Comments
 (0)