当前位置: 首页 > 软件库 > 数据库相关 > >

nim-allographer

授权协议 MIT License
开发语言 JavaScript
所属分类 数据库相关
软件类型 开源软件
地区 不详
投 递 者 欧镜
操作系统 跨平台
开源组织
适用人群 未知
 软件概览

allographer

nimble
Build Status

An asynchronous query builder library inspired by Laravel/PHP and Orator/Python for Nim

Easy to access Rdb

Query Builder

import asyncdispatch
import allographer/connection
import allographer/query_builder

let maxConnections = 95
let timeout = 30
let rdb = dbOpen(PostgreSql, "database", "user", "password" "localhost", 5432, maxConnections, timeout)
# also available
# let rdb = dbOpen(Sqlite3, "/path/to/db/sqlite3.db", maxConnections=maxConnections, timeout=timeout)
# let rdb = dbOpen(MySQL, "database", "user", "password" "localhost", 3306, maxConnections, timeout)
# let rdb = dbOpen(MariaDB, "database", "user", "password" "localhost", 3306, maxConnections, timeout)

proc main(){.async.} =
  let result = await rdb
                    .table("users")
                    .select("id", "email", "name")
                    .limit(5)
                    .offset(10)
                    .get()
  echo result

waitFor main()

>> SELECT id, email, name FROM users LIMIT 5 OFFSET 10
>> @[
  {"id":11,"email":"user11@gmail.com","name":"user11"},
  {"id":12,"email":"user12@gmail.com","name":"user12"},
  {"id":13,"email":"user13@gmail.com","name":"user13"},
  {"id":14,"email":"user14@gmail.com","name":"user14"},
  {"id":15,"email":"user15@gmail.com","name":"user15"}
]

Schema Builder

import allographer/schema_builder


rdb.schema([
  table("auth", [
    Column().increments("id"),
    Column().string("name").nullable(),
    Column().timestamp("created_at").default()
  ]),
  table("users", [
    Column().increments("id"),
    Column().string("name"),
    Column().foreign("auth_id").reference("id").on("auth").onDelete(SET_NULL)
  ])
])

>> CREATE TABLE auth (
    id INT NOT NULL PRIMARY KEY,
    name VARCHAR,
    created_at DATETIME DEFAULT (NOW())
)
>> CREATE TABLE users (
    id INT NOT NULL PRIMARY KEY,
    name VARCHAR NOT NULL,
    auth_id INT,
    FOREIGN KEY(auth_id) REFERENCES auth(id) ON DELETE SET NULL
)

rdb.alter(
  table("users", [
    add().string("email").unique().default(""),
    delete("name")
  ])
)

>> ALTER TABLE "users" ADD COLUMN `email` UNIQUE DEFAULT "" CHECK (length(`email`) <= 255)
>> ALTER TABLE "users" DROP `name`

Index


Install

nimble install allographer

Set up

First of all, add nim binary path

export PATH=$PATH:~/.nimble/bin

After install allographer, "dbtool" command is going to be available.

Createing connection

Database connection should be definded as singleton variable.

database.nim

import allographer/connection

let rdb* = dbOpen(PostgreSql, "database", "user", "password" "localhost", 5432, maxConnections, timeout)

# you can create connection for multiple database at same time.
let sqliteRdb* = dbOpen(Sqlite3, "/path/to/db/sqlite3.db", maxConnections=maxConnections, timeout=timeout)
let mysqlRdb* = dbOpen(MySQL, "database", "user", "password" "localhost", 3306, maxConnections, timeout)
let mariaRdb* = dbOpen(MariaDB, "database", "user", "password" "localhost", 3306, maxConnections, timeout)

Then, call connection when you run query.

run_sql.nim

import asyncdispatch
import allographer/query_builder
from database import rdb

proc main(){.async.}=
  echo await rdb.table("users").get()

waitFor main()

Logging

Please set args in dbOpen()

proc dbOpen*(driver:Driver, database:string="", user:string="", password:string="",
            host: string="", port=0, maxConnections=1, timeout=30,
            shouldDisplayLog=false, shouldOutputLogFile=false, logDir=""):Rdb
  • shouldDisplayLog: Whether display logging in terminal console or not.
  • shouldOutputLogFile: Whether output logging in log file or not.
  • logDir: Define logging dir path.

Documents

Query Builder
Schema Builder

Nim API Documents

Query Builder

base
grammars
exec
connection

Schema Builder

schema
table
column
alter

Development

Branch naming rule

Please create this branch name when you will create a new pull request.

Branch Description
feature/*** New feature branch
hotfix/*** Bug fix branch
chore/*** Chore work or maintenance

This naming rule automatically labels the pull request.

 相关资料
  • Nim

    Nimrod(已改名为 Nim)是一种静态类型的编译系统编程语言。它结合了 Python、Ada和Modula等成熟语言的成功理念。 高效的 Nim生成不依赖于虚拟机的本机无依赖可执行文件,这些可执行文件很小并且允许重新分配。 Nim编译器和生成的可执行文件支持所有主要平台,例如Windows,Linux,BSD和macOS。 在C ++和Rust的启发下,Nim的内存管理是确定性的,并且可以使用

  • nim-asciitables simple terminal ascii tables for nim DEPRECATION project is deprecated in favor for nim-terminaltables How to use asciitables has a very simple api setHeaders to set column names addRo

  • nim-terminaltables terminal tables for nim API API docs available hereterminaltables has a very small API newUnicodeTable uses unicodeStyle newAsciiTable uses asciiStyle Table style is configurable us

  • Dashing is a library to quickly create terminal-based dashboards in Nim. Dashing provides high-level components: vertical and horizontal charts, gauges, log panes, text windows and screen splitting. I

  • Gatabase Works with ARC, ORC, --panics:on, --experimental:strictFuncs. Use Gatabase is designed as 1 simplified Strong Static Typed Connection-Pooling Compile-Time SQL DSL Sugar. Nim mimics SQL. ~1000

  • libssh2.nim Nim wrapper for libssh2 For document please refer to libssh2 website Requirements In order to use this wrapper, libssh2 must installed on your system: Mac OSX:$ port install libssh2 Ubuntu

相关阅读

相关文章

相关问答

相关文档