Skip to content
Merged
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
16 changes: 6 additions & 10 deletions packages/cli-server/src/source/effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,14 @@ export async function runCliLoadSourceEffect(input: {
}): Promise<CliLoadSourceEffectResult> {
const sourceConditions = [
eq(dataSources.organizationId, input.effect.organizationId),
eq(dataSources.name, input.effect.sourceKey),
];
if (input.effect.sourceProvider) {
sourceConditions.push(
eq(dataSources.provider, input.effect.sourceProvider)
);
}

const row = await input.db.query.dataSources.findFirst({
const rows = await input.db.query.dataSources.findMany({
columns: {
id: true,
name: true,
Expand All @@ -69,22 +68,19 @@ export async function runCliLoadSourceEffect(input: {
where: and(...sourceConditions),
});

if (!row) {
return {
kind: "not_found",
};
}
const row = rows
.map((candidate) => createCliQuerySourceRecord(candidate))
.find((source) => source?.sourceKey === input.effect.sourceKey);

const source = createCliQuerySourceRecord(row);
if (!source) {
if (!row) {
return {
kind: "not_found",
};
}

return {
kind: "found",
source,
source: row,
};
}

Expand Down
4 changes: 4 additions & 0 deletions packages/cli-server/src/source/model.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ describe("source model", () => {
expect(createCliSourceKey(" .. ")).toBeNull();
});

it("derives safe source keys from display-style connection names", () => {
expect(createCliSourceKey("Slack - wordbricks")).toBe("slack-wordbricks");
});

it("exposes BigQuery as both query and API capable", () => {
expect(getCliSourceInterfaceTypes("bigquery")).toEqual(["query", "api"]);
});
Expand Down
24 changes: 21 additions & 3 deletions packages/cli-server/src/source/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,33 @@ import { isCliSourceKey } from "../identifiers";

type CliSourceInterfaceType = "query" | "api";

const CLI_SOURCE_KEY_NORMALIZATION_PATTERN = /[^a-z0-9._-]+/g;
const CLI_SOURCE_KEY_BOUNDARY_PATTERN = /^[._-]+|[._-]+$/g;
const CLI_SOURCE_KEY_REPEAT_PATTERN = /[._-]{2,}/g;

function normalizeCliSourceKey(name: string): string | null {
const normalized = name
.trim()
.toLowerCase()
.replaceAll(CLI_SOURCE_KEY_NORMALIZATION_PATTERN, "-")
.replaceAll(CLI_SOURCE_KEY_BOUNDARY_PATTERN, "")
.replaceAll(CLI_SOURCE_KEY_REPEAT_PATTERN, "-");

return normalized.length > 0 && isCliSourceKey(normalized)
? normalized
: null;
}

// The backing table still stores the CLI-visible source identity in
// data_sources.name. The CLI domain treats that normalized org-unique name as
// the canonical sourceKey so the rest of the workflow code never reaches for
// raw table field names.
export function createCliSourceKey(name: string): string | null {
const normalized = name.trim();
return normalized.length > 0 && isCliSourceKey(normalized)
? normalized
: null;
if (normalized.length > 0 && isCliSourceKey(normalized)) {
return normalized;
}
return normalizeCliSourceKey(normalized);
}

export function createCliSourceRecord(input: {
Expand Down
Loading