Skip to content

Commit

Permalink
Correctly format empty boolean fields (#140)
Browse files Browse the repository at this point in the history
  • Loading branch information
leo authored Feb 11, 2025
1 parent 9f7caa0 commit 514c7ea
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 13 deletions.
11 changes: 7 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,13 @@ class Transaction {
let newSlug = field.mountingPath;
let newValue = row[fieldIndex];

if (field.type === 'json' || field.type === 'blob') {
newValue = JSON.parse(newValue as string);
} else if (field.type === 'boolean') {
newValue = Boolean(newValue);
// If the value of the field isn't empty, format it.
if (newValue !== null) {
if (field.type === 'json' || field.type === 'blob') {
newValue = JSON.parse(newValue as string);
} else if (field.type === 'boolean') {
newValue = Boolean(newValue);
}
}

// If the query is used to alter the database schema, the result of the query
Expand Down
8 changes: 4 additions & 4 deletions tests/instructions/before-after.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -423,10 +423,10 @@ test('get multiple records before cursor ordered by empty boolean field', async
const rawResults = await queryEphemeralDatabase(models, transaction.statements);
const result = transaction.formatResults(rawResults)[0] as MultipleRecordResult;

const firstRecordSwimming = false;
const firstRecordSwimming: null = null;
const firstRecordTime = new Date('2024-12-10T10:47:58.079Z');

const lastRecordSwimming = false;
const lastRecordSwimming: null = null;
const lastRecordTime = new Date('2024-12-09T10:47:58.079Z');

expect(result.records).toEqual([
Expand Down Expand Up @@ -454,8 +454,8 @@ test('get multiple records before cursor ordered by empty boolean field', async
},
]);

expect(result.moreBefore).toBe(`${firstRecordSwimming},${firstRecordTime.getTime()}`);
expect(result.moreAfter).toBe(`${lastRecordSwimming},${lastRecordTime.getTime()}`);
expect(result.moreBefore).toBe(`RONIN_NULL,${firstRecordTime.getTime()}`);
expect(result.moreAfter).toBe(`RONIN_NULL,${lastRecordTime.getTime()}`);
});

test('get multiple records before cursor ordered by empty number field', async () => {
Expand Down
133 changes: 128 additions & 5 deletions tests/instructions/with.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,88 @@ test('get single record with link field and id with condition', async () => {
expect(result.record?.account).toBe('acc_39h8fhe98hefah9j');
});

test('get single record with boolean field', async () => {
const queries: Array<Query> = [
{
get: {
member: {
with: {
pending: false,
},
},
},
},
];

const models: Array<Model> = [
{
slug: 'member',
fields: [
{
slug: 'pending',
type: 'boolean',
},
],
},
];

const transaction = new Transaction(queries, { models });

expect(transaction.statements).toEqual([
{
statement: `SELECT "id", "ronin.createdAt", "ronin.createdBy", "ronin.updatedAt", "ronin.updatedBy", "pending" FROM "members" WHERE "pending" = ?1 LIMIT 1`,
params: [0],
returning: true,
},
]);

const rawResults = await queryEphemeralDatabase(models, transaction.statements);
const result = transaction.formatResults(rawResults)[0] as SingleRecordResult;

expect(result.record).toHaveProperty('pending', false);
});

test('get single record with boolean field (empty)', async () => {
const queries: Array<Query> = [
{
get: {
member: {
with: {
someEmptyField: null,
},
},
},
},
];

const models: Array<Model> = [
{
slug: 'member',
fields: [
{
slug: 'someEmptyField',
type: 'boolean',
},
],
},
];

const transaction = new Transaction(queries, { models });

expect(transaction.statements).toEqual([
{
statement: `SELECT "id", "ronin.createdAt", "ronin.createdBy", "ronin.updatedAt", "ronin.updatedBy", "someEmptyField" FROM "members" WHERE "someEmptyField" IS NULL LIMIT 1`,
params: [],
returning: true,
},
]);

const rawResults = await queryEphemeralDatabase(models, transaction.statements);
const result = transaction.formatResults(rawResults)[0] as SingleRecordResult;

expect(result.record).toHaveProperty('someEmptyField', null);
});

test('get single record with json field', async () => {
const queries: Array<Query> = [
{
Expand Down Expand Up @@ -829,6 +911,47 @@ test('get single record with json field', async () => {
expect(result.record?.locations).toHaveProperty('europe', 'berlin');
});

test('get single record with json field (empty)', async () => {
const queries: Array<Query> = [
{
get: {
team: {
with: {
someEmptyField: null,
},
},
},
},
];

const models: Array<Model> = [
{
slug: 'team',
fields: [
{
slug: 'someEmptyField',
type: 'json',
},
],
},
];

const transaction = new Transaction(queries, { models });

expect(transaction.statements).toEqual([
{
statement: `SELECT "id", "ronin.createdAt", "ronin.createdBy", "ronin.updatedAt", "ronin.updatedBy", "someEmptyField" FROM "teams" WHERE "someEmptyField" IS NULL LIMIT 1`,
params: [],
returning: true,
},
]);

const rawResults = await queryEphemeralDatabase(models, transaction.statements);
const result = transaction.formatResults(rawResults)[0] as SingleRecordResult;

expect(result.record).toHaveProperty('someEmptyField', null);
});

test('get single record with blob field', async () => {
const queries: Array<Query> = [
{
Expand Down Expand Up @@ -874,13 +997,13 @@ test('get single record with blob field', async () => {
expect(result.record?.avatar).toHaveProperty('meta.type', 'image/png');
});

test('get single record with empty field', async () => {
test('get single record with string field (empty)', async () => {
const queries: Array<Query> = [
{
get: {
beach: {
with: {
region: null,
someEmptyField: null,
},
},
},
Expand All @@ -892,7 +1015,7 @@ test('get single record with empty field', async () => {
slug: 'beach',
fields: [
{
slug: 'region',
slug: 'someEmptyField',
type: 'string',
},
],
Expand All @@ -903,7 +1026,7 @@ test('get single record with empty field', async () => {

expect(transaction.statements).toEqual([
{
statement: `SELECT "id", "ronin.createdAt", "ronin.createdBy", "ronin.updatedAt", "ronin.updatedBy", "region" FROM "beaches" WHERE "region" IS NULL LIMIT 1`,
statement: `SELECT "id", "ronin.createdAt", "ronin.createdBy", "ronin.updatedAt", "ronin.updatedBy", "someEmptyField" FROM "beaches" WHERE "someEmptyField" IS NULL LIMIT 1`,
params: [],
returning: true,
},
Expand All @@ -912,7 +1035,7 @@ test('get single record with empty field', async () => {
const rawResults = await queryEphemeralDatabase(models, transaction.statements);
const result = transaction.formatResults(rawResults)[0] as SingleRecordResult;

expect(result.record).toHaveProperty('region', null);
expect(result.record).toHaveProperty('someEmptyField', null);
});

test('get single record with one of fields', async () => {
Expand Down

0 comments on commit 514c7ea

Please sign in to comment.