fix major field not populating

This commit is contained in:
chark1es 2025-02-03 12:39:30 -08:00
parent 3bc60e4739
commit 6df69275b9
2 changed files with 575 additions and 505 deletions

View file

@ -6,6 +6,12 @@ interface BaseRecord {
[key: string]: any; [key: string]: any;
} }
// Interface for request options
interface RequestOptions {
fields?: string[];
disableAutoCancellation?: boolean;
}
export class Get { export class Get {
private auth: Authentication; private auth: Authentication;
private static instance: Get; private static instance: Get;
@ -28,13 +34,13 @@ export class Get {
* Get a single record by ID * Get a single record by ID
* @param collectionName The name of the collection * @param collectionName The name of the collection
* @param recordId The ID of the record to retrieve * @param recordId The ID of the record to retrieve
* @param fields Optional array of fields to select * @param options Optional request options including fields to select and auto-cancellation control
* @returns The requested record * @returns The requested record
*/ */
public async getOne<T extends BaseRecord>( public async getOne<T extends BaseRecord>(
collectionName: string, collectionName: string,
recordId: string, recordId: string,
fields?: string[] options?: RequestOptions,
): Promise<T> { ): Promise<T> {
if (!this.auth.isAuthenticated()) { if (!this.auth.isAuthenticated()) {
throw new Error("User must be authenticated to retrieve records"); throw new Error("User must be authenticated to retrieve records");
@ -42,8 +48,13 @@ export class Get {
try { try {
const pb = this.auth.getPocketBase(); const pb = this.auth.getPocketBase();
const options = fields ? { fields: fields.join(",") } : undefined; const requestOptions = {
return await pb.collection(collectionName).getOne<T>(recordId, options); ...(options?.fields && { fields: options.fields.join(",") }),
...(options?.disableAutoCancellation && { requestKey: null }),
};
return await pb
.collection(collectionName)
.getOne<T>(recordId, requestOptions);
} catch (err) { } catch (err) {
console.error(`Failed to get record from ${collectionName}:`, err); console.error(`Failed to get record from ${collectionName}:`, err);
throw err; throw err;
@ -54,13 +65,13 @@ export class Get {
* Get multiple records by their IDs * Get multiple records by their IDs
* @param collectionName The name of the collection * @param collectionName The name of the collection
* @param recordIds Array of record IDs to retrieve * @param recordIds Array of record IDs to retrieve
* @param fields Optional array of fields to select * @param options Optional request options including fields to select and auto-cancellation control
* @returns Array of requested records * @returns Array of requested records
*/ */
public async getMany<T extends BaseRecord>( public async getMany<T extends BaseRecord>(
collectionName: string, collectionName: string,
recordIds: string[], recordIds: string[],
fields?: string[] options?: RequestOptions,
): Promise<T[]> { ): Promise<T[]> {
if (!this.auth.isAuthenticated()) { if (!this.auth.isAuthenticated()) {
throw new Error("User must be authenticated to retrieve records"); throw new Error("User must be authenticated to retrieve records");
@ -69,16 +80,19 @@ export class Get {
try { try {
const pb = this.auth.getPocketBase(); const pb = this.auth.getPocketBase();
const filter = `id ?~ "${recordIds.join("|")}"`; const filter = `id ?~ "${recordIds.join("|")}"`;
const options = { const requestOptions = {
filter, filter,
...(fields && { fields: fields.join(",") }) ...(options?.fields && { fields: options.fields.join(",") }),
...(options?.disableAutoCancellation && { requestKey: null }),
}; };
const result = await pb.collection(collectionName).getFullList<T>(options); const result = await pb
.collection(collectionName)
.getFullList<T>(requestOptions);
// Sort results to match the order of requested IDs // Sort results to match the order of requested IDs
const recordMap = new Map(result.map(record => [record.id, record])); const recordMap = new Map(result.map((record) => [record.id, record]));
return recordIds.map(id => recordMap.get(id)).filter(Boolean) as T[]; return recordIds.map((id) => recordMap.get(id)).filter(Boolean) as T[];
} catch (err) { } catch (err) {
console.error(`Failed to get records from ${collectionName}:`, err); console.error(`Failed to get records from ${collectionName}:`, err);
throw err; throw err;
@ -92,7 +106,7 @@ export class Get {
* @param perPage Number of items per page * @param perPage Number of items per page
* @param filter Optional filter string * @param filter Optional filter string
* @param sort Optional sort string * @param sort Optional sort string
* @param fields Optional array of fields to select * @param options Optional request options including fields to select and auto-cancellation control
* @returns Paginated list of records * @returns Paginated list of records
*/ */
public async getList<T extends BaseRecord>( public async getList<T extends BaseRecord>(
@ -101,7 +115,7 @@ export class Get {
perPage: number = 20, perPage: number = 20,
filter?: string, filter?: string,
sort?: string, sort?: string,
fields?: string[] options?: RequestOptions,
): Promise<{ ): Promise<{
page: number; page: number;
perPage: number; perPage: number;
@ -115,20 +129,23 @@ export class Get {
try { try {
const pb = this.auth.getPocketBase(); const pb = this.auth.getPocketBase();
const options = { const requestOptions = {
...(filter && { filter }), ...(filter && { filter }),
...(sort && { sort }), ...(sort && { sort }),
...(fields && { fields: fields.join(",") }) ...(options?.fields && { fields: options.fields.join(",") }),
...(options?.disableAutoCancellation && { requestKey: null }),
}; };
const result = await pb.collection(collectionName).getList<T>(page, perPage, options); const result = await pb
.collection(collectionName)
.getList<T>(page, perPage, requestOptions);
return { return {
page: result.page, page: result.page,
perPage: result.perPage, perPage: result.perPage,
totalItems: result.totalItems, totalItems: result.totalItems,
totalPages: result.totalPages, totalPages: result.totalPages,
items: result.items items: result.items,
}; };
} catch (err) { } catch (err) {
console.error(`Failed to get list from ${collectionName}:`, err); console.error(`Failed to get list from ${collectionName}:`, err);
@ -141,14 +158,14 @@ export class Get {
* @param collectionName The name of the collection * @param collectionName The name of the collection
* @param filter Optional filter string * @param filter Optional filter string
* @param sort Optional sort string * @param sort Optional sort string
* @param fields Optional array of fields to select * @param options Optional request options including fields to select and auto-cancellation control
* @returns Array of all matching records * @returns Array of all matching records
*/ */
public async getAll<T extends BaseRecord>( public async getAll<T extends BaseRecord>(
collectionName: string, collectionName: string,
filter?: string, filter?: string,
sort?: string, sort?: string,
fields?: string[] options?: RequestOptions,
): Promise<T[]> { ): Promise<T[]> {
if (!this.auth.isAuthenticated()) { if (!this.auth.isAuthenticated()) {
throw new Error("User must be authenticated to retrieve records"); throw new Error("User must be authenticated to retrieve records");
@ -156,13 +173,14 @@ export class Get {
try { try {
const pb = this.auth.getPocketBase(); const pb = this.auth.getPocketBase();
const options = { const requestOptions = {
...(filter && { filter }), ...(filter && { filter }),
...(sort && { sort }), ...(sort && { sort }),
...(fields && { fields: fields.join(",") }) ...(options?.fields && { fields: options.fields.join(",") }),
...(options?.disableAutoCancellation && { requestKey: null }),
}; };
return await pb.collection(collectionName).getFullList<T>(options); return await pb.collection(collectionName).getFullList<T>(requestOptions);
} catch (err) { } catch (err) {
console.error(`Failed to get all records from ${collectionName}:`, err); console.error(`Failed to get all records from ${collectionName}:`, err);
throw err; throw err;
@ -173,13 +191,13 @@ export class Get {
* Get the first record that matches a filter * Get the first record that matches a filter
* @param collectionName The name of the collection * @param collectionName The name of the collection
* @param filter Filter string * @param filter Filter string
* @param fields Optional array of fields to select * @param options Optional request options including fields to select and auto-cancellation control
* @returns The first matching record or null if none found * @returns The first matching record or null if none found
*/ */
public async getFirst<T extends BaseRecord>( public async getFirst<T extends BaseRecord>(
collectionName: string, collectionName: string,
filter: string, filter: string,
fields?: string[] options?: RequestOptions,
): Promise<T | null> { ): Promise<T | null> {
if (!this.auth.isAuthenticated()) { if (!this.auth.isAuthenticated()) {
throw new Error("User must be authenticated to retrieve records"); throw new Error("User must be authenticated to retrieve records");
@ -187,18 +205,21 @@ export class Get {
try { try {
const pb = this.auth.getPocketBase(); const pb = this.auth.getPocketBase();
const options = { const requestOptions = {
filter, filter,
...(fields && { fields: fields.join(",") }), ...(options?.fields && { fields: options.fields.join(",") }),
...(options?.disableAutoCancellation && { requestKey: null }),
sort: "created", sort: "created",
perPage: 1 perPage: 1,
}; };
const result = await pb.collection(collectionName).getList<T>(1, 1, options); const result = await pb
.collection(collectionName)
.getList<T>(1, 1, requestOptions);
return result.items.length > 0 ? result.items[0] : null; return result.items.length > 0 ? result.items[0] : null;
} catch (err) { } catch (err) {
console.error(`Failed to get first record from ${collectionName}:`, err); console.error(`Failed to get first record from ${collectionName}:`, err);
throw err; throw err;
} }
} }
} }

File diff suppressed because it is too large Load diff