Skip to content

Commit

Permalink
feat: move algolia (#385)
Browse files Browse the repository at this point in the history
  • Loading branch information
bodinsamuel committed Aug 8, 2019
1 parent 373d341 commit e5d7bec
Show file tree
Hide file tree
Showing 5 changed files with 220 additions and 97 deletions.
69 changes: 69 additions & 0 deletions src/__tests__/createStateManager.test.js
@@ -0,0 +1,69 @@
import createStateManager from '../createStateManager.js';

describe('stateManager', () => {
describe('get()', () => {
it('should get userData from algolia', async () => {
const mock = {
getSettings: jest.fn(() => {
return {
userData: 'foobar',
};
}),
};
const stateManager = createStateManager(mock);
const userData = await stateManager.get();

expect(mock.getSettings).toHaveBeenCalled();
expect(userData).toBe('foobar');
});
});

describe('set()', () => {
it('should set userData to algolia', async () => {
const mock = {
setSettings: jest.fn(),
};
const stateManager = createStateManager(mock);
await stateManager.set('state');

expect(mock.setSettings).toHaveBeenCalledWith({
userData: 'state',
});
});
});

describe('reset()', () => {
it('should reset userData', async () => {
const mock = {
setSettings: jest.fn(),
};
const stateManager = createStateManager(mock);
await stateManager.reset();

expect(mock.setSettings).toHaveBeenCalled();
});
});

describe('save()', () => {
it('should save userData to algolia', async () => {
const mock = {
getSettings: jest.fn(() => {
return {
userData: { bar: 'foo' },
};
}),
setSettings: jest.fn(),
};
const stateManager = createStateManager(mock);
await stateManager.save({ foo: 'bar' });

expect(mock.getSettings).toHaveBeenCalled();
expect(mock.setSettings).toHaveBeenCalledWith({
userData: {
bar: 'foo',
foo: 'bar',
},
});
});
});
});
63 changes: 63 additions & 0 deletions src/algolia/index.js
@@ -0,0 +1,63 @@
import algoliasearch from 'algoliasearch';

function createClient(appId, apiKey, indexName) {
const client = algoliasearch(appId, apiKey);
return {
index: client.initIndex(indexName),
client,
};
}

/**
* Prepare algolia for indexing
* @param {object} config
*/
async function prepare(config) {
if (!config.apiKey) {
throw new Error(
'npm-search: Please provide the `apiKey` env variable and restart'
);
}

// Get main index and boostrap algolia client
const { index: mainIndex, client } = createClient(
config.appId,
config.apiKey,
config.indexName
);
const { index: bootstrapIndex } = createClient(
config.appId,
config.apiKey,
config.bootstrapIndexName
);

// Ensure indices exists by calling an empty setSettings()
await mainIndex.setSettings({});
await bootstrapIndex.setSettings({});

return {
client,
mainIndex,
bootstrapIndex,
};
}

/**
*
* @param {AlgoliasearchIndex} index
* @param {object} config
*/
async function putDefaultSettings(index, config) {
await index.setSettings(config.indexSettings);

await index.batchSynonyms(config.indexSynonyms, {
replaceExistingSynonyms: true,
});
const { taskID } = await index.batchRules(config.indexRules, {
replaceExistingRules: true,
});

await index.waitTask(taskID);
}

export { prepare, putDefaultSettings };
15 changes: 0 additions & 15 deletions src/createAlgoliaIndex.js

This file was deleted.

104 changes: 53 additions & 51 deletions src/createStateManager.js
Expand Up @@ -7,54 +7,56 @@ const defaultState = {
bootstrapLastId: undefined,
};

let currentState;

export default algoliaIndex => ({
async check() {
if (config.seq !== null) return this.reset();
const state = await this.get();

if (state === undefined) {
return this.reset();
}

return state;
},

async get() {
if (currentState) {
return currentState;
}

const start = Date.now();
const { userData } = await algoliaIndex.getSettings();
datadog.timing('stateManager.get', Date.now() - start);

return userData;
},

async set(state) {
currentState = state;

const start = Date.now();
await algoliaIndex.setSettings({
userData: state,
});
datadog.timing('stateManager.set', Date.now() - start);

return state;
},

async reset() {
return await this.set(defaultState);
},

async save(partial) {
const current = (await this.get()) || defaultState;

return await this.set({
...current,
...partial,
});
},
});
export default algoliaIndex => {
let currentState;

return {
async check() {
if (config.seq !== null) return this.reset();
const state = await this.get();

if (state === undefined) {
return this.reset();
}

return state;
},

async get() {
if (currentState) {
return currentState;
}

const start = Date.now();
const { userData } = await algoliaIndex.getSettings();
datadog.timing('stateManager.get', Date.now() - start);

return userData;
},

async set(state) {
currentState = state;

const start = Date.now();
await algoliaIndex.setSettings({
userData: state,
});
datadog.timing('stateManager.set', Date.now() - start);

return state;
},

async reset() {
return await this.set(defaultState);
},

async save(partial) {
const current = (await this.get()) || defaultState;

return await this.set({
...current,
...partial,
});
},
};
};

0 comments on commit e5d7bec

Please sign in to comment.