Skip to content

Commit 0cb9113

Browse files
committed
Merge pull request apa512#86 from apa512/index-create
Modify index-create to allow simple indexes
2 parents 8bb5792 + 87182e5 commit 0cb9113

File tree

3 files changed

+17
-11
lines changed

3 files changed

+17
-11
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ All notable changes to this project will be documented in this file. This change
1414
- Update protobuf support to RethinkDB 2.1.0.
1515
- Update docstring for `rethinkdb.query/without`.
1616
- Update arity and docstring for `rethinkdb.query/merge` to support merging any number of objects.
17+
- Add new arity to `rethinkdb.query/index-create` to allow creating simple indexes from field names. [#86](https://github.com/apa512/clj-rethinkdb/pull/86)
1718

1819
## [0.10.1] - 2015-07-08
1920
### Added

src/rethinkdb/query.cljc

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,13 @@
8484
(term :TABLE_LIST [db])))
8585

8686
(defn index-create
87-
"Create a new secondary index on a table."
88-
[table index-name func & [optargs]]
89-
(term :INDEX_CREATE [table index-name func] optargs))
87+
"Create a new secondary index on a table. To create a simple index based on the
88+
value of a single field, pass the field as the index-name. If you need to
89+
pass optargs without a function, then set func to nil."
90+
[table index-name & [func optargs]]
91+
(if (some? func)
92+
(term :INDEX_CREATE [table index-name func] optargs)
93+
(term :INDEX_CREATE [table index-name] optargs)))
9094

9195
(defn index-drop
9296
"Delete a previously created secondary index."
@@ -634,13 +638,13 @@
634638
(term :NOW []))
635639

636640
(defn time
637-
"Create a time object for a specific time."
638-
[& date-time-parts]
639-
(let [args (concat date-time-parts
640-
(if (string? (last date-time-parts))
641-
[]
642-
["+00:00"]))]
643-
(term :TIME args)))
641+
"Create a time object for a specific time."
642+
[& date-time-parts]
643+
(let [args (concat date-time-parts
644+
(if (string? (last date-time-parts))
645+
[]
646+
["+00:00"]))]
647+
(term :TIME args)))
644648

645649
(defn epoch-time
646650
"Create a time object based on seconds since epoch. The first argument is a

test/rethinkdb/core_test.clj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,14 @@
6363
(r/insert {:id (java.util.UUID/randomUUID)})) {:inserted 1}
6464
(r/table-drop (r/db test-db) :tmp) {:tables_dropped 1}
6565
(r/table-drop :tmp2) {:tables_dropped 1}
66+
(-> (r/table test-table) (r/index-create :name)) {:created 1}
6667
(-> (r/table test-table) (r/index-create :tmp (r/fn [row] 1))) {:created 1}
6768
(-> (r/table test-table)
6869
(r/index-create :type (r/fn [row]
6970
(r/get-field row :type)))) {:created 1}
7071
(-> (r/table test-table) (r/index-rename :tmp :xxx)) {:renamed 1}
7172
(-> (r/table test-table) (r/index-drop :xxx)) {:dropped 1})
72-
(is (= ["type"] (r/run (-> (r/table test-table) r/index-list) conn)))))
73+
(is (= ["name" "type"] (r/run (-> (r/table test-table) r/index-list) conn)))))
7374

7475
(deftest manipulating-data
7576
(with-open [conn (r/connect :db test-db)]

0 commit comments

Comments
 (0)