Skip to content

Commit

Permalink
fix: add error for when schema is not initialized (aws-amplify#9874)
Browse files Browse the repository at this point in the history
* fix: add error for when schema is not initialized

* chore: change warning to error

* refactor: remove schemaInitialized variable
  • Loading branch information
dpilch authored May 12, 2022
1 parent a9ae27f commit a63f0ee
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
16 changes: 16 additions & 0 deletions packages/datastore/__tests__/DataStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,22 @@ describe('DataStore tests', () => {
({ initSchema, DataStore } = require('../src/datastore/datastore'));
});

test('error on schema not initialized on start', async () => {
const errorLog = jest.spyOn(console, 'error');
const errorRegex = /Schema is not initialized/;
await expect(DataStore.start()).rejects.toThrow(errorRegex);

expect(errorLog).toHaveBeenCalledWith(expect.stringMatching(errorRegex));
});

test('error on schema not initialized on clear', async () => {
const errorLog = jest.spyOn(console, 'error');
const errorRegex = /Schema is not initialized/;
await expect(DataStore.clear()).rejects.toThrow(errorRegex);

expect(errorLog).toHaveBeenCalledWith(expect.stringMatching(errorRegex));
});

describe('initSchema tests', () => {
test('Model class is created', () => {
const classes = initSchema(testSchema());
Expand Down
16 changes: 16 additions & 0 deletions packages/datastore/src/datastore/datastore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,20 @@ const initSchema = (userSchema: Schema) => {
return userClasses;
};

/* Checks if the schema has been initialized by initSchema().
*
* Call this function before accessing schema.
* Currently this only needs to be called in start() and clear() because all other functions will call start first.
*/
const checkSchemaInitialized = () => {
if (schema === undefined) {
const message =
'Schema is not initialized. DataStore will not function as expected. This could happen if you have multiple versions of DataStore installed. Please see https://docs.amplify.aws/lib/troubleshooting/upgrading/q/platform/js/#check-for-duplicate-versions';
logger.error(message);
throw new Error(message);
}
};

const createTypeClasses: (
namespace: SchemaNamespace
) => TypeConstructorMap = namespace => {
Expand Down Expand Up @@ -729,6 +743,7 @@ class DataStore {

await this.storage.init();

checkSchemaInitialized();
await checkSchemaVersion(this.storage, schema.version);

const { aws_appsync_graphqlEndpoint } = this.amplifyConfig;
Expand Down Expand Up @@ -1384,6 +1399,7 @@ class DataStore {
};

clear = async function clear() {
checkSchemaInitialized();
if (this.storage === undefined) {
// connect to storage so that it can be cleared without fully starting DataStore
this.storage = new Storage(
Expand Down

0 comments on commit a63f0ee

Please sign in to comment.