Skip to content

Commit

Permalink
Added target for reference fields (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
leo authored Oct 29, 2024
1 parent b2cb2a7 commit 69e0c02
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 38 deletions.
4 changes: 2 additions & 2 deletions src/instructions/to.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,9 @@ export const handleTo = (
);

const composeStatement = (subQueryType: 'create' | 'drop', value?: unknown) => {
const origin =
const source =
queryType === 'create' ? { id: toInstruction.id } : withInstruction;
const recordDetails: Record<string, unknown> = { origin };
const recordDetails: Record<string, unknown> = { source };

if (value) recordDetails.target = value;

Expand Down
2 changes: 1 addition & 1 deletion src/types/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type SchemaFieldNormal = SchemaFieldBasics & {

export type SchemaFieldReference = SchemaFieldBasics & {
type: 'reference';
schema: string;
target: string;
kind?: 'one' | 'many';
};

Expand Down
21 changes: 11 additions & 10 deletions src/utils/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ const SYSTEM_FIELDS: Array<SchemaField> = [
{
name: 'RONIN - Created By',
type: 'reference',
schema: 'account',
target: 'account',
slug: 'ronin.createdBy',
},
{
Expand All @@ -191,7 +191,7 @@ const SYSTEM_FIELDS: Array<SchemaField> = [
{
name: 'RONIN - Updated By',
type: 'reference',
schema: 'account',
target: 'account',
slug: 'ronin.updatedBy',
},
];
Expand Down Expand Up @@ -227,9 +227,10 @@ const SYSTEM_SCHEMAS: Array<Schema> = [
fields: [
...SYSTEM_FIELDS,
{ slug: 'name', type: 'string' },
{ slug: 'slug', type: 'string' },
{ slug: 'type', type: 'string' },
{ slug: 'schema', type: 'reference', schema: 'schema' },
{ slug: 'slug', type: 'string', required: true },
{ slug: 'type', type: 'string', required: true },
{ slug: 'schema', type: 'reference', target: 'schema' },
{ slug: 'target', type: 'reference', target: 'schema' },
{ slug: 'required', type: 'boolean' },
{ slug: 'defaultValue', type: 'string' },
{ slug: 'unique', type: 'boolean' },
Expand Down Expand Up @@ -258,7 +259,7 @@ export const addSystemSchemas = (schemas: Array<Schema>): Array<Schema> => {
// different queries in the codebase of an application.
for (const field of schema.fields || []) {
if (field.type === 'reference' && !field.slug.startsWith('ronin.')) {
const relatedSchema = getSchemaBySlug(list, field.schema);
const relatedSchema = getSchemaBySlug(list, field.target);

let fieldSlug = relatedSchema.slug;

Expand All @@ -274,14 +275,14 @@ export const addSystemSchemas = (schemas: Array<Schema>): Array<Schema> => {
slug: fieldSlug,
fields: [
{
slug: 'origin',
slug: 'source',
type: 'reference',
schema: schema.slug,
target: schema.slug,
},
{
slug: 'target',
type: 'reference',
schema: relatedSchema.slug,
target: relatedSchema.slug,
},
],
});
Expand All @@ -303,7 +304,7 @@ export const addSystemSchemas = (schemas: Array<Schema>): Array<Schema> => {

// Additionally, add a default shortcut for resolving the child records in the
// related schema.
const relatedSchemaToModify = list.find((schema) => schema.slug === field.schema);
const relatedSchemaToModify = list.find((schema) => schema.slug === field.target);
if (!relatedSchemaToModify) throw new Error('Missing related schema');

relatedSchemaToModify.including = {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/statement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ export const composeConditions = (
// This can be either a string or an object with conditions such as `being`.
recordTarget = values[0];
} else {
const relatedSchema = getSchemaBySlug(schemas, schemaField.schema);
const relatedSchema = getSchemaBySlug(schemas, schemaField.target);

const subQuery: Query = {
get: {
Expand Down
18 changes: 9 additions & 9 deletions tests/instructions/for.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ test('get single record for pre-defined condition', () => {
{
slug: 'account',
type: 'reference',
schema: 'account',
target: 'account',
},
{
slug: 'space',
type: 'reference',
schema: 'space',
target: 'space',
},
{
slug: 'activeAt',
Expand All @@ -51,7 +51,7 @@ test('get single record for pre-defined condition', () => {
{
slug: 'space',
type: 'reference',
schema: 'space',
target: 'space',
},
],
for: {
Expand Down Expand Up @@ -99,12 +99,12 @@ test('get single record for pre-defined condition containing sub query', () => {
{
slug: 'account',
type: 'reference',
schema: 'account',
target: 'account',
},
{
slug: 'space',
type: 'reference',
schema: 'space',
target: 'space',
},
{
slug: 'activeAt',
Expand All @@ -119,7 +119,7 @@ test('get single record for pre-defined condition containing sub query', () => {
{
slug: 'space',
type: 'reference',
schema: 'space',
target: 'space',
},
],
for: {
Expand Down Expand Up @@ -177,12 +177,12 @@ test('get single record for pre-defined field containing sub query', () => {
{
slug: 'account',
type: 'reference',
schema: 'account',
target: 'account',
},
{
slug: 'space',
type: 'reference',
schema: 'space',
target: 'space',
},
{
slug: 'activeAt',
Expand All @@ -197,7 +197,7 @@ test('get single record for pre-defined field containing sub query', () => {
{
slug: 'space',
type: 'reference',
schema: 'space',
target: 'space',
},
],
for: {
Expand Down
6 changes: 3 additions & 3 deletions tests/instructions/including.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ test('get single record including parent record (many-to-one)', () => {
{
slug: 'account',
type: 'reference',
schema: 'account',
target: 'account',
kind: 'one',
},
],
Expand Down Expand Up @@ -57,7 +57,7 @@ test('get single record including child records (one-to-many, defined manually)'
{
slug: 'comments',
type: 'reference',
schema: 'comment',
target: 'comment',
kind: 'many',
},
],
Expand Down Expand Up @@ -97,7 +97,7 @@ test('get single record including child records (one-to-many, defined automatica
{
slug: 'account',
type: 'reference',
schema: 'account',
target: 'account',
},
],
},
Expand Down
18 changes: 9 additions & 9 deletions tests/instructions/to.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ test('set single record to new one-cardinality reference field', () => {
{
slug: 'account',
type: 'reference',
schema: 'account',
target: 'account',
},
],
},
Expand Down Expand Up @@ -119,7 +119,7 @@ test('set single record to new many-cardinality reference field', () => {
{
slug: 'comments',
type: 'reference',
schema: 'comment',
target: 'comment',
kind: 'many',
},
],
Expand All @@ -139,8 +139,8 @@ test('set single record to new many-cardinality reference field', () => {
const { writeStatements, readStatement, values } = compileQueryInput(query, schemas);

expect(writeStatements).toEqual([
'DELETE FROM "ronin_posts_comments" WHERE ("origin" = ?1)',
'INSERT INTO "ronin_posts_comments" ("origin", "target", "id", "ronin.createdAt", "ronin.updatedAt") VALUES (?2, (SELECT "id" FROM "comments" WHERE ("content" = ?3) LIMIT 1), ?4, ?5, ?6)',
'DELETE FROM "ronin_posts_comments" WHERE ("source" = ?1)',
'INSERT INTO "ronin_posts_comments" ("source", "target", "id", "ronin.createdAt", "ronin.updatedAt") VALUES (?2, (SELECT "id" FROM "comments" WHERE ("content" = ?3) LIMIT 1), ?4, ?5, ?6)',
]);

expect(readStatement).toBe(
Expand Down Expand Up @@ -187,7 +187,7 @@ test('set single record to new many-cardinality reference field (add)', () => {
{
slug: 'comments',
type: 'reference',
schema: 'comment',
target: 'comment',
kind: 'many',
},
],
Expand All @@ -207,7 +207,7 @@ test('set single record to new many-cardinality reference field (add)', () => {
const { writeStatements, readStatement, values } = compileQueryInput(query, schemas);

expect(writeStatements).toEqual([
'INSERT INTO "ronin_posts_comments" ("origin", "target", "id", "ronin.createdAt", "ronin.updatedAt") VALUES (?1, (SELECT "id" FROM "comments" WHERE ("content" = ?2) LIMIT 1), ?3, ?4, ?5)',
'INSERT INTO "ronin_posts_comments" ("source", "target", "id", "ronin.createdAt", "ronin.updatedAt") VALUES (?1, (SELECT "id" FROM "comments" WHERE ("content" = ?2) LIMIT 1), ?3, ?4, ?5)',
]);

expect(readStatement).toBe(
Expand Down Expand Up @@ -253,7 +253,7 @@ test('set single record to new many-cardinality reference field (delete)', () =>
{
slug: 'comments',
type: 'reference',
schema: 'comment',
target: 'comment',
kind: 'many',
},
],
Expand All @@ -273,7 +273,7 @@ test('set single record to new many-cardinality reference field (delete)', () =>
const { writeStatements, readStatement, values } = compileQueryInput(query, schemas);

expect(writeStatements).toEqual([
'DELETE FROM "ronin_posts_comments" WHERE ("origin" = ?1 AND "target" = (SELECT "id" FROM "comments" WHERE ("content" = ?2) LIMIT 1))',
'DELETE FROM "ronin_posts_comments" WHERE ("source" = ?1 AND "target" = (SELECT "id" FROM "comments" WHERE ("content" = ?2) LIMIT 1))',
]);

expect(readStatement).toBe(
Expand Down Expand Up @@ -553,7 +553,7 @@ test('set single record to new grouped reference field', () => {
{
slug: 'billing.manager',
type: 'reference',
schema: 'account',
target: 'account',
},
],
},
Expand Down
6 changes: 3 additions & 3 deletions tests/instructions/with.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ test('get single record with reference field', () => {
{
slug: 'account',
type: 'reference',
schema: 'account',
target: 'account',
},
],
},
Expand Down Expand Up @@ -544,7 +544,7 @@ test('get single record with reference field and id', () => {
{
slug: 'account',
type: 'reference',
schema: 'account',
target: 'account',
},
],
},
Expand Down Expand Up @@ -583,7 +583,7 @@ test('get single record with reference field and id with condition', () => {
{
slug: 'account',
type: 'reference',
schema: 'account',
target: 'account',
},
],
},
Expand Down

0 comments on commit 69e0c02

Please sign in to comment.