Skip to content

Commit

Permalink
Always require slug for model entities (#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
leo authored Feb 13, 2025
1 parent 072480b commit a52bef5
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 10 deletions.
5 changes: 3 additions & 2 deletions src/instructions/using.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Model, ModelPreset } from '@/src/types/model';
import type { Model, ModelField, ModelPreset } from '@/src/types/model';
import type { Instructions, SetInstructions } from '@/src/types/query';
import { QUERY_SYMBOLS, RoninError, findInObject, isObject } from '@/src/utils/helpers';

Expand All @@ -25,7 +25,8 @@ export const handleUsing = (
// If a preset with the slug `links` is being requested, add the presets of all link
// fields separately.
if ('links' in normalizedUsing) {
for (const [fieldSlug, field] of Object.entries(model.fields)) {
for (const [fieldSlug, rest] of Object.entries(model.fields)) {
const field = { slug: fieldSlug, ...rest } as ModelField;
if (field.type !== 'link' || field.kind === 'many') continue;
normalizedUsing[fieldSlug] = null;
}
Expand Down
3 changes: 2 additions & 1 deletion src/model/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,8 @@ export const addDefaultModelPresets = (list: Array<Model>, model: Model): Model
// Do not assign default presets for associative models.
if (subModel.system?.associationSlug) return null;

const field = Object.entries(subModel.fields).find(([_, field]) => {
const field = Object.entries(subModel.fields).find(([fieldSlug, rest]) => {
const field = { slug: fieldSlug, ...rest } as ModelField;
return field.type === 'link' && field.target === model.slug;
});

Expand Down
5 changes: 0 additions & 5 deletions src/model/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,30 +218,25 @@ export const getSystemFields = (idPrefix: Model['idPrefix']): Model['fields'] =>
id: {
name: 'ID',
type: 'string',
slug: 'id',
defaultValue: ID_EXPRESSION(idPrefix),
},
'ronin.createdAt': {
name: 'RONIN - Created At',
type: 'date',
slug: 'ronin.createdAt',
defaultValue: CURRENT_TIME_EXPRESSION,
},
'ronin.createdBy': {
name: 'RONIN - Created By',
type: 'string',
slug: 'ronin.createdBy',
},
'ronin.updatedAt': {
name: 'RONIN - Updated At',
type: 'date',
slug: 'ronin.updatedAt',
defaultValue: CURRENT_TIME_EXPRESSION,
},
'ronin.updatedBy': {
name: 'RONIN - Updated By',
type: 'string',
slug: 'ronin.updatedBy',
},
});

Expand Down
2 changes: 1 addition & 1 deletion src/types/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ export type ModelEntity = ModelField | ModelIndex | ModelTrigger | ModelPreset;

export type ModelEntityList<T extends { slug: string }> = Record<
NonNullable<T['slug']>,
Partial<T>
T extends infer U ? Omit<U, 'slug'> : never
>;

export interface Model<
Expand Down
2 changes: 1 addition & 1 deletion tests/options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ test('inline statement parameters containing serialized expression', async () =>
params: [],
},
{
statement: `INSERT INTO "ronin_schema" ("slug", "fields", "id", "pluralSlug", "name", "pluralName", "idPrefix", "table", "identifiers.name", "identifiers.slug") VALUES ('account', json('{"id":{"name":"ID","type":"string","slug":"id","defaultValue":{"__RONIN_EXPRESSION":"''acc_'' || lower(substr(hex(randomblob(12)), 1, 16))"}},"ronin.createdAt":{"name":"RONIN - Created At","type":"date","slug":"ronin.createdAt","defaultValue":{"__RONIN_EXPRESSION":"strftime(''%Y-%m-%dT%H:%M:%f'', ''now'') || ''Z''"}},"ronin.createdBy":{"name":"RONIN - Created By","type":"string","slug":"ronin.createdBy"},"ronin.updatedAt":{"name":"RONIN - Updated At","type":"date","slug":"ronin.updatedAt","defaultValue":{"__RONIN_EXPRESSION":"strftime(''%Y-%m-%dT%H:%M:%f'', ''now'') || ''Z''"}},"ronin.updatedBy":{"name":"RONIN - Updated By","type":"string","slug":"ronin.updatedBy"},"activeAt":{"name":"Active At","type":"date","defaultValue":{"__RONIN_EXPRESSION":"strftime(''%Y-%m-%dT%H:%M:%f'', ''now'') || ''Z''"}}}'), 'mod_1f052f8432bc861b', 'accounts', 'Account', 'Accounts', 'acc', 'accounts', 'id', 'id') RETURNING "id", "ronin.createdAt", "ronin.createdBy", "ronin.updatedAt", "ronin.updatedBy", "name", "pluralName", "slug", "pluralSlug", "idPrefix", "table", "identifiers.name", "identifiers.slug", "fields", "indexes", "triggers", "presets"`,
statement: `INSERT INTO "ronin_schema" ("slug", "fields", "id", "pluralSlug", "name", "pluralName", "idPrefix", "table", "identifiers.name", "identifiers.slug") VALUES ('account', json('{"id":{"name":"ID","type":"string","defaultValue":{"__RONIN_EXPRESSION":"''acc_'' || lower(substr(hex(randomblob(12)), 1, 16))"}},"ronin.createdAt":{"name":"RONIN - Created At","type":"date","defaultValue":{"__RONIN_EXPRESSION":"strftime(''%Y-%m-%dT%H:%M:%f'', ''now'') || ''Z''"}},"ronin.createdBy":{"name":"RONIN - Created By","type":"string"},"ronin.updatedAt":{"name":"RONIN - Updated At","type":"date","defaultValue":{"__RONIN_EXPRESSION":"strftime(''%Y-%m-%dT%H:%M:%f'', ''now'') || ''Z''"}},"ronin.updatedBy":{"name":"RONIN - Updated By","type":"string"},"activeAt":{"name":"Active At","type":"date","defaultValue":{"__RONIN_EXPRESSION":"strftime(''%Y-%m-%dT%H:%M:%f'', ''now'') || ''Z''"}}}'), 'mod_1f052f8432bc861b', 'accounts', 'Account', 'Accounts', 'acc', 'accounts', 'id', 'id') RETURNING "id", "ronin.createdAt", "ronin.createdBy", "ronin.updatedAt", "ronin.updatedBy", "name", "pluralName", "slug", "pluralSlug", "idPrefix", "table", "identifiers.name", "identifiers.slug", "fields", "indexes", "triggers", "presets"`,
params: [],
returning: true,
},
Expand Down

0 comments on commit a52bef5

Please sign in to comment.