Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add BigIntField #193

Merged
merged 2 commits into from
Sep 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions packages/entity-database-adapter-knex/src/EntityFields.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { EntityFieldDefinition } from '@expo/entity';

/**
* EntityFieldDefinition for a Postres column with a JS JSON array type.
*/
export class JSONArrayField extends EntityFieldDefinition<any[]> {
protected validateInputValueInternal(value: any[]): boolean {
return Array.isArray(value);
}
}

/**
* EntityFieldDefinition for a Postgres column that may be a JS JSON array type.
* Does not do any validation.
*/
export class MaybeJSONArrayField extends EntityFieldDefinition<any | any[]> {
protected validateInputValueInternal(_value: any): boolean {
return true;
}
}

/**
* EntityFieldDefinition for a Postgres BIGINT column.
*/
export class BigIntField extends EntityFieldDefinition<string> {
protected validateInputValueInternal(value: string): boolean {
return typeof value === 'string';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ import {
EntityDatabaseAdapter,
FieldTransformerMap,
FieldTransformer,
JSONArrayField,
MaybeJSONArrayField,
TableQuerySelectionModifiers,
TableFieldSingleValueEqualityCondition,
TableFieldMultiValueEqualityCondition,
TableQuerySelectionModifiersWithOrderByRaw,
} from '@expo/entity';
import { Knex } from 'knex';

import { JSONArrayField, MaybeJSONArrayField } from './EntityFields';
import wrapNativePostgresCallAsync from './errors/wrapNativePostgresCallAsync';

export default class PostgresEntityDatabaseAdapter<TFields> extends EntityDatabaseAdapter<TFields> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,27 @@ describe('postgres entity integration', () => {
});
});

describe('BIGINT fields', () => {
it('supports BIGINT fields', async () => {
const vc1 = new ViewerContext(createKnexIntegrationTestEntityCompanionProvider(knexInstance));

let entity = await enforceAsyncResult(
PostgresTestEntity.creator(vc1).setField('bigintField', '72057594037928038').createAsync()
);
expect(entity.getField('bigintField')).toEqual('72057594037928038');

entity = await enforceAsyncResult(
PostgresTestEntity.updater(entity).setField('bigintField', '10').updateAsync()
);
expect(entity.getField('bigintField')).toEqual('10');

entity = await enforceAsyncResult(
PostgresTestEntity.updater(entity).setField('bigintField', '-10').updateAsync()
);
expect(entity.getField('bigintField')).toEqual('-10');
});
});

describe('conjunction field equality loading', () => {
it('supports single fieldValue and multiple fieldValues', async () => {
const vc1 = new ViewerContext(createKnexIntegrationTestEntityCompanionProvider(knexInstance));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { describeFieldTestCase } from '@expo/entity';

import { BigIntField, JSONArrayField, MaybeJSONArrayField } from '../EntityFields';

wschurman marked this conversation as resolved.
Show resolved Hide resolved
describeFieldTestCase(
new JSONArrayField({ columnName: 'wat' }),
[[[1, 2]] as any, [['hello']] as any],
[1, 'hello']
);
describeFieldTestCase(
new MaybeJSONArrayField({ columnName: 'wat' }),
[1, 'hello', [['hello']]],
[]
);
describeFieldTestCase(
new BigIntField({ columnName: 'wat' }),
['123457682149816498126412896', '123', '-1', '-124147812641876482716841'],
[1, false, -1, 1e6, {}]
);
1 change: 1 addition & 0 deletions packages/entity-database-adapter-knex/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
export { default as PostgresEntityDatabaseAdapter } from './PostgresEntityDatabaseAdapter';
export { default as PostgresEntityDatabaseAdapterProvider } from './PostgresEntityDatabaseAdapterProvider';
export { default as PostgresEntityQueryContextProvider } from './PostgresEntityQueryContextProvider';
export * from './EntityFields';
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ import {
StringField,
BooleanField,
StringArrayField,
JSONArrayField,
JSONObjectField,
DateField,
MaybeJSONArrayField,
EntityConfiguration,
EntityCompanionDefinition,
Entity,
} from '@expo/entity';
import { Knex } from 'knex';

import { BigIntField, JSONArrayField, MaybeJSONArrayField } from '../EntityFields';

type PostgresTestEntityFields = {
id: string;
name: string | null;
Expand All @@ -28,6 +28,7 @@ type PostgresTestEntityFields = {
} | null;
dateField: Date | null;
maybeJsonArrayField: string[] | { hello: string } | null;
bigintField: string | null;
};

export default class PostgresTestEntity extends Entity<
Expand Down Expand Up @@ -61,6 +62,7 @@ export default class PostgresTestEntity extends Entity<
table.jsonb('json_object_field');
table.dateTime('date_field', { useTz: true });
table.jsonb('maybe_json_array_field');
table.bigint('bigint_field');
});
}
await knex.into(tableName).truncate();
Expand Down Expand Up @@ -147,6 +149,9 @@ export const postgresTestEntityConfiguration = new EntityConfiguration<PostgresT
maybeJsonArrayField: new MaybeJSONArrayField({
columnName: 'maybe_json_array_field',
}),
bigintField: new BigIntField({
columnName: 'bigint_field',
}),
},
databaseAdapterFlavor: 'postgres',
cacheAdapterFlavor: 'redis',
Expand Down
19 changes: 0 additions & 19 deletions packages/entity/src/EntityFields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,22 +86,3 @@ export class EnumField extends EntityFieldDefinition<string | number> {
return typeof value === 'number' || typeof value === 'string';
}
}

/**
* EntityFieldDefinition for a column with a JS JSON array type.
*/
export class JSONArrayField extends EntityFieldDefinition<any[]> {
protected validateInputValueInternal(value: any[]): boolean {
return Array.isArray(value);
}
}

/**
* EntityFieldDefinition for a column that may be a JS JSON array type.
* Does not do any validation.
*/
export class MaybeJSONArrayField extends EntityFieldDefinition<any | any[]> {
protected validateInputValueInternal(_value: any): boolean {
return true;
}
}
12 changes: 0 additions & 12 deletions packages/entity/src/__tests__/EntityFields-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import {
StringArrayField,
JSONObjectField,
EnumField,
JSONArrayField,
MaybeJSONArrayField,
} from '../EntityFields';
import describeFieldTestCase from '../utils/testing/describeFieldTestCase';

Expand Down Expand Up @@ -71,13 +69,3 @@ describeFieldTestCase(
);
describeFieldTestCase(new JSONObjectField({ columnName: 'wat' }), [{}], [true, 'hello']);
describeFieldTestCase(new EnumField({ columnName: 'wat' }), ['hello', 1], [true]);
describeFieldTestCase(
new JSONArrayField({ columnName: 'wat' }),
[[[1, 2]] as any, [['hello']] as any], // jest test cases need extra wrapping array
[1, 'hello']
);
describeFieldTestCase(
new MaybeJSONArrayField({ columnName: 'wat' }),
[1, 'hello', [['hello']]], // jest test cases need extra wrapping array
[]
);