-
Notifications
You must be signed in to change notification settings - Fork 60
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
feature: tenancy #51
Conversation
async getProjectById(id: number) { | ||
const result = await this.db | ||
.select() | ||
.from(this.scope) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
QUE NICE
1ba3456
to
91afaac
Compare
const document = await getDocumentByPath({ | ||
commitId: input.commitId, | ||
commit, |
There was a problem hiding this comment.
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.
projectId: input.projectId, | ||
workspaceId: ctx.workspace.id, | ||
}) | ||
await projectScope.getProjectById(input.projectId) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
.handler(async ({ input, ctx }) => { | ||
const commitsScope = new CommitsRepository(ctx.project.workspaceId) | ||
const commit = await commitsScope | ||
.getCommitById(input.commitId) | ||
.then((r) => r.unwrap()) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why this?
There was a problem hiding this comment.
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" |
There was a problem hiding this comment.
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] |
There was a problem hiding this comment.
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)
})
There was a problem hiding this comment.
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
documentUuid: documentVersions.documentUuid, | ||
mergedAt: max(commits.mergedAt).as('maxMergedAt'), | ||
}) | ||
.from(documentVersions) |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 🙏
Refactor data accessors to repositories which enforce tenancy. There's two guiding principles:
unsafely
to make things clearservices
folder – should receive object instances instead of object ids. This is to ensure consumers have to enforce tenancy before writing to database.