Skip to content

Commit 158f057

Browse files
committed
ts index fix and canonical naming
1 parent a8308ad commit 158f057

5 files changed

Lines changed: 36 additions & 15 deletions

File tree

crates/bindings-typescript/src/lib/indexes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import type { ColumnIsUnique } from './constraints';
99
* existing column names are referenced.
1010
*/
1111
export type IndexOpts<AllowedCol extends string> = {
12-
name?: string;
12+
accessor?: string;
1313
} & (
1414
| { algorithm: 'btree'; columns: readonly AllowedCol[] }
1515
| { algorithm: 'hash'; columns: readonly AllowedCol[] }

crates/bindings-typescript/src/lib/schema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ export function tableToSchema<
8989

9090
type AllowedCol = keyof T['rowType']['row'] & string;
9191
return {
92-
sourceName: schema.tableName ?? accName,
92+
sourceName: accName,
9393
accessorName: toCamelCase(accName),
9494
columns: schema.rowType.row, // typed as T[i]['rowType']['row'] under TablesToSchema<T>
9595
rowType: schema.rowSpacetimeType,

crates/bindings-typescript/src/lib/table.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ export function table<Row extends RowObj, const Opts extends TableOpts<Row>>(
422422
// the name and accessor name of an index across all SDKs.
423423
indexes.push({
424424
sourceName: undefined,
425-
accessorName: indexOpts.name,
425+
accessorName: indexOpts.accessor,
426426
algorithm,
427427
});
428428
}
@@ -439,15 +439,6 @@ export function table<Row extends RowObj, const Opts extends TableOpts<Row>>(
439439
}
440440
}
441441

442-
for (const index of indexes) {
443-
const cols =
444-
index.algorithm.tag === 'Direct'
445-
? [index.algorithm.value]
446-
: index.algorithm.value;
447-
const colS = cols.map(i => colNameList[i]).join('_');
448-
index.sourceName = `${name}_${colS}_idx_${index.algorithm.tag.toLowerCase()}`;
449-
}
450-
451442
const productType = row.algebraicType.value as RowBuilder<
452443
CoerceRow<Row>
453444
>['algebraicType']['value'];
@@ -466,8 +457,20 @@ export function table<Row extends RowObj, const Opts extends TableOpts<Row>>(
466457
if (row.typeName === undefined) {
467458
row.typeName = toPascalCase(tableName);
468459
}
460+
461+
// Build index source names using accName
462+
for (const index of indexes) {
463+
const cols =
464+
index.algorithm.tag === 'Direct'
465+
? [index.algorithm.value]
466+
: index.algorithm.value;
467+
468+
const colS = cols.map(i => colNameList[i]).join('_');
469+
index.sourceName = `${accName}_${colS}_idx_${index.algorithm.tag.toLowerCase()}`;
470+
}
471+
469472
return {
470-
sourceName: tableName,
473+
sourceName: accName,
471474
productTypeRef: ctx.registerTypesRecursively(row).ref,
472475
primaryKey: pk,
473476
indexes,

crates/bindings-typescript/src/server/schema.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,15 @@ export function schema<const H extends Record<string, UntypedTableSchema>>(
540540
tableName: tableDef.sourceName,
541541
});
542542
}
543+
if (table.tableName) {
544+
ctx.moduleDef.explicitNames.entries.push ({
545+
tag: "Table",
546+
value: {
547+
sourceName: accName,
548+
canonicalName: table.tableName,
549+
}
550+
})
551+
}
543552
}
544553
return { tables: tableSchemas } as TablesToSchema<H>;
545554
});

crates/bindings-typescript/src/server/views.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,6 @@ export function registerView<
143143
? AnonymousViewFn<S, Params, Ret>
144144
: ViewFn<S, Params, Ret>
145145
) {
146-
const name = opts.name ?? exportName;
147146
const paramsBuilder = new RowBuilder(params, toPascalCase(name));
148147

149148
// Register return types if they are product types
@@ -156,14 +155,24 @@ export function registerView<
156155
);
157156

158157
ctx.moduleDef.views.push({
159-
sourceName: name,
158+
sourceName: exportName,
160159
index: (anon ? ctx.anonViews : ctx.views).length,
161160
isPublic: opts.public,
162161
isAnonymous: anon,
163162
params: paramType,
164163
returnType,
165164
});
166165

166+
if (opts.name != null) {
167+
ctx.moduleDef.explicitNames.entries.push({
168+
tag: "Function",
169+
value: {
170+
sourceName: exportName,
171+
canonicalName: opts.name
172+
}
173+
})
174+
}
175+
167176
// If it is an option, we wrap the function to make the return look like an array.
168177
if (returnType.tag == 'Sum') {
169178
const originalFn = fn;

0 commit comments

Comments
 (0)