func (object *Record) SaveAndTriggerWithOptions(endpoint string, trigger bool, force_create bool) {
	connection := database.GetConnection()
	defer connection.Release()

	object.UpdatedAt = time.Now().Unix()

	collection := connection.C(object.Collection())

	var err error

	if object.Id == bson.ObjectId("") {
		object.Id = bson.NewObjectId()
		object.CreatedAt = time.Now().Unix()
		err = collection.Insert(object)
		if trigger {
			//object.UpdateReferenceMap()
			go object.FindAndExecuteTriggerForEvent(endpoint + ":create")
		}
	} else {
		err = collection.UpdateId(object.Id, object)
		//fmt.Println("SAVE", object.Content)
		if trigger {
			//object.UpdateReferenceMap()
			if force_create {
				go object.FindAndExecuteTriggerForEvent(endpoint + ":create")
			} else {
				go object.FindAndExecuteTriggerForEvent(endpoint + ":update")
			}
		}
	}
	if err != nil {
		fmt.Println("MGO ERROR", endpoint, err, object.Id)
	}
}
示例#2
0
func (endpoint Endpoint) FilesForRecords(records []Record) []File {
	result := []File{}

	ids := []bson.ObjectId{}

	for _, record := range records {
		if record.FileIds != nil {
			ids = append(ids, record.FileIds...)
		}
	}

	if len(ids) != 0 {
		query := bson.M{"_id": map[string]interface{}{"$in": ids}}

		collection := endpoint.ApplicationId.Hex()
		collection += "_" + endpoint.Id.Hex()
		collection += "_files"

		connection := database.GetConnection()
		database.Query(connection, collection, query).All(&result)
		connection.Release()
	}

	return result
}
func (object *File) Delete() {

	connection := database.GetConnection()
	defer connection.Release()
	logger.Debug("[Database] Deleting File")

	collection := connection.C(object.Collection())
	collection.RemoveId(object.Id)
}
示例#4
0
func StatusPingHandler(c echo.Context) error {
	err := database.GetConnection().Session.Ping()

	if err == nil {
		return c.String(http.StatusOK, "OK")
	}

	return c.String(http.StatusInternalServerError, "ERROR")
}
func (object *User) Delete() {
	connection := database.GetConnection()
	defer connection.Release()
	logger.Debug("[Database] Deleting User")

	collection := connection.C(object.Collection())
	collection.RemoveId(object.Id)
	key := object.ApplicationId.Hex() + ".users." + object.Id.Hex()
	cache.Clear(key)
}
func (object *Record) DeleteAndTrigger(endpoint string, trigger bool) {
	connection := database.GetConnection()
	defer connection.Release()
	logger.Debug("[Database] Deleting Record")

	collection := connection.C(object.Collection())
	collection.RemoveId(object.Id)
	if trigger {
		go object.FindAndExecuteTriggerForEvent(endpoint + ":destroy")
	}
}
func (object *Group) Save() {
	connection := database.GetConnection()
	defer connection.Release()
	logger.Debug("[Database] Saving Group")

	collection := connection.C(object.Collection())
	if object.Id == bson.ObjectId("") {
		object.Id = bson.NewObjectId()
		collection.Insert(object)
	} else {
		collection.UpdateId(object.Id, object)
	}
}
示例#8
0
func (application Application) FindUsersByIds(ids []bson.ObjectId) []User {
	results := []User{}

	in := map[string]interface{}{"$in": ids}

	query := map[string]interface{}{"_id": in}

	connection := database.GetConnection()
	defer connection.Release()

	database.Query(connection, application.Id.Hex()+"_users", query).All(&results)

	return results
}
func (object *Session) Save() {

	connection := database.GetConnection()
	defer connection.Release()
	logger.Debug("[Database] Saving Session")

	collection := connection.C(object.Collection())
	if object.Id == bson.ObjectId("") {
		object.Id = bson.NewObjectId()
		collection.Insert(object)
	} else {
		collection.UpdateId(object.Id, object)
	}

	b, _ := json.Marshal(object)
	key := object.ApplicationId.Hex() + ".sessions." + object.Id.Hex()
	cache.Set(key, string(b), 5*time.Minute)
}
示例#10
0
func (application Application) AllEndpoints() []Endpoint {
	var results []Endpoint

	query_map := map[string]interface{}{}

	collection := application.Id.Hex() + "_endpoints"

	// Reserve a connection (And ensure we release it)
	connection := database.GetConnection()

	// Query the database
	database.Query(connection, collection, query_map).All(&results)

	// Release the database connection
	connection.Release()

	return results
}
func (object *Endpoint) Save() {

	connection := database.GetConnection()
	defer connection.Release()
	logger.Debug("[Database] Saving Endpoint")

	collection := connection.C(object.Collection())

	if object.Id == bson.ObjectId("") {
		object.Id = bson.NewObjectId()
		collection.Insert(object)
	} else {
		collection.UpdateId(object.Id, object)
	}

	key := object.ApplicationId.Hex() + ".endpoints." + object.Id.Hex()
	cache.Clear(key)

}
示例#12
0
func runCron() {
	results := []model.Application{}
	query_map := map[string]interface{}{}

	collection := "applications"

	// Reserve a connection (And ensure we release it)
	connection := database.GetConnection()

	// Query the database
	database.Query(connection, collection, query_map).All(&results)

	// Release the database connection
	connection.Release()

	for _, application := range results {
		application.ExecuteCronTrigger()
	}
}
func (object *User) Save() bool {

	connection := database.GetConnection()
	defer connection.Release()
	logger.Debug("[Database] Saving User")

	collection := connection.C(object.Collection())

	if object.Id == bson.ObjectId("") {
		object.Id = bson.NewObjectId()
		err := collection.Insert(object)
		if err != nil {
			return mgo.IsDup(err)
		}
	} else {
		collection.UpdateId(object.Id, object)
	}

	key := object.ApplicationId.Hex() + ".users." + object.Id.Hex()
	b, _ := json.Marshal(object)
	cache.SetSync(key, string(b), 5*time.Minute)

	return false
}
示例#14
0
func (r *Record) UpdateReferenceMapsWithoutConcurrency() {
	connection := database.GetConnection()
	r.UpdateOwnReferences(connection)
	connection.Release()
	r.UpdateDependantObjects(database.GetConnection())
}
示例#15
0
func (e Endpoint) findRecordsWhereWithCount(query map[string]interface{}, groups []bson.ObjectId, super_user bool, action string, should_count bool) ([]Record, int) {
	var results []Record

	// Handle the $show_count param
	if !should_count && query["$show_count"] != nil {
		if query["$show_count"].(bool) == true {
			should_count = true
		}

		delete(query, "$show_count")
	}

	// Get the collection name
	collection := e.ApplicationId.Hex() + "_" + e.Id.Hex()

	// Sanitize and Nest the Query
	query_map := sanitizeQueryAsNested(query, false)

	// Check if the user is covered by default groups
	if !super_user && !e.CanGroupsPerform(groups, action) {
		// If not add a restriction on the query
		find := query_map["$find"].(map[string]interface{})
		find["permissions."+action+"_group_ids"] = map[string]interface{}{"$in": groups}
		query_map["$find"] = find
	}

	// Reserve a connection (And ensure we release it)
	connection := database.GetConnection()

	// Query the database
	query_cursor := database.Query(connection, collection, query_map["$find"].(map[string]interface{}))

	count := 0

	if should_count {
		count, _ = query_cursor.Count()
	} else {
		count = -1
	}

	for key, value := range query_map {
		if key == "$find" {

		} else if key == "$sort" {
			keys := []string{}

			for key, v := range value.(map[string]interface{}) {

				object_type := reflect.TypeOf(v).String()

				n := int64(1)

				if object_type == "json.Number" {
					n, _ = (v.(json.Number)).Int64()
				} else if object_type == "int" {
					n = int64(v.(int))
				}

				if n == -1 {
					keys = append(keys, "-"+key)
				} else {
					keys = append(keys, key)
				}
			}

			query_cursor = query_cursor.Sort(keys...)
		} else if key == "$limit" {
			is := (value.(json.Number)).String()
			i, _ := strconv.Atoi(is)
			query_cursor = query_cursor.Limit(i)
		} else if key == "$skip" {
			is := (value.(json.Number)).String()
			i, _ := strconv.Atoi(is)
			query_cursor = query_cursor.Skip(i)
		} else if key == "$only" {
			//fields, _ := bson.Marshal(value.(map[string]interface{}))
			query_cursor = query_cursor.Select(value)
		}
	}

	query_cursor.All(&results)

	connection.Release()

	// Return our results
	return results, count
}