Skip to content

Commit

Permalink
Dynamically import compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
leo committed Jan 24, 2025
1 parent 8a4fdfc commit aaf6d39
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 12 deletions.
4 changes: 3 additions & 1 deletion src/utils/database.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import fs from 'node:fs';
import { ROOT_MODEL, Transaction } from '@ronin/compiler';
import { getPackage } from '@/src/utils/misc';
import { type Database, Engine } from '@ronin/engine';
import { MemoryResolver } from '@ronin/engine/resolvers/memory';

export const initializeDatabase = async (
fsPath = '.ronin/db.sqlite',
): Promise<Database> => {
const { Transaction, ROOT_MODEL } = await getPackage('compiler');

const engine = new Engine({
resolvers: [(engine) => new MemoryResolver(engine)],
});
Expand Down
11 changes: 8 additions & 3 deletions src/utils/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { parseArgs } from 'node:util';
import { fieldsToCreate, fieldsToDrop } from '@/src/utils/field';
import { spinner } from '@/src/utils/spinner';
import type { Model, Result } from '@ronin/compiler';
import type * as CompilerPackage from '@ronin/compiler';
import type * as SyntaxPackage from '@ronin/syntax/queries';
import resolveFrom from 'resolve-from';

Expand Down Expand Up @@ -356,12 +357,16 @@ export const getResponseBody = async <T>(
};

/**
* Retrieves an instance of the RONIN syntax package.
* Retrieves an instance of a RONIN package.
*
* @returns An instance of the package.
*/
export const getSyntaxPackage = (): Promise<typeof SyntaxPackage> => {
const roninSyntaxPath = resolveFrom.silent(process.cwd(), '@ronin/syntax/queries');
export const getPackage = <Name extends 'syntax/queries' | 'compiler'>(
name: Name,
): Promise<
Name extends 'syntax/queries' ? typeof SyntaxPackage : typeof CompilerPackage
> => {
const roninSyntaxPath = resolveFrom.silent(process.cwd(), `@ronin/${name}`);

if (!roninSyntaxPath) {
throw new Error(
Expand Down
4 changes: 2 additions & 2 deletions src/utils/model.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { IGNORED_FIELDS } from '@/src/utils/migration';
import { type QueryResponse, getResponseBody } from '@/src/utils/misc';
import { type QueryResponse, getPackage, getResponseBody } from '@/src/utils/misc';
import type { Model } from '@ronin/compiler';
import { Transaction } from '@ronin/compiler';
import type { Database } from '@ronin/engine';
import type { Row } from '@ronin/engine/types';

Expand All @@ -23,6 +22,7 @@ export const getModels = async (
spaceId?: string,
isLocal = true,
): Promise<Array<Model>> => {
const { Transaction } = await getPackage('compiler');
const transaction = new Transaction([{ get: { models: null } }]);

let rawResults: Array<Array<Row>>;
Expand Down
14 changes: 8 additions & 6 deletions src/utils/protocol.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import fs from 'node:fs';
import path from 'node:path';
import { formatCode } from '@/src/utils/format';
import { getSyntaxPackage } from '@/src/utils/misc';
import { type Model, type Query, type Statement, Transaction } from '@ronin/compiler';
import { getPackage } from '@/src/utils/misc';
import type { Model, Query, Statement } from '@ronin/compiler';

/**
* Protocol represents a set of database migration queries that can be executed in sequence.
Expand All @@ -28,7 +28,7 @@ export class Protocol {
* @returns The Protocol instance for chaining.
*/
async convertToQueryObjects(): Promise<Protocol> {
const roninSyntax = await getSyntaxPackage();
const roninSyntax = await getPackage('syntax/queries');

this._queries = this._roninQueries.map((queryString) => {
return this.queryToObject(roninSyntax, queryString);
Expand All @@ -47,7 +47,7 @@ export class Protocol {
* @private
*/
private queryToObject = (
roninSyntax: Awaited<ReturnType<typeof getSyntaxPackage>>,
roninSyntax: Awaited<ReturnType<typeof getPackage<'syntax/queries'>>>,
query: string,
): Query => {
const { getSyntaxProxy } = roninSyntax;
Expand Down Expand Up @@ -117,7 +117,7 @@ export default () => [

const queries = await import(filePath);

const roninSyntax = await getSyntaxPackage();
const roninSyntax = await getPackage('syntax/queries');
const { getBatchProxy } = roninSyntax;
const queryObjects = getBatchProxy(() => queries.default());

Expand Down Expand Up @@ -164,7 +164,9 @@ export default () => [
*
* @returns Array of SQL statements.
*/
getSQLStatements = (models: Array<Model>): Array<Statement> => {
getSQLStatements = async (models: Array<Model>): Promise<Array<Statement>> => {
const { Transaction } = await getPackage('compiler');

return new Transaction(this._queries, {
models,
inlineParams: true,
Expand Down

0 comments on commit aaf6d39

Please sign in to comment.