This package has been deprecated

Author message:

Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.

mapped-sqlite3

1.0.0 • Public • Published

MappedSQLite3

The library that allows you to work with SQLite3 like with Maps. (key/value).

Installing:

npm i mapped-sqlite3 --save

Usage

const MappedSQLite3 = require("mapped-sqlite3");
const db = new MappedSQLite3("./db.sqlite");
 
db.createTable("Testing").then(async () => {
    let table = db.table("Testing");
    await table.set("Key", 1 + 1);
 
    console.log(await table.get("Key")); // 2
    console.log(await table.all()); // { Key: 2 }
 
    // Will set multiple columns.
    await table.setSeveral({
        a: 1, // number
        b: "Hello, world!", // string
        c: true, // boolean
        e: undefined, // undefined
        f: {a: 5}, // object
        g: [1, 2, 3] // object
    });
 
    console.log(await table.all()); // { ... }
    console.log(await table.filter(i => typeof (i) === "number")); // { Key: 2, a: 1 }
    console.log(await table.find(i => typeof (i) === "string" && i.startsWith("Hello"))); // "Hello, world!"
    await table.remove("c"); // c - true
    console.log(await table.get("c")); // null
    console.log((await table.get("g"))[0]); // 1
 
    await table.clear();
 
    await table.set("a", 1);
    await table.set("b", 3);
    await table.set("c", 2);
 
    console.log(await table.all()); // { a: 1, b: 3, c: 2 }
    console.log(await table.sort((a, b) => a[1] - b[1])); // { a: 1, c: 2, b: 3 }
 
    await db.renameTable("Testing", "Testing2");
    table = db.table("Testing2");
    console.log(await table.all()); // { a: 1, b: 3, c: 2 }
    await db.filterTable("Testing2", i => i <= 1);
    console.log(await table.all()); // { a: 1 }
    
    await db.dropTable("Testing2");
});

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 and NaN is not supported and NaN will be converted to "NaN" (string) and null will be converted to undefined. Functions are not supported too.

  • When mapped-sqlite3 creates new table it has key, value and type rows.

  • All Database API is asynchronous and returns Promises, so you'll need to use .then or async/await.

  • You can acess sqlite3 database class with this.db and path to database with this.path.

API

All examples are in async function.

Database

<database>.createTable(name);

Creates new table in database.

await db.createTable("test");

<database>.dropTable(name);

Deletes (drops) table in database.

await db.dropTable("test");

<database>.renameTable(oldName, newName);

Renames table in database.

await db.renameTable("test", "test2");

<database>.filterTable(name, func);

Filters table. (If func returns true it won't be deleted).

// will delete all variables that are not string.
db.filterTable("test", row => typeof(row) === "string");

<database>.table(name);

Generates table object.

await db.createTable("test");
const table = await db.table("test"); // { 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.get("a"); // 1 

<table>.set(key, value);

Sets value by key.

// table - {}
await table.set("a", 1);
// table - { a: 1 }

<table>.setSeveral(object);

Sets all key/values in object into table.

// table - {}
await table.setSeveral({
    a: 1,
    b: "Hello, world!"
});
// table - { a: 1, b: "Hello, world!" }

<table>.all();

Gets all key/values from table.

await table.all(); // {...}

<table>.filter(func);

Gets all key/values that passed filter check.

// table - { a: 1, b: "Hi", c: true }
await table.filter(i => i === 1); // { 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.find(i => i.x === 2); // { 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.sort((a, b) => a[1] - b[1]); // { a: 1, c: 2, b: 3 }

<table>.remove(key);

Removes column by key.

// table - { test: 1 }
await table.remove("test");
// table - {}

<table>.clear();

Deletes all columns in table.

// table - { a: 1, b: 2 }
await table.clear();
// table - {}

License

MIT

Package Sidebar

Install

npm i mapped-sqlite3

Weekly Downloads

1

Version

1.0.0

License

MIT

Unpacked Size

555 kB

Total Files

11

Last publish

Collaborators

  • dimden