@astrojs/db 0.11.7 → 0.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,3 @@
1
- import type { AstroIntegration } from 'astro';
2
1
  import type { z } from 'zod';
3
2
  import type { MaybeArray, booleanColumnSchema, columnSchema, columnsSchema, dateColumnSchema, dbConfigSchema, indexSchema, jsonColumnSchema, numberColumnOptsSchema, numberColumnSchema, referenceableColumnSchema, resolvedIndexSchema, tableSchema, textColumnOptsSchema, textColumnSchema } from './schemas.js';
4
3
  export type ResolvedIndexes = z.output<typeof dbConfigSchema>['tables'][string]['indexes'];
@@ -46,14 +45,16 @@ interface LegacyIndexConfig<TColumns extends ColumnsConfig> extends z.input<type
46
45
  }
47
46
  export type NumberColumnOpts = z.input<typeof numberColumnOptsSchema>;
48
47
  export type TextColumnOpts = z.input<typeof textColumnOptsSchema>;
49
- export type AstroDbIntegration = AstroIntegration & {
50
- hooks: {
51
- 'astro:db:setup'?: (options: {
52
- extendDb: (options: {
53
- configEntrypoint?: URL | string;
54
- seedEntrypoint?: URL | string;
55
- }) => void;
56
- }) => void | Promise<void>;
57
- };
58
- };
48
+ declare global {
49
+ namespace Astro {
50
+ interface IntegrationHooks {
51
+ 'astro:db:setup'?: (options: {
52
+ extendDb: (options: {
53
+ configEntrypoint?: URL | string;
54
+ seedEntrypoint?: URL | string;
55
+ }) => void;
56
+ }) => void | Promise<void>;
57
+ }
58
+ }
59
+ }
59
60
  export {};
@@ -1,10 +1,10 @@
1
1
  import type { AstroConfig, AstroIntegration } from 'astro';
2
- import type { AstroDbIntegration } from './types.js';
2
+ import './types.js';
3
3
  export type VitePlugin = Required<AstroConfig['vite']>['plugins'][number];
4
4
  export declare function getAstroEnv(envMode?: string): Record<`ASTRO_${string}`, string>;
5
5
  export declare function getRemoteDatabaseUrl(): string;
6
6
  export declare function getDbDirectoryUrl(root: URL | string): URL;
7
- export declare function defineDbIntegration(integration: AstroDbIntegration): AstroIntegration;
7
+ export declare function defineDbIntegration(integration: AstroIntegration): AstroIntegration;
8
8
  export type Result<T> = {
9
9
  success: true;
10
10
  data: T;
@@ -1,8 +1,8 @@
1
1
  import type { AstroConfig } from 'astro';
2
- import type { Arguments } from 'yargs-parser';
3
2
  import type { DBConfig } from '../../../types.js';
3
+ import type { YargsArguments } from '../../types.js';
4
4
  export declare function cmd({ astroConfig, dbConfig, flags, }: {
5
5
  astroConfig: AstroConfig;
6
6
  dbConfig: DBConfig;
7
- flags: Arguments;
7
+ flags: YargsArguments;
8
8
  }): Promise<void>;
@@ -1,8 +1,8 @@
1
1
  import type { AstroConfig } from 'astro';
2
- import type { Arguments } from 'yargs-parser';
3
2
  import type { DBConfig } from '../../../types.js';
3
+ import type { YargsArguments } from '../../types.js';
4
4
  export declare function cmd({ flags, }: {
5
5
  astroConfig: AstroConfig;
6
6
  dbConfig: DBConfig;
7
- flags: Arguments;
7
+ flags: YargsArguments;
8
8
  }): Promise<void>;
@@ -1,8 +1,8 @@
1
1
  import type { AstroConfig } from 'astro';
2
- import type { Arguments } from 'yargs-parser';
3
2
  import { type DBConfig } from '../../../types.js';
3
+ import type { YargsArguments } from '../../types.js';
4
4
  export declare function cmd({ dbConfig, flags, }: {
5
5
  astroConfig: AstroConfig;
6
6
  dbConfig: DBConfig;
7
- flags: Arguments;
7
+ flags: YargsArguments;
8
8
  }): Promise<void>;
@@ -1,8 +1,8 @@
1
1
  import type { AstroConfig } from 'astro';
2
- import type { Arguments } from 'yargs-parser';
3
2
  import type { DBConfigInput } from '../../../types.js';
3
+ import type { YargsArguments } from '../../types.js';
4
4
  export declare function cmd({ flags, astroConfig, }: {
5
5
  dbConfig: DBConfigInput;
6
6
  astroConfig: AstroConfig;
7
- flags: Arguments;
7
+ flags: YargsArguments;
8
8
  }): Promise<void>;
@@ -1,8 +1,8 @@
1
1
  import type { AstroConfig } from 'astro';
2
- import type { Arguments } from 'yargs-parser';
3
2
  import type { DBConfig } from '../../../types.js';
3
+ import type { YargsArguments } from '../../types.js';
4
4
  export declare function cmd({ dbConfig, flags, }: {
5
5
  astroConfig: AstroConfig;
6
6
  dbConfig: DBConfig;
7
- flags: Arguments;
7
+ flags: YargsArguments;
8
8
  }): Promise<void>;
@@ -1,6 +1,6 @@
1
1
  import type { AstroConfig } from 'astro';
2
- import type { Arguments } from 'yargs-parser';
2
+ import type { YargsArguments } from './types.js';
3
3
  export declare function cli({ flags, config: astroConfig, }: {
4
- flags: Arguments;
4
+ flags: YargsArguments;
5
5
  config: AstroConfig;
6
6
  }): Promise<void>;
@@ -27,7 +27,7 @@ function printHelp({
27
27
  message.push(
28
28
  linebreak(),
29
29
  ` ${bgGreen(black(` ${commandName} `))} ${green(
30
- `v${"0.11.7"}`
30
+ `v${"0.13.0"}`
31
31
  )} ${headline}`
32
32
  );
33
33
  }
@@ -0,0 +1,5 @@
1
+ export interface YargsArguments {
2
+ _: Array<string | number>;
3
+ '--'?: Array<string | number>;
4
+ [argName: string]: any;
5
+ }
File without changes
@@ -1,4 +1,5 @@
1
1
  import { existsSync } from "fs";
2
+ import { parseArgs } from "node:util";
2
3
  import { dirname } from "path";
3
4
  import { fileURLToPath } from "url";
4
5
  import { getManagedAppTokenOrExit } from "@astrojs/studio";
@@ -10,7 +11,6 @@ import {
10
11
  loadEnv,
11
12
  mergeConfig
12
13
  } from "vite";
13
- import parseArgs from "yargs-parser";
14
14
  import { AstroDbError } from "../../runtime/utils.js";
15
15
  import { CONFIG_FILE_NAMES, DB_PATH } from "../consts.js";
16
16
  import { EXEC_DEFAULT_EXPORT_ERROR, EXEC_ERROR } from "../errors.js";
@@ -18,12 +18,11 @@ import { resolveDbConfig } from "../load-file.js";
18
18
  import { SEED_DEV_FILE_NAME } from "../queries.js";
19
19
  import { getDbDirectoryUrl } from "../utils.js";
20
20
  import { fileURLIntegration } from "./file-url.js";
21
- import { typegenInternal } from "./typegen.js";
21
+ import { getDtsContent } from "./typegen.js";
22
22
  import {
23
23
  resolved,
24
24
  vitePluginDb
25
25
  } from "./vite-plugin-db.js";
26
- import { vitePluginInjectEnvTs } from "./vite-plugin-inject-env-ts.js";
27
26
  function astroDBIntegration() {
28
27
  let connectToStudio = false;
29
28
  let configFileDependencies = [];
@@ -57,8 +56,8 @@ function astroDBIntegration() {
57
56
  output = config.output;
58
57
  if (command === "preview") return;
59
58
  let dbPlugin = void 0;
60
- const args = parseArgs(process.argv.slice(3));
61
- connectToStudio = process.env.ASTRO_INTERNAL_TEST_REMOTE || args["remote"];
59
+ const args = parseArgs({ strict: false });
60
+ connectToStudio = !!process.env.ASTRO_INTERNAL_TEST_REMOTE || !!args.values.remote;
62
61
  if (connectToStudio) {
63
62
  appToken = await getManagedAppTokenOrExit();
64
63
  dbPlugin = vitePluginDb({
@@ -85,11 +84,11 @@ function astroDBIntegration() {
85
84
  updateConfig({
86
85
  vite: {
87
86
  assetsInclude: [DB_PATH],
88
- plugins: [dbPlugin, vitePluginInjectEnvTs(config, logger)]
87
+ plugins: [dbPlugin]
89
88
  }
90
89
  });
91
90
  },
92
- "astro:config:done": async ({ config }) => {
91
+ "astro:config:done": async ({ config, injectTypes }) => {
93
92
  if (command === "preview") return;
94
93
  const { dbConfig, dependencies, integrationSeedPaths } = await resolveDbConfig(config);
95
94
  tables.get = () => dbConfig.tables;
@@ -100,7 +99,10 @@ function astroDBIntegration() {
100
99
  await mkdir(dirname(fileURLToPath(localDbUrl)), { recursive: true });
101
100
  await writeFile(localDbUrl, "");
102
101
  }
103
- await typegenInternal({ tables: tables.get() ?? {}, root: config.root });
102
+ injectTypes({
103
+ filename: "db.d.ts",
104
+ content: getDtsContent(tables.get() ?? {})
105
+ });
104
106
  },
105
107
  "astro:server:setup": async ({ server, logger }) => {
106
108
  seedHandler.execute = async (fileUrl) => {
@@ -1,7 +1,2 @@
1
- import type { AstroConfig } from 'astro';
2
1
  import type { DBTables } from '../types.js';
3
- export declare function typegen(astroConfig: Pick<AstroConfig, 'root' | 'integrations'>): Promise<void>;
4
- export declare function typegenInternal({ tables, root }: {
5
- tables: DBTables;
6
- root: URL;
7
- }): Promise<void>;
2
+ export declare function getDtsContent(tables: DBTables): string;
@@ -1,22 +1,11 @@
1
- import { existsSync } from "node:fs";
2
- import { mkdir, writeFile } from "node:fs/promises";
3
- import { DB_TYPES_FILE, RUNTIME_IMPORT } from "../consts.js";
4
- import { resolveDbConfig } from "../load-file.js";
5
- async function typegen(astroConfig) {
6
- const { dbConfig } = await resolveDbConfig(astroConfig);
7
- await typegenInternal({ tables: dbConfig.tables, root: astroConfig.root });
8
- }
9
- async function typegenInternal({ tables, root }) {
1
+ import { RUNTIME_IMPORT } from "../consts.js";
2
+ function getDtsContent(tables) {
10
3
  const content = `// This file is generated by Astro DB
11
4
  declare module 'astro:db' {
12
5
  ${Object.entries(tables).map(([name, table]) => generateTableType(name, table)).join("\n")}
13
6
  }
14
7
  `;
15
- const dotAstroDir = new URL(".astro/", root);
16
- if (!existsSync(dotAstroDir)) {
17
- await mkdir(dotAstroDir);
18
- }
19
- await writeFile(new URL(DB_TYPES_FILE, dotAstroDir), content);
8
+ return content;
20
9
  }
21
10
  function generateTableType(name, table) {
22
11
  const sanitizedColumnsList = Object.entries(table.columns).filter(([, val]) => !val.schema.deprecated);
@@ -28,6 +17,5 @@ function generateTableType(name, table) {
28
17
  return tableType;
29
18
  }
30
19
  export {
31
- typegen,
32
- typegenInternal
20
+ getDtsContent
33
21
  };
@@ -1,4 +1,5 @@
1
1
  import type { AstroConfig } from 'astro';
2
+ import './types.js';
2
3
  /**
3
4
  * Load a user’s `astro:db` configuration file and additional configuration files provided by integrations.
4
5
  */
@@ -8,9 +8,8 @@ import { INTEGRATION_TABLE_CONFLICT_ERROR } from "./errors.js";
8
8
  import { errorMap } from "./integration/error-map.js";
9
9
  import { getConfigVirtualModContents } from "./integration/vite-plugin-db.js";
10
10
  import { dbConfigSchema } from "./schemas.js";
11
- import {} from "./types.js";
11
+ import "./types.js";
12
12
  import { getAstroEnv, getDbDirectoryUrl } from "./utils.js";
13
- const isDbIntegration = (integration) => "astro:db:setup" in integration.hooks;
14
13
  async function resolveDbConfig({
15
14
  root,
16
15
  integrations
@@ -21,7 +20,6 @@ async function resolveDbConfig({
21
20
  const integrationDbConfigPaths = [];
22
21
  const integrationSeedPaths = [];
23
22
  for (const integration of integrations) {
24
- if (!isDbIntegration(integration)) continue;
25
23
  const { name, hooks } = integration;
26
24
  if (hooks["astro:db:setup"]) {
27
25
  hooks["astro:db:setup"]({
@@ -143,7 +143,7 @@ function getDefaultValueSql(columnName, column) {
143
143
  let stringified = "";
144
144
  try {
145
145
  stringified = JSON.stringify(column.schema.default);
146
- } catch (e) {
146
+ } catch {
147
147
  console.log(
148
148
  `Invalid default value for column ${bold(
149
149
  columnName
@@ -1,4 +1,3 @@
1
- import type { AstroIntegration } from 'astro';
2
1
  import type { z } from 'zod';
3
2
  import type { MaybeArray, booleanColumnSchema, columnSchema, columnsSchema, dateColumnSchema, dbConfigSchema, indexSchema, jsonColumnSchema, numberColumnOptsSchema, numberColumnSchema, referenceableColumnSchema, resolvedIndexSchema, tableSchema, textColumnOptsSchema, textColumnSchema } from './schemas.js';
4
3
  export type ResolvedIndexes = z.output<typeof dbConfigSchema>['tables'][string]['indexes'];
@@ -46,14 +45,16 @@ interface LegacyIndexConfig<TColumns extends ColumnsConfig> extends z.input<type
46
45
  }
47
46
  export type NumberColumnOpts = z.input<typeof numberColumnOptsSchema>;
48
47
  export type TextColumnOpts = z.input<typeof textColumnOptsSchema>;
49
- export type AstroDbIntegration = AstroIntegration & {
50
- hooks: {
51
- 'astro:db:setup'?: (options: {
52
- extendDb: (options: {
53
- configEntrypoint?: URL | string;
54
- seedEntrypoint?: URL | string;
55
- }) => void;
56
- }) => void | Promise<void>;
57
- };
58
- };
48
+ declare global {
49
+ namespace Astro {
50
+ interface IntegrationHooks {
51
+ 'astro:db:setup'?: (options: {
52
+ extendDb: (options: {
53
+ configEntrypoint?: URL | string;
54
+ seedEntrypoint?: URL | string;
55
+ }) => void;
56
+ }) => void | Promise<void>;
57
+ }
58
+ }
59
+ }
59
60
  export {};
@@ -1,10 +1,10 @@
1
1
  import type { AstroConfig, AstroIntegration } from 'astro';
2
- import type { AstroDbIntegration } from './types.js';
2
+ import './types.js';
3
3
  export type VitePlugin = Required<AstroConfig['vite']>['plugins'][number];
4
4
  export declare function getAstroEnv(envMode?: string): Record<`ASTRO_${string}`, string>;
5
5
  export declare function getRemoteDatabaseUrl(): string;
6
6
  export declare function getDbDirectoryUrl(root: URL | string): URL;
7
- export declare function defineDbIntegration(integration: AstroDbIntegration): AstroIntegration;
7
+ export declare function defineDbIntegration(integration: AstroIntegration): AstroIntegration;
8
8
  export type Result<T> = {
9
9
  success: true;
10
10
  data: T;
@@ -1,5 +1,6 @@
1
1
  import { getAstroStudioEnv } from "@astrojs/studio";
2
2
  import { loadEnv } from "vite";
3
+ import "./types.js";
3
4
  function getAstroEnv(envMode = "") {
4
5
  const env = loadEnv(envMode, process.cwd(), "ASTRO_");
5
6
  return env;
package/dist/index.d.ts CHANGED
@@ -1,4 +1,3 @@
1
1
  export type { TableConfig } from './core/types.js';
2
2
  export { cli } from './core/cli/index.js';
3
3
  export { integration as default } from './core/integration/index.js';
4
- export { typegen } from './core/integration/typegen.js';
package/dist/index.js CHANGED
@@ -1,8 +1,6 @@
1
1
  import { cli } from "./core/cli/index.js";
2
2
  import { integration } from "./core/integration/index.js";
3
- import { typegen } from "./core/integration/typegen.js";
4
3
  export {
5
4
  cli,
6
- integration as default,
7
- typegen
5
+ integration as default
8
6
  };
@@ -53,7 +53,7 @@ function createRemoteDatabaseClient(appToken, remoteDbURL) {
53
53
  try {
54
54
  const json = await res.json();
55
55
  remoteResult = remoteResultSchema.parse(json);
56
- } catch (e) {
56
+ } catch {
57
57
  throw new DetailedLibsqlError({
58
58
  message: await getUnexpectedResponseMessage(res),
59
59
  code: KNOWN_ERROR_CODES.SQL_QUERY_FAILED
@@ -103,7 +103,7 @@ function createRemoteDatabaseClient(appToken, remoteDbURL) {
103
103
  try {
104
104
  const json = await res.json();
105
105
  remoteResults = z.array(remoteResultSchema).parse(json);
106
- } catch (e) {
106
+ } catch {
107
107
  throw new DetailedLibsqlError({
108
108
  message: await getUnexpectedResponseMessage(res),
109
109
  code: KNOWN_ERROR_CODES.SQL_QUERY_FAILED
@@ -148,7 +148,7 @@ async function parseRemoteError(response) {
148
148
  let error;
149
149
  try {
150
150
  error = errorSchema.parse(await response.clone().json()).error;
151
- } catch (e) {
151
+ } catch {
152
152
  return new DetailedLibsqlError({
153
153
  message: await getUnexpectedResponseMessage(response),
154
154
  code: KNOWN_ERROR_CODES.SQL_QUERY_FAILED
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@astrojs/db",
3
- "version": "0.11.7",
3
+ "version": "0.13.0",
4
4
  "description": "Add libSQL and Astro Studio support to your Astro site",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -62,9 +62,8 @@
62
62
  "astro-integration"
63
63
  ],
64
64
  "dependencies": {
65
- "@libsql/client": "^0.6.2",
65
+ "@libsql/client": "^0.9.0",
66
66
  "async-listen": "^3.0.1",
67
- "ci-info": "^4.0.0",
68
67
  "deep-diff": "^1.0.2",
69
68
  "drizzle-orm": "^0.31.2",
70
69
  "github-slugger": "^2.0.0",
@@ -74,20 +73,16 @@
74
73
  "ora": "^8.0.1",
75
74
  "prompts": "^2.4.2",
76
75
  "strip-ansi": "^7.1.0",
77
- "yargs-parser": "^21.1.1",
78
76
  "zod": "^3.23.8",
79
77
  "@astrojs/studio": "0.1.1"
80
78
  },
81
79
  "devDependencies": {
82
- "@types/chai": "^4.3.16",
83
80
  "@types/deep-diff": "^1.0.5",
84
- "@types/diff": "^5.2.1",
85
81
  "@types/prompts": "^2.4.9",
86
- "@types/yargs-parser": "^21.0.3",
87
- "cheerio": "1.0.0-rc.12",
88
- "typescript": "^5.5.2",
89
- "vite": "^5.3.1",
90
- "astro": "4.11.2",
82
+ "cheerio": "1.0.0",
83
+ "typescript": "^5.5.4",
84
+ "vite": "^5.4.0",
85
+ "astro": "4.14.0",
91
86
  "astro-scripts": "0.0.14"
92
87
  },
93
88
  "scripts": {
@@ -1,11 +0,0 @@
1
- import type { AstroIntegrationLogger } from 'astro';
2
- import type { VitePlugin } from '../utils.js';
3
- export declare function vitePluginInjectEnvTs({ srcDir, root }: {
4
- srcDir: URL;
5
- root: URL;
6
- }, logger: AstroIntegrationLogger): VitePlugin;
7
- export declare function setUpEnvTs({ srcDir, root, logger, }: {
8
- srcDir: URL;
9
- root: URL;
10
- logger: AstroIntegrationLogger;
11
- }): Promise<void>;
@@ -1,52 +0,0 @@
1
- import { existsSync } from "node:fs";
2
- import { readFile, writeFile } from "node:fs/promises";
3
- import path from "node:path";
4
- import { fileURLToPath } from "node:url";
5
- import { bold } from "kleur/colors";
6
- import { normalizePath } from "vite";
7
- import { DB_TYPES_FILE } from "../consts.js";
8
- function vitePluginInjectEnvTs({ srcDir, root }, logger) {
9
- return {
10
- name: "db-inject-env-ts",
11
- enforce: "post",
12
- async config() {
13
- await setUpEnvTs({ srcDir, root, logger });
14
- }
15
- };
16
- }
17
- async function setUpEnvTs({
18
- srcDir,
19
- root,
20
- logger
21
- }) {
22
- const envTsPath = getEnvTsPath({ srcDir });
23
- const envTsPathRelativetoRoot = normalizePath(
24
- path.relative(fileURLToPath(root), fileURLToPath(envTsPath))
25
- );
26
- if (existsSync(envTsPath)) {
27
- let typesEnvContents = await readFile(envTsPath, "utf-8");
28
- const dotAstroDir = new URL(".astro/", root);
29
- if (!existsSync(dotAstroDir)) return;
30
- const dbTypeReference = getDBTypeReference({ srcDir, dotAstroDir });
31
- if (!typesEnvContents.includes(dbTypeReference)) {
32
- typesEnvContents = `${dbTypeReference}
33
- ${typesEnvContents}`;
34
- await writeFile(envTsPath, typesEnvContents, "utf-8");
35
- logger.info(`Added ${bold(envTsPathRelativetoRoot)} types`);
36
- }
37
- }
38
- }
39
- function getDBTypeReference({ srcDir, dotAstroDir }) {
40
- const dbTypesFile = new URL(DB_TYPES_FILE, dotAstroDir);
41
- const contentTypesRelativeToSrcDir = normalizePath(
42
- path.relative(fileURLToPath(srcDir), fileURLToPath(dbTypesFile))
43
- );
44
- return `/// <reference path=${JSON.stringify(contentTypesRelativeToSrcDir)} />`;
45
- }
46
- function getEnvTsPath({ srcDir }) {
47
- return new URL("env.d.ts", srcDir);
48
- }
49
- export {
50
- setUpEnvTs,
51
- vitePluginInjectEnvTs
52
- };