Skip to content
Closed
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
12 changes: 11 additions & 1 deletion packages/datasource-elasticsearch/src/introspection/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,12 @@ export interface ElasticsearchDatasourceOptionsBuilder {
/**
* Add a collection from an index
*/
addCollectionFromIndex({ name, indexName }: ElasticsearchCollectionFromIndexOptions): this;
addCollectionFromIndex({
name,
indexName,
overrideTypeConverter,
enableCount,
}: ElasticsearchCollectionFromIndexOptions): this;

/**
* Add a collection from a template
Expand All @@ -71,6 +76,7 @@ export interface ElasticsearchDatasourceOptionsBuilder {
templateName,
generateIndexName,
overrideTypeConverter,
enableCount,
}: ElasticsearchCollectionFromTemplateOptions): this;
}

Expand All @@ -90,6 +96,7 @@ export class ElasticsearchDatasourceBuilder implements ElasticsearchDatasourceOp
name,
indexName,
overrideTypeConverter,
enableCount,
}: ElasticsearchCollectionFromIndexOptions): this {
this.collectionsPromises.push(
(async () => {
Expand All @@ -108,6 +115,7 @@ export class ElasticsearchDatasourceBuilder implements ElasticsearchDatasourceOp
mapping[indexName].mappings,
() => indexName,
overrideTypeConverter,
enableCount,
);
})(),
);
Expand All @@ -120,6 +128,7 @@ export class ElasticsearchDatasourceBuilder implements ElasticsearchDatasourceOp
templateName,
generateIndexName,
overrideTypeConverter,
enableCount,
}: ElasticsearchCollectionFromTemplateOptions): this {
this.collectionsPromises.push(
(async () => {
Expand All @@ -129,6 +138,7 @@ export class ElasticsearchDatasourceBuilder implements ElasticsearchDatasourceOp
name,
typeof generateIndexName === 'string' ? () => generateIndexName : generateIndexName,
overrideTypeConverter,
enableCount,
);

return modelFromTemplate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export default async function introspectTemplate(
overrideName?: string,
generateIndexName?: (record?: unknown) => string,
overrideTypeConverter?: OverrideTypeConverter,
enableCount?: boolean,
) {
const isIndexTemplate = await elasticsearchClient.indices.existsIndexTemplate({
name: templateName,
Expand Down Expand Up @@ -40,6 +41,7 @@ export default async function introspectTemplate(
mappings,
generateIndexName,
overrideTypeConverter,
enableCount,
);
}

Expand All @@ -59,5 +61,6 @@ export default async function introspectTemplate(
mappings,
generateIndexName,
overrideTypeConverter,
enableCount,
);
}
36 changes: 36 additions & 0 deletions packages/datasource-elasticsearch/test/collection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,42 @@ describe('ElasticsearchDataSource > Collection', () => {
expect(elasticsearchCollection.nativeDriver).toBeDefined();
});

describe('count capability', () => {
const buildCollection = (enableCount?: boolean) => {
const dataSource = Symbol('datasource') as unknown as DataSource;
const mockClient = new MockClient();
const client = new Client({
node: 'http://localhost:9200',
Connection: mockClient.getConnection(),
});

const model = new ModelElasticsearch(
client,
'__collection__',
['indexPattern'],
['alias'],
{ properties: { name: { type: 'integer' } } },
undefined,
undefined,
enableCount,
);

return new ElasticsearchCollection(dataSource, model, jest.fn(), client);
};

it('exposes a countable schema when the model has enableCount = true', () => {
const collection = buildCollection(true);

expect(collection.schema.countable).toBe(true);
});

it('exposes a non-countable schema by default', () => {
const collection = buildCollection();

expect(collection.schema.countable).toBe(false);
});
});

// describe('create', () => {
// });

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,5 +161,34 @@ describe('introspection > builder', () => {

await deleteElasticsearchTemplate('test-template', 'test-template-*');
});

it('should propagate enableCount to the produced model', async () => {
const { client } = await createElasticsearchTemplate(
'test-template-count',
{
indexPattern: 'test-template-count-*',
alias: 'test-template-count-alias',
mapping: {
dynamic: 'strict',
properties: { id: { type: 'integer' } },
},
},
[{ id: 1, index: 'test-template-count-index' }],
);

const elasticsearchDatasourceBuilder = new ElasticsearchDatasourceBuilder(client);

elasticsearchDatasourceBuilder.addCollectionFromTemplate({
name: 'countable',
templateName: 'test-template-count',
enableCount: true,
});

const [model] = await elasticsearchDatasourceBuilder.createCollectionsFromConfiguration();

expect(model.enableCount).toBe(true);

await deleteElasticsearchTemplate('test-template-count', 'test-template-count-*');
});
});
});
Loading