Skip to content

Could give a example for creating, deleting, updating, selecting? #113

Closed

Description

@kdurant

I am a novice, if only there is a detailed tutorial.

Activity

  1. changed the title [-]Can give a example for creating, deleting, updating, selecting?[/-] [+]Could give a example for creating, deleting, updating, selecting?[/+] on Sep 6, 2021
  2. kkharji commented on Sep 6, 2021

    @kkharji
    Owner

    Here's something I just wrote, I will try to include example in the readme soon

    local has_sqlite, sqlite = pcall(require, "sqlite")
    if not has_sqlite then
      print "Couldn't find sqlite.lua. Cannot use persistent history"
      return nil
    end
    
    ---@class NeoclipEntry @entry content
    ---@field regtype string
    ---@field contents string[]
    ---@field filetype string
    
    ---@class NeoClipTable: sqlite_tbl
    local M = sqlite.tbl("neoclip", {
      id = true,
      regtype = "text",
      contents = "luatable",
      filetype = "text",
    })
    
    function M:init(config)
      config = config or {}
      local db_path = config.db_path or vim.fn.stdpath "data" .. "/databases/neoclip.sqlite3"
      self:set_db(sqlite.new(db_path)) --- directory ensuring should be out of this module
    end
    
    M:init() --- called outside the module to set user db path
    
    ---Insert new neclip entry
    ---@param entry NeoclipEntry[] | NeoclipEntry
    function M:insert(entry)
      -- do some pre processing before inserting a row
      return self:__insert(entry)
    end
    
    M:insert {
      {
        contents = { "line 1", "  line 2", "line 3" },
        filetype = "lua",
        regtype = "l",
      },
      {
        contents = { "some other string" },
        filetype = "lua",
        regtype = "l",
      },
    }
    
    ---Get content using {query} if non-nil
    ---@param query sqlite_query_select|nil @(look into lua-dev.nvim to get autocomletion)
    ---@return NeoclipEntry[]
    function M:get(query)
      local entries = self:__get(query)
      -- do some sorting or stuff you need.
      return entries
    end
    
    print(vim.inspect(M:get()))
    
    return M
    
  3. kdurant commented on Sep 7, 2021

    @kdurant
    Author

    @tami5 Thank you for your help.

  4. JeysonFlores commented on Sep 21, 2021

    @JeysonFlores

    I agree with @kdurant . For what I saw on the example, this library it seems to have pretty advanced and handy ways to manage an sqlite database however it's quite cryptic to read the doc/sqlite.txt file. With the example that @tami5 wrote in this issue my doubts increased instead of being resolved. I hope you consider make a step by step example, it seems to be a really great library I can't really put my hands on it just because I don't have enough information

  5. kkharji commented on Sep 21, 2021

    @kkharji
    Owner

    @JeysonFlores 😆 sorry to hear that, yah. it's a library with a style guide missing. Part of the complexity comes from the fact that this library is both a binding and an opinionated wrapper and there is multiple ways to use it. I'd love to create step by step example to explain it all, but I'm afraid I might not be able to. However, I managed to cleanup and add bookmark manager example

    For now checkout powered by sqlite.lua, it's a good place to start for create something powered by sqlite.lua, some use low level api and other uses the wrapper

  6. Repository owner locked and limited conversation to collaborators on Sep 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions