Skip to content

Commit

Permalink
handle relation fields with maxSelect = null (#28)
Browse files Browse the repository at this point in the history
when maxSelect = null (left empty), it accepts unlimitted number of values
  • Loading branch information
duytbp authored Jan 2, 2023
1 parent cd0a573 commit fd61e68
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ const pbSchemaTypescriptMap = {
? "string[]"
: "string",
relation: (fieldSchema: FieldSchema) =>
fieldSchema.options.maxSelect && fieldSchema.options.maxSelect > 1
? `${RECORD_ID_STRING_NAME}[]`
: RECORD_ID_STRING_NAME,
fieldSchema.options.maxSelect && fieldSchema.options.maxSelect === 1
? RECORD_ID_STRING_NAME
: `${RECORD_ID_STRING_NAME}[]`,
// DEPRECATED: PocketBase v0.8 does not have a dedicated user relation
user: (fieldSchema: FieldSchema) =>
fieldSchema.options.maxSelect && fieldSchema.options.maxSelect > 1
Expand Down
15 changes: 14 additions & 1 deletion test/lib.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ describe("createTypeField", () => {
name: "relationField",
type: "relation",
})
).toEqual("\trelationField: RecordIdString")
).toEqual("\trelationField: RecordIdString[]")
})

it("converts relation type with multiple options", () => {
Expand All @@ -335,6 +335,19 @@ describe("createTypeField", () => {
).toEqual("\trelationFieldMany: RecordIdString[]")
})

it("converts relation type with unset maxSelect", () => {
expect(
createTypeField("test_collection", {
...defaultFieldSchema,
name: "relationFieldMany",
type: "relation",
options: {
maxSelect: null,
},
})
).toEqual("\trelationFieldMany: RecordIdString[]")
})

// DEPRECATED: This was removed in PocketBase v0.8
it("converts user relation type", () => {
expect(
Expand Down

0 comments on commit fd61e68

Please sign in to comment.