Skip to content

mateusabelli/refine-sqlite

Folders and files

NameName
Last commit message
Last commit date

Latest commit

f10541f · Nov 1, 2023

History

39 Commits
Oct 20, 2023
Oct 21, 2023
Oct 21, 2023
Sep 16, 2023
Aug 30, 2023
Oct 15, 2023
Aug 30, 2023
Oct 29, 2023
Aug 30, 2023
Nov 1, 2023
Oct 15, 2023
Sep 29, 2023
Sep 23, 2023
Aug 30, 2023
Aug 30, 2023
Aug 30, 2023

Repository files navigation

refine-sqlite

Connector for backends created with SQLite.

npm version npm GitHub license Node.js CI


Getting Started

With refine-sqlite you can quickly start creating your app as fast as possible by leveraging the easy-to-use methods powered by refine to interact with your SQLite database.

Features

  • Easy to use - Just import the data provider and use it in your code.
  • Lightweight - The SQLite database is lightweight and can be used locally.
  • Simple - You won't need to type SQL queries, just use the data provider methods. Except for the tables creation, which you can easily do with the DB Browser for SQLite.

Installation

npm install refine-sqlite

Usage

First, create a database file. You can use the DB Browser for SQLite to easily create the tables and insert some data, or you can also use the sqlite3 command line shell.

Then, import the data provider to use in an async function, and pass the database file path as a parameter.

Finally, use the methods to create, update, delete, and get data from your database, filtering and sorting as you wish.

Note resource is the name of the table in the database.

Full example

import { dataProvider } from "refine-sqlite";

async function getPosts() {
  const response = await dataProvider("database.db")
    .getList({
      resource: "posts",
      filters: [
        {
          field: "category_id",
          operator: "eq",
          value: ["2"],
        },
      ],
      sorters: [
        {
          field: "title",
          order: "asc",
        },
      ],
    });
  const { data } = response;
  return data;
}

Expected data from response

Using the test.db database file.

{
  data: [
    { id: 6, title: 'Dolorem unde et officiis.', category_id: 2 },
    { id: 1, title: 'Soluta et est est.', category_id: 2 }
  ],
  total: 2
}

Available methods

Method Description
create() Creates a new record.
update() Updates a record.
deleteOne() Deletes a record.
getOne() Gets a single record.
getList() Gets a list of records.
getMany() Gets a list of records by their ids.

Note Not all the methods have been implemented. See all here.

How to use the methods (Click to expand)
  • create()

    create({ 
      resource: "posts",
      variables: { 
        title: "New post", 
        body: "New post body"
      }
    });
  • update()

    update({ 
      resource: "posts",
      id: 1,
      variables: {
        title: "Updated post" 
      } 
    });
  • deleteOne()

    deleteOne({ 
      resource: "posts",
      id: 1
    });
  • getOne()

    getOne({ 
      resource: "posts",
      id: 3
    });
  • getList()

    getList({ resource: "posts" });
  • getMany()

    getMany({ 
      resource: "posts",
      ids: [1, 2, 3]
    });

Available filters

Filter Description
eq Is equal to
ne Is not equal to
gte Greater than or equal to
lte Less than or equal to
contains Contains

Note Not all the filters have been implemented. See all here.

How to use the filters (Click to expand)
  • eq
    filters: [{
      field: "id", operator: "eq", value: 1
    }]
  • ne
    filters: [{
      field: "id", operator: "ne", value: 1
    }]
  • gte
    filters: [{
      field: "id", operator: "gte", value: 1
    }]
  • lte
    filters: [{
      field: "id", operator: "lte", value: 1
    }]
  • contains
    filters: [{
      field: "title", operator: "contains", value: "Lorem"
    }]

Available sorters

Order Description
asc Ascending order
desc Descending order

How to use the sorters (Click to expand)
  • asc
    sorters: [{
      field: "id", order: "asc"
    }]
  • desc
    sorters: [{
      field: "id", order: "desc"
    }]

Development

Clone the repository

git clone https://github.com/mateusabelli/refine-sqlite.git

Install the dependencies

cd refine-sqlite
pnpm install

Build and test

pnpm run build
pnpm run test

Important Before the tests run, the database file test.db is deleted and recreated.

Contributing

All contributions are welcome and appreciated! Please create an Issue or Pull Request if you encounter any problems or have suggestions for improvements.

If you want to say thank you or/and support active development of refine-sqlite

shoutout-sponsors

License

refine-sqlite is free and open-source software licensed under the MIT License.
The feather icon is from Phosphor Icons licensed under the MIT License.