firestore-odm 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/firestore-odm.rb +17 -0
- data/lib/firestore-odm/collection.rb +47 -0
- data/lib/firestore-odm/document.rb +75 -0
- data/lib/firestore-odm/model.rb +10 -0
- data/lib/firestore-odm/schema.rb +42 -0
- metadata +48 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 46f4499f36b15866c173936a87f131a1dcd577a910ae756e639e8bb813f40d37
|
4
|
+
data.tar.gz: aa51288692a8f243edaef885845094f432430a235d76f3e099adbd4da2042894
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e0357329bb07784d50c21c314c6060bd3aef22b6d09021c18e321fb7322274de2f6f945d65a199df80765207109ddbefec3ade2c21e1781b8fb204497519d731
|
7
|
+
data.tar.gz: 3092565a2150f89088e53ab8b081f797da1c2c99937170f798d8b472a9fa459af79ac46a9c20bbd347713c7572ca436a2f4d41278601cd0d40d5db5dd3ce48bc
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require_relative 'firestore-odm/document'
|
2
|
+
require_relative 'firestore-odm/model'
|
3
|
+
|
4
|
+
class Google::Cloud::Firestore::DocumentReference
|
5
|
+
def to_s
|
6
|
+
document_id
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
class Module
|
11
|
+
# Defines a setter method.
|
12
|
+
# @param name [String, Symbol]
|
13
|
+
# @param block
|
14
|
+
def define_setter_method name, &block
|
15
|
+
define_method("#{name}=", &block)
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'google/cloud/firestore'
|
2
|
+
|
3
|
+
module FirestoreODM
|
4
|
+
module Collection
|
5
|
+
# Returns the collection used by the model.
|
6
|
+
# @param root [Google::Cloud::Firestore::DocumentReference]
|
7
|
+
# @return [Google::Cloud::Firestore::CollectionReference] the collection.
|
8
|
+
def collection root = nil
|
9
|
+
(root or Google::Cloud::Firestore.new).collection @path
|
10
|
+
end
|
11
|
+
|
12
|
+
# Sets the collection name.
|
13
|
+
# @param name [String, Symbol] the collection name.
|
14
|
+
def path name
|
15
|
+
@path = name
|
16
|
+
return
|
17
|
+
end
|
18
|
+
|
19
|
+
# Creates a document.
|
20
|
+
# @return [FirestoreODM::Model] the document.
|
21
|
+
def create opts = {}, &block
|
22
|
+
block.call o = self.new
|
23
|
+
doc = collection(opts[:relative_to]).add o.changes
|
24
|
+
self.new doc
|
25
|
+
end
|
26
|
+
|
27
|
+
# Creates a document.
|
28
|
+
# @param id [String] the document id.
|
29
|
+
# @param root [Google::Cloud::Firestore::DocumentReference]
|
30
|
+
# @return [FirestoreODM::Model] the document.
|
31
|
+
def create_with_id id, opts = {}, &block
|
32
|
+
block.call o = self.new
|
33
|
+
|
34
|
+
doc = collection(opts[:relative_to]).doc id
|
35
|
+
doc.create o.changes
|
36
|
+
self.from_id id
|
37
|
+
end
|
38
|
+
|
39
|
+
# Gets a document.
|
40
|
+
# @param id [String]
|
41
|
+
# @return [FirestoreODM::Model] the document.
|
42
|
+
def from_id id, opts = {}
|
43
|
+
doc = collection(opts[:relative_to]).doc id
|
44
|
+
self.new doc
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
module FirestoreODM
|
4
|
+
class Document
|
5
|
+
attr_accessor :document
|
6
|
+
attr_reader :data, :changes
|
7
|
+
|
8
|
+
# Constructor.
|
9
|
+
# @param document [Google::Cloud::Firestore::DocumentReference] the document reference.
|
10
|
+
def initialize document = nil
|
11
|
+
unless document.nil?
|
12
|
+
@document = document
|
13
|
+
@data = document.get.data
|
14
|
+
end
|
15
|
+
|
16
|
+
discard_changes
|
17
|
+
end
|
18
|
+
|
19
|
+
# Gets the document id.
|
20
|
+
# @return [String] the document id.
|
21
|
+
def id
|
22
|
+
document.document_id
|
23
|
+
end
|
24
|
+
|
25
|
+
# Gets the document path
|
26
|
+
# @return [String] the document path.
|
27
|
+
def path
|
28
|
+
document.document_path
|
29
|
+
end
|
30
|
+
|
31
|
+
# Saves any changes made to the document.
|
32
|
+
def save
|
33
|
+
document.update changes
|
34
|
+
@data = document.get.data
|
35
|
+
discard_changes
|
36
|
+
return
|
37
|
+
end
|
38
|
+
|
39
|
+
# Discards any changes made to the document.
|
40
|
+
def discard_changes
|
41
|
+
@changes = {}
|
42
|
+
return
|
43
|
+
end
|
44
|
+
|
45
|
+
# Deletes the document.
|
46
|
+
def delete
|
47
|
+
document.delete
|
48
|
+
end
|
49
|
+
|
50
|
+
# Gets a field.
|
51
|
+
# @param key [String, Symbol] the field's name.
|
52
|
+
# @return the field's value.
|
53
|
+
def get key
|
54
|
+
changes[key] or data[key]
|
55
|
+
end
|
56
|
+
|
57
|
+
# Sets a field.
|
58
|
+
# @param key [String, Symbol] the field's name.
|
59
|
+
# @param value the field's value.
|
60
|
+
def set key, value
|
61
|
+
changes[key] = value
|
62
|
+
return
|
63
|
+
end
|
64
|
+
|
65
|
+
# Converts the document to a JSON string.
|
66
|
+
# @param opts options to be passed to {JSON#generate}.
|
67
|
+
# @return [String] a JSON string.
|
68
|
+
def to_json opts = nil
|
69
|
+
data.merge(changes).to_json(opts)
|
70
|
+
end
|
71
|
+
|
72
|
+
alias :[] :get
|
73
|
+
alias :[]= :set
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module FirestoreODM
|
2
|
+
module Schema
|
3
|
+
# Defines a field.
|
4
|
+
# @param name [String, Symbol] the field's name.
|
5
|
+
# @param type [Class] the field's type.
|
6
|
+
# @param opts [Hash] the field's options.
|
7
|
+
def field name, type, opts = {}
|
8
|
+
define_method(name) { self[name] }
|
9
|
+
define_setter_method(name) { |value| self[name] = value }
|
10
|
+
return
|
11
|
+
end
|
12
|
+
|
13
|
+
# Defines a reference field.
|
14
|
+
# @param name [String, Symbol] the reference's name.
|
15
|
+
# @param model [Database::Model] the reference's model.
|
16
|
+
# @param opts [Hash] the reference's options.
|
17
|
+
def reference name, model, opts = {}
|
18
|
+
define_method(name) { model.new self[name] }
|
19
|
+
define_setter_method(name) { |value| self[name] = value.document }
|
20
|
+
return
|
21
|
+
end
|
22
|
+
|
23
|
+
# @param model [Database::Model]
|
24
|
+
def contains model, opts = {}
|
25
|
+
name = model.name.downcase
|
26
|
+
define_method("create_#{name}") do |opts = {}, &block|
|
27
|
+
opts = {:relative_to => document}.merge opts
|
28
|
+
model.create(opts, &block)
|
29
|
+
end
|
30
|
+
|
31
|
+
define_method("create_#{name}_with_id") do |id, opts = {}, &block|
|
32
|
+
opts = {:relative_to => document}.merge opts
|
33
|
+
model.create_with_id(id, opts, &block)
|
34
|
+
end
|
35
|
+
|
36
|
+
define_method("#{name}_from_id") do |id, opts = {}|
|
37
|
+
opts = {:relative_to => document}.merge opts
|
38
|
+
model.from_id(id, opts)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
metadata
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: firestore-odm
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Felipe Cabrera
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-05-31 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: ''
|
14
|
+
email: fecabrera0@outlook.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/firestore-odm.rb
|
20
|
+
- lib/firestore-odm/collection.rb
|
21
|
+
- lib/firestore-odm/document.rb
|
22
|
+
- lib/firestore-odm/model.rb
|
23
|
+
- lib/firestore-odm/schema.rb
|
24
|
+
homepage:
|
25
|
+
licenses:
|
26
|
+
- BSD-2-Clause
|
27
|
+
metadata:
|
28
|
+
source_code_uri: https://github.org/fecabrera/firestore-odm
|
29
|
+
post_install_message:
|
30
|
+
rdoc_options: []
|
31
|
+
require_paths:
|
32
|
+
- lib
|
33
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - ">="
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
requirements: []
|
44
|
+
rubygems_version: 3.0.8
|
45
|
+
signing_key:
|
46
|
+
specification_version: 4
|
47
|
+
summary: ODM for Firestore in Ruby
|
48
|
+
test_files: []
|