aws-amplify / amplify-js

A declarative JavaScript library for application development using cloud services.
https://docs.amplify.aws/lib/q/platform/js
Apache License 2.0
9.42k stars 2.12k forks source link

Datastore getDefaultAdapter fails, window is not defined #6204

Closed dlucatero closed 3 years ago

dlucatero commented 4 years ago

Describe the bug I have set up Datastore in a worker thread using ThreadsJS, any function call to DataStore (save, query) fails as window is not defined here:

https://github.com/aws-amplify/amplify-js/blob/38350d7e581d4341ca569ecea57988ff38779faf/packages/datastore/src/storage/adapter/getDefaultAdapter/index.ts#L4

globalThis.indexedDB is available in this context, would there be any concerns to using globalThis?

Code Snippet Worker setup:

import { spawn, Thread, Worker } from 'threads';

export async function setupWorker() {
    const datastore = await spawn(new Worker('@/workers/worker.ts'));
    datastore.setup();
}

worker.ts

import { DataStore } from '@aws-amplify/datastore'
import { Amplify } from '@aws-amplify/core'
import awsconfig from '@/aws-exports'
import { Post } from '@/models/index'

Amplify.configure(awsconfig)
export async function setup()
{
  await DataStore.save(
    new Post({
      title: "My First Post",
      rating: 10,
      status: PostStatus.ACTIVE
    })
  );
}

Screenshots amplifyerror

Environment ``` System: OS: Windows 10 10.0.17763 CPU: (8) x64 Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz Memory: 17.78 GB / 31.88 GB Binaries: Node: 10.15.0 - C:\Program Files\nodejs\node.EXE Yarn: 1.22.0 - C:\Program Files (x86)\Yarn\bin\yarn.CMD npm: 6.12.0 - C:\Program Files\nodejs\npm.CMD Browsers: Edge: 44.17763.831.0 Internet Explorer: 11.0.17763.771 npmPackages: @aws-amplify/api: latest => 3.1.16 @aws-amplify/core: latest => 3.4.0 @aws-amplify/datastore: latest => 2.2.4 @aws-amplify/pubsub: latest => 3.0.17 @aws-amplify/storage: latest => 3.2.7 @babel/core: ^7.10.3 => 7.10.3 @types/jest: ^26.0.0 => 26.0.0 @types/jquery: ^3.5.0 => 3.5.0 @types/lodash: ^4.14.156 => 4.14.156 @types/luxon: ^1.24.1 => 1.24.1 @typescript-eslint/eslint-plugin: ^3.4.0 => 3.4.0 @typescript-eslint/parser: ^3.4.0 => 3.4.0 @vue/cli-plugin-babel: ^4.4.5 => 4.4.5 @vue/cli-plugin-e2e-nightwatch: ^4.4.5 => 4.4.5 @vue/cli-plugin-eslint: ^4.4.5 => 4.4.5 @vue/cli-plugin-router: ^4.4.5 => 4.4.5 @vue/cli-plugin-typescript: ^4.4.5 => 4.4.5 @vue/cli-plugin-unit-jest: ^4.4.5 => 4.4.5 @vue/cli-plugin-vuex: ^4.4.5 => 4.4.5 @vue/cli-service: ^4.4.5 => 4.4.5 @vue/eslint-config-airbnb: ^5.0.2 => 5.0.2 @vue/eslint-config-typescript: ^5.0.2 => 5.0.2 @vue/test-utils: 1.0.0-beta.29 => 1.0.0-beta.29 babel-core: 7.0.0-bridge.0 => 7.0.0-bridge.0 babel-eslint: ^10.1.0 => 10.1.0 backtrace-js: ^0.0.11 => 0.0.11 core-js: ^3.6.5 => 3.6.5 eslint: ^6.8.0 => 6.8.0 eslint-plugin-vue: ^6.2.2 => 6.2.2 exoskeleton: ^0.7.0 => 0.7.0 hooper: ^0.3.4 => 0.3.4 jquery: ^3.5.1 => 3.5.1 lint-staged: ^10.2.11 => 10.2.11 lodash: ^4.17.11 => 4.17.15 luxon: ^1.24.1 => 1.24.1 moment: ^2.27.0 => 2.27.0 mousetrap: ^1.6.5 => 1.6.5 node-sass: ^4.14.1 => 4.14.1 observable-fns: ^0.5.1 => 0.5.1 popper.js: ^1.16.1 => 1.16.1 portal-vue: ^2.1.7 => 2.1.7 sass-loader: ^8.0.2 => 8.0.2 style-resources-loader: ^1.3.2 => 1.3.3 threads: ^1.6.2 => 1.6.2 threads-plugin: ^1.3.2 => 1.3.2 ts-jest: ^24.3.0 => 24.3.0 typescript: ^3.9.5 => 3.9.5 underscore: ^1.10.2 => 1.10.2 v-tooltip: ^2.0.3 => 2.0.3 vue: ^2.6.10 => 2.6.11 vue-cli-plugin-style-resources-loader: ^0.1.4 => 0.1.4 vue-force-next-tick: ^1.1.0 => 1.1.0 vue-i18n: ^8.18.2 => 8.18.2 vue-mq: ^1.0.1 => 1.0.1 vue-router: ^3.3.4 => 3.3.4 vue-slider-component: ^3.1.5 => 3.1.5 vue-svg-loader: ^0.12.0 => 0.12.0 vue-template-compiler: ^2.5.21 => 2.6.11 vuex: ^3.4.0 => 3.4.0 npmGlobalPackages: @aws-amplify/cli: 4.21.3 @sentry/cli: 1.48.0 backtrace-morgue: 1.15.3 grunt-cli: 1.3.2 npm: 6.12.0 ```
dlucatero commented 4 years ago

Testing with the following code, this solves the issue with the adapter

var getDefaultAdapter = function () {
    if ((globalThis && globalThis.indexedDB) || (window && window.indexedDB)) {
        return require('../indexeddb').default;
    }
    if (process && process.env) {
        throw new Error('Node is not supported');
    }
};`

Also bumped into the same issue in here, other places might pop up as my implementation progresses, so maybe more changes would be needed to support this.

https://github.com/aws-amplify/amplify-js/blob/c6e8162bd9a148453f3cb1f58960ed3a59a50a0b/packages/core/src/Util/Reachability.ts#L14

manueliglesias commented 4 years ago

Thanks for the report and the details.

I am labeling this as a feature-request so we can look into better supporting Workers across all the library

iartemiev commented 3 years ago

Closing this issue, as worker support was added in @aws-amplify/datastore@2.6.0

github-actions[bot] commented 2 years ago

This issue has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs.

Looking for a help forum? We recommend joining the Amplify Community Discord server *-help channels or Discussions for those types of questions.