changeset 1220:4721c482c86b

cleaner Db example
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 21 Mar 2018 15:54:57 -0600
parents b602badc876b
children b99947af8b79
files examples/blog/src/lib/Db.luan examples/blog/src/lib/Db_mod.luan examples/blog/src/lib/Post.luan examples/blog/src/lib/test.luan examples/blog/src/private/tools/backup.html.luan examples/blog/src/private/tools/lucene.html.luan
diffstat 6 files changed, 34 insertions(+), 35 deletions(-) [+]
line wrap: on
line diff
--- a/examples/blog/src/lib/Db.luan	Wed Mar 21 15:36:54 2018 -0600
+++ b/examples/blog/src/lib/Db.luan	Wed Mar 21 15:54:57 2018 -0600
@@ -1,1 +1,19 @@
-return require "site:/lib/Db_mod.luan".new_db()
+local Lucene = require "luan:lucene/Lucene.luan"
+local Io = require "luan:Io.luan"
+
+
+local Db = {}
+
+function Db.new_db(lucene_dir)
+	local dir = Io.uri(lucene_dir).to_string()
+	local db = Lucene.index( dir, Lucene.type.english, {"subject","content"} )
+	
+--	this is how you index a field
+--	db.indexed_fields.post_date = Lucene.type.long
+
+	return db
+end
+
+Db.db = Db.new_db("site:/private/local/lucene")
+
+return Db
--- a/examples/blog/src/lib/Db_mod.luan	Wed Mar 21 15:36:54 2018 -0600
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,19 +0,0 @@
-local Lucene = require "luan:lucene/Lucene.luan"
-local Io = require "luan:Io.luan"
-
-
-local Db_mod = {}
-
-Db_mod.lucene_dir = "site:/private/local/lucene"
-
-function Db_mod.new_db()
-	local dir = Io.uri(Db_mod.lucene_dir).to_string()
-	local db = Lucene.index( dir, Lucene.type.english, {"subject","content"} )
-	
---	this is how you index a field
---	db.indexed_fields.post_date = Lucene.type.long
-
-	return db
-end
-
-return Db_mod
--- a/examples/blog/src/lib/Post.luan	Wed Mar 21 15:36:54 2018 -0600
+++ b/examples/blog/src/lib/Post.luan	Wed Mar 21 15:54:57 2018 -0600
@@ -6,7 +6,7 @@
 local now = Time.now or error()
 local String = require "luan:String.luan"
 local trim = String.trim or error()
-local Db = require "site:/lib/Db.luan"
+local db = require("site:/lib/Db.luan").db or error()
 
 
 local Post = {}
@@ -33,7 +33,7 @@
 			content = this.content
 			date = this.date
 		}
-		Db.save(doc)
+		db.save(doc)
 		this.id = doc.id
 	end
 
@@ -41,12 +41,12 @@
 end
 
 function Post.get_by_id(id)
-	local doc = Db.get_document("id:"..id)
+	local doc = db.get_document("id:"..id)
 	return doc and from_doc(doc)
 end
 
 function Post.get_all()
-	local docs = Db.search("type:post",1,1000,"id desc")
+	local docs = db.search("type:post",1,1000,"id desc")
 	local posts = {}
 	for _, doc in ipairs(docs) do
 		posts[#posts+1] = from_doc(doc)
@@ -59,7 +59,7 @@
 	if #query == 0 then
 		return Post.get_all()
 	end
-	local docs = Db.search(query,1,1000)
+	local docs = db.search(query,1,1000)
 	local posts = {}
 	for _, doc in ipairs(docs) do
 		posts[#posts+1] = from_doc(doc)
--- a/examples/blog/src/lib/test.luan	Wed Mar 21 15:36:54 2018 -0600
+++ b/examples/blog/src/lib/test.luan	Wed Mar 21 15:54:57 2018 -0600
@@ -10,13 +10,9 @@
 local String = require "luan:String.luan"
 local matches = String.matches or error()
 
-Http.uncache_site()
-
-local Db_mod = require "site:/lib/Db_mod.luan"
-
-Db_mod.lucene_dir = "site:/private/local/lucene_test"
 local Db = require "site:/lib/Db.luan"
-Db.delete_all()
+Db.db = Db.new_db("site:/private/local/lucene_test")
+Db.db.delete_all()
 
 local Post = require "site:/lib/Post.luan"
 local page
--- a/examples/blog/src/private/tools/backup.html.luan	Wed Mar 21 15:36:54 2018 -0600
+++ b/examples/blog/src/private/tools/backup.html.luan	Wed Mar 21 15:54:57 2018 -0600
@@ -1,12 +1,14 @@
+local Luan = require "luan:Luan.luan"
+local error = Luan.error
 local Io = require "luan:Io.luan"
 local Http = require "luan:http/Http.luan"
-local Db = require "site:/lib/Db.luan"
+local db = require("site:/lib/Db.luan").db or error()
 
 
 return function()
 	local backup = Io.uri "site:/private/local/backup.zip"
 	backup.delete()
-	Db.zip(backup)
+	db.zip(backup)
 
 	Io.stdout = Http.response.text_writer()
 %>
--- a/examples/blog/src/private/tools/lucene.html.luan	Wed Mar 21 15:36:54 2018 -0600
+++ b/examples/blog/src/private/tools/lucene.html.luan	Wed Mar 21 15:54:57 2018 -0600
@@ -1,4 +1,6 @@
+local Luan = require "luan:Luan.luan"
+local error = Luan.error
 local Web_search = require "luan:lucene/Web_search.luan"
-local Db = require "site:/lib/Db.luan"
+local db = require("site:/lib/Db.luan").db or error()
 
-return Web_search.of(Db)
+return Web_search.of(db)