Skip to content

Commit

Permalink
[types] add UpdateParams and LinkParams (#653)
Browse files Browse the repository at this point in the history
  • Loading branch information
stopachka authored Dec 23, 2024
1 parent 84a3975 commit d36aaa3
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 41 deletions.
4 changes: 4 additions & 0 deletions client/packages/admin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ import {
type InstaQLEntity,
type InstaQLResult,
type InstantRules,
type UpdateParams,
type LinkParams,
} from "@instantdb/core";

import version from "./version";
Expand Down Expand Up @@ -951,4 +953,6 @@ export {
type InstaQLEntity,
type InstaQLResult,
type InstantRules,
type UpdateParams,
type LinkParams,
};
4 changes: 4 additions & 0 deletions client/packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ import type {
ValueTypes,
InstantUnknownSchema,
BackwardsCompatibleSchema,
UpdateParams,
LinkParams,
} from "./schemaTypes";

const defaultOpenDevtool = true;
Expand Down Expand Up @@ -730,4 +732,6 @@ export {
type IInstantDatabase,
type BackwardsCompatibleSchema,
type InstantRules,
type UpdateParams,
type LinkParams,
};
38 changes: 5 additions & 33 deletions client/packages/core/src/instatx.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import type { DataAttrDef, IContainEntitiesAndLinks, InstantGraph, LinkAttrDef } from "./schemaTypes";
import type {
IContainEntitiesAndLinks,
LinkParams,
UpdateParams,
} from "./schemaTypes";

type Action = "update" | "link" | "unlink" | "delete" | "merge";
type EType = string;
Expand All @@ -8,38 +12,6 @@ type LookupRef = [string, any];
type Lookup = string;
export type Op = [Action, EType, Id | LookupRef, Args];

type UpdateParams<
Schema extends IContainEntitiesAndLinks<any, any>,
EntityName extends keyof Schema["entities"],
> = {
[AttrName in keyof Schema["entities"][EntityName]["attrs"]]?: Schema["entities"][EntityName]["attrs"][AttrName] extends DataAttrDef<
infer ValueType,
infer IsRequired
>
? IsRequired extends true
? ValueType
: ValueType | null
: never;
} & (Schema extends IContainEntitiesAndLinks<any, any>
? {}
: {
[attribute: string]: any;
});

type LinkParams<
Schema extends IContainEntitiesAndLinks<any, any>,
EntityName extends keyof Schema["entities"],
> = {
[LinkName in keyof Schema["entities"][EntityName]["links"]]?: Schema["entities"][EntityName]["links"][LinkName] extends LinkAttrDef<
infer Cardinality,
any
>
? Cardinality extends "one"
? string
: string | string[]
: never;
} & (Schema extends InstantGraph<any, any> ? {} : { [attribute: string]: any });

export interface TransactionChunk<
Schema extends IContainEntitiesAndLinks<any, any>,
EntityName extends keyof Schema["entities"],
Expand Down
44 changes: 36 additions & 8 deletions client/packages/core/src/schemaTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,21 +305,21 @@ export class InstantSchemaDef<

/**
* @deprecated
* `withRoomSchema` is deprecated. Define your schema in `rooms` directly:
*
* @example
* // Before:
* const schema = i.schema({
* `withRoomSchema` is deprecated. Define your schema in `rooms` directly:
*
* @example
* // Before:
* const schema = i.schema({
* // ...
* }).withRoomSchema<RoomSchema>()
*
*
* // After
* const schema = i.schema({
* rooms: {
* // ...
* }
* })
*
*
* @see https://instantdb.com/docs/presence-and-topics#typesafety
*/
withRoomSchema<_RoomSchema extends RoomSchemaShape>() {
Expand All @@ -335,7 +335,7 @@ export class InstantSchemaDef<
/**
* @deprecated
* `i.graph` is deprecated. Use `i.schema` instead.
*
*
* @see https://instantdb.com/docs/modeling-data
*/
export class InstantGraph<
Expand Down Expand Up @@ -457,3 +457,31 @@ export type InstantUnknownSchema = InstantSchemaDef<
UnknownLinks<UnknownEntities>,
UnknownRooms
>;

export type UpdateParams<
Schema extends IContainEntitiesAndLinks<any, any>,
EntityName extends keyof Schema["entities"],
> = {
[AttrName in keyof Schema["entities"][EntityName]["attrs"]]?: Schema["entities"][EntityName]["attrs"][AttrName] extends DataAttrDef<
infer ValueType,
infer IsRequired
>
? IsRequired extends true
? ValueType
: ValueType | null
: never;
};

export type LinkParams<
Schema extends IContainEntitiesAndLinks<any, any>,
EntityName extends keyof Schema["entities"],
> = {
[LinkName in keyof Schema["entities"][EntityName]["links"]]?: Schema["entities"][EntityName]["links"][LinkName] extends LinkAttrDef<
infer Cardinality,
any
>
? Cardinality extends "one"
? string
: string | string[]
: never;
};
4 changes: 4 additions & 0 deletions client/packages/react-native/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ import {
type InstantSchemaDef,
type InstantUnknownSchema,
type InstantRules,
type UpdateParams,
type LinkParams,
} from "@instantdb/core";

/**
Expand Down Expand Up @@ -147,4 +149,6 @@ export {
type InstantUnknownSchema,
type BackwardsCompatibleSchema,
type InstantRules,
type UpdateParams,
type LinkParams,
};
4 changes: 4 additions & 0 deletions client/packages/react/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ import {
type InstantSchemaDef,
type BackwardsCompatibleSchema,
type InstantRules,
type UpdateParams,
type LinkParams,
} from "@instantdb/core";

import InstantReactAbstractDatabase from "./InstantReactAbstractDatabase";
Expand Down Expand Up @@ -95,4 +97,6 @@ export {
type InstantSchemaDef,
type BackwardsCompatibleSchema,
type InstantRules,
type UpdateParams,
type LinkParams,
};
39 changes: 39 additions & 0 deletions client/sandbox/strong-init-vite/src/typescript_test_abstract.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { i, id, init, UpdateParams } from "@instantdb/admin";

const _schema = i.schema({ entities: { users: i.entity({ email: i.string() }) } });
type _AppSchema = typeof _schema;
interface AppSchema extends _AppSchema {}
const schema: AppSchema = _schema;

const db = init({
adminToken: "...",
appId: "...",
schema,
});

type Collection = keyof typeof schema.entities;

type EntityUpdate<T extends Collection> = UpdateParams<typeof schema, T>;

export const newEntity = async <T extends Collection>(
type: T,
props: EntityUpdate<T>,
) => {
const theId = id();
await db.transact(db.tx[type][theId].update(props));
return theId;
};

// seems good
const existing_attr_works = await newEntity("users", { email: "[email protected]" });

// @ts-expect-error
const non_existing_attr_errors = await newEntity("users", { blabla: "[email protected]" });

// @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;

0 comments on commit d36aaa3

Please sign in to comment.