WatermelonDB icon indicating copy to clipboard operation
WatermelonDB copied to clipboard

Using with Svelte?

Open frederikhors opened this issue 4 years ago • 15 comments

It's the first time I come here.

I'm a Svelte 3 happy user.

Do you know if there will ever be a way to use this amazing project with Svelte?

Is it React.js only?

frederikhors avatar Sep 17 '20 13:09 frederikhors

No, it's not React.js only. There's no React-specific logic in the core, but you'll probably need some glue code similar to our withObservables. Look at this thread: https://github.com/Nozbe/WatermelonDB/issues/765

radex avatar Sep 17 '20 13:09 radex

I'm tring it right now with a new https://github.com/sveltejs/template app.

I'm using this code:

import { Database } from '@nozbe/watermelondb'
import LokiJSAdapter from '@nozbe/watermelondb/adapters/lokijs'

import schema from './schema'
import Post from 'model/Post'
import Comment from 'model/Comment'

const adapter = new LokiJSAdapter({
  schema,
  useWebWorker: false,
  useIncrementalIndexedDB: true
})

export const database = new Database({
  adapter,
  modelClasses: [Post, Comment],
  actionsEnabled: true
})

the very first error when I'm just importing database is:

Uncaught (in promise) TypeError: Failed to resolve module specifier "fs". Relative references must start with either "/", "./", or "../".

in browser's console.

What can it be?

EDIT: This can be fixed after this: https://github.com/Nozbe/WatermelonDB/issues/823.

frederikhors avatar Sep 18 '20 23:09 frederikhors

So far I can say that I have managed to make it work. Even observables (which in Svelte are very easy to use: https://svelte.dev/docs#Store_contract).

Now the problem is with the declaration of the fields: decorators are creating problems for the bundler (Rollup).

Is there any way to avoid them for now, @radex?

frederikhors avatar Sep 21 '20 13:09 frederikhors

As soon as I have something stable I will push a test project so it will also pass your sifting and we can maybe officially support Svelte! :smile:

frederikhors avatar Sep 21 '20 13:09 frederikhors

Now the problem is with the declaration of the fields: decorators are creating problems for the bundler (Rollup). Is there any way to avoid them for now, @radex?

You can avoid decorators, but it's not been designed for it, so the result isn't pretty. Just take a look at what Watermelon decorators do, they're really simple: https://github.com/Nozbe/WatermelonDB/tree/master/src/decorators

radex avatar Sep 21 '20 14:09 radex

Thanks. I'm trying with Typescript now. I'll let you know in a few moments.

frederikhors avatar Sep 21 '20 14:09 frederikhors

It's working with Typescript. I'll update this issue.

frederikhors avatar Sep 21 '20 17:09 frederikhors

@frederikhors I love svelte, and wanna made something with svelte+watermelondb, mind to share how you made it works? thanks in advanced

fachrihawari avatar Jan 05 '21 13:01 fachrihawari

@frederikhors i am also interesting in this topic. Could you explain and show some code snippets, how you made it works? Does it also works with vite.js (the new bundler for sveltekit) ?

niklasgrewe avatar Apr 12 '21 10:04 niklasgrewe

I got something working or partially working, but it took some workarounds I haven't tried before on other rollup projects. (There's definitely some "duct tape.") Hopefully it keeps working 🤞 and if it does, I'll try to share what worked

floer32 avatar Sep 28 '21 09:09 floer32

Hi @hangtwenty ! I'm also interested in building something with WatermelonDB and Svelte, do you have a public sample repository that I can look at? Is it worth it, in your opinion, to make the two of them cooperate?

trenta3 avatar Aug 26 '22 22:08 trenta3

I'm interested too!

maietta avatar Feb 18 '23 16:02 maietta

I got something working or partially working, but it took some workarounds I haven't tried before on other rollup projects. (There's definitely some "duct tape.") Hopefully it keeps working 🤞 and if it does, I'll try to share what worked

How is it going? @floer32

jgod avatar Aug 07 '23 22:08 jgod

Anyone got a template for WatermelonDB and Svelte Kit integration now in 2023?

alfrednerstu avatar Nov 23 '23 14:11 alfrednerstu

Any template for Svelte(Kit) available in 2024? It's gonna be really interesting with upcoming Svelte 5 runes 😄

bluepuma77 avatar Mar 04 '24 11:03 bluepuma77