Comments (8)
So, fields
would either be an array with type Column
s or Expression
s. These are actually not exported yet so Mammoth would need to export this.
Something like the below may work then. Is that what you're looking for?
function fetch<T extends Expression<any, boolean, string>>(db: Db, id: string, ...fields: T[]) {
return db.select(
db.user.name,
db.user.email,
...fields,
).from(db.user).where(db.user.id.eq(id));
}
[Sure, issues is fine. Then at least people can read along if they are interested.]
from mammoth.
Yeah, I think that's roughly what I'm looking for.
But is there a way to ensure the specified fields
are from the correct table?
from mammoth.
In your case, in your fetch function example above, I think you could use a type of Column<string, 'user', any, boolean, boolean, any>
to limit only columns from the user table.
In Mammoth there is no constraint on which table you can select in the select fn. This is a bit difficult to achieve because of Mammoth's goal to stick to SQL as close as possible: the select() is before the from() so we cannot know which columns will be ok to select.
There is an idea to at least change the type of the column if it's not in any of the from items e.g. change type to never if it will cause an issue (or change the whole result set to never, even better I guess). This is similar to how columns are automatically marked as nullable if they are left joined.
from mammoth.
That works for me. Thanks!
In Mammoth there is no constraint on which table you can select in the select fn. This is a bit difficult to achieve because of Mammoth's goal to stick to SQL as close as possible: the select() is before the from() so we cannot know which columns will be ok to select.
Putting .from
first might not be a bad idea. There's already some precedent in LINQ
// Specify the data source.
int[] scores = new int[] { 97, 92, 81, 60 };
// Define the query expression.
IEnumerable<int> scoreQuery =
from score in scores
where score > 80
select score;
I believe they chose that order for better auto-complete in the IDE. That order also matches the logical order of operations when evaluating a query.
I understand the idea of wanting to match SQL closely, but Mammoth's other goal is static type safety, so maybe the tradeoff is worth it?
from mammoth.
Sticking close to SQL is considered more important than being complete type safe by Mammoth. We want to avoid creating our own SQL dialect and have newcomers learn a new abstraction. I touched this briefly at https://nullbyt.es/a-new-typescript-postgres-query-builder/.
In any case, I'll get a new type in soon so you can have your own columns specified from the outside (in your fields example).
from mammoth.
How about an additional selectFrom
method to get the best of both worlds?
from mammoth.
Well, it won't cover all use cases, as you might also want to join with and select, etc.
For now, anything new in addition to SQL I leave up to user-land e.g. a more ORM-ish library. Maybe in the future when Mammoth is more complete we can create more safety by introducing these new ways in the library itself.
from mammoth.
It's now easier to write your own selectFromX
, see #218. This should also provide a solution to the original request.
from mammoth.
Related Issues (20)
- ON CONSTRAINT name cannot be a parameter token HOT 1
- select where not like HOT 3
- Next package publication? HOT 5
- Support empty strings for DEFAULT HOT 2
- How to declare `IDENTITY` columns? HOT 2
- orderBy alias HOT 7
- Alias coalesce column HOT 4
- Automatically resolving schema and relationships directly from the database HOT 2
- Temporal tables
- Mixing `raw` with `Expression` values.
- `Array<Token>` is assignable to `Token`
- Way to associate additional metadata with schema?
- Support custom types
- Generated SQL for WHERE clause doesn't match AST nesting HOT 1
- How can we help Mammoth 2.x move forward?
- guidance for Date objects
- How to configure db.count() to return a `number` type?
- Query inside jsonb
- CASE statements don't include the `CASE` and `END` keywords
- Option for `.as()` output to be wrapped in double-quotes? HOT 2
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 mammoth.