Website Structure

This commit is contained in:
supalerk-ar66 2026-01-13 10:46:40 +07:00
parent 62812f2090
commit 71f0676a62
22365 changed files with 4265753 additions and 791 deletions

View file

@ -0,0 +1,42 @@
import { type Logger, type RelationalSchemaConfig, type Query, type TablesRelationalConfig } from "drizzle-orm";
import { SQLiteAsyncDialect, SQLiteSession, SQLitePreparedQuery } from "drizzle-orm/sqlite-core";
import type { PreparedQueryConfig, SelectedFieldsOrdered, SQLiteExecuteMethod, SQLiteTransactionConfig } from "drizzle-orm/sqlite-core";
import type { Database, Statement } from "db0";
// Used as reference: https://github.com/drizzle-team/drizzle-orm/blob/main/drizzle-orm/src/d1/session.ts
export interface DB0SessionOptions {
logger?: Logger;
}
export declare class DB0Session<
TFullSchema extends Record<string, unknown>,
TSchema extends TablesRelationalConfig
> extends SQLiteSession<"async", unknown, TFullSchema, TSchema> {
private db;
private schema;
private options;
dialect: SQLiteAsyncDialect;
private logger;
constructor(db: Database, dialect: SQLiteAsyncDialect, schema: RelationalSchemaConfig<TSchema> | undefined, options?: DB0SessionOptions);
// @ts-expect-error TODO
prepareQuery(query: Query, fields: SelectedFieldsOrdered | undefined, executeMethod: SQLiteExecuteMethod, customResultMapper?: (rows: unknown[][]) => unknown): DB0PreparedQuery;
// TODO: Implement batch
// TODO: Implement transaction
transaction<T>(transaction: (tx: any) => T | Promise<T>, config?: SQLiteTransactionConfig): Promise<T>;
}
export declare class DB0PreparedQuery<T extends PreparedQueryConfig = PreparedQueryConfig> extends SQLitePreparedQuery<{
type: "async";
run: Awaited<ReturnType<Statement["run"]>>;
all: T["all"];
get: T["get"];
values: T["values"];
execute: T["execute"];
}> {
private stmt;
private logger;
constructor(stmt: Statement, query: Query, logger: Logger, fields: SelectedFieldsOrdered | undefined, executeMethod: SQLiteExecuteMethod, customResultMapper?: (rows: unknown[][]) => unknown);
run(): Promise<{
success: boolean;
}>;
all(): Promise<unknown[]>;
get(): Promise<unknown>;
values(): Promise<unknown[]>;
}

View file

@ -0,0 +1,57 @@
import { NoopLogger } from "drizzle-orm";
import { SQLiteSession, SQLitePreparedQuery } from "drizzle-orm/sqlite-core";
export class DB0Session extends SQLiteSession {
dialect;
logger;
constructor(db, dialect, schema, options = {}) {
super(dialect);
this.db = db;
this.schema = schema;
this.options = options;
this.logger = options.logger ?? new NoopLogger();
}
// @ts-expect-error TODO
prepareQuery(query, fields, executeMethod, customResultMapper) {
const stmt = this.db.prepare(query.sql);
return new DB0PreparedQuery(stmt, query, this.logger, fields, executeMethod, customResultMapper);
}
// TODO: Implement batch
// TODO: Implement transaction
transaction(transaction, config) {
throw new Error("transaction is not implemented!");
// const tx = new D1Transaction('async', this.dialect, this, this.schema);
// await this.run(sql.raw(`begin${config?.behavior ? ' ' + config.behavior : ''}`));
// try {
// const result = await transaction(tx);
// await this.run(sql`commit`);
// return result;
// } catch (err) {
// await this.run(sql`rollback`);
// throw err;
// }
}
}
export class DB0PreparedQuery extends SQLitePreparedQuery {
constructor(stmt, query, logger, fields, executeMethod, customResultMapper) {
super("async", executeMethod, query);
this.stmt = stmt;
this.logger = logger;
}
run() {
return this.stmt.run(...this.query.params);
}
all() {
return this.stmt.all(...this.query.params);
}
get() {
return this.stmt.get(...this.query.params);
}
values() {
return Promise.reject(new Error("values is not implemented!"));
}
}
// Object.defineProperty(DB0PreparedQuery, entityKind, {
// value: "DB0PreparedQuery",
// enumerable: true,
// configurable: true,
// });

View file

@ -0,0 +1 @@
export {};

View file

@ -0,0 +1,48 @@
import { getTableName, is, Column, SQL } from "drizzle-orm";
// Source: https://github.com/drizzle-team/drizzle-orm/blob/main/drizzle-orm/src/utils.ts#L14
/** @internal */
export function mapResultRow(columns, row, joinsNotNullableMap) {
// Key -> nested object key, value -> table name if all fields in the nested object are from the same table, false otherwise
const nullifyMap = {};
// eslint-disable-next-line unicorn/no-array-reduce
const result = columns.reduce((result, { path, field }, columnIndex) => {
let decoder;
if (is(field, Column)) {
decoder = field;
} else if (is(field, SQL)) {
decoder = "decoder" in field && field.decoder;
} else {
decoder = "decoder" in field.sql && field.sql.decoder;
}
let node = result;
for (const [pathChunkIndex, pathChunk] of path.entries()) {
if (pathChunkIndex < path.length - 1) {
if (!(pathChunk in node)) {
node[pathChunk] = {};
}
node = node[pathChunk];
} else {
const rawValue = row[columnIndex];
const value = node[pathChunk] = rawValue === null ? null : decoder.mapFromDriverValue(rawValue);
if (joinsNotNullableMap && is(field, Column) && path.length === 2) {
const objectName = path[0];
if (!(objectName in nullifyMap)) {
nullifyMap[objectName] = value === null ? getTableName(field.table) : false;
} else if (typeof nullifyMap[objectName] === "string" && nullifyMap[objectName] !== getTableName(field.table)) {
nullifyMap[objectName] = false;
}
}
}
}
return result;
}, {});
// Nullify all nested objects from nullifyMap that are nullable
if (joinsNotNullableMap && Object.keys(nullifyMap).length > 0) {
for (const [objectName, tableName] of Object.entries(nullifyMap)) {
if (typeof tableName === "string" && !joinsNotNullableMap[tableName]) {
result[objectName] = null;
}
}
}
return result;
}

View file

@ -0,0 +1,4 @@
import { BaseSQLiteDatabase } from "drizzle-orm/sqlite-core";
import type { Database } from "db0";
export type DrizzleDatabase<TSchema extends Record<string, unknown> = Record<string, never>> = BaseSQLiteDatabase<"async", any, TSchema>;
export declare function drizzle<TSchema extends Record<string, unknown> = Record<string, never>>(db: Database): DrizzleDatabase<TSchema>;

View file

@ -0,0 +1,15 @@
import { BaseSQLiteDatabase, SQLiteAsyncDialect } from "drizzle-orm/sqlite-core";
import { DB0Session } from "./_session.mjs";
export function drizzle(db) {
// TODO: Support schema
const schema = undefined;
const dialect = new SQLiteAsyncDialect();
const session = new DB0Session(db, dialect, schema);
return new BaseSQLiteDatabase(
"async",
dialect,
// @ts-expect-error TODO
session,
schema
);
}