90crew-sqlite-async 中文文档教程

发布于 3年前 浏览 28 项目主页 更新于 3年前

fork version

更改 sqlite 5.0.2 -> 5.0.0

sqlite-async

ES6 基于 Promise 的 sqlite3 模块接口。

sqlite-async 模块导出 Database 类。 每个方法的工作原理与原始 sqlite3 API 相同。 每个方法都返回一个承诺,而不是回调。 prepare 方法返回一个 Statement 实例,该实例还将所有原始语句方法包装到返回承诺的方法中。

Usage

const Database = require('sqlite-async')

Database.open('test.db')
    .then(db => {
        ...
    })
    .catch(err => {
        ...
    })

test/sqlite-async-test.js 文件提供了示例。

API

Database.SQLITE3_VERSION

返回此模块当前使用的 node-sqlite3 包版本号的静态 getter。

Database.open(filename, mode)

实例化新的 Database 对象并调用 Database#open(filename, mode) 的静态方法。 返回一个由数据库实例解析的承诺。

Database#open(filename, mode)

使用与 sqlite3 Database 构造函数相同的参数打开数据库。 返回一个由数据库实例解析的承诺。

Database#close([fn])

关闭数据库并返回一个承诺。 如果指定了可选的提供程序函数 (fn),则在关闭数据库之前调用它作为 fn(db)此函数必须返回一个承诺。无论可选提供者函数返回的承诺是否已解决或被拒绝,数据库都会关闭。 如果返回的 promise 被拒绝,则 close 方法本身将返回一个被拒绝的 promise,其中包含 provider 函数返回的 promise 的错误。

Database#run(sql, [param, …])

相当于sqlite3的Database#run方法。 返回使用来自 sqlite3 回调函数的 this 参数解析的承诺。

Database#get(sql, [param, …])

相当于sqlite3的Database#get方法。 返回随行解决的承诺。

Database#all(sql, [param, …])

相当于sqlite3的Database#all方法。 返回用行解决的承诺。

Database#each(sql, [param, …], callback)

相当于sqlite3的Database#each方法。 需要每行回调函数。 返回一个由数据库实例解析的承诺。

Database#exec(sql)

相当于sqlite3的Database#exec方法。 返回一个由数据库实例解析的承诺。

Database#transaction(fn)

transaction 方法允许返回承诺的函数被包装在事务中。 该函数通过 Database 实例作为其参数。 返回使用函数的承诺值解决的承诺。

db.transaction((db) => {
    return Promise.all([
        db.run('INSERT INTO test VALUES (2, "two")'),
        db.run('INSERT INTO test VALUES (2, "three")')
    ])
})

Database#prepare(sql, [param, …])

相当于sqlite3的Database#prepare方法。 返回一个由 Statement 实例解决的承诺。

Statement#bind([param, …])

相当于sqlite3的Statement#bind方法。 返回一个由 Statement 实例解决的承诺。

Statement#reset()

相当于sqlite3的Statement#reset方法。 返回一个由 Statement 实例解决的承诺。

Statement#finalize()

相当于sqlite3的Statement#finalize方法。 返回一个没有价值的承诺,因为该语句不能再使用。

Statement#run([param, …])

相当于sqlite3的Statement#run方法。 返回使用来自 sqlite3 回调函数的 this 参数解析的承诺。

Statement#get([param, …])

相当于sqlite3的Statement#get方法。 返回随行解决的承诺。

Statement#all([param, …])

相当于sqlite3的Statement#all方法。 返回用行解决的承诺。

Statement#each([param, …], callback)

相当于sqlite3的Statement#each方法。 需要每行回调函数。 返回一个由 Statement 实例解决的承诺。

License

MIT 许可证

版权所有 (c) 2021 Frank Hellwig

特此免费向任何获得副本的人授予许可 本软件和相关文档文件(“软件”),处理 在软件中不受限制,包括但不限于权利 使用、复制、修改、合并、发布、分发、再许可和/或出售 该软件的副本,并允许该软件是 提供这样做,但须满足以下条件:

上述版权声明和本许可声明应包含在所有 软件的副本或重要部分。

本软件“按原样”提供,不提供任何形式的明示或保证 暗示的,包括但不限于适销性保证, 适用于特定目的和非侵权。 在任何情况下都不得 作者或版权持有人对任何索赔、损害或其他 责任,无论是在合同、侵权或其他方面的行为中,由以下原因引起, 出于或与软件或使用或其他交易有关 软件。

fork version

change sqlite 5.0.2 -> 5.0.0

sqlite-async

ES6 Promise-based interface to the sqlite3 module.

The sqlite-async module exports the Database class. Each method works the same as the original sqlite3 API. Instead of callbacks, each method returns a promise. The prepare method returns a Statement instance that also wraps all of the original statement methods into methods that return a promise.

Usage

const Database = require('sqlite-async')

Database.open('test.db')
    .then(db => {
        ...
    })
    .catch(err => {
        ...
    })

The test/sqlite-async-test.js file provides examples.

API

Database.SQLITE3_VERSION

Static getter that returns the version number of the node-sqlite3 package currently being used by this module.

Database.open(filename, mode)

Static method that instantiates a new Database object and calls Database#open(filename, mode). Returns a promise that is resolved with the Database instance.

Database#open(filename, mode)

Opens the database with the same arguments as the sqlite3 Database constructor. Returns a promise that is resolved with the Database instance.

Database#close([fn])

Closes the database and returns a promise. If the optional provider function (fn) is specified, it is called as fn(db) before the database is closed. This function must return a promise. The database is closed regardless of whether the promise returned by the optional provider function is resolved or rejected. If the returned promise is rejected, then the close method itself will return a rejected promise with the error from the promise returned by the provider function.

Database#run(sql, [param, …])

Equivalent to the sqlite3 Database#run method. Returns a promise that is resolved with the this parameter from the sqlite3 callback function.

Database#get(sql, [param, …])

Equivalent to the sqlite3 Database#get method. Returns a promise that is resolved with the row.

Database#all(sql, [param, …])

Equivalent to the sqlite3 Database#all method. Returns a promise that is resolved with the rows.

Database#each(sql, [param, …], callback)

Equivalent to the sqlite3 Database#each method. The per-row callback function is requied. Returns a promise that is resolved with the Database instance.

Database#exec(sql)

Equivalent to the sqlite3 Database#exec method. Returns a promise that is resolved with the Database instance.

Database#transaction(fn)

The transaction method allows a function returning a promise to be wrapped in a transaction. The function is passed the Database instance as its parameter. Returns a promise that is resolved with the function's promise value.

db.transaction((db) => {
    return Promise.all([
        db.run('INSERT INTO test VALUES (2, "two")'),
        db.run('INSERT INTO test VALUES (2, "three")')
    ])
})

Database#prepare(sql, [param, …])

Equivalent to the sqlite3 Database#prepare method. Returns a promise that is resolved with the Statement instance.

Statement#bind([param, …])

Equivalent to the sqlite3 Statement#bind method. Returns a promise that is resolved with the Statement instance.

Statement#reset()

Equivalent to the sqlite3 Statement#reset method. Returns a promise that is resolved with the Statement instance.

Statement#finalize()

Equivalent to the sqlite3 Statement#finalize method. Returns a promise that is resolved with no value because the statement can no longer be used.

Statement#run([param, …])

Equivalent to the sqlite3 Statement#run method. Returns a promise that is resolved with the this parameter from the sqlite3 callback function.

Statement#get([param, …])

Equivalent to the sqlite3 Statement#get method. Returns a promise that is resolved with the row.

Statement#all([param, …])

Equivalent to the sqlite3 Statement#all method. Returns a promise that is resolved with the rows.

Statement#each([param, …], callback)

Equivalent to the sqlite3 Statement#each method. The per-row callback function is requied. Returns a promise that is resolved with the Statement instance.

License

MIT License

Copyright (c) 2021 Frank Hellwig

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文