Coder Social home page Coder Social logo

How do I use `insertFrom`? about beam HOT 10 CLOSED

3noch avatar 3noch commented on August 16, 2024
How do I use `insertFrom`?

from beam.

Comments (10)

tathougies avatar tathougies commented on August 16, 2024

Should be something like

data TableT f =
  Table { _column1 :: C f Int
        , _column2 :: C f Text }
  deriving Generic
deriving instance Beamable TableT

instance Table TableT ...

data Table2T f =
  Table2 { _column3 :: C f Text }
  deriving Generic
deriving instance Beamable Table2T

instance Table Table2T ...

data Db entity =
  Db { _tbl1 :: entity (TableEntity TableT)
     , _tbl2 :: entity (TableEntity Table2) }
  deriving Generic
instance Database Db

db :: DatabaseSettings be Db
db = autoDbSettings

insert (_tbl1 db) $
insertFrom $ select $
fmap (\(Table2 column3) -> Tablet (charLength_ column3) column3) $
all_ (_tbl2 db)

from beam.

3noch avatar 3noch commented on August 16, 2024

In my case _tbl1 has a primary key so I wanted to use default_ to not specify that. However, you can't select DEFAULT on the RHS.

from beam.

tathougies avatar tathougies commented on August 16, 2024

@3noch That's right. That's a SQL limitation. Are you asking about using projections (like INSERT INTO tbl(field1, field2,...)) in insert? If so, you're right, there's no current way to do this, but we can add this in if you need it

from beam.

3noch avatar 3noch commented on August 16, 2024

Yes that must be what I'm looking for, a way to do a projection on the FROM SELECT so that I can specify some (but not all) of the target table columns.

from beam.

tathougies avatar tathougies commented on August 16, 2024

I think we could potentially do a syntax along the lines of:

insertFields (_tbl1 db) (\tbl -> _field1 tbl) $ insertFrom $ select $ fmap (\(Table2 column3) -> column3) $ all_ (_tbl2 db)

So basically you specify the tbl to insert into, a projection from that table, and then we'd rework the types a bit to allow insertFrom to return arbitrary projections instead of tables only. Would that work?

from beam.

luigy avatar luigy commented on August 16, 2024

in the case of falling back to default isn't that what Auto Nothing is for or does that not help in this case?

from beam.

tathougies avatar tathougies commented on August 16, 2024

Auto Nothing is a beam convenience. It's unrepresentable in general in SQL

from beam.

3noch avatar 3noch commented on August 16, 2024

@tathougies I have very little opinion or wisdom to offer regarding how you represent this in Beam. I don't quite see how your example would work but I don't think that means much. In particular, the trick is saying how fields from two different tables would correspond. That doesn't seem trivial to me.

from beam.

tathougies avatar tathougies commented on August 16, 2024

Many months later, but insertOnly is now a thing. From the docs:

runInsert $
  insertOnly (invoice chinookDb)
             (\i -> ( invoiceCustomer i, invoiceDate i, invoiceBillingAddress i, invoiceTotal i ) ) $
  insertFrom $ do
    c <- all_ (customer chinookDb)

    -- We'll just charge each customer $10 to be mean!
    pure (primaryKey c, currentTimestamp_, customerAddress c, as_ @Scientific $ val_ 10)

inserts values only into the customer, date, billing address, and total columns.

from beam.

3noch avatar 3noch commented on August 16, 2024

Sweet!

from beam.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.