Skip to content

Commit

Permalink
Allow to process fields, indexes and triggers as objects (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
colodenn authored Feb 20, 2025
1 parent 4911f9d commit 8abd94f
Show file tree
Hide file tree
Showing 16 changed files with 489 additions and 522 deletions.
80 changes: 39 additions & 41 deletions bun.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"resolve-from": "5.0.0"
},
"devDependencies": {
"ronin": "6.0.27",
"ronin": "6.2.16",
"@biomejs/biome": "1.9.4",
"@types/bun": "1.2.1",
"@types/ini": "4.1.1",
Expand Down
66 changes: 45 additions & 21 deletions src/utils/field.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { MigrationOptions } from '@/src/utils/migration';
import { RONIN_SCHEMA_TEMP_SUFFIX } from '@/src/utils/misc';
import { convertArrayToObject } from '@/src/utils/model';
import {
createFieldQuery,
createTempColumnQuery,
Expand Down Expand Up @@ -118,13 +119,16 @@ export const diffFields = async (
if (field.from.type === 'link') {
diff.push(
...createTempModelQuery(
modelSlug,
[
{ ...field.to, slug: field.from.slug },
...definedFields.filter((local) => local.slug !== field.to.slug),
],
indexes,
triggers,
{
slug: modelSlug,
// @ts-expect-error This will work once the types are fixed.
fields: convertArrayToObject([
{ ...field.to, slug: field.from.slug },
...definedFields.filter((local) => local.slug !== field.to.slug),
]),
indexes: convertArrayToObject(indexes),
triggers: convertArrayToObject(triggers),
},
[
renameFieldQuery(
`${RONIN_SCHEMA_TEMP_SUFFIX}${modelSlug}`,
Expand Down Expand Up @@ -176,10 +180,13 @@ export const diffFields = async (

diff.push(
...createTempModelQuery(
modelSlug,
updatedFields || [],
[],
[],
{
slug: modelSlug,
// @ts-expect-error This will work once the types are fixed.
fields: convertArrayToObject(updatedFields || []),
indexes: convertArrayToObject(indexes),
triggers: convertArrayToObject(triggers),
},
queries,
existingFields,
),
Expand Down Expand Up @@ -284,7 +291,14 @@ const adjustFields = (
indexes: Array<ModelIndex>,
triggers: Array<ModelTrigger>,
): Array<string> => {
return createTempModelQuery(modelSlug, fields, indexes, triggers);
return createTempModelQuery({
slug: modelSlug,
// @ts-expect-error This will work once the types are fixed.
fields: convertArrayToObject(fields),
// @ts-expect-error This will work once the types are fixed.
indexes,
triggers: convertArrayToObject(triggers),
});
};

/**
Expand Down Expand Up @@ -338,20 +352,22 @@ export const createFields = async (
);

return createTempModelQuery(
modelSlug,
updatedFields || [],
[],
[],
{
slug: modelSlug,
// @ts-expect-error This will work once the types are fixed.
fields: convertArrayToObject(updatedFields || []),
},
queries,
existingFields,
);
}

return createTempModelQuery(
modelSlug,
definedFields || [],
[],
[],
{
slug: modelSlug,
// @ts-expect-error This will work once the types are fixed.
fields: convertArrayToObject(definedFields || []),
},
[],
existingFields,
);
Expand Down Expand Up @@ -419,7 +435,15 @@ const deleteFields = (
const diff: Array<string> = [];
for (const fieldToDrop of fieldsToDrop) {
if (fieldToDrop.unique) {
return createTempModelQuery(modelSlug, fields, [], [], [], fields);
return createTempModelQuery(
{
slug: modelSlug,
// @ts-expect-error This will work once the types are fixed.
fields: convertArrayToObject(fields),
},
[],
fields,
);
}
diff.push(dropFieldQuery(modelSlug, fieldToDrop.slug));
}
Expand Down
Loading

0 comments on commit 8abd94f

Please sign in to comment.