Skip to content

Commit

Permalink
[strong-init] make links and rooms optional in schema
Browse files Browse the repository at this point in the history
  • Loading branch information
stopachka committed Dec 18, 2024
1 parent 9a0ccbc commit 0986390
Show file tree
Hide file tree
Showing 25 changed files with 31 additions and 67 deletions.
10 changes: 0 additions & 10 deletions client/packages/core/__tests__/src/instaml.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,6 @@ test("Schema: uses info in `attrs` and `links`", () => {
},
},
},
rooms: {},
});

const commentId = uuid();
Expand Down Expand Up @@ -814,7 +813,6 @@ test("Schema: doesn't create duplicate ref attrs", () => {
},
},
},
rooms: {},
});

const commentId = uuid();
Expand Down Expand Up @@ -870,8 +868,6 @@ test("Schema: lookup creates unique attrs for custom lookups", () => {
nickname: i.string().unique().indexed(),
}),
},
links: {},
rooms: {},
});

const ops = instatx.tx.users[instatx.lookup("nickname", "stopanator")].update(
Expand Down Expand Up @@ -933,7 +929,6 @@ test("Schema: lookup creates unique attrs for lookups in link values", () => {
},
},
},
rooms: {},
});

const uid = uuid();
Expand Down Expand Up @@ -1015,7 +1010,6 @@ test("Schema: lookup creates unique attrs for lookups in link values with arrays
},
},
},
rooms: {},
});

const uid = uuid();
Expand Down Expand Up @@ -1108,7 +1102,6 @@ test("Schema: lookup creates unique ref attrs for ref lookup", () => {
},
},
},
rooms: {},
});

const uid = uuid();
Expand Down Expand Up @@ -1192,7 +1185,6 @@ test("Schema: lookup creates unique ref attrs for ref lookup in link value", ()
},
},
},
rooms: {},
});
const uid = uuid();
const ops = [
Expand Down Expand Up @@ -1257,8 +1249,6 @@ test("Schema: populates checked-data-type", () => {
j: i.json(),
}),
},
links: {},
rooms: {},
});

const commentId = uuid();
Expand Down
3 changes: 0 additions & 3 deletions client/packages/core/__tests__/src/instaql.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -866,7 +866,6 @@ test("arbitrary ordering with dates", () => {
num: i.number().indexed(),
}),
},
links: {},
});

const txSteps = [];
Expand Down Expand Up @@ -1085,8 +1084,6 @@ test("comparators", () => {
boolean: i.boolean().indexed(),
}),
},
links: {},
rooms: {},
});

const txSteps = [];
Expand Down
2 changes: 0 additions & 2 deletions client/packages/core/__tests__/src/instaqlInference.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ test("many-to-many with inference", () => {
reverse: { on: "tags", has: "many", label: "posts" },
},
},
rooms: {},
});

const ids = {
Expand Down Expand Up @@ -94,7 +93,6 @@ test("one-to-one with inference", () => {
reverse: { on: "profiles", has: "one", label: "user" },
},
},
rooms: {},
});

const ids = {
Expand Down
1 change: 0 additions & 1 deletion client/packages/core/__tests__/src/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ test("runs without exception", () => {
},
},
},
rooms: {},
});

const demoQuery = {
Expand Down
12 changes: 7 additions & 5 deletions client/packages/core/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,18 +161,20 @@ function schema<
rooms,
}: {
entities: EntitiesWithoutLinks;
links: Links;
rooms: Rooms;
links?: Links;
rooms?: Rooms;
}) {
const linksDef = links ?? {} as Links;
const roomsDef = rooms ?? {} as Rooms;
return new InstantSchemaDef(
enrichEntitiesWithLinks<EntitiesWithoutLinks, Links>(entities, links),
enrichEntitiesWithLinks<EntitiesWithoutLinks, Links>(entities, linksDef),
// (XXX): LinksDef<any> stems from TypeScript’s inability to reconcile the
// type EntitiesWithLinks<EntitiesWithoutLinks, Links> with
// EntitiesWithoutLinks. TypeScript is strict about ensuring that types are
// correctly aligned and does not allow for substituting a type that might
// be broader or have additional properties.
links as LinksDef<any>,
rooms,
linksDef as LinksDef<any>,
roomsDef,
);
}

Expand Down
1 change: 0 additions & 1 deletion client/sandbox/admin-sdk-express/src/with-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ const schema = i.schema({
},
},
},
rooms: {},
});

const db = init({
Expand Down
2 changes: 0 additions & 2 deletions client/sandbox/react-native-expo/app/play/colors-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ const schema = i.schema({
color: i.string(),
}),
},
links: {},
rooms: {},
});

const { useQuery, transact, tx } = init({
Expand Down
4 changes: 1 addition & 3 deletions client/sandbox/react-nextjs/pages/play/color.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import config from "../../config";
const schema = i.schema({
entities: {
colors: i.entity({ color: i.string() }),
},
links: {},
rooms: {},
}
});

const db = init({ ...config, schema });
Expand Down
1 change: 0 additions & 1 deletion client/sandbox/react-nextjs/pages/play/ephemeral.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ const name = `user ${Date.now()}`;

const schema = i.schema({
entities: {},
links: {},
rooms: {
"demo-room": {
presence: i.entity({
Expand Down
1 change: 0 additions & 1 deletion client/sandbox/react-nextjs/pages/play/missing-attrs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ const schema = i.schema({
},
},
},
rooms: {},
});

function Example({ appId, useSchema }: { appId: string; useSchema: boolean }) {
Expand Down
1 change: 0 additions & 1 deletion client/sandbox/react-nextjs/pages/play/operators.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ const schema = i.schema({
},
},
},
rooms: {},
});

function randInt(max: number) {
Expand Down
1 change: 0 additions & 1 deletion client/sandbox/react-nextjs/pages/play/query-like.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ const schema = i.schema({
},
},
},
rooms: {},
});

function Example({ appId }: { appId: string }) {
Expand Down
32 changes: 18 additions & 14 deletions client/sandbox/react-nextjs/pages/play/query-once-toggle.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/*
* There was a bug where if we toggled connections on and off,
* we could enter a state where multiple _ws instances were active.
*
* This led to a weird bug where queryOnce would hang. Use this
* playground if you ever want to reproduce the bug.
*
*
*/
* There was a bug where if we toggled connections on and off,
* we could enter a state where multiple _ws instances were active.
*
* This led to a weird bug where queryOnce would hang. Use this
* playground if you ever want to reproduce the bug.
*
*
*/

import { init, i } from "@instantdb/react";
import Head from "next/head";
Expand All @@ -18,13 +18,11 @@ const schema = i.schema({
onceTest: i.entity({ text: i.string() }),
posts: i.entity({ title: i.string() }),
},
links: {},
rooms: {},
});

const db = init({
...config,
schema
schema,
});

function useEffectOnce(cb: () => void) {
Expand All @@ -42,18 +40,24 @@ function Main() {
setTimeout(() => {
const p1 = db.queryOnce({ posts: {} });
window.dispatchEvent(new Event("offline"));
const p2 = db.queryOnce({ posts: {} }).catch(e => console.log('expected fail, we are offline'));
const p2 = db
.queryOnce({ posts: {} })
.catch((e) => console.log("expected fail, we are offline"));
window.dispatchEvent(new Event("online"));
window.dispatchEvent(new Event("offline"));
window.dispatchEvent(new Event("online"));
const p3 = db.queryOnce({ posts: {} });
Promise.all([p1, p3]).then(
(succ) => {
console.log('Check inspector; how many active ws connections do you see?')
console.log(
"Check inspector; how many active ws connections do you see?",
);
setResult({ succ });
},
(err) => {
console.error('Uh oh, we should definitely have gotten a response here');
console.error(
"Uh oh, we should definitely have gotten a response here",
);
setResult({ err });
},
);
Expand Down
4 changes: 1 addition & 3 deletions client/sandbox/react-nextjs/pages/play/query-once.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ import config from "../../config";
const schema = i.schema({
entities: {
onceTest: i.entity({ text: i.string() }),
},
links: {},
rooms: {},
}
});

const db = init({...config, schema});
Expand Down
1 change: 0 additions & 1 deletion client/sandbox/react-nextjs/pages/play/strong-todos.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ const _schema = i.schema({
},
},
},
rooms: {},
});
// This helps Typescript display nicer intellisense
type _AppSchema = typeof _schema;
Expand Down
3 changes: 1 addition & 2 deletions client/sandbox/vanilla-js-vite/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ const db = init({
title: i.string(),
}),
},
links: {},
rooms: {},

}),
});

Expand Down
2 changes: 0 additions & 2 deletions client/www/pages/docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ const schema = i.schema({
createdAt: i.number(),
}),
},
links: {},
rooms: {},
});

type Todo = InstaQLEntity<typeof schema, "todos">;
Expand Down
8 changes: 3 additions & 5 deletions client/www/pages/docs/init.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ The first step to using Instant in your app is to call `init` before rendering y
import { init } from '@instantdb/react';

// Instant app
const APP_ID = "__APP_ID__";
const APP_ID = '__APP_ID__';

const db = init({ appId: APP_ID });

Expand All @@ -27,7 +27,7 @@ If you're using typescript, `init` accepts a `schema` argument. Adding a schema
import { init, i } from '@instantdb/react';

// Instant app
const APP_ID = "__APP_ID__";
const APP_ID = '__APP_ID__';

const schema = i.schema({
entities: {
Expand All @@ -37,11 +37,9 @@ const schema = i.schema({
createdAt: i.number(),
}),
},
links: {},
rooms: {},
});

const db = init({ appId: APP_ID, schema });
```

To learn more about writing schemas, head on over to the [Modeling your data](/docs/modeling-data) section.
To learn more about writing schemas, head on over to the [Modeling your data](/docs/modeling-data) section.
1 change: 0 additions & 1 deletion client/www/pages/docs/instaql.md
Original file line number Diff line number Diff line change
Expand Up @@ -1056,7 +1056,6 @@ const _schema = i.schema({
reverse: { on: 'goals', has: 'many', label: 'todos' },
},
},
rooms: {},
});

// This helps Typescript display better intellisense
Expand Down
1 change: 0 additions & 1 deletion client/www/pages/docs/modeling-data.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ const _schema = i.schema({
reverse: { on: "$users", has: "one", label: "profile" },
},
},
rooms: {},
});

// This helps Typescript display better intellisense
Expand Down
1 change: 0 additions & 1 deletion client/www/pages/docs/patterns.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ const _schema = i.schema({
},
},
},
rooms: {},
});

// This helps Typescript display nicer intellisense
Expand Down
2 changes: 0 additions & 2 deletions client/www/pages/docs/start-rn.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ const schema = i.schema({
value: i.string(),
}),
},
links: {},
rooms: {},
});

type Color = InstaQLEntity<typeof schema, "colors">;
Expand Down
2 changes: 0 additions & 2 deletions client/www/pages/docs/start-vanilla.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ const schema = i.schema({
createdAt: i.date(),
}),
},
links: {},
rooms: {},
});

type Todo = InstaQLEntity<typeof schema, "todos">;
Expand Down
1 change: 0 additions & 1 deletion client/www/pages/docs/users.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ const _schema = i.schema({
reverse: { on: '$users', has: 'one', label: 'profile' },
},
},
rooms: {},
});

// This helps Typescript display nicer intellisense
Expand Down
1 change: 0 additions & 1 deletion client/www/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ const appId = 'fc5a4977-910a-43d9-ac28-39c7837c1eb5';

const schema = i.schema({
entities: {},
links: {},
rooms: {
landing: {
presence: i.entity({}),
Expand Down

0 comments on commit 0986390

Please sign in to comment.