Skip to content
This repository has been archived by the owner on Oct 25, 2021. It is now read-only.

Commit

Permalink
Merge pull request #156 from Financial-Times/sensible-schema-init
Browse files Browse the repository at this point in the history
more sensible sdk initialisation
  • Loading branch information
wheresrhys authored Mar 2, 2020
2 parents b0c80a2 + 336e396 commit 3fcf140
Show file tree
Hide file tree
Showing 18 changed files with 53 additions and 38 deletions.
5 changes: 1 addition & 4 deletions packages/tc-api-express/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,13 @@ const getApp = async (options = {}) => {
router.use(bodyParsers);
router.use(restPath, restMiddlewares, getRestApi(options));
router.use(errorToErrors);

schema.init(schemaOptions);
const {
isSchemaUpdating,
graphqlHandler,
listenForSchemaChanges: updateGraphqlApiOnSchemaChange,
} = getGraphqlApi(options);

schema.init();
updateGraphqlApiOnSchemaChange();
updateConstraintsOnSchemaChange();

Expand All @@ -83,14 +82,12 @@ const getApp = async (options = {}) => {
availableEvents,
};

schema.init(schemaOptions);
updateGraphqlApiOnSchemaChange();
// avoids this running in every single spec file
// instead we explictly set up the constraints once before we start the test suite
if (!process.env.TREECREEPER_TEST) {
updateConstraintsOnSchemaChange();
}

await schema.ready();

return app;
Expand Down
9 changes: 9 additions & 0 deletions packages/tc-schema-sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ The package exports a singleton instance, and once initialised, `@financial-time

### Initialisation

This is a little odd (and should be improved in future)

- When using local, static data (`schemaDirectory` or `schemaData` options below), the `init()` method populates the sdk with data immediately and its methods can be used to access the data immediately
- When using remote data (`schemaBaseUrl`), no data is populated, and `schema.ready()` must be awaited before using the sdk's synchronous methods.

Be aware of the idiosyncrasy above if you ever come across errors complaining that no data is available.

### `init(options)`

The package exports an `init(options)` function, that takes the following options:

- `schemaDirectory` - absolute path to a directory that contains schema files as yaml. Will use the `TREECREEPER_SCHEMA_DIRECTORY` environment variable if defined. This is the preferred way of specifying the directory
Expand Down
2 changes: 1 addition & 1 deletion packages/tc-schema-sdk/browser.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { SDK } = require('./sdk');

module.exports = new SDK({ init: false });
module.exports = new SDK();

module.exports.SDK = SDK;
6 changes: 5 additions & 1 deletion packages/tc-schema-sdk/lib/__tests__/polling.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ const { RawDataWrapper } = require('../raw-data-wrapper');
const { Cache } = require('../cache');

const create = options =>
new SchemaUpdater(options, new RawDataWrapper(), new Cache());
new SchemaUpdater({
options,
rawData: new RawDataWrapper(),
cache: new Cache(),
});

const nextTick = () => new Promise(res => process.nextTick(res));

Expand Down
6 changes: 5 additions & 1 deletion packages/tc-schema-sdk/lib/__tests__/stale.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ const { RawDataWrapper } = require('../raw-data-wrapper');
const { Cache } = require('../cache');

const create = options =>
new SchemaUpdater(options, new RawDataWrapper(), new Cache());
new SchemaUpdater({
options,
rawData: new RawDataWrapper(),
cache: new Cache(),
});

const timer = delay => new Promise(res => setTimeout(res, delay));
const nextTick = () => new Promise(res => process.nextTick(res));
Expand Down
2 changes: 1 addition & 1 deletion packages/tc-schema-sdk/lib/__tests__/validate-code.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { SDK } = require('../../sdk');
const { SDK } = require('../..');

describe('validateCode', () => {
const {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { SDK } = require('../../sdk');
const { SDK } = require('../..');

describe('validatePropertyName', () => {
const {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { SDK } = require('../../sdk');
const { SDK } = require('../..');
const readYaml = require('../read-yaml');

const getValidator = (type, { enums, relationshipTypes } = {}) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { SDK } = require('../../sdk');
const { SDK } = require('../..');

describe('validateTypeName', () => {
const {
Expand Down
6 changes: 4 additions & 2 deletions packages/tc-schema-sdk/lib/updater.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ const EventEmitter = require('events');
const fetch = require('node-fetch');

class SchemaUpdater {
constructor(options, rawData, cache, readYaml) {
constructor({ options, rawData, cache, readYaml }) {
this.eventEmitter = new EventEmitter();
this.lastRefreshDate = 0;
this.rawData = rawData;
this.cache = cache;
this.readYaml = readYaml;
this.configure(options);
if (options) {
this.configure(options);
}
}

configure({
Expand Down
32 changes: 11 additions & 21 deletions packages/tc-schema-sdk/sdk.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,17 @@ const { SchemaUpdater } = require('./lib/updater');
const utils = require('./lib/utils');

class SDK {
constructor(options = {}) {
constructor(options) {
this.cache = new Cache();
this.rawData = new RawDataWrapper();
this.TreecreeperUserError = TreecreeperUserError;
this.subscribers = [];
this.updater = new SchemaUpdater({
options,
rawData: this.rawData,
cache: this.cache,
readYaml: this.readYaml,
});

this.TreecreeperUserError = TreecreeperUserError;
this.getEnums = this.createEnrichedAccessor(enums);
this.getPrimitiveTypes = this.createEnrichedAccessor(primitiveTypes);
this.getStringValidator = this.createEnrichedAccessor(stringValidator);
Expand All @@ -40,10 +45,6 @@ class SDK {
Object.entries(utils).forEach(([name, method]) => {
this[name] = method.bind(this);
});

if (options.init !== false) {
this.init(options);
}
}

createEnrichedAccessor({ accessor, cacheKeyGenerator }) {
Expand All @@ -53,19 +54,8 @@ class SDK {
);
}

async init(options) {
const schemaUpdater = new SchemaUpdater(
options,
this.rawData,
this.cache,
this.readYaml,
);

// hook up schema updater to this.cache then
schemaUpdater.on('change', data => {
this.subscribers.forEach(handler => handler(data));
});
this.updater = schemaUpdater;
init(options) {
return this.updater.configure(options);
}

async ready() {
Expand All @@ -82,7 +72,7 @@ class SDK {
if (this.rawData.isHydrated) {
handler(event);
}
this.subscribers.push(handler);
this.updater.on('change', handler);
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/tc-schema-sdk/server.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { SDK } = require('./sdk');
SDK.prototype.readYaml = require('./lib/read-yaml');

module.exports = new SDK({ init: false });
module.exports = new SDK();
module.exports.SDK = SDK;
4 changes: 3 additions & 1 deletion packages/tc-schema-validator/tests/enums.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/* global it, describe, expect */
const { SDK } = require('@financial-times/tc-schema-sdk');

const enums = new SDK().rawData.getEnums();
const sdk = new SDK();
sdk.init();
const enums = sdk.rawData.getEnums();

describe('enums', () => {
Object.entries(enums).forEach(([name, { description, options }]) => {
Expand Down
4 changes: 3 additions & 1 deletion packages/tc-schema-validator/tests/primitive-types.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/* global it, describe, expect */
const { SDK } = require('@financial-times/tc-schema-sdk');

const primitiveTypes = new SDK().rawData.getPrimitiveTypes();
const sdk = new SDK();
sdk.init();
const primitiveTypes = sdk.rawData.getPrimitiveTypes();

describe('primitive types', () => {
Object.entries(primitiveTypes).forEach(([name, { component, graphql }]) => {
Expand Down
4 changes: 3 additions & 1 deletion packages/tc-schema-validator/tests/string-patterns.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/* global it, describe, expect */
const { SDK } = require('@financial-times/tc-schema-sdk');

const stringPatterns = new SDK().rawData.getStringPatterns();
const sdk = new SDK();
sdk.init();
const stringPatterns = sdk.rawData.getStringPatterns();
const longString = 'x'.repeat(257);

describe('string patterns', () => {
Expand Down
1 change: 1 addition & 0 deletions packages/tc-schema-validator/tests/type-hierarchy.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
const { SDK } = require('@financial-times/tc-schema-sdk');

const sdk = new SDK();
sdk.init();
const { readYaml, rawData } = sdk;

const typeHierarchy = rawData.getTypeHierarchy();
Expand Down
1 change: 1 addition & 0 deletions packages/tc-schema-validator/tests/type-test-suite.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const validURL = require('valid-url');
const { SDK } = require('@financial-times/tc-schema-sdk');

const sdk = new SDK();
sdk.init();
const primitiveTypesMap = sdk.getPrimitiveTypes();
const { getStringValidator } = sdk;
const enums = sdk.rawData.getEnums();
Expand Down
1 change: 1 addition & 0 deletions packages/tc-schema-validator/tests/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const { SDK } = require('@financial-times/tc-schema-sdk');
const { typeTestSuite, relationshipTestSuite } = require('./type-test-suite');

const sdk = new SDK();
sdk.init();
const { readYaml } = sdk;

const makePath = dir =>
Expand Down

0 comments on commit 3fcf140

Please sign in to comment.