1.1.1 • Published 3 years ago

dbcover v1.1.1

Weekly downloads
7
License
UNLICENSED
Repository
github
Last release
3 years ago

A Simple ORM Module

!UNDER DEVELOPMENT!

[] Improve cache system. [] Rewrite inner code with promise and simplify it.

GO TO ISSUES TO SEE KNOWN BUGS.

Install: npm install dbcover

Common Usage (in coffeescript):

{Observe, Model, Validators}   = require 'dbcover'
# Require whatever validator you need.
# List of validators: integer, string, email, required

# Define database connection
Observe.define 'repo',
  name: 'default'
  provider:
    type: 'mysql'
    options:
      host: 'localhost'
      port: 3306
      password: '123123'
      user: 'root'
      database: 'test'
      
Observe.define 'repo',
  name: 'pg'
  provider:
    type: 'postgresql'
    options:
      host: 'localhost'
      port: 3306
      password: '123123'
      user: 'root'
      database: 'test'

Observe.define 'cache',
  name: 'default'
  provider:
    type: 'redis'
    options:
      host: 'localhost'
      port: 6379

# Define an model
User = new Model
  meta:
    table: 'users'
    repo: 'pg'   # default repo name is 'default'
    cache: 'default' # leave meta.cache blank or set it false to disable caching
    ttl: 10  # cache expire time(seconds), must be set if cache is enabled for this model. if ttl < 1, then no expire time would be applied.
    fields: [
      # set auto to true to support auto increment columns
      {name: 'id', type: 'integer', auto: true, primkey: true, uniq: true}
      {name: 'email',   type: 'string', validator: 'email', primkey: true, uniq: true}
      {name: 'random_key', type: 'string', primkey: true}
      {name: 'age',     type: 'integer', validator: new Validators.integer(10, 100)}
      {name: 'extra',   type: 'json'}
      {name: 'created', type: 'timestamp'}
    ]
    # primkey(s) with one or more columns. remember to use the name you definded above, not the real column name.
    indices: [
      {name: 'pk', fields: ['email', 'age']}
    ]

  sayHi: () ->
    console.log 'hi'

  rename: (name, id)->
    deferred = Q.defer()
    raw_sql = "UPDATE #{@$table} SET name = ?, updated_at='#{new Date}' WHERE id = ?"
    # You may use @$repo.query to make customized queries.
    # Use ? or :var instead of raw variables to avoid SQL injection.
    @$repo.query raw_sql, [name, id], (err, result)->
      deferred.reject err if err?
      deferred.resolve result
    return deferred.promise

# Turn on debug, default is false
User.turnOnDebug(true)

# Creating instance
user = User.new
  userId: 23
  email: 'rankjie-nospam@gmail.com'
  age: 22

# Save instance 
promise = user.save()

# Updating attributes 
user.age = 23
user.extra = { nick: 'rankjie' }
promise = user.update()

# Deleting instance 
promise = user.delete()

# Count rows
promise = user.count()

# Count rows with WHERE age > 30
promise = user.count("age > 30")


# Query 
# All promises returned by Query or findBy, will be resolved with a set of instances ( first() and findByIndeices which has `uniq:true` will return just one object )
# Or, rejected with errors.
promise = User.findById(123)
# You can append '__gt' or '__lt' to the field name, that would equals to '>' and '<'
promise = User.find(age: 30, created__gt: 2334343).first()
promise = User.find(age__lt: 30, created: 2334343).first()
# Get all the entries
promise = User.find(age: 30).all()
# Find by primkeys 
promise = User.findByPk(userID: 234, age: 99)
# prim keys like random_key will be transformed to camel case like RandomKey
promise = User.findByRandomKey('someValue')


# list all the data
promise = User.find()
  .orderBy('created', 'desc')
  .all()

# list from 0 to 10
promise = User.find('age > ? and created > ?', [30, 234242])
  .orderBy('created', 'desc')
  .list(0, 10)

# get them all
promise = User.find('age > :age and created > :created')
  .set(age: 30, created: 1234324234)
  .orderBy('created', 'desc')
  .all()

# get the first one
promise = User.find('age > :age and created > :created')
  .set(age: 30, created: 1234324234)
  .orderBy('created', 'desc')
  .first()
1.1.1

3 years ago

1.1.0

3 years ago

1.0.8

4 years ago

1.0.7

5 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago

0.7.0

6 years ago

0.6.3

6 years ago

0.6.2

6 years ago

0.6.1

6 years ago

0.6.0

9 years ago

0.5.9

9 years ago

0.5.8

9 years ago

0.5.7

9 years ago

0.5.6

10 years ago

0.5.5

10 years ago

0.5.4

10 years ago

0.5.3

10 years ago

0.5.2

10 years ago

0.5.1

10 years ago

0.5.0

10 years ago

0.4.9

10 years ago

0.4.8

10 years ago

0.4.7

10 years ago

0.4.6

10 years ago

0.4.5

10 years ago

0.4.4

10 years ago

0.4.3

10 years ago

0.4.1

10 years ago

0.4.0

10 years ago

0.3.9

10 years ago

0.3.8

10 years ago

0.3.7

10 years ago

0.3.6

10 years ago

0.3.5

10 years ago

0.3.4

10 years ago

0.3.3

10 years ago

0.3.1

10 years ago

0.3.0

10 years ago

0.2.9

10 years ago

0.2.8

10 years ago

0.2.7

10 years ago

0.2.6

10 years ago

0.2.5

10 years ago

0.2.4

10 years ago

0.2.3

10 years ago

0.2.2

10 years ago

0.2.1

10 years ago

0.2.0

10 years ago

0.1.9

10 years ago

0.1.8

11 years ago

0.1.6

11 years ago

0.1.5

11 years ago

0.1.4

11 years ago

0.1.3

11 years ago

0.1.2

11 years ago

0.1.1

11 years ago

0.1.0

11 years ago

0.0.9

11 years ago

0.0.8

11 years ago

0.0.7

11 years ago

0.0.6

11 years ago

0.0.5

11 years ago

0.0.4

11 years ago

0.0.3

11 years ago

0.0.2

11 years ago