@astrojs/db 0.1.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.
Files changed (56) hide show
  1. package/LICENSE +59 -0
  2. package/components/Renderer.astro +14 -0
  3. package/components/astro-env.d.ts +1 -0
  4. package/components/index.ts +2 -0
  5. package/components/tsconfig.json +7 -0
  6. package/config-augment.d.ts +4 -0
  7. package/dist/cli/commands/push/index.d.ts +6 -0
  8. package/dist/cli/commands/push/index.js +144 -0
  9. package/dist/cli/commands/sync/index.d.ts +6 -0
  10. package/dist/cli/commands/sync/index.js +45 -0
  11. package/dist/cli/commands/verify/index.d.ts +6 -0
  12. package/dist/cli/commands/verify/index.js +25 -0
  13. package/dist/cli/index.d.ts +6 -0
  14. package/dist/cli/index.js +24 -0
  15. package/dist/cli/queries.d.ts +19 -0
  16. package/dist/cli/queries.js +453 -0
  17. package/dist/cli/seed.d.ts +6 -0
  18. package/dist/cli/sync/admin.d.ts +33 -0
  19. package/dist/cli/sync/index.d.ts +1 -0
  20. package/dist/cli/sync/index.js +0 -0
  21. package/dist/cli/sync/migrate.d.ts +1 -0
  22. package/dist/cli/sync/migrate.js +0 -0
  23. package/dist/cli/sync/queries.d.ts +19 -0
  24. package/dist/cli/sync/remote-db.d.ts +1 -0
  25. package/dist/config.d.ts +1149 -0
  26. package/dist/config.js +53 -0
  27. package/dist/consts.d.ts +6 -0
  28. package/dist/consts.js +19 -0
  29. package/dist/error-map.d.ts +6 -0
  30. package/dist/error-map.js +79 -0
  31. package/dist/errors.d.ts +3 -0
  32. package/dist/errors.js +15 -0
  33. package/dist/index.d.ts +3 -0
  34. package/dist/index.js +10 -0
  35. package/dist/integration.d.ts +2 -0
  36. package/dist/integration.js +68 -0
  37. package/dist/internal-drizzle.d.ts +1 -0
  38. package/dist/internal-drizzle.js +48 -0
  39. package/dist/internal.d.ts +50 -0
  40. package/dist/internal.js +250 -0
  41. package/dist/load-astro-config.d.ts +6 -0
  42. package/dist/load-astro-config.js +79 -0
  43. package/dist/migrations.d.ts +9 -0
  44. package/dist/migrations.js +41 -0
  45. package/dist/typegen.d.ts +5 -0
  46. package/dist/typegen.js +57 -0
  47. package/dist/types.d.ts +1367 -0
  48. package/dist/types.js +58 -0
  49. package/dist/utils.d.ts +59 -0
  50. package/dist/utils.js +84 -0
  51. package/dist/vite-plugin-db.d.ts +19 -0
  52. package/dist/vite-plugin-db.js +66 -0
  53. package/dist/vite-plugin-inject-env-ts.d.ts +9 -0
  54. package/dist/vite-plugin-inject-env-ts.js +49 -0
  55. package/index.d.ts +3 -0
  56. package/package.json +72 -0
@@ -0,0 +1,79 @@
1
+ import fs from "node:fs";
2
+ import path from "node:path";
3
+ import { pathToFileURL } from "node:url";
4
+ import { bold, red } from "kleur/colors";
5
+ import { createServer } from "vite";
6
+ async function loadAstroConfig(root) {
7
+ const configPath = search(root);
8
+ if (!configPath)
9
+ return {};
10
+ try {
11
+ return await loadConfigWithVite(configPath);
12
+ } catch (e) {
13
+ console.error(`${bold(red("[astro]"))} Unable to load Astro config.
14
+ `);
15
+ throw e;
16
+ }
17
+ }
18
+ function search(root) {
19
+ const paths = [
20
+ "astro.config.mjs",
21
+ "astro.config.js",
22
+ "astro.config.ts",
23
+ "astro.config.mts",
24
+ "astro.config.cjs",
25
+ "astro.config.cts"
26
+ ].map((p) => path.join(root, p));
27
+ for (const file of paths) {
28
+ if (fs.existsSync(file)) {
29
+ return file;
30
+ }
31
+ }
32
+ }
33
+ async function loadConfigWithVite(configPath) {
34
+ if (/\.[cm]?js$/.test(configPath)) {
35
+ try {
36
+ const config = await import(
37
+ /* @vite-ignore */
38
+ pathToFileURL(configPath).toString() + "?t=" + Date.now()
39
+ );
40
+ return config.default ?? {};
41
+ } catch (e) {
42
+ }
43
+ }
44
+ let server;
45
+ try {
46
+ server = await createViteServer();
47
+ const mod = await server.ssrLoadModule(configPath, { fixStacktrace: true });
48
+ return mod.default ?? {};
49
+ } finally {
50
+ if (server) {
51
+ await server.close();
52
+ }
53
+ }
54
+ }
55
+ async function createViteServer() {
56
+ const viteServer = await createServer({
57
+ server: { middlewareMode: true, hmr: false, watch: { ignored: ["**"] } },
58
+ optimizeDeps: { disabled: true },
59
+ clearScreen: false,
60
+ appType: "custom",
61
+ ssr: {
62
+ // NOTE: Vite doesn't externalize linked packages by default. During testing locally,
63
+ // these dependencies trip up Vite's dev SSR transform. Awaiting upstream feature:
64
+ // https://github.com/vitejs/vite/pull/10939
65
+ external: [
66
+ "@astrojs/tailwind",
67
+ "@astrojs/mdx",
68
+ "@astrojs/react",
69
+ "@astrojs/preact",
70
+ "@astrojs/sitemap",
71
+ "@astrojs/markdoc"
72
+ ]
73
+ }
74
+ });
75
+ return viteServer;
76
+ }
77
+ export {
78
+ loadAstroConfig
79
+ };
@@ -0,0 +1,9 @@
1
+ import type { DBCollections } from './types.js';
2
+ export declare function getMigrations(): Promise<string[]>;
3
+ export declare function loadMigration(migration: string): Promise<{
4
+ diff: any[];
5
+ db: string[];
6
+ }>;
7
+ export declare function loadInitialSnapshot(): Promise<DBCollections>;
8
+ export declare function initializeMigrationsDirectory(currentSnapshot: unknown): Promise<void>;
9
+ export declare function initializeFromMigrations(allMigrationFiles: string[]): Promise<DBCollections>;
@@ -0,0 +1,41 @@
1
+ import deepDiff from "deep-diff";
2
+ import { mkdir, readFile, readdir, writeFile } from "fs/promises";
3
+ const { applyChange } = deepDiff;
4
+ async function getMigrations() {
5
+ const migrationFiles = await readdir("./migrations").catch((err) => {
6
+ if (err.code === "ENOENT") {
7
+ return [];
8
+ }
9
+ throw err;
10
+ });
11
+ return migrationFiles;
12
+ }
13
+ async function loadMigration(migration) {
14
+ return JSON.parse(await readFile(`./migrations/${migration}`, "utf-8"));
15
+ }
16
+ async function loadInitialSnapshot() {
17
+ return JSON.parse(await readFile("./migrations/0000_snapshot.json", "utf-8"));
18
+ }
19
+ async function initializeMigrationsDirectory(currentSnapshot) {
20
+ await mkdir("./migrations", { recursive: true });
21
+ await writeFile("./migrations/0000_snapshot.json", JSON.stringify(currentSnapshot, void 0, 2));
22
+ }
23
+ async function initializeFromMigrations(allMigrationFiles) {
24
+ const prevSnapshot = await loadInitialSnapshot();
25
+ for (const migration of allMigrationFiles) {
26
+ if (migration === "0000_snapshot.json")
27
+ continue;
28
+ const migrationContent = await loadMigration(migration);
29
+ migrationContent.diff.forEach((change) => {
30
+ applyChange(prevSnapshot, {}, change);
31
+ });
32
+ }
33
+ return prevSnapshot;
34
+ }
35
+ export {
36
+ getMigrations,
37
+ initializeFromMigrations,
38
+ initializeMigrationsDirectory,
39
+ loadInitialSnapshot,
40
+ loadMigration
41
+ };
@@ -0,0 +1,5 @@
1
+ import type { DBCollections } from './types.js';
2
+ export declare function typegen({ collections, root }: {
3
+ collections: DBCollections;
4
+ root: URL;
5
+ }): Promise<void>;
@@ -0,0 +1,57 @@
1
+ import { existsSync } from "node:fs";
2
+ import { mkdir, writeFile } from "node:fs/promises";
3
+ import { DB_TYPES_FILE, DRIZZLE_MOD_IMPORT, INTERNAL_MOD_IMPORT } from "./consts.js";
4
+ async function typegen({ collections, root }) {
5
+ const content = `// This file is generated by \`studio sync\`
6
+ declare module 'astro:db' {
7
+ export const db: import(${INTERNAL_MOD_IMPORT}).SqliteDB;
8
+ export * from ${DRIZZLE_MOD_IMPORT}
9
+
10
+ ${Object.entries(collections).map(([name, collection]) => generateTableType(name, collection)).join("\n")}
11
+ }
12
+ `;
13
+ const dotAstroDir = new URL(".astro/", root);
14
+ if (!existsSync(dotAstroDir)) {
15
+ await mkdir(dotAstroDir);
16
+ }
17
+ await writeFile(new URL(DB_TYPES_FILE, dotAstroDir), content);
18
+ }
19
+ function generateTableType(name, collection) {
20
+ let tableType = ` export const ${name}: import(${INTERNAL_MOD_IMPORT}).AstroTable<{
21
+ name: ${JSON.stringify(name)};
22
+ columns: {
23
+ id: import(${INTERNAL_MOD_IMPORT}).AstroId<{
24
+ tableName: ${JSON.stringify(name)};
25
+ }>;`;
26
+ for (const [fieldName, field] of Object.entries(collection.fields)) {
27
+ const drizzleInterface = schemaTypeToDrizzleInterface(field.type);
28
+ tableType += `
29
+ ${fieldName}: import(${INTERNAL_MOD_IMPORT}).${drizzleInterface}<{
30
+ tableName: ${JSON.stringify(name)};
31
+ name: ${JSON.stringify(fieldName)};
32
+ notNull: ${field.optional ? "false" : "true"};
33
+ hasDefault: ${typeof field.default !== "undefined" ? "true" : "false"};
34
+ }>;`;
35
+ }
36
+ tableType += `
37
+ };
38
+ }>;`;
39
+ return tableType;
40
+ }
41
+ function schemaTypeToDrizzleInterface(type) {
42
+ switch (type) {
43
+ case "text":
44
+ return "AstroText";
45
+ case "number":
46
+ return "AstroNumber";
47
+ case "boolean":
48
+ return "AstroBoolean";
49
+ case "date":
50
+ return "AstroDate";
51
+ case "json":
52
+ return "AstroJson";
53
+ }
54
+ }
55
+ export {
56
+ typegen
57
+ };