Package: pyfreedb
PyFreeDBShip Faster with Google Sheets as a Database!PyFreeDBis a Python library that provides common and simple database abstractions on top of Google Sheets.FeaturesProvide a straightforwardkey-valueandrow based databaseinterfaces on top of Google Sheets.Serve your datawithout any server setup(by leveraging Google Sheets infrastructure).Supportflexible enough query languageto perform various data queries.Manually manipulate datavia the familiar Google Sheets UI (no admin page required).For more details, please readour analysison other alternatives and how it compares withFreeDB.Table of ContentsProtocolsGetting StartedInstallationPre-requisitesRow StoreQuerying RowsCounting RowsInserting RowsUpdating RowsDeleting RowsModel Field to Column MappingKV StoreGet ValueSet KeyDelete KeySupported ModesProtocolsClients are strongly encouraged to read through theprotocols documentto see how things work
under the hood andthe limitations.Getting StartedInstallationpip install pyfreedbPre-requisitesObtain a GoogleOAuth2orService Accountcredentials.Prepare a Google Sheets spreadsheet where the data will be stored.Row StoreLet's assume each row in the table is represented by thePersonobject.frompyfreedb.rowimportmodelsclassPerson(models.Model):name=models.StringField()age=models.IntegerField()frompyfreedb.providers.google.authimportServiceAccountGoogleAuthClient,OAuth2GoogleAuthClientfrompyfreedb.rowimportGoogleSheetRowStore,AUTH_SCOPES# If using Google Service Account.auth_client=ServiceAccountGoogleAuthClient.from_service_account_file("<path_to_service_account_json>",scopes=AUTH_SCOPES,)# If using Google OAuth2 Flow.auth_client=OAuth2GoogleAuthClient.from_authorized_user_file("<path_to_cached_credentials_json>",client_secret_filename="<path_to_client_secret_json>",scopes=AUTH_SCOPES,)store=GoogleSheetRowStore(auth_client,spreadsheet_id="<spreadsheet_id>",sheet_name="<sheet_name>",object_cls=Person,)Querying Rows# Select all columns of all rows.rows=store.select().execute()# Select a few columns for all rows (non-selected struct fields will have default value).rows=store.select("name").execute()# Select rows with conditions.rows=store.select().where("name = ? OR age >= ?","freedb",10).execute()# Select rows with sorting/order by.frompyfreedb.rowimportOrderingrows=store.select().order_by(Ordering.ASC("name"),Ordering.DESC("age")).execute()# Select rows with offset and limitrows=store.select().offset(10).limit(20).execute()Counting Rows# Count all rows.count=store.count().execute()# Count rows with conditions.count=store.count().where("name = ? OR age >= ?","freedb",10).execute()Inserting Rowsrows=[Person(name="no_pointer",age=10),Person(name="with_pointer",age=20)]store.insert(rows).execute()Updating Rows# Update all rows.store.update({"name":"new_name","age":100}).execute()# Update rows with conditions.store.update({"name":"new_name","age":100}).where("name = ? OR age >= ?","freedb",10).execute()Deleting Rows# Delete all rows.store.delete().execute()# Delete rows with conditions.store.delete().where("name = ? OR age >= ?","freedb",10).execute()Model Field to Column MappingYou can pass keyword argumentcolumn_nameto theFieldconstructor when defining the models to change the column
name in the sheet. Without this keyword argument, the library will use the field name as the column name (case
sensitive).# This will map to the exact column name of "name" and "age".classPerson(models.Model):name=models.StringField()age=models.IntegerField()# This will map to the exact column name of "Name" and "Age".classPerson(models.Model):name=models.StringField(column_name="Name")age=models.IntegerField(column_name="Age")KV Storefrompyfreedb.providers.google.authimportServiceAccountGoogleAuthClient,OAuth2GoogleAuthClientfrompyfreedb.kvimportGoogleSheetKVStore,AUTH_SCOPES# If using Google Service Account.auth_client=ServiceAccountGoogleAuthClient.from_service_account_file("<path_to_service_account_json>",scopes=AUTH_SCOPES,)# If using Google OAuth2 Flow.auth_client=OAuth2GoogleAuthClient.from_authorized_user_file("<path_to_cached_credentials_json>",client_secret_filename="<path_to_client_secret_json>",scopes=AUTH_SCOPES,)store=GoogleSheetKVStore(auth_client,spreadsheet_id="<spreadsheet_id>",sheet_name="<sheet_name>",mode=GoogleSheetKVStore.APPEND_ONLY_MODE,)Get ValueIf the key is not found,pyfreedb.kv.KeyNotFoundErrorwill be returned.store.get("k1")Set Keystore.set("k1",b"some_value")Delete Keystore.delete("k1")Supported ModesFor more details on how the two modes are different, please read theprotocol document.There are 2 different modes supported:Default mode.Append only mode.// Default modestore=GoogleSheetKVStore(auth_client,spreadsheet_id="<spreadsheet_id>",sheet_name="<sheet_name>",mode=GoogleSheetKVStore.DEFAULT_MODE,)// Append only modestore=GoogleSheetKVStore(auth_client,spreadsheet_id="<spreadsheet_id>",sheet_name="<sheet_name>",mode=GoogleSheetKVStore.APPEND_ONLY_MODE,)LicenseThis project isMIT licensed.