Skip to content

Commit

Permalink
roar
Browse files Browse the repository at this point in the history
  • Loading branch information
stopachka committed Dec 22, 2024
1 parent 52aff81 commit 04a5ae1
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 25 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,
};
18 changes: 1 addition & 17 deletions client/packages/core/src/instatx.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import type {
DataAttrDef,
IContainEntitiesAndLinks,
InstantGraph,
LinkAttrDef,
LinkParams,
UpdateParams,
} from "./schemaTypes";

Expand All @@ -14,20 +12,6 @@ type LookupRef = [string, any];
type Lookup = string;
export type Op = [Action, EType, Id | LookupRef, Args];

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
14 changes: 14 additions & 0 deletions client/packages/core/src/schemaTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -466,3 +466,17 @@ export type UpdateParams<
Schema["entities"][EntityName]["attrs"][AttrName]
>;
};

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,
};
21 changes: 13 additions & 8 deletions client/sandbox/strong-init-vite/src/typescript_test_abstract.tsx
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;
Expand All @@ -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;

0 comments on commit 04a5ae1

Please sign in to comment.