Skip to content

Commit

Permalink
Breaking changes to codegen (#1371)
Browse files Browse the repository at this point in the history
  • Loading branch information
SferaDev authored May 6, 2024
1 parent 75d1671 commit d7e5c87
Show file tree
Hide file tree
Showing 21 changed files with 279 additions and 508 deletions.
14 changes: 3 additions & 11 deletions cli/src/base.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
import { Command, Flags, Interfaces } from '@oclif/core';
import {
buildClient,
getAPIKey,
getBranch,
getHostUrl,
parseWorkspacesUrlParts,
Schemas,
XataApiPlugin
} from '@xata.io/client';
import { buildClient, getHostUrl, parseWorkspacesUrlParts, Schemas, XataApiPlugin } from '@xata.io/client';
import { XataImportPlugin } from '@xata.io/importer';
import ansiRegex from 'ansi-regex';
import chalk from 'chalk';
Expand Down Expand Up @@ -192,7 +184,7 @@ export abstract class BaseCommand<T extends typeof Command> extends Command {
const { flags } = await this.parseCommand();
const profileName = flags.profile || getEnvProfileName();

const apiKey = getAPIKey();
const apiKey = process.env.XATA_API_KEY;
const useEnv = !ignoreEnv || profileName === 'default';
if (useEnv && apiKey) return buildProfile({ name: 'default', apiKey });

Expand Down Expand Up @@ -531,7 +523,7 @@ export abstract class BaseCommand<T extends typeof Command> extends Command {
}

getCurrentBranchName() {
return getBranch() ?? 'main';
return process.env.XATA_BRANCH ?? 'main';
}

async updateConfig() {
Expand Down
23 changes: 5 additions & 18 deletions cli/src/commands/codegen/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Flags } from '@oclif/core';
import { generate, isValidJavascriptTarget, javascriptTargets } from '@xata.io/codegen';
import chalk from 'chalk';
import { mkdir, readFile, writeFile } from 'fs/promises';
import { mkdir, writeFile } from 'fs/promises';
import path, { dirname, extname, relative } from 'path';
import { BaseCommand } from '../../base.js';
import { ProjectConfig } from '../../config.js';
import { getBranchDetailsWithPgRoll } from '../../migrations/pgroll.js';
import { safeReadFile } from '../../utils/files.js';

export const languages: Record<string, 'javascript' | 'typescript'> = {
'.js': 'javascript',
Expand Down Expand Up @@ -46,9 +47,6 @@ export default class Codegen extends BaseCommand<typeof Codegen> {
}),
'worker-id': Flags.string({
description: 'Xata worker deployment id'
}),
'experimental-incremental-build': Flags.boolean({
description: 'Experimental: Keep the source code in the generated file and only update the parts that changed'
})
};

Expand Down Expand Up @@ -84,27 +82,16 @@ export default class Codegen extends BaseCommand<typeof Codegen> {
}

const xata = await this.getXataClient();
const { workspace, region, database, branch, databaseURL } = await this.getParsedDatabaseURLWithBranch(
flags.db,
flags.branch
);
const { workspace, region, database, branch } = await this.getParsedDatabaseURLWithBranch(flags.db, flags.branch);
const { schema } = await getBranchDetailsWithPgRoll(xata, { workspace, region, database, branch });

const codegenBranch = flags['inject-branch'] ? branch : undefined;

// Experimental: Keep the source code in the generated file and only update the parts that changed
const incrementalBuild =
flags['experimental-incremental-build'] ?? this.projectConfig?.experimental?.incrementalBuild ?? false;
const existingCode = incrementalBuild ? await readFile(output, 'utf8').catch(() => undefined) : undefined;
const existingCode = await safeReadFile(output);

const result = await generate({
schema,
databaseURL,
language,
moduleType,
javascriptTarget,
branch: codegenBranch,
existingCode
existingCode: existingCode ?? ''
});

const { typescript, javascript, types } = result;
Expand Down
123 changes: 59 additions & 64 deletions cli/src/commands/init/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,7 @@ describe('xata init', () => {
}",
"package.json": "{"name":"test","version":"1.0.0"}",
"readme.md": "",
"xataCustom.ts": "// Generated by Xata Codegen 0.29.4. Please do not edit.
import { buildClient } from "@xata.io/client";
"xataCustom.ts": "import { buildClient, getDeployPreviewBranch } from "@xata.io/client";
import type {
BaseClientOptions,
SchemaInference,
Expand All @@ -181,24 +180,23 @@ describe('xata init', () => {
const DatabaseClient = buildClient();
const defaultOptions = {
databaseURL: "https://test-1234.us-east-1.xata.sh/db/db1",
};
export class XataClient extends DatabaseClient<DatabaseSchema> {
constructor(options?: BaseClientOptions) {
super({ ...defaultOptions, ...options }, tables);
super(
{
apiKey: process.env.XATA_API_KEY,
databaseURL: process.env.XATA_DATABASE_URL,
// Use deploy preview branch if available, otherwise use branch from environment
branch:
getDeployPreviewBranch(process.env) ??
process.env.XATA_BRANCH ??
"main",
...options,
},
tables
);
}
}
let instance: XataClient | undefined = undefined;
export const getXataClient = () => {
if (instance) return instance;
instance = new XataClient();
return instance;
};
",
}
`);
Expand Down Expand Up @@ -242,8 +240,7 @@ describe('xata init', () => {
}
}",
"readme.md": "",
"xataCustom.ts": "// Generated by Xata Codegen 0.29.4. Please do not edit.
import { buildClient } from "@xata.io/client";
"xataCustom.ts": "import { buildClient, getDeployPreviewBranch } from "@xata.io/client";
import type {
BaseClientOptions,
SchemaInference,
Expand All @@ -266,24 +263,23 @@ describe('xata init', () => {
const DatabaseClient = buildClient();
const defaultOptions = {
databaseURL: "https://test-1234.us-east-1.xata.sh/db/db1",
};
export class XataClient extends DatabaseClient<DatabaseSchema> {
constructor(options?: BaseClientOptions) {
super({ ...defaultOptions, ...options }, tables);
super(
{
apiKey: process.env.XATA_API_KEY,
databaseURL: process.env.XATA_DATABASE_URL,
// Use deploy preview branch if available, otherwise use branch from environment
branch:
getDeployPreviewBranch(process.env) ??
process.env.XATA_BRANCH ??
"main",
...options,
},
tables
);
}
}
let instance: XataClient | undefined = undefined;
export const getXataClient = () => {
if (instance) return instance;
instance = new XataClient();
return instance;
};
",
}
`);
Expand Down Expand Up @@ -319,8 +315,10 @@ describe('xata init', () => {
}
}",
"readme.md": "",
"xataCustom.ts": "// Generated by Xata Codegen 0.29.4. Please do not edit.
import { buildClient } from "npm:@xata.io/client@latest";
"xataCustom.ts": "import {
buildClient,
getDeployPreviewBranch,
} from "npm:@xata.io/client@latest";
import type {
BaseClientOptions,
SchemaInference,
Expand All @@ -343,24 +341,23 @@ describe('xata init', () => {
const DatabaseClient = buildClient();
const defaultOptions = {
databaseURL: "https://test-1234.us-east-1.xata.sh/db/db1",
};
export class XataClient extends DatabaseClient<DatabaseSchema> {
constructor(options?: BaseClientOptions) {
super({ ...defaultOptions, ...options }, tables);
super(
{
apiKey: Deno.env.get("XATA_API_KEY"),
databaseURL: Deno.env.get("XATA_DATABASE_URL"),
// Use deploy preview branch if available, otherwise use branch from environment
branch:
getDeployPreviewBranch(Deno.env.get) ??
Deno.env.get("XATA_BRANCH") ??
"main",
...options,
},
tables
);
}
}
let instance: XataClient | undefined = undefined;
export const getXataClient = () => {
if (instance) return instance;
instance = new XataClient();
return instance;
};
",
}
`);
Expand Down Expand Up @@ -400,8 +397,7 @@ describe('xata init', () => {
"package.json": "{"name":"test","version":"1.0.0"}",
"pnpm-lock.yaml": "lockfileVersion: '6.0'",
"readme.md": "",
"xataCustom.ts": "// Generated by Xata Codegen 0.29.4. Please do not edit.
import { buildClient } from "@xata.io/client";
"xataCustom.ts": "import { buildClient, getDeployPreviewBranch } from "@xata.io/client";
import type {
BaseClientOptions,
SchemaInference,
Expand All @@ -424,24 +420,23 @@ describe('xata init', () => {
const DatabaseClient = buildClient();
const defaultOptions = {
databaseURL: "https://test-1234.us-east-1.xata.sh/db/db1",
};
export class XataClient extends DatabaseClient<DatabaseSchema> {
constructor(options?: BaseClientOptions) {
super({ ...defaultOptions, ...options }, tables);
super(
{
apiKey: process.env.XATA_API_KEY,
databaseURL: process.env.XATA_DATABASE_URL,
// Use deploy preview branch if available, otherwise use branch from environment
branch:
getDeployPreviewBranch(process.env) ??
process.env.XATA_BRANCH ??
"main",
...options,
},
tables
);
}
}
let instance: XataClient | undefined = undefined;
export const getXataClient = () => {
if (instance) return instance;
instance = new XataClient();
return instance;
};
",
}
`);
Expand Down
2 changes: 1 addition & 1 deletion cli/src/commands/shell/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export default class Shell extends BaseCommand<typeof Shell> {
const branchDetails = await getBranchDetailsWithPgRoll(xata, { workspace, region, database, branch });
const { schema } = branchDetails;

const { javascript } = await generate({ language: 'javascript', databaseURL, schema });
const { javascript } = await generate({ language: 'javascript', schema });
await fs.writeFile(tempFile, javascript);
} catch (err) {
const message = err instanceof Error ? err.message : String(err);
Expand Down
3 changes: 1 addition & 2 deletions cli/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const projectConfigSchema = z.object({
databaseURL: z.string(),
codegen: z.object({
output: z.string(),
moduleType: z.enum(['cjs', 'esm', 'deno']),
moduleType: z.enum(['cjs', 'esm', 'deno', 'vite']),
declarations: z.boolean(),
javascriptTarget: z.enum([
'es5',
Expand All @@ -21,7 +21,6 @@ export const projectConfigSchema = z.object({
])
}),
experimental: z.object({
incrementalBuild: z.boolean(),
workflow: z.boolean()
})
});
Expand Down
4 changes: 2 additions & 2 deletions cli/src/migrations/pgroll.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Schemas, XataApiClient } from '@xata.io/client';
import { Column } from '@xata.io/codegen';
import { OpRawSQL, OpRenameConstraint, PgRollOperation } from '@xata.io/pgroll';
import path from 'path';
import z from 'zod';
import { XataClient } from '../base.js';
import { BranchSchemaFormatted } from '../commands/schema/types.js';
import { safeJSONParse, safeReadFile } from '../utils/files.js';
import { migrationsDir, readMigrationsDir } from './files.js';
import { MigrationFilePgroll, migrationFilePgroll } from './schema.js';
import { OpRawSQL, OpRenameConstraint, PgRollOperation } from '@xata.io/pgroll';
import { BranchSchemaFormatted } from '../commands/schema/types.js';

export const isBranchPgRollEnabled = (details: Schemas.DBBranch) => {
// @ts-expect-error TODO: Fix this when api is finalized
Expand Down
3 changes: 1 addition & 2 deletions packages/client/src/api/client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { defaultTrace, TraceFunction } from '../schema/tracing';
import { getAPIKey } from '../util/environment';
import { FetchImpl, getFetchImplementation } from '../util/fetch';
import { RequiredKeys } from '../util/types';
import { generateUUID } from '../util/uuid';
Expand Down Expand Up @@ -40,7 +39,7 @@ const buildApiClient = () =>
class {
constructor(options: XataApiClientOptions = {}) {
const provider = options.host ?? 'production';
const apiKey = options.apiKey ?? getAPIKey();
const apiKey = options.apiKey;
const trace = options.trace ?? defaultTrace;
const clientID = generateUUID();

Expand Down
29 changes: 6 additions & 23 deletions packages/client/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { defaultTrace, TraceFunction } from './schema/tracing';
import { SearchPlugin, SearchPluginResult } from './search';
import { SQLPlugin, SQLPluginResult } from './sql';
import { TransactionPlugin, TransactionPluginResult } from './transaction';
import { getAPIKey, getBranch, getDatabaseURL, getEnableBrowserVariable, getPreviewBranch } from './util/environment';
import { FetchImpl, getFetchImplementation } from './util/fetch';
import { AllRequired, StringKeys } from './util/types';
import { generateUUID } from './util/uuid';
Expand Down Expand Up @@ -83,7 +82,7 @@ export const buildClient = <Plugins extends Record<string, XataPlugin> = {}>(plu

#parseOptions(options?: BaseClientOptions): SafeOptions {
// If is running from the browser and the user didn't pass `enableBrowser` we throw an error
const enableBrowser = options?.enableBrowser ?? getEnableBrowserVariable() ?? false;
const enableBrowser = options?.enableBrowser ?? false;
// @ts-ignore Window, Deno are not globals
const isBrowser = typeof window !== 'undefined' && typeof Deno === 'undefined';
if (isBrowser && !enableBrowser) {
Expand All @@ -93,8 +92,9 @@ export const buildClient = <Plugins extends Record<string, XataPlugin> = {}>(plu
}

const fetch = getFetchImplementation(options?.fetch);
const databaseURL = options?.databaseURL || getDatabaseURL();
const apiKey = options?.apiKey || getAPIKey();
const databaseURL = options?.databaseURL;
const apiKey = options?.apiKey;
const branch = options?.branch;
const trace = options?.trace ?? defaultTrace;
const clientName = options?.clientName;
const host = options?.host ?? 'production';
Expand All @@ -108,25 +108,8 @@ export const buildClient = <Plugins extends Record<string, XataPlugin> = {}>(plu
throw new Error('Option databaseURL is required');
}

const envBranch = getBranch();
const previewBranch = getPreviewBranch();
const branch = options?.branch || previewBranch || envBranch || 'main';
if (!!previewBranch && branch !== previewBranch) {
console.warn(
`Ignoring preview branch ${previewBranch} because branch option was passed to the client constructor with value ${branch}`
);
} else if (!!envBranch && branch !== envBranch) {
console.warn(
`Ignoring branch ${envBranch} because branch option was passed to the client constructor with value ${branch}`
);
} else if (!!previewBranch && !!envBranch && previewBranch !== envBranch) {
console.warn(
`Ignoring preview branch ${previewBranch} and branch ${envBranch} because branch option was passed to the client constructor with value ${branch}`
);
} else if (!previewBranch && !envBranch && options?.branch === undefined) {
console.warn(
`No branch was passed to the client constructor. Using default branch ${branch}. You can set the branch with the environment variable XATA_BRANCH or by passing the branch option to the client constructor.`
);
if (!branch) {
throw new Error('Option branch is required');
}

return {
Expand Down
Loading

0 comments on commit d7e5c87

Please sign in to comment.