Skip to content

Commit

Permalink
Allow for passing field expressions to indexes (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
leo authored Nov 11, 2024
1 parent 6beeba9 commit 624ac6f
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 6 deletions.
8 changes: 8 additions & 0 deletions src/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ export const RONIN_SCHEMA_SYMBOLS = {
VALUE: '__RONIN_VALUE',
} as const;

/**
* A regular expression for matching the symbol that represents a field of a schema.
*/
export const RONIN_SCHEMA_FIELD_REGEX = new RegExp(
`${RONIN_SCHEMA_SYMBOLS.FIELD}[a-zA-Z0-9]+`,
'g',
);

type RoninErrorCode =
| 'SCHEMA_NOT_FOUND'
| 'FIELD_NOT_FOUND'
Expand Down
20 changes: 15 additions & 5 deletions src/utils/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import type {
SchemaTriggerField,
} from '@/src/types/schema';
import {
RONIN_SCHEMA_FIELD_REGEX,
RONIN_SCHEMA_SYMBOLS,
RoninError,
convertToCamelCase,
Expand Down Expand Up @@ -771,15 +772,24 @@ export const addSchemaQueries = (
let statement = `${tableAction}${unique ? ' UNIQUE' : ''} INDEX "${indexName}"`;

if (queryType === 'create') {
const schema = targetSchema as Schema;
const columns = fields.map((field) => {
let fieldSelector = '';

// If the slug of a field is provided, find the field in the schema, obtain its
// column selector, and place it in the SQL statement.
if ('slug' in field) {
({ fieldSelector } = getFieldFromSchema(
targetSchema as Schema,
field.slug,
'to',
));
({ fieldSelector } = getFieldFromSchema(schema, field.slug, 'to'));
}
// Alternatively, if an expression is provided instead of the slug of a field,
// find all fields inside the expression, obtain their column selectors, and
// insert them into the expression, after which the expression can be used in the
// SQL statement.
else if ('expression' in field) {
fieldSelector = field.expression.replace(RONIN_SCHEMA_FIELD_REGEX, (match) => {
const fieldSlug = match.replace(RONIN_SCHEMA_SYMBOLS.FIELD, '');
return getFieldFromSchema(schema, fieldSlug, 'to').fieldSelector;
});
}

if (field.collation) fieldSelector += ` COLLATE ${field.collation}`;
Expand Down
62 changes: 61 additions & 1 deletion tests/meta.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,7 @@ test('create new index', () => {
]);
});

test('create new index with filters', () => {
test('create new index with filter', () => {
const fields = [
{
slug: 'email',
Expand Down Expand Up @@ -710,6 +710,66 @@ test('create new index with filters', () => {
]);
});

test('create new index with field expressions', () => {
const fields = [
{
expression: `LOWER(${RONIN_SCHEMA_SYMBOLS.FIELD}firstName || ' ' || ${RONIN_SCHEMA_SYMBOLS.FIELD}lastName)`,
},
];

const queries: Array<Query> = [
{
create: {
index: {
to: {
slug: 'index_name',
schema: { slug: 'account' },
fields,
},
},
},
},
];

const schemas: Array<Schema> = [
{
slug: 'account',
fields: [
{
slug: 'firstName',
type: 'string',
},
{
slug: 'lastName',
type: 'string',
},
],
},
];

const statements = compileQueries(queries, schemas);

expect(statements).toEqual([
{
statement: `CREATE INDEX "index_name" ON "accounts" (LOWER("firstName" || ' ' || "lastName"))`,
params: [],
},
{
statement:
'INSERT INTO "indexes" ("slug", "schema", "fields", "id", "ronin.createdAt", "ronin.updatedAt") VALUES (?1, (SELECT "id" FROM "schemas" WHERE ("slug" = ?2) LIMIT 1), IIF("fields" IS NULL, ?3, json_patch("fields", ?3)), ?4, ?5, ?6) RETURNING *',
params: [
'index_name',
'account',
JSON.stringify(fields),
expect.stringMatching(RECORD_ID_REGEX),
expect.stringMatching(RECORD_TIMESTAMP_REGEX),
expect.stringMatching(RECORD_TIMESTAMP_REGEX),
],
returning: true,
},
]);
});

test('create new index with ordered and collated fields', () => {
const fields = [
{
Expand Down

0 comments on commit 624ac6f

Please sign in to comment.