Skip to content

Commit

Permalink
only include ViewCollection interface when there's one
Browse files Browse the repository at this point in the history
  • Loading branch information
satohshi committed Nov 23, 2024
1 parent 98ce197 commit eca427f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/hungry-wasps-count.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'pocketbase-ts-schema-generator': patch
---

only include `ViewCollection` interface when there's one
18 changes: 10 additions & 8 deletions src/generate-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,17 @@ export default () => {
)

// interfaces
let addedUniqueKey = false
let collectionInterfaces =
newLine(0, BASE_COLLECTION_INTERFACE, 2) +
newLine(0, AUTH_COLLECTION_INTERFACE, 2) +
newLine(0, VIEW_COLLECTION_INTERFACE, 2)
let header = newLine(0, BASE_COLLECTION_INTERFACE, 2) + newLine(0, AUTH_COLLECTION_INTERFACE, 2)
let collectionInterfaces = ''
const fieldSets: Set<string>[] = []
for (const collection of allCollections) {
const fields = new Set<string>()

// only add view interface if needed
if (collection.type === 'view' && !header.includes(VIEW_COLLECTION_INTERFACE)) {
header += newLine(0, VIEW_COLLECTION_INTERFACE, 2)
}

collectionInterfaces += newLine(
0,
`export interface ${toPascalCase(collection.name)} extends ${
Expand Down Expand Up @@ -105,9 +107,8 @@ export default () => {
// add unique identifier if there are collections with the same set of fields
if (fieldSets.some((set) => haveSameValues(set, fields))) {
// add unique identifier at the top if there are collections with the same set of fields
if (!addedUniqueKey) {
collectionInterfaces = newLine(0, UNIQUE_IDENTIFIER_KEY, 2) + collectionInterfaces
addedUniqueKey = true
if (!header.includes(UNIQUE_IDENTIFIER_KEY)) {
header = newLine(0, UNIQUE_IDENTIFIER_KEY, 2) + header
}
collectionInterfaces += newLine(1, UNIQUE_IDENTIFIER)
}
Expand All @@ -116,6 +117,7 @@ export default () => {

fieldSets.push(fields)
}
collectionInterfaces = header + collectionInterfaces

// relations
const collectionToRelationMap: Record<string, string[]> = {}
Expand Down

0 comments on commit eca427f

Please sign in to comment.