-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
44 additions
and
25 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
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 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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import { i, id, init, InstaQLEntity } from "@instantdb/admin"; | ||
import { i, id, init, InstaQLEntity, UpdateParams } from "@instantdb/admin"; | ||
|
||
const _schema = i.schema({ entities: { users: i.entity({ email: i.string() }) } }); | ||
type _AppSchema = typeof _schema; | ||
|
@@ -13,22 +13,27 @@ const db = init({ | |
|
||
type Collection = keyof typeof schema.entities; | ||
|
||
type Entity<T extends Collection> = InstaQLEntity<typeof schema, T, {}>; | ||
type EntityUpdate<T extends Collection> = UpdateParams<typeof schema, T>; | ||
|
||
export const newEntity = async <T extends Collection>( | ||
type: T, | ||
props: Omit<Entity<T>, "id">, | ||
props: EntityUpdate<T>, | ||
) => { | ||
const theId = id(); | ||
await db.transact(db.tx[type][theId].update(props)); | ||
return theId; | ||
}; | ||
|
||
// seems good | ||
const alice = await newEntity("users", { email: "[email protected]" }); | ||
const existing_attr_works = await newEntity("users", { email: "[email protected]" }); | ||
|
||
// Should be a typing error but is not | ||
const bob = await newEntity("users", { blabla: "[email protected]" }); | ||
// @ts-expect-error | ||
const non_existing_attr_errors = await newEntity("users", { blabla: "[email protected]" }); | ||
|
||
// Should be a typing error but is not | ||
const eve = await newEntity("users", { email: 123 }); | ||
// @ts-expect-error | ||
const wrong_type_errors = await newEntity("users", { email: 123 }); | ||
|
||
// to silence ts warnings | ||
existing_attr_works; | ||
non_existing_attr_errors; | ||
wrong_type_errors; |