Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Do I Properly Initialize RxDB in a Vue App with global

Tags:

vue.js

rxdb

I'm trying out RxDB for the first time in a Vue 3 app, and getting started is a bit more tricky than I expected (and it's probably my fault).

I installed RxDB with NPM in my project ("rxdb": "^9.20.0"), then I did this in my main.js file:

import { createRxDatabase, RxDatabase } from 'rxdb'
 
const db = await createRxDatabase({
  name: 'MyApp', adapter: 'idb'
})

console.dir(db)

I then get this error:

Uncaught ReferenceError: global is not defined

So I check the docs and found this that I need a global polyfill like this:

(window as any).global = window;
(window as any).process = {
    env: { DEBUG: undefined },
};

That looks to be an example using Typescript from Angular. When I try it in Vue, I then get this error:

Invalid assignment target
3  |  import { createRxDatabase, RxDatabase } from 'rxdb'
4  |  
5  |  const db = await createRxDatabase({
   |             ^
6  |    name: 'Avid', adapter: 'idb'
7  |  })

10 minutes in and I'm already a mess. 😅 Can someone show me how to properly start up RxDB in a Vue app?

like image 722
Clifton Labrum Avatar asked Oct 23 '25 14:10

Clifton Labrum


1 Answers

You can't use await in root. It must be inside async function.

let db

async function initDb() {
  db = await createRxDatabase(...)
}

initDb()
like image 60
AHOYAHOY Avatar answered Oct 26 '25 03:10

AHOYAHOY



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!