Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Allow RecordType to be passed to managers #747

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
17 changes: 10 additions & 7 deletions packages/api-client-core/src/GadgetRecordList.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable no-throw-literal */
/* eslint-disable @typescript-eslint/require-await */
import type { Jsonify } from "type-fest";
import type { GadgetRecord, RecordShape } from "./GadgetRecord.js";
import type { GadgetRecord, GadgetRecord_, RecordShape } from "./GadgetRecord.js";
import type { InternalModelManager } from "./InternalModelManager.js";
import type { AnyModelManager } from "./ModelManager.js";
import { GadgetClientError, GadgetOperationError } from "./support.js";
Expand All @@ -13,17 +13,20 @@ type PaginationConfig = {
};

/** Represents a list of objects returned from the API. Facilitates iterating and paginating. */
export class GadgetRecordList<Shape extends RecordShape> extends Array<GadgetRecord<Shape>> {
export class GadgetRecordList<
Shape extends RecordShape,
RecordType extends GadgetRecord_<Shape> = GadgetRecord<any>
> extends Array<RecordType> {
modelManager!: AnyModelManager | InternalModelManager<Shape>;
pagination!: PaginationConfig;

/** Internal method used to create a list. Should not be used by applications. */
static boot<Shape extends RecordShape>(
static boot<Shape extends RecordShape, RecordType extends GadgetRecord_<Shape> = GadgetRecord<any>>(
modelManager: AnyModelManager | InternalModelManager<Shape>,
records: GadgetRecord<Shape>[],
records: RecordType[],
pagination: PaginationConfig
) {
const list = new GadgetRecordList<Shape>();
const list = new GadgetRecordList<Shape, RecordType>();
list.push(...records);
list.modelManager = modelManager;
list.pagination = pagination;
Expand Down Expand Up @@ -71,7 +74,7 @@ export class GadgetRecordList<Shape extends RecordShape> extends Array<GadgetRec
...options,
after: this.pagination.pageInfo.endCursor,
first: first || last,
}) as Promise<GadgetRecordList<Shape>>;
}) as Promise<GadgetRecordList<Shape, RecordType>>;
return await nextPage;
}

Expand All @@ -86,7 +89,7 @@ export class GadgetRecordList<Shape extends RecordShape> extends Array<GadgetRec
...options,
before: this.pagination.pageInfo.startCursor,
last: last || first,
}) as Promise<GadgetRecordList<Shape>>;
}) as Promise<GadgetRecordList<Shape, RecordType>>;
return await prevPage;
}
}
14 changes: 8 additions & 6 deletions packages/api-client-core/src/ModelManager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { GadgetConnection } from "./GadgetConnection.js";
import type { GadgetRecord } from "./GadgetRecord.js";
import { GadgetRecord, GadgetRecord_ } from "./GadgetRecord.js";
import type { GadgetRecordList } from "./GadgetRecordList.js";

export type AnyModelFinderMetadata = {
Expand All @@ -24,9 +24,11 @@ export type AnyModelFinderMetadata = {
* This is a generic interface. Concrete ones are generated by Gadget, */
export interface AnyModelManager {
connection: GadgetConnection;
findOne: ((id: string, options: any) => Promise<GadgetRecord<any>>) & AnyModelFinderMetadata;
findMany: ((options: any) => Promise<GadgetRecordList<any>>) & AnyModelFinderMetadata;
findFirst: ((options: any) => Promise<GadgetRecord<any>>) & AnyModelFinderMetadata;
maybeFindFirst(options: any): Promise<GadgetRecord<any> | null>;
maybeFindOne(id: string, options: any): Promise<GadgetRecord<any> | null>;
findOne: (<RecordType extends GadgetRecord_<any> = GadgetRecord<any>>(id: string, options: any) => Promise<RecordType>) &
AnyModelFinderMetadata;
findMany: (<RecordType extends GadgetRecord_<any> = GadgetRecord<any>>(options: any) => Promise<GadgetRecordList<any, RecordType>>) &
AnyModelFinderMetadata;
findFirst: (<RecordType extends GadgetRecord_<any> = GadgetRecord<any>>(options: any) => Promise<RecordType>) & AnyModelFinderMetadata;
maybeFindFirst<RecordType extends GadgetRecord_<any> = GadgetRecord<any>>(options: any): Promise<RecordType | null>;
maybeFindOne<RecordType extends GadgetRecord_<any> = GadgetRecord<any>>(id: string, options: any): Promise<RecordType | null>;
}
Loading