mirror of
https://github.com/withastro/astro.git
synced 2025-01-27 22:19:04 -05:00
wip
This commit is contained in:
parent
452a3c8581
commit
3ed9ae33e5
2 changed files with 27 additions and 12 deletions
|
@ -16,6 +16,7 @@ import { SQLiteAsyncDialect } from 'drizzle-orm/sqlite-core';
|
||||||
import type { AstroIntegrationLogger } from 'astro';
|
import type { AstroIntegrationLogger } from 'astro';
|
||||||
import type { DBUserConfig } from '../core/types.js';
|
import type { DBUserConfig } from '../core/types.js';
|
||||||
import { hasPrimaryKey } from '../runtime/index.js';
|
import { hasPrimaryKey } from '../runtime/index.js';
|
||||||
|
import type { SQLiteInsert } from 'drizzle-orm/sqlite-core';
|
||||||
|
|
||||||
const sqlite = new SQLiteAsyncDialect();
|
const sqlite = new SQLiteAsyncDialect();
|
||||||
|
|
||||||
|
@ -37,6 +38,10 @@ export async function setupDbTables({
|
||||||
}) {
|
}) {
|
||||||
const setupQueries: SQL[] = [];
|
const setupQueries: SQL[] = [];
|
||||||
for (const [name, collection] of Object.entries(collections)) {
|
for (const [name, collection] of Object.entries(collections)) {
|
||||||
|
// We don't reset writable collections in production
|
||||||
|
if (mode === 'build' && collection.writable) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
const dropQuery = sql.raw(`DROP TABLE IF EXISTS ${name}`);
|
const dropQuery = sql.raw(`DROP TABLE IF EXISTS ${name}`);
|
||||||
const createQuery = sql.raw(getCreateTableQuery(name, collection, useForeignKeys));
|
const createQuery = sql.raw(getCreateTableQuery(name, collection, useForeignKeys));
|
||||||
const indexQueries = getCreateIndexQueries(name, collection);
|
const indexQueries = getCreateIndexQueries(name, collection);
|
||||||
|
@ -48,17 +53,26 @@ export async function setupDbTables({
|
||||||
if (data) {
|
if (data) {
|
||||||
try {
|
try {
|
||||||
await data({
|
await data({
|
||||||
seed: async ({ table }, values) => {
|
seed: async (collection, values, options = {}) => {
|
||||||
const result = Array.isArray(values)
|
const shouldRunInDev = (options.when === 'dev' || options.when === 'always') ? true : true;
|
||||||
? db.insert(table).values(values).returning()
|
const shouldRunInBuild = (options.when === 'build' || options.when === 'always') ? true : !collection.writable;
|
||||||
: db
|
const shouldRun = mode === 'dev' ? shouldRunInDev : shouldRunInBuild;
|
||||||
.insert(table)
|
if (options.returning && (!shouldRunInDev || !shouldRunInBuild)) {
|
||||||
.values(values as any)
|
throw new Error(`'returning' is set to true, but this data will not always seed. Set 'when' to 'always' to seed this data on every run, including production.`);
|
||||||
.returning()
|
}
|
||||||
.get();
|
if (!shouldRun) {
|
||||||
// Drizzle types don't *quite* line up, and it's tough to debug why.
|
return;
|
||||||
// we're casting and calling this close enough :)
|
}
|
||||||
return result as any;
|
let result: SQLiteInsert<any, any, any, any> = db
|
||||||
|
.insert(collection.table)
|
||||||
|
.values(values as any);
|
||||||
|
if (options.returning) {
|
||||||
|
result = result.returning();
|
||||||
|
}
|
||||||
|
if (!Array.isArray(values)) {
|
||||||
|
result = result.get();
|
||||||
|
}
|
||||||
|
return await result.execute();
|
||||||
},
|
},
|
||||||
db,
|
db,
|
||||||
mode,
|
mode,
|
||||||
|
|
|
@ -207,7 +207,8 @@ export type DBDataContext = {
|
||||||
TData extends MaybeArray<SQLiteInsertValue<Table<string, TFields>>>,
|
TData extends MaybeArray<SQLiteInsertValue<Table<string, TFields>>>,
|
||||||
>(
|
>(
|
||||||
collection: ResolvedCollectionConfig<TFields>,
|
collection: ResolvedCollectionConfig<TFields>,
|
||||||
data: TData
|
data: TData,
|
||||||
|
options?: { returning?: boolean, when?: 'build' | 'dev' | 'always' | 'never'}
|
||||||
) => Promise<
|
) => Promise<
|
||||||
TData extends Array<SQLiteInsertValue<Table<string, TFields>>>
|
TData extends Array<SQLiteInsertValue<Table<string, TFields>>>
|
||||||
? InferSelectModel<Table<string, TFields>>[]
|
? InferSelectModel<Table<string, TFields>>[]
|
||||||
|
|
Loading…
Add table
Reference in a new issue