npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@dokuhero/expo-orm

v0.3.1

Published

SQLite ORM for Typescript works in Expo

Downloads

8

Readme

Expo ORM

SQLite ORM for Typescript works in Expo

Installation

Using npm:

npm i -S @dokuhero/expo-orm

or yarn:

yarn add @dokuhero/expo-orm

Usage

For the best practice, is by using a repository pattern. That you will have two directories called models and repository. A models directory will contains all models that represents all tables you have in your SQLite database and repository contains the repository class you'll use to interact with the models.

Here's the example of the folder structure:

+-- models
|   +-- index.ts
|   +-- Settings.ts
|   +-- User.ts
+-- repository
|   +-- index.ts

Create Model Classes

// models/Settings.ts

import { Column, Primary } from '@dokuhero/expo-orm'

export class Settings {
  @Primary()
  id: number = 0

  @Column('NVARCHAR')
  theme: string = ''
}
// models/User.ts

import { Column, Primary } from '@dokuhero/expo-orm'

export class User {
  @Primary()
  id: number = 0

  @Column('NVARCHAR')
  name: string = ''
}

Now export all your models in your models/index.ts. So everytime you add new model, you need to export it again trough models/index.ts. This is for convenience use when accessing models later on in repository.

// models/index.ts

export { User } from './User'
export { Settings } from './Settings'

Create Repository Class

// repository/index.ts

import * as models from '../models'
import { Db } from '@dokuhero/expo-orm'

// Now you have all your models types
type Models = typeof models

export class Repo {
  // Define database instance
  static db: Db<Models>

  // This action will create tables based on models
  // in your SQLite database if it's not exists yet.
  // Call this only once on your start-up project
  static async init() {
    this.db = await Db.init({
      database: 'name-of-database',
      entities: models
    })
  }

  // Now you're ready to interact with all models you have.
  // For example:

  static async getSettings(): Promise<models.Setting> {
    return await this.db.tables.Settings.selectOne()
  }

  static async updateSettings(value: Partial<models.Setting>) {
    const settings = (await this.getSettings()) || { id: 1 }
    await this.db.tables.Settings.upsert({ ...settings, ...value })
  }

  // And so on...
}

License

MIT