0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-03-03 22:57:08 -05:00
This commit is contained in:
Matthew Phillips 2024-01-26 14:39:29 -05:00
parent 23eaa88192
commit f66b9df1c2
5 changed files with 21 additions and 7 deletions

View file

@ -1,6 +1,6 @@
{
"name": "@astrojs/db",
"version": "0.1.7",
"version": "0.1.15",
"description": "",
"license": "MIT",
"type": "module",

View file

@ -32,7 +32,7 @@ import {
} from 'drizzle-orm/sqlite-core';
import { z } from 'zod';
import type { AstroIntegrationLogger } from 'astro';
export { createRemoteDatabaseClient } from './utils-runtime.js';
export { createRemoteDatabaseClient, findLocalDatabase } from './utils-runtime.js';
export type SqliteDB = SqliteRemoteDatabase;
export type { Table } from './types.js';

View file

@ -1,13 +1,14 @@
import { existsSync } from 'node:fs';
import { mkdir, writeFile } from 'node:fs/promises';
import type { DBCollection, DBCollections, FieldType } from './types.js';
import type { DBCollection, DBCollections } from './types.js';
import { DB_TYPES_FILE, DRIZZLE_MOD_IMPORT, INTERNAL_MOD_IMPORT } from './consts.js';
export async function typegen({ collections, root }: { collections: DBCollections; root: URL }) {
const content = `// This file is generated by \`studio sync\`
declare module 'astro:db' {
export const db: import(${INTERNAL_MOD_IMPORT}).SqliteDB;
export * from ${DRIZZLE_MOD_IMPORT}
export const dbUrl: string;
export * from ${DRIZZLE_MOD_IMPORT};
${Object.entries(collections)
.map(([name, collection]) => generateTableType(name, collection))

View file

@ -1,6 +1,18 @@
import type { InStatement } from '@libsql/client';
import { drizzle } from 'drizzle-orm/sqlite-proxy';
import { z } from 'zod';
import { DB_PATH } from './consts.js';
export function findLocalDatabase(localDbURL: string): string {
let dbURL: URL | undefined = undefined;
if(process.env.VERCEL) {
dbURL = new URL(DB_PATH, 'file://' + process.cwd() + '/vercel/path0/');
} else {
dbURL = new URL(localDbURL);
}
return dbURL.toString();
}
export function createRemoteDatabaseClient(appToken: string, remoteDbURL: string) {
const url = new URL('./db/query/', remoteDbURL);

View file

@ -56,15 +56,16 @@ export function getVirtualModContents({
dbUrl: string;
}) {
return `
import { collectionToTable, createLocalDatabaseClient } from ${INTERNAL_MOD_IMPORT};
import { collectionToTable, createLocalDatabaseClient, findLocalDatabase } from ${INTERNAL_MOD_IMPORT};
export const dbUrl = findLocalDatabase(${JSON.stringify(dbUrl)});
const params = ${JSON.stringify({
collections,
seeding: false,
})};
params.dbUrl = dbUrl;
params.dbUrl = new URL(${JSON.stringify(DB_PATH)}, 'file://' + process.cwd() + '/');
console.log('createLocalDatabaseClient', params);
export const db = await createLocalDatabaseClient(params);
export * from ${DRIZZLE_MOD_IMPORT};