More experimenting with tiedot

This commit is contained in:
Deluan
2016-02-27 03:35:01 -05:00
parent 4f9054b738
commit ecc0df9e7c
14 changed files with 175 additions and 35 deletions
+27 -9
View File
@@ -11,20 +11,38 @@ var (
once sync.Once
)
func createCollection(name string) *db.Col {
func createCollection(name string, ix ...interface{}) *db.Col {
log := false
if dbInstance().Use(name) == nil {
if err := dbInstance().Create(name); err != nil {
beego.Error(err)
}
log = true
}
col := dbInstance().Use(name)
if col != nil {
return col
}
if err := dbInstance().Create(name); err != nil {
beego.Error(err)
}
if err := col.Index([]string{"Id"}); err != nil {
beego.Error(name, err)
createIndex(col, []string{"Id"}, log)
for _, i := range ix {
switch i := i.(type) {
case string:
createIndex(col, []string{i}, log)
case []string:
createIndex(col, i, log)
default:
beego.Error("Trying to create an Index with an invalid type: ", i)
}
}
return col
}
func createIndex(col *db.Col, path []string, log bool) (err error) {
if err := col.Index(path); err != nil && log {
beego.Error(path, err)
}
return err
}
func dbInstance() *db.DB {
once.Do(func() {
instance, err := db.OpenDB(beego.AppConfig.String("dbPath"))