Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

finalize WIP API #10280

Merged
merged 90 commits into from
Mar 2, 2024
Merged
Show file tree
Hide file tree
Changes from 76 commits
Commits
Show all changes
90 commits
Select commit Hold shift + click to select a range
fbb9c50
feat: no more readable / writable
bholmesdev Feb 26, 2024
448edc1
fix: table typegen
bholmesdev Feb 26, 2024
cc23ba2
wip: move data seeding
bholmesdev Feb 26, 2024
607664f
chore: add scripts to basics
bholmesdev Feb 27, 2024
2299a9c
feat: data() -> seed file
bholmesdev Feb 27, 2024
203dbe3
refactor: ensure precedence of file name
bholmesdev Feb 27, 2024
9c0139b
feat: db execute command
bholmesdev Feb 27, 2024
cbc4dc5
fix: test imports
bholmesdev Feb 27, 2024
6f0c5d3
chore: remove old readable error tests
bholmesdev Feb 27, 2024
80154b4
feat: support local db with `db execute`
bholmesdev Feb 27, 2024
8886b5c
refactor: remove integrations from test for now
bholmesdev Feb 27, 2024
56ea81d
chore: stray comment
bholmesdev Feb 27, 2024
274504b
chore: remove `table` config object
bholmesdev Feb 27, 2024
0a3efac
feat: `db.batch`!
bholmesdev Feb 27, 2024
f170997
refactor: move migrations/ inside db/
bholmesdev Feb 28, 2024
a0cebe3
fix: move ticketing-example to seed file
bholmesdev Feb 28, 2024
30854cd
fix: disable foreign keys when recreating tables
bholmesdev Feb 28, 2024
fbc136e
refactor: standardize migrations dir
bholmesdev Feb 28, 2024
783c22d
feat: move to db/config.ts
bholmesdev Feb 29, 2024
4f8e275
feat: file watching for db/config.ts dependencies
bholmesdev Feb 29, 2024
8936506
feat: remove unsafeDisableStudio
bholmesdev Feb 29, 2024
69277b9
chroe: remove bad import
bholmesdev Feb 29, 2024
258a946
feat: parse config.ts from cli
bholmesdev Feb 29, 2024
933c02f
chore: remove async from localDatabaseClient
bholmesdev Feb 29, 2024
1e5e235
fix: update recipes config and seed
bholmesdev Feb 29, 2024
217d5bb
chore: update unit tests
bholmesdev Feb 29, 2024
25cb455
chore: update tests to dev server
bholmesdev Feb 29, 2024
0b9d963
refactor: collectionToTable -> asDrizzleTable
bholmesdev Feb 29, 2024
dc0af08
chore: tidy up collection -> table error states
bholmesdev Feb 29, 2024
868beb5
refactor: regexp -> endsWith
bholmesdev Feb 29, 2024
63f042f
feat: pretty error inserting into table
bholmesdev Feb 29, 2024
36a97e6
refactor: try/catch -> catch()
bholmesdev Feb 29, 2024
5abb5bf
feat: expose utils for integration seed files
bholmesdev Feb 29, 2024
8823108
fix: add config import to db client modules
bholmesdev Feb 29, 2024
c175372
fix: just use generic "seeding database" error
bholmesdev Feb 29, 2024
f5b5605
Merge branch 'main' into feat/phil-collins-closing-song
bholmesdev Feb 29, 2024
146ece1
chore: remove unused link args
bholmesdev Feb 29, 2024
432775d
fix: migration queries im,port
bholmesdev Feb 29, 2024
be3fd92
chore: remove irrelevant glob/ example
bholmesdev Feb 29, 2024
275f28b
feat: format migration file path
bholmesdev Feb 29, 2024
21ef62c
feat: support all config file names
bholmesdev Feb 29, 2024
a3fcfdc
chore: remove db.batch() for now
bholmesdev Feb 29, 2024
56e6c57
chore: remove `db` object
bholmesdev Feb 29, 2024
985c48a
core: remove unused integration file
bholmesdev Feb 29, 2024
8603b5c
chore: changeset
bholmesdev Feb 29, 2024
09f0a93
fix: foreign key empty error message
bholmesdev Feb 29, 2024
715b679
chore: remove old TODO
bholmesdev Feb 29, 2024
6f1941d
fix: bad context reference
bholmesdev Feb 29, 2024
a738346
refactor: seedDev -> seedLocal
bholmesdev Mar 1, 2024
7c21b96
wip: throw some console logs at github
bholmesdev Mar 1, 2024
30aa333
wip: avoid seeding astro:db imported by seed file
bholmesdev Mar 1, 2024
ce30dd1
wip: use anything in db/
bholmesdev Mar 1, 2024
c6031b1
refactor: only seed when loaded within srcDir
bholmesdev Mar 1, 2024
b21543f
refactor: avoid resolution when not seeding
bholmesdev Mar 1, 2024
ec4730a
chore: remove logs
bholmesdev Mar 1, 2024
48dca90
refactor: seed within create local db client
bholmesdev Mar 1, 2024
918d607
refactor: use normalizePath
bholmesdev Mar 1, 2024
a3d88a7
wip: logs
bholmesdev Mar 1, 2024
1e245ed
Merge branch 'main' into feat/phil-collins-closing-song
bholmesdev Mar 1, 2024
de79f2b
wip: logs
bholmesdev Mar 1, 2024
e10167f
refactor: early return
bholmesdev Mar 1, 2024
cf2d9a8
chore: more logs
bholmesdev Mar 1, 2024
da2b8fb
refactor: no batch
bholmesdev Mar 1, 2024
47409c6
fix: use beforeAll
bholmesdev Mar 1, 2024
2255a1e
refactor: move all tests to base block
bholmesdev Mar 1, 2024
3af9835
wip: log dev server starting
bholmesdev Mar 1, 2024
aba33ee
chore: remove logs
bholmesdev Mar 1, 2024
37585ce
wip: demo ready
bholmesdev Mar 1, 2024
ff8c571
chore: remove duplicate recreateTables() call
bholmesdev Mar 1, 2024
b6ce989
Revert "wip: demo ready"
bholmesdev Mar 1, 2024
483fbd1
refactor: beforeEach to isolate dev servers
bholmesdev Mar 1, 2024
3d77be2
chore: remove useBundledDbUrl
bholmesdev Mar 1, 2024
c2e2721
refactor: naming and seed scope
bholmesdev Mar 1, 2024
5c467c6
chore: remove stray console logs
bholmesdev Mar 1, 2024
1a669ea
wip: fix windows file import
bholmesdev Mar 1, 2024
46fd65d
wip: try fileURLToPath
bholmesdev Mar 1, 2024
ac08bc1
Revert "wip: try fileURLToPath"
bholmesdev Mar 1, 2024
9e52f3b
Revert "wip: fix windows file import"
bholmesdev Mar 1, 2024
c489825
refactor: dir -> directory
bholmesdev Mar 1, 2024
0e9e1ae
refactor: move execute file to cli
bholmesdev Mar 1, 2024
67d7f70
refactor: remove seed.dev convention
bholmesdev Mar 1, 2024
65f569d
wip: attempt fileURLToPath
natemoo-re Mar 1, 2024
eb37e30
wip: debug the file exists
bholmesdev Mar 1, 2024
9bffad0
fix: use mjs??
bholmesdev Mar 1, 2024
a00c48a
chore: remove duplicate seedLocal
bholmesdev Mar 1, 2024
830af5c
chore: remove log check
bholmesdev Mar 1, 2024
94449d5
refactor: use in memory db for tests
bholmesdev Mar 2, 2024
6bea900
chore: clean up test comment
bholmesdev Mar 2, 2024
4973383
fix: avoid file writes for db setup on in memory db
bholmesdev Mar 2, 2024
76e618d
chore: bump db changeset to minor
bholmesdev Mar 2, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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": patch
bholmesdev marked this conversation as resolved.
Show resolved Hide resolved
---

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
38 changes: 38 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,38 @@
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 { executeFile } from '../../../load-file.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);

await executeFile({
fileUrl,
tables: dbConfig.tables ?? {},
root: astroConfig.root,
appToken: appToken.token,
});
}
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 { getMigrationsDirUrl } from '../../../utils.js';
bholmesdev marked this conversation as resolved.
Show resolved Hide resolved
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 = getMigrationsDirUrl(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.');
}