@astrojs/db 0.15.1 → 0.16.1
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.
- package/dist/_internal/core/utils.d.ts +1 -10
- package/dist/_internal/runtime/types.d.ts +17 -11
- package/dist/_internal/runtime/utils.d.ts +0 -15
- package/dist/core/cli/commands/execute/index.js +5 -5
- package/dist/core/cli/commands/push/index.js +6 -40
- package/dist/core/cli/commands/shell/index.js +3 -9
- package/dist/core/cli/commands/verify/index.js +2 -7
- package/dist/core/cli/index.js +4 -16
- package/dist/core/cli/migration-queries.d.ts +1 -4
- package/dist/core/cli/migration-queries.js +8 -30
- package/dist/core/cli/print-help.js +1 -1
- package/dist/core/integration/index.js +5 -8
- package/dist/core/integration/vite-plugin-db.d.ts +3 -3
- package/dist/core/integration/vite-plugin-db.js +10 -13
- package/dist/core/load-file.d.ts +1 -1
- package/dist/core/load-file.js +0 -2
- package/dist/core/schemas.d.ts +22 -22
- package/dist/core/schemas.js +1 -1
- package/dist/core/utils.d.ts +1 -10
- package/dist/core/utils.js +2 -27
- package/dist/runtime/db-client.d.ts +5 -6
- package/dist/runtime/db-client.js +10 -167
- package/dist/runtime/types.d.ts +17 -11
- package/dist/runtime/utils.d.ts +0 -15
- package/dist/runtime/utils.js +1 -26
- package/package.json +5 -10
- package/dist/core/cli/commands/link/index.d.ts +0 -1
- package/dist/core/cli/commands/link/index.js +0 -266
- package/dist/core/cli/commands/login/index.d.ts +0 -8
- package/dist/core/cli/commands/login/index.js +0 -76
- package/dist/core/cli/commands/logout/index.d.ts +0 -1
- package/dist/core/cli/commands/logout/index.js +0 -9
package/dist/core/schemas.d.ts
CHANGED
|
@@ -321,7 +321,7 @@ export declare const dateColumnSchema: z.ZodObject<{
|
|
|
321
321
|
label?: string | undefined;
|
|
322
322
|
collection?: string | undefined;
|
|
323
323
|
}, {
|
|
324
|
-
default?: SQL<any> |
|
|
324
|
+
default?: Date | SQL<any> | undefined;
|
|
325
325
|
name?: string | undefined;
|
|
326
326
|
label?: string | undefined;
|
|
327
327
|
optional?: boolean | undefined;
|
|
@@ -343,7 +343,7 @@ export declare const dateColumnSchema: z.ZodObject<{
|
|
|
343
343
|
}, {
|
|
344
344
|
type: "date";
|
|
345
345
|
schema: {
|
|
346
|
-
default?: SQL<any> |
|
|
346
|
+
default?: Date | SQL<any> | undefined;
|
|
347
347
|
name?: string | undefined;
|
|
348
348
|
label?: string | undefined;
|
|
349
349
|
optional?: boolean | undefined;
|
|
@@ -619,7 +619,7 @@ export declare const columnSchema: z.ZodDiscriminatedUnion<"type", [z.ZodObject<
|
|
|
619
619
|
label?: string | undefined;
|
|
620
620
|
collection?: string | undefined;
|
|
621
621
|
}, {
|
|
622
|
-
default?: SQL<any> |
|
|
622
|
+
default?: Date | SQL<any> | undefined;
|
|
623
623
|
name?: string | undefined;
|
|
624
624
|
label?: string | undefined;
|
|
625
625
|
optional?: boolean | undefined;
|
|
@@ -641,7 +641,7 @@ export declare const columnSchema: z.ZodDiscriminatedUnion<"type", [z.ZodObject<
|
|
|
641
641
|
}, {
|
|
642
642
|
type: "date";
|
|
643
643
|
schema: {
|
|
644
|
-
default?: SQL<any> |
|
|
644
|
+
default?: Date | SQL<any> | undefined;
|
|
645
645
|
name?: string | undefined;
|
|
646
646
|
label?: string | undefined;
|
|
647
647
|
optional?: boolean | undefined;
|
|
@@ -1063,7 +1063,7 @@ export declare const columnsSchema: z.ZodRecord<z.ZodString, z.ZodDiscriminatedU
|
|
|
1063
1063
|
label?: string | undefined;
|
|
1064
1064
|
collection?: string | undefined;
|
|
1065
1065
|
}, {
|
|
1066
|
-
default?: SQL<any> |
|
|
1066
|
+
default?: Date | SQL<any> | undefined;
|
|
1067
1067
|
name?: string | undefined;
|
|
1068
1068
|
label?: string | undefined;
|
|
1069
1069
|
optional?: boolean | undefined;
|
|
@@ -1085,7 +1085,7 @@ export declare const columnsSchema: z.ZodRecord<z.ZodString, z.ZodDiscriminatedU
|
|
|
1085
1085
|
}, {
|
|
1086
1086
|
type: "date";
|
|
1087
1087
|
schema: {
|
|
1088
|
-
default?: SQL<any> |
|
|
1088
|
+
default?: Date | SQL<any> | undefined;
|
|
1089
1089
|
name?: string | undefined;
|
|
1090
1090
|
label?: string | undefined;
|
|
1091
1091
|
optional?: boolean | undefined;
|
|
@@ -1391,7 +1391,7 @@ export declare const tableSchema: z.ZodObject<{
|
|
|
1391
1391
|
label?: string | undefined;
|
|
1392
1392
|
collection?: string | undefined;
|
|
1393
1393
|
}, {
|
|
1394
|
-
default?: SQL<any> |
|
|
1394
|
+
default?: Date | SQL<any> | undefined;
|
|
1395
1395
|
name?: string | undefined;
|
|
1396
1396
|
label?: string | undefined;
|
|
1397
1397
|
optional?: boolean | undefined;
|
|
@@ -1413,7 +1413,7 @@ export declare const tableSchema: z.ZodObject<{
|
|
|
1413
1413
|
}, {
|
|
1414
1414
|
type: "date";
|
|
1415
1415
|
schema: {
|
|
1416
|
-
default?: SQL<any> |
|
|
1416
|
+
default?: Date | SQL<any> | undefined;
|
|
1417
1417
|
name?: string | undefined;
|
|
1418
1418
|
label?: string | undefined;
|
|
1419
1419
|
optional?: boolean | undefined;
|
|
@@ -1497,6 +1497,7 @@ export declare const tableSchema: z.ZodObject<{
|
|
|
1497
1497
|
foreignKeys: z.ZodOptional<z.ZodArray<z.ZodType<ForeignKeysOutput, ZodTypeDef, ForeignKeysInput>, "many">>;
|
|
1498
1498
|
deprecated: z.ZodDefault<z.ZodOptional<z.ZodBoolean>>;
|
|
1499
1499
|
}, "strip", z.ZodTypeAny, {
|
|
1500
|
+
deprecated: boolean;
|
|
1500
1501
|
columns: Record<string, {
|
|
1501
1502
|
type: "boolean";
|
|
1502
1503
|
schema: {
|
|
@@ -1569,7 +1570,6 @@ export declare const tableSchema: z.ZodObject<{
|
|
|
1569
1570
|
collection?: string | undefined;
|
|
1570
1571
|
};
|
|
1571
1572
|
}>;
|
|
1572
|
-
deprecated: boolean;
|
|
1573
1573
|
indexes?: {
|
|
1574
1574
|
on: string | string[];
|
|
1575
1575
|
name?: string | undefined;
|
|
@@ -1632,7 +1632,7 @@ export declare const tableSchema: z.ZodObject<{
|
|
|
1632
1632
|
} | {
|
|
1633
1633
|
type: "date";
|
|
1634
1634
|
schema: {
|
|
1635
|
-
default?: SQL<any> |
|
|
1635
|
+
default?: Date | SQL<any> | undefined;
|
|
1636
1636
|
name?: string | undefined;
|
|
1637
1637
|
label?: string | undefined;
|
|
1638
1638
|
optional?: boolean | undefined;
|
|
@@ -1880,7 +1880,7 @@ export declare const tablesSchema: z.ZodEffects<z.ZodRecord<z.ZodString, z.ZodOb
|
|
|
1880
1880
|
label?: string | undefined;
|
|
1881
1881
|
collection?: string | undefined;
|
|
1882
1882
|
}, {
|
|
1883
|
-
default?: SQL<any> |
|
|
1883
|
+
default?: Date | SQL<any> | undefined;
|
|
1884
1884
|
name?: string | undefined;
|
|
1885
1885
|
label?: string | undefined;
|
|
1886
1886
|
optional?: boolean | undefined;
|
|
@@ -1902,7 +1902,7 @@ export declare const tablesSchema: z.ZodEffects<z.ZodRecord<z.ZodString, z.ZodOb
|
|
|
1902
1902
|
}, {
|
|
1903
1903
|
type: "date";
|
|
1904
1904
|
schema: {
|
|
1905
|
-
default?: SQL<any> |
|
|
1905
|
+
default?: Date | SQL<any> | undefined;
|
|
1906
1906
|
name?: string | undefined;
|
|
1907
1907
|
label?: string | undefined;
|
|
1908
1908
|
optional?: boolean | undefined;
|
|
@@ -1986,6 +1986,7 @@ export declare const tablesSchema: z.ZodEffects<z.ZodRecord<z.ZodString, z.ZodOb
|
|
|
1986
1986
|
foreignKeys: z.ZodOptional<z.ZodArray<z.ZodType<ForeignKeysOutput, ZodTypeDef, ForeignKeysInput>, "many">>;
|
|
1987
1987
|
deprecated: z.ZodDefault<z.ZodOptional<z.ZodBoolean>>;
|
|
1988
1988
|
}, "strip", z.ZodTypeAny, {
|
|
1989
|
+
deprecated: boolean;
|
|
1989
1990
|
columns: Record<string, {
|
|
1990
1991
|
type: "boolean";
|
|
1991
1992
|
schema: {
|
|
@@ -2058,7 +2059,6 @@ export declare const tablesSchema: z.ZodEffects<z.ZodRecord<z.ZodString, z.ZodOb
|
|
|
2058
2059
|
collection?: string | undefined;
|
|
2059
2060
|
};
|
|
2060
2061
|
}>;
|
|
2061
|
-
deprecated: boolean;
|
|
2062
2062
|
indexes?: {
|
|
2063
2063
|
on: string | string[];
|
|
2064
2064
|
name?: string | undefined;
|
|
@@ -2121,7 +2121,7 @@ export declare const tablesSchema: z.ZodEffects<z.ZodRecord<z.ZodString, z.ZodOb
|
|
|
2121
2121
|
} | {
|
|
2122
2122
|
type: "date";
|
|
2123
2123
|
schema: {
|
|
2124
|
-
default?: SQL<any> |
|
|
2124
|
+
default?: Date | SQL<any> | undefined;
|
|
2125
2125
|
name?: string | undefined;
|
|
2126
2126
|
label?: string | undefined;
|
|
2127
2127
|
optional?: boolean | undefined;
|
|
@@ -2152,6 +2152,7 @@ export declare const tablesSchema: z.ZodEffects<z.ZodRecord<z.ZodString, z.ZodOb
|
|
|
2152
2152
|
}> | undefined;
|
|
2153
2153
|
foreignKeys?: ForeignKeysInput[] | undefined;
|
|
2154
2154
|
}>>, Record<string, {
|
|
2155
|
+
deprecated: boolean;
|
|
2155
2156
|
columns: Record<string, {
|
|
2156
2157
|
type: "boolean";
|
|
2157
2158
|
schema: {
|
|
@@ -2224,7 +2225,6 @@ export declare const tablesSchema: z.ZodEffects<z.ZodRecord<z.ZodString, z.ZodOb
|
|
|
2224
2225
|
collection?: string | undefined;
|
|
2225
2226
|
};
|
|
2226
2227
|
}>;
|
|
2227
|
-
deprecated: boolean;
|
|
2228
2228
|
indexes?: {
|
|
2229
2229
|
on: string | string[];
|
|
2230
2230
|
name?: string | undefined;
|
|
@@ -2453,7 +2453,7 @@ export declare const dbConfigSchema: z.ZodEffects<z.ZodObject<{
|
|
|
2453
2453
|
label?: string | undefined;
|
|
2454
2454
|
collection?: string | undefined;
|
|
2455
2455
|
}, {
|
|
2456
|
-
default?: SQL<any> |
|
|
2456
|
+
default?: Date | SQL<any> | undefined;
|
|
2457
2457
|
name?: string | undefined;
|
|
2458
2458
|
label?: string | undefined;
|
|
2459
2459
|
optional?: boolean | undefined;
|
|
@@ -2475,7 +2475,7 @@ export declare const dbConfigSchema: z.ZodEffects<z.ZodObject<{
|
|
|
2475
2475
|
}, {
|
|
2476
2476
|
type: "date";
|
|
2477
2477
|
schema: {
|
|
2478
|
-
default?: SQL<any> |
|
|
2478
|
+
default?: Date | SQL<any> | undefined;
|
|
2479
2479
|
name?: string | undefined;
|
|
2480
2480
|
label?: string | undefined;
|
|
2481
2481
|
optional?: boolean | undefined;
|
|
@@ -2559,6 +2559,7 @@ export declare const dbConfigSchema: z.ZodEffects<z.ZodObject<{
|
|
|
2559
2559
|
foreignKeys: z.ZodOptional<z.ZodArray<z.ZodType<ForeignKeysOutput, ZodTypeDef, ForeignKeysInput>, "many">>;
|
|
2560
2560
|
deprecated: z.ZodDefault<z.ZodOptional<z.ZodBoolean>>;
|
|
2561
2561
|
}, "strip", z.ZodTypeAny, {
|
|
2562
|
+
deprecated: boolean;
|
|
2562
2563
|
columns: Record<string, {
|
|
2563
2564
|
type: "boolean";
|
|
2564
2565
|
schema: {
|
|
@@ -2631,7 +2632,6 @@ export declare const dbConfigSchema: z.ZodEffects<z.ZodObject<{
|
|
|
2631
2632
|
collection?: string | undefined;
|
|
2632
2633
|
};
|
|
2633
2634
|
}>;
|
|
2634
|
-
deprecated: boolean;
|
|
2635
2635
|
indexes?: {
|
|
2636
2636
|
on: string | string[];
|
|
2637
2637
|
name?: string | undefined;
|
|
@@ -2694,7 +2694,7 @@ export declare const dbConfigSchema: z.ZodEffects<z.ZodObject<{
|
|
|
2694
2694
|
} | {
|
|
2695
2695
|
type: "date";
|
|
2696
2696
|
schema: {
|
|
2697
|
-
default?: SQL<any> |
|
|
2697
|
+
default?: Date | SQL<any> | undefined;
|
|
2698
2698
|
name?: string | undefined;
|
|
2699
2699
|
label?: string | undefined;
|
|
2700
2700
|
optional?: boolean | undefined;
|
|
@@ -2725,6 +2725,7 @@ export declare const dbConfigSchema: z.ZodEffects<z.ZodObject<{
|
|
|
2725
2725
|
}> | undefined;
|
|
2726
2726
|
foreignKeys?: ForeignKeysInput[] | undefined;
|
|
2727
2727
|
}>>, Record<string, {
|
|
2728
|
+
deprecated: boolean;
|
|
2728
2729
|
columns: Record<string, {
|
|
2729
2730
|
type: "boolean";
|
|
2730
2731
|
schema: {
|
|
@@ -2797,7 +2798,6 @@ export declare const dbConfigSchema: z.ZodEffects<z.ZodObject<{
|
|
|
2797
2798
|
collection?: string | undefined;
|
|
2798
2799
|
};
|
|
2799
2800
|
}>;
|
|
2800
|
-
deprecated: boolean;
|
|
2801
2801
|
indexes?: {
|
|
2802
2802
|
on: string | string[];
|
|
2803
2803
|
name?: string | undefined;
|
|
@@ -2810,6 +2810,7 @@ export declare const dbConfigSchema: z.ZodEffects<z.ZodObject<{
|
|
|
2810
2810
|
}>, unknown>>;
|
|
2811
2811
|
}, "strip", z.ZodTypeAny, {
|
|
2812
2812
|
tables?: Record<string, {
|
|
2813
|
+
deprecated: boolean;
|
|
2813
2814
|
columns: Record<string, {
|
|
2814
2815
|
type: "boolean";
|
|
2815
2816
|
schema: {
|
|
@@ -2882,7 +2883,6 @@ export declare const dbConfigSchema: z.ZodEffects<z.ZodObject<{
|
|
|
2882
2883
|
collection?: string | undefined;
|
|
2883
2884
|
};
|
|
2884
2885
|
}>;
|
|
2885
|
-
deprecated: boolean;
|
|
2886
2886
|
indexes?: {
|
|
2887
2887
|
on: string | string[];
|
|
2888
2888
|
name?: string | undefined;
|
|
@@ -2901,6 +2901,7 @@ export declare const dbConfigSchema: z.ZodEffects<z.ZodObject<{
|
|
|
2901
2901
|
on: string | string[];
|
|
2902
2902
|
unique?: boolean | undefined;
|
|
2903
2903
|
}>;
|
|
2904
|
+
deprecated: boolean;
|
|
2904
2905
|
columns: Record<string, {
|
|
2905
2906
|
type: "boolean";
|
|
2906
2907
|
schema: {
|
|
@@ -2973,7 +2974,6 @@ export declare const dbConfigSchema: z.ZodEffects<z.ZodObject<{
|
|
|
2973
2974
|
collection?: string | undefined;
|
|
2974
2975
|
};
|
|
2975
2976
|
}>;
|
|
2976
|
-
deprecated: boolean;
|
|
2977
2977
|
foreignKeys?: ForeignKeysOutput[] | undefined;
|
|
2978
2978
|
}>;
|
|
2979
2979
|
}, {
|
package/dist/core/schemas.js
CHANGED
|
@@ -65,7 +65,7 @@ const textColumnBaseSchema = baseColumnSchema.omit({ optional: true }).extend({
|
|
|
65
65
|
z.object({
|
|
66
66
|
// text primary key allows NULL values.
|
|
67
67
|
// NULL values bypass unique checks, which could
|
|
68
|
-
// lead to duplicate URLs per record
|
|
68
|
+
// lead to duplicate URLs per record.
|
|
69
69
|
// disable `optional` for primary keys.
|
|
70
70
|
primaryKey: z.literal(true),
|
|
71
71
|
optional: z.literal(false).optional()
|
package/dist/core/utils.d.ts
CHANGED
|
@@ -1,23 +1,14 @@
|
|
|
1
|
-
import { type ManagedAppToken } from '@astrojs/studio';
|
|
2
1
|
import type { AstroConfig, AstroIntegration } from 'astro';
|
|
3
2
|
import './types.js';
|
|
4
3
|
export type VitePlugin = Required<AstroConfig['vite']>['plugins'][number];
|
|
5
4
|
export declare function getAstroEnv(envMode?: string): Record<`ASTRO_${string}`, string>;
|
|
6
5
|
export type RemoteDatabaseInfo = {
|
|
7
|
-
type: 'libsql' | 'studio';
|
|
8
6
|
url: string;
|
|
7
|
+
token: string;
|
|
9
8
|
};
|
|
10
9
|
export declare function getRemoteDatabaseInfo(): RemoteDatabaseInfo;
|
|
11
|
-
export declare function getManagedRemoteToken(token?: string, dbInfo?: RemoteDatabaseInfo): Promise<ManagedAppToken>;
|
|
12
10
|
export declare function getDbDirectoryUrl(root: URL | string): URL;
|
|
13
11
|
export declare function defineDbIntegration(integration: AstroIntegration): AstroIntegration;
|
|
14
|
-
export type Result<T> = {
|
|
15
|
-
success: true;
|
|
16
|
-
data: T;
|
|
17
|
-
} | {
|
|
18
|
-
success: false;
|
|
19
|
-
data: unknown;
|
|
20
|
-
};
|
|
21
12
|
/**
|
|
22
13
|
* Map an object's values to a new set of values
|
|
23
14
|
* while preserving types.
|
package/dist/core/utils.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { getAstroStudioEnv, getManagedAppTokenOrExit } from "@astrojs/studio";
|
|
2
1
|
import { loadEnv } from "vite";
|
|
3
2
|
import "./types.js";
|
|
4
3
|
function getAstroEnv(envMode = "") {
|
|
@@ -7,34 +6,11 @@ function getAstroEnv(envMode = "") {
|
|
|
7
6
|
}
|
|
8
7
|
function getRemoteDatabaseInfo() {
|
|
9
8
|
const astroEnv = getAstroEnv();
|
|
10
|
-
const studioEnv = getAstroStudioEnv();
|
|
11
|
-
if (studioEnv.ASTRO_STUDIO_REMOTE_DB_URL)
|
|
12
|
-
return {
|
|
13
|
-
type: "studio",
|
|
14
|
-
url: studioEnv.ASTRO_STUDIO_REMOTE_DB_URL
|
|
15
|
-
};
|
|
16
|
-
if (astroEnv.ASTRO_DB_REMOTE_URL)
|
|
17
|
-
return {
|
|
18
|
-
type: "libsql",
|
|
19
|
-
url: astroEnv.ASTRO_DB_REMOTE_URL
|
|
20
|
-
};
|
|
21
9
|
return {
|
|
22
|
-
|
|
23
|
-
|
|
10
|
+
url: astroEnv.ASTRO_DB_REMOTE_URL,
|
|
11
|
+
token: astroEnv.ASTRO_DB_APP_TOKEN
|
|
24
12
|
};
|
|
25
13
|
}
|
|
26
|
-
function getManagedRemoteToken(token, dbInfo) {
|
|
27
|
-
dbInfo ??= getRemoteDatabaseInfo();
|
|
28
|
-
if (dbInfo.type === "studio") {
|
|
29
|
-
return getManagedAppTokenOrExit(token);
|
|
30
|
-
}
|
|
31
|
-
const astroEnv = getAstroEnv();
|
|
32
|
-
return Promise.resolve({
|
|
33
|
-
token: token ?? astroEnv.ASTRO_DB_APP_TOKEN,
|
|
34
|
-
renew: () => Promise.resolve(),
|
|
35
|
-
destroy: () => Promise.resolve()
|
|
36
|
-
});
|
|
37
|
-
}
|
|
38
14
|
function getDbDirectoryUrl(root) {
|
|
39
15
|
return new URL("db/", root);
|
|
40
16
|
}
|
|
@@ -50,7 +26,6 @@ export {
|
|
|
50
26
|
defineDbIntegration,
|
|
51
27
|
getAstroEnv,
|
|
52
28
|
getDbDirectoryUrl,
|
|
53
|
-
getManagedRemoteToken,
|
|
54
29
|
getRemoteDatabaseInfo,
|
|
55
30
|
mapObject
|
|
56
31
|
};
|
|
@@ -1,16 +1,15 @@
|
|
|
1
1
|
import { type Config as LibSQLConfig } from '@libsql/client';
|
|
2
2
|
import type { LibSQLDatabase } from 'drizzle-orm/libsql';
|
|
3
|
-
import { type SqliteRemoteDatabase } from 'drizzle-orm/sqlite-proxy';
|
|
4
3
|
type LocalDbClientOptions = {
|
|
5
4
|
dbUrl: string;
|
|
6
|
-
enableTransactions: boolean;
|
|
7
5
|
};
|
|
8
6
|
export declare function createLocalDatabaseClient(options: LocalDbClientOptions): LibSQLDatabase;
|
|
9
7
|
type RemoteDbClientOptions = {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
8
|
+
token: string;
|
|
9
|
+
url: string | URL;
|
|
10
|
+
};
|
|
11
|
+
export declare function createRemoteDatabaseClient(options: RemoteDbClientOptions): LibSQLDatabase<Record<string, never>> & {
|
|
12
|
+
$client: import("@libsql/client").Client;
|
|
13
13
|
};
|
|
14
|
-
export declare function createRemoteDatabaseClient(options: RemoteDbClientOptions): SqliteRemoteDatabase<Record<string, never>>;
|
|
15
14
|
export declare function parseOpts(config: Record<string, string>): Partial<LibSQLConfig>;
|
|
16
15
|
export {};
|
|
@@ -1,37 +1,15 @@
|
|
|
1
1
|
import { createClient } from "@libsql/client";
|
|
2
2
|
import { drizzle as drizzleLibsql } from "drizzle-orm/libsql";
|
|
3
|
-
import { drizzle as drizzleProxy } from "drizzle-orm/sqlite-proxy";
|
|
4
|
-
import { z } from "zod";
|
|
5
|
-
import { DetailedLibsqlError, safeFetch } from "./utils.js";
|
|
6
3
|
const isWebContainer = !!process.versions?.webcontainer;
|
|
7
|
-
function applyTransactionNotSupported(db) {
|
|
8
|
-
Object.assign(db, {
|
|
9
|
-
transaction() {
|
|
10
|
-
throw new Error(
|
|
11
|
-
"`db.transaction()` is not currently supported. We recommend `db.batch()` for automatic error rollbacks across multiple queries."
|
|
12
|
-
);
|
|
13
|
-
}
|
|
14
|
-
});
|
|
15
|
-
}
|
|
16
4
|
function createLocalDatabaseClient(options) {
|
|
17
5
|
const url = isWebContainer ? "file:content.db" : options.dbUrl;
|
|
18
6
|
const client = createClient({ url });
|
|
19
7
|
const db = drizzleLibsql(client);
|
|
20
|
-
if (!options.enableTransactions) {
|
|
21
|
-
applyTransactionNotSupported(db);
|
|
22
|
-
}
|
|
23
8
|
return db;
|
|
24
9
|
}
|
|
25
|
-
const remoteResultSchema = z.object({
|
|
26
|
-
columns: z.array(z.string()),
|
|
27
|
-
columnTypes: z.array(z.string()),
|
|
28
|
-
rows: z.array(z.array(z.unknown())),
|
|
29
|
-
rowsAffected: z.number(),
|
|
30
|
-
lastInsertRowid: z.unknown().optional()
|
|
31
|
-
});
|
|
32
10
|
function createRemoteDatabaseClient(options) {
|
|
33
|
-
const
|
|
34
|
-
return
|
|
11
|
+
const url = new URL(options.url);
|
|
12
|
+
return createRemoteLibSQLClient(options.token, url, options.url.toString());
|
|
35
13
|
}
|
|
36
14
|
function parseOpts(config) {
|
|
37
15
|
return {
|
|
@@ -43,153 +21,18 @@ function parseOpts(config) {
|
|
|
43
21
|
...config.concurrency ? { concurrency: parseInt(config.concurrency) } : {}
|
|
44
22
|
};
|
|
45
23
|
}
|
|
46
|
-
function createRemoteLibSQLClient(
|
|
47
|
-
const options = Object.fromEntries(
|
|
48
|
-
|
|
49
|
-
let url =
|
|
50
|
-
if (
|
|
24
|
+
function createRemoteLibSQLClient(authToken, dbURL, rawUrl) {
|
|
25
|
+
const options = Object.fromEntries(dbURL.searchParams.entries());
|
|
26
|
+
dbURL.search = "";
|
|
27
|
+
let url = dbURL.toString();
|
|
28
|
+
if (dbURL.protocol === "memory:") {
|
|
51
29
|
url = ":memory:";
|
|
52
|
-
} else if (
|
|
53
|
-
url = "file:" +
|
|
30
|
+
} else if (dbURL.protocol === "file:" && dbURL.pathname.startsWith("/") && !rawUrl.startsWith("file:/")) {
|
|
31
|
+
url = "file:" + dbURL.pathname.substring(1);
|
|
54
32
|
}
|
|
55
|
-
const client = createClient({ ...parseOpts(options), url, authToken
|
|
33
|
+
const client = createClient({ ...parseOpts(options), url, authToken });
|
|
56
34
|
return drizzleLibsql(client);
|
|
57
35
|
}
|
|
58
|
-
function createStudioDatabaseClient(appToken, remoteDbURL) {
|
|
59
|
-
if (appToken == null) {
|
|
60
|
-
throw new Error(`Cannot create a remote client: missing app token.`);
|
|
61
|
-
}
|
|
62
|
-
const url = new URL("/db/query", remoteDbURL);
|
|
63
|
-
const db = drizzleProxy(
|
|
64
|
-
async (sql, parameters, method) => {
|
|
65
|
-
const requestBody = { sql, args: parameters };
|
|
66
|
-
const res = await safeFetch(
|
|
67
|
-
url,
|
|
68
|
-
{
|
|
69
|
-
method: "POST",
|
|
70
|
-
headers: {
|
|
71
|
-
Authorization: `Bearer ${appToken}`,
|
|
72
|
-
"Content-Type": "application/json"
|
|
73
|
-
},
|
|
74
|
-
body: JSON.stringify(requestBody)
|
|
75
|
-
},
|
|
76
|
-
async (response) => {
|
|
77
|
-
throw await parseRemoteError(response);
|
|
78
|
-
}
|
|
79
|
-
);
|
|
80
|
-
let remoteResult;
|
|
81
|
-
try {
|
|
82
|
-
const json = await res.json();
|
|
83
|
-
remoteResult = remoteResultSchema.parse(json);
|
|
84
|
-
} catch {
|
|
85
|
-
throw new DetailedLibsqlError({
|
|
86
|
-
message: await getUnexpectedResponseMessage(res),
|
|
87
|
-
code: KNOWN_ERROR_CODES.SQL_QUERY_FAILED
|
|
88
|
-
});
|
|
89
|
-
}
|
|
90
|
-
if (method === "run") {
|
|
91
|
-
const rawRows = Array.from(remoteResult.rows);
|
|
92
|
-
remoteResult.rows.toJSON = () => rawRows;
|
|
93
|
-
for (let i = 0; i < remoteResult.rows.length; i++) {
|
|
94
|
-
let row = remoteResult.rows[i];
|
|
95
|
-
let item = {};
|
|
96
|
-
remoteResult.columns.forEach((col, index) => {
|
|
97
|
-
item[col] = row[index];
|
|
98
|
-
});
|
|
99
|
-
remoteResult.rows[i] = item;
|
|
100
|
-
}
|
|
101
|
-
return remoteResult;
|
|
102
|
-
}
|
|
103
|
-
const rowValues = [];
|
|
104
|
-
for (const row of remoteResult.rows) {
|
|
105
|
-
if (row != null && typeof row === "object") {
|
|
106
|
-
rowValues.push(Object.values(row));
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
if (method === "get") {
|
|
110
|
-
return { rows: rowValues[0] };
|
|
111
|
-
}
|
|
112
|
-
return { rows: rowValues };
|
|
113
|
-
},
|
|
114
|
-
async (queries) => {
|
|
115
|
-
const stmts = queries.map(({ sql, params }) => ({ sql, args: params }));
|
|
116
|
-
const res = await safeFetch(
|
|
117
|
-
url,
|
|
118
|
-
{
|
|
119
|
-
method: "POST",
|
|
120
|
-
headers: {
|
|
121
|
-
Authorization: `Bearer ${appToken}`,
|
|
122
|
-
"Content-Type": "application/json"
|
|
123
|
-
},
|
|
124
|
-
body: JSON.stringify(stmts)
|
|
125
|
-
},
|
|
126
|
-
async (response) => {
|
|
127
|
-
throw await parseRemoteError(response);
|
|
128
|
-
}
|
|
129
|
-
);
|
|
130
|
-
let remoteResults;
|
|
131
|
-
try {
|
|
132
|
-
const json = await res.json();
|
|
133
|
-
remoteResults = z.array(remoteResultSchema).parse(json);
|
|
134
|
-
} catch {
|
|
135
|
-
throw new DetailedLibsqlError({
|
|
136
|
-
message: await getUnexpectedResponseMessage(res),
|
|
137
|
-
code: KNOWN_ERROR_CODES.SQL_QUERY_FAILED
|
|
138
|
-
});
|
|
139
|
-
}
|
|
140
|
-
let results = [];
|
|
141
|
-
for (const [idx, rawResult] of remoteResults.entries()) {
|
|
142
|
-
if (queries[idx]?.method === "run") {
|
|
143
|
-
results.push(rawResult);
|
|
144
|
-
continue;
|
|
145
|
-
}
|
|
146
|
-
const rowValues = [];
|
|
147
|
-
for (const row of rawResult.rows) {
|
|
148
|
-
if (row != null && typeof row === "object") {
|
|
149
|
-
rowValues.push(Object.values(row));
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
if (queries[idx]?.method === "get") {
|
|
153
|
-
results.push({ rows: rowValues[0] });
|
|
154
|
-
}
|
|
155
|
-
results.push({ rows: rowValues });
|
|
156
|
-
}
|
|
157
|
-
return results;
|
|
158
|
-
}
|
|
159
|
-
);
|
|
160
|
-
applyTransactionNotSupported(db);
|
|
161
|
-
return db;
|
|
162
|
-
}
|
|
163
|
-
const errorSchema = z.object({
|
|
164
|
-
success: z.boolean(),
|
|
165
|
-
error: z.object({
|
|
166
|
-
code: z.string(),
|
|
167
|
-
details: z.string().optional()
|
|
168
|
-
})
|
|
169
|
-
});
|
|
170
|
-
const KNOWN_ERROR_CODES = {
|
|
171
|
-
SQL_QUERY_FAILED: "SQL_QUERY_FAILED"
|
|
172
|
-
};
|
|
173
|
-
const getUnexpectedResponseMessage = async (response) => `Unexpected response from remote database:
|
|
174
|
-
(Status ${response.status}) ${await response.clone().text()}`;
|
|
175
|
-
async function parseRemoteError(response) {
|
|
176
|
-
let error;
|
|
177
|
-
try {
|
|
178
|
-
error = errorSchema.parse(await response.clone().json()).error;
|
|
179
|
-
} catch {
|
|
180
|
-
return new DetailedLibsqlError({
|
|
181
|
-
message: await getUnexpectedResponseMessage(response),
|
|
182
|
-
code: KNOWN_ERROR_CODES.SQL_QUERY_FAILED
|
|
183
|
-
});
|
|
184
|
-
}
|
|
185
|
-
let baseDetails = error.details?.replace(/.*SQLite error: /, "") ?? "Error querying remote database.";
|
|
186
|
-
const details = baseDetails.slice(baseDetails.indexOf(":") + 1).trim();
|
|
187
|
-
let hint = `See the Astro DB guide for query and push instructions: https://docs.astro.build/en/guides/astro-db/#query-your-database`;
|
|
188
|
-
if (error.code === KNOWN_ERROR_CODES.SQL_QUERY_FAILED && details.includes("no such table")) {
|
|
189
|
-
hint = `Did you run \`astro db push\` to push your latest table schemas?`;
|
|
190
|
-
}
|
|
191
|
-
return new DetailedLibsqlError({ message: details, code: error.code, hint });
|
|
192
|
-
}
|
|
193
36
|
export {
|
|
194
37
|
createLocalDatabaseClient,
|
|
195
38
|
createRemoteDatabaseClient,
|
package/dist/runtime/types.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { ColumnBaseConfig, ColumnDataType } from 'drizzle-orm';
|
|
2
2
|
import type { SQLiteColumn, SQLiteTableWithColumns } from 'drizzle-orm/sqlite-core';
|
|
3
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'>;
|
|
4
|
+
type GeneratedConfig<T extends ColumnDataType = ColumnDataType> = Pick<ColumnBaseConfig<T, string>, 'name' | 'tableName' | 'notNull' | 'hasDefault' | 'hasRuntimeDefault' | 'isPrimaryKey'>;
|
|
5
5
|
type AstroText<T extends GeneratedConfig<'string'>> = SQLiteColumn<T & {
|
|
6
6
|
data: string;
|
|
7
7
|
dataType: 'string';
|
|
@@ -9,9 +9,9 @@ type AstroText<T extends GeneratedConfig<'string'>> = SQLiteColumn<T & {
|
|
|
9
9
|
driverParam: string;
|
|
10
10
|
enumValues: never;
|
|
11
11
|
baseColumn: never;
|
|
12
|
-
isPrimaryKey: boolean;
|
|
13
12
|
isAutoincrement: boolean;
|
|
14
|
-
|
|
13
|
+
identity: undefined;
|
|
14
|
+
generated: undefined;
|
|
15
15
|
}>;
|
|
16
16
|
type AstroDate<T extends GeneratedConfig<'custom'>> = SQLiteColumn<T & {
|
|
17
17
|
data: Date;
|
|
@@ -20,9 +20,9 @@ type AstroDate<T extends GeneratedConfig<'custom'>> = SQLiteColumn<T & {
|
|
|
20
20
|
driverParam: string;
|
|
21
21
|
enumValues: never;
|
|
22
22
|
baseColumn: never;
|
|
23
|
-
isPrimaryKey: boolean;
|
|
24
23
|
isAutoincrement: boolean;
|
|
25
|
-
|
|
24
|
+
identity: undefined;
|
|
25
|
+
generated: undefined;
|
|
26
26
|
}>;
|
|
27
27
|
type AstroBoolean<T extends GeneratedConfig<'boolean'>> = SQLiteColumn<T & {
|
|
28
28
|
data: boolean;
|
|
@@ -31,9 +31,9 @@ type AstroBoolean<T extends GeneratedConfig<'boolean'>> = SQLiteColumn<T & {
|
|
|
31
31
|
driverParam: number;
|
|
32
32
|
enumValues: never;
|
|
33
33
|
baseColumn: never;
|
|
34
|
-
isPrimaryKey: boolean;
|
|
35
34
|
isAutoincrement: boolean;
|
|
36
|
-
|
|
35
|
+
identity: undefined;
|
|
36
|
+
generated: undefined;
|
|
37
37
|
}>;
|
|
38
38
|
type AstroNumber<T extends GeneratedConfig<'number'>> = SQLiteColumn<T & {
|
|
39
39
|
data: number;
|
|
@@ -42,9 +42,9 @@ type AstroNumber<T extends GeneratedConfig<'number'>> = SQLiteColumn<T & {
|
|
|
42
42
|
driverParam: number;
|
|
43
43
|
enumValues: never;
|
|
44
44
|
baseColumn: never;
|
|
45
|
-
isPrimaryKey: boolean;
|
|
46
45
|
isAutoincrement: boolean;
|
|
47
|
-
|
|
46
|
+
identity: undefined;
|
|
47
|
+
generated: undefined;
|
|
48
48
|
}>;
|
|
49
49
|
type AstroJson<T extends GeneratedConfig<'custom'>> = SQLiteColumn<T & {
|
|
50
50
|
data: unknown;
|
|
@@ -53,9 +53,9 @@ type AstroJson<T extends GeneratedConfig<'custom'>> = SQLiteColumn<T & {
|
|
|
53
53
|
driverParam: string;
|
|
54
54
|
enumValues: never;
|
|
55
55
|
baseColumn: never;
|
|
56
|
-
isPrimaryKey: boolean;
|
|
57
56
|
isAutoincrement: boolean;
|
|
58
|
-
|
|
57
|
+
identity: undefined;
|
|
58
|
+
generated: undefined;
|
|
59
59
|
}>;
|
|
60
60
|
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;
|
|
61
61
|
export type Table<TTableName extends string, TColumns extends OutputColumnsConfig | ColumnsConfig> = SQLiteTableWithColumns<{
|
|
@@ -66,11 +66,17 @@ export type Table<TTableName extends string, TColumns extends OutputColumnsConfi
|
|
|
66
66
|
[K in Extract<keyof TColumns, string>]: Column<TColumns[K]['type'], {
|
|
67
67
|
tableName: TTableName;
|
|
68
68
|
name: K;
|
|
69
|
+
isPrimaryKey: TColumns[K]['schema'] extends {
|
|
70
|
+
primaryKey: true;
|
|
71
|
+
} ? true : false;
|
|
69
72
|
hasDefault: TColumns[K]['schema'] extends {
|
|
70
73
|
default: NonNullable<unknown>;
|
|
71
74
|
} ? true : TColumns[K]['schema'] extends {
|
|
72
75
|
primaryKey: true;
|
|
73
76
|
} ? true : false;
|
|
77
|
+
hasRuntimeDefault: TColumns[K]['schema'] extends {
|
|
78
|
+
default: NonNullable<unknown>;
|
|
79
|
+
} ? true : false;
|
|
74
80
|
notNull: TColumns[K]['schema']['optional'] extends true ? false : true;
|
|
75
81
|
}>;
|
|
76
82
|
};
|
package/dist/runtime/utils.d.ts
CHANGED
|
@@ -1,22 +1,7 @@
|
|
|
1
1
|
import { LibsqlError } from '@libsql/client';
|
|
2
2
|
import { AstroError } from 'astro/errors';
|
|
3
|
-
/**
|
|
4
|
-
* 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.
|
|
5
|
-
*/
|
|
6
|
-
export declare function safeFetch(url: Parameters<typeof fetch>[0], options?: Parameters<typeof fetch>[1], onNotOK?: (response: Response) => void | Promise<void>): Promise<Response>;
|
|
7
3
|
export declare class AstroDbError extends AstroError {
|
|
8
4
|
name: string;
|
|
9
5
|
}
|
|
10
|
-
export declare class DetailedLibsqlError extends LibsqlError {
|
|
11
|
-
name: string;
|
|
12
|
-
hint?: string;
|
|
13
|
-
constructor({ message, code, hint, rawCode, cause, }: {
|
|
14
|
-
message: string;
|
|
15
|
-
code: string;
|
|
16
|
-
hint?: string;
|
|
17
|
-
rawCode?: number;
|
|
18
|
-
cause?: Error;
|
|
19
|
-
});
|
|
20
|
-
}
|
|
21
6
|
export declare function isDbError(err: unknown): err is LibsqlError;
|
|
22
7
|
export declare function pathToFileURL(path: string): URL;
|