Coder Social home page Coder Social logo

itinance / sql-schema-lite Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 22 KB

A simple SQL-builder in modern JavaScript especially for SQLite3

License: MIT License

JavaScript 100.00%
sql-statement sql-builder schema orm javascript sqlite3 sqlite-orm react-nat

sql-schema-lite's Introduction

sql-schema-lite

Motivation:

This is something like a SQL-Builder primarily in the context of mobile applications or web applications using Sqlite3, which is present on mobile devices and in any modern web browser (websql is sqlite3 under the hood).

It takes concrete schema definitions and supports the developer with building specific SQL-statements like SELECT, UPDATE, INSERT, DELETE. (DDL-statements like CREATE TABLE/ALTER TABLE are coming soon)

The library is supposed to be a loosely coupled SQL builder library without any bindings to concrete OS-related or database-related implementations, but can be used as a base library around SQL-statement building for those advanced implementations. So feel free to use this in any environment (whether it is server side with NodeJS, client side on browsers or cross platform mobile applications with Cordova, ReactNative and so on...)

That means, this library is NOT an ORM-implementation (Object relational mapper), but it is the outsourced part of a ReactNative-specific library react_native_sqlite_orm on top of react-native-sqlite-storage supporting sqlite3 for both iOS and Android, that is currently under heavy development and not being published yet (coming very soon).

Installation:

via yarn:

yarn add sqlite-schema-lite

via npm:

npm install --save sqlite-schema-lite

Running tests

Tests are written with jest. You need to install the jest-client best globally: npm install jest-cli.

yarn install
jest

Or if you're still using npm:

npm i
jest

Examples:

Define a schema per JS or JSON:

const schemaContact = {
  name: 'contact',
  defaultVersion: 1,

  // Version 1:

  1: {
    id: {
      type: SqliteTypes.INTEGER,
      autoIncrement: true,
      primaryKey: true,
    },
    first_name: {
      type: SqliteTypes.TEXT,
      index: true,
      nullable: true,
    },
    last_name: {
      type: SqliteTypes.TEXT,
      index: true,
      nullable: false,
    },
    street: {
      type: SqliteTypes.TEXT,
      index: true,
      nullable: false,
    },
    postalcode: {
      type: SqliteTypes.INTEGER,
      index: true,
      nullable: false,
    },
    city: {
      type: SqliteTypes.TEXT,
      index: true,
      nullable: false,
    },
  }
}

Let's create SQL-statements for SELECT, INSERT, UPDATE, DELETE automatically:

const schema = new ModelSchema( schemaContact )
const sql = new SqlBuilder(schema)

const entity = {
  id: 10,
  first_name: 'Alfons',
  last_name: 'Zitterbacke',
  street: 'Friedenseck 8',
  postalcode: '15232',
  city: 'Frankfurt (Oder)'
}

console.log( sql.update(entity) )

update() returns a plain object containing the SQL-statement as is with prepared statements and the parameter-array containing all parameters. It takes note of primary keys and use them as WHERE-statement for a specific record update:

{
  stmt: 'UPDATE contact SET first_name=?, last_name=?, street=?, postalcode=?, city=? WHERE id=?',
  params: ['Alfons', 'Zitterbacke', 'Friedenseck 8', '15232', 'Frankfurt (Oder)', 10]
}

Contribution:

Contributors are welcome! Feel free to submit pull requests or open discussions.

In case of submitting pull requests (highly appreciated) please keep in mind that we are [test driven] (https://github.com/itinance/sql-schema-lite/tree/master/__tests__).

Author

Hagen Hübel, Munich/Starnberg, Germany

Fullstacker, currently massive ReactNative developer & consultant

LinkedIn

sql-schema-lite's People

Contributors

itinance avatar

Watchers

 avatar  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.