-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Give records a reference to the model manager which loaded them
We have a few upcoming things that require records to be able to do stuff to themselves -- reload, select new fields from the backend, maybe `record.save` or similar, and I suspect more in the future. I still think they should act mostly as DTOs that don't have a lot of business logic on them, but if we want to be able to even know what model they are for, we need a reference to the thing that produced them! This passes along the model manager which instantiated a record to the record in both imperative API land and in React land. In order to make this change work in React land, we need a new little bit of metadata exported from the generated client. Our React hooks get passed functions from the api client like `useAction(api.post.create)`, so we need to be able to hop back from the `create` function that we get by reference to the `api.post` object. The generated client will need to decorate the `create` function with a reference, which is not super hard, but means that we have a backwards compatibility issue. `@gadgetinc/react` can't assume that it is upgraded at the same time as the api client, so it can't assume it is working against a newly generated api client that has this metadata. For this reason, the model manager property on `GadgetRecord` is optional, which reflects the way it will be used in the real world without necessarily having that metadata available. When we go to build things like `record.reload()`, we can make that fail at runtime with a message saying "regenerate your client to get this to work!", instead of just assuming it is present.
- Loading branch information
Showing
19 changed files
with
160 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import type { GadgetConnection } from "./GadgetConnection"; | ||
import type { GadgetRecord } from "./GadgetRecord"; | ||
import type { GadgetRecordList } from "./GadgetRecordList"; | ||
import type { InternalModelManager } from "./InternalModelManager"; | ||
|
||
/** | ||
* The manager class for a given model that uses the Public API, like `api.post` or `api.user` | ||
**/ | ||
export interface AnyPublicModelManager { | ||
connection: GadgetConnection; | ||
apiIdentifier?: string; | ||
findOne(id: string, options: any): Promise<GadgetRecord<any>>; | ||
maybeFindOne(id: string, options: any): Promise<GadgetRecord<any> | null>; | ||
findMany(options: any): Promise<GadgetRecordList<any>>; | ||
findFirst(options: any): Promise<GadgetRecord<any>>; | ||
maybeFindFirst(options: any): Promise<GadgetRecord<any> | null>; | ||
} | ||
|
||
/** | ||
* The manager class for a given single model that uses the Public API, like `api.session` | ||
**/ | ||
export interface AnyPublicSingletonModelManager { | ||
connection: GadgetConnection; | ||
apiIdentifier?: string; | ||
get(): Promise<GadgetRecord<any>>; | ||
} | ||
|
||
/** | ||
* Any model manager, either public or internal | ||
*/ | ||
export type AnyModelManager = AnyPublicModelManager | AnyPublicSingletonModelManager | InternalModelManager; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.