Sequelize Tables (Beginner Question) Node.js - node.js

Ok so I just started looking into databases yesterday
index.js: https://hatebin.com/pvqnubsrrs
database.js: https://hatebin.com/csgvmvolfz
I just want it to insert into the DB,
how would I accomplish that?
Someone told me it's creating a new DB, but when I run the command it says the table doesn't exist
The error says:
Error: SequelizeDatabaseError: SQLITE_ERROR: no such table: PointsSystems
However, I cannot find a spot in the code where I use PointsSystems, I only use PointsSystem, without the s
Edit: Thank you SigFried for making this more readable.

The error says PointSystems because, by default, Sequelize will pluralize the name you gave to the table. Your code looks O.K, the only missing part and the solution to your problem is the await sequelize.sync({ force: true })}. I made a little example to show you how it's done in a very basic way.
You should be able to find out why the sequelize.sync() methods is needed if you read this.

Related

Jooq database/schema name mapping

I use jooq to generate objects against a local database, but when running "for real" later in production the actual databases will have different names. To remedy this I use the <outputSchemaToDefault>true</outputSchemaToDefault> config option (maven).
At the same time, we have multiple databases (schemas), and are using a connection pool to the server like "jdbc:mysql://localhost:3306/" (without specifying a database here).
How do I tell jooq which database to use when running queries?
I have tried all config I can think of:
new Settings()
.withRenderSchema(true) // true/false seems to make no difference.
.withRenderCatalog(true) // true/false seems to make no difference.
.withRenderMapping(new RenderMapping()
.withDefaultSchema("my_database") // Seems to have no effect.
// The above 3 configs always give me an error saying "no database selected".
// Adding this gives me 'my_database.my_table' does not exist - while it actually does.
.withSchemata(new MappedSchema()
.withInputExpression(Pattern.compile(".*"))
.withOutput("my_database")
));
I have also tried using a database/schema name, as in not configuring outputSchemaToDefault. But then, adding the MappedSchema code above, but that gives me errors with "'my_databasemy_database.my_table' does not exist", which is correct. I have no clue why that code gives me the database/schema name twice?
Edit:
When jooq tells me that the db.table does not exist, if I put a break point in a good place and get the sql from jooq and run exactly that against my database it does work. But jooq fails to run it.
Also, I'm using version 3.15.3 of jooq.
I solved it. Instead of using .withInputExpression(Pattern.compile(".*")), it seems to work with .withInput("").
I'm still not sure why it works, or if this is the "correct" way of solving it. But at least it is a way forward.
No clue why using the pattern, I got the name twice though. But that one I'll leave alone.

Unable to use Code data type in Mongoose Schema

I wanted to store a data type of Code in my MongoDB database per the docs.
The issue is that when I use Mongoose to specify this type, I get an error:
ReferenceError: Code is not defined
And yes, Mongoose's doc's don't have a type of Code.
Say I want to save Javascript code in my MongoDB database, how could I do so?
For more insight into why I am doing this: I want to save Puppeteer scripts in the database, and using the FS module add them to a file, execute them and send the results as a response object back to the client.
Right now, the code is saved as String, which seems to cause execution errors.
For more information, please don't hesitate to ask.
Thank you,

mongoose findById would not work with correct Id

I was creating a database storing user information. Previously, the system worked well, after I performed npm update of mongoose, the system failed and I dont know why. The following is the error message I encountered.
CastError: Cast to ObjectId failed for value "xxxxxxxxxxxxxxx" at path "_id" for model "User"
Then, I write some simple testing code to test if the database is connected successfully. For example:
User.findOne({firstName: "David"}, function(err, user){console.log(user.lastName)});
The above worked perfectly.
I then found the corresponding Id from the database directly and create variable like:
var testId = "abcde"
and tried the following code:
User.findById(testId, function(err, user){console.log(user.firstName)});
Then it just output the error messages.
I have tried mongoose.Types.ObjectId.isValid('abcde') (abcde is just example) and it returned true. Therefore, I really have no ideas why it did not work...is it a mongoose bug?
Please help if you know the answer, thank you very much!
I just solved the problem.
As I mentioned, the findById function did not work after I updated mongoose. I was struggling on the code for few days and when I woke up this morning, I suddenly thought of upgrading my node.js.
That worked.
After upgrading node.js from v4.x.x to latest version, everything worked fine : )
This taught me a lesson that I should not upgrade a single module alone and care of compatibility issue.

Collection name in Mongoose

Why would a database named 'blog' not allow a record insert and also give no return error and why would a database named 'blogs' allow a record inserts and return errors?
I just spent several hours going through all my code thinking I did something wrong. I have written many mongoose connected apps but when using the following it would return success but not insert the record and return no error as to why:
mongooose.connect('mongodb://localhost:27017/blog');
After banging my head against a wall for a bit I decided to change the database name:
mongooose.connect('mongodb://localhost:27017/blogs');
It works! But why would this name convention matter? I can't find anything in the documentation for MongoDB or Mongoosejs.
So I'm fairly certain mongodb doesn't care about database name "blog" vs "blogs". However, do note that mongoose has the questionably-helpful feature of silently queueing up operations while the database connection is still not established and then firing them off if/when the database connection is ready. That could be causing your confusion. To test that theory, pass a callback to mongoose.connect and put a console.log in the callback so you know exactly when the connection is ready.

Spelling error issue in routes while scaffolding Compound js

I'm new with node and compound. While i tried to scaffold
compound g crud leaveApplication leave_code:string description:string applicable:string carry_forward:boolean limit_type:boolean lop:boolean od:boolean co:boolean leave_revision:boolean active:boolean
I was getting some errors, then i tried
compound g crud leave code:string description:string applicable:string cForward:boolean limit:boolean lop:boolean od:boolean co:boolean leave_revision:boolean active:boolean
But the error now occurred was in the name of routes
leaves GET /leaves.:format? leaves#index
leaves POST /leaves.:format? leaves#create
new_leafe GET /leaves/new.:format? leaves#new
edit_leafe GET /leaves/:id/edit.:format? leaves#edit
leafe DELETE /leaves/:id.:format? leaves#destroy
leafe PUT /leaves/:id.:format? leaves#update
leafe GET /leaves/:id.:format? leaves#show
These were the routes i was getting.
Why is that so?
it looks like compound is turning your model name into plural (=leaves) and then, instead of using your provided singular name, turning this plural name back, resulting in "leaf".
Does this make any sense? ;-) Or did I get you question wrong?
If you could provide the "some errors" and the full error message, it would be easier to help ;)
Btw, I just experienced that using camel case for models doesn't seem to be a good idea with compound.js.
It's mangeling the camelcase in some places (e.g. inside the controllers), but in others not (schema.js) creating a application with some errors...

Resources