@astrojs/db 0.0.0-10646-20240402132948

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.
Files changed (78) hide show
  1. package/LICENSE +59 -0
  2. package/README.md +38 -0
  3. package/dist/_internal/core/integration/error-map.d.ts +6 -0
  4. package/dist/_internal/core/schemas.d.ts +4034 -0
  5. package/dist/_internal/core/types.d.ts +59 -0
  6. package/dist/_internal/core/utils.d.ts +20 -0
  7. package/dist/_internal/runtime/config.d.ts +154 -0
  8. package/dist/_internal/runtime/types.d.ts +69 -0
  9. package/dist/core/cli/commands/execute/index.d.ts +8 -0
  10. package/dist/core/cli/commands/execute/index.js +66 -0
  11. package/dist/core/cli/commands/link/index.d.ts +20 -0
  12. package/dist/core/cli/commands/link/index.js +252 -0
  13. package/dist/core/cli/commands/login/index.d.ts +8 -0
  14. package/dist/core/cli/commands/login/index.js +55 -0
  15. package/dist/core/cli/commands/logout/index.d.ts +1 -0
  16. package/dist/core/cli/commands/logout/index.js +9 -0
  17. package/dist/core/cli/commands/push/index.d.ts +8 -0
  18. package/dist/core/cli/commands/push/index.js +93 -0
  19. package/dist/core/cli/commands/shell/index.d.ts +8 -0
  20. package/dist/core/cli/commands/shell/index.js +33 -0
  21. package/dist/core/cli/commands/verify/index.d.ts +8 -0
  22. package/dist/core/cli/commands/verify/index.js +46 -0
  23. package/dist/core/cli/index.d.ts +6 -0
  24. package/dist/core/cli/index.js +76 -0
  25. package/dist/core/cli/migration-queries.d.ts +23 -0
  26. package/dist/core/cli/migration-queries.js +386 -0
  27. package/dist/core/cli/print-help.d.ts +11 -0
  28. package/dist/core/cli/print-help.js +55 -0
  29. package/dist/core/consts.d.ts +8 -0
  30. package/dist/core/consts.js +21 -0
  31. package/dist/core/errors.d.ts +10 -0
  32. package/dist/core/errors.js +56 -0
  33. package/dist/core/integration/error-map.d.ts +6 -0
  34. package/dist/core/integration/error-map.js +79 -0
  35. package/dist/core/integration/file-url.d.ts +2 -0
  36. package/dist/core/integration/file-url.js +81 -0
  37. package/dist/core/integration/index.d.ts +2 -0
  38. package/dist/core/integration/index.js +160 -0
  39. package/dist/core/integration/typegen.d.ts +7 -0
  40. package/dist/core/integration/typegen.js +33 -0
  41. package/dist/core/integration/vite-plugin-db.d.ts +39 -0
  42. package/dist/core/integration/vite-plugin-db.js +134 -0
  43. package/dist/core/integration/vite-plugin-inject-env-ts.d.ts +11 -0
  44. package/dist/core/integration/vite-plugin-inject-env-ts.js +53 -0
  45. package/dist/core/load-file.d.ts +253 -0
  46. package/dist/core/load-file.js +170 -0
  47. package/dist/core/schemas.d.ts +4034 -0
  48. package/dist/core/schemas.js +186 -0
  49. package/dist/core/tokens.d.ts +11 -0
  50. package/dist/core/tokens.js +181 -0
  51. package/dist/core/types.d.ts +59 -0
  52. package/dist/core/types.js +0 -0
  53. package/dist/core/utils.d.ts +20 -0
  54. package/dist/core/utils.js +32 -0
  55. package/dist/index.d.ts +4 -0
  56. package/dist/index.js +8 -0
  57. package/dist/runtime/config.js +111 -0
  58. package/dist/runtime/db-client.d.ts +6 -0
  59. package/dist/runtime/db-client.js +148 -0
  60. package/dist/runtime/drizzle.d.ts +1 -0
  61. package/dist/runtime/drizzle.js +48 -0
  62. package/dist/runtime/errors.d.ts +5 -0
  63. package/dist/runtime/errors.js +31 -0
  64. package/dist/runtime/index.d.ts +26 -0
  65. package/dist/runtime/index.js +131 -0
  66. package/dist/runtime/queries.d.ts +71 -0
  67. package/dist/runtime/queries.js +169 -0
  68. package/dist/runtime/seed-local.d.ts +10 -0
  69. package/dist/runtime/seed-local.js +55 -0
  70. package/dist/runtime/types.d.ts +69 -0
  71. package/dist/runtime/types.js +8 -0
  72. package/dist/runtime/utils.d.ts +8 -0
  73. package/dist/runtime/utils.js +17 -0
  74. package/dist/utils.d.ts +2 -0
  75. package/dist/utils.js +6 -0
  76. package/index.d.ts +3 -0
  77. package/package.json +95 -0
  78. package/virtual.d.ts +45 -0
@@ -0,0 +1,253 @@
1
+ import type { AstroConfig } from 'astro';
2
+ /**
3
+ * Load a user’s `astro:db` configuration file and additional configuration files provided by integrations.
4
+ */
5
+ export declare function resolveDbConfig({ root, integrations, }: Pick<AstroConfig, 'root' | 'integrations'>): Promise<{
6
+ /** Resolved `astro:db` config, including tables added by integrations. */
7
+ dbConfig: {
8
+ tables: Record<string, {
9
+ indexes: Record<string, {
10
+ on: (string | string[]) & (string | string[] | undefined);
11
+ unique?: boolean | undefined;
12
+ }>;
13
+ deprecated: boolean;
14
+ columns: Record<string, {
15
+ type: "boolean";
16
+ schema: {
17
+ optional: boolean;
18
+ unique: boolean;
19
+ deprecated: boolean;
20
+ name?: string | undefined;
21
+ label?: string | undefined;
22
+ collection?: string | undefined;
23
+ default?: boolean | import("../runtime/types.js").SerializedSQL | undefined;
24
+ };
25
+ } | {
26
+ type: "number";
27
+ schema: ({
28
+ unique: boolean;
29
+ deprecated: boolean;
30
+ name?: string | undefined;
31
+ label?: string | undefined;
32
+ collection?: string | undefined;
33
+ } & {
34
+ optional: boolean;
35
+ primaryKey: false;
36
+ default?: number | import("../runtime/types.js").SerializedSQL | undefined;
37
+ } & {
38
+ references?: any | undefined;
39
+ }) | ({
40
+ unique: boolean;
41
+ deprecated: boolean;
42
+ name?: string | undefined;
43
+ label?: string | undefined;
44
+ collection?: string | undefined;
45
+ } & {
46
+ primaryKey: true;
47
+ optional?: false | undefined;
48
+ default?: undefined;
49
+ } & {
50
+ references?: any | undefined;
51
+ });
52
+ } | {
53
+ type: "text";
54
+ schema: ({
55
+ unique: boolean;
56
+ deprecated: boolean;
57
+ name?: string | undefined;
58
+ label?: string | undefined;
59
+ collection?: string | undefined;
60
+ default?: string | import("../runtime/types.js").SerializedSQL | undefined;
61
+ multiline?: boolean | undefined;
62
+ } & {
63
+ optional: boolean;
64
+ primaryKey: false;
65
+ } & {
66
+ references?: any | undefined;
67
+ }) | ({
68
+ unique: boolean;
69
+ deprecated: boolean;
70
+ name?: string | undefined;
71
+ label?: string | undefined;
72
+ collection?: string | undefined;
73
+ default?: string | import("../runtime/types.js").SerializedSQL | undefined;
74
+ multiline?: boolean | undefined;
75
+ } & {
76
+ primaryKey: true;
77
+ optional?: false | undefined;
78
+ } & {
79
+ references?: any | undefined;
80
+ });
81
+ } | {
82
+ type: "date";
83
+ schema: {
84
+ optional: boolean;
85
+ unique: boolean;
86
+ deprecated: boolean;
87
+ name?: string | undefined;
88
+ label?: string | undefined;
89
+ collection?: string | undefined;
90
+ default?: string | import("../runtime/types.js").SerializedSQL | undefined;
91
+ };
92
+ } | {
93
+ type: "json";
94
+ schema: {
95
+ optional: boolean;
96
+ unique: boolean;
97
+ deprecated: boolean;
98
+ name?: string | undefined;
99
+ label?: string | undefined;
100
+ collection?: string | undefined;
101
+ default?: unknown;
102
+ };
103
+ }>;
104
+ foreignKeys?: (Omit<{
105
+ columns: import("./schemas.js").MaybeArray<string>;
106
+ references: () => import("./schemas.js").MaybeArray<Omit<{
107
+ type: "number";
108
+ schema: ({
109
+ name?: string | undefined;
110
+ label?: string | undefined;
111
+ unique?: boolean | undefined;
112
+ deprecated?: boolean | undefined;
113
+ collection?: string | undefined;
114
+ } & {
115
+ primaryKey?: false | undefined;
116
+ optional?: boolean | undefined;
117
+ default?: number | import("drizzle-orm").SQL<any> | undefined;
118
+ } & {
119
+ references?: (() => any) | undefined;
120
+ }) | ({
121
+ name?: string | undefined;
122
+ label?: string | undefined;
123
+ unique?: boolean | undefined;
124
+ deprecated?: boolean | undefined;
125
+ collection?: string | undefined;
126
+ } & {
127
+ primaryKey: true;
128
+ optional?: false | undefined;
129
+ default?: undefined;
130
+ } & {
131
+ references?: (() => any) | undefined;
132
+ });
133
+ } | {
134
+ type: "text";
135
+ schema: ({
136
+ name?: string | undefined;
137
+ label?: string | undefined;
138
+ unique?: boolean | undefined;
139
+ deprecated?: boolean | undefined;
140
+ collection?: string | undefined;
141
+ default?: string | import("drizzle-orm").SQL<any> | undefined;
142
+ multiline?: boolean | undefined;
143
+ } & {
144
+ primaryKey?: false | undefined;
145
+ optional?: boolean | undefined;
146
+ } & {
147
+ references?: (() => any) | undefined;
148
+ }) | ({
149
+ name?: string | undefined;
150
+ label?: string | undefined;
151
+ unique?: boolean | undefined;
152
+ deprecated?: boolean | undefined;
153
+ collection?: string | undefined;
154
+ default?: string | import("drizzle-orm").SQL<any> | undefined;
155
+ multiline?: boolean | undefined;
156
+ } & {
157
+ primaryKey: true;
158
+ optional?: false | undefined;
159
+ } & {
160
+ references?: (() => any) | undefined;
161
+ });
162
+ }, "references">>;
163
+ }, "references"> & {
164
+ references: import("./schemas.js").MaybeArray<Omit<{
165
+ type: "number";
166
+ schema: ({
167
+ unique: boolean;
168
+ deprecated: boolean;
169
+ name?: string | undefined;
170
+ label?: string | undefined;
171
+ collection?: string | undefined;
172
+ } & {
173
+ optional: boolean;
174
+ primaryKey: false;
175
+ default?: number | import("../runtime/types.js").SerializedSQL | undefined;
176
+ } & {
177
+ references?: any | undefined;
178
+ }) | ({
179
+ unique: boolean;
180
+ deprecated: boolean;
181
+ name?: string | undefined;
182
+ label?: string | undefined;
183
+ collection?: string | undefined;
184
+ } & {
185
+ primaryKey: true;
186
+ optional?: false | undefined;
187
+ default?: undefined;
188
+ } & {
189
+ references?: any | undefined;
190
+ });
191
+ } | {
192
+ type: "text";
193
+ schema: ({
194
+ unique: boolean;
195
+ deprecated: boolean;
196
+ name?: string | undefined;
197
+ label?: string | undefined;
198
+ collection?: string | undefined;
199
+ default?: string | import("../runtime/types.js").SerializedSQL | undefined;
200
+ multiline?: boolean | undefined;
201
+ } & {
202
+ optional: boolean;
203
+ primaryKey: false;
204
+ } & {
205
+ references?: any | undefined;
206
+ }) | ({
207
+ unique: boolean;
208
+ deprecated: boolean;
209
+ name?: string | undefined;
210
+ label?: string | undefined;
211
+ collection?: string | undefined;
212
+ default?: string | import("../runtime/types.js").SerializedSQL | undefined;
213
+ multiline?: boolean | undefined;
214
+ } & {
215
+ primaryKey: true;
216
+ optional?: false | undefined;
217
+ } & {
218
+ references?: any | undefined;
219
+ });
220
+ }, "references">>;
221
+ })[] | undefined;
222
+ }>;
223
+ };
224
+ /** Dependencies imported into the user config file. */
225
+ dependencies: string[];
226
+ /** Additional `astro:db` seed file paths provided by integrations. */
227
+ integrationSeedPaths: (string | URL)[];
228
+ }>;
229
+ /**
230
+ * Bundle arbitrary `mjs` or `ts` file.
231
+ * Simplified fork from Vite's `bundleConfigFile` function.
232
+ *
233
+ * @see https://github.com/vitejs/vite/blob/main/packages/vite/src/node/config.ts#L961
234
+ */
235
+ export declare function bundleFile({ fileUrl, root, virtualModContents, }: {
236
+ fileUrl: URL;
237
+ root: URL;
238
+ virtualModContents: string;
239
+ }): Promise<{
240
+ code: string;
241
+ dependencies: string[];
242
+ }>;
243
+ /**
244
+ * Forked from Vite config loader, replacing CJS-based path concat with ESM only
245
+ *
246
+ * @see https://github.com/vitejs/vite/blob/main/packages/vite/src/node/config.ts#L1074
247
+ */
248
+ export declare function importBundledFile({ code, root, }: {
249
+ code: string;
250
+ root: URL;
251
+ }): Promise<{
252
+ default?: unknown;
253
+ }>;
@@ -0,0 +1,170 @@
1
+ import { existsSync } from "node:fs";
2
+ import { unlink, writeFile } from "node:fs/promises";
3
+ import { createRequire } from "node:module";
4
+ import { fileURLToPath, pathToFileURL } from "node:url";
5
+ import { build as esbuild } from "esbuild";
6
+ import { CONFIG_FILE_NAMES, VIRTUAL_MODULE_ID } from "./consts.js";
7
+ import { INTEGRATION_TABLE_CONFLICT_ERROR } from "./errors.js";
8
+ import { errorMap } from "./integration/error-map.js";
9
+ import { getConfigVirtualModContents } from "./integration/vite-plugin-db.js";
10
+ import { dbConfigSchema } from "./schemas.js";
11
+ import {} from "./types.js";
12
+ import { getDbDirectoryUrl } from "./utils.js";
13
+ const isDbIntegration = (integration) => "astro:db:setup" in integration.hooks;
14
+ async function resolveDbConfig({
15
+ root,
16
+ integrations
17
+ }) {
18
+ const { mod, dependencies } = await loadUserConfigFile(root);
19
+ const userDbConfig = dbConfigSchema.parse(mod?.default ?? {}, { errorMap });
20
+ const dbConfig = { tables: userDbConfig.tables ?? {} };
21
+ const integrationDbConfigPaths = [];
22
+ const integrationSeedPaths = [];
23
+ for (const integration of integrations) {
24
+ if (!isDbIntegration(integration))
25
+ continue;
26
+ const { name, hooks } = integration;
27
+ if (hooks["astro:db:setup"]) {
28
+ hooks["astro:db:setup"]({
29
+ extendDb({ configEntrypoint, seedEntrypoint }) {
30
+ if (configEntrypoint) {
31
+ integrationDbConfigPaths.push({ name, configEntrypoint });
32
+ }
33
+ if (seedEntrypoint) {
34
+ integrationSeedPaths.push(seedEntrypoint);
35
+ }
36
+ }
37
+ });
38
+ }
39
+ }
40
+ for (const { name, configEntrypoint } of integrationDbConfigPaths) {
41
+ const loadedConfig = await loadIntegrationConfigFile(root, configEntrypoint);
42
+ const integrationDbConfig = dbConfigSchema.parse(loadedConfig.mod?.default ?? {}, {
43
+ errorMap
44
+ });
45
+ for (const key in integrationDbConfig.tables) {
46
+ if (key in dbConfig.tables) {
47
+ const isUserConflict = key in (userDbConfig.tables ?? {});
48
+ throw new Error(INTEGRATION_TABLE_CONFLICT_ERROR(name, key, isUserConflict));
49
+ } else {
50
+ dbConfig.tables[key] = integrationDbConfig.tables[key];
51
+ }
52
+ }
53
+ }
54
+ return {
55
+ /** Resolved `astro:db` config, including tables added by integrations. */
56
+ dbConfig,
57
+ /** Dependencies imported into the user config file. */
58
+ dependencies,
59
+ /** Additional `astro:db` seed file paths provided by integrations. */
60
+ integrationSeedPaths
61
+ };
62
+ }
63
+ async function loadUserConfigFile(root) {
64
+ let configFileUrl;
65
+ for (const fileName of CONFIG_FILE_NAMES) {
66
+ const fileUrl = new URL(fileName, getDbDirectoryUrl(root));
67
+ if (existsSync(fileUrl)) {
68
+ configFileUrl = fileUrl;
69
+ }
70
+ }
71
+ return await loadAndBundleDbConfigFile({ root, fileUrl: configFileUrl });
72
+ }
73
+ async function loadIntegrationConfigFile(root, filePathOrUrl) {
74
+ let fileUrl;
75
+ if (typeof filePathOrUrl === "string") {
76
+ const { resolve } = createRequire(root);
77
+ const resolvedFilePath = resolve(filePathOrUrl);
78
+ fileUrl = pathToFileURL(resolvedFilePath);
79
+ } else {
80
+ fileUrl = filePathOrUrl;
81
+ }
82
+ return await loadAndBundleDbConfigFile({ root, fileUrl });
83
+ }
84
+ async function loadAndBundleDbConfigFile({
85
+ root,
86
+ fileUrl
87
+ }) {
88
+ if (!fileUrl) {
89
+ return { mod: void 0, dependencies: [] };
90
+ }
91
+ const { code, dependencies } = await bundleFile({
92
+ virtualModContents: getConfigVirtualModContents(),
93
+ root,
94
+ fileUrl
95
+ });
96
+ return {
97
+ mod: await importBundledFile({ code, root }),
98
+ dependencies
99
+ };
100
+ }
101
+ async function bundleFile({
102
+ fileUrl,
103
+ root,
104
+ virtualModContents
105
+ }) {
106
+ const result = await esbuild({
107
+ absWorkingDir: process.cwd(),
108
+ entryPoints: [fileURLToPath(fileUrl)],
109
+ outfile: "out.js",
110
+ packages: "external",
111
+ write: false,
112
+ target: ["node16"],
113
+ platform: "node",
114
+ bundle: true,
115
+ format: "esm",
116
+ sourcemap: "inline",
117
+ metafile: true,
118
+ define: {
119
+ "import.meta.env.ASTRO_STUDIO_REMOTE_DB_URL": "undefined"
120
+ },
121
+ plugins: [
122
+ {
123
+ name: "resolve-astro-db",
124
+ setup(build) {
125
+ build.onResolve({ filter: /^astro:db$/ }, ({ path }) => {
126
+ return { path, namespace: VIRTUAL_MODULE_ID };
127
+ });
128
+ build.onLoad({ namespace: VIRTUAL_MODULE_ID, filter: /.*/ }, () => {
129
+ return {
130
+ contents: virtualModContents,
131
+ // Needed to resolve runtime dependencies
132
+ resolveDir: fileURLToPath(root)
133
+ };
134
+ });
135
+ }
136
+ }
137
+ ]
138
+ });
139
+ const file = result.outputFiles[0];
140
+ if (!file) {
141
+ throw new Error(`Unexpected: no output file`);
142
+ }
143
+ return {
144
+ code: file.text,
145
+ dependencies: Object.keys(result.metafile.inputs)
146
+ };
147
+ }
148
+ async function importBundledFile({
149
+ code,
150
+ root
151
+ }) {
152
+ const tmpFileUrl = new URL(`./db.timestamp-${Date.now()}.mjs`, root);
153
+ await writeFile(tmpFileUrl, code, { encoding: "utf8" });
154
+ try {
155
+ return await import(
156
+ /* @vite-ignore */
157
+ tmpFileUrl.toString()
158
+ );
159
+ } finally {
160
+ try {
161
+ await unlink(tmpFileUrl);
162
+ } catch {
163
+ }
164
+ }
165
+ }
166
+ export {
167
+ bundleFile,
168
+ importBundledFile,
169
+ resolveDbConfig
170
+ };