mewwme-sqlite
TypeScript icon, indicating that this package has built-in type declarations

2.0.1 • Public • Published

mewwme-sqlite

SQlite3 database but simplified with easy methods and TypeScript support!

Features

  • Supports string arrays only (other types aren't supported yet).
  • Supports boolean values.
  • Simple and easy to use.
  • Poweful typings.

Installation

npm install mewwme-sqlite

Usage

Create a new database using the class SQLiteDatabase:

import { SQLiteDatabase } from 'mewwme-sqlite';

const db = new SQLiteDatabase('path/to/your/file.db');

Here are the available methods to use with SQLiteDatabase class.

Note Every key is required (NOT NULL), you can make one or some of them optional by adding { nullable: true }.

// Creates a new table:
await db.create({
    name: 'users',
    overwrite: true, // IF EXISTS
    keys: {
        id: ['INTEGER', {
            primary: true, // PRIMARY KEY
            autoincrement: true // AUTOINCREMENT
        }],
        username: ['TEXT'],
        age: ['INTEGER'],
        alive: ['BOOLEAN', {
                nullable: true
        }],
        languages: ['ARRAY']
    }
});

// Insert a new row in a table:
await db.insert('users', {
    username: 'John',
    age: 35,
    alive: true,
    languages: ['Python', 'Typescript', 'C++']
});

// Select from a table:
await db.select('users'); // → [{ ... }]

await db.select('users', { username: 'John' }); // → [{ ... }]
await db.select('users', { age: 35 }); // → [{ ... }]
await db.select('users', { username: 'John', age: 40 }); // → []

// Ensure if a table exist or not, or by using filter:
await db.ensure('users'); // → true

await db.ensure('users', { username: 'John' }); // → true
await db.ensure('users', { age: 35 }); // → true
await db.ensure('users', { username: 'John', age: 40 });// → false

// Deletes a row from a table:
await db.delete('users', { username: 'John' });

// Deletes a table:
await db.drop('users');

// Close the database:
await db.close();

Typings

The class has a type parameter with type of array, you can include many schemas as you want, just make sure the types are correct and equal to created tables.

type UsersSchema = {
    name: 'users',
    keys: {
        username: string,
        age: number,
        alive?: boolean, // ← Nullable
        languages: string[]
    }
};

new SQLiteDatabase<[UsersSchema, ...]>(...);

Package Sidebar

Install

npm i mewwme-sqlite

Weekly Downloads

4

Version

2.0.1

License

GPL-3.0

Unpacked Size

58 kB

Total Files

16

Last publish

Collaborators

  • lrmn7