Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use dexie in nuxt3? #93

Open
gusdn0828 opened this issue Jan 28, 2022 · 1 comment
Open

How to use dexie in nuxt3? #93

gusdn0828 opened this issue Jan 28, 2022 · 1 comment

Comments

@gusdn0828
Copy link

I wrote db.ts and inserted it in composables folder.
and, I adjusted the dexie in nuxt3, but i got the error like this.

I imported the module itself. it worked well. I think that composable way is bug or something wrong.

Below is source code of composable way.
Please help me!

Uncaught (in promise) SyntaxError: Identifier 'MySubClassedDexie' has already been declared
  • db.ts
// db.ts
import Dexie, { Table } from "dexie";

export interface Friend {
  id?: number;
  name: string;
  age: number;
}

export class MySubClassedDexie extends Dexie {
  // 'friends' is added by dexie when declaring the stores()
  // We just tell the typing system this is the case
  friends!: Table<Friend>;

  constructor() {
    super("myDatabase");
    this.version(1).stores({
      friends: "++id, name, age", // Primary key and indexed props
    });
  }
}

const db = new MySubClassedDexie();
export default db;
  • App.vue
<template>
  <fieldset>
    <legend>Add new friend</legend>
    <label>
      Name:
      <input v-model="friendName" type="text" />
    </label>
    <br />
    <label>
      Age:
      <input v-model="friendAge" type="number" />
    </label>
    <br />
    <button @click="addFriend">Add Friend</button>
    <p>{{ status }}</p>
  </fieldset>
</template>
<script setup lang="ts">
import { ref } from "vue";

const status = ref("");
const friendName = ref("");
const friendAge = ref(20);
const addFriend = async () => {
  try {
    // Add the new friend!
    const id = await db.friends.add({
      name: friendName.value,
      age: friendAge.value,
    });

    status.value = `Friend ${friendName.value}
          successfully added. Got id ${id}`;

    // Reset form:
    friendName.value = "";
    friendAge.value = 10;
  } catch (error) {
    status.value = `Failed to add
          ${friendName.value}: ${error}`;
  }
};
</script>
@asadmalik
Copy link

asadmalik commented Oct 3, 2023

Use Dexie related code in onMounted callback.

`<script setup>
import Dexie from 'dexie';

onMounted(async () => {

const db = new Dexie("MyDatabase");
db.version(1).stores({
    friends: '++id, name, age', // Primary key and indexed props
});

const id = await db.friends.add({
    name: 'John Smith__Mounted',
    age: '22',
});

console.log(`Friend successfully added. Got id ${id}`);

});

</script>`

Hope this helps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants