MappedSQLite3
The library that allows you to work with SQLite3 like with Maps. (key/value).
Installing:
npm i mapped-sqlite3 --save
Usage
const MappedSQLite3 = ;const db = "./db.sqlite"; db;
Information
-
To start using
mapped-sqlite3
you need to create new database file (Just empy file with.db
or.sqlite
extension). -
You can use already created databases, but make sure that you're not using
mapped-sqlite3
API in already created table. -
You can save
string
,number
,object/array
,boolean
,undefined
data types.null
andNaN
is not supported andNaN
will be converted to"NaN"
(string
) andnull
will be converted toundefined
. Functions are not supported too. -
When
mapped-sqlite3
creates new table it haskey
,value
andtype
rows. -
All Database API is asynchronous and returns Promises, so you'll need to use
.then
orasync/await
. -
You can acess
sqlite3
database class withthis.db
and path to database withthis.path
.
API
All examples are in async function.
Database
<database>.createTable(name);
Creates new table in database.
await db;
<database>.dropTable(name);
Deletes (drops) table in database.
await db;
<database>.renameTable(oldName, newName);
Renames table in database.
await db;
<database>.filterTable(name, func);
Filters table. (If func
returns true
it won't be deleted).
// will delete all variables that are not string.db;
<database>.table(name);
Generates table
object.
await db;const table = await db; // { get: ..., ... }
Table
Note: // table - {...}
is just visual representation of real table.
<table>.get(key);
Gets value by key.
Note: Read Information above to get info about supported types.
// table - { a: 1 }await table; // 1
<table>.set(key, value);
Sets value by key.
// table - {}await table;// table - { a: 1 }
<table>.setSeveral(object);
Sets all key/values in object
into table.
// table - {}await table;// table - { a: 1, b: "Hello, world!" }
<table>.all();
Gets all key/values from table.
await tableall; // {...}
<table>.filter(func);
Gets all key/values that passed filter check.
// table - { a: 1, b: "Hi", c: true }await table; // { a: 1 }
<table>.find(func);
Gets first key/value that passed filter check.
// table - { a: {x:1}, b: {x:2}, c: {x:3} }await table; // { x: 2 }
<table>.sort(func);
Returns sorted table.
func
should pass 2 arguments - a
and b
. a
and b
is arrays that contains key
and value
- [0] element is key
, [1] element is value
.
Note: Learn about sorting here - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort.
// table - { a: 1, b: 3, c: 2 }await table; // { a: 1, c: 2, b: 3 }
<table>.remove(key);
Removes column by key.
// table - { test: 1 }await table;// table - {}
<table>.clear();
Deletes all columns in table.
// table - { a: 1, b: 2 }await tableclear;// table - {}
License
MIT