@astrojs/db 0.3.5 → 0.4.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/link/index.js +7 -3
- package/dist/core/cli/commands/push/index.js +3 -3
- package/dist/core/cli/migration-queries.d.ts +4 -4
- package/dist/core/cli/migration-queries.js +73 -73
- package/dist/core/cli/migrations.js +15 -7
- package/dist/core/errors.js +7 -3
- package/dist/core/integration/file-url.js +2 -1
- package/dist/core/integration/index.js +106 -57
- package/dist/core/integration/typegen.d.ts +3 -3
- package/dist/core/integration/typegen.js +8 -8
- package/dist/core/integration/vite-plugin-db.d.ts +14 -9
- package/dist/core/integration/vite-plugin-db.js +15 -12
- package/dist/core/integration/vite-plugin-inject-env-ts.d.ts +4 -2
- package/dist/core/integration/vite-plugin-inject-env-ts.js +8 -4
- package/dist/core/queries.d.ts +12 -12
- package/dist/core/queries.js +49 -46
- package/dist/core/types.d.ts +232 -231
- package/dist/core/types.js +56 -55
- package/dist/core/utils.js +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +4 -4
- package/dist/runtime/db-client.d.ts +9 -4
- package/dist/runtime/db-client.js +11 -7
- package/dist/runtime/index.d.ts +7 -7
- package/dist/runtime/index.js +27 -29
- package/dist/runtime/types.d.ts +7 -7
- package/package.json +3 -5
- package/components/Renderer.astro +0 -14
- package/components/astro-env.d.ts +0 -1
- package/components/index.ts +0 -2
- package/components/tsconfig.json +0 -7
- package/dist/cli/commands/push/index.d.ts +0 -6
- package/dist/cli/commands/shell/index.d.ts +0 -6
- package/dist/cli/commands/sync/index.d.ts +0 -6
- package/dist/cli/commands/verify/index.d.ts +0 -6
- package/dist/cli/index.d.ts +0 -6
- package/dist/cli/queries.d.ts +0 -18
- package/dist/cli/seed.d.ts +0 -6
- package/dist/cli/sync/admin.d.ts +0 -33
- package/dist/cli/sync/index.d.ts +0 -1
- package/dist/cli/sync/migrate.d.ts +0 -1
- package/dist/cli/sync/queries.d.ts +0 -19
- package/dist/cli/sync/remote-db.d.ts +0 -1
- package/dist/config.d.ts +0 -1374
- package/dist/consts.d.ts +0 -7
- package/dist/core/cli/commands/sync/index.d.ts +0 -6
- package/dist/error-map.d.ts +0 -6
- package/dist/errors.d.ts +0 -3
- package/dist/file-url-integration.d.ts +0 -2
- package/dist/integration.d.ts +0 -2
- package/dist/internal-drizzle.d.ts +0 -1
- package/dist/internal.d.ts +0 -47
- package/dist/load-astro-config.d.ts +0 -6
- package/dist/migrations.d.ts +0 -12
- package/dist/root.d.ts +0 -3
- package/dist/typegen.d.ts +0 -5
- package/dist/types.d.ts +0 -1604
- package/dist/utils-runtime.d.ts +0 -1
- package/dist/utils.d.ts +0 -59
- package/dist/vite-plugin-db.d.ts +0 -19
- package/dist/vite-plugin-inject-env-ts.d.ts +0 -9
package/dist/core/types.js
CHANGED
|
@@ -11,25 +11,25 @@ const sqlSchema = z.instanceof(SQL).transform(
|
|
|
11
11
|
sql: sqlite.sqlToQuery(sqlObj).sql
|
|
12
12
|
})
|
|
13
13
|
);
|
|
14
|
-
const
|
|
14
|
+
const baseColumnSchema = z.object({
|
|
15
15
|
label: z.string().optional(),
|
|
16
16
|
optional: z.boolean().optional().default(false),
|
|
17
17
|
unique: z.boolean().optional().default(false),
|
|
18
|
-
// Defined when `
|
|
18
|
+
// Defined when `defineReadableTable()` is called
|
|
19
19
|
name: z.string().optional(),
|
|
20
20
|
collection: z.string().optional()
|
|
21
21
|
});
|
|
22
|
-
const
|
|
22
|
+
const booleanColumnSchema = z.object({
|
|
23
23
|
type: z.literal("boolean"),
|
|
24
|
-
schema:
|
|
24
|
+
schema: baseColumnSchema.extend({
|
|
25
25
|
default: z.union([z.boolean(), sqlSchema]).optional()
|
|
26
26
|
})
|
|
27
27
|
});
|
|
28
|
-
const
|
|
28
|
+
const numberColumnBaseSchema = baseColumnSchema.omit({ optional: true }).and(
|
|
29
29
|
z.union([
|
|
30
30
|
z.object({
|
|
31
31
|
primaryKey: z.literal(false).optional().default(false),
|
|
32
|
-
optional:
|
|
32
|
+
optional: baseColumnSchema.shape.optional,
|
|
33
33
|
default: z.union([z.number(), sqlSchema]).optional()
|
|
34
34
|
}),
|
|
35
35
|
z.object({
|
|
@@ -42,23 +42,23 @@ const numberFieldBaseSchema = baseFieldSchema.omit({ optional: true }).and(
|
|
|
42
42
|
})
|
|
43
43
|
])
|
|
44
44
|
);
|
|
45
|
-
const
|
|
45
|
+
const numberColumnOptsSchema = numberColumnBaseSchema.and(
|
|
46
46
|
z.object({
|
|
47
|
-
references: z.function().returns(z.lazy(() =>
|
|
47
|
+
references: z.function().returns(z.lazy(() => numberColumnSchema)).optional().transform((fn) => fn?.())
|
|
48
48
|
})
|
|
49
49
|
);
|
|
50
|
-
const
|
|
50
|
+
const numberColumnSchema = z.object({
|
|
51
51
|
type: z.literal("number"),
|
|
52
|
-
schema:
|
|
52
|
+
schema: numberColumnOptsSchema
|
|
53
53
|
});
|
|
54
|
-
const
|
|
54
|
+
const textColumnBaseSchema = baseColumnSchema.omit({ optional: true }).extend({
|
|
55
55
|
default: z.union([z.string(), sqlSchema]).optional(),
|
|
56
56
|
multiline: z.boolean().optional()
|
|
57
57
|
}).and(
|
|
58
58
|
z.union([
|
|
59
59
|
z.object({
|
|
60
60
|
primaryKey: z.literal(false).optional().default(false),
|
|
61
|
-
optional:
|
|
61
|
+
optional: baseColumnSchema.shape.optional
|
|
62
62
|
}),
|
|
63
63
|
z.object({
|
|
64
64
|
// text primary key allows NULL values.
|
|
@@ -70,18 +70,18 @@ const textFieldBaseSchema = baseFieldSchema.omit({ optional: true }).extend({
|
|
|
70
70
|
})
|
|
71
71
|
])
|
|
72
72
|
);
|
|
73
|
-
const
|
|
73
|
+
const textColumnOptsSchema = textColumnBaseSchema.and(
|
|
74
74
|
z.object({
|
|
75
|
-
references: z.function().returns(z.lazy(() =>
|
|
75
|
+
references: z.function().returns(z.lazy(() => textColumnSchema)).optional().transform((fn) => fn?.())
|
|
76
76
|
})
|
|
77
77
|
);
|
|
78
|
-
const
|
|
78
|
+
const textColumnSchema = z.object({
|
|
79
79
|
type: z.literal("text"),
|
|
80
|
-
schema:
|
|
80
|
+
schema: textColumnOptsSchema
|
|
81
81
|
});
|
|
82
|
-
const
|
|
82
|
+
const dateColumnSchema = z.object({
|
|
83
83
|
type: z.literal("date"),
|
|
84
|
-
schema:
|
|
84
|
+
schema: baseColumnSchema.extend({
|
|
85
85
|
default: z.union([
|
|
86
86
|
sqlSchema,
|
|
87
87
|
// transform to ISO string for serialization
|
|
@@ -89,31 +89,31 @@ const dateFieldSchema = z.object({
|
|
|
89
89
|
]).optional()
|
|
90
90
|
})
|
|
91
91
|
});
|
|
92
|
-
const
|
|
92
|
+
const jsonColumnSchema = z.object({
|
|
93
93
|
type: z.literal("json"),
|
|
94
|
-
schema:
|
|
94
|
+
schema: baseColumnSchema.extend({
|
|
95
95
|
default: z.unknown().optional()
|
|
96
96
|
})
|
|
97
97
|
});
|
|
98
|
-
const
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
98
|
+
const columnSchema = z.union([
|
|
99
|
+
booleanColumnSchema,
|
|
100
|
+
numberColumnSchema,
|
|
101
|
+
textColumnSchema,
|
|
102
|
+
dateColumnSchema,
|
|
103
|
+
jsonColumnSchema
|
|
104
104
|
]);
|
|
105
|
-
const
|
|
106
|
-
const
|
|
105
|
+
const referenceableColumnSchema = z.union([textColumnSchema, numberColumnSchema]);
|
|
106
|
+
const columnsSchema = z.record(columnSchema);
|
|
107
107
|
const indexSchema = z.object({
|
|
108
108
|
on: z.string().or(z.array(z.string())),
|
|
109
109
|
unique: z.boolean().optional()
|
|
110
110
|
});
|
|
111
111
|
const foreignKeysSchema = z.object({
|
|
112
|
-
|
|
113
|
-
references: z.function().returns(z.lazy(() =>
|
|
112
|
+
columns: z.string().or(z.array(z.string())),
|
|
113
|
+
references: z.function().returns(z.lazy(() => referenceableColumnSchema.or(z.array(referenceableColumnSchema)))).transform((fn) => fn())
|
|
114
114
|
});
|
|
115
115
|
const baseCollectionSchema = z.object({
|
|
116
|
-
|
|
116
|
+
columns: columnsSchema,
|
|
117
117
|
indexes: z.record(indexSchema).optional(),
|
|
118
118
|
foreignKeys: z.array(foreignKeysSchema).optional()
|
|
119
119
|
});
|
|
@@ -124,17 +124,17 @@ const writableCollectionSchema = baseCollectionSchema.extend({
|
|
|
124
124
|
writable: z.literal(true)
|
|
125
125
|
});
|
|
126
126
|
const collectionSchema = z.union([readableCollectionSchema, writableCollectionSchema]);
|
|
127
|
-
const
|
|
128
|
-
const
|
|
129
|
-
for (const [collectionName, collection] of Object.entries(
|
|
127
|
+
const tablesSchema = z.preprocess((rawCollections) => {
|
|
128
|
+
const tables = z.record(z.any()).parse(rawCollections, { errorMap });
|
|
129
|
+
for (const [collectionName, collection] of Object.entries(tables)) {
|
|
130
130
|
collection.table = collectionToTable(
|
|
131
131
|
collectionName,
|
|
132
132
|
collectionSchema.parse(collection, { errorMap })
|
|
133
133
|
);
|
|
134
|
-
const {
|
|
135
|
-
for (const [
|
|
136
|
-
|
|
137
|
-
|
|
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;
|
|
138
138
|
}
|
|
139
139
|
}
|
|
140
140
|
return rawCollections;
|
|
@@ -142,10 +142,11 @@ const collectionsSchema = z.preprocess((rawCollections) => {
|
|
|
142
142
|
function defineData(fn) {
|
|
143
143
|
return fn;
|
|
144
144
|
}
|
|
145
|
+
const dbDataFn = z.function().returns(z.union([z.void(), z.promise(z.void())]));
|
|
145
146
|
const dbConfigSchema = z.object({
|
|
146
147
|
studio: z.boolean().optional(),
|
|
147
|
-
|
|
148
|
-
data: z.
|
|
148
|
+
tables: tablesSchema.optional(),
|
|
149
|
+
data: z.union([dbDataFn, z.array(dbDataFn)]).optional(),
|
|
149
150
|
unsafeWritable: z.boolean().optional().default(false)
|
|
150
151
|
});
|
|
151
152
|
const astroConfigWithDbSchema = z.object({
|
|
@@ -159,13 +160,13 @@ function baseDefineCollection(userConfig, writable) {
|
|
|
159
160
|
table: null
|
|
160
161
|
};
|
|
161
162
|
}
|
|
162
|
-
function
|
|
163
|
+
function defineReadableTable(userConfig) {
|
|
163
164
|
return baseDefineCollection(userConfig, false);
|
|
164
165
|
}
|
|
165
|
-
function
|
|
166
|
+
function defineWritableTable(userConfig) {
|
|
166
167
|
return baseDefineCollection(userConfig, true);
|
|
167
168
|
}
|
|
168
|
-
function
|
|
169
|
+
function createColumn(type, schema) {
|
|
169
170
|
return {
|
|
170
171
|
type,
|
|
171
172
|
/**
|
|
@@ -174,35 +175,35 @@ function createField(type, schema) {
|
|
|
174
175
|
schema
|
|
175
176
|
};
|
|
176
177
|
}
|
|
177
|
-
const
|
|
178
|
+
const column = {
|
|
178
179
|
number: (opts = {}) => {
|
|
179
|
-
return
|
|
180
|
+
return createColumn("number", opts);
|
|
180
181
|
},
|
|
181
182
|
boolean: (opts = {}) => {
|
|
182
|
-
return
|
|
183
|
+
return createColumn("boolean", opts);
|
|
183
184
|
},
|
|
184
185
|
text: (opts = {}) => {
|
|
185
|
-
return
|
|
186
|
+
return createColumn("text", opts);
|
|
186
187
|
},
|
|
187
188
|
date(opts = {}) {
|
|
188
|
-
return
|
|
189
|
+
return createColumn("date", opts);
|
|
189
190
|
},
|
|
190
191
|
json(opts = {}) {
|
|
191
|
-
return
|
|
192
|
+
return createColumn("json", opts);
|
|
192
193
|
}
|
|
193
194
|
};
|
|
194
195
|
export {
|
|
195
196
|
astroConfigWithDbSchema,
|
|
196
197
|
collectionSchema,
|
|
197
|
-
|
|
198
|
+
column,
|
|
199
|
+
columnSchema,
|
|
198
200
|
dbConfigSchema,
|
|
199
|
-
defineCollection,
|
|
200
201
|
defineData,
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
fieldSchema,
|
|
202
|
+
defineReadableTable,
|
|
203
|
+
defineWritableTable,
|
|
204
204
|
indexSchema,
|
|
205
205
|
readableCollectionSchema,
|
|
206
|
-
|
|
206
|
+
referenceableColumnSchema,
|
|
207
|
+
tablesSchema,
|
|
207
208
|
writableCollectionSchema
|
|
208
209
|
};
|
package/dist/core/utils.js
CHANGED
|
@@ -5,7 +5,7 @@ function getAstroStudioEnv(envMode = "") {
|
|
|
5
5
|
}
|
|
6
6
|
function getRemoteDatabaseUrl() {
|
|
7
7
|
const env = getAstroStudioEnv();
|
|
8
|
-
return env.ASTRO_STUDIO_REMOTE_DB_URL || "https://
|
|
8
|
+
return env.ASTRO_STUDIO_REMOTE_DB_URL || "https://db.services.astro.build";
|
|
9
9
|
}
|
|
10
10
|
function getAstroStudioUrl() {
|
|
11
11
|
const env = getAstroStudioEnv();
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { defineReadableTable, defineWritableTable, defineData, column } from './core/types.js';
|
|
2
2
|
export type { ResolvedCollectionConfig, DBDataContext } from './core/types.js';
|
|
3
3
|
export { cli } from './core/cli/index.js';
|
|
4
4
|
export { integration as default } from './core/integration/index.js';
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { defineReadableTable, defineWritableTable, defineData, column } from "./core/types.js";
|
|
2
2
|
import { cli } from "./core/cli/index.js";
|
|
3
3
|
import { integration } from "./core/integration/index.js";
|
|
4
4
|
import { sql, NOW, TRUE, FALSE } from "./runtime/index.js";
|
|
@@ -7,10 +7,10 @@ export {
|
|
|
7
7
|
NOW,
|
|
8
8
|
TRUE,
|
|
9
9
|
cli,
|
|
10
|
+
column,
|
|
10
11
|
integration as default,
|
|
11
|
-
defineCollection,
|
|
12
12
|
defineData,
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
defineReadableTable,
|
|
14
|
+
defineWritableTable,
|
|
15
15
|
sql
|
|
16
16
|
};
|
|
@@ -1,7 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
/// <reference types="node" resolution-mode="require"/>
|
|
2
|
+
import { type DBTables } from '../core/types.js';
|
|
3
|
+
import type { LibSQLDatabase } from 'drizzle-orm/libsql';
|
|
4
|
+
interface LocalDatabaseClient extends LibSQLDatabase, Disposable {
|
|
5
|
+
}
|
|
6
|
+
export declare function createLocalDatabaseClient({ tables, dbUrl, seeding, }: {
|
|
3
7
|
dbUrl: string;
|
|
4
|
-
|
|
8
|
+
tables: DBTables;
|
|
5
9
|
seeding: boolean;
|
|
6
|
-
}): Promise<
|
|
10
|
+
}): Promise<LocalDatabaseClient>;
|
|
7
11
|
export declare function createRemoteDatabaseClient(appToken: string, remoteDbURL: string): import("drizzle-orm/sqlite-proxy").SqliteRemoteDatabase<Record<string, never>>;
|
|
12
|
+
export {};
|
|
@@ -7,34 +7,38 @@ import { z } from "zod";
|
|
|
7
7
|
import { getTableName } from "drizzle-orm";
|
|
8
8
|
const isWebContainer = !!process.versions?.webcontainer;
|
|
9
9
|
async function createLocalDatabaseClient({
|
|
10
|
-
|
|
10
|
+
tables,
|
|
11
11
|
dbUrl,
|
|
12
12
|
seeding
|
|
13
13
|
}) {
|
|
14
14
|
const url = isWebContainer ? "file:content.db" : dbUrl;
|
|
15
15
|
const client = createClient({ url });
|
|
16
|
-
const db = drizzleLibsql(client)
|
|
16
|
+
const db = Object.assign(drizzleLibsql(client), {
|
|
17
|
+
[Symbol.dispose || Symbol.for("Symbol.dispose")]() {
|
|
18
|
+
client.close();
|
|
19
|
+
}
|
|
20
|
+
});
|
|
17
21
|
if (seeding)
|
|
18
22
|
return db;
|
|
19
23
|
const { insert: drizzleInsert, update: drizzleUpdate, delete: drizzleDelete } = db;
|
|
20
24
|
return Object.assign(db, {
|
|
21
25
|
insert(Table) {
|
|
22
|
-
checkIfModificationIsAllowed(
|
|
26
|
+
checkIfModificationIsAllowed(tables, Table);
|
|
23
27
|
return drizzleInsert.call(this, Table);
|
|
24
28
|
},
|
|
25
29
|
update(Table) {
|
|
26
|
-
checkIfModificationIsAllowed(
|
|
30
|
+
checkIfModificationIsAllowed(tables, Table);
|
|
27
31
|
return drizzleUpdate.call(this, Table);
|
|
28
32
|
},
|
|
29
33
|
delete(Table) {
|
|
30
|
-
checkIfModificationIsAllowed(
|
|
34
|
+
checkIfModificationIsAllowed(tables, Table);
|
|
31
35
|
return drizzleDelete.call(this, Table);
|
|
32
36
|
}
|
|
33
37
|
});
|
|
34
38
|
}
|
|
35
|
-
function checkIfModificationIsAllowed(
|
|
39
|
+
function checkIfModificationIsAllowed(tables, Table) {
|
|
36
40
|
const tableName = getTableName(Table);
|
|
37
|
-
const collection =
|
|
41
|
+
const collection = tables[tableName];
|
|
38
42
|
if (!collection.writable) {
|
|
39
43
|
throw new Error(`The [${tableName}] collection is read-only.`);
|
|
40
44
|
}
|
package/dist/runtime/index.d.ts
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
import type { SqliteRemoteDatabase } from 'drizzle-orm/sqlite-proxy';
|
|
2
|
-
import { type
|
|
3
|
-
import { type ColumnDataType, sql
|
|
2
|
+
import { type DBTable, type DBColumn } from '../core/types.js';
|
|
3
|
+
import { type ColumnDataType, sql } from 'drizzle-orm';
|
|
4
4
|
export { sql };
|
|
5
5
|
export type SqliteDB = SqliteRemoteDatabase;
|
|
6
6
|
export type { Table } from './types.js';
|
|
7
7
|
export { createRemoteDatabaseClient, createLocalDatabaseClient } from './db-client.js';
|
|
8
|
-
export declare function hasPrimaryKey(
|
|
9
|
-
export declare const NOW: SQL<unknown>;
|
|
10
|
-
export declare const TRUE: SQL<unknown>;
|
|
11
|
-
export declare const FALSE: SQL<unknown>;
|
|
12
|
-
export declare function collectionToTable(name: string, collection:
|
|
8
|
+
export declare function hasPrimaryKey(column: DBColumn): boolean;
|
|
9
|
+
export declare const NOW: import("drizzle-orm").SQL<unknown>;
|
|
10
|
+
export declare const TRUE: import("drizzle-orm").SQL<unknown>;
|
|
11
|
+
export declare const FALSE: import("drizzle-orm").SQL<unknown>;
|
|
12
|
+
export declare function collectionToTable(name: string, collection: DBTable): import("drizzle-orm/sqlite-core").SQLiteTableWithColumns<{
|
|
13
13
|
name: string;
|
|
14
14
|
schema: undefined;
|
|
15
15
|
columns: {
|
package/dist/runtime/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import {} from "../core/types.js";
|
|
2
|
-
import { sql
|
|
2
|
+
import { sql } from "drizzle-orm";
|
|
3
3
|
import {
|
|
4
4
|
customType,
|
|
5
5
|
integer,
|
|
@@ -7,11 +7,10 @@ import {
|
|
|
7
7
|
text,
|
|
8
8
|
index
|
|
9
9
|
} from "drizzle-orm/sqlite-core";
|
|
10
|
-
import { z } from "zod";
|
|
11
10
|
import { isSerializedSQL } from "./types.js";
|
|
12
11
|
import { createRemoteDatabaseClient, createLocalDatabaseClient } from "./db-client.js";
|
|
13
|
-
function hasPrimaryKey(
|
|
14
|
-
return "primaryKey" in
|
|
12
|
+
function hasPrimaryKey(column) {
|
|
13
|
+
return "primaryKey" in column.schema && !!column.schema.primaryKey;
|
|
15
14
|
}
|
|
16
15
|
const NOW = sql`CURRENT_TIMESTAMP`;
|
|
17
16
|
const TRUE = sql`TRUE`;
|
|
@@ -24,7 +23,6 @@ const dateType = customType({
|
|
|
24
23
|
return value.toISOString();
|
|
25
24
|
},
|
|
26
25
|
fromDriver(value) {
|
|
27
|
-
console.log("driver", value);
|
|
28
26
|
return new Date(value);
|
|
29
27
|
}
|
|
30
28
|
});
|
|
@@ -41,11 +39,11 @@ const jsonType = customType({
|
|
|
41
39
|
});
|
|
42
40
|
function collectionToTable(name, collection) {
|
|
43
41
|
const columns = {};
|
|
44
|
-
if (!Object.entries(collection.
|
|
42
|
+
if (!Object.entries(collection.columns).some(([, column]) => hasPrimaryKey(column))) {
|
|
45
43
|
columns["_id"] = integer("_id").primaryKey();
|
|
46
44
|
}
|
|
47
|
-
for (const [
|
|
48
|
-
columns[
|
|
45
|
+
for (const [columnName, column] of Object.entries(collection.columns)) {
|
|
46
|
+
columns[columnName] = columnMapper(columnName, column);
|
|
49
47
|
}
|
|
50
48
|
const table = sqliteTable(name, columns, (ormTable) => {
|
|
51
49
|
const indexes = {};
|
|
@@ -63,48 +61,48 @@ function collectionToTable(name, collection) {
|
|
|
63
61
|
function atLeastOne(arr) {
|
|
64
62
|
return arr.length > 0;
|
|
65
63
|
}
|
|
66
|
-
function columnMapper(
|
|
64
|
+
function columnMapper(columnName, column) {
|
|
67
65
|
let c;
|
|
68
|
-
switch (
|
|
66
|
+
switch (column.type) {
|
|
69
67
|
case "text": {
|
|
70
|
-
c = text(
|
|
71
|
-
if (
|
|
72
|
-
c = c.default(handleSerializedSQL(
|
|
73
|
-
if (
|
|
68
|
+
c = text(columnName);
|
|
69
|
+
if (column.schema.default !== void 0)
|
|
70
|
+
c = c.default(handleSerializedSQL(column.schema.default));
|
|
71
|
+
if (column.schema.primaryKey === true)
|
|
74
72
|
c = c.primaryKey();
|
|
75
73
|
break;
|
|
76
74
|
}
|
|
77
75
|
case "number": {
|
|
78
|
-
c = integer(
|
|
79
|
-
if (
|
|
80
|
-
c = c.default(handleSerializedSQL(
|
|
81
|
-
if (
|
|
76
|
+
c = integer(columnName);
|
|
77
|
+
if (column.schema.default !== void 0)
|
|
78
|
+
c = c.default(handleSerializedSQL(column.schema.default));
|
|
79
|
+
if (column.schema.primaryKey === true)
|
|
82
80
|
c = c.primaryKey();
|
|
83
81
|
break;
|
|
84
82
|
}
|
|
85
83
|
case "boolean": {
|
|
86
|
-
c = integer(
|
|
87
|
-
if (
|
|
88
|
-
c = c.default(handleSerializedSQL(
|
|
84
|
+
c = integer(columnName, { mode: "boolean" });
|
|
85
|
+
if (column.schema.default !== void 0)
|
|
86
|
+
c = c.default(handleSerializedSQL(column.schema.default));
|
|
89
87
|
break;
|
|
90
88
|
}
|
|
91
89
|
case "json":
|
|
92
|
-
c = jsonType(
|
|
93
|
-
if (
|
|
94
|
-
c = c.default(
|
|
90
|
+
c = jsonType(columnName);
|
|
91
|
+
if (column.schema.default !== void 0)
|
|
92
|
+
c = c.default(column.schema.default);
|
|
95
93
|
break;
|
|
96
94
|
case "date": {
|
|
97
|
-
c = dateType(
|
|
98
|
-
if (
|
|
99
|
-
const def = handleSerializedSQL(
|
|
95
|
+
c = dateType(columnName);
|
|
96
|
+
if (column.schema.default !== void 0) {
|
|
97
|
+
const def = handleSerializedSQL(column.schema.default);
|
|
100
98
|
c = c.default(typeof def === "string" ? new Date(def) : def);
|
|
101
99
|
}
|
|
102
100
|
break;
|
|
103
101
|
}
|
|
104
102
|
}
|
|
105
|
-
if (!
|
|
103
|
+
if (!column.schema.optional)
|
|
106
104
|
c = c.notNull();
|
|
107
|
-
if (
|
|
105
|
+
if (column.schema.unique)
|
|
108
106
|
c = c.unique();
|
|
109
107
|
return c;
|
|
110
108
|
}
|
package/dist/runtime/types.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { ColumnDataType, ColumnBaseConfig } from 'drizzle-orm';
|
|
2
2
|
import type { SQLiteColumn, SQLiteTableWithColumns } from 'drizzle-orm/sqlite-core';
|
|
3
|
-
import type {
|
|
3
|
+
import type { DBColumn, ColumnsConfig } from '../core/types.js';
|
|
4
4
|
type GeneratedConfig<T extends ColumnDataType = ColumnDataType> = Pick<ColumnBaseConfig<T, string>, 'name' | 'tableName' | 'notNull' | 'hasDefault'>;
|
|
5
5
|
export type AstroText<T extends GeneratedConfig<'string'>> = SQLiteColumn<T & {
|
|
6
6
|
data: string;
|
|
@@ -42,21 +42,21 @@ export type AstroJson<T extends GeneratedConfig<'custom'>> = SQLiteColumn<T & {
|
|
|
42
42
|
enumValues: never;
|
|
43
43
|
baseColumn: never;
|
|
44
44
|
}>;
|
|
45
|
-
export type Column<T extends
|
|
46
|
-
export type Table<TTableName extends string,
|
|
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 ColumnsConfig> = SQLiteTableWithColumns<{
|
|
47
47
|
name: TTableName;
|
|
48
48
|
schema: undefined;
|
|
49
49
|
dialect: 'sqlite';
|
|
50
50
|
columns: {
|
|
51
|
-
[K in Extract<keyof
|
|
51
|
+
[K in Extract<keyof TColumns, string>]: Column<TColumns[K]['type'], {
|
|
52
52
|
tableName: TTableName;
|
|
53
53
|
name: K;
|
|
54
|
-
hasDefault:
|
|
54
|
+
hasDefault: TColumns[K]['schema'] extends {
|
|
55
55
|
default: NonNullable<unknown>;
|
|
56
|
-
} ? true :
|
|
56
|
+
} ? true : TColumns[K]['schema'] extends {
|
|
57
57
|
primaryKey: true;
|
|
58
58
|
} ? true : false;
|
|
59
|
-
notNull:
|
|
59
|
+
notNull: TColumns[K]['schema']['optional'] extends true ? false : true;
|
|
60
60
|
}>;
|
|
61
61
|
};
|
|
62
62
|
}>;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@astrojs/db",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -12,7 +12,6 @@
|
|
|
12
12
|
"types": "./index.d.ts",
|
|
13
13
|
"import": "./dist/index.js"
|
|
14
14
|
},
|
|
15
|
-
"./components": "./components/index.ts",
|
|
16
15
|
"./runtime": {
|
|
17
16
|
"types": "./dist/runtime/index.d.ts",
|
|
18
17
|
"import": "./dist/runtime/index.js"
|
|
@@ -39,8 +38,7 @@
|
|
|
39
38
|
"files": [
|
|
40
39
|
"index.d.ts",
|
|
41
40
|
"config-augment.d.ts",
|
|
42
|
-
"dist"
|
|
43
|
-
"components"
|
|
41
|
+
"dist"
|
|
44
42
|
],
|
|
45
43
|
"keywords": [
|
|
46
44
|
"withastro",
|
|
@@ -70,7 +68,7 @@
|
|
|
70
68
|
"mocha": "^10.2.0",
|
|
71
69
|
"typescript": "^5.2.2",
|
|
72
70
|
"vite": "^4.4.11",
|
|
73
|
-
"astro": "4.4.
|
|
71
|
+
"astro": "4.4.4",
|
|
74
72
|
"astro-scripts": "0.0.14"
|
|
75
73
|
},
|
|
76
74
|
"scripts": {
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
import { Renderer as MarkdocRenderer } from '@astrojs/markdoc/components';
|
|
3
|
-
import { Markdoc } from '@astrojs/markdoc/config';
|
|
4
|
-
|
|
5
|
-
interface Props {
|
|
6
|
-
content: string;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
const { content } = Astro.props;
|
|
10
|
-
|
|
11
|
-
const ast = Markdoc.parse(content);
|
|
12
|
-
---
|
|
13
|
-
|
|
14
|
-
<MarkdocRenderer stringifiedAst={JSON.stringify(ast)} config={{}} />
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
/// <reference types="astro/client" />
|
package/components/index.ts
DELETED
package/components/tsconfig.json
DELETED
package/dist/cli/index.d.ts
DELETED
package/dist/cli/queries.d.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import type { DBCollection, DBSnapshot } from '../types.js';
|
|
2
|
-
interface PromptResponses {
|
|
3
|
-
allowDataLoss: boolean;
|
|
4
|
-
columnRenames: Record<string, string | false>;
|
|
5
|
-
collectionRenames: Record<string, string | false>;
|
|
6
|
-
}
|
|
7
|
-
export declare function getMigrationQueries({ oldSnapshot, newSnapshot, promptResponses, }: {
|
|
8
|
-
oldSnapshot: DBSnapshot;
|
|
9
|
-
newSnapshot: DBSnapshot;
|
|
10
|
-
promptResponses?: PromptResponses;
|
|
11
|
-
}): Promise<string[]>;
|
|
12
|
-
export declare function getCollectionChangeQueries({ collectionName, oldCollection, newCollection, promptResponses, }: {
|
|
13
|
-
collectionName: string;
|
|
14
|
-
oldCollection: DBCollection;
|
|
15
|
-
newCollection: DBCollection;
|
|
16
|
-
promptResponses?: PromptResponses;
|
|
17
|
-
}): Promise<string[]>;
|
|
18
|
-
export {};
|