Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Support for PocketBase 0.23 with Improved expand Property Optionality Based on Texpand #107

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 29 additions & 12 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ async function fromDatabase(dbPath) {
const result = await db.all("SELECT * FROM _collections");
return result.map((collection) => ({
...collection,
fields: JSON.parse(collection.fields)
fields: JSON.parse(collection.schema)
}));
}
async function fromJSON(path) {
Expand Down Expand Up @@ -77,18 +77,34 @@ var ALIAS_TYPE_DEFINITIONS = `// Alias types for improved usability
export type ${DATE_STRING_TYPE_NAME} = string
export type ${RECORD_ID_STRING_NAME} = string
export type ${HTML_STRING_NAME} = string`;
var IS_EXACTLY_UNKNOWN_TYPE_DEFINITION = `// Utility type to check if T is exactly unknown
type IsExactlyUnknown<T> =
unknown extends T
? T extends unknown
? keyof T extends never
? true
: false
: false
: false`;
var BASE_SYSTEM_FIELDS_DEFINITION = `// System fields
export type BaseSystemFields<T = never> = {
id: ${RECORD_ID_STRING_NAME}
collectionId: string
collectionName: Collections
expand?: T
}`;
var AUTH_SYSTEM_FIELDS_DEFINITION = `export type AuthSystemFields<T = never> = {
email: string
emailVisibility: boolean
username: string
verified: boolean
export type BaseSystemFields<T = unknown> = IsExactlyUnknown<T> extends true
? {
id: ${RECORD_ID_STRING_NAME}
collectionId: string
collectionName: Collections
expand?: unknown
}
: {
id: ${RECORD_ID_STRING_NAME}
collectionId: string
collectionName: Collections
expand: T
};`;
var AUTH_SYSTEM_FIELDS_DEFINITION = `export type AuthSystemFields<T = unknown> = {
email: string
emailVisibility: boolean
username: string
verified: boolean
} & BaseSystemFields<T>`;

// src/utils.ts
Expand Down Expand Up @@ -252,6 +268,7 @@ function generate(results, options2) {
options2.sdk && IMPORTS,
createCollectionEnum(sortedCollectionNames),
ALIAS_TYPE_DEFINITIONS,
IS_EXACTLY_UNKNOWN_TYPE_DEFINITION,
BASE_SYSTEM_FIELDS_DEFINITION,
AUTH_SYSTEM_FIELDS_DEFINITION,
RECORD_TYPE_COMMENT,
Expand Down
39 changes: 27 additions & 12 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,33 @@ export const ALIAS_TYPE_DEFINITIONS = `// Alias types for improved usability
export type ${DATE_STRING_TYPE_NAME} = string
export type ${RECORD_ID_STRING_NAME} = string
export type ${HTML_STRING_NAME} = string`

export const IS_EXACTLY_UNKNOWN_TYPE_DEFINITION = `// Utility type to check if T is exactly unknown
type IsExactlyUnknown<T> =
unknown extends T
? T extends unknown
? keyof T extends never
? true
: false
: false
Comment on lines +21 to +25
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are these nested conditions necessary? I would keep it simple since it's really just checking for the default param

: false`
export const BASE_SYSTEM_FIELDS_DEFINITION = `// System fields
export type BaseSystemFields<T = never> = {
\tid: ${RECORD_ID_STRING_NAME}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

keep the /t white space style, otherwise the output is inconsistent

\tcollectionId: string
\tcollectionName: Collections
\texpand?: T
}`
export type BaseSystemFields<T = unknown> = IsExactlyUnknown<T> extends true
? {
id: ${RECORD_ID_STRING_NAME}
collectionId: string
collectionName: Collections
expand?: unknown
}
: {
id: ${RECORD_ID_STRING_NAME}
collectionId: string
collectionName: Collections
expand: T
};`

export const AUTH_SYSTEM_FIELDS_DEFINITION = `export type AuthSystemFields<T = never> = {
\temail: string
\temailVisibility: boolean
\tusername: string
\tverified: boolean
export const AUTH_SYSTEM_FIELDS_DEFINITION = `export type AuthSystemFields<T = unknown> = {
email: string
emailVisibility: boolean
username: string
verified: boolean
} & BaseSystemFields<T>`
2 changes: 2 additions & 0 deletions src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
RECORD_TYPE_COMMENT,
RESPONSE_TYPE_COMMENT,
IMPORTS,
IS_EXACTLY_UNKNOWN_TYPE_DEFINITION,
} from "./constants"
import { CollectionRecord, FieldSchema } from "./types"
import {
Expand Down Expand Up @@ -52,6 +53,7 @@ export function generate(
options.sdk && IMPORTS,
createCollectionEnum(sortedCollectionNames),
ALIAS_TYPE_DEFINITIONS,
IS_EXACTLY_UNKNOWN_TYPE_DEFINITION,
BASE_SYSTEM_FIELDS_DEFINITION,
AUTH_SYSTEM_FIELDS_DEFINITION,
RECORD_TYPE_COMMENT,
Expand Down
2 changes: 1 addition & 1 deletion src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export async function fromDatabase(
const result = await db.all("SELECT * FROM _collections")
return result.map((collection) => ({
...collection,
fields: JSON.parse(collection.fields),
fields: JSON.parse(collection.schema),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why does this need to change?

}))
}

Expand Down
41 changes: 29 additions & 12 deletions test/__snapshots__/fromJSON.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,36 @@ export type IsoDateString = string
export type RecordIdString = string
export type HTMLString = string

// System fields
export type BaseSystemFields<T = never> = {
id: RecordIdString
collectionId: string
collectionName: Collections
expand?: T
}
// Utility type to check if T is exactly unknown
type IsExactlyUnknown<T> =
unknown extends T
? T extends unknown
? keyof T extends never
? true
: false
: false
: false
Comment on lines +30 to +38
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this just return the expected type and be joined with BaseSystemFields? This would prevent some repetition and be more explicit about what it's doing.

Suggested change
// Utility type to check if T is exactly unknown
type IsExactlyUnknown<T> =
unknown extends T
? T extends unknown
? keyof T extends never
? true
: false
: false
: false
// Utility type to get expand field. If T is provided, expand is no longer optional
type ExpandType<T> =
unknown extends T
? T extends unknown
? { expand?: unknown }
: { expand: T }
: { expand: T }


export type AuthSystemFields<T = never> = {
email: string
emailVisibility: boolean
username: string
verified: boolean
// System fields
export type BaseSystemFields<T = unknown> = IsExactlyUnknown<T> extends true
? {
id: RecordIdString
collectionId: string
collectionName: Collections
expand?: unknown
}
: {
id: RecordIdString
collectionId: string
collectionName: Collections
expand: T
};

export type AuthSystemFields<T = unknown> = {
email: string
emailVisibility: boolean
username: string
verified: boolean
} & BaseSystemFields<T>

// Record types for each collection
Expand Down
41 changes: 29 additions & 12 deletions test/__snapshots__/lib.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,36 @@ export type IsoDateString = string
export type RecordIdString = string
export type HTMLString = string

// System fields
export type BaseSystemFields<T = never> = {
id: RecordIdString
collectionId: string
collectionName: Collections
expand?: T
}
// Utility type to check if T is exactly unknown
type IsExactlyUnknown<T> =
unknown extends T
? T extends unknown
? keyof T extends never
? true
: false
: false
: false

export type AuthSystemFields<T = never> = {
email: string
emailVisibility: boolean
username: string
verified: boolean
// System fields
export type BaseSystemFields<T = unknown> = IsExactlyUnknown<T> extends true
? {
id: RecordIdString
collectionId: string
collectionName: Collections
expand?: unknown
}
: {
id: RecordIdString
collectionId: string
collectionName: Collections
expand: T
};

export type AuthSystemFields<T = unknown> = {
email: string
emailVisibility: boolean
username: string
verified: boolean
} & BaseSystemFields<T>

// Record types for each collection
Expand Down
41 changes: 29 additions & 12 deletions test/pocketbase-types-example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,36 @@ export type IsoDateString = string
export type RecordIdString = string
export type HTMLString = string

// System fields
export type BaseSystemFields<T = never> = {
id: RecordIdString
collectionId: string
collectionName: Collections
expand?: T
}
// Utility type to check if T is exactly unknown
type IsExactlyUnknown<T> =
unknown extends T
? T extends unknown
? keyof T extends never
? true
: false
: false
: false

export type AuthSystemFields<T = never> = {
email: string
emailVisibility: boolean
username: string
verified: boolean
// System fields
export type BaseSystemFields<T = unknown> = IsExactlyUnknown<T> extends true
? {
id: RecordIdString
collectionId: string
collectionName: Collections
expand?: unknown
}
: {
id: RecordIdString
collectionId: string
collectionName: Collections
expand: T
};

export type AuthSystemFields<T = unknown> = {
email: string
emailVisibility: boolean
username: string
verified: boolean
} & BaseSystemFields<T>

// Record types for each collection
Expand Down
Loading