From c0bbf38e680dfddb318a1e8e1ace4aca61572dfc Mon Sep 17 00:00:00 2001 From: stopachka Date: Tue, 26 Nov 2024 15:45:06 -0800 Subject: [PATCH 1/3] roar --- client/packages/core/src/index.ts | 14 ++++++++++++++ client/sandbox/strong-init-vite/instant.perms.ts | 15 ++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/client/packages/core/src/index.ts b/client/packages/core/src/index.ts index 21b8d5345..0d383b5c4 100644 --- a/client/packages/core/src/index.ts +++ b/client/packages/core/src/index.ts @@ -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, @@ -915,4 +928,5 @@ export { type InstantUnknownSchema, type IInstantDatabase, type BackwardsCompatibleSchema, + type InstantRules, }; diff --git a/client/sandbox/strong-init-vite/instant.perms.ts b/client/sandbox/strong-init-vite/instant.perms.ts index ce41942a0..7f1482c70 100644 --- a/client/sandbox/strong-init-vite/instant.perms.ts +++ b/client/sandbox/strong-init-vite/instant.perms.ts @@ -1,9 +1,22 @@ +type InstantRules = { + [EntityName: string]: { + allow: { + view?: string; + create?: string; + update?: string; + delete?: string; + }; + bind?: string[]; + }; +}; + const rules = { attrs: { allow: { create: "false", }, + bind: ["admin", "foo", "bar", "biz"], }, -}; +} satisfies InstantRules; export default rules; From 2b7baf497a981c5684b9ea5b2831de3eba9a8d93 Mon Sep 17 00:00:00 2001 From: stopachka Date: Tue, 26 Nov 2024 15:50:22 -0800 Subject: [PATCH 2/3] roar --- client/packages/admin/src/index.ts | 2 ++ client/packages/cli/index.js | 10 +++--- client/packages/react-native/src/index.ts | 2 ++ client/packages/react/src/index.ts | 2 ++ .../sandbox/strong-init-vite/instant.perms.ts | 36 ++++++++++++------- 5 files changed, 36 insertions(+), 16 deletions(-) diff --git a/client/packages/admin/src/index.ts b/client/packages/admin/src/index.ts index aed6d94b7..bb5851475 100644 --- a/client/packages/admin/src/index.ts +++ b/client/packages/admin/src/index.ts @@ -44,6 +44,7 @@ import { type InstantUnknownSchema, type InstaQLEntity, type InstaQLResult, + type InstantRules, } from "@instantdb/core"; import version from "./version"; @@ -931,4 +932,5 @@ export { type InstantUnknownSchema, type InstaQLEntity, type InstaQLResult, + type InstantRules, }; diff --git a/client/packages/cli/index.js b/client/packages/cli/index.js index 90c52365a..9ad29d606 100644 --- a/client/packages/cli/index.js +++ b/client/packages/cli/index.js @@ -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({ @@ -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", ); @@ -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) : ` @@ -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(); diff --git a/client/packages/react-native/src/index.ts b/client/packages/react-native/src/index.ts index de73b7b79..40e82689a 100644 --- a/client/packages/react-native/src/index.ts +++ b/client/packages/react-native/src/index.ts @@ -50,6 +50,7 @@ import { type InstantConfig, type InstantSchemaDef, type InstantUnknownSchema, + type InstantRules, } from "@instantdb/core"; /** @@ -146,4 +147,5 @@ export { type InstantSchemaDef, type InstantUnknownSchema, type BackwardsCompatibleSchema, + type InstantRules, }; diff --git a/client/packages/react/src/index.ts b/client/packages/react/src/index.ts index 18c22f0ee..370e2d8f0 100644 --- a/client/packages/react/src/index.ts +++ b/client/packages/react/src/index.ts @@ -37,6 +37,7 @@ import { type InstantUnknownSchema, type InstantSchemaDef, type BackwardsCompatibleSchema, + type InstantRules, } from "@instantdb/core"; import { InstantReact } from "./InstantReact"; @@ -94,4 +95,5 @@ export { type InstantUnknownSchema, type InstantSchemaDef, type BackwardsCompatibleSchema, + type InstantRules, }; diff --git a/client/sandbox/strong-init-vite/instant.perms.ts b/client/sandbox/strong-init-vite/instant.perms.ts index 7f1482c70..998256260 100644 --- a/client/sandbox/strong-init-vite/instant.perms.ts +++ b/client/sandbox/strong-init-vite/instant.perms.ts @@ -1,21 +1,33 @@ -type InstantRules = { - [EntityName: string]: { - allow: { - view?: string; - create?: string; - update?: string; - delete?: string; - }; - bind?: string[]; - }; -}; +// Docs: https://www.instantdb.com/docs/permissions + +import { type InstantRules } from "@instantdb/react"; const rules = { attrs: { allow: { create: "false", }, - bind: ["admin", "foo", "bar", "biz"], + }, + posts: { + bind: [ + "isAdmin", + "auth.email == 'stepan.p@gmail.com'", + "isOwner", + "auth.uid == data.author", + ], + allow: { + create: "isAdmin || isOwner", + delete: "isAdmin || isOwner", + update: "isAdmin || isOwner", + }, + }, + postBodies: { + bind: ["isAdmin", "auth.email == 'stepan.p@gmail.com'"], + allow: { + create: "isAdmin", + delete: "isAdmin", + update: "isAdmin", + }, }, } satisfies InstantRules; From a0f623c38f119ee4ea26bbd7a6cb5d240f2ebf8b Mon Sep 17 00:00:00 2001 From: stopachka Date: Tue, 26 Nov 2024 16:42:03 -0800 Subject: [PATCH 3/3] roar --- .../sandbox/strong-init-vite/instant.perms.ts | 21 ------------------- 1 file changed, 21 deletions(-) diff --git a/client/sandbox/strong-init-vite/instant.perms.ts b/client/sandbox/strong-init-vite/instant.perms.ts index 998256260..0474d6af9 100644 --- a/client/sandbox/strong-init-vite/instant.perms.ts +++ b/client/sandbox/strong-init-vite/instant.perms.ts @@ -8,27 +8,6 @@ const rules = { create: "false", }, }, - posts: { - bind: [ - "isAdmin", - "auth.email == 'stepan.p@gmail.com'", - "isOwner", - "auth.uid == data.author", - ], - allow: { - create: "isAdmin || isOwner", - delete: "isAdmin || isOwner", - update: "isAdmin || isOwner", - }, - }, - postBodies: { - bind: ["isAdmin", "auth.email == 'stepan.p@gmail.com'"], - allow: { - create: "isAdmin", - delete: "isAdmin", - update: "isAdmin", - }, - }, } satisfies InstantRules; export default rules;