@astrojs/db 0.4.1 → 0.6.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.
- package/dist/core/cli/commands/execute/index.d.ts +8 -0
- package/dist/core/cli/commands/execute/index.js +32 -0
- package/dist/core/cli/commands/link/index.d.ts +20 -8
- package/dist/core/cli/commands/link/index.js +191 -31
- package/dist/core/cli/commands/login/index.d.ts +4 -2
- package/dist/core/cli/commands/login/index.js +31 -22
- package/dist/core/cli/commands/logout/index.d.ts +1 -6
- package/dist/core/cli/commands/logout/index.js +1 -1
- package/dist/core/cli/commands/push/index.d.ts +4 -2
- package/dist/core/cli/commands/push/index.js +30 -170
- package/dist/core/cli/commands/shell/index.d.ts +4 -2
- package/dist/core/cli/commands/shell/index.js +3 -1
- package/dist/core/cli/commands/verify/index.d.ts +4 -2
- package/dist/core/cli/commands/verify/index.js +23 -37
- package/dist/core/cli/index.d.ts +1 -1
- package/dist/core/cli/index.js +26 -17
- package/dist/core/cli/migration-queries.d.ts +8 -13
- package/dist/core/cli/migration-queries.js +65 -121
- package/dist/core/consts.d.ts +2 -0
- package/dist/core/consts.js +4 -0
- package/dist/core/errors.d.ts +9 -7
- package/dist/core/errors.js +44 -43
- package/dist/core/integration/file-url.js +2 -5
- package/dist/core/integration/index.js +56 -112
- package/dist/core/integration/typegen.js +3 -13
- package/dist/core/integration/vite-plugin-db.d.ts +9 -5
- package/dist/core/integration/vite-plugin-db.js +66 -23
- package/dist/core/integration/vite-plugin-inject-env-ts.d.ts +1 -1
- package/dist/core/load-file.d.ts +31 -0
- package/dist/core/load-file.js +98 -0
- package/dist/core/tokens.js +1 -1
- package/dist/core/types.d.ts +1148 -5306
- package/dist/core/types.js +19 -85
- package/dist/core/utils.d.ts +1 -0
- package/dist/core/utils.js +4 -0
- package/dist/index.d.ts +2 -3
- package/dist/index.js +3 -5
- package/dist/runtime/config.d.ts +147 -0
- package/dist/runtime/config.js +42 -0
- package/dist/runtime/db-client.d.ts +2 -9
- package/dist/runtime/db-client.js +8 -39
- package/dist/runtime/index.d.ts +5 -4
- package/dist/runtime/index.js +12 -10
- package/dist/{core → runtime}/queries.d.ts +20 -17
- package/dist/{core → runtime}/queries.js +67 -91
- package/dist/runtime/types.d.ts +3 -3
- package/dist/utils.d.ts +1 -0
- package/dist/utils.js +4 -0
- package/index.d.ts +5 -3
- package/package.json +20 -5
- package/config-augment.d.ts +0 -4
- package/dist/core/cli/commands/gen/index.d.ts +0 -6
- package/dist/core/cli/commands/gen/index.js +0 -39
- package/dist/core/cli/migrations.d.ts +0 -34
- package/dist/core/cli/migrations.js +0 -129
- package/dist/core/integration/load-astro-config.d.ts +0 -6
- package/dist/core/integration/load-astro-config.js +0 -79
package/dist/core/types.js
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
|
+
import { SQL } from "drizzle-orm";
|
|
1
2
|
import { SQLiteAsyncDialect } from "drizzle-orm/sqlite-core";
|
|
2
|
-
import { collectionToTable } from "../runtime/index.js";
|
|
3
3
|
import { z } from "zod";
|
|
4
|
-
import { SQL } from "drizzle-orm";
|
|
5
|
-
import { errorMap } from "./integration/error-map.js";
|
|
6
4
|
import { SERIALIZED_SQL_KEY } from "../runtime/types.js";
|
|
5
|
+
import { errorMap } from "./integration/error-map.js";
|
|
7
6
|
const sqlite = new SQLiteAsyncDialect();
|
|
8
7
|
const sqlSchema = z.instanceof(SQL).transform(
|
|
9
8
|
(sqlObj) => ({
|
|
@@ -15,8 +14,10 @@ const baseColumnSchema = z.object({
|
|
|
15
14
|
label: z.string().optional(),
|
|
16
15
|
optional: z.boolean().optional().default(false),
|
|
17
16
|
unique: z.boolean().optional().default(false),
|
|
17
|
+
deprecated: z.boolean().optional().default(false),
|
|
18
18
|
// Defined when `defineReadableTable()` is called
|
|
19
19
|
name: z.string().optional(),
|
|
20
|
+
// TODO: rename to `tableName`. Breaking schema change
|
|
20
21
|
collection: z.string().optional()
|
|
21
22
|
});
|
|
22
23
|
const booleanColumnSchema = z.object({
|
|
@@ -112,98 +113,31 @@ const foreignKeysSchema = z.object({
|
|
|
112
113
|
columns: z.string().or(z.array(z.string())),
|
|
113
114
|
references: z.function().returns(z.lazy(() => referenceableColumnSchema.or(z.array(referenceableColumnSchema)))).transform((fn) => fn())
|
|
114
115
|
});
|
|
115
|
-
const
|
|
116
|
+
const tableSchema = z.object({
|
|
116
117
|
columns: columnsSchema,
|
|
117
118
|
indexes: z.record(indexSchema).optional(),
|
|
118
|
-
foreignKeys: z.array(foreignKeysSchema).optional()
|
|
119
|
-
|
|
120
|
-
const readableCollectionSchema = baseCollectionSchema.extend({
|
|
121
|
-
writable: z.literal(false)
|
|
119
|
+
foreignKeys: z.array(foreignKeysSchema).optional(),
|
|
120
|
+
deprecated: z.boolean().optional().default(false)
|
|
122
121
|
});
|
|
123
|
-
const
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
const
|
|
127
|
-
const
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
collection.table = collectionToTable(
|
|
131
|
-
collectionName,
|
|
132
|
-
collectionSchema.parse(collection, { errorMap })
|
|
133
|
-
);
|
|
134
|
-
const { columns } = z.object({ columns: z.record(z.any()) }).parse(collection, { errorMap });
|
|
135
|
-
for (const [columnName, column2] of Object.entries(columns)) {
|
|
136
|
-
column2.schema.name = columnName;
|
|
137
|
-
column2.schema.collection = collectionName;
|
|
122
|
+
const tablesSchema = z.preprocess((rawTables) => {
|
|
123
|
+
const tables = z.record(z.any()).parse(rawTables, { errorMap });
|
|
124
|
+
for (const [tableName, table] of Object.entries(tables)) {
|
|
125
|
+
const { columns } = z.object({ columns: z.record(z.any()) }).parse(table, { errorMap });
|
|
126
|
+
for (const [columnName, column] of Object.entries(columns)) {
|
|
127
|
+
column.schema.name = columnName;
|
|
128
|
+
column.schema.collection = tableName;
|
|
138
129
|
}
|
|
139
130
|
}
|
|
140
|
-
return
|
|
141
|
-
}, z.record(
|
|
142
|
-
function defineData(fn) {
|
|
143
|
-
return fn;
|
|
144
|
-
}
|
|
145
|
-
const dbDataFn = z.function().returns(z.union([z.void(), z.promise(z.void())]));
|
|
131
|
+
return rawTables;
|
|
132
|
+
}, z.record(tableSchema));
|
|
146
133
|
const dbConfigSchema = z.object({
|
|
147
|
-
|
|
148
|
-
tables: tablesSchema.optional(),
|
|
149
|
-
data: z.union([dbDataFn, z.array(dbDataFn)]).optional(),
|
|
150
|
-
unsafeWritable: z.boolean().optional().default(false)
|
|
151
|
-
});
|
|
152
|
-
const astroConfigWithDbSchema = z.object({
|
|
153
|
-
db: dbConfigSchema.optional()
|
|
134
|
+
tables: tablesSchema.optional()
|
|
154
135
|
});
|
|
155
|
-
function baseDefineCollection(userConfig, writable) {
|
|
156
|
-
return {
|
|
157
|
-
...userConfig,
|
|
158
|
-
writable,
|
|
159
|
-
// set at runtime to get the table name
|
|
160
|
-
table: null
|
|
161
|
-
};
|
|
162
|
-
}
|
|
163
|
-
function defineReadableTable(userConfig) {
|
|
164
|
-
return baseDefineCollection(userConfig, false);
|
|
165
|
-
}
|
|
166
|
-
function defineWritableTable(userConfig) {
|
|
167
|
-
return baseDefineCollection(userConfig, true);
|
|
168
|
-
}
|
|
169
|
-
function createColumn(type, schema) {
|
|
170
|
-
return {
|
|
171
|
-
type,
|
|
172
|
-
/**
|
|
173
|
-
* @internal
|
|
174
|
-
*/
|
|
175
|
-
schema
|
|
176
|
-
};
|
|
177
|
-
}
|
|
178
|
-
const column = {
|
|
179
|
-
number: (opts = {}) => {
|
|
180
|
-
return createColumn("number", opts);
|
|
181
|
-
},
|
|
182
|
-
boolean: (opts = {}) => {
|
|
183
|
-
return createColumn("boolean", opts);
|
|
184
|
-
},
|
|
185
|
-
text: (opts = {}) => {
|
|
186
|
-
return createColumn("text", opts);
|
|
187
|
-
},
|
|
188
|
-
date(opts = {}) {
|
|
189
|
-
return createColumn("date", opts);
|
|
190
|
-
},
|
|
191
|
-
json(opts = {}) {
|
|
192
|
-
return createColumn("json", opts);
|
|
193
|
-
}
|
|
194
|
-
};
|
|
195
136
|
export {
|
|
196
|
-
astroConfigWithDbSchema,
|
|
197
|
-
collectionSchema,
|
|
198
|
-
column,
|
|
199
137
|
columnSchema,
|
|
200
138
|
dbConfigSchema,
|
|
201
|
-
defineData,
|
|
202
|
-
defineReadableTable,
|
|
203
|
-
defineWritableTable,
|
|
204
139
|
indexSchema,
|
|
205
|
-
readableCollectionSchema,
|
|
206
140
|
referenceableColumnSchema,
|
|
207
|
-
|
|
208
|
-
|
|
141
|
+
tableSchema,
|
|
142
|
+
tablesSchema
|
|
209
143
|
};
|
package/dist/core/utils.d.ts
CHANGED
|
@@ -3,3 +3,4 @@ export type VitePlugin = Required<AstroConfig['vite']>['plugins'][number];
|
|
|
3
3
|
export declare function getAstroStudioEnv(envMode?: string): Record<`ASTRO_STUDIO_${string}`, string>;
|
|
4
4
|
export declare function getRemoteDatabaseUrl(): string;
|
|
5
5
|
export declare function getAstroStudioUrl(): string;
|
|
6
|
+
export declare function getDbDirectoryUrl(root: URL | string): URL;
|
package/dist/core/utils.js
CHANGED
|
@@ -11,8 +11,12 @@ function getAstroStudioUrl() {
|
|
|
11
11
|
const env = getAstroStudioEnv();
|
|
12
12
|
return env.ASTRO_STUDIO_URL || "https://stardate.astro.build";
|
|
13
13
|
}
|
|
14
|
+
function getDbDirectoryUrl(root) {
|
|
15
|
+
return new URL("db/", root);
|
|
16
|
+
}
|
|
14
17
|
export {
|
|
15
18
|
getAstroStudioEnv,
|
|
16
19
|
getAstroStudioUrl,
|
|
20
|
+
getDbDirectoryUrl,
|
|
17
21
|
getRemoteDatabaseUrl
|
|
18
22
|
};
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
export {
|
|
2
|
-
export type { ResolvedCollectionConfig, DBDataContext } from './core/types.js';
|
|
1
|
+
export type { ResolvedCollectionConfig, TableConfig } from './core/types.js';
|
|
3
2
|
export { cli } from './core/cli/index.js';
|
|
4
3
|
export { integration as default } from './core/integration/index.js';
|
|
5
|
-
export { sql, NOW, TRUE, FALSE } from './runtime/
|
|
4
|
+
export { sql, NOW, TRUE, FALSE, defineDB, defineTable, column } from './runtime/config.js';
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import { defineReadableTable, defineWritableTable, defineData, column } from "./core/types.js";
|
|
2
1
|
import { cli } from "./core/cli/index.js";
|
|
3
2
|
import { integration } from "./core/integration/index.js";
|
|
4
|
-
import { sql, NOW, TRUE, FALSE } from "./runtime/
|
|
3
|
+
import { sql, NOW, TRUE, FALSE, defineDB, defineTable, column } from "./runtime/config.js";
|
|
5
4
|
export {
|
|
6
5
|
FALSE,
|
|
7
6
|
NOW,
|
|
@@ -9,8 +8,7 @@ export {
|
|
|
9
8
|
cli,
|
|
10
9
|
column,
|
|
11
10
|
integration as default,
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
defineWritableTable,
|
|
11
|
+
defineDB,
|
|
12
|
+
defineTable,
|
|
15
13
|
sql
|
|
16
14
|
};
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import type { ColumnsConfig, DBConfigInput, TableConfig } from '../core/types.js';
|
|
2
|
+
export declare const column: {
|
|
3
|
+
number: <T extends ({
|
|
4
|
+
name?: string | undefined;
|
|
5
|
+
label?: string | undefined;
|
|
6
|
+
unique?: boolean | undefined;
|
|
7
|
+
deprecated?: boolean | undefined;
|
|
8
|
+
collection?: string | undefined;
|
|
9
|
+
} & ({
|
|
10
|
+
primaryKey?: false | undefined;
|
|
11
|
+
optional?: boolean | undefined;
|
|
12
|
+
default?: number | import("drizzle-orm").SQL<any> | undefined;
|
|
13
|
+
} | {
|
|
14
|
+
primaryKey: true;
|
|
15
|
+
optional?: false | undefined;
|
|
16
|
+
default?: undefined;
|
|
17
|
+
})) & {
|
|
18
|
+
references?: (() => {
|
|
19
|
+
type: "number";
|
|
20
|
+
schema: ({
|
|
21
|
+
name?: string | undefined;
|
|
22
|
+
label?: string | undefined;
|
|
23
|
+
unique?: boolean | undefined;
|
|
24
|
+
deprecated?: boolean | undefined;
|
|
25
|
+
collection?: string | undefined;
|
|
26
|
+
} & {
|
|
27
|
+
primaryKey?: false | undefined;
|
|
28
|
+
optional?: boolean | undefined;
|
|
29
|
+
default?: number | import("drizzle-orm").SQL<any> | undefined;
|
|
30
|
+
} & any) | ({
|
|
31
|
+
name?: string | undefined;
|
|
32
|
+
label?: string | undefined;
|
|
33
|
+
unique?: boolean | undefined;
|
|
34
|
+
deprecated?: boolean | undefined;
|
|
35
|
+
collection?: string | undefined;
|
|
36
|
+
} & {
|
|
37
|
+
primaryKey: true;
|
|
38
|
+
optional?: false | undefined;
|
|
39
|
+
default?: undefined;
|
|
40
|
+
} & any);
|
|
41
|
+
}) | undefined;
|
|
42
|
+
}>(opts?: T) => {
|
|
43
|
+
type: "number";
|
|
44
|
+
/**
|
|
45
|
+
* @internal
|
|
46
|
+
*/
|
|
47
|
+
schema: T;
|
|
48
|
+
};
|
|
49
|
+
boolean: <T_1 extends {
|
|
50
|
+
name?: string | undefined;
|
|
51
|
+
label?: string | undefined;
|
|
52
|
+
optional?: boolean | undefined;
|
|
53
|
+
unique?: boolean | undefined;
|
|
54
|
+
deprecated?: boolean | undefined;
|
|
55
|
+
collection?: string | undefined;
|
|
56
|
+
default?: boolean | import("drizzle-orm").SQL<any> | undefined;
|
|
57
|
+
}>(opts?: T_1) => {
|
|
58
|
+
type: "boolean";
|
|
59
|
+
/**
|
|
60
|
+
* @internal
|
|
61
|
+
*/
|
|
62
|
+
schema: T_1;
|
|
63
|
+
};
|
|
64
|
+
text: <T_2 extends ({
|
|
65
|
+
name?: string | undefined;
|
|
66
|
+
label?: string | undefined;
|
|
67
|
+
unique?: boolean | undefined;
|
|
68
|
+
deprecated?: boolean | undefined;
|
|
69
|
+
collection?: string | undefined;
|
|
70
|
+
default?: string | import("drizzle-orm").SQL<any> | undefined;
|
|
71
|
+
multiline?: boolean | undefined;
|
|
72
|
+
} & ({
|
|
73
|
+
primaryKey?: false | undefined;
|
|
74
|
+
optional?: boolean | undefined;
|
|
75
|
+
} | {
|
|
76
|
+
primaryKey: true;
|
|
77
|
+
optional?: false | undefined;
|
|
78
|
+
})) & {
|
|
79
|
+
references?: (() => {
|
|
80
|
+
type: "text";
|
|
81
|
+
schema: ({
|
|
82
|
+
name?: string | undefined;
|
|
83
|
+
label?: string | undefined;
|
|
84
|
+
unique?: boolean | undefined;
|
|
85
|
+
deprecated?: boolean | undefined;
|
|
86
|
+
collection?: string | undefined;
|
|
87
|
+
default?: string | import("drizzle-orm").SQL<any> | undefined;
|
|
88
|
+
multiline?: boolean | undefined;
|
|
89
|
+
} & {
|
|
90
|
+
primaryKey?: false | undefined;
|
|
91
|
+
optional?: boolean | undefined;
|
|
92
|
+
} & any) | ({
|
|
93
|
+
name?: string | undefined;
|
|
94
|
+
label?: string | undefined;
|
|
95
|
+
unique?: boolean | undefined;
|
|
96
|
+
deprecated?: boolean | undefined;
|
|
97
|
+
collection?: string | undefined;
|
|
98
|
+
default?: string | import("drizzle-orm").SQL<any> | undefined;
|
|
99
|
+
multiline?: boolean | undefined;
|
|
100
|
+
} & {
|
|
101
|
+
primaryKey: true;
|
|
102
|
+
optional?: false | undefined;
|
|
103
|
+
} & any);
|
|
104
|
+
}) | undefined;
|
|
105
|
+
}>(opts?: T_2) => {
|
|
106
|
+
type: "text";
|
|
107
|
+
/**
|
|
108
|
+
* @internal
|
|
109
|
+
*/
|
|
110
|
+
schema: T_2;
|
|
111
|
+
};
|
|
112
|
+
date<T_3 extends {
|
|
113
|
+
name?: string | undefined;
|
|
114
|
+
label?: string | undefined;
|
|
115
|
+
optional?: boolean | undefined;
|
|
116
|
+
unique?: boolean | undefined;
|
|
117
|
+
deprecated?: boolean | undefined;
|
|
118
|
+
collection?: string | undefined;
|
|
119
|
+
default?: Date | import("drizzle-orm").SQL<any> | undefined;
|
|
120
|
+
}>(opts?: T_3): {
|
|
121
|
+
type: "date";
|
|
122
|
+
/**
|
|
123
|
+
* @internal
|
|
124
|
+
*/
|
|
125
|
+
schema: T_3;
|
|
126
|
+
};
|
|
127
|
+
json<T_4 extends {
|
|
128
|
+
name?: string | undefined;
|
|
129
|
+
label?: string | undefined;
|
|
130
|
+
optional?: boolean | undefined;
|
|
131
|
+
unique?: boolean | undefined;
|
|
132
|
+
deprecated?: boolean | undefined;
|
|
133
|
+
collection?: string | undefined;
|
|
134
|
+
default?: unknown;
|
|
135
|
+
}>(opts?: T_4): {
|
|
136
|
+
type: "json";
|
|
137
|
+
/**
|
|
138
|
+
* @internal
|
|
139
|
+
*/
|
|
140
|
+
schema: T_4;
|
|
141
|
+
};
|
|
142
|
+
};
|
|
143
|
+
export declare function defineTable<TColumns extends ColumnsConfig>(userConfig: TableConfig<TColumns>): TableConfig<TColumns>;
|
|
144
|
+
export declare function defineDB(userConfig: DBConfigInput): {
|
|
145
|
+
tables?: unknown;
|
|
146
|
+
};
|
|
147
|
+
export { sql, NOW, TRUE, FALSE } from './index.js';
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
function createColumn(type, schema) {
|
|
2
|
+
return {
|
|
3
|
+
type,
|
|
4
|
+
/**
|
|
5
|
+
* @internal
|
|
6
|
+
*/
|
|
7
|
+
schema
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
const column = {
|
|
11
|
+
number: (opts = {}) => {
|
|
12
|
+
return createColumn("number", opts);
|
|
13
|
+
},
|
|
14
|
+
boolean: (opts = {}) => {
|
|
15
|
+
return createColumn("boolean", opts);
|
|
16
|
+
},
|
|
17
|
+
text: (opts = {}) => {
|
|
18
|
+
return createColumn("text", opts);
|
|
19
|
+
},
|
|
20
|
+
date(opts = {}) {
|
|
21
|
+
return createColumn("date", opts);
|
|
22
|
+
},
|
|
23
|
+
json(opts = {}) {
|
|
24
|
+
return createColumn("json", opts);
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
function defineTable(userConfig) {
|
|
28
|
+
return userConfig;
|
|
29
|
+
}
|
|
30
|
+
function defineDB(userConfig) {
|
|
31
|
+
return userConfig;
|
|
32
|
+
}
|
|
33
|
+
import { sql, NOW, TRUE, FALSE } from "./index.js";
|
|
34
|
+
export {
|
|
35
|
+
FALSE,
|
|
36
|
+
NOW,
|
|
37
|
+
TRUE,
|
|
38
|
+
column,
|
|
39
|
+
defineDB,
|
|
40
|
+
defineTable,
|
|
41
|
+
sql
|
|
42
|
+
};
|
|
@@ -1,12 +1,5 @@
|
|
|
1
|
-
/// <reference types="node" resolution-mode="require"/>
|
|
2
|
-
import { type DBTables } from '../core/types.js';
|
|
3
1
|
import type { LibSQLDatabase } from 'drizzle-orm/libsql';
|
|
4
|
-
|
|
5
|
-
}
|
|
6
|
-
export declare function createLocalDatabaseClient({ tables, dbUrl, seeding, }: {
|
|
2
|
+
export declare function createLocalDatabaseClient({ dbUrl }: {
|
|
7
3
|
dbUrl: string;
|
|
8
|
-
|
|
9
|
-
seeding: boolean;
|
|
10
|
-
}): Promise<LocalDatabaseClient>;
|
|
4
|
+
}): LibSQLDatabase;
|
|
11
5
|
export declare function createRemoteDatabaseClient(appToken: string, remoteDbURL: string): import("drizzle-orm/sqlite-proxy").SqliteRemoteDatabase<Record<string, never>>;
|
|
12
|
-
export {};
|
|
@@ -1,53 +1,19 @@
|
|
|
1
1
|
import { createClient } from "@libsql/client";
|
|
2
|
-
import {} from "../core/types.js";
|
|
3
2
|
import { drizzle as drizzleLibsql } from "drizzle-orm/libsql";
|
|
4
3
|
import { drizzle as drizzleProxy } from "drizzle-orm/sqlite-proxy";
|
|
5
|
-
import {} from "drizzle-orm/sqlite-core";
|
|
6
4
|
import { z } from "zod";
|
|
7
|
-
import { getTableName } from "drizzle-orm";
|
|
8
5
|
const isWebContainer = !!process.versions?.webcontainer;
|
|
9
|
-
|
|
10
|
-
tables,
|
|
11
|
-
dbUrl,
|
|
12
|
-
seeding
|
|
13
|
-
}) {
|
|
6
|
+
function createLocalDatabaseClient({ dbUrl }) {
|
|
14
7
|
const url = isWebContainer ? "file:content.db" : dbUrl;
|
|
15
|
-
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
}
|
|
20
|
-
});
|
|
21
|
-
if (seeding)
|
|
22
|
-
return db;
|
|
23
|
-
const { insert: drizzleInsert, update: drizzleUpdate, delete: drizzleDelete } = db;
|
|
24
|
-
return Object.assign(db, {
|
|
25
|
-
insert(Table) {
|
|
26
|
-
checkIfModificationIsAllowed(tables, Table);
|
|
27
|
-
return drizzleInsert.call(this, Table);
|
|
28
|
-
},
|
|
29
|
-
update(Table) {
|
|
30
|
-
checkIfModificationIsAllowed(tables, Table);
|
|
31
|
-
return drizzleUpdate.call(this, Table);
|
|
32
|
-
},
|
|
33
|
-
delete(Table) {
|
|
34
|
-
checkIfModificationIsAllowed(tables, Table);
|
|
35
|
-
return drizzleDelete.call(this, Table);
|
|
36
|
-
}
|
|
37
|
-
});
|
|
38
|
-
}
|
|
39
|
-
function checkIfModificationIsAllowed(tables, Table) {
|
|
40
|
-
const tableName = getTableName(Table);
|
|
41
|
-
const collection = tables[tableName];
|
|
42
|
-
if (!collection.writable) {
|
|
43
|
-
throw new Error(`The [${tableName}] collection is read-only.`);
|
|
44
|
-
}
|
|
8
|
+
console.log("memory", process.env.TEST_IN_MEMORY_DB);
|
|
9
|
+
const client = createClient({ url: process.env.TEST_IN_MEMORY_DB ? ":memory:" : url });
|
|
10
|
+
const db = drizzleLibsql(client);
|
|
11
|
+
return db;
|
|
45
12
|
}
|
|
46
13
|
function createRemoteDatabaseClient(appToken, remoteDbURL) {
|
|
47
14
|
const url = new URL("/db/query", remoteDbURL);
|
|
48
15
|
const db = drizzleProxy(async (sql, parameters, method) => {
|
|
49
16
|
const requestBody = { sql, args: parameters };
|
|
50
|
-
console.info(JSON.stringify(requestBody));
|
|
51
17
|
const res = await fetch(url, {
|
|
52
18
|
method: "POST",
|
|
53
19
|
headers: {
|
|
@@ -88,6 +54,9 @@ Full error: Unexpected JSON response. ${e instanceof Error ? e.message : String(
|
|
|
88
54
|
}
|
|
89
55
|
return { rows: rowValues };
|
|
90
56
|
});
|
|
57
|
+
db.batch = (_drizzleQueries) => {
|
|
58
|
+
throw new Error("db.batch() is not currently supported.");
|
|
59
|
+
};
|
|
91
60
|
return db;
|
|
92
61
|
}
|
|
93
62
|
export {
|
package/dist/runtime/index.d.ts
CHANGED
|
@@ -1,15 +1,16 @@
|
|
|
1
|
-
import type { SqliteRemoteDatabase } from 'drizzle-orm/sqlite-proxy';
|
|
2
|
-
import { type DBTable, type DBColumn } from '../core/types.js';
|
|
3
1
|
import { type ColumnDataType, sql } from 'drizzle-orm';
|
|
2
|
+
import type { LibSQLDatabase } from 'drizzle-orm/libsql';
|
|
3
|
+
import { type DBColumn, type DBTable } from '../core/types.js';
|
|
4
4
|
export { sql };
|
|
5
|
-
export type SqliteDB =
|
|
5
|
+
export type SqliteDB = LibSQLDatabase;
|
|
6
6
|
export type { Table } from './types.js';
|
|
7
7
|
export { createRemoteDatabaseClient, createLocalDatabaseClient } from './db-client.js';
|
|
8
|
+
export { seedLocal } from './queries.js';
|
|
8
9
|
export declare function hasPrimaryKey(column: DBColumn): boolean;
|
|
9
10
|
export declare const NOW: import("drizzle-orm").SQL<unknown>;
|
|
10
11
|
export declare const TRUE: import("drizzle-orm").SQL<unknown>;
|
|
11
12
|
export declare const FALSE: import("drizzle-orm").SQL<unknown>;
|
|
12
|
-
export declare function
|
|
13
|
+
export declare function asDrizzleTable(name: string, table: DBTable): import("drizzle-orm/sqlite-core").SQLiteTableWithColumns<{
|
|
13
14
|
name: string;
|
|
14
15
|
schema: undefined;
|
|
15
16
|
columns: {
|
package/dist/runtime/index.js
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
|
-
import {} from "../core/types.js";
|
|
2
1
|
import { sql } from "drizzle-orm";
|
|
3
2
|
import {
|
|
4
3
|
customType,
|
|
4
|
+
index,
|
|
5
5
|
integer,
|
|
6
6
|
sqliteTable,
|
|
7
|
-
text
|
|
8
|
-
index
|
|
7
|
+
text
|
|
9
8
|
} from "drizzle-orm/sqlite-core";
|
|
9
|
+
import {} from "../core/types.js";
|
|
10
10
|
import { isSerializedSQL } from "./types.js";
|
|
11
11
|
import { createRemoteDatabaseClient, createLocalDatabaseClient } from "./db-client.js";
|
|
12
|
+
import { seedLocal } from "./queries.js";
|
|
12
13
|
function hasPrimaryKey(column) {
|
|
13
14
|
return "primaryKey" in column.schema && !!column.schema.primaryKey;
|
|
14
15
|
}
|
|
@@ -37,17 +38,17 @@ const jsonType = customType({
|
|
|
37
38
|
return JSON.parse(value);
|
|
38
39
|
}
|
|
39
40
|
});
|
|
40
|
-
function
|
|
41
|
+
function asDrizzleTable(name, table) {
|
|
41
42
|
const columns = {};
|
|
42
|
-
if (!Object.entries(
|
|
43
|
+
if (!Object.entries(table.columns).some(([, column]) => hasPrimaryKey(column))) {
|
|
43
44
|
columns["_id"] = integer("_id").primaryKey();
|
|
44
45
|
}
|
|
45
|
-
for (const [columnName, column] of Object.entries(
|
|
46
|
+
for (const [columnName, column] of Object.entries(table.columns)) {
|
|
46
47
|
columns[columnName] = columnMapper(columnName, column);
|
|
47
48
|
}
|
|
48
|
-
const
|
|
49
|
+
const drizzleTable = sqliteTable(name, columns, (ormTable) => {
|
|
49
50
|
const indexes = {};
|
|
50
|
-
for (const [indexName, indexProps] of Object.entries(
|
|
51
|
+
for (const [indexName, indexProps] of Object.entries(table.indexes ?? {})) {
|
|
51
52
|
const onColNames = Array.isArray(indexProps.on) ? indexProps.on : [indexProps.on];
|
|
52
53
|
const onCols = onColNames.map((colName) => ormTable[colName]);
|
|
53
54
|
if (!atLeastOne(onCols))
|
|
@@ -56,7 +57,7 @@ function collectionToTable(name, collection) {
|
|
|
56
57
|
}
|
|
57
58
|
return indexes;
|
|
58
59
|
});
|
|
59
|
-
return
|
|
60
|
+
return drizzleTable;
|
|
60
61
|
}
|
|
61
62
|
function atLeastOne(arr) {
|
|
62
63
|
return arr.length > 0;
|
|
@@ -116,9 +117,10 @@ export {
|
|
|
116
117
|
FALSE,
|
|
117
118
|
NOW,
|
|
118
119
|
TRUE,
|
|
119
|
-
|
|
120
|
+
asDrizzleTable,
|
|
120
121
|
createLocalDatabaseClient,
|
|
121
122
|
createRemoteDatabaseClient,
|
|
122
123
|
hasPrimaryKey,
|
|
124
|
+
seedLocal,
|
|
123
125
|
sql
|
|
124
126
|
};
|
|
@@ -1,37 +1,38 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import { type
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
db: SqliteRemoteDatabase;
|
|
1
|
+
import type { BooleanColumn, ColumnType, DBColumn, DBTable, DBTables, DateColumn, JsonColumn, NumberColumn, TextColumn } from '../core/types.js';
|
|
2
|
+
import { type SqliteDB } from './index.js';
|
|
3
|
+
export declare const SEED_DEV_FILE_NAME: string[];
|
|
4
|
+
export declare function seedLocal({ db, tables, fileGlob, }: {
|
|
5
|
+
db: SqliteDB;
|
|
7
6
|
tables: DBTables;
|
|
7
|
+
fileGlob: Record<string, () => Promise<void>>;
|
|
8
8
|
}): Promise<void>;
|
|
9
|
-
export declare function
|
|
10
|
-
db:
|
|
11
|
-
|
|
12
|
-
logger?: AstroIntegrationLogger;
|
|
13
|
-
mode: 'dev' | 'build';
|
|
9
|
+
export declare function recreateTables({ db, tables }: {
|
|
10
|
+
db: SqliteDB;
|
|
11
|
+
tables: DBTables;
|
|
14
12
|
}): Promise<void>;
|
|
15
|
-
export declare function
|
|
16
|
-
export declare function
|
|
17
|
-
export declare function
|
|
13
|
+
export declare function getDropTableIfExistsQuery(tableName: string): string;
|
|
14
|
+
export declare function getCreateTableQuery(tableName: string, table: DBTable): string;
|
|
15
|
+
export declare function getCreateIndexQueries(tableName: string, table: Pick<DBTable, 'indexes'>): string[];
|
|
16
|
+
export declare function getCreateForeignKeyQueries(tableName: string, table: DBTable): string[];
|
|
18
17
|
export declare function schemaTypeToSqlType(type: ColumnType): 'text' | 'integer';
|
|
19
18
|
export declare function getModifiers(columnName: string, column: DBColumn): string;
|
|
20
19
|
export declare function getReferencesConfig(column: DBColumn): {
|
|
21
20
|
type: "number";
|
|
22
21
|
schema: ({
|
|
23
22
|
unique: boolean;
|
|
23
|
+
deprecated: boolean;
|
|
24
24
|
name?: string | undefined;
|
|
25
25
|
label?: string | undefined;
|
|
26
26
|
collection?: string | undefined;
|
|
27
27
|
} & {
|
|
28
28
|
optional: boolean;
|
|
29
29
|
primaryKey: false;
|
|
30
|
-
default?: number | import("
|
|
30
|
+
default?: number | import("./types.js").SerializedSQL | undefined;
|
|
31
31
|
} & {
|
|
32
32
|
references?: any | undefined;
|
|
33
33
|
}) | ({
|
|
34
34
|
unique: boolean;
|
|
35
|
+
deprecated: boolean;
|
|
35
36
|
name?: string | undefined;
|
|
36
37
|
label?: string | undefined;
|
|
37
38
|
collection?: string | undefined;
|
|
@@ -46,10 +47,11 @@ export declare function getReferencesConfig(column: DBColumn): {
|
|
|
46
47
|
type: "text";
|
|
47
48
|
schema: ({
|
|
48
49
|
unique: boolean;
|
|
50
|
+
deprecated: boolean;
|
|
49
51
|
name?: string | undefined;
|
|
50
52
|
label?: string | undefined;
|
|
51
53
|
collection?: string | undefined;
|
|
52
|
-
default?: string | import("
|
|
54
|
+
default?: string | import("./types.js").SerializedSQL | undefined;
|
|
53
55
|
multiline?: boolean | undefined;
|
|
54
56
|
} & {
|
|
55
57
|
optional: boolean;
|
|
@@ -58,10 +60,11 @@ export declare function getReferencesConfig(column: DBColumn): {
|
|
|
58
60
|
references?: any | undefined;
|
|
59
61
|
}) | ({
|
|
60
62
|
unique: boolean;
|
|
63
|
+
deprecated: boolean;
|
|
61
64
|
name?: string | undefined;
|
|
62
65
|
label?: string | undefined;
|
|
63
66
|
collection?: string | undefined;
|
|
64
|
-
default?: string | import("
|
|
67
|
+
default?: string | import("./types.js").SerializedSQL | undefined;
|
|
65
68
|
multiline?: boolean | undefined;
|
|
66
69
|
} & {
|
|
67
70
|
primaryKey: true;
|