@astrojs/db 0.7.2 → 0.8.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.
@@ -0,0 +1,151 @@
1
+ import { SQL } from "drizzle-orm";
2
+ import { SQLiteAsyncDialect } from "drizzle-orm/sqlite-core";
3
+ import { z } from "zod";
4
+ import { SERIALIZED_SQL_KEY } from "../runtime/types.js";
5
+ import { errorMap } from "./integration/error-map.js";
6
+ const sqlite = new SQLiteAsyncDialect();
7
+ const sqlSchema = z.instanceof(SQL).transform(
8
+ (sqlObj) => ({
9
+ [SERIALIZED_SQL_KEY]: true,
10
+ sql: sqlite.sqlToQuery(sqlObj).sql
11
+ })
12
+ );
13
+ const baseColumnSchema = z.object({
14
+ label: z.string().optional(),
15
+ optional: z.boolean().optional().default(false),
16
+ unique: z.boolean().optional().default(false),
17
+ deprecated: z.boolean().optional().default(false),
18
+ // Defined when `defineReadableTable()` is called
19
+ name: z.string().optional(),
20
+ // TODO: rename to `tableName`. Breaking schema change
21
+ collection: z.string().optional()
22
+ });
23
+ const booleanColumnSchema = z.object({
24
+ type: z.literal("boolean"),
25
+ schema: baseColumnSchema.extend({
26
+ default: z.union([z.boolean(), sqlSchema]).optional()
27
+ })
28
+ });
29
+ const numberColumnBaseSchema = baseColumnSchema.omit({ optional: true }).and(
30
+ z.union([
31
+ z.object({
32
+ primaryKey: z.literal(false).optional().default(false),
33
+ optional: baseColumnSchema.shape.optional,
34
+ default: z.union([z.number(), sqlSchema]).optional()
35
+ }),
36
+ z.object({
37
+ // `integer primary key` uses ROWID as the default value.
38
+ // `optional` and `default` do not have an effect,
39
+ // so disable these config options for primary keys.
40
+ primaryKey: z.literal(true),
41
+ optional: z.literal(false).optional(),
42
+ default: z.literal(void 0).optional()
43
+ })
44
+ ])
45
+ );
46
+ const numberColumnOptsSchema = numberColumnBaseSchema.and(
47
+ z.object({
48
+ references: z.function().returns(z.lazy(() => numberColumnSchema)).optional().transform((fn) => fn?.())
49
+ })
50
+ );
51
+ const numberColumnSchema = z.object({
52
+ type: z.literal("number"),
53
+ schema: numberColumnOptsSchema
54
+ });
55
+ const textColumnBaseSchema = baseColumnSchema.omit({ optional: true }).extend({
56
+ default: z.union([z.string(), sqlSchema]).optional(),
57
+ multiline: z.boolean().optional()
58
+ }).and(
59
+ z.union([
60
+ z.object({
61
+ primaryKey: z.literal(false).optional().default(false),
62
+ optional: baseColumnSchema.shape.optional
63
+ }),
64
+ z.object({
65
+ // text primary key allows NULL values.
66
+ // NULL values bypass unique checks, which could
67
+ // lead to duplicate URLs per record in Astro Studio.
68
+ // disable `optional` for primary keys.
69
+ primaryKey: z.literal(true),
70
+ optional: z.literal(false).optional()
71
+ })
72
+ ])
73
+ );
74
+ const textColumnOptsSchema = textColumnBaseSchema.and(
75
+ z.object({
76
+ references: z.function().returns(z.lazy(() => textColumnSchema)).optional().transform((fn) => fn?.())
77
+ })
78
+ );
79
+ const textColumnSchema = z.object({
80
+ type: z.literal("text"),
81
+ schema: textColumnOptsSchema
82
+ });
83
+ const dateColumnSchema = z.object({
84
+ type: z.literal("date"),
85
+ schema: baseColumnSchema.extend({
86
+ default: z.union([
87
+ sqlSchema,
88
+ // transform to ISO string for serialization
89
+ z.date().transform((d) => d.toISOString())
90
+ ]).optional()
91
+ })
92
+ });
93
+ const jsonColumnSchema = z.object({
94
+ type: z.literal("json"),
95
+ schema: baseColumnSchema.extend({
96
+ default: z.unknown().optional()
97
+ })
98
+ });
99
+ const columnSchema = z.union([
100
+ booleanColumnSchema,
101
+ numberColumnSchema,
102
+ textColumnSchema,
103
+ dateColumnSchema,
104
+ jsonColumnSchema
105
+ ]);
106
+ const referenceableColumnSchema = z.union([textColumnSchema, numberColumnSchema]);
107
+ const columnsSchema = z.record(columnSchema);
108
+ const indexSchema = z.object({
109
+ on: z.string().or(z.array(z.string())),
110
+ unique: z.boolean().optional()
111
+ });
112
+ const foreignKeysSchema = z.object({
113
+ columns: z.string().or(z.array(z.string())),
114
+ references: z.function().returns(z.lazy(() => referenceableColumnSchema.or(z.array(referenceableColumnSchema)))).transform((fn) => fn())
115
+ });
116
+ const tableSchema = z.object({
117
+ columns: columnsSchema,
118
+ indexes: z.record(indexSchema).optional(),
119
+ foreignKeys: z.array(foreignKeysSchema).optional(),
120
+ deprecated: z.boolean().optional().default(false)
121
+ });
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;
129
+ }
130
+ }
131
+ return rawTables;
132
+ }, z.record(tableSchema));
133
+ const dbConfigSchema = z.object({
134
+ tables: tablesSchema.optional()
135
+ });
136
+ export {
137
+ booleanColumnSchema,
138
+ columnSchema,
139
+ columnsSchema,
140
+ dateColumnSchema,
141
+ dbConfigSchema,
142
+ indexSchema,
143
+ jsonColumnSchema,
144
+ numberColumnOptsSchema,
145
+ numberColumnSchema,
146
+ referenceableColumnSchema,
147
+ tableSchema,
148
+ tablesSchema,
149
+ textColumnOptsSchema,
150
+ textColumnSchema
151
+ };