mirror of
https://github.com/withastro/astro.git
synced 2024-12-30 22:03:56 -05:00
refactor: store raw sql instead
This commit is contained in:
parent
6adb7fc985
commit
16e899643d
4 changed files with 8 additions and 6 deletions
|
@ -11,7 +11,7 @@ import {
|
|||
type TextField,
|
||||
} from '../core/types.js';
|
||||
import { bold } from 'kleur/colors';
|
||||
import { SQL, sql } from 'drizzle-orm';
|
||||
import { type SQL, sql } from 'drizzle-orm';
|
||||
import { SQLiteAsyncDialect } from 'drizzle-orm/sqlite-core';
|
||||
import type { AstroIntegrationLogger } from 'astro';
|
||||
import type { DBUserConfig } from '../core/types.js';
|
||||
|
@ -222,7 +222,7 @@ function toDefault<T>(def: T | SQL<any>): string {
|
|||
|
||||
function getDefaultValueSql(columnName: string, column: DBFieldWithDefault): string {
|
||||
if (isSerializedSQL(column.default)) {
|
||||
return sqlite.sqlToQuery(new SQL(column.default.queryChunks)).sql;
|
||||
return column.default.sql;
|
||||
}
|
||||
|
||||
switch (column.type) {
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import { type SQLiteInsertValue } from 'drizzle-orm/sqlite-core';
|
||||
import { SQLiteAsyncDialect, type SQLiteInsertValue } from 'drizzle-orm/sqlite-core';
|
||||
import type { InferSelectModel } from 'drizzle-orm';
|
||||
import { collectionToTable, type SqliteDB, type Table } from '../runtime/index.js';
|
||||
import { z, type ZodTypeDef } from 'zod';
|
||||
|
@ -10,10 +10,12 @@ export type MaybePromise<T> = T | Promise<T>;
|
|||
export type MaybeArray<T> = T | T[];
|
||||
|
||||
// Transform to serializable object for migration files
|
||||
const sqlite = new SQLiteAsyncDialect();
|
||||
|
||||
const sqlSchema = z.instanceof(SQL<any>).transform(
|
||||
(sqlObj): SerializedSQL => ({
|
||||
[SERIALIZED_SQL_KEY]: true,
|
||||
queryChunks: sqlObj.queryChunks,
|
||||
sql: sqlite.sqlToQuery(sqlObj).sql,
|
||||
})
|
||||
);
|
||||
|
||||
|
|
|
@ -149,7 +149,7 @@ function columnMapper(fieldName: string, field: DBField, isJsonSerializable: boo
|
|||
|
||||
function handleSerializedSQL<T>(def: T | SerializedSQL) {
|
||||
if (isSerializedSQL(def)) {
|
||||
return new SQL(def.queryChunks);
|
||||
return sql.raw(def.sql);
|
||||
}
|
||||
return def;
|
||||
}
|
||||
|
|
|
@ -101,7 +101,7 @@ export type Table<
|
|||
export const SERIALIZED_SQL_KEY = '__serializedSQL';
|
||||
export type SerializedSQL = {
|
||||
[SERIALIZED_SQL_KEY]: true;
|
||||
queryChunks: SQLChunk[];
|
||||
sql: string;
|
||||
};
|
||||
|
||||
export function isSerializedSQL(value: any): value is SerializedSQL {
|
||||
|
|
Loading…
Reference in a new issue