Comments (4)
Correct. LookupCodes.find() has returned a class object (representing our model) but when calling the Update() method of our model object we could instead get an object representing outcome i.e. rowsAffected.
Code wise I would approach use as follows :
const code = await LookupCodes.find(1);
const {rowsAffected} = await code.update({ code: 'xxx' });
if (rowsAffected===1) { ....}
The above reflect one change, but related to this is minor issue (and this might be a non-issue and considered working as intended) relating to the return value when directly using class methods for update(), delete()
LookupCodes.where("id", 1).update({code_id: "X"})
LookupCodes.where("id", 1).delete();
These do return rowsAffected but they do so as a class object
LookupCodes { affectedRows: 1, lastInsertId: 0 }
Ideally it would be good if results were return as object { affectedRows: 1, lastInsertId: 0 }
allowing use case
const {rowsAffected} = await LookupCodes.where("id", 1).update({code_id: "X"})
Prior to finding "denodb" I was using "mysql" directly and using its return values in this way.
At the moment when class methods directly I require the following ..
const updateResult = await LookupCodeModel.editLookupCode(id,data);
const affectedRows = updateResult["affectedRows"]
if (rowsAffected===1) { ....}
Part of this was my own learning when understanding the return value. It's workable but little more involved. I guess also that return value of class object LookupCode when using this methods directly is misaligned as they don't represent structure of the model.
Hope this makes sense. Happy to clarify further if needed.
edit: wording
from denodb.
Hey,
Thanks for raising this question, it's interesting.
So let me quickly explain my first way of thinking. Initially, I expected this:
const code = await LookupCodes.find(1);
const updatedCode = await code.update({ code: 'xxx' });
But, from what I understand, you would need the following:
const resultsFromUpdate = await code.update({ code: 'xxx' });
// resultsFromUpdate would then have "rowAffected" etc.
I think your approach is better and more in sync with classes behaviour i.e. Model.update
, Model.delete
which won't return the model itself but the results from the query.
Just to make sure, you wish to use things the way I wrote it with resultsFromUpdate
right?
from denodb.
Thanks for confirming, I hope this little change will make things easier for you!
It should now be working with #59. Just remember to reload, deno run --reload ...
and you should be good to go.
If not, just reopen the issue okay?
See you around :)
from denodb.
Hi @eveningkid ,
Thanks again for this change. The fix provides a better result. One follow up question is whether it should return Model Class object type or just plain object type. This is minor really.
Currently value is returned as LookupCodes { affectedRows: 1, lastInsertId: 0 }
Which makes it seem likes it a class object of LookupCodes but it's not really. Do you think it's possible or worthwhile to return as base object so data elements can be referenced directly.
const {rowsAffected} = await resultFind.delete();
PS. I can't re-open, not that I wanted to before asking question. Let me know if you want a new issue if you want to track change - if worthwhile doing.
from denodb.
Related Issues (20)
- Query always returns `undefined`. HOT 3
- Crashing when deployed to Deno Deploy HOT 2
- TLS connection failed with message: invalid peer certificate contents / PostgresError: no pg_hba.conf entry for host HOT 5
- Foreign key constraint name defaults to the name of the column
- Serialized Model option rather than classes?
- Can't import the latest version 1.0.40 HOT 5
- How to use denodb to connect to multiple postgresql databases having same models
- [Feature] Support SQL `SELECT DISTINCT`
- insert or update
- hash module break! HOT 4
- sqlite v3.1.3 not working on Windows
- Postgres Foreign Key is created with integer type by default HOT 2
- Timestamps
- Postgres connector makes the script unable to exit normally HOT 1
- How to set logging? HOT 2
- Postgresql: Timestamp fields adding hours each time records are updated
- Multiple Primary Key Exists HOT 1
- Some problems in model.ts
- A dependency is broken HOT 15
- Module not found "https://deno.land/std/node/events.ts". HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from denodb.