docstr: The Database Component enables to interact with an SQLite database to store some structured information
  about your application.
  
  The provided models are SQLModel tables
  
  Arguments:
      models: A SQLModel or a list of SQLModels table to be added to the database.
      db_filename: The name of the SQLite database.
      store_interval: Time interval (in seconds) at which the database is periodically synchronized to the Drive.
                      Note that the database is also always synchronized on exit.
      debug: Whether to run the database in debug mode.
  
  Example::
  
      from typing import List
      from sqlmodel import SQLModel, Field
      from uuid import uuid4
  
      from lightning.app import LightningFlow, LightningApp
      from lightning.app.components.database import Database, DatabaseClient
  
      class CounterModel(SQLModel, table=True):
          __table_args__ = {"extend_existing": True}
  
          id: int = Field(default=None, primary_key=True)
          count: int
  
  
      class Flow(LightningFlow):
  
          def __init__(self):
              super().__init__()
              self._private_token = uuid4().hex
              self.db = Database(models=[CounterModel])
              self._client = None
              self.counter = 0
  
          def run(self):
              self.db.run(token=self._private_token)
  
              if not self.db.alive():
                  return
  
              if self.counter == 0:
                  self._client = DatabaseClient(
                      model=CounterModel,
                      db_url=self.db.url,
                      token=self._private_token,
                  )
  
              rows = self._client.select_all()
  
              print(f"{self.counter}: {rows}")
  
              if not rows:
                  self._client.insert(CounterModel(count=0))
              else:
                  row: CounterModel = rows[0]
                  row.count += 1
                  self._client.update(row)
  
              if self.counter >= 100:
                  row: CounterModel = rows[0]
                  self._client.delete(row)
                  self.stop()
  
              self.counter += 1
  
      app = LightningApp(Flow())
  
  If you want to use nested SQLModels, we provide a utility to do so as follows:
  
  Example::
  
      from typing import List
      from sqlmodel import SQLModel, Field
      from sqlalchemy import Column
  
      from lightning.app.components.database.utilities import pydantic_column_type
  
      class KeyValuePair(SQLModel):
          name: str
          value: str
  
      class CounterModel(SQLModel, table=True):
          __table_args__ = {"extend_existing": True}
  
          name: int = Field(default=None, primary_key=True)
  
          # RIGHT THERE ! You need to use Field and Column with the `pydantic_column_type` utility.
          kv: List[KeyValuePair] = Field(..., sa_column=Column(pydantic_column_type(List[KeyValuePair])))
docstr_tok: ["The","Database","Component","enables","to","interact","with","an","SQLite","database","to","store","some","structured","information","about","your","application",".","The","provided","models","are","SQLModel","tables","Arguments",":","models",":","A","SQLModel","or","a","list","of","SQLModels","table","to","be","added","to","the","database",".","db_filename",":","The","name","of","the","SQLite","database",".","store_interval",":","Time","interval","(","in","seconds",")","at","which","the","database","is","periodically","synchronized","to","the","Drive",".","Note","that","the","database","is","also","always","synchronized","on","exit",".","debug",":","Whether","to","run","the","database","in","debug","mode",".","Example",":",":","from","typing","import","List","from","sqlmodel","import","SQLModel",",","Field","from","uuid","import","uuid4","from","lightning.app","import","LightningFlow",",","LightningApp","from","lightning.app.components.database","import","Database",",","DatabaseClient","class","CounterModel","(","SQLModel",",","table=True",")",":","__table_args__","=","{","``","extend_existing","''",":","True","}","id",":","int","=","Field","(","default=None",",","primary_key=True",")","count",":","int","class","Flow","(","LightningFlow",")",":","def","__init__","(","self",")",":","super","(",")",".__init__","(",")","self._private_token","=","uuid4","(",")",".hex","self.db","=","Database","(","models=","[","CounterModel","]",")","self._client","=","None","self.counter","=","0","def","run","(","self",")",":","self.db.run","(","token=self._private_token",")","if","not","self.db.alive","(",")",":","return","if","self.counter","==","0",":","self._client","=","DatabaseClient","(","model=CounterModel",",","db_url=self.db.url",",","token=self._private_token",",",")","rows","=","self._client.select_all","(",")","print","(","f","''","{","self.counter","}",":","{","rows","}","''",")","if","not","rows",":","self._client.insert","(","CounterModel","(","count=0",")",")","else",":","row",":","CounterModel","=","rows","[","0","]","row.count","+=","1","self._client.update","(","row",")","if","self.counter",">","=","100",":","row",":","CounterModel","=","rows","[","0","]","self._client.delete","(","row",")","self.stop","(",")","self.counter","+=","1","app","=","LightningApp","(","Flow","(",")",")","If","you","want","to","use","nested","SQLModels",",","we","provide","a","utility","to","do","so","as","follows",":","Example",":",":","from","typing","import","List","from","sqlmodel","import","SQLModel",",","Field","from","sqlalchemy","import","Column","from","lightning.app.components.database.utilities","import","pydantic_column_type","class","KeyValuePair","(","SQLModel",")",":","name",":","str","value",":","str","class","CounterModel","(","SQLModel",",","table=True",")",":","__table_args__","=","{","``","extend_existing","''",":","True","}","name",":","int","=","Field","(","default=None",",","primary_key=True",")","#","RIGHT","THERE","!","You","need","to","use","Field","and","Column","with","the","`","pydantic_column_type","`","utility",".","kv",":","List","[","KeyValuePair","]","=","Field","(","...",",","sa_column=Column","(","pydantic_column_type","(","List","[","KeyValuePair","]",")",")",")"]
code: def __init__(
      self,
      models: Union[Type["SQLModel"], List[Type["SQLModel"]]],
      db_filename: str = "database.db",
      store_interval: int = 10,
      debug: bool = False,
  ) -> None:
      """The Database Component enables to interact with an SQLite database to store some structured information
      about your application.
  
      The provided models are SQLModel tables
  
      Arguments:
          models: A SQLModel or a list of SQLModels table to be added to the database.
          db_filename: The name of the SQLite database.
          store_interval: Time interval (in seconds) at which the database is periodically synchronized to the Drive.
                          Note that the database is also always synchronized on exit.
          debug: Whether to run the database in debug mode.
  
      Example::
  
          from typing import List
          from sqlmodel import SQLModel, Field
          from uuid import uuid4
  
          from lightning.app import LightningFlow, LightningApp
          from lightning.app.components.database import Database, DatabaseClient
  
          class CounterModel(SQLModel, table=True):
              __table_args__ = {"extend_existing": True}
  
              id: int = Field(default=None, primary_key=True)
              count: int
  
  
          class Flow(LightningFlow):
  
              def __init__(self):
                  super().__init__()
                  self._private_token = uuid4().hex
                  self.db = Database(models=[CounterModel])
                  self._client = None
                  self.counter = 0
  
              def run(self):
                  self.db.run(token=self._private_token)
  
                  if not self.db.alive():
                      return
  
                  if self.counter == 0:
                      self._client = DatabaseClient(
                          model=CounterModel,
                          db_url=self.db.url,
                          token=self._private_token,
                      )
  
                  rows = self._client.select_all()
  
                  print(f"{self.counter}: {rows}")
  
                  if not rows:
                      self._client.insert(CounterModel(count=0))
                  else:
                      row: CounterModel = rows[0]
                      row.count += 1
                      self._client.update(row)
  
                  if self.counter >= 100:
                      row: CounterModel = rows[0]
                      self._client.delete(row)
                      self.stop()
  
                  self.counter += 1
  
          app = LightningApp(Flow())
  
      If you want to use nested SQLModels, we provide a utility to do so as follows:
  
      Example::
  
          from typing import List
          from sqlmodel import SQLModel, Field
          from sqlalchemy import Column
  
          from lightning.app.components.database.utilities import pydantic_column_type
  
          class KeyValuePair(SQLModel):
              name: str
              value: str
  
          class CounterModel(SQLModel, table=True):
              __table_args__ = {"extend_existing": True}
  
              name: int = Field(default=None, primary_key=True)
  
              # RIGHT THERE ! You need to use Field and Column with the `pydantic_column_type` utility.
              kv: List[KeyValuePair] = Field(..., sa_column=Column(pydantic_column_type(List[KeyValuePair])))
  
      """
      super().__init__(
          parallel=True, cloud_build_config=BuildConfig(["sqlmodel"])
      )
      self.db_filename = db_filename
      self._root_folder = os.path.dirname(db_filename)
      self.debug = debug
      self.store_interval = store_interval
      self._models = models if isinstance(models, list) else [models]
      self._store_thread = None
      self._exit_event = None
  
imports: import asyncio
  import os
  import sqlite3
  import sys
  import tempfile
  import threading
  import traceback
  from typing import List, Optional, Type, Union
  import uvicorn
  from fastapi import FastAPI
  from uvicorn import run
  from lightning.app.components.database.utilities import _create_database, _Delete, _Insert, _SelectAll, _Update
  from lightning.app.core.work import LightningWork
  from lightning.app.storage import Drive
  from lightning.app.utilities.app_helpers import Logger
  from lightning.app.utilities.imports import _is_sqlmodel_available
  from lightning.app.utilities.packaging.build_config import BuildConfig
label_desc: Use image node_id 1 to create a new Database object from inherited base classes: LightningWork with example: obj = Database(models, db_filename, store_interval, debug)