Skip to content

Commit

Permalink
Allow for passing model entities as objects (#52)
Browse files Browse the repository at this point in the history
* Bumped `@ronin/compiler` package to `0.17.1`

* Updated model `indexes` & `triggers` record value types

* Bumped `@ronin/compiler` package to `0.17.2`

* Removed `fields` & `presets` formatting from `getSyntaxProxy`

* Refactored tests model properties from arrays to objects

* Updated model `presets` type to use `ModelPreset['instructions']`
  • Loading branch information
NuroDev authored Feb 13, 2025
1 parent afb645e commit 0b92b1c
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 220 deletions.
Binary file modified bun.lockb
Binary file not shown.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"license": "Apache-2.0",
"devDependencies": {
"@biomejs/biome": "1.9.4",
"@ronin/compiler": "0.17.0",
"@ronin/compiler": "0.17.2",
"@types/bun": "1.2.1",
"tsup": "8.3.6",
"typescript": "5.7.3"
Expand Down
50 changes: 1 addition & 49 deletions src/queries/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { DeepCallable, ResultRecord } from '@/src/queries/types';
import type { Model } from '@/src/schema';
import { isPlainObject, mutateStructure, setProperty } from '@/src/utils';
import { type ModelField, QUERY_SYMBOLS, type Query } from '@ronin/compiler';
import { QUERY_SYMBOLS, type Query } from '@ronin/compiler';

/**
* Utility type to convert a tuple of promises into a tuple of their resolved types.
Expand Down Expand Up @@ -102,53 +101,6 @@ export const getSyntaxProxy = <Structure, ReturnValue = ResultRecord>(config?: {

setProperty(structure, pathJoined, value);

const isModelQuery = config?.root === `${QUERY_SYMBOLS.QUERY}.create`;
const modelQueryValue = (structure as any)?.[QUERY_SYMBOLS.QUERY]?.create?.model;

// If a `create.model` query was provided or a `model` is being constructed,
// serialize the model structure.
if ((isModelQuery && modelQueryValue) || config?.modelType) {
const createdModel = isModelQuery ? modelQueryValue : structure;
const newModel = { ...createdModel };

if (newModel.fields) {
const formatFields = (
fields: Record<string, ModelField>,
parent?: string,
): Array<ModelField> => {
return Object.entries(fields).flatMap(([slug, rest]) => {
if (rest.type) {
return [
{ slug: parent ? `${parent}.${slug}` : slug, ...(rest as object) },
];
}

return formatFields(rest as unknown as Record<string, ModelField>, slug);
});
};

newModel.fields = formatFields(newModel.fields);
}

if (newModel.presets) {
newModel.presets = Object.entries(newModel.presets).map(
([slug, instructions]) => ({
slug,
instructions,
}),
);
}

if (isModelQuery) {
(structure as any)[QUERY_SYMBOLS.QUERY].create.model = newModel;
} else {
const model = structure as Model;

if (newModel.fields) model.fields = newModel.fields;
if (newModel.presets) model.presets = newModel.presets;
}
}

// If the function call is happening inside a batch, return a new proxy, to
// allow for continuing to chain `get` accessors and function calls after
// existing function calls in the same query.
Expand Down
4 changes: 2 additions & 2 deletions src/schema/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,12 @@ export interface Model<Fields = RecordWithoutForbiddenKeys<Primitives>>
/**
* Database indexes to optimize query performance.
*/
indexes?: Record<string, ModelIndex<Array<ModelField & { slug: keyof Fields }>>>;
indexes?: Record<string, Omit<ModelIndex<Record<keyof Fields, ModelField>>, 'slug'>>;

/**
* Queries that run automatically in response to other queries.
*/
triggers?: Record<string, ModelTrigger<Array<ModelField & { slug: keyof Fields }>>>;
triggers?: Record<string, Omit<ModelTrigger<Record<keyof Fields, ModelField>>, 'slug'>>;

/**
* Predefined query instructions that can be reused across multiple different queries.
Expand Down
Loading

0 comments on commit 0b92b1c

Please sign in to comment.