Coder Social home page Coder Social logo

litejdb's Introduction

LiteJDB (Lite JSON Database)

LiteJDB is a lightweight database management system prototype, Pandas based, designed for simplicity and flexibility. It efficiently stores and manages data using JSON serialization, making it easy to work with structured information. It's a personal experiment for small projects that require a quick and easy solution for data storage and search.

Example

from litejdb import LiteJDB

# Initialize a generic database with a given entity name
student_database = LiteJDB('student_database.json')

add()

Add a new record to the database

student_database.add({"first_name": "John", "last_name": "Doe", "age": 25})
student_database.add({"first_name": "Jane", "last_name": "Smith", "age": 30})
student_database.add({"first_name": "John", "last_name": "Smith", "age": 22})

save()

Save the database to a JSON file

student_database.save()

load()

Load the database from a JSON file

student_database.load()

fields()

Return the list of all fields

fields = student_database.fields()
print (fields)
['first_name', 'last_name', 'age']

query()

Filter records based on multiple fields and their values

filters = "first_name == 'John' and last_name == 'Doe' and age > 23"
result = student_database.query(filters)
print (result)
  first_name last_name  age
0       John       Doe   25
# Return id list
result = student_database.query(filters, "get_id")

[0]

get()

Retrieve a record by its ID

obj = student_database.get(1)
print (obj)
first_name     Jane
last_name     Smith
age              30
Name: 1, dtype: object

Retrieve first_name field by its ID

first_name = student_database.get(1).first_name
print (first_name)

Jane

update()

Update/replace field value by its ID

student_database.update(0, 'first_name', 'Johnny')
print (student_database.df().head())
  first_name last_name  age
0     Johnny       Doe   25
1       Jane     Smith   30
2       John     Smith   22

delete()

Delete a record by its ID

student_database.delete(1)

Delete a records 0 and 1 by their ID

student_database.delete([0, 1])

df()

Use database as Pandas dataframe

df = student_database.df().head()
print (df)
  first_name last_name  age
0       John       Doe   25
1       Jane     Smith   30
2       John     Smith   22

Iterate over rows

for index, row in student_database.df().iterrows():
  print (row['first_name'])
Johnny
Jane
John

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.