Giter VIP home page Giter VIP logo

expo-sqlite-orm's Introduction

Expo SQLite ORM

Build Status Downloads Version License

It is a simple ORM utility to use with expo sqlite

Install

yarn add expo-sqlite-orm

Creating a model

You need to provide 3 things:

  • database: Instance of expo SQLite or promise with that instance
  • tableName: The name of the table
  • columnMapping: The columns for the model and their types
    • Supported options: type, primary_key, not_null, unique, default
import { SQLite } from 'expo'
import { BaseModel, types } from 'expo-sqlite-orm'

export default class Animal extends BaseModel {
  constructor(obj) {
    super(obj)
  }

  static get database() {
    return Promise.resolve(SQLite.openDatabase('database.db'))
  }

  static get tableName() {
    return 'animals'
  }

  static get columnMapping() {
    return {
      id: { type: types.INTEGER, primary_key: true }, // For while only supports id as primary key
      name: { type: types.TEXT, not_null: true },
      color: { type: types.TEXT },
      age: { type: types.NUMERIC },
      another_uid: { type: types.INTEGER, unique: true },
      timestamp: { type: types.INTEGER, default: () => Date.now() }
    }
  }
}

Database operations

Drop table

Animal.dropTable()

Create table

Animal.createTable()

Create a record

const props = {
  name: 'Bob',
  color: 'Brown',
  age: 2
}

const animal = new Animal(props)
animal.save()

or

const props = {
  name: 'Bob',
  color: 'Brown',
  age: 2
}

Animal.create(props)

Find a record

const id = 1
Animal.find(id)

or

Animal.findBy({ age_eq: 12345, color_cont: '%Brown%' })

Update a record

const id = 1
const animal = await Animal.find(id)
animal.age = 3
animal.save()

or

const props = {
  id: 1 // required
  age: 3
}

Animal.update(props)

Destroy a record

const id = 1
Animal.destroy(id)

or

const id = 1
const animal = await Animal.find(id)
animal.destroy()

Destroy all records

Animal.destroyAll()

Query

const options = {
  columns: 'id, name',
  where: {
    age_gt: 2
  },
  page: 2,
  limit: 30,
  order: 'name ASC'
}

Animal.query(options)

Where operations

  • eq: =,
  • neq: <>,
  • lt: <,
  • lteq: <=,
  • gt: >,
  • gteq: >=,
  • cont: LIKE

Data types

  • INTEGER
  • FLOAT
  • TEXT
  • NUMERIC
  • DATE
  • DATETIME
  • BOOLEAN
  • JSON

Author

License

This project is licensed under MIT License

expo-sqlite-orm's People

Contributors

dflourusso avatar

Watchers

James Cloos 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.