Initial commit - Event Planner application

This commit is contained in:
mberlin
2026-03-18 14:55:56 -03:00
commit 86d779eb4d
7548 changed files with 1006324 additions and 0 deletions

View File

@@ -0,0 +1,107 @@
import {
type AliasNode,
CompiledQuery,
type DatabaseConnection,
type DatabaseIntrospector,
DefaultQueryCompiler,
type Dialect,
DialectAdapterBase,
type Driver,
type Kysely,
type QueryCompiler,
type QueryResult,
type TransactionSettings,
} from 'kysely';
/**
* Subset of oracledb's Pool interface used by the dialect.
* We define our own interface to avoid importing the `oracledb` package directly.
*/
export interface OraclePool {
getConnection(): Promise<OraclePoolConnection>;
close(drainTime?: number): Promise<void>;
}
/**
* Subset of oracledb's Connection interface used by the dialect.
*/
export interface OraclePoolConnection {
execute<R>(
sql: string,
params: unknown[],
options?: Record<string, unknown>,
): Promise<{
rows?: R[];
rowsAffected?: number;
resultSet?: OracleResultSet<R>;
outBinds?: unknown;
}>;
commit(): Promise<void>;
rollback(): Promise<void>;
close(): Promise<void>;
}
interface OracleResultSet<R> {
getRow(): Promise<R>;
close(): Promise<void>;
}
declare class OracleQueryCompiler extends DefaultQueryCompiler {
protected getLeftIdentifierWrapper(): string;
protected getRightIdentifierWrapper(): string;
protected visitAlias(node: AliasNode): void;
}
declare class OracleAdapter extends DialectAdapterBase {
#private;
get supportsReturning(): boolean;
get supportsTransactionalDdl(): boolean;
acquireMigrationLock(_: Kysely<any>): Promise<void>;
releaseMigrationLock(_: Kysely<any>): Promise<void>;
}
declare class OracleConnection implements DatabaseConnection {
#private;
readonly id: number;
constructor(connection: OraclePoolConnection, executeOptions?: Record<string, unknown>);
executeQuery<R>(compiledQuery: CompiledQuery): Promise<QueryResult<R>>;
formatQuery(query: CompiledQuery): {
sql: string;
bindParams: unknown[];
};
streamQuery<R>(compiledQuery: CompiledQuery, _chunkSize?: number): AsyncIterableIterator<QueryResult<R>>;
get connection(): OraclePoolConnection;
}
declare class OracleDriver implements Driver {
#private;
constructor(config: OracleDialectConfig);
init(): Promise<void>;
acquireConnection(): Promise<OracleConnection>;
savepoint(
connection: OracleConnection,
savepointName: string,
compileQuery: QueryCompiler['compileQuery'],
): Promise<void>;
rollbackToSavepoint(
connection: OracleConnection,
savepointName: string,
compileQuery: QueryCompiler['compileQuery'],
): Promise<void>;
releaseSavepoint(
connection: OracleConnection,
savepointName: string,
compileQuery: QueryCompiler['compileQuery'],
): Promise<void>;
beginTransaction(connection: OracleConnection, settings: TransactionSettings): Promise<void>;
commitTransaction(connection: OracleConnection): Promise<void>;
rollbackTransaction(connection: OracleConnection): Promise<void>;
releaseConnection(connection: OracleConnection): Promise<void>;
destroy(): Promise<void>;
}
export interface OracleDialectConfig {
pool: OraclePool;
executeOptions?: Record<string, unknown>;
}
export declare class OracleDialect implements Dialect {
#private;
constructor(config: OracleDialectConfig);
createDriver(): OracleDriver;
createAdapter(): OracleAdapter;
createIntrospector(db: Kysely<any>): DatabaseIntrospector;
createQueryCompiler(): OracleQueryCompiler;
}
export {};

View File

@@ -0,0 +1,172 @@
// inlined https://github.com/griffiths-waite/kysely-oracledb with minor adjustments
/* v8 ignore start: internal Kysely driver integration, tested through the main Oracle driver */
import {
CompiledQuery,
createQueryId,
DefaultQueryCompiler,
DialectAdapterBase,
IdentifierNode,
RawNode,
} from 'kysely';
function parseSavepointCommand(command, savepointName) {
return RawNode.createWithChildren([
RawNode.createWithSql(`${command} `),
IdentifierNode.create(savepointName), // ensures savepointName gets sanitized
]);
}
class OracleQueryCompiler extends DefaultQueryCompiler {
getLeftIdentifierWrapper() {
return '';
}
getRightIdentifierWrapper() {
return '';
}
visitAlias(node) {
this.visitNode(node.node);
this.append(' ');
this.visitNode(node.alias);
}
}
class OracleAdapter extends DialectAdapterBase {
#supportsReturning = false;
#supportsTransactionalDdl = false;
get supportsReturning() {
return this.#supportsReturning;
}
get supportsTransactionalDdl() {
return this.#supportsTransactionalDdl;
}
async acquireMigrationLock(_) {
throw new Error('Not implemented');
}
async releaseMigrationLock(_) {
throw new Error('Not implemented');
}
}
const OUT_FORMAT_OBJECT = 4002;
let i = 0;
class OracleConnection {
id = i++;
#executeOptions;
#connection;
constructor(connection, executeOptions) {
this.#executeOptions = executeOptions ?? {};
this.#connection = connection;
}
async executeQuery(compiledQuery) {
const { sql, bindParams } = this.formatQuery(compiledQuery);
const result = await this.#connection.execute(sql, bindParams, {
autoCommit: compiledQuery.autoCommit,
outFormat: OUT_FORMAT_OBJECT,
...this.#executeOptions,
});
return {
rows: result?.rows || [],
numAffectedRows: result.rowsAffected ? BigInt(result.rowsAffected) : undefined,
// @ts-ignore internal extension for Oracle returning clause
outBinds: result.outBinds,
};
}
formatQuery(query) {
return {
sql: query.sql.replace(/\$(\d+)/g, (_match, p1) => `:${parseInt(p1, 10) - 1}`), // Format bind params in Oracle syntax :0, :1, etc.
bindParams: query.parameters,
};
}
async *streamQuery(compiledQuery, _chunkSize) {
const { sql, bindParams } = this.formatQuery(compiledQuery);
const result = await this.#connection.execute(sql, bindParams, {
resultSet: true,
autoCommit: compiledQuery.autoCommit,
outFormat: OUT_FORMAT_OBJECT,
...this.#executeOptions,
});
const rs = result.resultSet;
try {
let row;
while ((row = await rs.getRow())) {
yield { rows: [row] };
}
} finally {
await rs.close();
}
}
get connection() {
return this.#connection;
}
}
class OracleDriver {
#config;
#connections = new Set();
constructor(config) {
this.#config = config;
}
async init() {
//
}
async acquireConnection() {
const connection = new OracleConnection(await this.#config.pool.getConnection(), this.#config.executeOptions);
this.#connections.add(connection);
return connection;
}
async savepoint(connection, savepointName, compileQuery) {
await connection.executeQuery(compileQuery(parseSavepointCommand('savepoint', savepointName), createQueryId()));
}
async rollbackToSavepoint(connection, savepointName, compileQuery) {
await connection.executeQuery(
compileQuery(parseSavepointCommand('rollback to savepoint', savepointName), createQueryId()),
);
}
async releaseSavepoint(connection, savepointName, compileQuery) {
//
}
async beginTransaction(connection, settings) {
if (settings.accessMode) {
await connection.executeQuery(CompiledQuery.raw(`set transaction ${settings.accessMode}`));
return;
}
if (settings.isolationLevel) {
await connection.executeQuery(CompiledQuery.raw(`set transaction isolation level ${settings.isolationLevel}`));
}
}
async commitTransaction(connection) {
await connection.connection.commit();
}
async rollbackTransaction(connection) {
await connection.connection.rollback();
}
async releaseConnection(connection) {
try {
await connection.connection.close();
} catch (err) {
//
} finally {
this.#connections.delete(connection);
}
}
async destroy() {
for (const connection of this.#connections) {
await this.releaseConnection(connection);
}
await this.#config.pool?.close(0);
}
}
export class OracleDialect {
#config;
constructor(config) {
this.#config = config;
}
createDriver() {
return new OracleDriver(this.#config);
}
createAdapter() {
return new OracleAdapter();
}
createIntrospector(db) {
throw new Error('Not implemented');
}
createQueryCompiler() {
return new OracleQueryCompiler();
}
}
/* v8 ignore stop */

View File

@@ -0,0 +1,19 @@
import { type Dictionary } from '@mikro-orm/core';
import { NativeQueryBuilder } from '../../query/NativeQueryBuilder.js';
/** @internal */
export declare function markOutBindings(obj: Dictionary): void;
/** @internal */
export declare class OracleNativeQueryBuilder extends NativeQueryBuilder {
as(alias: string): this;
compile(): {
sql: string;
params: unknown[];
};
protected compileTruncate(): void;
protected combineParts(): {
sql: string;
params: unknown[];
};
private compileUpsert;
protected compileSelect(): void;
}

View File

@@ -0,0 +1,245 @@
import { raw, RawQueryFragment, Utils } from '@mikro-orm/core';
import { QueryType } from '../../query/enums.js';
import { NativeQueryBuilder } from '../../query/NativeQueryBuilder.js';
/** @internal */
export function markOutBindings(obj) {
Object.defineProperty(obj, '__outBindings', {
value: true,
writable: true,
configurable: true,
enumerable: false,
});
}
/** @internal */
export class OracleNativeQueryBuilder extends NativeQueryBuilder {
as(alias) {
this.wrap('(', `) ${this.platform.quoteIdentifier(alias)}`);
return this;
}
compile() {
if (!this.type) {
throw new Error('No query type provided');
}
this.parts.length = 0;
this.params.length = 0;
/* v8 ignore next 3: query comment branch */
if (this.options.comment) {
this.parts.push(...this.options.comment.map(comment => `/* ${comment} */`));
}
let copy;
if (this.options.onConflict && !Utils.isEmpty(Utils.asArray(this.options.data)[0])) {
this.compileUpsert();
} else {
if (this.options.returning && Array.isArray(this.options.data) && this.options.data.length > 1) {
copy = [...this.options.data];
this.options.data.length = 1;
}
switch (this.type) {
case QueryType.SELECT:
case QueryType.COUNT:
this.compileSelect();
break;
case QueryType.INSERT:
this.compileInsert();
break;
case QueryType.UPDATE:
this.compileUpdate();
break;
case QueryType.DELETE:
this.compileDelete();
break;
case QueryType.TRUNCATE:
this.compileTruncate();
break;
}
this.addOnConflictClause();
}
if (this.options.returning) {
const isUpsert = this.options.onConflict && !Utils.isEmpty(Utils.asArray(this.options.data)[0]);
const prefix = isUpsert ? `${this.getTableName()}.` : '';
const fields = this.options.returning.map(field => prefix + this.quote(Array.isArray(field) ? field[0] : field));
const into = this.options.returning.map(field => ':out_' + (Array.isArray(field) ? field[0] : field));
const outBindings = this.options.returning.map(field => {
const name = 'out_' + (Array.isArray(field) ? field[0] : field);
const type = Array.isArray(field) ? field[1] : 'string';
return [name, type];
});
markOutBindings(outBindings);
this.parts.push(`returning ${fields.join(', ')}`);
this.parts.push(`into ${into.join(', ')}`);
this.params.push(outBindings);
}
this.addLockClause();
if (!copy) {
return this.combineParts();
}
// multi insert with returning
const sql = this.parts.join(' ');
const blockLines = [];
const block2Lines = [];
const keys = Object.keys(copy[0]);
const last = this.params[this.params.length - 1];
/* v8 ignore next 3: defensive check — output bindings are always set by compile() */
if (!Array.isArray(last) || !('__outBindings' in last) || !last.__outBindings) {
throw new Error('Output bindings are required for multi insert with returning');
}
const outBindings = {};
markOutBindings(outBindings);
for (let i = 0; i < copy.length; i++) {
const params = [];
for (const key of keys) {
/* v8 ignore next 3: undefined value branch in multi-insert */
if (typeof copy[i][key] === 'undefined') {
params.push(this.platform.usesDefaultKeyword() ? raw('default') : null);
} else {
params.push(copy[i][key]);
}
}
// we need to interpolate to allow proper escaping
const formatted = this.platform.formatQuery(sql, params).replaceAll(`'`, `''`);
/* v8 ignore next 3: returning field type branches */
const using = this.options.returning.map(field => {
const name = Array.isArray(field) ? field[0] : field;
const type = Array.isArray(field) ? field[1] : 'string';
outBindings[`out_${name}__${i}`] = {
dir: this.platform.mapToBindType('out'),
type: this.platform.mapToBindType(type),
};
return `out :out_${name}__${i}`;
});
blockLines.push(` execute immediate '${formatted}' using ${using.join(', ')};`);
block2Lines.push(` execute immediate '${sql}' using ${using.join(', ')};`);
}
const block = `begin\n${blockLines.join('\n')}\n end;`;
const block2 = `begin\n${block2Lines.join('\n')}\n end;`;
// save raw query without interpolation for logging,
Object.defineProperty(outBindings, '__rawQuery', {
value: block2,
writable: true,
configurable: true,
enumerable: false,
});
this.options.data = copy;
return { sql: block, params: [outBindings] };
}
compileTruncate() {
super.compileTruncate();
this.parts.push('drop all storage cascade');
}
combineParts() {
let sql = this.parts.join(' ');
const last = this.params[this.params.length - 1];
if (this.options.wrap) {
const [a, b] = this.options.wrap;
sql = `${a}${sql}${b}`;
}
if (!(Array.isArray(last) && '__outBindings' in last && last.__outBindings)) {
return { sql, params: this.params };
}
const out = this.params.pop();
const outBindings = {};
markOutBindings(outBindings);
this.params.push(outBindings);
for (const item of out) {
outBindings[item[0]] = {
dir: this.platform.mapToBindType('out'),
type: this.platform.mapToBindType(item[1]),
};
}
return { sql, params: this.params };
}
compileUpsert() {
const clause = this.options.onConflict;
const dataAsArray = Utils.asArray(this.options.data);
const keys = Object.keys(dataAsArray[0]);
const parts = [];
for (const data of dataAsArray) {
for (const key of keys) {
this.params.push(data[key]);
}
parts.push(`select ${keys.map(k => `? as ${this.quote(k)}`).join(', ')} from dual`);
}
this.parts.push(`merge into ${this.getTableName()}`);
this.parts.push(`using (${parts.join(' union all ')}) tsource`);
/* v8 ignore next 4: RawQueryFragment conflict fields branch */
if (clause.fields instanceof RawQueryFragment) {
this.parts.push(clause.fields.sql);
this.params.push(...clause.fields.params);
} else if (clause.fields.length > 0) {
const fields = clause.fields.map(field => {
const col = this.quote(field);
return `${this.getTableName()}.${col} = tsource.${col}`;
});
this.parts.push(`on (${fields.join(' and ')})`);
}
const sourceColumns = keys.map(field => `tsource.${this.quote(field)}`).join(', ');
const destinationColumns = keys.map(field => this.quote(field)).join(', ');
this.parts.push(`when not matched then insert (${destinationColumns}) values (${sourceColumns})`);
if (!clause.ignore) {
/* v8 ignore next: merge type branch */
if (!clause.merge || Array.isArray(clause.merge)) {
const mergeParts = (clause.merge || keys)
.filter(field => !Array.isArray(clause.fields) || !clause.fields.includes(field))
.filter(field => keys.includes(field)) // only reference columns present in the source data
.map(column => `${this.quote(column)} = tsource.${this.quote(column)}`);
/* v8 ignore next 10: empty mergeParts branch */
if (mergeParts.length > 0) {
this.parts.push('when matched');
if (clause.where) {
this.parts.push(`and ${clause.where.sql}`);
this.params.push(...clause.where.params);
}
this.parts.push('then update set');
this.parts.push(mergeParts.join(', '));
}
} /* v8 ignore start: object-form merge branch */ else if (typeof clause.merge === 'object') {
this.parts.push('when matched');
if (clause.where) {
this.parts.push(`and ${clause.where.sql}`);
this.params.push(...clause.where.params);
}
this.parts.push('then update set');
const parts = Object.entries(clause.merge).map(([key, value]) => {
this.params.push(value);
return `${this.getTableName()}.${this.quote(key)} = ?`;
});
this.parts.push(parts.join(', '));
}
/* v8 ignore stop */
}
}
compileSelect() {
this.parts.push('select');
this.addHintComment();
this.parts.push(`${this.getFields()} from ${this.getTableName()}`);
if (this.options.joins) {
for (const join of this.options.joins) {
this.parts.push(join.sql);
this.params.push(...join.params);
}
}
if (this.options.where?.sql.trim()) {
this.parts.push(`where ${this.options.where.sql}`);
this.params.push(...this.options.where.params);
}
if (this.options.groupBy) {
const fields = this.options.groupBy.map(field => this.quote(field));
this.parts.push(`group by ${fields.join(', ')}`);
}
if (this.options.having) {
this.parts.push(`having ${this.options.having.sql}`);
this.params.push(...this.options.having.params);
}
if (this.options.orderBy) {
this.parts.push(`order by ${this.options.orderBy}`);
}
if (this.options.offset != null) {
this.parts.push(`offset ? rows`);
this.params.push(this.options.offset);
}
if (this.options.limit != null) {
this.parts.push(`fetch next ? rows only`);
this.params.push(this.options.limit);
}
}
}

View File

@@ -0,0 +1,2 @@
export * from './OracleDialect.js';
export * from './OracleNativeQueryBuilder.js';

View File

@@ -0,0 +1,2 @@
export * from './OracleDialect.js';
export * from './OracleNativeQueryBuilder.js';