0.9.5 • Published 9 years ago

bignosql v0.9.5

Weekly downloads
3
License
MIT
Repository
github
Last release
9 years ago

bignosql

NPM Version NPM Downloads

Smart NoSQL wrapper for SQL.

This isn't a ORM, it just simplify the SQL usage.

Installation

$ npm install --save bignosql

# And one of the following:
$ npm install --save pg # for postgresql
$ npm install --save mysql # for mysql and like mysql

Usage

var bignosql = require('bignosql');

var client = bignosql.connect("pgsql", {
        host: "127.0.0.1",
        port: 5432,
        database: "bignosql",
        user: "vietor",
        password: ""
    }, {
        debug: true
    });

var model = client.model("test", {
        id: bignosql.Number,
        key: bignosql.String,
        value: {
           type: bignosql.Number,
           default: 0
        }
    });

model.insert({key: 'demo', value: 0}, {return: "id"}, function(err, result) {
});
model.find({id: 1}, {id: 1, key: 1, value: 1}, function(err, rows) {
});
model.find({}).select({id: 1, key: 1, value: 1}).sort({id: -1}).skip(0).limit(1).exec(function(err, rows) {
});
model.update({id: 1}, {$inc: {value: 1}}, function(err, count) {
});
model.count({value: {$gt: 0}}, function(err, count) {
});
model.remove({id: 1}, function(err, count) {
});

API

bignosql

Any

The variables definition for Schema Undefined type.
Don't direct usage the variable, it is local usage.

Number

The variables definiton for Schema Number type.

String

The variables definiton for Schema String type.

connect(type, parameters, options)

Connect the sql database, return the Client object

NameTypeDescription
typestringtarget sql type
parametersObjectthe sql connect params
optionsObject(options)the options for bignosql
type & parameters

type: pgsql

use pg.pools, parameters details

type: mysql

use mysql.createPool parameters details

Object(Options)
NameTypeDefaultDescription
debugboolenfalseswitch in debug message

Client

model(name, schema)

Create a model for SQL Table, return Model object.

NameTypeDescription
namestringthe table name for sql
schemaObject(Schema)the schema definition
Object(Schema)

The Schema is a simple object, Key is the column name, Value is a type definition.
The type definition may be a variable or Object(ShemaType).

Object(SchemaType)
NameTypeDefaultDescription
typevariablesAnycolumn data type
defaultobjectUndefineddefault value for column

Model

insert(doc, options, callback)

Insert one record to table.

NameTypeDescription
docObjectthe columns and it values
optionsObject(InsertOptions)the options for insert
callbackfunctionthe callback for result
Object(InsertOptions)
NameTypeDefaultDescription
idstringUndefinedfor result the auto incriment name

find(query, fields, callback)

Finds some records from table.

NameTypeDescription
queryObjectthe columns and it conditions
fieldsObjectrequired column names
callbackfunctioncallback for result

when callback was null, it return Query object

Object(Condition)
KeyTypeDescription
$orArrayOR condition array
$inArrayvlaue in a array
$ninArrayvalue not in a array
$eqObjectvalue = a object
$neObjectvalue != a object
$ltnumbervalue < a number
$ltenumbervalue <= a number
$gtnumbervalue > a number
$gtenumbervalue >= a number
$regexstringvalue match a regexp string
{
    "$or": [
        {
            "key": {
                "$in": ["demo", "demo2"]
            }
        }, {
            "value": {
                "gte": 1,
                "lt": 2
            }
        }, {
            "key": {
                "$regex": "^demo.*"
            }
        }
    ]
}
Query
select(fields)

Set required column names. It's a K/V Object, key is column name, V 1 or true was required.

sort(fields)

Set column sort order. It's a K/V Object, key is column name, V 1 was ASC, -1 was DESC.

skip(n)

Set skip row count in table

limit(n)

Set limit row count in result

exec(callback)

callback for result

count(query, callback)

Get count from table.

NameTypeDescription
queryObjectthe columns and it conditions
callbackfunctioncallback for result

update(query, update, options, callback)

Update records from table.

NameTypeDescription
queryObjectthe columns and it conditions
updateObject(UpdateScript)update script
callbackfunctioncallback for result
Object(UpdateScript)

It was a simple record object like doc in insert, or a complex object.

KeyTypeDescription
$setdocthe record object
$incObjectthe K/V for number addition
{
    "$set": {
        "key": "demo"
    },
    "$inc": {
        "value": -9
    }
}
Object(UpdateOptions)

only supoort for pgsql

NameTypeDefaultDescription
returnstringUndefinedfor result a column

remove(query, callback)

Remove records from table.

NameTypeDescription
queryObjectthe columns and it conditions
callbackfunctioncallback for result

License

MIT

0.9.5

9 years ago

0.9.4

9 years ago

0.9.3

9 years ago

0.9.2

9 years ago

0.9.1

9 years ago

0.9.0

9 years ago