Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[cli] Add a type helper to rules #557

Merged
merged 3 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions client/packages/admin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import {
type InstantUnknownSchema,
type InstaQLEntity,
type InstaQLResult,
type InstantRules,
} from "@instantdb/core";

import version from "./version";
Expand Down Expand Up @@ -931,4 +932,5 @@ export {
type InstantUnknownSchema,
type InstaQLEntity,
type InstaQLResult,
type InstantRules,
};
10 changes: 6 additions & 4 deletions client/packages/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,7 @@ async function pullSchema(appId, { pkgDir, instantModuleName }) {
return true;
}

async function pullPerms(appId, { pkgDir }) {
async function pullPerms(appId, { pkgDir, instantModuleName }) {
console.log("Pulling perms...");

const pullRes = await fetchJson({
Expand All @@ -742,7 +742,7 @@ async function pullPerms(appId, { pkgDir }) {
const permsPath = join(pkgDir, "instant.perms.ts");
await writeTypescript(
permsPath,
generatePermsTypescriptFile(pullRes.data.perms || {}),
generatePermsTypescriptFile(pullRes.data.perms || {}, instantModuleName),
"utf-8",
);

Expand Down Expand Up @@ -1478,7 +1478,7 @@ function appDashUrl(id) {
return `${instantDashOrigin}/dash?s=main&t=home&app=${id}`;
}

function generatePermsTypescriptFile(perms) {
function generatePermsTypescriptFile(perms, instantModuleName) {
const rulesTxt = Object.keys(perms).length
? JSON.stringify(perms, null, 2)
: `
Expand All @@ -1504,7 +1504,9 @@ function generatePermsTypescriptFile(perms) {
return `
// Docs: https://www.instantdb.com/docs/permissions

const rules = ${rulesTxt};
import { type InstantRules } from "${instantModuleName ?? "@instantdb/core"}";

const rules = ${rulesTxt} satisfies InstantRules;

export default rules;
`.trim();
Expand Down
14 changes: 14 additions & 0 deletions client/packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,19 @@ function init_experimental<
return client;
}

type InstantRules = {
[EntityName: string]: {
allow: {
view?: string;
create?: string;
update?: string;
delete?: string;
};
bind?: string[];
};
};


export {
// bada bing bada boom
init,
Expand Down Expand Up @@ -915,4 +928,5 @@ export {
type InstantUnknownSchema,
type IInstantDatabase,
type BackwardsCompatibleSchema,
type InstantRules,
};
2 changes: 2 additions & 0 deletions client/packages/react-native/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import {
type InstantConfig,
type InstantSchemaDef,
type InstantUnknownSchema,
type InstantRules,
} from "@instantdb/core";

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

import { InstantReact } from "./InstantReact";
Expand Down Expand Up @@ -94,4 +95,5 @@ export {
type InstantUnknownSchema,
type InstantSchemaDef,
type BackwardsCompatibleSchema,
type InstantRules,
};
6 changes: 5 additions & 1 deletion client/sandbox/strong-init-vite/instant.perms.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
// Docs: https://www.instantdb.com/docs/permissions

import { type InstantRules } from "@instantdb/react";

const rules = {
attrs: {
allow: {
create: "false",
},
},
};
} satisfies InstantRules;

export default rules;