From 3ab9ca6cf48b7aff0f7022c07a05061535f2b007 Mon Sep 17 00:00:00 2001 From: Patrick Moody <4031292+patmood@users.noreply.github.com> Date: Fri, 1 Sep 2023 13:30:07 +0930 Subject: [PATCH] sort fields alphabetically (#78) --- dist/index.js | 4 ++-- package.json | 2 +- src/lib.ts | 1 + test/__snapshots__/fromJSON.test.ts.snap | 30 ++++++++++++------------ test/lib.test.ts | 30 ++++++++++++++++++++++++ test/pocketbase-types-example.ts | 30 ++++++++++++------------ 6 files changed, 64 insertions(+), 33 deletions(-) diff --git a/dist/index.js b/dist/index.js index 378a969..ec14865 100755 --- a/dist/index.js +++ b/dist/index.js @@ -251,7 +251,7 @@ function createRecordType(name, schema) { const genericArgs = getGenericArgStringWithDefault(schema, { includeExpand: false }); - const fields = schema.map((fieldSchema) => createTypeField(name, fieldSchema)).join("\n"); + const fields = schema.map((fieldSchema) => createTypeField(name, fieldSchema)).sort().join("\n"); return `${selectOptionEnums}export type ${typeName}Record${genericArgs} = ${fields ? `{ ${fields} }` : "never"}`; @@ -304,7 +304,7 @@ async function main(options2) { import { program } from "commander"; // package.json -var version = "1.1.12"; +var version = "1.1.13"; // src/index.ts program.name("Pocketbase Typegen").version(version).description( diff --git a/package.json b/package.json index fc2b825..a66acaf 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "pocketbase-typegen", - "version": "1.1.12", + "version": "1.1.13", "description": "Generate pocketbase record types from your database", "main": "dist/index.js", "bin": { diff --git a/src/lib.ts b/src/lib.ts index 5ff9394..f08561c 100644 --- a/src/lib.ts +++ b/src/lib.ts @@ -65,6 +65,7 @@ export function createRecordType( }) const fields = schema .map((fieldSchema: FieldSchema) => createTypeField(name, fieldSchema)) + .sort() .join("\n") return `${selectOptionEnums}export type ${typeName}Record${genericArgs} = ${ diff --git a/test/__snapshots__/fromJSON.test.ts.snap b/test/__snapshots__/fromJSON.test.ts.snap index e4f7d3d..8c3c9b6 100644 --- a/test/__snapshots__/fromJSON.test.ts.snap +++ b/test/__snapshots__/fromJSON.test.ts.snap @@ -55,40 +55,40 @@ export enum EverythingSelectFieldOptions { "sy?mb@!$" = "sy?mb@!$", } export type EverythingRecord = { - text_field?: string - number_field?: number + another_json_field?: null | Tanother_json_field bool_field?: boolean - email_field?: string - url_field?: string + custom_relation_field?: RecordIdString[] date_field?: IsoDateString - select_field?: EverythingSelectFieldOptions - json_field?: null | Tjson_field - another_json_field?: null | Tanother_json_field + email_field?: string file_field?: string - three_files_field?: string[] - user_relation_field?: RecordIdString - custom_relation_field?: RecordIdString[] + json_field?: null | Tjson_field + number_field?: number post_relation_field?: RecordIdString - select_field_no_values?: string rich_editor_field?: HTMLString + select_field?: EverythingSelectFieldOptions + select_field_no_values?: string + text_field?: string + three_files_field?: string[] + url_field?: string + user_relation_field?: RecordIdString } export type MyViewRecord = { + json_field?: null | Tjson_field post_relation_field?: RecordIdString text_field?: string - json_field?: null | Tjson_field } export type PostsRecord = { + field1?: number field?: string - nonempty_field: string nonempty_bool: boolean - field1?: number + nonempty_field: string } export type UsersRecord = { - name?: string avatar?: string + name?: string } // Response types include system fields and match responses from the PocketBase API diff --git a/test/lib.test.ts b/test/lib.test.ts index 4328a1c..83f412a 100644 --- a/test/lib.test.ts +++ b/test/lib.test.ts @@ -66,6 +66,36 @@ describe("createRecordType", () => { const result = createRecordType(name, schema) expect(result).toMatchSnapshot() }) + + it("sorts fields alphabetically", () => { + const name = "books" + const schema: FieldSchema[] = [ + { + id: "1", + name: "banana", + options: {}, + required: false, + system: false, + type: "text", + unique: false, + }, + { + id: "1", + name: "apple", + options: {}, + required: false, + system: false, + type: "text", + unique: false, + }, + ] + const result = createRecordType(name, schema) + const aIndex = result.indexOf("apple") + const bIndex = result.indexOf("banana") + expect(aIndex).toBeGreaterThan(0) + expect(bIndex).toBeGreaterThan(0) + expect(aIndex).toBeLessThan(bIndex) + }) }) describe("createResponseType", () => { diff --git a/test/pocketbase-types-example.ts b/test/pocketbase-types-example.ts index 5b952b7..17db13b 100644 --- a/test/pocketbase-types-example.ts +++ b/test/pocketbase-types-example.ts @@ -52,40 +52,40 @@ export enum EverythingSelectFieldOptions { "sy?mb@!$" = "sy?mb@!$", } export type EverythingRecord = { - text_field?: string - number_field?: number + another_json_field?: null | Tanother_json_field bool_field?: boolean - email_field?: string - url_field?: string + custom_relation_field?: RecordIdString[] date_field?: IsoDateString - select_field?: EverythingSelectFieldOptions - json_field?: null | Tjson_field - another_json_field?: null | Tanother_json_field + email_field?: string file_field?: string - three_files_field?: string[] - user_relation_field?: RecordIdString - custom_relation_field?: RecordIdString[] + json_field?: null | Tjson_field + number_field?: number post_relation_field?: RecordIdString - select_field_no_values?: string rich_editor_field?: HTMLString + select_field?: EverythingSelectFieldOptions + select_field_no_values?: string + text_field?: string + three_files_field?: string[] + url_field?: string + user_relation_field?: RecordIdString } export type MyViewRecord = { + json_field?: null | Tjson_field post_relation_field?: RecordIdString text_field?: string - json_field?: null | Tjson_field } export type PostsRecord = { + field1?: number field?: string - nonempty_field: string nonempty_bool: boolean - field1?: number + nonempty_field: string } export type UsersRecord = { - name?: string avatar?: string + name?: string } // Response types include system fields and match responses from the PocketBase API