Skip to content

Commit

Permalink
Merge pull request #2968 from SeedCompany/edgedb/qb-implicit-type-or-…
Browse files Browse the repository at this point in the history
…module

EdgeDB QB implicit default type or named module
  • Loading branch information
CarsonF authored Nov 20, 2023
2 parents 9f47163 + 12637e7 commit b58306d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class AuthenticationEdgeDBRepository
}

async getRootUserId() {
const query = e.assert_exists(e.select(e.default.RootUser).assert_single());
const query = e.assert_exists(e.select(e.RootUser).assert_single());
const result = await this.db.run(query);
return result.id;
}
Expand All @@ -31,7 +31,7 @@ export class AuthenticationEdgeDBRepository
}

async savePasswordHashOnUser(userId: ID, passwordHash: string) {
const user = e.select(e.default.User, () => ({
const user = e.select(e.User, () => ({
filter_single: { id: userId },
}));
const query = e
Expand Down Expand Up @@ -61,7 +61,7 @@ export class AuthenticationEdgeDBRepository
async connectSessionToUser(input: LoginInput, session: Session): Promise<ID> {
const user = e.assert_exists(
{ message: 'User not found' },
e.select(e.default.User, () => ({
e.select(e.User, () => ({
filter_single: { email: input.email },
})),
);
Expand Down Expand Up @@ -101,7 +101,7 @@ export class AuthenticationEdgeDBRepository
scopedRoles: withScope('global', user.roles),
}),
impersonatee: e.assert_single(
e.select(e.default.User, (user) => ({
e.select(e.User, (user) => ({
scopedRoles: withScope('global', user.roles),
filter: e.op(user.id, '=', impersonateeId ?? e.cast(e.uuid, e.set())),
})),
Expand All @@ -116,7 +116,7 @@ export class AuthenticationEdgeDBRepository
}

async rolesForUser(userId: ID) {
const query = e.select(e.default.User, (user) => ({
const query = e.select(e.User, (user) => ({
scopedRoles: withScope('global', user.roles),
filter_single: { id: userId },
}));
Expand Down Expand Up @@ -145,7 +145,7 @@ export class AuthenticationEdgeDBRepository
}

async userByEmail(email: string) {
const query = e.select(e.default.User, () => ({
const query = e.select(e.User, () => ({
filter_single: { email },
}));
const result = await this.db.run(query);
Expand Down
20 changes: 10 additions & 10 deletions src/components/user/user.edgedb.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { e, EdgeDB, isExclusivityViolation } from '~/core/edgedb';
import { CreatePerson, User, UserListInput } from './dto';
import { UserRepository } from './user.repository';

const hydrate = e.shape(e.default.User, (user) => ({
const hydrate = e.shape(e.User, (user) => ({
...user['*'],
// Other links if needed
}));
Expand All @@ -23,7 +23,7 @@ export class UserEdgedbRepository extends UserRepository {
}

async readOne(id: ID, _session: Session | ID) {
const query = e.select(e.default.User, (user) => ({
const query = e.select(e.User, (user) => ({
...hydrate(user),
filter_single: { id },
}));
Expand All @@ -36,7 +36,7 @@ export class UserEdgedbRepository extends UserRepository {

async readMany(ids: readonly ID[], _session: Session | ID) {
const query = e.params({ ids: e.array(e.uuid) }, ({ ids }) =>
e.select(e.default.User, (user) => ({
e.select(e.User, (user) => ({
...hydrate(user),
filter: e.op(user.id, 'in', e.array_unpack(ids)),
})),
Expand All @@ -46,16 +46,16 @@ export class UserEdgedbRepository extends UserRepository {
}

async doesEmailAddressExist(email: string) {
const query = e.select(e.default.User, () => ({
const query = e.select(e.User, () => ({
filter_single: { email },
}));
const result = await this.edgedb.run(query);
return !!result;
}

async list(input: UserListInput, _session: Session) {
const sortKey = input.sort as keyof (typeof e.default.User)['*'];
const all = e.select(e.default.User, (user) => ({
const sortKey = input.sort as keyof (typeof e.User)['*'];
const all = e.select(e.User, (user) => ({
filter: e.all(
input.filter.pinned != null
? e.op(user.pinned, '=', input.filter.pinned)
Expand Down Expand Up @@ -83,7 +83,7 @@ export class UserEdgedbRepository extends UserRepository {
}

async create(input: CreatePerson) {
const query = e.insert(e.default.User, { ...input });
const query = e.insert(e.User, { ...input });
try {
const result = await this.edgedb.run(query);
return result.id;
Expand All @@ -103,7 +103,7 @@ export class UserEdgedbRepository extends UserRepository {
user: User,
email: string | null | undefined,
): Promise<void> {
const query = e.update(e.default.User, () => ({
const query = e.update(e.User, () => ({
filter_single: { id: user.id },
set: { email },
}));
Expand All @@ -122,15 +122,15 @@ export class UserEdgedbRepository extends UserRepository {
}

async updateRoles(user: User, roles: Role[]): Promise<void> {
const query = e.update(e.default.User, () => ({
const query = e.update(e.User, () => ({
filter_single: { id: user.id },
set: { roles },
}));
await this.edgedb.run(query);
}

async delete(id: ID, _session: Session, _object: User): Promise<void> {
const query = e.delete(e.default.User, () => ({
const query = e.delete(e.User, () => ({
filter_single: { id },
}));
try {
Expand Down
27 changes: 27 additions & 0 deletions src/core/edgedb/generator/query-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export async function generateQueryBuilder({
changeCustomScalars(qbDir);
changeImplicitIDType(qbDir);
allowOrderingByEnums(qbDir);
mergeDefaultTypesWithModuleNames(qbDir);
}

function addJsExtensionDeepPathsOfEdgedbLibrary(qbDir: Directory) {
Expand Down Expand Up @@ -72,3 +73,29 @@ function allowOrderingByEnums(qbDir: Directory) {
.setType('TypeSet<EnumType | ScalarType | ObjectType>');
file.fixMissingImports();
}

function mergeDefaultTypesWithModuleNames(qbDir: Directory) {
const file = qbDir.getSourceFileOrThrow('index.ts');
const st = file.getVariableDeclarationOrThrow('ExportDefault');
const typeText = st.getTypeNodeOrThrow().getFullText();
const value = st.getInitializerIfKindOrThrow(
SyntaxKind.ObjectLiteralExpression,
);
// Regex is faster here than ts-morph type parsing
const conflicting = (
typeText.match(/Omit<typeof _default, (.+)>/)?.[1].split(/ \| /g) ?? []
).map((s) => s.slice(1, -1));
let newTypeText = typeText;
for (const name of conflicting) {
newTypeText = newTypeText.replace(
`typeof _${name}`,
`typeof _${name} & typeof _default.${name}`,
);
value
.getPropertyOrThrow(`"${name}"`)
.asKindOrThrow(SyntaxKind.PropertyAssignment)
.getInitializerOrThrow()
.replaceWithText(`{ ..._${name}, ..._default.${name} }`);
}
st.setType(newTypeText);
}

0 comments on commit b58306d

Please sign in to comment.