Skip to content

Commit

Permalink
finalize WIP API (#10280)
Browse files Browse the repository at this point in the history
* feat: no more readable / writable

* fix: table typegen

* wip: move data seeding

* chore: add scripts to basics

* feat: data() -> seed file

* refactor: ensure precedence of file name

* feat: db execute command

* fix: test imports

* chore: remove old readable error tests

* feat: support local db with `db execute`

* refactor: remove integrations from test for now

* chore: stray comment

* chore: remove `table` config object

* feat: `db.batch`!

* refactor: move migrations/ inside db/

* fix: move ticketing-example to seed file

* fix: disable foreign keys when recreating tables

* refactor: standardize migrations dir

* feat: move to db/config.ts

* feat: file watching for db/config.ts dependencies

* feat: remove unsafeDisableStudio

* chroe: remove bad import

* feat: parse config.ts from cli

* chore: remove async from localDatabaseClient

* fix: update recipes config and seed

* chore: update unit tests

* chore: update tests to dev server

* refactor: collectionToTable -> asDrizzleTable

* chore: tidy up collection -> table error states

* refactor: regexp -> endsWith

* feat: pretty error inserting into table

* refactor: try/catch -> catch()

* feat: expose utils for integration seed files

* fix: add config import to db client modules

* fix: just use generic "seeding database" error

* chore: remove unused link args

* fix: migration queries im,port

* chore: remove irrelevant glob/ example

* feat: format migration file path

* feat: support all config file names

* chore: remove db.batch() for now

* chore: remove `db` object

* core: remove unused integration file

* chore: changeset

* fix: foreign key empty error message

* chore: remove old TODO

* fix: bad context reference

* refactor: seedDev -> seedLocal

* wip: throw some console logs at github

* wip: avoid seeding astro:db imported by seed file

* wip: use anything in db/

* refactor: only seed when loaded within srcDir

* refactor: avoid resolution when not seeding

* chore: remove logs

* refactor: seed within create local db client

* refactor: use normalizePath

* wip: logs

* wip: logs

* refactor: early return

* chore: more logs

* refactor: no batch

* fix: use beforeAll

* refactor: move all tests to base block

* wip: log dev server starting

* chore: remove logs

* wip: demo ready

* chore: remove duplicate recreateTables() call

* Revert "wip: demo ready"

This reverts commit 37585ce.

* refactor: beforeEach to isolate dev servers

* chore: remove useBundledDbUrl

* refactor: naming and seed scope

* chore: remove stray console logs

* wip: fix windows file import

* wip: try fileURLToPath

* Revert "wip: try fileURLToPath"

This reverts commit 46fd65d.

* Revert "wip: fix windows file import"

This reverts commit 1a669ea.

* refactor: dir -> directory

* refactor: move execute file to cli

* refactor: remove seed.dev convention

* wip: attempt fileURLToPath

* wip: debug the file exists

* fix: use mjs??

* chore: remove duplicate seedLocal

* chore: remove log check

* refactor: use in memory db for tests

* chore: clean up test comment

* fix: avoid file writes for db setup on in memory db

* chore: bump db changeset to minor

---------

Co-authored-by: Nate Moore <nate@astro.build>
  • Loading branch information
bholmesdev and natemoo-re authored Mar 2, 2024
1 parent 4b6e2fb commit 3488be9
Show file tree
Hide file tree
Showing 58 changed files with 1,096 additions and 1,239 deletions.
6 changes: 6 additions & 0 deletions .changeset/rich-turtles-live.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"astro": patch
"@astrojs/db": minor
---

Finalize db API to a shared db/ directory.
1 change: 0 additions & 1 deletion packages/astro/src/core/config/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ export const AstroConfigSchema = z.object({
.optional()
.default('attribute'),
adapter: z.object({ name: z.string(), hooks: z.object({}).passthrough().default({}) }).optional(),
db: z.object({}).passthrough().default({}).optional(),
integrations: z.preprocess(
// preprocess
(val) => (Array.isArray(val) ? val.flat(Infinity).filter(Boolean) : val),
Expand Down
4 changes: 0 additions & 4 deletions packages/db/config-augment.d.ts

This file was deleted.

8 changes: 5 additions & 3 deletions packages/db/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/// <reference types="./config-augment.d.ts" />
export * from './dist/index.js';
export { default } from './dist/index.js';
export { default, cli } from './dist/index.js';

declare module 'astro:db' {
export { defineTable, defineDB, column, sql, NOW, TRUE, FALSE } from './dist/index.js';
}
14 changes: 14 additions & 0 deletions packages/db/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
"types": "./index.d.ts",
"import": "./dist/index.js"
},
"./utils": {
"types": "./dist/utils.d.ts",
"import": "./dist/utils.js"
},
"./runtime": {
"types": "./dist/runtime/index.d.ts",
"import": "./dist/runtime/index.js"
Expand All @@ -20,18 +24,28 @@
"types": "./dist/runtime/drizzle.d.ts",
"import": "./dist/runtime/drizzle.js"
},
"./runtime/config": {
"types": "./dist/runtime/config.d.ts",
"import": "./dist/runtime/config.js"
},
"./package.json": "./package.json"
},
"typesVersions": {
"*": {
".": [
"./index.d.ts"
],
"utils": [
"./dist/utils.d.ts"
],
"runtime": [
"./dist/runtime/index.d.ts"
],
"runtime/drizzle": [
"./dist/runtime/drizzle.d.ts"
],
"runtime/config": [
"./dist/runtime/config.d.ts"
]
}
},
Expand Down
40 changes: 40 additions & 0 deletions packages/db/src/core/cli/commands/execute/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import type { AstroConfig } from 'astro';
import type { Arguments } from 'yargs-parser';
import { MISSING_EXECUTE_PATH_ERROR, FILE_NOT_FOUND_ERROR } from '../../../errors.js';
import { existsSync } from 'node:fs';
import { getManagedAppTokenOrExit } from '../../../tokens.js';
import { type DBConfig } from '../../../types.js';
import { bundleFile, importBundledFile } from '../../../load-file.js';
import { getStudioVirtualModContents } from '../../../integration/vite-plugin-db.js';

export async function cmd({
astroConfig,
dbConfig,
flags,
}: {
astroConfig: AstroConfig;
dbConfig: DBConfig;
flags: Arguments;
}) {
const filePath = flags._[4];
if (typeof filePath !== 'string') {
console.error(MISSING_EXECUTE_PATH_ERROR);
process.exit(1);
}

const fileUrl = new URL(filePath, astroConfig.root);
if (!existsSync(fileUrl)) {
console.error(FILE_NOT_FOUND_ERROR(filePath));
process.exit(1);
}

const appToken = await getManagedAppTokenOrExit(flags.token);

const virtualModContents = getStudioVirtualModContents({
tables: dbConfig.tables ?? {},
appToken: appToken.token,
});
const { code } = await bundleFile({ virtualModContents, root: astroConfig.root, fileUrl });
// Executable files use top-level await. Importing will run the file.
await importBundledFile({ code, root: astroConfig.root });
}
29 changes: 21 additions & 8 deletions packages/db/src/core/cli/commands/gen/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { fileURLToPath } from 'node:url';
import { writeFile } from 'node:fs/promises';
import type { AstroConfig } from 'astro';
import { bgRed, red, reset } from 'kleur/colors';
import { bold, bgRed, red, reset } from 'kleur/colors';
import type { Arguments } from 'yargs-parser';
import { getMigrationQueries } from '../../migration-queries.js';
import {
Expand All @@ -9,12 +10,23 @@ import {
getMigrationStatus,
initializeMigrationsDirectory,
} from '../../migrations.js';
import { getMigrationsDirectoryUrl } from '../../../utils.js';
import type { DBConfig } from '../../../types.js';
import { relative } from 'node:path';

export async function cmd({ config }: { config: AstroConfig; flags: Arguments }) {
const migration = await getMigrationStatus(config);
export async function cmd({
astroConfig,
dbConfig,
}: {
astroConfig: AstroConfig;
dbConfig: DBConfig;
flags: Arguments;
}) {
const migration = await getMigrationStatus({ dbConfig, root: astroConfig.root });
const migrationsDir = getMigrationsDirectoryUrl(astroConfig.root);

if (migration.state === 'no-migrations-found') {
await initializeMigrationsDirectory(migration.currentSnapshot);
await initializeMigrationsDirectory(migration.currentSnapshot, migrationsDir);
console.log(MIGRATIONS_CREATED);
return;
} else if (migration.state === 'up-to-date') {
Expand All @@ -30,14 +42,15 @@ export async function cmd({ config }: { config: AstroConfig; flags: Arguments })
// Warn the user about any changes that lead to data-loss.
// When the user runs `db push`, they will be prompted to confirm these changes.
confirmations.map((message) => console.log(bgRed(' !!! ') + ' ' + red(message)));
const migrationFileContent = {
const content = {
diff,
db: migrationQueries,
// TODO(fks): Encode the relevant data, instead of the raw message.
// This will give `db push` more control over the formatting of the message.
confirm: confirmations.map((c) => reset(c)),
};
const migrationFileName = `./migrations/${newFilename}`;
await writeFile(migrationFileName, JSON.stringify(migrationFileContent, undefined, 2));
console.log(migrationFileName + ' created!');
const fileUrl = new URL(newFilename, migrationsDir);
const relativePath = relative(fileURLToPath(astroConfig.root), fileURLToPath(fileUrl));
await writeFile(fileUrl, JSON.stringify(content, undefined, 2));
console.log(bold(relativePath) + ' created!');
}
4 changes: 1 addition & 3 deletions packages/db/src/core/cli/commands/link/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import { mkdir, writeFile } from 'node:fs/promises';
import { homedir } from 'node:os';
import { basename } from 'node:path';
import type { AstroConfig } from 'astro';
import { slug } from 'github-slugger';
import { bgRed, cyan } from 'kleur/colors';
import ora from 'ora';
import prompts from 'prompts';
import type { Arguments } from 'yargs-parser';
import { MISSING_SESSION_ID_ERROR } from '../../../errors.js';
import { PROJECT_ID_FILE, getSessionIdFromFile } from '../../../tokens.js';
import { getAstroStudioUrl } from '../../../utils.js';

export async function cmd({}: { config: AstroConfig; flags: Arguments }) {
export async function cmd() {
const sessionToken = await getSessionIdFromFile();
if (!sessionToken) {
console.error(MISSING_SESSION_ID_ERROR);
Expand Down
9 changes: 8 additions & 1 deletion packages/db/src/core/cli/commands/login/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import open from 'open';
import ora from 'ora';
import type { Arguments } from 'yargs-parser';
import { SESSION_LOGIN_FILE } from '../../../tokens.js';
import type { DBConfig } from '../../../types.js';
import { getAstroStudioUrl } from '../../../utils.js';

// NOTE(fks): How the Astro CLI login process works:
Expand Down Expand Up @@ -47,7 +48,13 @@ async function createServer(): Promise<{ url: string; promise: Promise<string> }
return { url: serverUrl, promise: sessionPromise };
}

export async function cmd({ flags }: { config: AstroConfig; flags: Arguments }) {
export async function cmd({
flags,
}: {
astroConfig: AstroConfig;
dbConfig: DBConfig;
flags: Arguments;
}) {
let session = flags.session;

if (!session) {
Expand Down
4 changes: 1 addition & 3 deletions packages/db/src/core/cli/commands/logout/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { unlink } from 'node:fs/promises';
import type { AstroConfig } from 'astro';
import type { Arguments } from 'yargs-parser';
import { SESSION_LOGIN_FILE } from '../../../tokens.js';

export async function cmd({}: { config: AstroConfig; flags: Arguments }) {
export async function cmd() {
await unlink(SESSION_LOGIN_FILE);
console.log('Successfully logged out of Astro Studio.');
}
Loading

0 comments on commit 3488be9

Please sign in to comment.