Skip to content

Commit

Permalink
fix system fields for view collection types (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
patmood authored Mar 2, 2023
1 parent b0f1208 commit 45a1c1f
Show file tree
Hide file tree
Showing 10 changed files with 9 additions and 49 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ Add it to your projects `package.json`:

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

- `Collections` An enum of all collections/
- `[CollectionName]Record` One type for each collection (eg ProfilesRecord)/
- `Collections` An enum of all collections.
- `[CollectionName]Record` One type for each collection (eg ProfilesRecord).
- `[CollectionName]Response` One response type for each collection (eg ProfilesResponse) which includes system fields. This is what is returned from the PocketBase API.
- `[CollectionName][FieldName]Options` If the collection contains a select field with set values, an enum of the options will be generated.
- `CollectionRecords` A type mapping each collection name to the record type.
Expand Down
11 changes: 1 addition & 10 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,6 @@ var AUTH_SYSTEM_FIELDS_DEFINITION = `export type AuthSystemFields<T = never> = {
username: string
verified: boolean
} & BaseSystemFields<T>`;
var VIEW_SYSTEM_FIELDS_DEFINITION = `export type ViewSystemFields<T = never> = {
id: ${RECORD_ID_STRING_NAME}
collectionId: string
collectionName: Collections
expand?: T
}`;

// src/generics.ts
function fieldNameToGeneric(name) {
Expand Down Expand Up @@ -123,8 +117,6 @@ function getSystemFields(type) {
switch (type) {
case "auth":
return "AuthSystemFields";
case "view":
return "ViewSystemFields";
default:
return "BaseSystemFields";
}
Expand Down Expand Up @@ -222,7 +214,6 @@ function generate(results) {
ALIAS_TYPE_DEFINITIONS,
BASE_SYSTEM_FIELDS_DEFINITION,
AUTH_SYSTEM_FIELDS_DEFINITION,
VIEW_SYSTEM_FIELDS_DEFINITION,
RECORD_TYPE_COMMENT,
...recordTypes,
responseTypes.join("\n"),
Expand Down Expand Up @@ -278,7 +269,7 @@ async function main(options2) {
import { program } from "commander";

// package.json
var version = "1.1.6";
var version = "1.1.7";

// 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.6",
"version": "1.1.7",
"description": "Generate pocketbase record types from your database",
"main": "dist/index.js",
"bin": {
Expand Down
7 changes: 0 additions & 7 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,3 @@ export const AUTH_SYSTEM_FIELDS_DEFINITION = `export type AuthSystemFields<T = n
\tusername: string
\tverified: boolean
} & BaseSystemFields<T>`

export const VIEW_SYSTEM_FIELDS_DEFINITION = `export type ViewSystemFields<T = never> = {
\tid: ${RECORD_ID_STRING_NAME}
\tcollectionId: string
\tcollectionName: Collections
\texpand?: T
}`
2 changes: 0 additions & 2 deletions src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
EXPORT_COMMENT,
RECORD_TYPE_COMMENT,
RESPONSE_TYPE_COMMENT,
VIEW_SYSTEM_FIELDS_DEFINITION,
} from "./constants"
import { CollectionRecord, FieldSchema } from "./types"
import {
Expand Down Expand Up @@ -45,7 +44,6 @@ export function generate(results: Array<CollectionRecord>): string {
ALIAS_TYPE_DEFINITIONS,
BASE_SYSTEM_FIELDS_DEFINITION,
AUTH_SYSTEM_FIELDS_DEFINITION,
VIEW_SYSTEM_FIELDS_DEFINITION,
RECORD_TYPE_COMMENT,
...recordTypes,
responseTypes.join("\n"),
Expand Down
3 changes: 1 addition & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ export function getSystemFields(type: CollectionRecord["type"]) {
switch (type) {
case "auth":
return "AuthSystemFields"
case "view":
return "ViewSystemFields"
default:
// `view` and `base` collection types share the same system fields (for now)
return "BaseSystemFields"
}
}
Expand Down
9 changes: 1 addition & 8 deletions test/__snapshots__/fromJSON.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,6 @@ export type AuthSystemFields<T = never> = {
verified: boolean
} & BaseSystemFields<T>
export type ViewSystemFields<T = never> = {
id: RecordIdString
collectionId: string
collectionName: Collections
expand?: T
}
// Record types for each collection
export type BaseRecord = {
Expand Down Expand Up @@ -102,7 +95,7 @@ export type UsersRecord = {
export type BaseResponse = BaseRecord & BaseSystemFields
export type CustomAuthResponse = CustomAuthRecord & AuthSystemFields
export type EverythingResponse<Tanother_json_field = unknown, Tjson_field = unknown, Texpand = unknown> = EverythingRecord<Tanother_json_field, Tjson_field> & BaseSystemFields<Texpand>
export type MyViewResponse<Tjson_field = unknown, Texpand = unknown> = MyViewRecord<Tjson_field> & ViewSystemFields<Texpand>
export type MyViewResponse<Tjson_field = unknown, Texpand = unknown> = MyViewRecord<Tjson_field> & BaseSystemFields<Texpand>
export type PostsResponse = PostsRecord & BaseSystemFields
export type UsersResponse = UsersRecord & AuthSystemFields
Expand Down
7 changes: 0 additions & 7 deletions test/__snapshots__/lib.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,6 @@ export type AuthSystemFields<T = never> = {
verified: boolean
} & BaseSystemFields<T>
export type ViewSystemFields<T = never> = {
id: RecordIdString
collectionId: string
collectionName: Collections
expand?: T
}
// Record types for each collection
export type BooksRecord = {
Expand Down
9 changes: 1 addition & 8 deletions test/pocketbase-types-example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,6 @@ export type AuthSystemFields<T = never> = {
verified: boolean
} & BaseSystemFields<T>

export type ViewSystemFields<T = never> = {
id: RecordIdString
collectionId: string
collectionName: Collections
expand?: T
}

// Record types for each collection

export type BaseRecord = {
Expand Down Expand Up @@ -99,7 +92,7 @@ export type UsersRecord = {
export type BaseResponse = BaseRecord & BaseSystemFields
export type CustomAuthResponse = CustomAuthRecord & AuthSystemFields
export type EverythingResponse<Tanother_json_field = unknown, Tjson_field = unknown, Texpand = unknown> = EverythingRecord<Tanother_json_field, Tjson_field> & BaseSystemFields<Texpand>
export type MyViewResponse<Tjson_field = unknown, Texpand = unknown> = MyViewRecord<Tjson_field> & ViewSystemFields<Texpand>
export type MyViewResponse<Tjson_field = unknown, Texpand = unknown> = MyViewRecord<Tjson_field> & BaseSystemFields<Texpand>
export type PostsResponse = PostsRecord & BaseSystemFields
export type UsersResponse = UsersRecord & AuthSystemFields

Expand Down
4 changes: 2 additions & 2 deletions test/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ describe("sanitizeFieldName", () => {

describe("getSystemFields", () => {
it("returns the system field type name for a given collection type", () => {
expect(getSystemFields("base")).toBe("BaseSystemFields")
expect(getSystemFields("auth")).toBe("AuthSystemFields")
expect(getSystemFields("view")).toBe("ViewSystemFields")
expect(getSystemFields("base")).toBe("BaseSystemFields")
expect(getSystemFields("view")).toBe("BaseSystemFields")
})
})

Expand Down

0 comments on commit 45a1c1f

Please sign in to comment.