{"id":269631,"date":"2015-12-04T13:21:02","date_gmt":"2015-12-04T10:21:02","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=269631"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=269631","title":{"rendered":"\u041f\u0440\u043e\u0435\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0432 PostgreSQL \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u043e-\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e API: \u041f\u043e\u043b\u043d\u043e\u0442\u0435\u043a\u0441\u0442\u043e\u0432\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u043c\u043d\u043e\u0433\u0438\u0445 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u043e\u0432(\u0427\u0430\u0441\u0442\u044c 2)"},"content":{"rendered":"<p>       \u0412 <a href=\"http:\/\/habrahabr.ru\/post\/272395\/\">\u043f\u0435\u0440\u0432\u043e\u0439 \u0447\u0430\u0441\u0442\u0438 \u044d\u0442\u043e\u0439 \u0441\u0435\u0440\u0438\u0438 \u0441\u0442\u0430\u0442\u0435\u0439<\/a>, \u044f \u0441\u043e\u0437\u0434\u0430\u043b \u0445\u043e\u0440\u043e\u0448\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f, \u0440\u0430\u0432\u043d\u043e \u043a\u0430\u043a \u0438 \u0434\u0440\u0443\u0433\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0449\u0443\u044e \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u0438\u0437\u043c\u0435\u043d\u044f\u0435\u043c\u044b\u0435 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u043e-\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u043d\u0430 \u043b\u0435\u0442\u0443. \u041e\u043d\u0438 \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0442 \u0438\u0441\u043f\u0440\u0430\u0432\u043d\u043e \u0438 \u0434\u0435\u043b\u0430\u044e\u0442 \u0438\u043c\u0435\u043d\u043d\u043e \u0442\u043e, \u0447\u0442\u043e \u043d\u0430\u0434\u043e, \u043d\u043e \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0435\u0449\u0435 \u043c\u043d\u043e\u0433\u043e\u0435. \u041e\u0441\u043e\u0431\u0435\u043d\u043d\u043e: <i>\u044f \u0445\u043e\u0447\u0443 \u043f\u043e\u043b\u043d\u043e\u0442\u0435\u043a\u0441\u0442\u043e\u0432\u044b\u0439 \u043f\u043e\u0438\u0441\u043a, \u0438\u043d\u0434\u0435\u043a\u0441\u0438\u0440\u0443\u0435\u043c\u044b\u0439 \u043d\u0430 \u043b\u0435\u0442\u0443 \u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u043c\u043d\u043e\u0433\u0438\u0445 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u0432\u043d\u0443\u0442\u0440\u0438 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438.<\/i><\/p>\n<p>  \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0441\u0434\u0435\u043b\u0430\u0435\u043c \u044d\u0442\u043e.<br \/>  <a name=\"habracut\"><\/a>  <\/p>\n<h4><b>\u041f\u043e\u043b\u043d\u043e\u0442\u0435\u043a\u0441\u0442\u043e\u0432\u044b\u0439 \u043f\u043e\u0438\u0441\u043a<\/b><\/h4>\n<p>  \u041d\u0430\u0448\u0430 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u043e-\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u0430\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0438\u043c\u0435\u0435\u0442 \u043f\u043e\u0438\u0441\u043a\u043e\u0432\u043e\u0435 \u043f\u043e\u043b\u0435 \u0442\u0438\u043f\u0430 <b>tsvector<\/b>, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u0438\u0440\u0443\u0435\u0442\u0441\u044f, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f GIN \u0438\u043d\u0434\u0435\u043a\u0441 \u0434\u043b\u044f \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u0438. \u042f \u0445\u043e\u0447\u0443 \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0442\u044c \u044d\u0442\u043e \u043f\u043e\u043b\u0435 \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437, \u043a\u043e\u0433\u0434\u0430 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u044e \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442, \u0438 \u043d\u0435 \u0445\u043e\u0447\u0443 \u043c\u043d\u043e\u0433\u043e \u0448\u0443\u043c\u0430 \u043e\u0442 API \u043a\u043e\u0433\u0434\u0430 \u044f \u044d\u0442\u043e \u0434\u0435\u043b\u0430\u044e.<\/p>\n<p>  \u0412 \u0441\u0432\u044f\u0437\u0438 \u0441 \u044d\u0442\u0438\u043c, \u044f \u043f\u0440\u0438\u0431\u0435\u0433\u043d\u0443 \u043a \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0443\u0441\u043b\u043e\u0432\u043d\u043e\u0441\u0442\u0438.<\/p>\n<p>  \u041e\u0431\u044b\u0447\u043d\u043e, \u043f\u0440\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 \u043f\u043e\u043b\u043d\u043e\u0442\u0435\u043a\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0430, \u043f\u043e\u043b\u044f \u0445\u0440\u0430\u043d\u044f\u0442\u0441\u044f \u0441 \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e-\u0442\u0430\u043a\u0438 \u0441\u043f\u0435\u0446\u0438\u0444\u0438\u0447\u0435\u0441\u043a\u0438\u043c\u0438 \u0438\u043c\u0435\u043d\u0430\u043c\u0438. \u0422\u0430\u043a\u0438\u043c\u0438 \u043a\u0430\u043a:  <\/p>\n<ul>\n<li>\u0418\u043c\u044f, \u0438\u043b\u0438 \u0444\u0430\u043c\u0438\u043b\u0438\u044f, \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u0430\u0434\u0440\u0435\u0441 \u044d\u043b\u0435\u043a\u0442\u0440\u043e\u043d\u043d\u043e\u0439 \u043f\u043e\u0447\u0442\u044b<\/li>\n<li>\u041d\u0430\u0437\u0432\u0430\u043d\u0438\u0435, \u0438\u043b\u0438 \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u0447\u0435\u0433\u043e-\u0442\u043e<\/li>\n<li>\u0410\u0434\u0440\u0435\u0441\u043d\u0430\u044f \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f<\/li>\n<\/ul>\n<p>  \u042f \u0445\u043e\u0442\u0435\u043b \u0431\u044b \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c \u043c\u043e\u0439 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442 \u0432 \u043c\u043e\u043c\u0435\u043d\u0442 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u043d\u0430 \u043d\u0430\u043b\u0438\u0447\u0438\u0435 \u043a\u0430\u043a\u0438\u0445-\u043b\u0438\u0431\u043e \u043a\u043b\u044e\u0447\u0435\u0439, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u044f \u0445\u043e\u0442\u0435\u043b \u0431\u044b \u043f\u0440\u043e\u0438\u043d\u0434\u0435\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438 \u043f\u043e\u0441\u043b\u0435 \u044d\u0442\u043e\u0433\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0438\u0445 \u0432 \u043f\u043e\u043b\u0435 <b>search<\/b>. \u042d\u0442\u043e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u044f \u043d\u0430\u0437\u0432\u0430\u043b <b>update_search<\/b>:  <\/p>\n<pre><code class=\"sql\">create function update_search(tbl varchar, id int) returns boolean as $$   \/\/get the record   var found = plv8.execute(&quot;select body from &quot; + tbl + &quot; where id=$1&quot;,id)[0];   if(found){     var doc = JSON.parse(found.body);     var searchFields = [&quot;name&quot;,&quot;email&quot;,&quot;first&quot;,&quot;first_name&quot;,                        &quot;last&quot;,&quot;last_name&quot;,&quot;description&quot;,&quot;title&quot;,                         &quot;street&quot;, &quot;city&quot;, &quot;state&quot;, &quot;zip&quot;, ];     var searchVals = [];     for(var key in doc){       if(searchFields.indexOf(key.toLowerCase()) &gt; -1){         searchVals.push(doc[key]);       }     };      if(searchVals.length &gt; 0){       var updateSql = &quot;update &quot; + tbl + &quot; set search = to_tsvector($1) where id =$2&quot;;       plv8.execute(updateSql, searchVals.join(&quot; &quot;), id);     }     return true;   }else{     return false;   }  $$ language plv8; <\/code><\/pre>\n<p>  \u042f \u0432\u043d\u043e\u0432\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e javascript (PLV8) \u0434\u043b\u044f \u044d\u0442\u0438\u0445 \u0446\u0435\u043b\u0435\u0439, \u0438 \u0432\u044b\u0442\u044f\u0433\u0438\u0432\u0430\u044e \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442, \u043e\u0441\u043d\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043d\u0430 ID. \u041f\u043e\u0441\u043b\u0435 \u0447\u0435\u0433\u043e \u044f \u043f\u0440\u043e\u0445\u043e\u0436\u0443 \u043f\u043e \u0432\u0441\u0435\u043c \u043a\u043b\u044e\u0447\u0430\u043c, \u0447\u0442\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u0435\u0441\u0442\u044c \u043b\u0438 \u0441\u0440\u0435\u0434\u0438 \u043d\u0438\u0445 \u0442\u0435, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u044f \u043c\u043e\u0433 \u0431\u044b \u0437\u0430\u0445\u043e\u0442\u0435\u0442\u044c \u0445\u0440\u0430\u043d\u0438\u0442\u044c, \u0438, \u0435\u0441\u043b\u0438 \u0435\u0441\u0442\u044c, \u043f\u043e\u043c\u0435\u0449\u0430\u044e \u0438\u0445 \u0432 \u043c\u0430\u0441\u0441\u0438\u0432.<\/p>\n<p>  \u0415\u0441\u043b\u0438 \u0432 \u044d\u0442\u043e\u0442 \u0430\u0440\u0445\u0438\u0432 \u0435\u0441\u0442\u044c \u043f\u043e\u043f\u0430\u0434\u0430\u043d\u0438\u044f, \u044f \u043a\u043e\u043d\u043a\u0430\u0442\u0438\u043d\u0438\u0440\u0443\u044e \u044d\u0442\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u044e \u0438\u0445 \u0432 \u043f\u043e\u043b\u0435 <b>search<\/b> \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044e <b>to_tsvector<\/b>, \u044f\u0432\u043b\u044f\u044e\u0449\u0443\u044e\u0441\u044f \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u043e\u0439 \u0432 Postgres, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0431\u0435\u0440\u0435\u0442 \u043e\u0431\u044b\u0447\u043d\u044b\u0439 \u0442\u0435\u043a\u0441\u0442 \u0438 \u043f\u0440\u0435\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0435\u0433\u043e \u0432 \u0438\u043d\u0434\u0435\u043a\u0441\u0438\u0440\u0443\u0435\u043c\u044b\u0435 \u0432\u0435\u043b\u0438\u0447\u0438\u043d\u044b.<\/p>\n<p>  \u0412\u043e\u0442 \u0438 \u043e\u043d\u043e! \u0412\u044b\u043f\u043e\u043b\u043d\u044f\u044f \u044d\u0442\u043e\u0442 \u0441\u043a\u0440\u0438\u043f\u0442, \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435:<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/post_images\/762\/29e\/f04\/76229ef04088c9dd8f0a2a4187b3d062.png\" alt=\"image\"\/><\/p>\n<p>  \u0418\u0434\u0435\u0430\u043b\u044c\u043d\u043e \u2014 \u0442\u0435\u043f\u0435\u0440\u044c \u044f \u043f\u0440\u043e\u0441\u0442\u043e \u043c\u043e\u0433\u0443 \u0432\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u044d\u0442\u043e \u0432 \u043a\u043e\u043d\u0435\u0446 \u043c\u043e\u0435\u0439 <b>save_document <\/b>\u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0438 \u043e\u043d\u043e \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c\u0441\u044f \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u043e\u043d\u043d\u043e \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437, \u043a\u043e\u0433\u0434\u0430 \u044f \u0447\u0442\u043e-\u0442\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u044e:<\/p>\n<pre><code class=\"sql\">create function save_document(tbl varchar, doc_string jsonb) returns jsonb as $$   var doc = JSON.parse(doc_string);   var result = null;   var id = doc.id;   var exists = plv8.execute(&quot;select table_name from information_schema.tables where table_name = $1&quot;, tbl)[0];    if(!exists){     plv8.execute(&quot;select create_document_table('&quot; + tbl + &quot;');&quot;);   }    if(id){     result = plv8.execute(&quot;update &quot; + tbl + &quot; set body=$1, updated_at = now() where id=$2 returning *;&quot;,doc_string,id);   }else{     result = plv8.execute(&quot;insert into &quot; + tbl + &quot;(body) values($1) returning *;&quot;, doc_string);     id = result[0].id;     doc.id = id;     result = plv8.execute(&quot;update &quot; + tbl + &quot; set body=$1 where id=$2 returning *&quot;,JSON.stringify(doc),id);   }    \/\/run the search indexer   plv8.execute(&quot;perform update_search($1, $2)&quot;, tbl,id);   return result[0] ? result[0].body : null;  $$ language plv8; <\/code><\/pre>\n<p>  <\/p>\n<h4><b>\u0421\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u043c\u043d\u043e\u0433\u0438\u0445 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u043e\u0432<\/b><\/h4>\n<p>  \u041d\u0430 \u0434\u0430\u043d\u043d\u044b\u0439 \u043c\u043e\u043c\u0435\u043d\u0442, \u044f \u043c\u043e\u0433\u0443 \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u0435\u0434\u0438\u043d\u0438\u0447\u043d\u044b\u0439 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <b>save_document<\/b>, \u043d\u043e \u044f \u0445\u043e\u0442\u0435\u043b \u0431\u044b \u0438\u043c\u0435\u0442\u044c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u0435\u0439 \u043c\u0430\u0441\u0441\u0438\u0432. \u042f \u043c\u043e\u0433\u0443 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u044d\u0442\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u044f \u0442\u0438\u043f \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430, \u043f\u043e\u0441\u043b\u0435 \u0447\u0435\u0433\u043e \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0442\u044c \u0446\u0438\u043a\u043b:<\/p>\n<pre><code class=\"sql\">create function save_document(tbl varchar, doc_string jsonb) returns jsonb as $$   var doc = JSON.parse(doc_string);    var exists = plv8.execute(&quot;select table_name from information_schema.tables where table_name = $1&quot;, tbl)[0];   if(!exists){     plv8.execute(&quot;select create_document_table('&quot; + tbl + &quot;');&quot;);   }    \/\/function that executes our SQL statement   var executeSql = function(theDoc){     var result = null;     var id = theDoc.id;     var toSave = JSON.stringify(theDoc);      if(id){       result=plv8.execute(&quot;update &quot; + tbl + &quot; set body=$1, updated_at = now() where id=$2 returning *;&quot;,toSave, id);     }else{       result=plv8.execute(&quot;insert into &quot; + tbl + &quot;(body) values($1) returning *;&quot;, toSave);        id = result[0].id;       \/\/put the id back on the document       theDoc.id = id;       \/\/resave it       result = plv8.execute(&quot;update &quot; + tbl + &quot; set body=$1 where id=$2 returning *;&quot;,JSON.stringify(theDoc),id);     }     plv8.execute(&quot;select update_search($1,$2)&quot;, tbl, id);     return result ? result[0].body : null;   }   var out = null;    \/\/was an array passed in?   if(doc instanceof Array){     for(var i = 0; i &lt; doc.length;i++){       executeSql(doc[i]);     }     \/\/just report back how many documents were saved     out = JSON.stringify({count : i, success : true});   }else{     out = executeSql(doc);   }   return out; $$ language plv8; <\/code><\/pre>\n<p>  \u0425\u043e\u0440\u043e\u0448\u0430\u044f \u0441\u0442\u043e\u0440\u043e\u043d\u0430 \u0440\u0430\u0431\u043e\u0442\u044b \u0437\u0434\u0435\u0441\u044c \u0441 javascript&#8217;\u043e\u043c \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u043b\u043e\u0433\u0438\u043a\u0430, \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u0430\u044f \u0434\u043b\u044f \u0442\u0430\u043a\u043e\u0439 \u0440\u0443\u0442\u0438\u043d\u044b, \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u0430 (\u0432 \u043f\u0440\u043e\u0442\u0438\u0432\u043e\u043f\u043e\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c PLPGSQL). \u042f \u0432\u044b\u0434\u0435\u043b\u0438\u043b \u0432\u0435\u0441\u044c \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0432 \u0435\u0433\u043e \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u2014 \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f javascript&#8217;\u043e\u043c \u043f\u043e\u0441\u043b\u0435 \u0432\u0441\u0435\u0433\u043e \u2014 \u0442\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u044f \u043c\u043e\u0433\u0443 \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u0434\u0443\u0431\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f.<\/p>\n<p>  \u0417\u0430\u0442\u0435\u043c \u044f \u0445\u043e\u0447\u0443 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u0447\u0442\u043e \u043f\u043e\u0434\u0430\u0432\u0430\u0435\u043c\u044b\u0439 \u043d\u0430 \u0432\u0445\u043e\u0434 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442 \u2014 \u043c\u0430\u0441\u0441\u0438\u0432. \u0415\u0441\u043b\u0438 \u044d\u0442\u043e \u0442\u0430\u043a, \u0442\u043e \u044f \u0438\u0434\u0443 \u043f\u043e \u0435\u0433\u043e \u0447\u043b\u0435\u043d\u0430\u043c \u0438 \u0432\u044b\u0437\u044b\u0432\u0430\u044e <b>executeSql<\/b>, \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044f \u0432\u0441\u0435, \u0447\u0442\u043e \u043d\u0430\u043a\u043e\u043f\u0438\u043b\u043e\u0441\u044c \u043f\u0440\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0438.<\/p>\n<p>  \u0415\u0441\u043b\u0438 \u044d\u0442\u043e \u043d\u0435 \u043c\u0430\u0441\u0441\u0438\u0432, \u044f \u043f\u0440\u043e\u0441\u0442\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e \u0432\u0441\u0435 \u0442\u0430\u043a \u0436\u0435, \u043a\u0430\u043a \u0438 \u0431\u044b\u043b\u043e, \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044f \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442 \u0446\u0435\u043b\u0438\u043a\u043e\u043c. \u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442:<\/p>\n<p>  <img decoding=\"async\" src=\"http:\/\/rob.conery.io\/wp-content\/uploads\/2015\/08\/bulk_save.png\" alt=\"image\"\/><\/p>\n<p>  \u041f\u0440\u0435\u043a\u0440\u0430\u0441\u043d\u043e! \u041b\u0443\u0447\u0448\u0435\u0435 \u0432 \u044d\u0442\u043e\u043c \u0442\u043e, \u0447\u0442\u043e <b>\u044d\u0442\u043e \u0432\u0441\u0435 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0432\u043d\u0443\u0442\u0440\u0438 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438<\/b>. \u041c\u043d\u0435 \u044d\u0442\u043e \u043d\u0440\u0430\u0432\u0438\u0442\u0441\u044f!<\/p>\n<h4><b>\u0421\u0442\u0440\u0430\u043d\u043d\u043e\u0441\u0442\u0438 Node<\/b><\/h4>\n<p>  \u0415\u0441\u043b\u0438 \u0431\u044b \u0442\u043e\u043b\u044c\u043a\u043e \u044d\u0442\u043e \u043c\u043e\u0433\u043b\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0438\u0434\u0435\u0430\u043b\u044c\u043d\u043e \u0438\u0437 Node! \u042f \u043f\u0440\u043e\u0431\u043e\u0432\u0430\u043b \u0438 \u0432 .NET, \u0438 \u0432 Node, \u0441 .NET \u0432\u0441\u0435 \u043f\u0440\u043e\u0441\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 (\u0441\u0442\u0440\u0430\u043d\u043d\u043e) \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 Npgsql. \u0418\u0437 Node, \u043d\u0435 \u0442\u043e \u0447\u0442\u043e\u0431\u044b.<\/p>\n<p>  \u041a\u043e\u0440\u043e\u0447\u0435 \u0433\u043e\u0432\u043e\u0440\u044f: node_pg \u0434\u0440\u0430\u0439\u0432\u0435\u0440 \u0434\u0435\u043b\u0430\u0435\u0442 \u043e\u0447\u0435\u043d\u044c \u0441\u0442\u0440\u0430\u043d\u043d\u043e\u0435 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0435, \u043a\u043e\u0433\u0434\u0430 \u043e\u043d \u0432\u0438\u0434\u0438\u0442 \u043e\u0431\u044a\u0435\u043a\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0432\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430. \u041e\u0431\u0440\u0430\u0442\u0438\u043c \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435:<\/p>\n<pre><code class=\"javascript\">var pg = require(&quot;pg&quot;); var run = function (sql, params, next) {   pg.connect(args.connectionString, function (err, db, done) {     \/\/throw if there's a connection error     assert.ok(err === null, err);      db.query(sql, params, function (err, result) {       \/\/we have the results, release the connection       done();       pg.end();       if(err){         next(err,null);       }else{         next(null, result.rows);       }     });   }); };  run(&quot;select * from save_document($1, $2)&quot;, ['customer_docs', {name : &quot;Larry&quot;}], function(err,res){   \/\/works just fine } <\/code><\/pre>\n<p>  \u042d\u0442\u043e \u043e\u0431\u044b\u0447\u043d\u044b\u0439 Node\/PG \u043a\u043e\u0434. \u0412 \u0441\u0430\u043c\u043e\u043c \u043a\u043e\u043d\u0446\u0435, \u0444\u0443\u043d\u043a\u0446\u0438\u044f run \u043d\u0430\u0441\u0442\u0440\u043e\u0435\u043d\u0430 \u0447\u0442\u043e\u0431\u044b \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u043c\u043e\u044e save_document \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435. \u041a\u043e\u0433\u0434\u0430 PG \u0432\u0438\u0434\u0438\u0442 \u0432\u0445\u043e\u0434\u043d\u043e\u0439 \u043e\u0431\u044a\u0435\u043a\u0442, \u043e\u043d \u043f\u0440\u0435\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0435\u0433\u043e \u0432 \u0441\u0442\u0440\u043e\u043a\u0443 \u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u043f\u0440\u043e\u0439\u0434\u0435\u0442 \u043d\u043e\u0440\u043c\u0430\u043b\u044c\u043d\u043e.<\/p>\n<p>  \u0412 \u0441\u043b\u0443\u0447\u0430\u0435 \u0436\u0435, \u0435\u0441\u043b\u0438 \u043f\u043e\u0441\u043b\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432\u2026<\/p>\n<pre><code class=\"javascript\">run(&quot;select * from save_document($1, $2)&quot;,           ['customer_docs', [{name : &quot;Larry&quot;}, {name : &quot;Susie&quot;}],           function(err,res){   \/\/crashes hard } <\/code><\/pre>\n<p>  \u042f \u043f\u043e\u043b\u0443\u0447\u0430\u044e \u043e\u0431\u0440\u0430\u0442\u043d\u043e \u043e\u0448\u0438\u0431\u043a\u0443, \u0441\u043e\u043e\u0431\u0449\u0430\u044e\u0449\u0443\u044e \u043c\u043d\u0435, \u0447\u0442\u043e \u044d\u0442\u043e \u043d\u0435\u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u044b\u0439 JSON. \u0421\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u043e\u0431 \u043e\u0448\u0438\u0431\u043a\u0435 (\u0438\u0437 Postgres) \u0441\u043e\u043e\u0431\u0449\u0438\u0442, \u0447\u0442\u043e \u044d\u0442\u043e \u0441\u0432\u044f\u0437\u0430\u043d\u043e \u0441\u043e \u0441\u043a\u0443\u0434\u043d\u043e \u043e\u0442\u0444\u043e\u0440\u043c\u0430\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u043c JSON:<\/p>\n<pre><code class=\"javascript\">{&quot;{name : &quot;Larry&quot;}, ...} <\/code><\/pre>\n<p>  \u0427\u0442\u043e\u2026 \u0434\u0430, \u044d\u0442\u043e \u0443\u0436\u0430\u0441\u043d\u043e. \u042f \u043f\u044b\u0442\u0430\u043b\u0441\u044f \u0441\u0444\u043e\u0440\u043c\u0443\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0447\u0442\u043e \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442, \u043d\u043e \u044d\u0442\u043e \u043f\u0440\u043e\u0441\u0442\u043e-\u043d\u0430\u043f\u0440\u043e\u0441\u0442\u043e \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a, \u0447\u0442\u043e node_pg \u0434\u0440\u0430\u0439\u0432\u0435\u0440 \u0440\u0430\u0437\u0431\u0438\u0440\u0430\u0435\u0442 \u0432\u043d\u0435\u0448\u043d\u0438\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u2014 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u0432\u044b\u0437\u044b\u0432\u0430\u044f \u043c\u0435\u0442\u043e\u0434 <b>flatten<\/b> \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 Underscores. \u042f \u043d\u0435 \u0437\u043d\u0430\u044e. \u0427\u0442\u043e\u0431\u044b \u044d\u0442\u043e \u043e\u0431\u043e\u0439\u0442\u0438, \u0432\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u0430\u0448 \u0432\u044b\u0437\u043e\u0432 \u043d\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435:<\/p>\n<pre><code class=\"javascript\">run(&quot;select * from save_document($1, $2)&quot;,           ['customer_docs', JSON.stringify([{name : &quot;Larry&quot;}, {name : &quot;Susie&quot;}]),           function(err,res){   \/\/Works fine } <\/code><\/pre>\n<p>  <\/p>\n<h4><b>\u0412\u043f\u0435\u0440\u0435\u0434!<\/b><\/h4>\n<p>  \u041f\u0440\u043e\u0446\u0435\u0434\u0443\u0440\u0430 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u0430 \u0433\u043b\u0430\u0434\u043a\u0430\u044f \u0438 \u044d\u0442\u043e \u043c\u0435\u043d\u044f \u0440\u0430\u0434\u0443\u0435\u0442. \u0412 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u044f \u043d\u0430\u0441\u0442\u0440\u043e\u044e \u043f\u043e\u0438\u0441\u043a\u043e\u0432\u0438\u043a\u0438, \u0430 \u0442\u0430\u043a\u0436\u0435 \u0441\u043e\u0437\u0434\u0430\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043f\u043e\u043b\u043d\u043e\u0442\u0435\u043a\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u043f\u043e\u0438\u0441\u043a\u0430.       <\/p>\n<div class=\"clear\"><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"http:\/\/habrahabr.ru\/post\/272411\/\"> http:\/\/habrahabr.ru\/post\/272411\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>       \u0412 <a href=\"http:\/\/habrahabr.ru\/post\/272395\/\">\u043f\u0435\u0440\u0432\u043e\u0439 \u0447\u0430\u0441\u0442\u0438 \u044d\u0442\u043e\u0439 \u0441\u0435\u0440\u0438\u0438 \u0441\u0442\u0430\u0442\u0435\u0439<\/a>, \u044f \u0441\u043e\u0437\u0434\u0430\u043b \u0445\u043e\u0440\u043e\u0448\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f, \u0440\u0430\u0432\u043d\u043e \u043a\u0430\u043a \u0438 \u0434\u0440\u0443\u0433\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0449\u0443\u044e \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u0438\u0437\u043c\u0435\u043d\u044f\u0435\u043c\u044b\u0435 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u043e-\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u043d\u0430 \u043b\u0435\u0442\u0443. \u041e\u043d\u0438 \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0442 \u0438\u0441\u043f\u0440\u0430\u0432\u043d\u043e \u0438 \u0434\u0435\u043b\u0430\u044e\u0442 \u0438\u043c\u0435\u043d\u043d\u043e \u0442\u043e, \u0447\u0442\u043e \u043d\u0430\u0434\u043e, \u043d\u043e \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0435\u0449\u0435 \u043c\u043d\u043e\u0433\u043e\u0435. \u041e\u0441\u043e\u0431\u0435\u043d\u043d\u043e: <i>\u044f \u0445\u043e\u0447\u0443 \u043f\u043e\u043b\u043d\u043e\u0442\u0435\u043a\u0441\u0442\u043e\u0432\u044b\u0439 \u043f\u043e\u0438\u0441\u043a, \u0438\u043d\u0434\u0435\u043a\u0441\u0438\u0440\u0443\u0435\u043c\u044b\u0439 \u043d\u0430 \u043b\u0435\u0442\u0443 \u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u043c\u043d\u043e\u0433\u0438\u0445 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u0432\u043d\u0443\u0442\u0440\u0438 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438.<\/i><\/p>\n<p>  \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0441\u0434\u0435\u043b\u0430\u0435\u043c \u044d\u0442\u043e.  <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-269631","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/269631","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=269631"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/269631\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=269631"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=269631"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=269631"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}