nodejs-sqldb

Local sql database module

Usage no npm install needed!

<script type="module">
  import nodejsSqldb from 'https://cdn.skypack.dev/nodejs-sqldb';
</script>

README

nodejs-sqldb

A simple local SQL database for NodeJS.

Easiest way to use the database, the table.add(object) and table.addAll(array) methods will check if the column exists before inserting the values:

const DB = require("nodejs-sqldb")

const tests = new DB();

tests.addAll([
  {"cpu":"9900k", "gpu": "2080ti", "score": 14443},
  {"cpu":"2700x", "gpu": "2080super", "score": 9962},
  {"cpu":"9700k", "gpu": "1080ti", "score": 10965}
])

// or add a single row

tests.add({"cpu":"9900k", "gpu": "2080ti", "score": 14443})

Most efficient and fastest way to use the database, table.insert(object) and table.insertAll(array) doesn't check if columns exists, if you're sure that they exists, it's the fastest way. To use .insert() and .insertAll() methods from the start, you must define the table structure. It takes an object with key:array pairs, so each key must have an value of array.

const DB = require("nodejs-sqldb")

const tests = new DB({
  "name": [],
  "gpu": [],
  "score": []
});

tests.insertAll([
  {"cpu":"9900k", "gpu": "2080ti", "score": 14443},
  {"cpu":"2700x", "gpu": "2080super", "score": 9962},
  {"cpu":"9700k", "gpu": "1080ti", "score": 10965}
])

Methods

table.insert(object)

Insert one objects into the table.

  table.insert({
    "name": "Damian",
    "age": 25
  })

table.insert(array)

Inserts an array of objects into the table.

  table.insert([
    {"name": "Damian", "age": 25},
   {"name": "Kevin", "age": 20}
  ])

table.add(object)

Adds one objects into the table while checking if the key(column) exists.

  table.add({
    "name": "Damian",
    "age": 25
  })

table.addAll(array)

Adds an array of objects into the table while checking if the key(column) exists.

  table.insert([
    {"name": "Damian", "age": 25, "work": "upholstery"},
    {"name": "Kevin", "age": 20, "hobbies": ["games","swimming","workingout"]}
  ])

table.row(id: int)

Returns an objects, takes an id as integer.

  const user = table.row(1)
  // {"name": "Kevin", "age": 20, "hobbies": ["games","swimming","workingout"]}

table.max(columnName: string)

Returns the highest value inside a column

  const oldest = table.max("age")
  // 25

table.maxId(columnName: string)

Returns row if of the highest value inside a column

  const oldest = table.maxId("age")
  // 0

table.min(columnName: string)

Returns the lowest value inside a column

  const oldest = table.min("age")
  // 20

table.minId(columnName: string)

Returns row if of the lowest value inside a column

  const oldest = table.minId("age")
  // 1

table.set(id: int, columnName: string, value:any)

Sets a value inside a column for a row.

  table.set(1, "work", "Web Developer")
  // Old: {"name": "Kevin", "age": 20, "hobbies": ["games","swimming","workingout"]}
  // New: {"name": "Kevin", "age": 20, "hobbies": ["games","swimming","workingout"], "work": "Web Developer"}

table.createColumn(columnName: string)

Creates a new column inside the table.

  table.createColumn("yearsOfExperience")
  console.log(table.data)

table.find(columnName: string, keywords: string|array)

Takes an array of keywords or string with keywords separated with spaces. Returns an array of row indexes.

  const result = table.find("Kevin") // find is case sensitive
  for(const i of result) console.log(table.id(i)) // {"name": "Kevin", "age": 20, "hobbies": ["games","swimming","workingout"], "work": "Web Developer"}