typeorm / react-native-example

Example project to demonstrate TypeORM with React Native.
65 stars 29 forks source link

Error AlreadyHasActiveConnectionError:Cannot create a new connection named 'default' #6

Open ahmadtlz opened 4 years ago

ahmadtlz commented 4 years ago

hi , the first time i reload the app (ctrl+m ==> reload )is work perfect but when i exit the app and again run it the warning popup i wanna close the connection but i don't no how :( this is my code in react-native App

export default class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      savedPost: false, progress: 'Post is being saved', loadedPost: [],
      textDescription: '',
      title: '',
      category: '',
      birthDate: '1998-8-02',
      author: ''
    };
    this.runDemo();
  }

  connect() {
    return createConnection({
      type: 'react-native',
      database: 'test',
      location: 'default',
      logging: ['error', 'query', 'schema'],
      synchronize: true,
      entities: [
        Author,
        Category,
        Post
      ]
    });``

  async runDemo() {
    try {
      // SQLite.openDatabase({ name: 'my.db', location: 'Library' }, this.successCB, this.errorCB);
      await this.connect();

      const category1 = new Category();
      category1.name = "TypeScript";

      const category2 = new Category();
      category2.name = "Programming";

      const author = new Author();
      author.name = "Person";

      const post = new Post();
      post.title = "Control flow based type analysis";
      post.text = "TypeScript 2.0 implements a control flow-based type analysis for local  variables and parameters.";
      post.categories = [category1, category2];
      post.author = author;

      const postRepository = getRepository(Post);
      // await postRepository.save(post);

      console.log("Post has been saved");
      this.setState({
        progress: "Post has been saved"
      });

      const loadedPost = await postRepository.find({ id: Between('10', '12') });

      // createQueryBuilder('post')
      // .innerJoinAndSelect("post.author", "author")
      // .innerJoinAndSelect('post.categories', 'categories')
      // .where('post.id = :id', { id: post.id })
      // .getOne();

      if (loadedPost) {
        console.log("Post has been loaded: ", loadedPost);
        this.setState({
          loadedPost: loadedPost
        });
      }
    } catch (err) {
console.log(err)
  }

1

2

3

arantessergio commented 4 years ago

Hey @dking01, I made a workaround, verifying if the connection exists and if it's connected before create a new one.



                const exists = manager.has('default')

                let conn: Connection

                if (exists) {
                    const defaultConn = manager.get('default')

                    if (defaultConn) {
                        conn = defaultConn

                        if (!conn.isConnected) {
                            await conn.connect()
                        }
                    }
                } else {
                    conn = manager.create({
                        name: 'default',
                        type: 'react-native',
                        database: 'myappdb',
                        location: 'default',
                        logging: ['error', 'query', 'schema'],
                        synchronize: true,
                        entities: [
                            Entity
                        ]
                    })

                    await conn.connect()