Exemple #1
0
def login(params, auth, cb):
    response = db.get_one('demo_users').where(
        COND("username", "==", params["username"])).apply()
    if response.status == 200:
        res = response.result
        if res["username"] == params["username"] and res["password"] == params[
                "password"]:
            cb(
                'response', {
                    'ack':
                    True,
                    'token':
                    jwt.encode(
                        {
                            "username": res["username"],
                            "password": res["password"]
                        },
                        SECRET,
                        algorithm='HS256').decode('utf-8')
                })
        else:
            cb('response', {'ack': False})
    else:
        print(response.error)
        cb('response', {'ack': False})
Exemple #2
0
def func(op_sel, i):
    if op_sel == 0:
        return db.insert('books').doc({
            variables.id: i,
            "name": "MyBook" + str(i),
            "author": "Author" + str(i)
        }).apply()
    elif op_sel == 1:
        return db.update('books').set({
            "author": "myself" + str(i)
        }).where(COND(variables.id, "==", i)).apply()
    elif op_sel == 2:
        return db.delete_one('books').where(COND(variables.id, "==",
                                                 i)).apply()
    else:
        return db.get('books').apply()
Exemple #3
0
from space_api import API, COND

api = API('books-app', 'localhost:4124')
db = api.my_sql()

b = db.begin_batch()
b.add(db.insert('books').doc({"name": "MyBook", "author": "John Doe"}))
b.add(db.insert('books').doc({"name": "Book_name", "author": "John Doe"}))
b.add(db.insert('books').docs([{"name": "BookName"}, {"name": "BookName"}]))
b.add(db.delete('books').where(COND('name', '==', 'Book_name')))
response = b.apply()
print(response)
# UPDATE
from space_api import API, AND, OR, COND
api = API("books-app", "localhost:4124")
db = api.my_sql()

# The condition to be matched
condition = COND("author", "==", "author1")

# Update the books
response = db.update("books").where(condition).set({"name": "A book"}).apply()

if response.status == 200:
    print("Success")
else:
    print(response.error)


# ----------------------------------------------------------------------------------------------------
# UPDATE ONE
from space_api import API, AND, OR, COND
api = API("books-app", "localhost:4124")
db = api.my_sql()

# The condition to be matched
condition = COND("author", "==", "author1")

# Update the books
response = db.update_one("books").where(condition).set({"name": "A book"}).apply()

if response.status == 200:
    print("Success")
print("limit one")
print(db.get_one('books').limit(2).apply())
print("skip all")
print(db.get('books').skip(2).apply())
print("skip one")
print(db.get_one('books').skip(2).apply())
print("sort all")
print(db.get('books').sort('-author').apply())
print("sort one")
print(db.get_one('books').sort('author').apply())
print("select all")
print(db.get('books').select({'author': 1}).apply())
print("select one")
print(db.get_one('books').select({'author': 1}).apply())
print("where all")
print(db.get('books').where(COND("name", "==", "Book_name")).apply())
print("where one")
print(db.get_one('books').where(COND("name", "==", "BookName")).apply())
print("distinct")
print(db.distinct("books").key("author").apply())
print("count")
print(db.count("books").apply())
print("count where")
print(db.count("books").where(COND("author", "==", "Jon Doe")).apply())

# update
print("set")
print(db.update('books').set({"author": "myself"}).apply())
print("set where")
print(
    db.update('books').where(COND("author", "==", "some_author")).set({
    db.insert('books').doc({
        "id": 1,
        "name": "MyBook" + str(1),
        "author": "Author" + str(1)
    }).apply())

i = initial
while True:
    print(
        "Insert",
        db.insert('books').doc({
            "id": i,
            "name": "MyBook" + str(i),
            "author": "Author" + str(i)
        }).apply())
    print(db.get('books').apply().result)
    time.sleep(2)
    print(
        "Update",
        db.update('books').set({
            "author": "myself" + str(i)
        }).where(COND("id", "==", i)).apply())
    print(db.get('books').apply().result)
    time.sleep(2)
    print("Delete", db.delete_one('books').where(COND("id", "==", i)).apply())
    print(db.get('books').apply().result)
    time.sleep(4)
    i += 1

api.close()
Exemple #7
0
# READ
from space_api import API, AND, OR, COND

# Initialize api with the project name and url of the space cloud
api = API("books-app", "localhost:4124")

# Initialize database(s) you intend to use
db = api.my_sql()

# The condition to be matched
condition = COND("id", "==", "1")

# Get the books
response = db.get("books").where(condition).apply()
if response.status == 200:
    print(response.result)
else:
    print(response.error)

# ----------------------------------------------------------------------------------------------------
# READ ONE
from space_api import API, AND, OR, COND

# Initialize api with the project name and url of the space cloud
api = API("books-app", "localhost:4124")

# Initialize database(s) you intend to use
db = api.my_sql()

# The condition to be matched
condition = COND("author", "==", "SomeAuthor")
# DELETE
from space_api import API, COND

# Initialize api with the project name and url of the space cloud
api = API("books-app", "localhost:4124")

# Initialize database(s) you intend to use
db = api.my_sql()

# The condition to be matched
condition = COND("name", "==", "SomeAwesomeBook")

# Delete all books which match a particular condition
response = db.delete("books").where(condition).apply()
if response.status == 200:
    print("Success")
else:
    print(response.error)

# ----------------------------------------------------------------------------------------------------
# DELETE ONE
from space_api import API, COND

# Initialize api with the project name and url of the space cloud
api = API("books-app", "localhost:4124")

# Initialize database(s) you intend to use
db = api.my_sql()

# The condition to be matched
condition = COND("name", "==", "SomeAwesomeBook")
Exemple #9
0
from space_api.utils import generate_find
from space_api import AND, COND, OR

condition = AND(COND('a', '>=', 1), COND('a', '<=', 10))
print(generate_find(condition))