Loading...
Loading...
Build complete, functional apps with InstantDB as the backend. Use when creating React/vanilla JS or expo applications. Triggers on requests for building apps.
npx skill4agent add instantdb/skills instantdb@instantdb/core@instantdb/react@instantdb/react-native@instantdb/admininstant.schema.tsinstant.perms.ts.envnpx instant-cli init-without-files --title <APP_NAME>npx instant-cli loginnpx instant-cli pull --yesinstant.schema.tsnpx instant-cli push schema --yesnpx instant-cli push schema --rename 'posts.author:posts.creator stores.owner:stores.manager' --yesinstant.perms.tsnpx instant-cli push perms --yesOrdering: order: { field: 'asc' | 'desc' }
Example: $: { order: { dueDate: 'asc' } }
Notes: - Field must be indexed + typed in schema
- Cannot order by nested attributes (e.g. 'owner.name')whereEquality: { field: value }
Inequality: { field: { $ne: value } }
Null checks: { field: { $isNull: true | false } }
Comparison: $gt, $lt, $gte, $lte (indexed + typed fields only)
Sets: { field: { $in: [v1, v2] } }
Substring: { field: { $like: 'Get%' } } // case-sensitive
{ field: { $ilike: '%get%' } } // case-insensitive
Logic: and: [ {...}, {...} ]
or: [ {...}, {...} ]
Nested fields: 'relation.field': valuewhere$exists$nin$regex$like$ilikestartsWithendsWithincludeslimitoffsetfirstafterlastbeforedata.ref("<path.to.attr>")auth.id in data.ref('post.author.id') // auth.id in list of author ids
data.ref('owner.id') == [] // there is no ownerauth.id in data.post.author.id
auth.id in data.ref('author')
data.ref('admins.id') == auth.id
auth.id == data.ref('owner.id')
data.ref('owner.id') == null
data.ref('owner.id').length > 0data.ref$user'admin' in auth.ref('$user.role.type')
auth.ref('$user.role.type')[0] == 'admin'auth.ref('role.type')
auth.ref('$user.role.type') == 'admin'newData.ref('x')
data.ref(someVar + '.members.id')viewauth.id == data.idcreateupdatedeleteviewupdatecreatedeletedata.ref$filesdata.path.startsWith(...)data.path.endsWith(...){
"$users": {
"allow": {
"view": "true"
},
"fields": {
"email": "auth.id == data.id"
}
}
}viewschemaschemaimport schema from '@/instant.schema`
// On client
import { init } from '@instantdb/react'; // or your relevant Instant SDK
const clientDb = init({ appId, schema });
// On backend
import { init } from '@instantdb/admin';
const adminDb = init({ appId, adminToken, schema });id()id()import { id } from '@instantdb/react'; // or your relevant Instant SDK
import { clientDb } from '@/lib/clientDb
clientDb.transact(clientDb.tx.todos[id()].create({ title: 'New Todo' }));import { AppSchema } from '@/instant.schema';
type Todo = InstaQLEntity<AppSchema, 'todos'>; // todo from clientDb.useQuery({ todos: {} })
type PostsWithProfile = InstaQLEntity<
AppSchema,
'posts',
{ author: { avatar: {} } }
>; // post from clientDb.useQuery({ posts: { author: { avatar: {} } } })db.useAuthdb.subscribeAuthimport { clientDb } from '@/lib/clientDb';
// For react/react-native apps use db.useAuth
function App() {
const { isLoading, user, error } = clientDb.useAuth();
if (isLoading) { return null; }
if (error) { return <Error message={error.message /}></div>; }
if (user) { return <Main />; }
return <Login />;
}
// For vanilla JS apps use db.subscribeAuth
function App() {
renderLoading();
db.subscribeAuth((auth) => {
if (auth.error) { renderAuthError(auth.error.message); }
else if (auth.user) { renderLoggedInPage(auth.user); }
else { renderSignInPage(); }
});
}@instantdb/admin// instant.schema.ts
const _schema = i.schema({
entities: {
$users: i.entity({
email: i.string().unique().indexed().optional(),
}),
profiles: i.entity({
displayName: i.string(),
}),
channels: i.entity({
name: i.string().indexed(),
}),
messages: i.entity({
content: i.string(),
timestamp: i.number().indexed(),
}),
},
links: {
userProfile: {
forward: { on: "profiles", has: "one", label: "user", onDelete: "cascade" }, // IMPORTANT: `cascade` can only be used in a has-one link
reverse: { on: "$users", has: "one", label: "profile" },
},
authorMessages: {
forward: { on: "messages", has: "one", label: "author", onDelete: "cascade" },
reverse: { on: "profiles", has: "many", label: "messages", },
},
channelMessages: {
forward: { on: "messages", has: "one", label: "channel", onDelete: "cascade" },
reverse: { on: "channels", has: "many", label: "messages" },
},
},
});
// scripts/seed.ts
import { id } from "@instantdb/admin";
import { adminDb } from "@/lib/adminDb";
const users: Record<string, User> = { ... }
const channels: Record<string, Channel> = { ... }
const mockMessages: Message[] = [ ... ]
function seed() {
console.log("Seeding db...");
const userTxs = Object.values(users).map(u => adminDb.tx.$users[u.id].create({}));
const profileTxs = Object.values(users).map(u => adminDb.tx.profiles[u.id].create({ displayName: u.displayName }).link({ user: u.id }));
const channelTxs = Object.values(channels).map(c => adminDb.tx.channels[c.id].create({ name: c.name }))
const messageTxs = mockMessages.map(m => {
const messageId = id();
return adminDb.tx.messages[messageId].create({
content: m.content,
timestamp: m.timestamp,
})
.link({ author: users[m.author].id })
.link({ channel: channels[m.channel].id });
})
adminDb.transact([...userTxs, ...profileTxs, ...channelTxs, ...messageTxs]);
}
seed();
// scripts/reset.ts
import { adminDb } from "@/lib/adminDb";
async function reset() {
console.log("Resetting database...");
const { $users, channels } = await adminDb.query({ $users: {}, channels: {} });
// Deleting all users will cascade delete profiles and messages
const userTxs = $users.map(user => adminDb.tx.$users[user.id].delete());
const channelTxs = channels.map(channel => adminDb.tx.channels[channel.id].delete());
adminDb.transact([...userTxs, ...channelTxs]);
}
reset();tsc --noEmit