mongoid-bulk-import 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.travis.yml +1 -0
- data/Gemfile.lock +1 -1
- data/README.md +21 -7
- data/lib/mongoid/bulk/import/version.rb +1 -1
- data/lib/mongoid/bulk/import.rb +23 -2
- metadata +2 -3
- data/lib/mongoid/persistable/creatable.rb +0 -34
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 51da653af49acd1c5864ac9486c2a5433002253cc76f5e82c916db2d3ba37cf0
|
4
|
+
data.tar.gz: 6e91982865e84d44fb831d93fc2b41fc6d5fb32fddfe86ef2476da59435708b5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9c5b39207d023542927f23ad76ed5a7e5288246bc8035d471d0ab35d0d7d95e99b78a71ac86d029cffe4d5197559b21a6050e8d7769a924f713c5ba5f9a376d9
|
7
|
+
data.tar.gz: d9ff9543faccd27263369d175a49ee8f294c2e40de4968ddbf6961d4260cc2d08400f1fbd138c803552b5b40aaa4e2105c27a82e230bf5207f8ec486756763cc
|
data/.travis.yml
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,8 +1,6 @@
|
|
1
1
|
# Mongoid::Bulk::Import
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
TODO: Delete this and the text above, and describe your gem
|
3
|
+
This gem adds an `bulk_insert` method to mongoid classes for bulk inserting data with validations support.
|
6
4
|
|
7
5
|
## Installation
|
8
6
|
|
@@ -22,13 +20,29 @@ Or install it yourself as:
|
|
22
20
|
|
23
21
|
## Usage
|
24
22
|
|
25
|
-
|
23
|
+
### Sample a single document
|
24
|
+
|
25
|
+
```ruby
|
26
26
|
|
27
|
-
|
27
|
+
class MyModel
|
28
|
+
include Mongoid::Document
|
29
|
+
field :name
|
30
|
+
end
|
31
|
+
|
32
|
+
MyModel.bulk_insert({ name: "Jack" })
|
33
|
+
```
|
28
34
|
|
29
|
-
|
35
|
+
### Sample multiple documents
|
30
36
|
|
31
|
-
|
37
|
+
```ruby
|
38
|
+
|
39
|
+
class MyModel
|
40
|
+
include Mongoid::Document
|
41
|
+
field :name
|
42
|
+
end
|
43
|
+
|
44
|
+
MyModel.bulk_insert([{ name: "Jack" }, { name: "Matt" }])
|
45
|
+
```
|
32
46
|
|
33
47
|
## Contributing
|
34
48
|
|
data/lib/mongoid/bulk/import.rb
CHANGED
@@ -1,10 +1,31 @@
|
|
1
1
|
require "mongoid/bulk/import/version"
|
2
|
+
require 'mongoid'
|
2
3
|
|
3
4
|
module Mongoid
|
4
5
|
module Bulk
|
5
6
|
module Import
|
6
|
-
|
7
|
-
|
7
|
+
extend ActiveSupport::Concern
|
8
|
+
Result = Struct.new(:failed_instances, :num_inserts, :ids)
|
9
|
+
|
10
|
+
def bulk_insert(attributes = nil, options = {}, &block)
|
11
|
+
_creating do
|
12
|
+
if attributes.is_a?(::Array)
|
13
|
+
documents = attributes.map { |attrs| bulk_insert_obj(attrs, options, &block) }
|
14
|
+
else
|
15
|
+
documents = [bulk_insert_obj(attributes, options, &block)]
|
16
|
+
end
|
17
|
+
valid_documents, invalid_documents = documents.partition { |doc| doc.errors.none? }
|
18
|
+
response = collection.insert_many(valid_documents.map(&:as_document)) unless valid_documents.blank?
|
19
|
+
Result.new(invalid_documents, response&.inserted_count || 0, response&.inserted_ids || [])
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def bulk_insert_obj(attributes = nil, options = {}, &block)
|
24
|
+
doc = new(attributes, &block)
|
25
|
+
doc.valid? if (options[:validate].nil? ? true : options[:validate])
|
26
|
+
doc
|
27
|
+
end
|
8
28
|
end
|
9
29
|
end
|
10
30
|
end
|
31
|
+
Mongoid::Document::ClassMethods.send(:include, Mongoid::Bulk::Import)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid-bulk-import
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Arun Bharati
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-09-
|
11
|
+
date: 2021-09-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mongoid
|
@@ -86,7 +86,6 @@ files:
|
|
86
86
|
- bin/setup
|
87
87
|
- lib/mongoid/bulk/import.rb
|
88
88
|
- lib/mongoid/bulk/import/version.rb
|
89
|
-
- lib/mongoid/persistable/creatable.rb
|
90
89
|
- mongoid-bulk-import.gemspec
|
91
90
|
homepage: https://github.com/Arunbharati/mongoid-bulk-import
|
92
91
|
licenses:
|
@@ -1,34 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Mongoid
|
4
|
-
module Persistable
|
5
|
-
|
6
|
-
# Defines behavior for persistence operations that create new documents.
|
7
|
-
module Creatable
|
8
|
-
extend ActiveSupport::Concern
|
9
|
-
Result = Struct.new(:failed_instances, :num_inserts, :ids)
|
10
|
-
|
11
|
-
module ClassMethods
|
12
|
-
|
13
|
-
def bulk_insert(attributes = nil, options = {}, &block)
|
14
|
-
_creating do
|
15
|
-
if attributes.is_a?(::Array)
|
16
|
-
documents = attributes.map { |attrs| bulk_insert_obj(attrs, options, &block) }
|
17
|
-
else
|
18
|
-
documents = [bulk_insert_obj(attributes, options, &block)]
|
19
|
-
end
|
20
|
-
valid_documents, invalid_documents = documents.partition { |doc| doc.errors.none? }
|
21
|
-
response = collection.insert_many(valid_documents.map(&:as_document)) unless valid_documents.blank?
|
22
|
-
Result.new(invalid_documents, response&.inserted_count || 0, response&.inserted_ids || [])
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
def bulk_insert_obj(attributes = nil, options = {}, &block)
|
27
|
-
doc = new(attributes, &block)
|
28
|
-
doc.valid? if (options[:validate].nil? ? true : options[:validate])
|
29
|
-
doc
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|