Utilizing Think ORM seamlessly across multiple files without the need to repeatedly establish a connection to the

I'm facing a situation where I have numerous models for thinky, and in each file I am required to create a new object for thinky and connect it multiple times due to the high number of models.

var dbconfig = require('../config/config.js')['rethinkdb'];
var thinky = require('thinky')(dbconfig);
var User = require('./user.js');
var type = thinky.type;
var r = thinky.r;

var Feedback = thinky.createModel("Feedback", {
    id: type.string(),
    feel: type.number().required(), // 0 = sad, 1 = happy
    reason: type.string(),
    description: type.string(),
    createdAt: type.date().default(r.now()),
    createdBy: type.string().required()
});

Feedback.ensureIndex("id");

module.exports = Feedback;

I'm seeking advice on how to avoid repeatedly instantiating the variable and creating new connections every time while still being able to organize all these data models in their own separate files.

Answer №1

Your current approach to building and exporting thinky models might not be optimal.

For a comprehensive guide on the recommended architecture, please check out:

Answer №2

Hey there, I've got the answer you've been looking for:

// Check out this code snippet from util/thinky.js
var thinky = require('thinky')({
    // thinky's options
})

module.exports = thinky;

To use it, simply follow these steps:

// Take a look at this example in models/user.js
var thinky = require(__dirname+'/util/thinky.js');
var type = thinky.type;

var User = thinky.createModel("User", {
    id: type.string(),
    name: type.string(),
    age: type.number()
});

module.exports = User;

Best regards, the person in the reflection (You're welcome)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using Vue's transition mode alongside v-for and v-if does not appear to function correctly

When using Vue with Vuetify, I am trying to dynamically change v-cards with the help of animate.css. However, I have run into an issue. The out-in mode is not working in this scenario as both fade-in and fade-out animations occur simultaneously. Is there a ...

JavaScript: Retrieving the names of children within a <div> element

Within my structure setup, there is a tower div with inner elements like this: <div class="tower"> <div class="E0">abc</div> <div class="GU">123</di </div> The challenge I am facing is that I need to access the in ...

What is the process for dynamically populating a select dropdown based on the selection made in another select dropdown?

I need to dynamically populate the second select box based on the option selected in the first select box. Here's what I have tried so far, but it doesn't seem to be working as expected. HTML: <form id="step1"> <p> Creat ...

The plugin "react" encountered a conflict while trying to sync with both the "package.json" and the "BaseConfig" files

Whenever I open Terminal in my react folder and try to start the react app using npm start, I always end up encountering an error on the browser. The error message states that the "react" plugin is conflicting between two paths: "package.json » eslint ...

Can I use a single component for all routes in NextJS?

Just starting out with NextJS and facing a simple problem: I'm wondering if it's possible to achieve the following setup using NextJS // with react-router-dom <Router> <> <Header /> <Switch> & ...

Unable to execute the grunt command in Windows Command Prompt

After setting up Node.js on my Windows machine, I managed to run the 'node -v' and 'npm -v' commands successfully. I also used the 'npm -g grunt-cli' command to install Grunt globally. However, when I tried to use the Grunt co ...

The mysterious anomaly in Vue.js

I am attempting to assign a data object named types upon receiving a response in the ready() method. This is what I have: export default { data () { return { types: null } }, ready () { TypeService.showAll(1) .then(functio ...

Can you explain why the .js code is causing such high CPU usage in popular video players?

Why is the .js code running continuously even when the video is paused and there is no user interaction? I observed this phenomenon on a Windows 10 Atom Tablet, particularly when in energy-saving mode. The CPU usage for video playback and decoding almost ...

modify the color of a particular div element in real-time

I am looking to dynamically change the color of the divs based on values from my database. Here is what I have so far: Database: shape id is_success id1 0 id2 1 id3 0 id4 1 <div class="container" style="background: black; widt ...

I encountered an issue while trying to install the Angular and npm package

After updating Nodejs, I decided to delete the old version of Angular and install a new one. However, I encountered an error during the installation process. I followed a series of steps in an attempt to resolve the issue, but ended up encountering a diff ...

Geckodriver in Firefox fails to initialize on Ubuntu Server

I am currently running Ubuntu Server 19.04 64bit and have the following packages installed: nodejs v10.16.0 Mozilla Firefox 68.0.1 In my node.js project, I have installed selenium-webdriver ^4.0.0-alpha.4. Additionally, I installed geckodriver 0.24.0 u ...

Enhanced capabilities for the <input> element

I am seeking to develop a component that incorporates an <input> tag and offers additional functionalities like a clear text value "X" icon or any other customized actions and markup, all while maintaining the same event bindings ((click), (keyup), e ...

Occasionally, the view fails to update following an $http request

Although this question has been posed multiple times before, none of the solutions seem to be effective for my issue. Controller app.controller('HomeController', function ($scope, $timeout, $http) { $scope.eventData = { heading: ...

Animating with JavaScript

I have a members button on my website that triggers a dropdown animation when clicked. However, the issue is that the animation occurs every time a page is loaded, even if the button is not clicked. I want the dropdown to only open when the button is click ...

Remove a specific date entry from the database using VUE JS and Axios

I am encountering a challenge with Vuejs as I work on a To-Do list. The tasks need to be stored in a MySQL database, and I also want the ability to delete tasks. While I have succeeded in importing and creating data, I'm facing difficulty in deleting ...

Exploring the Process of Passing Data with MongoDB's find().count()

Currently, I am working on developing a straightforward Express application with MongoDB. I have set up an endpoint that searches for a user by their name and then displays the relevant data stored in the database on the page. While attempting to incorpora ...

Tips for creating Selenium code to verify the asterisk symbol

Looking to create Selenium code for validating the presence of asterisks with mandatory fields such as First Name*, Last Name*, Enter Address*, and Enter Phone Number*. The validation needs to confirm the asterisk appears after each field name. Currently ...

The Express website can be viewed on select devices while unavailable on others

When trying to access my website (anarchychess.xyz), I am able to do so on my PC and laptop, but encounter an error on other devices, including mobile phones and a friend's PC. The browser displays the message: This site can't be reached anarch ...

Vue - Passing a parent's ref to a child component as a prop

Is there a way to pass the current component's reference to a child component in Vue.js? <template> <div class="screen" ref="screen"> <child-component :screenRef="screenRef"> </child-component> </div ...

Issue encountered during the download of specific NPM packages

When attempting to download specific packages like node-portaudio and image-to-ascii, I encountered an error. Interestingly, other packages are downloading without any issues. The error message reads as follows: > [email protected] install /home/ex-m ...