profile
viewpoint

amite/angular-testing 1

learning how to test with angular and karma

amite/bootup-laravel 1

base project for laravel

amite/Cards-In-The-Cloud 1

The easy way to send personalized e-cards to your friends and family for free

amite/advanced-react 0

React Server Side Rendering with Express + Custom Redux + Unit Testing with Jest

amite/Amanda 0

JSON Schema validator

issue commentgajus/slonik

How do I insert multiple records in my database using Slonik?

I did manage to figure it out. Just leaving the answer here for posterity: https://stackoverflow.com/questions/64585026/how-do-i-insert-multiple-records-into-my-postgres-database-using-slonik/64591158#64591158

amite

comment created time in 3 days

issue commentgajus/slonik

How do I insert multiple records in my database using Slonik?

@gajus I tried this example. But I can't get it to run. What does INSERT INTO (foo, bar, baz) mean? Are foo, bar, baz tables? I am really sorry. This is really confusing. How do I insert into my users table?

amite

comment created time in 3 days

issue openedgajus/slonik

How do I insert multiple records in my database using Slonik?

I am a front end developer and this is my first time using Slonik.

I want to know how I can make this query dynamic by inserting the data (which is hardcoded) using function parameters:

const addMany = async (connection = slonik) => {
  const useResult = await connection.query(sql`
    INSERT into 
      users (username, email) 
    VALUES 
      ('amite', 'amite@gmail.com'),
      ('nilesh', 'nil@gmail.com'),
      ('nikhil', 'nik@gmail.com')
      RETURNING *;
  `);

  return useResult;
};

Do I need to create tuples using string concatenation?

      ('amite', 'amite@gmail.com'),
      ('nilesh', 'nil@gmail.com'),
      ('nikhil', 'nik@gmail.com')

What I have tried so far is:

const addManyUsers = async(connection = slonik) => {
  const keys = [
    'username', 'email'
  ];
  
  const values = [
    ['nilesh', 'bailey'],
    ['nilesh@gmail.com', 'bailey@gmail.com']
  ]
  
  const identifiers = keys.map((key) => {
    return sql.identifier([key]);
  });
  
  const query = sql`
    INSERT INTO users
      (${sql.join(identifiers, sql`, `)})
    VALUES
      (${sql.unnest(values, sql`, `)})
    RETURNING *
  `
  const records = await connection.query(query)
  return records
}

When I run this I get the error:

(node:5975) UnhandledPromiseRejectionWarning: Error: **Column types length must match tuple member length.**
    at Object.createUnnestSqlFragment (/Users/shreekant/Documents/code/node/postgres-starter/node_modules/slonik/dist/sqlFragmentFactories/createUnnestSqlFragment.js:29:19)
    at Object.createSqlTokenSqlFragment (/Users/shreekant/Documents/code/node/postgres-starter/node_modules/slonik/dist/factories/createSqlTokenSqlFragment.js:27:39)
    at sql (/Users/shreekant/Documents/code/node/postgres-starter/node_modules/slonik/dist/factories/createSqlTag.js:39:65)
    at addManyUsers (/Users/shreekant/Documents/code/node/postgres-starter/app/models/db.js:58:20)
    at Object.<anonymous> (/Users/shreekant/Documents/code/node/postgres-starter/app/models/db.js:72:1)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
    at internal/main/run_main_module.js:17:47
(Use `node --trace-warnings ...` to show where the warning was created)
(node:5975) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
(node:5975) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

What am I doing wrong?

created time in 3 days

startedpmndrs/zustand

started time in 5 days

startedlukejacksonn/servor

started time in 11 days

startedalanqchen/Bear-Blog-Engine

started time in 12 days

startedreact-bootstrap/react-bootstrap

started time in 12 days

startedgrommet/grommet

started time in 14 days

startedcreativetimofficial/tailwind-starter-kit

started time in 14 days

startedalex-cory/use-ssr

started time in 15 days

startedchesterheng/advanced-react-patterns

started time in 15 days

startedhaskell/parsec

started time in 17 days

startedcdepillabout/pretty-simple

started time in 17 days

startedkentcdodds/advanced-react-patterns

started time in 18 days

fork amite/emacs-haskell-tutorial

Tutorial on setting up Emacs to do Haskell programming

fork in 19 days

startedserras/emacs-haskell-tutorial

started time in 19 days

startedbmkmanoj/js-by-examples

started time in 20 days

startedRhywun/get-programming-with-haskell

started time in 22 days

startedadrianhajdin/project_news_alan_ai

started time in 3 months

startedcarlosfrodrigues/silkscreen

started time in 3 months

starteddevelopit/redaxios

started time in 3 months

startedvadimdemedes/ink

started time in 3 months

startedstrapi/strapi-starter-react-blog

started time in 3 months

startedcoodoo/xstate-examples

started time in 3 months

more