Skip to content

Commit

Permalink
sort fields alphabetically (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
patmood authored Sep 1, 2023
1 parent f049dcd commit 3ab9ca6
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 33 deletions.
4 changes: 2 additions & 2 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"}`;
Expand Down Expand Up @@ -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(
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.12",
"version": "1.1.13",
"description": "Generate pocketbase record types from your database",
"main": "dist/index.js",
"bin": {
Expand Down
1 change: 1 addition & 0 deletions src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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} = ${
Expand Down
30 changes: 15 additions & 15 deletions test/__snapshots__/fromJSON.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -55,40 +55,40 @@ export enum EverythingSelectFieldOptions {
"sy?mb@!$" = "sy?mb@!$",
}
export type EverythingRecord<Tanother_json_field = unknown, Tjson_field = unknown> = {
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<Tjson_field = unknown> = {
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
Expand Down
30 changes: 30 additions & 0 deletions test/lib.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down
30 changes: 15 additions & 15 deletions test/pocketbase-types-example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,40 +52,40 @@ export enum EverythingSelectFieldOptions {
"sy?mb@!$" = "sy?mb@!$",
}
export type EverythingRecord<Tanother_json_field = unknown, Tjson_field = unknown> = {
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<Tjson_field = unknown> = {
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
Expand Down

0 comments on commit 3ab9ca6

Please sign in to comment.