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

feature: tenancy #51

Merged
merged 1 commit into from
Jul 26, 2024
Merged

feature: tenancy #51

merged 1 commit into from
Jul 26, 2024

Conversation

geclos
Copy link
Collaborator

@geclos geclos commented Jul 22, 2024

Refactor data accessors to repositories which enforce tenancy. There's two guiding principles:

  • All data access should happen via repositories so to ensure tenancy. In the cases that tenancy must not be enforced data accessors have been prepended with unsafely to make things clear
  • All data writes – mainly everything under services folder – should receive object instances instead of object ids. This is to ensure consumers have to enforce tenancy before writing to database.

@geclos geclos added the 🚧 wip Work in progress label Jul 22, 2024
async getProjectById(id: number) {
const result = await this.db
.select()
.from(this.scope)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

QUE NICE

@geclos geclos force-pushed the feature/tenancy branch from 91afaac to 63dda37 Compare July 26, 2024 11:12
const document = await getDocumentByPath({
commitId: input.commitId,
commit,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as a general rule make all db readers/writers receive objects instead of identifiers. This will force the consumer to ensure tenancy and will reduce the amount of redundants roundtrips to the database. It results in fatter controllers but that is ok, tenancy is ensured at the entrypoints of our backend.

@geclos geclos removed the 🚧 wip Work in progress label Jul 26, 2024
projectId: input.projectId,
workspaceId: ctx.workspace.id,
})
await projectScope.getProjectById(input.projectId)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice that this is a class call it find when is by ID

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤷🏼 can change if we prefer it

Comment on lines +18 to +22
.handler(async ({ input, ctx }) => {
const commitsScope = new CommitsRepository(ctx.project.workspaceId)
const commit = await commitsScope
.getCommitById(input.commitId)
.then((r) => r.unwrap())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to use the workspaceId to get the commitScope? Shouldn't we use the commitId instead?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nein, commitId alone is not scoped by tenant, you have to first scope by tenant (the workspace) before you can do any search of commits

@@ -60,7 +57,7 @@ export async function getCurrentUserFromDB({
}: {
userId: string | undefined
}): PromisedResult<SessionData, NotFoundError> {
const user = await getUser(userId)
const user = await unsafelyGetUser(userId)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this?

Copy link
Collaborator Author

@geclos geclos Jul 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the PR description:

All data access should happen via repositories so to ensure tenancy. In the cases that tenancy must not be enforced data accessors have been prepended with unsafely to make things clear

"eslint": "9.x"
"react": ">=18.x",
"react-dom": ">=18.x",
"eslint": ">=8.x"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

.where(eq(this.scope.id, documentId))

// NOTE: I hate this
const document = res[0]
Copy link
Contributor

@andresgutgon andresgutgon Jul 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this.db.documentVersions.findFirst({
  where: eq(this.scope.id, documentId)
})

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nein, that's one of the negative effects of using subqueries you cannot use drizzle query dsl

@geclos geclos merged commit c3bf7b1 into main Jul 26, 2024
2 checks passed
@geclos geclos deleted the feature/tenancy branch July 26, 2024 12:47
documentUuid: documentVersions.documentUuid,
mergedAt: max(commits.mergedAt).as('maxMergedAt'),
})
.from(documentVersions)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not using the scope no?

const documentsFromMergedCommits = await this.db
.with(lastVersionOfEachDocument)
.select(getTableColumns(documentVersions))
.from(documentVersions)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is also not using the scope I think

Copy link
Contributor

@andresgutgon andresgutgon Jul 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't fix it before I merge my PR. I'm sick of resolving conflicts 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants