@astrojs/db
Advanced tools
Comparing version 0.1.16 to 0.1.17
import type { SQLiteInsertValue } from 'drizzle-orm/sqlite-core'; | ||
import type { SqliteDB, Table } from './internal.js'; | ||
import type { MaybeArray, collectionSchema } from './types.js'; | ||
import { type BooleanField, type DBFieldInput, type DateFieldInput, type JsonField, type NumberField, type TextField, type MaybePromise } from './types.js'; | ||
import { type BooleanField, type DBFieldInput, type DateFieldInput, type JsonField, type NumberField, type TextField, type indexSchema, type MaybePromise } from './types.js'; | ||
import { z } from 'zod'; | ||
export type DBFieldsConfig = z.input<typeof collectionSchema>['fields']; | ||
export type DBDataContext = { | ||
db: SqliteDB; | ||
seed<TFields extends DBFieldsConfig>(collection: ResolvedCollectionConfig<TFields>, data: MaybeArray<SQLiteInsertValue<Table<string, | ||
/** TODO: true type inference */ Record<Extract<keyof TFields, string>, DBFieldsConfig[number]>>>>): Promise<any> /** TODO: type output */; | ||
seed<TFields extends FieldsConfig>(collection: ResolvedCollectionConfig<TFields>, data: MaybeArray<SQLiteInsertValue<Table<string, | ||
/** TODO: true type inference */ Record<Extract<keyof TFields, string>, FieldsConfig[number]>>>>): Promise<any> /** TODO: type output */; | ||
mode: 'dev' | 'build'; | ||
@@ -116,5 +115,15 @@ }; | ||
}>]>>; | ||
writable: z.ZodLiteral<false>; | ||
indexes: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodObject<{ | ||
on: z.ZodUnion<[z.ZodString, z.ZodArray<z.ZodString, "many">]>; | ||
unique: z.ZodOptional<z.ZodBoolean>; | ||
}, "strip", z.ZodTypeAny, { | ||
on: (string | string[]) & (string | string[] | undefined); | ||
unique?: boolean | undefined; | ||
}, { | ||
on: (string | string[]) & (string | string[] | undefined); | ||
unique?: boolean | undefined; | ||
}>>>; | ||
table: z.ZodAny; | ||
_setMeta: z.ZodOptional<z.ZodFunction<z.ZodTuple<[], z.ZodUnknown>, z.ZodUnknown>>; | ||
writable: z.ZodLiteral<false>; | ||
}, "strip", z.ZodTypeAny, { | ||
@@ -156,2 +165,6 @@ fields: Record<string, { | ||
writable: false; | ||
indexes?: Record<string, { | ||
on: (string | string[]) & (string | string[] | undefined); | ||
unique?: boolean | undefined; | ||
}> | undefined; | ||
table?: any; | ||
@@ -195,2 +208,6 @@ _setMeta?: ((...args: unknown[]) => unknown) | undefined; | ||
writable: false; | ||
indexes?: Record<string, { | ||
on: (string | string[]) & (string | string[] | undefined); | ||
unique?: boolean | undefined; | ||
}> | undefined; | ||
table?: any; | ||
@@ -299,5 +316,15 @@ _setMeta?: ((...args: unknown[]) => unknown) | undefined; | ||
}>]>>; | ||
writable: z.ZodLiteral<true>; | ||
indexes: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodObject<{ | ||
on: z.ZodUnion<[z.ZodString, z.ZodArray<z.ZodString, "many">]>; | ||
unique: z.ZodOptional<z.ZodBoolean>; | ||
}, "strip", z.ZodTypeAny, { | ||
on: (string | string[]) & (string | string[] | undefined); | ||
unique?: boolean | undefined; | ||
}, { | ||
on: (string | string[]) & (string | string[] | undefined); | ||
unique?: boolean | undefined; | ||
}>>>; | ||
table: z.ZodAny; | ||
_setMeta: z.ZodOptional<z.ZodFunction<z.ZodTuple<[], z.ZodUnknown>, z.ZodUnknown>>; | ||
writable: z.ZodLiteral<true>; | ||
}, "strip", z.ZodTypeAny, { | ||
@@ -339,2 +366,6 @@ fields: Record<string, { | ||
writable: true; | ||
indexes?: Record<string, { | ||
on: (string | string[]) & (string | string[] | undefined); | ||
unique?: boolean | undefined; | ||
}> | undefined; | ||
table?: any; | ||
@@ -378,2 +409,6 @@ _setMeta?: ((...args: unknown[]) => unknown) | undefined; | ||
writable: true; | ||
indexes?: Record<string, { | ||
on: (string | string[]) & (string | string[] | undefined); | ||
unique?: boolean | undefined; | ||
}> | undefined; | ||
table?: any; | ||
@@ -421,2 +456,6 @@ _setMeta?: ((...args: unknown[]) => unknown) | undefined; | ||
writable: false; | ||
indexes?: Record<string, { | ||
on: (string | string[]) & (string | string[] | undefined); | ||
unique?: boolean | undefined; | ||
}> | undefined; | ||
table?: any; | ||
@@ -460,2 +499,6 @@ _setMeta?: ((...args: unknown[]) => unknown) | undefined; | ||
writable: true; | ||
indexes?: Record<string, { | ||
on: (string | string[]) & (string | string[] | undefined); | ||
unique?: boolean | undefined; | ||
}> | undefined; | ||
table?: any; | ||
@@ -503,2 +546,6 @@ _setMeta?: ((...args: unknown[]) => unknown) | undefined; | ||
writable: false; | ||
indexes?: Record<string, { | ||
on: (string | string[]) & (string | string[] | undefined); | ||
unique?: boolean | undefined; | ||
}> | undefined; | ||
table?: any; | ||
@@ -542,2 +589,6 @@ _setMeta?: ((...args: unknown[]) => unknown) | undefined; | ||
writable: true; | ||
indexes?: Record<string, { | ||
on: (string | string[]) & (string | string[] | undefined); | ||
unique?: boolean | undefined; | ||
}> | undefined; | ||
table?: any; | ||
@@ -655,5 +706,15 @@ _setMeta?: ((...args: unknown[]) => unknown) | undefined; | ||
}>]>>; | ||
writable: z.ZodLiteral<false>; | ||
indexes: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodObject<{ | ||
on: z.ZodUnion<[z.ZodString, z.ZodArray<z.ZodString, "many">]>; | ||
unique: z.ZodOptional<z.ZodBoolean>; | ||
}, "strip", z.ZodTypeAny, { | ||
on: (string | string[]) & (string | string[] | undefined); | ||
unique?: boolean | undefined; | ||
}, { | ||
on: (string | string[]) & (string | string[] | undefined); | ||
unique?: boolean | undefined; | ||
}>>>; | ||
table: z.ZodAny; | ||
_setMeta: z.ZodOptional<z.ZodFunction<z.ZodTuple<[], z.ZodUnknown>, z.ZodUnknown>>; | ||
writable: z.ZodLiteral<false>; | ||
}, "strip", z.ZodTypeAny, { | ||
@@ -695,2 +756,6 @@ fields: Record<string, { | ||
writable: false; | ||
indexes?: Record<string, { | ||
on: (string | string[]) & (string | string[] | undefined); | ||
unique?: boolean | undefined; | ||
}> | undefined; | ||
table?: any; | ||
@@ -734,2 +799,6 @@ _setMeta?: ((...args: unknown[]) => unknown) | undefined; | ||
writable: false; | ||
indexes?: Record<string, { | ||
on: (string | string[]) & (string | string[] | undefined); | ||
unique?: boolean | undefined; | ||
}> | undefined; | ||
table?: any; | ||
@@ -838,5 +907,15 @@ _setMeta?: ((...args: unknown[]) => unknown) | undefined; | ||
}>]>>; | ||
writable: z.ZodLiteral<true>; | ||
indexes: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodObject<{ | ||
on: z.ZodUnion<[z.ZodString, z.ZodArray<z.ZodString, "many">]>; | ||
unique: z.ZodOptional<z.ZodBoolean>; | ||
}, "strip", z.ZodTypeAny, { | ||
on: (string | string[]) & (string | string[] | undefined); | ||
unique?: boolean | undefined; | ||
}, { | ||
on: (string | string[]) & (string | string[] | undefined); | ||
unique?: boolean | undefined; | ||
}>>>; | ||
table: z.ZodAny; | ||
_setMeta: z.ZodOptional<z.ZodFunction<z.ZodTuple<[], z.ZodUnknown>, z.ZodUnknown>>; | ||
writable: z.ZodLiteral<true>; | ||
}, "strip", z.ZodTypeAny, { | ||
@@ -878,2 +957,6 @@ fields: Record<string, { | ||
writable: true; | ||
indexes?: Record<string, { | ||
on: (string | string[]) & (string | string[] | undefined); | ||
unique?: boolean | undefined; | ||
}> | undefined; | ||
table?: any; | ||
@@ -917,2 +1000,6 @@ _setMeta?: ((...args: unknown[]) => unknown) | undefined; | ||
writable: true; | ||
indexes?: Record<string, { | ||
on: (string | string[]) & (string | string[] | undefined); | ||
unique?: boolean | undefined; | ||
}> | undefined; | ||
table?: any; | ||
@@ -960,2 +1047,6 @@ _setMeta?: ((...args: unknown[]) => unknown) | undefined; | ||
writable: false; | ||
indexes?: Record<string, { | ||
on: (string | string[]) & (string | string[] | undefined); | ||
unique?: boolean | undefined; | ||
}> | undefined; | ||
table?: any; | ||
@@ -999,2 +1090,6 @@ _setMeta?: ((...args: unknown[]) => unknown) | undefined; | ||
writable: true; | ||
indexes?: Record<string, { | ||
on: (string | string[]) & (string | string[] | undefined); | ||
unique?: boolean | undefined; | ||
}> | undefined; | ||
table?: any; | ||
@@ -1042,2 +1137,6 @@ _setMeta?: ((...args: unknown[]) => unknown) | undefined; | ||
writable: false; | ||
indexes?: Record<string, { | ||
on: (string | string[]) & (string | string[] | undefined); | ||
unique?: boolean | undefined; | ||
}> | undefined; | ||
table?: any; | ||
@@ -1081,2 +1180,6 @@ _setMeta?: ((...args: unknown[]) => unknown) | undefined; | ||
writable: true; | ||
indexes?: Record<string, { | ||
on: (string | string[]) & (string | string[] | undefined); | ||
unique?: boolean | undefined; | ||
}> | undefined; | ||
table?: any; | ||
@@ -1126,2 +1229,6 @@ _setMeta?: ((...args: unknown[]) => unknown) | undefined; | ||
writable: false; | ||
indexes?: Record<string, { | ||
on: (string | string[]) & (string | string[] | undefined); | ||
unique?: boolean | undefined; | ||
}> | undefined; | ||
table?: any; | ||
@@ -1165,2 +1272,6 @@ _setMeta?: ((...args: unknown[]) => unknown) | undefined; | ||
writable: true; | ||
indexes?: Record<string, { | ||
on: (string | string[]) & (string | string[] | undefined); | ||
unique?: boolean | undefined; | ||
}> | undefined; | ||
table?: any; | ||
@@ -1210,2 +1321,6 @@ _setMeta?: ((...args: unknown[]) => unknown) | undefined; | ||
writable: false; | ||
indexes?: Record<string, { | ||
on: (string | string[]) & (string | string[] | undefined); | ||
unique?: boolean | undefined; | ||
}> | undefined; | ||
table?: any; | ||
@@ -1249,2 +1364,6 @@ _setMeta?: ((...args: unknown[]) => unknown) | undefined; | ||
writable: true; | ||
indexes?: Record<string, { | ||
on: (string | string[]) & (string | string[] | undefined); | ||
unique?: boolean | undefined; | ||
}> | undefined; | ||
table?: any; | ||
@@ -1256,11 +1375,16 @@ _setMeta?: ((...args: unknown[]) => unknown) | undefined; | ||
}>; | ||
type CollectionConfig<TFields extends DBFieldsConfig> = { | ||
export type FieldsConfig = z.input<typeof collectionSchema>['fields']; | ||
interface CollectionConfig<TFields extends FieldsConfig> extends Pick<z.input<typeof collectionSchema>, 'fields' | 'indexes'> { | ||
fields: TFields; | ||
}; | ||
export type ResolvedCollectionConfig<TFields extends DBFieldsConfig = DBFieldsConfig, Writable extends boolean = boolean> = CollectionConfig<TFields> & { | ||
indexes?: Record<string, IndexConfig<TFields>>; | ||
} | ||
interface IndexConfig<TFields extends FieldsConfig> extends z.input<typeof indexSchema> { | ||
on: MaybeArray<Extract<keyof TFields, string>>; | ||
} | ||
export type ResolvedCollectionConfig<TFields extends FieldsConfig = FieldsConfig, Writable extends boolean = boolean> = CollectionConfig<TFields> & { | ||
writable: Writable; | ||
table: Table<string, TFields>; | ||
}; | ||
export declare function defineCollection<TFields extends DBFieldsConfig>(userConfig: CollectionConfig<TFields>): ResolvedCollectionConfig<TFields, false>; | ||
export declare function defineWritableCollection<TFields extends DBFieldsConfig>(userConfig: CollectionConfig<TFields>): ResolvedCollectionConfig<TFields, true>; | ||
export declare function defineCollection<TFields extends FieldsConfig>(userConfig: CollectionConfig<TFields>): ResolvedCollectionConfig<TFields, false>; | ||
export declare function defineWritableCollection<TFields extends FieldsConfig>(userConfig: CollectionConfig<TFields>): ResolvedCollectionConfig<TFields, true>; | ||
export type AstroConfigWithDB = z.infer<typeof astroConfigWithDbSchema>; | ||
@@ -1267,0 +1391,0 @@ type FieldOpts<T extends DBFieldInput> = Omit<T, 'type'>; |
import type { AstroIntegration } from 'astro'; | ||
export declare function integration(): AstroIntegration; | ||
export declare function integration(): AstroIntegration[]; |
@@ -15,3 +15,4 @@ import { vitePluginDb } from "./vite-plugin-db.js"; | ||
import { bold } from "kleur/colors"; | ||
function integration() { | ||
import { fileURLIntegration } from "./file-url-integration.js"; | ||
function astroDBIntegration() { | ||
return { | ||
@@ -43,3 +44,4 @@ name: "astro:db", | ||
collections, | ||
appToken | ||
appToken, | ||
output: config.output | ||
}); | ||
@@ -66,3 +68,6 @@ } else { | ||
logger.info("Collections set up \u{1F680}"); | ||
dbPlugin = vitePluginDb({ connectToStudio: false, collections, dbUrl: dbUrl.href }); | ||
dbPlugin = vitePluginDb({ | ||
connectToStudio: false, | ||
collections | ||
}); | ||
} | ||
@@ -72,3 +77,13 @@ updateConfig({ | ||
assetsInclude: [DB_PATH], | ||
plugins: [dbPlugin, vitePluginInjectEnvTs(config)] | ||
plugins: [dbPlugin, vitePluginInjectEnvTs(config), { | ||
name: "my-plugin", | ||
resolveId(id) { | ||
if (id.endsWith("?server-path")) { | ||
} | ||
}, | ||
load(id) { | ||
if (id.endsWith("?server-path")) { | ||
} | ||
} | ||
}] | ||
} | ||
@@ -81,4 +96,7 @@ }); | ||
} | ||
function integration() { | ||
return [astroDBIntegration(), fileURLIntegration()]; | ||
} | ||
export { | ||
integration | ||
}; |
import type { SqliteRemoteDatabase } from 'drizzle-orm/sqlite-proxy'; | ||
import { type BooleanField, type DBCollection, type DBCollections, type DBField, type DateField, type FieldType, type JsonField, type NumberField, type TextField } from './types.js'; | ||
import { type LibSQLDatabase } from 'drizzle-orm/libsql'; | ||
import { type ColumnDataType } from 'drizzle-orm'; | ||
import { type SQL, type ColumnDataType } from 'drizzle-orm'; | ||
import type { AstroIntegrationLogger } from 'astro'; | ||
@@ -9,3 +8,3 @@ import type { DBUserConfig } from './config.js'; | ||
export type { Table } from './types.js'; | ||
export { createRemoteDatabaseClient, findLocalDatabase } from './utils-runtime.js'; | ||
export { createRemoteDatabaseClient } from './utils-runtime.js'; | ||
export declare function hasPrimaryKey(field: DBField): boolean; | ||
@@ -16,3 +15,3 @@ export declare function createLocalDatabaseClient({ collections, dbUrl, seeding, }: { | ||
seeding: boolean; | ||
}): Promise<LibSQLDatabase<Record<string, never>>>; | ||
}): Promise<import("drizzle-orm/libsql").LibSQLDatabase<Record<string, never>>>; | ||
export declare function setupDbTables({ db, data, collections, logger, mode, }: { | ||
@@ -26,2 +25,3 @@ db: SqliteRemoteDatabase; | ||
export declare function getCreateTableQuery(collectionName: string, collection: DBCollection): string; | ||
export declare function getTableIndexQueries(collectionName: string, collection: DBCollection): SQL<unknown>[]; | ||
export declare function schemaTypeToSqlType(type: FieldType): 'text' | 'integer'; | ||
@@ -28,0 +28,0 @@ export declare function getModifiers(fieldName: string, field: DBField): string; |
@@ -15,6 +15,7 @@ import { createClient } from "@libsql/client"; | ||
sqliteTable, | ||
text | ||
text, | ||
index | ||
} from "drizzle-orm/sqlite-core"; | ||
import { z } from "zod"; | ||
import { createRemoteDatabaseClient, findLocalDatabase } from "./utils-runtime.js"; | ||
import { createRemoteDatabaseClient } from "./utils-runtime.js"; | ||
const sqlite = new SQLiteAsyncDialect(); | ||
@@ -67,3 +68,4 @@ function hasPrimaryKey(field) { | ||
const createQuery = sql.raw(getCreateTableQuery(name, collection)); | ||
setupQueries.push(dropQuery, createQuery); | ||
const indexQueries = getTableIndexQueries(name, collection); | ||
setupQueries.push(dropQuery, createQuery, ...indexQueries); | ||
} | ||
@@ -117,2 +119,17 @@ for (const q of setupQueries) { | ||
} | ||
function getTableIndexQueries(collectionName, collection) { | ||
let queries = []; | ||
for (const [indexName, indexProps] of Object.entries(collection.indexes ?? {})) { | ||
const onColNames = Array.isArray(indexProps.on) ? indexProps.on : [indexProps.on]; | ||
const onCols = onColNames.map((colName) => sqlite.escapeName(colName)); | ||
const unique = indexProps.unique ? "UNIQUE " : ""; | ||
const indexQuery = sql.raw( | ||
`CREATE ${unique}INDEX ${sqlite.escapeName(indexName)} ON ${sqlite.escapeName( | ||
collectionName | ||
)} (${onCols.join(", ")})` | ||
); | ||
queries.push(indexQuery); | ||
} | ||
return queries; | ||
} | ||
function schemaTypeToSqlType(type) { | ||
@@ -210,5 +227,18 @@ switch (type) { | ||
} | ||
const table = sqliteTable(name, columns); | ||
const table = sqliteTable(name, columns, (ormTable) => { | ||
const indexes = {}; | ||
for (const [indexName, indexProps] of Object.entries(collection.indexes ?? {})) { | ||
const onColNames = Array.isArray(indexProps.on) ? indexProps.on : [indexProps.on]; | ||
const onCols = onColNames.map((colName) => ormTable[colName]); | ||
if (!atLeastOne(onCols)) | ||
continue; | ||
indexes[indexName] = index(indexName).on(...onCols); | ||
} | ||
return indexes; | ||
}); | ||
return table; | ||
} | ||
function atLeastOne(arr) { | ||
return arr.length > 0; | ||
} | ||
function columnMapper(fieldName, field, isJsonSerializable) { | ||
@@ -275,5 +305,5 @@ let c; | ||
createRemoteDatabaseClient, | ||
findLocalDatabase, | ||
getCreateTableQuery, | ||
getModifiers, | ||
getTableIndexQueries, | ||
hasDefault, | ||
@@ -280,0 +310,0 @@ hasPrimaryKey, |
@@ -308,2 +308,12 @@ import type { ColumnDataType, ColumnBaseConfig } from 'drizzle-orm'; | ||
}>]>>; | ||
export declare const indexSchema: z.ZodObject<{ | ||
on: z.ZodUnion<[z.ZodString, z.ZodArray<z.ZodString, "many">]>; | ||
unique: z.ZodOptional<z.ZodBoolean>; | ||
}, "strip", z.ZodTypeAny, { | ||
on: (string | string[]) & (string | string[] | undefined); | ||
unique?: boolean | undefined; | ||
}, { | ||
on: (string | string[]) & (string | string[] | undefined); | ||
unique?: boolean | undefined; | ||
}>; | ||
export declare const readableCollectionSchema: z.ZodObject<{ | ||
@@ -410,5 +420,15 @@ fields: z.ZodRecord<z.ZodString, z.ZodUnion<[z.ZodObject<{ | ||
}>]>>; | ||
writable: z.ZodLiteral<false>; | ||
indexes: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodObject<{ | ||
on: z.ZodUnion<[z.ZodString, z.ZodArray<z.ZodString, "many">]>; | ||
unique: z.ZodOptional<z.ZodBoolean>; | ||
}, "strip", z.ZodTypeAny, { | ||
on: (string | string[]) & (string | string[] | undefined); | ||
unique?: boolean | undefined; | ||
}, { | ||
on: (string | string[]) & (string | string[] | undefined); | ||
unique?: boolean | undefined; | ||
}>>>; | ||
table: z.ZodAny; | ||
_setMeta: z.ZodOptional<z.ZodFunction<z.ZodTuple<[], z.ZodUnknown>, z.ZodUnknown>>; | ||
writable: z.ZodLiteral<false>; | ||
}, "strip", z.ZodTypeAny, { | ||
@@ -450,2 +470,6 @@ fields: Record<string, { | ||
writable: false; | ||
indexes?: Record<string, { | ||
on: (string | string[]) & (string | string[] | undefined); | ||
unique?: boolean | undefined; | ||
}> | undefined; | ||
table?: any; | ||
@@ -489,2 +513,6 @@ _setMeta?: ((...args: unknown[]) => unknown) | undefined; | ||
writable: false; | ||
indexes?: Record<string, { | ||
on: (string | string[]) & (string | string[] | undefined); | ||
unique?: boolean | undefined; | ||
}> | undefined; | ||
table?: any; | ||
@@ -594,5 +622,15 @@ _setMeta?: ((...args: unknown[]) => unknown) | undefined; | ||
}>]>>; | ||
writable: z.ZodLiteral<true>; | ||
indexes: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodObject<{ | ||
on: z.ZodUnion<[z.ZodString, z.ZodArray<z.ZodString, "many">]>; | ||
unique: z.ZodOptional<z.ZodBoolean>; | ||
}, "strip", z.ZodTypeAny, { | ||
on: (string | string[]) & (string | string[] | undefined); | ||
unique?: boolean | undefined; | ||
}, { | ||
on: (string | string[]) & (string | string[] | undefined); | ||
unique?: boolean | undefined; | ||
}>>>; | ||
table: z.ZodAny; | ||
_setMeta: z.ZodOptional<z.ZodFunction<z.ZodTuple<[], z.ZodUnknown>, z.ZodUnknown>>; | ||
writable: z.ZodLiteral<true>; | ||
}, "strip", z.ZodTypeAny, { | ||
@@ -634,2 +672,6 @@ fields: Record<string, { | ||
writable: true; | ||
indexes?: Record<string, { | ||
on: (string | string[]) & (string | string[] | undefined); | ||
unique?: boolean | undefined; | ||
}> | undefined; | ||
table?: any; | ||
@@ -673,2 +715,6 @@ _setMeta?: ((...args: unknown[]) => unknown) | undefined; | ||
writable: true; | ||
indexes?: Record<string, { | ||
on: (string | string[]) & (string | string[] | undefined); | ||
unique?: boolean | undefined; | ||
}> | undefined; | ||
table?: any; | ||
@@ -778,5 +824,15 @@ _setMeta?: ((...args: unknown[]) => unknown) | undefined; | ||
}>]>>; | ||
writable: z.ZodLiteral<false>; | ||
indexes: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodObject<{ | ||
on: z.ZodUnion<[z.ZodString, z.ZodArray<z.ZodString, "many">]>; | ||
unique: z.ZodOptional<z.ZodBoolean>; | ||
}, "strip", z.ZodTypeAny, { | ||
on: (string | string[]) & (string | string[] | undefined); | ||
unique?: boolean | undefined; | ||
}, { | ||
on: (string | string[]) & (string | string[] | undefined); | ||
unique?: boolean | undefined; | ||
}>>>; | ||
table: z.ZodAny; | ||
_setMeta: z.ZodOptional<z.ZodFunction<z.ZodTuple<[], z.ZodUnknown>, z.ZodUnknown>>; | ||
writable: z.ZodLiteral<false>; | ||
}, "strip", z.ZodTypeAny, { | ||
@@ -818,2 +874,6 @@ fields: Record<string, { | ||
writable: false; | ||
indexes?: Record<string, { | ||
on: (string | string[]) & (string | string[] | undefined); | ||
unique?: boolean | undefined; | ||
}> | undefined; | ||
table?: any; | ||
@@ -857,2 +917,6 @@ _setMeta?: ((...args: unknown[]) => unknown) | undefined; | ||
writable: false; | ||
indexes?: Record<string, { | ||
on: (string | string[]) & (string | string[] | undefined); | ||
unique?: boolean | undefined; | ||
}> | undefined; | ||
table?: any; | ||
@@ -961,5 +1025,15 @@ _setMeta?: ((...args: unknown[]) => unknown) | undefined; | ||
}>]>>; | ||
writable: z.ZodLiteral<true>; | ||
indexes: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodObject<{ | ||
on: z.ZodUnion<[z.ZodString, z.ZodArray<z.ZodString, "many">]>; | ||
unique: z.ZodOptional<z.ZodBoolean>; | ||
}, "strip", z.ZodTypeAny, { | ||
on: (string | string[]) & (string | string[] | undefined); | ||
unique?: boolean | undefined; | ||
}, { | ||
on: (string | string[]) & (string | string[] | undefined); | ||
unique?: boolean | undefined; | ||
}>>>; | ||
table: z.ZodAny; | ||
_setMeta: z.ZodOptional<z.ZodFunction<z.ZodTuple<[], z.ZodUnknown>, z.ZodUnknown>>; | ||
writable: z.ZodLiteral<true>; | ||
}, "strip", z.ZodTypeAny, { | ||
@@ -1001,2 +1075,6 @@ fields: Record<string, { | ||
writable: true; | ||
indexes?: Record<string, { | ||
on: (string | string[]) & (string | string[] | undefined); | ||
unique?: boolean | undefined; | ||
}> | undefined; | ||
table?: any; | ||
@@ -1040,2 +1118,6 @@ _setMeta?: ((...args: unknown[]) => unknown) | undefined; | ||
writable: true; | ||
indexes?: Record<string, { | ||
on: (string | string[]) & (string | string[] | undefined); | ||
unique?: boolean | undefined; | ||
}> | undefined; | ||
table?: any; | ||
@@ -1145,5 +1227,15 @@ _setMeta?: ((...args: unknown[]) => unknown) | undefined; | ||
}>]>>; | ||
writable: z.ZodLiteral<false>; | ||
indexes: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodObject<{ | ||
on: z.ZodUnion<[z.ZodString, z.ZodArray<z.ZodString, "many">]>; | ||
unique: z.ZodOptional<z.ZodBoolean>; | ||
}, "strip", z.ZodTypeAny, { | ||
on: (string | string[]) & (string | string[] | undefined); | ||
unique?: boolean | undefined; | ||
}, { | ||
on: (string | string[]) & (string | string[] | undefined); | ||
unique?: boolean | undefined; | ||
}>>>; | ||
table: z.ZodAny; | ||
_setMeta: z.ZodOptional<z.ZodFunction<z.ZodTuple<[], z.ZodUnknown>, z.ZodUnknown>>; | ||
writable: z.ZodLiteral<false>; | ||
}, "strip", z.ZodTypeAny, { | ||
@@ -1185,2 +1277,6 @@ fields: Record<string, { | ||
writable: false; | ||
indexes?: Record<string, { | ||
on: (string | string[]) & (string | string[] | undefined); | ||
unique?: boolean | undefined; | ||
}> | undefined; | ||
table?: any; | ||
@@ -1224,2 +1320,6 @@ _setMeta?: ((...args: unknown[]) => unknown) | undefined; | ||
writable: false; | ||
indexes?: Record<string, { | ||
on: (string | string[]) & (string | string[] | undefined); | ||
unique?: boolean | undefined; | ||
}> | undefined; | ||
table?: any; | ||
@@ -1328,5 +1428,15 @@ _setMeta?: ((...args: unknown[]) => unknown) | undefined; | ||
}>]>>; | ||
writable: z.ZodLiteral<true>; | ||
indexes: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodObject<{ | ||
on: z.ZodUnion<[z.ZodString, z.ZodArray<z.ZodString, "many">]>; | ||
unique: z.ZodOptional<z.ZodBoolean>; | ||
}, "strip", z.ZodTypeAny, { | ||
on: (string | string[]) & (string | string[] | undefined); | ||
unique?: boolean | undefined; | ||
}, { | ||
on: (string | string[]) & (string | string[] | undefined); | ||
unique?: boolean | undefined; | ||
}>>>; | ||
table: z.ZodAny; | ||
_setMeta: z.ZodOptional<z.ZodFunction<z.ZodTuple<[], z.ZodUnknown>, z.ZodUnknown>>; | ||
writable: z.ZodLiteral<true>; | ||
}, "strip", z.ZodTypeAny, { | ||
@@ -1368,2 +1478,6 @@ fields: Record<string, { | ||
writable: true; | ||
indexes?: Record<string, { | ||
on: (string | string[]) & (string | string[] | undefined); | ||
unique?: boolean | undefined; | ||
}> | undefined; | ||
table?: any; | ||
@@ -1407,2 +1521,6 @@ _setMeta?: ((...args: unknown[]) => unknown) | undefined; | ||
writable: true; | ||
indexes?: Record<string, { | ||
on: (string | string[]) & (string | string[] | undefined); | ||
unique?: boolean | undefined; | ||
}> | undefined; | ||
table?: any; | ||
@@ -1409,0 +1527,0 @@ _setMeta?: ((...args: unknown[]) => unknown) | undefined; |
@@ -43,14 +43,19 @@ import { z } from "zod"; | ||
const fieldsSchema = z.record(fieldSchema); | ||
const readableCollectionSchema = z.object({ | ||
fields: fieldsSchema, | ||
writable: z.literal(false), | ||
table: z.any(), | ||
_setMeta: z.function().optional() | ||
const indexSchema = z.object({ | ||
on: z.string().or(z.array(z.string())), | ||
unique: z.boolean().optional() | ||
}); | ||
const writableCollectionSchema = z.object({ | ||
const indexesSchema = z.record(indexSchema); | ||
const baseCollectionSchema = z.object({ | ||
fields: fieldsSchema, | ||
writable: z.literal(true), | ||
indexes: indexesSchema.optional(), | ||
table: z.any(), | ||
_setMeta: z.function().optional() | ||
}); | ||
const readableCollectionSchema = baseCollectionSchema.extend({ | ||
writable: z.literal(false) | ||
}); | ||
const writableCollectionSchema = baseCollectionSchema.extend({ | ||
writable: z.literal(true) | ||
}); | ||
const collectionSchema = z.union([readableCollectionSchema, writableCollectionSchema]); | ||
@@ -61,4 +66,5 @@ const collectionsSchema = z.record(collectionSchema); | ||
collectionsSchema, | ||
indexSchema, | ||
readableCollectionSchema, | ||
writableCollectionSchema | ||
}; |
@@ -1,2 +0,1 @@ | ||
export declare function findLocalDatabase(localDbURL: string): string; | ||
export declare function createRemoteDatabaseClient(appToken: string, remoteDbURL: string): import("drizzle-orm/sqlite-proxy").SqliteRemoteDatabase<Record<string, never>>; |
import { drizzle } from "drizzle-orm/sqlite-proxy"; | ||
import { z } from "zod"; | ||
import { DB_PATH } from "./consts.js"; | ||
function findLocalDatabase(localDbURL) { | ||
let dbURL = void 0; | ||
if (process.env.VERCEL) { | ||
dbURL = new URL(DB_PATH, "file://" + process.cwd() + "/vercel/path0/"); | ||
} else { | ||
dbURL = new URL(localDbURL); | ||
} | ||
return dbURL.toString(); | ||
} | ||
function createRemoteDatabaseClient(appToken, remoteDbURL) { | ||
@@ -61,4 +51,3 @@ const url = new URL("./db/query/", remoteDbURL); | ||
export { | ||
createRemoteDatabaseClient, | ||
findLocalDatabase | ||
createRemoteDatabaseClient | ||
}; |
@@ -0,1 +1,2 @@ | ||
import type { AstroConfig } from 'astro'; | ||
import type { DBCollections } from './types.js'; | ||
@@ -6,4 +7,4 @@ import type { VitePlugin } from './utils.js'; | ||
collections: DBCollections; | ||
dbUrl: string; | ||
} | { | ||
output: AstroConfig['output']; | ||
connectToStudio: true; | ||
@@ -13,5 +14,4 @@ collections: DBCollections; | ||
}): VitePlugin; | ||
export declare function getVirtualModContents({ collections, dbUrl, }: { | ||
export declare function getVirtualModContents({ collections, }: { | ||
collections: DBCollections; | ||
dbUrl: string; | ||
}): string; | ||
@@ -18,0 +18,0 @@ export declare function getStudioVirtualModContents({ collections, appToken, }: { |
import { DRIZZLE_MOD_IMPORT, INTERNAL_MOD_IMPORT, VIRTUAL_MODULE_ID, DB_PATH } from "./consts.js"; | ||
import fs from "node:fs"; | ||
const resolvedVirtualModuleId = "\0" + VIRTUAL_MODULE_ID; | ||
@@ -20,12 +19,2 @@ function vitePluginDb(params) { | ||
return getVirtualModContents(params); | ||
}, | ||
async buildEnd() { | ||
if ("dbUrl" in params) { | ||
const data = await fs.promises.readFile(new URL(params.dbUrl)); | ||
this.emitFile({ | ||
fileName: "content.db", | ||
source: data, | ||
type: "asset" | ||
}); | ||
} | ||
} | ||
@@ -35,10 +24,8 @@ }; | ||
function getVirtualModContents({ | ||
collections, | ||
dbUrl | ||
collections | ||
}) { | ||
return ` | ||
import { collectionToTable, createLocalDatabaseClient, findLocalDatabase } from ${INTERNAL_MOD_IMPORT}; | ||
import dbUrl from './.astro/content.db?file-url'; | ||
export const dbUrl = findLocalDatabase(${JSON.stringify(dbUrl)}); | ||
const params = ${JSON.stringify({ | ||
@@ -45,0 +32,0 @@ collections, |
{ | ||
"name": "@astrojs/db", | ||
"version": "0.1.16", | ||
"version": "0.1.17", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
188731
62
5160
5