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 🙏

© 2025 – Pkg Stats / Ryan Hefner

react-native-model

v1.0.5

Published

model to handle validations , sqlite , multi language and etc.

Downloads

36

Readme

react-native-model

A model for work with :

  • react native sqlite
  • validation
  • multi language
  • manage API URL

how to install


first install sqlite2

npm install react-native-sqlite-2 --save

react-native link react-native-sqlite-2

then install model

npm install react-native-model

how to use it


you have to build and config your BaseModel then extend your models from this model

import {Model} from "react-native-model";
export class BaseModel extends Model {

    static BASE_URL = "https://your base domain/";//for manage your api urls
    static VERSION = "v1";//most APIs use a version, this version append to base url if you don't have it use empty quot
    static LANGUAGE="en";
    static showLog=false;//by default is true if you don't want to see raw queries in the log, set it to false;
    static DB_CONFIG = {
        db_name: "my.db",
        version: "1.0",
        display_name: "SQLite Menu Database",
        size: 200000,
    };
    static TRANSLATE={
        en:{
            _required_: "_field_ " + "is require",
            _unique_: "_field_ " + "is already taken",
            _email_: "_field_ " + "is not a valid email",
            _number_: "_field_ " + "should be a number",
            _integer_: "_field_ " + "should be an integer",
            _max_:  "maximum of"+" _field_ "+"should be"+" _validValue1_ ",
            _min_:  "minimum of"+" _field_ "+"should be"+" _validValue1_ ",
            _between_:  " value of"+" _field_ "+"should be between"+" _validValue1_ "+"and"+" _validValue2_ ",
            _string_:  " _field_ "+"should be a string",
            _maxLength_:  "max length of"+" _field_ "+"is "+" _validValue1_ ",
            _minLength_:  "min length of"+" _field_ "+"is "+" _validValue1_ ",
            //your translate
        },
        //another language like farsi :
        fa: {
            _required_: "_field_ " + "نمی تواند خالی باشد",
            _unique_: "_field_ " + "با مقدار "+"_validValue1_ "+"قبلا ثبت شده است",
            _email_: "_field_ " + "یک ایمیل معتبر نیست",
            _number_: "_field_ " + "باید یک عدد باشد",
            _integer_: "_field_ " + "باید یک عدد صحیح باشد",
            _max_:  "حداکثر مقدار"+" _field_ "+" باید"+" _validValue1_ "+"باشد",
            _min_:  "حداقل مقدار"+" _field_ "+" باید"+" _validValue1_ "+"باشد",
            _between_:  " مقدار"+" _field_ "+" باید بین"+" _validValue1_ "+"و"+" _validValue2_ "+"باشد",
            _string_:  " _field_ "+"باید یک رشته باشد",
            _maxLength_:  "حداکثر طول"+" _field_ "+" باید"+" _validValue1_ "+"کاراکتر باشد",
            _minLength_:  "حداقل طول"+" _field_ "+" باید"+" _validValue1_ "+"کاراکتر باشد",
            _compare_:  "_field_"+" و "+"_validValue1_"+" باید برابر باشند",
            //your translate
        },
    };
}

ok after config your base model, you can build your models here is an example of user model :

import {BaseModel} from "your above baseMode";
export class UserModel extends BaseModel {

    constructor() {
        super(true,true);//first parameter: if your model has db set it true, second parameter: if your API has version
        this.createTable();//build your table if Not Exists
    }

    static getIndexUrl() {
       return this.__getUrl(["users"],{username:"ali",name:"sth"});//BASE_URL/VERSION/users?username=ali&name=sth
    }
    
    static tableName() {//return your table name
        return "user";
    }

    static table() {//your table definition
        let table;
        table = {
            id: "INTEGER PRIMARY KEY NOT NULL",
            username: "VARCHAR",
            password: "VARCHAR",
        };
        return table;
    }

    rules() {//your validation rules
        return [
            {field: ['username'], rule: 'unique'},
            {field: ['username', 'password'], rule: 'required'},
            {field:['password'], rule:'string',maxLength:50,minLength:6},
        ];
    }
}

Insert Query :

async function saveModel() {
        let model = new UserModel();
        model.username = "ziaadini";
        model.password = "my pass";

        if (await model.validate()) {
            let isSaved = await model.save(false);
            console.log("is Saved", isSaved);
        } else {
            console.log('validation error is ', model.getErrors());
        }
    }
//shorter syntax
async function saveModel2() {
        let model = new UserModel();
        model.username = "ziaadini";
        model.password = "my pass";

        if (await model.save()) {
            console.log("is Saved", true);
        } else {
            console.log('validation error is ', model.getErrors());
        }
    }

validate function => check validations defined in rules() method inside UserModel

save method => insert data to user table, what is false parameter? save method by default call validate function and if model is not valid return false as a promise else return result as a promise. if set false save method don't check validation.

save method also is for update i will explain it in next lecture.

Select Query :

  async function findUsers() {
        let users = await UserModel.find(new UserModel).limit(5).all();
        console.log(users);
    }
    async function findUserByID(id) {
        let user = await UserModel.find(new UserModel).where({id: id}).one();
        console.log(user);
    }

some point is here :

  • if you pass new UserModel to the find method the result is instance of UserModel class else is an object
  • one method return only one result as an object(or model instance).
  • if your query has not any result one method resove null
  • all method return all result as array of objects(or model instance)
  • in your query has not any result all method resolve empty array

Update Query :

  async function updateUser(id, username) {
        let user = await UserModel.find(new UserModel).where({id: id, username: username}).one();
        if (user) {
            user.username = "new username";
            user.password = "new password";
            let isSaved = await user.save();
            if (isSaved) {
                console.info('is saved : ', isSaved);
            } else {
                console.info('errors : ', user.getErrors());
            }
        } else {
            console.log("user dos not exist");
        }
    }

a question is we are use save method for both insert and update query but how? the point is if isNewRecord==false save method execute update query don't forget your model should has a primary key(here is id). above example could change some thing like this (but not recommended) :

 let user=new UserModel();
    //config to update
    user.id=1;
    user.isNewRecord=false;

    user.username = "new username";
    user.password = "new password";
    let isSaved = await user.save();

translate validation errors and use multi language : just overwrite attributeLabels method inside your model, your model have to some thing like this :

import {BaseModel} from "models/BaseModel";

export class UserModel extends BaseModel {

    constructor() {
        super(true,true);
        this.createTable();
    }

    static getIndexUrl() {
        return this.__getUrl(["users"],{username:"ali",name:"some value"});
    }

    static tableName() {
        return "user";
    }

    static table() {
        let table;
        table = {
            id: "INTEGER PRIMARY KEY NOT NULL",
            username: "VARCHAR",
            password: "VARCHAR",
        };
        return table;
    }

    rules() {
        return [
            {field: ['username'], rule: 'unique'},
            {field: ['username', 'password'], rule: 'required'},
            {field:['password'], rule:'string',maxLength:50,minLength:6},
        ];
    }
    attributeLabels() {
        return {
            username : this.constructor.translate(this.constructor.LANGUAGE,"username"),
            password :  this.constructor.translate(this.constructor.LANGUAGE,"password"),
            //OR if you don't want use multi language
            // username:'Username',
            // password:'Password',
        }
    }
}

note : don't forget to put username and password fields in TRANSLATE object inside your base model class

if it's not important for you to have multi language or same translate for your attributes, you can simply use commented above codes, but not recommended.

you can use this model to work with database and i think is awesome. this document need some additional work i will complete it later.