@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,55 @@
1
+ import { LibsqlError } from "@libsql/client";
2
+ import { sql } from "drizzle-orm";
3
+ import { SQLiteAsyncDialect } from "drizzle-orm/sqlite-core";
4
+ import {} from "../core/types.js";
5
+ import { AstroDbError } from "./utils.js";
6
+ import { SEED_DEFAULT_EXPORT_ERROR } from "./errors.js";
7
+ import { getCreateIndexQueries, getCreateTableQuery } from "./queries.js";
8
+ const sqlite = new SQLiteAsyncDialect();
9
+ async function seedLocal({
10
+ db,
11
+ tables,
12
+ // Glob all potential seed files to catch renames and deletions.
13
+ userSeedGlob,
14
+ integrationSeedFunctions
15
+ }) {
16
+ await recreateTables({ db, tables });
17
+ const seedFunctions = [];
18
+ const seedFilePath = Object.keys(userSeedGlob)[0];
19
+ if (seedFilePath) {
20
+ const mod = userSeedGlob[seedFilePath];
21
+ if (!mod.default)
22
+ throw new AstroDbError(SEED_DEFAULT_EXPORT_ERROR(seedFilePath));
23
+ seedFunctions.push(mod.default);
24
+ }
25
+ for (const seedFn of integrationSeedFunctions) {
26
+ seedFunctions.push(seedFn);
27
+ }
28
+ for (const seed of seedFunctions) {
29
+ try {
30
+ await seed();
31
+ } catch (e) {
32
+ if (e instanceof LibsqlError) {
33
+ throw new AstroDbError(`Failed to seed database:
34
+ ${e.message}`);
35
+ }
36
+ throw e;
37
+ }
38
+ }
39
+ }
40
+ async function recreateTables({ db, tables }) {
41
+ const setupQueries = [];
42
+ for (const [name, table] of Object.entries(tables)) {
43
+ const dropQuery = sql.raw(`DROP TABLE IF EXISTS ${sqlite.escapeName(name)}`);
44
+ const createQuery = sql.raw(getCreateTableQuery(name, table));
45
+ const indexQueries = getCreateIndexQueries(name, table);
46
+ setupQueries.push(dropQuery, createQuery, ...indexQueries.map((s) => sql.raw(s)));
47
+ }
48
+ await db.batch([
49
+ db.run(sql`pragma defer_foreign_keys=true;`),
50
+ ...setupQueries.map((q) => db.run(q))
51
+ ]);
52
+ }
53
+ export {
54
+ seedLocal
55
+ };
@@ -0,0 +1,69 @@
1
+ import type { ColumnBaseConfig, ColumnDataType } from 'drizzle-orm';
2
+ import type { SQLiteColumn, SQLiteTableWithColumns } from 'drizzle-orm/sqlite-core';
3
+ import type { ColumnsConfig, DBColumn, OutputColumnsConfig } from '../core/types.js';
4
+ type GeneratedConfig<T extends ColumnDataType = ColumnDataType> = Pick<ColumnBaseConfig<T, string>, 'name' | 'tableName' | 'notNull' | 'hasDefault'>;
5
+ export type AstroText<T extends GeneratedConfig<'string'>> = SQLiteColumn<T & {
6
+ data: string;
7
+ dataType: 'string';
8
+ columnType: 'SQLiteText';
9
+ driverParam: string;
10
+ enumValues: never;
11
+ baseColumn: never;
12
+ }>;
13
+ export type AstroDate<T extends GeneratedConfig<'custom'>> = SQLiteColumn<T & {
14
+ data: Date;
15
+ dataType: 'custom';
16
+ columnType: 'SQLiteCustomColumn';
17
+ driverParam: string;
18
+ enumValues: never;
19
+ baseColumn: never;
20
+ }>;
21
+ export type AstroBoolean<T extends GeneratedConfig<'boolean'>> = SQLiteColumn<T & {
22
+ data: boolean;
23
+ dataType: 'boolean';
24
+ columnType: 'SQLiteBoolean';
25
+ driverParam: number;
26
+ enumValues: never;
27
+ baseColumn: never;
28
+ }>;
29
+ export type AstroNumber<T extends GeneratedConfig<'number'>> = SQLiteColumn<T & {
30
+ data: number;
31
+ dataType: 'number';
32
+ columnType: 'SQLiteInteger';
33
+ driverParam: number;
34
+ enumValues: never;
35
+ baseColumn: never;
36
+ }>;
37
+ export type AstroJson<T extends GeneratedConfig<'custom'>> = SQLiteColumn<T & {
38
+ data: unknown;
39
+ dataType: 'custom';
40
+ columnType: 'SQLiteCustomColumn';
41
+ driverParam: string;
42
+ enumValues: never;
43
+ baseColumn: never;
44
+ }>;
45
+ export type Column<T extends DBColumn['type'], S extends GeneratedConfig> = T extends 'boolean' ? AstroBoolean<S> : T extends 'number' ? AstroNumber<S> : T extends 'text' ? AstroText<S> : T extends 'date' ? AstroDate<S> : T extends 'json' ? AstroJson<S> : never;
46
+ export type Table<TTableName extends string, TColumns extends OutputColumnsConfig | ColumnsConfig> = SQLiteTableWithColumns<{
47
+ name: TTableName;
48
+ schema: undefined;
49
+ dialect: 'sqlite';
50
+ columns: {
51
+ [K in Extract<keyof TColumns, string>]: Column<TColumns[K]['type'], {
52
+ tableName: TTableName;
53
+ name: K;
54
+ hasDefault: TColumns[K]['schema'] extends {
55
+ default: NonNullable<unknown>;
56
+ } ? true : TColumns[K]['schema'] extends {
57
+ primaryKey: true;
58
+ } ? true : false;
59
+ notNull: TColumns[K]['schema']['optional'] extends true ? false : true;
60
+ }>;
61
+ };
62
+ }>;
63
+ export declare const SERIALIZED_SQL_KEY = "__serializedSQL";
64
+ export type SerializedSQL = {
65
+ [SERIALIZED_SQL_KEY]: true;
66
+ sql: string;
67
+ };
68
+ export declare function isSerializedSQL(value: any): value is SerializedSQL;
69
+ export {};
@@ -0,0 +1,8 @@
1
+ const SERIALIZED_SQL_KEY = "__serializedSQL";
2
+ function isSerializedSQL(value) {
3
+ return typeof value === "object" && value !== null && SERIALIZED_SQL_KEY in value;
4
+ }
5
+ export {
6
+ SERIALIZED_SQL_KEY,
7
+ isSerializedSQL
8
+ };
@@ -0,0 +1,8 @@
1
+ import { AstroError } from 'astro/errors';
2
+ /**
3
+ * Small wrapper around fetch that throws an error if the response is not OK. Allows for custom error handling as well through the onNotOK callback.
4
+ */
5
+ export declare function safeFetch(url: Parameters<typeof fetch>[0], options?: Parameters<typeof fetch>[1], onNotOK?: (response: Response) => void | Promise<void>): Promise<Response>;
6
+ export declare class AstroDbError extends AstroError {
7
+ name: string;
8
+ }
@@ -0,0 +1,17 @@
1
+ import { AstroError } from "astro/errors";
2
+ async function safeFetch(url, options = {}, onNotOK = () => {
3
+ throw new Error(`Request to ${url} returned a non-OK status code.`);
4
+ }) {
5
+ const response = await fetch(url, options);
6
+ if (!response.ok) {
7
+ await onNotOK(response);
8
+ }
9
+ return response;
10
+ }
11
+ class AstroDbError extends AstroError {
12
+ name = "Astro DB Error";
13
+ }
14
+ export {
15
+ AstroDbError,
16
+ safeFetch
17
+ };
@@ -0,0 +1,2 @@
1
+ export { defineDbIntegration } from './core/utils.js';
2
+ export { asDrizzleTable } from './runtime/index.js';
package/dist/utils.js ADDED
@@ -0,0 +1,6 @@
1
+ import { defineDbIntegration } from "./core/utils.js";
2
+ import { asDrizzleTable } from "./runtime/index.js";
3
+ export {
4
+ asDrizzleTable,
5
+ defineDbIntegration
6
+ };
package/index.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ import './virtual.js';
2
+
3
+ export { default, cli } from './dist/index.js';
package/package.json ADDED
@@ -0,0 +1,95 @@
1
+ {
2
+ "name": "@astrojs/db",
3
+ "version": "0.0.0-10646-20240402132948",
4
+ "description": "",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "author": "withastro",
8
+ "types": "./index.d.ts",
9
+ "main": "./dist/index.js",
10
+ "exports": {
11
+ ".": {
12
+ "types": "./index.d.ts",
13
+ "import": "./dist/index.js"
14
+ },
15
+ "./utils": {
16
+ "types": "./dist/utils.d.ts",
17
+ "import": "./dist/utils.js"
18
+ },
19
+ "./runtime": {
20
+ "types": "./dist/runtime/index.d.ts",
21
+ "import": "./dist/runtime/index.js"
22
+ },
23
+ "./dist/runtime/config.js": {
24
+ "import": "./dist/runtime/config.js"
25
+ },
26
+ "./types": {
27
+ "types": "./dist/core/types.d.ts",
28
+ "import": "./dist/core/types.js"
29
+ },
30
+ "./package.json": "./package.json"
31
+ },
32
+ "typesVersions": {
33
+ "*": {
34
+ ".": [
35
+ "./index.d.ts"
36
+ ],
37
+ "types": [
38
+ "./dist/types.d.ts"
39
+ ],
40
+ "utils": [
41
+ "./dist/utils.d.ts"
42
+ ],
43
+ "runtime": [
44
+ "./dist/runtime/index.d.ts"
45
+ ]
46
+ }
47
+ },
48
+ "files": [
49
+ "index.d.ts",
50
+ "virtual.d.ts",
51
+ "dist"
52
+ ],
53
+ "keywords": [
54
+ "withastro",
55
+ "astro-integration"
56
+ ],
57
+ "dependencies": {
58
+ "@libsql/client": "^0.5.5",
59
+ "async-listen": "^3.0.1",
60
+ "deep-diff": "^1.0.2",
61
+ "drizzle-orm": "^0.30.4",
62
+ "github-slugger": "^2.0.0",
63
+ "kleur": "^4.1.5",
64
+ "nanoid": "^5.0.1",
65
+ "open": "^10.0.3",
66
+ "ora": "^7.0.1",
67
+ "prompts": "^2.4.2",
68
+ "strip-ansi": "^7.1.0",
69
+ "yargs-parser": "^21.1.1",
70
+ "zod": "^3.22.4"
71
+ },
72
+ "devDependencies": {
73
+ "@types/chai": "^4.3.6",
74
+ "@types/deep-diff": "^1.0.5",
75
+ "@types/diff": "^5.0.8",
76
+ "@types/mocha": "^10.0.2",
77
+ "@types/prompts": "^2.4.8",
78
+ "@types/yargs-parser": "^21.0.3",
79
+ "chai": "^4.3.10",
80
+ "cheerio": "1.0.0-rc.12",
81
+ "mocha": "^10.2.0",
82
+ "typescript": "^5.2.2",
83
+ "vite": "^5.1.4",
84
+ "astro-scripts": "0.0.14",
85
+ "astro": "4.5.13"
86
+ },
87
+ "scripts": {
88
+ "types:config": "tsc -p ./tsconfig.config-types.json",
89
+ "build": "astro-scripts build \"src/**/*.ts\" && tsc && pnpm types:config",
90
+ "build:ci": "astro-scripts build \"src/**/*.ts\"",
91
+ "dev": "astro-scripts dev \"src/**/*.ts\"",
92
+ "test": "mocha --exit --timeout 20000 \"test/*.js\" \"test/unit/**/*.js\"",
93
+ "test:match": "mocha --timeout 20000 \"test/*.js\" \"test/unit/*.js\" -g"
94
+ }
95
+ }
package/virtual.d.ts ADDED
@@ -0,0 +1,45 @@
1
+ declare module 'astro:db' {
2
+ type RuntimeConfig = typeof import('./dist/_internal/runtime/config.js');
3
+
4
+ export const db: import('./dist/_internal/runtime/config.js').Database;
5
+ export const dbUrl: string;
6
+
7
+ export const sql: RuntimeConfig['sql'];
8
+ export const NOW: RuntimeConfig['NOW'];
9
+ export const TRUE: RuntimeConfig['TRUE'];
10
+ export const FALSE: RuntimeConfig['FALSE'];
11
+ export const column: RuntimeConfig['column'];
12
+ export const defineDb: RuntimeConfig['defineDb'];
13
+ export const defineTable: RuntimeConfig['defineTable'];
14
+ export const isDbError: RuntimeConfig['isDbError'];
15
+
16
+ export const eq: RuntimeConfig['eq'];
17
+ export const gt: RuntimeConfig['gt'];
18
+ export const gte: RuntimeConfig['gte'];
19
+ export const lt: RuntimeConfig['lt'];
20
+ export const lte: RuntimeConfig['lte'];
21
+ export const ne: RuntimeConfig['ne'];
22
+ export const isNull: RuntimeConfig['isNull'];
23
+ export const isNotNull: RuntimeConfig['isNotNull'];
24
+ export const inArray: RuntimeConfig['inArray'];
25
+ export const notInArray: RuntimeConfig['notInArray'];
26
+ export const exists: RuntimeConfig['exists'];
27
+ export const notExists: RuntimeConfig['notExists'];
28
+ export const between: RuntimeConfig['between'];
29
+ export const notBetween: RuntimeConfig['notBetween'];
30
+ export const like: RuntimeConfig['like'];
31
+ export const notIlike: RuntimeConfig['notIlike'];
32
+ export const not: RuntimeConfig['not'];
33
+ export const asc: RuntimeConfig['asc'];
34
+ export const desc: RuntimeConfig['desc'];
35
+ export const and: RuntimeConfig['and'];
36
+ export const or: RuntimeConfig['or'];
37
+ export const count: RuntimeConfig['count'];
38
+ export const countDistinct: RuntimeConfig['countDistinct'];
39
+ export const avg: RuntimeConfig['avg'];
40
+ export const avgDistinct: RuntimeConfig['avgDistinct'];
41
+ export const sum: RuntimeConfig['sum'];
42
+ export const sumDistinct: RuntimeConfig['sumDistinct'];
43
+ export const max: RuntimeConfig['max'];
44
+ export const min: RuntimeConfig['min'];
45
+ }