From 45a1c1fc7dfd955b08523337a35525a286e2382b Mon Sep 17 00:00:00 2001 From: Patrick Moody <4031292+patmood@users.noreply.github.com> Date: Thu, 2 Mar 2023 11:18:12 -0800 Subject: [PATCH] fix system fields for view collection types (#53) --- README.md | 4 ++-- dist/index.js | 11 +---------- package.json | 2 +- src/constants.ts | 7 ------- src/lib.ts | 2 -- src/utils.ts | 3 +-- test/__snapshots__/fromJSON.test.ts.snap | 9 +-------- test/__snapshots__/lib.test.ts.snap | 7 ------- test/pocketbase-types-example.ts | 9 +-------- test/utils.test.ts | 4 ++-- 10 files changed, 9 insertions(+), 49 deletions(-) diff --git a/README.md b/README.md index a35fc96..c280ce4 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/dist/index.js b/dist/index.js index 8772524..90e5570 100755 --- a/dist/index.js +++ b/dist/index.js @@ -67,12 +67,6 @@ var AUTH_SYSTEM_FIELDS_DEFINITION = `export type AuthSystemFields = { username: string verified: boolean } & BaseSystemFields`; -var VIEW_SYSTEM_FIELDS_DEFINITION = `export type ViewSystemFields = { - id: ${RECORD_ID_STRING_NAME} - collectionId: string - collectionName: Collections - expand?: T -}`; // src/generics.ts function fieldNameToGeneric(name) { @@ -123,8 +117,6 @@ function getSystemFields(type) { switch (type) { case "auth": return "AuthSystemFields"; - case "view": - return "ViewSystemFields"; default: return "BaseSystemFields"; } @@ -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"), @@ -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( diff --git a/package.json b/package.json index c507565..2cfe5be 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/constants.ts b/src/constants.ts index 35352b4..14ea5ec 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -29,10 +29,3 @@ export const AUTH_SYSTEM_FIELDS_DEFINITION = `export type AuthSystemFields` - -export const VIEW_SYSTEM_FIELDS_DEFINITION = `export type ViewSystemFields = { - \tid: ${RECORD_ID_STRING_NAME} - \tcollectionId: string - \tcollectionName: Collections - \texpand?: T -}` diff --git a/src/lib.ts b/src/lib.ts index 5a6940b..dd70920 100644 --- a/src/lib.ts +++ b/src/lib.ts @@ -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 { @@ -45,7 +44,6 @@ export function generate(results: Array): string { ALIAS_TYPE_DEFINITIONS, BASE_SYSTEM_FIELDS_DEFINITION, AUTH_SYSTEM_FIELDS_DEFINITION, - VIEW_SYSTEM_FIELDS_DEFINITION, RECORD_TYPE_COMMENT, ...recordTypes, responseTypes.join("\n"), diff --git a/src/utils.ts b/src/utils.ts index e14eca4..6f32122 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -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" } } diff --git a/test/__snapshots__/fromJSON.test.ts.snap b/test/__snapshots__/fromJSON.test.ts.snap index 974c4cf..0cc10a0 100644 --- a/test/__snapshots__/fromJSON.test.ts.snap +++ b/test/__snapshots__/fromJSON.test.ts.snap @@ -36,13 +36,6 @@ export type AuthSystemFields = { verified: boolean } & BaseSystemFields -export type ViewSystemFields = { - id: RecordIdString - collectionId: string - collectionName: Collections - expand?: T -} - // Record types for each collection export type BaseRecord = { @@ -102,7 +95,7 @@ export type UsersRecord = { export type BaseResponse = BaseRecord & BaseSystemFields export type CustomAuthResponse = CustomAuthRecord & AuthSystemFields export type EverythingResponse = EverythingRecord & BaseSystemFields -export type MyViewResponse = MyViewRecord & ViewSystemFields +export type MyViewResponse = MyViewRecord & BaseSystemFields export type PostsResponse = PostsRecord & BaseSystemFields export type UsersResponse = UsersRecord & AuthSystemFields diff --git a/test/__snapshots__/lib.test.ts.snap b/test/__snapshots__/lib.test.ts.snap index daf51c7..3124cbf 100644 --- a/test/__snapshots__/lib.test.ts.snap +++ b/test/__snapshots__/lib.test.ts.snap @@ -51,13 +51,6 @@ export type AuthSystemFields = { verified: boolean } & BaseSystemFields -export type ViewSystemFields = { - id: RecordIdString - collectionId: string - collectionName: Collections - expand?: T -} - // Record types for each collection export type BooksRecord = { diff --git a/test/pocketbase-types-example.ts b/test/pocketbase-types-example.ts index 3bba5fa..e19d311 100644 --- a/test/pocketbase-types-example.ts +++ b/test/pocketbase-types-example.ts @@ -33,13 +33,6 @@ export type AuthSystemFields = { verified: boolean } & BaseSystemFields -export type ViewSystemFields = { - id: RecordIdString - collectionId: string - collectionName: Collections - expand?: T -} - // Record types for each collection export type BaseRecord = { @@ -99,7 +92,7 @@ export type UsersRecord = { export type BaseResponse = BaseRecord & BaseSystemFields export type CustomAuthResponse = CustomAuthRecord & AuthSystemFields export type EverythingResponse = EverythingRecord & BaseSystemFields -export type MyViewResponse = MyViewRecord & ViewSystemFields +export type MyViewResponse = MyViewRecord & BaseSystemFields export type PostsResponse = PostsRecord & BaseSystemFields export type UsersResponse = UsersRecord & AuthSystemFields diff --git a/test/utils.test.ts b/test/utils.test.ts index a71e449..83dec32 100644 --- a/test/utils.test.ts +++ b/test/utils.test.ts @@ -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") }) })