Skip to content
Open
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
8 changes: 5 additions & 3 deletions crates/bindings-typescript/src/server/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -594,15 +594,17 @@ class ModuleHooksImpl implements ModuleHooks {
));
}

/** Query builder scoped to the submodule's own tables, with namespace-prefixed
* source names so generated SQL targets the mounted table names. */
/** Query builder scoped to the submodule's own tables. Like the db view, it
* names each table by accessor path (`namePrefix` holds accessor keys): the
* host resolves that alias, but not an accessor namespace joined to a
* table's explicit canonical name. */
#getSubmoduleQueryBuilder(submoduleIdx: number): QueryBuilder<any> {
const m = this.#flatSubmodules[submoduleIdx];
return (m.queryBuilder_ ??= makeQueryBuilder({
tables: Object.fromEntries(
Object.entries(m.schemaTables).map(([key, def]) => [
key,
{ ...def, sourceName: m.namePrefix + def.sourceName },
{ ...def, sourceName: m.namePrefix + def.accessorName },
])
),
}));
Expand Down
27 changes: 27 additions & 0 deletions crates/bindings-typescript/tests/ctx_as.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ describe('ctx.as alias proxy', () => {
let schema: typeof import('../src/server/schema').schema;
let table: typeof import('../src/lib/table').table;
let t: typeof import('../src/lib/type_builders').t;
let toSql: typeof import('../src/lib/query').toSql;
let moduleHooks: symbol;

beforeAll(async () => {
({ schema } = await import('../src/server/schema'));
({ table } = await import('../src/lib/table'));
({ t } = await import('../src/lib/type_builders'));
({ toSql } = await import('../src/lib/query'));
({ moduleHooks } = (await import('spacetime:sys@2.0')) as any);
});

Expand Down Expand Up @@ -129,4 +131,29 @@ describe('ctx.as alias proxy', () => {
expect(capturedCtx.as.myauth.as.baz.db.bazTable).toBeDefined();
expect(capturedCtx.as.myauth.as.baz.sender).toBe(capturedCtx.sender);
});

it('ctx.from in a submodule view names an explicitly named table by its accessor', async () => {
const sessions = table(
{ name: 'auth_sessions', public: true },
{ id: t.u64().primaryKey().autoInc() }
);
const authSchema = schema({ sessions });
let sql: string | undefined;
const allSessions = authSchema.anonymousView(
{ public: true },
t.array(sessions.rowType),
ctx => {
sql = toSql(ctx.from.sessions);
return ctx.from.sessions;
}
);
const consumer = schema({ myAuth: { default: authSchema, allSessions } });

const hooks = (consumer as any)[moduleHooks]({});
hooks.__call_view_anon__(0, new Uint8Array());

// The host resolves `myAuth.sessions` (accessors) and the canonical
// `my_auth.auth_sessions`, but not a mix of the two.
expect(sql).toBe('SELECT * FROM "myAuth"."sessions"');
});
});