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

react-native-database-async

v1.0.1

Published

Database service based on AsyncStorage

Downloads

1

Readme

react-native-database-async

Database service based on AsyncStorage

Installation

npm i react-native-database-async --save

First of all, you must define your database schema.

To do this, just create a .json file like the example below and define your tables and columns. It is not yet possible to define a type for each column, so at the moment they accept any value.

DbSchema.json

[
  {
    "name": "users",
    "columns": ["iduser", "name", "lastname", "age"]
  },
  {
    "name": "products",
    "columns": ["idproduct", "description", "price", "quantity"]
  }
]

"name" = Name of the table.

"columns" = Table columns.

Create database

Always create your database in your project's initial file, so that it is the first thing to be executed. If the database already exists, he just ignored it.

Using the above schema, we will create our database in the example below.

import DbSchema from "./DbSchema.json";
import AsyncDatabase from "react-native-database-async";

async componentDidMount() {
  /** Creates a database called "db_default" using the schema contained in the DbSchema.json file */
  const createDb = await AsyncDatabase.createDatabase("db_default", DbSchema);

  console.log(createDb); // Should return true.
}

Delete database

Permanently deletes the database and all its data.

import AsyncDatabase from "react-native-database-async";

async componentDidMount() {
  const deleteDb = await AsyncDatabase.deleteDatabase("db_default");
}

Add new tables to the database.

You can add new tables to the database without affecting those that already exist, just create a new .json file with the schema of the new tables and proceed as the example below.

import UpdatedSchema from "./UpdatedSchema.json";
import AsyncDatabase from "react-native-database-async";

async componentDidMount() {
  /** Adds the new schema tables to the "db_default" database, if the table already exists does nothing. */
  const addNewTablesDb = await AsyncDatabase.addDatabaseTable("db_default", UpdatedSchema);

  console.log(addNewTablesDb); // Should return true.
}

Update a table in the database.

You can update a table in the database by adding new fields to it.

import AsyncDatabase from "react-native-database-async";

async componentDidMount() {
  /** Adds the "city" and "state" fields to the "users" table. */
  const newFields = ["city", "state"];
  const updateTableDb = await AsyncDatabase.updateDatabaseTable("db_default", "users", newFields);

  console.log(updateTableDb); // Should return true.
}

Insert statement.

Insert a new record in the database.

import AsyncDatabase from "react-native-database-async";

async componentDidMount() {
  const insertValues = {
    iduser: 1,
    name: "John",
    lastname: "Doe",
    age: 25
  };
  
  /** Insert a value in "users" table, if the column does not exist in the table schema it will return an exception. */
  const insert = await AsyncDatabase.insert("users", insertValues);

  console.log(updateTableDb); // Should return true.
}

Where statement.

It is not yet possible to use multiple wheres, currently supporting only one per instruction.

Where types:

  • eq: Equal
  • bt: Bigger than
  • be: Bigger equal
  • lt: Less than
  • le: Less equal

Where equal type example

const where = {
  type: 'eq',
  field: 'iduser', // Field to compare.
  toBe: 1 // Value to be.,
  fetch: true // Available only in select statement, returns only the first value.
}

OrderBy statement.

OrderBy types:

  • desc: DESC
  • asc: ASC

OrderBy desc type example

const orderBy = {
  type: 'desc',
  field: 'age', // Field to order.
}

Select statement.

Returns a value or values from the database.

import AsyncDatabase from "react-native-database-async";

async componentDidMount() {
  const where = {
    type: "eq",
    field: "iduser",
    toBe: 1,
    fetch: true
  };
  
  const orderBy = {
    type: "desc",
    field: "age"
  };
  
  /** Select all, without using where or orderBy. */
  const selectAll = await AsyncDatabase.select("users"); // Return array of values.

  const selectWithWhere = await AsyncDatabase.select("users", where); // Return only value that match with iduser == 1.
  
  /** Without where. **/
  const selectWithOrderBy =  await AsyncDatabase.select("users", null, orderBy); // Return all values, ordened by age DESC.
}

Update statement.

Updates the values in the database.

import AsyncDatabase from "react-native-database-async";

async componentDidMount() {
  const where = {
    type: "eq",
    field: "iduser",
    toBe: 1,
  };
  
  const updateValues = {
    age: 30,
    name: "Torvald"
  };
  
  /** Updates the values in "iduser" == 1 */
  const update = await AsyncDatabase.update("users", updateValues, where); // Returns true.
}

Delete statement.

Deletes a value from the database.

import AsyncDatabase from "react-native-database-async";

async componentDidMount() {
  const where = {
    type: "eq",
    field: "iduser",
    toBe: 1,
  };
  
  /** Delete the values in "iduser" == 1 */
  const delete = await AsyncDatabase.delete("users", where); // Returns true.
}

Docs

  • createDatabase(databaseName, schema)
  • deleteDatabase(databaseName)
  • addDatabaseTable(databaseName, schema)
  • updateDatabaseTable(databaseName, tableName, newColumns)
  • insert(tableName, values)
  • select(tableName, whereStatement = null, orderByStatement = null)
  • update(tableName, values, whereStatement) // Pass type = null in where to update all records.
  • delete(tableName, where)