GithubHelp home page GithubHelp logo

sqlitec's Introduction

A Dart plugin to generate boilerplate for your local database, featuring low coupling and type safety, completely inspired by the sqlc go library.

This plugin works in any OS and can be used with sqflite and sqflite_common_ffi

Features

Use this plugin in your dart application to:

  • Generate classes from your DDL code.
  • Generate methods from your queries with type safety.
  • Consume data from your database without needing to worry about type casting and all the boilerplate.

Getting started

To use this plugin you need to install sqflite or sqflite_common_ffi.

Usage

Create a .sql file anywhere in your lib folder and write your custom SQL commands.

create table customers (
    id integer primary key autoincrement,
    name varchar not null default null,
    status varchar not null default ''
);

--name getCustomerByName :one
select * from customers where name = ? and status = :status;

--name: insertCustomer :exec
insert into customers(name, status) values (?, ?);
Generated code:
// in file sqlitec/schemas.dart
class Customers {
  static const String $tableInfo = 'customers';
  static const String $createTableStatement = 'CREATE TABLE customers(id integer PRIMARY KEY AUTOINCREMENT, name varchar NOT NULL DEFAULT NULL, status varchar NOT NULL DEFAULT \'\')';
  int id;
  String name;
  String status;

  Customers({
    required this.id,
    required this.name,
    required this.status,
  });

  factory Customers.fromJson(Map<String, dynamic> jsonMap) {
    return Customers(
      id: (jsonMap['id'] as num).toInt(),
      name: jsonMap['name'] as String,
      status: jsonMap['status'] as String,
    );
  }
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'name': name,
      'status': status,
    };
  }

  String toString() {
    return '''Customers(
  id: $id,
  name: $name,
  status: $status,
)''';  }

...
// methods inside Queries class on sqlitec/queries.sqlitec.dart
Future<Customers?> getCustumerByNameAndStatus(String $arg1, {
  required String status,
}) async {
    final result = await db.rawQuery(
      'SELECT * FROM customers WHERE name = ? AND status = ?',
      [$arg1, status],
    );
    
    if (result.isEmpty) return null;
    final resultFirst = result.first;
    return Customers.fromJson(resultFirst);
}

Future<int> insertCustomer({
  required String name,
  required String status,
}) async {
    final result = await db.rawInsert(
      'INSERT INTO customers (name, status) VALUES (?, ?)',
      [
        name,
        status,
      ],
    );

    return result;
}

Using the generated code

final queries = Queries(db: /*your db instance*/);

await queries.insertCustomer(name: 'Bob', status: 'registered');
final user = await queries.getCustomerByNameAndStatus('Bob', status: 'registered');

print(user); // Customers(id: 1, name: Bob, status: registered,)

sqlitec's People

Contributors

kmsbin avatar

Stargazers

Hyunwoo Kim avatar

Watchers

 avatar

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.