Skip to main content

A JameSQL database implemented in Python.

Project description

test workflow

JameSQL

A JameSQL database implemented in Python.

This project has support for:

  • Inserting records
  • Deleting records
  • Searching for records that match a query
  • Searching for records that match multiple query conditions

This database does not enforce a schema, so you can insert records with different fields.

Installation

To install this project, run:

git clone https://github.com/capjamesg/jamesql
cd jamesql
pip3 install -e .

Usage

Create a database

To create a database, use the following code:

from nosql import NoSQL

index = JameSQL()

Add documents to a database

To add documents to a database, use the following code:

index.add({"title": "tolerate it", "artist": "Taylor Swift"})
index.insert({"title": "betty", "artist": "Taylor Swift"})

When documents are added, a uuid key is added for use in uniquely identifying the document.

Search for documents

A query has the following format:

{
    "query": {},
    "limit": 2,
    "sort_by": "song",
    "skip": 1
}
  • query is a dictionary that contains the fields to search for.
  • limit is the maximum number of documents to return. (default 10)
  • sort_by is the field to sort by. (default None)
  • skip is the number of documents to skip. This is useful for implementing pagination. (default 0)

limit, sort_by, and skip are optional.

Within the query key you can query for documents that match one or more conditions.

There are three operators you can use for condition matching:

  • equals
  • contains
  • starts_with

Here is an example of a query that searches for documents that have the artist field set to Taylor Swift:

query = {
    "query": {
        "artist": {
            "equals": "Taylor Swift"
        }
    }
}

You can also search for documents that have the artist field set to Taylor Swift and the title field set to tolerate it:

query = {
    "query": {
        "and": [
            {
                "artist": {
                    "equals": "Taylor Swift"
                }
            },
            {
                "title": {
                    "equals": "tolerate it"
                }
            }
        ]
    }
}

You can nest conditions to create complex queries, like:

query = {
    "query": {
        "or": {
            "and": [
                {"title": {"starts_with": "tolerate"}},
                {"title": {"contains": "it"}},
            ],
            "lyric": {"contains": "kiss"},
        }
    },
    "limit": 2,
    "sort_by": "title",
}

To search for documents that match a query, use the following code:

result = index.search(query)

This will return a list of documents that match the query.

Update documents

You need a document UUID to update a document. You can retrieve a UUID by searching for a document.

Here is an example showing how to update a document:

response = index.search(
    {
        "query": {"title": {"equals": "tolerate it"}},
        "limit": 10,
        "sort_by": "title",
    }
)

uuid = response["documents"][0]["uuid"]

index.update(uuid, {"title": "tolerate it (folklore)", "artist": "Taylor Swift"})

update is an override operation. This means you must provide the full document that you want to save, instead of only the fields you want to update.

Delete documents

You need a document UUID to delete a document. You can retrieve a UUID by searching for a document.

Here is an example showing how to delete a document:

response = index.search(
    {
        "query": {"title": {"equals": "tolerate it"}},
        "limit": 10,
        "sort_by": "title",
    }
)

uuid = response["documents"][0]["uuid"]

index.remove(uuid)

You can validate the document has been deleted using this code:

response = index.search(
    {
        "query": {"title": {"equals": "tolerate it"}},
        "limit": 10,
        "sort_by": "title",
    }
)

assert len(response["documents"]) == 0

Testing

This project comes with two test suites:

  • Unit tests
  • A benchmarking test

You can run the unit tests using:

pytest tests/test.py

You can run the benchmark test using:

pytest tests/stress.py

The benchmark test evaluates how long it takes to run a query in a database with 300,000 records.

License

This project is licensed under an MIT license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

jamesql-0.1.0.tar.gz (7.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

jamesql-0.1.0-py3-none-any.whl (9.4 kB view details)

Uploaded Python 3

File details

Details for the file jamesql-0.1.0.tar.gz.

File metadata

  • Download URL: jamesql-0.1.0.tar.gz
  • Upload date:
  • Size: 7.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.9

File hashes

Hashes for jamesql-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d77fd9c94871ab698b877f69f3607bae93532b609e2c3a712398d4caea9d89ec
MD5 b9057979cf11d80b1ea00e06f21e1d67
BLAKE2b-256 5ebb6305721fa9492169e8fa24cf8269554ad1de3db37851cf2946f3d70ef1e5

See more details on using hashes here.

File details

Details for the file jamesql-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: jamesql-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 9.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.9

File hashes

Hashes for jamesql-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 99156447e67c9b3bd034023cffefc87e2c1cb0c0286a37925d664fcc2bc13b99
MD5 e7870504d56186856759adc8f1b45494
BLAKE2b-256 248e0858604780d305c992a8d15314680ffab9a1aa876c853fba7fce5a2ac222

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page