A Deno Persistent, Embeddable and Optimized NoSQL Database for JS & TS

About DnDB

DnDB is a javascript persistent database written for Deno and available for Typescript.

Inspired by NeDB, DnDB is a robust but minimalist database engine written on JS that uses/emulates the mongo query API to edit and find data, making it 100% exportable to a mongojs environment.

Notice: This project is under development, so it is subject to future improvements and changes.

Why DnDB?

DnDB is an incredibly fast and powerful data store. All methods are streamed and buffered to optimize data reading/writing. DnDB is not only faster, but it consumes less resources (RAM) since only a small portion of the data stream is stored in memory by the buffer, enabling optimal performance on large data collections.

๐Ÿงช Quick test

Run the demo to test DnDB in a server environment.

๐Ÿ“ฆ Importing

deno.land

import Datastore from 'https://deno.land/x/dndb@0.2.4/mod.ts'

nest.land

import Datastore from 'https://x.nest.land/dndb@0.2.4/mod.ts'

๐Ÿ“– Usage

DnDB works by instantiating collections stored in a specific file given by the user in the instance configuration. By default the created instance exposes the necessary methods to work the datastore.

All the api methods are asynchronous by default, so they return promises, but itโ€™s also possible to pass a callback to them to work in a traditional mongo way.

โœ”๏ธ Instantiating the collection

import Datastore from 'https://deno.land/x/dndb@0.2.4/mod.ts'

const db = new Datastore({ filename:"./database.db", autoload: true })

When you instantiate a collection you can pass it a config object with a couple of options:

  • filename: The filename is the absolute path to your target file. If no filename is provided, DnDB will automatically create one in the current working directory, and if a full path is not specified, it will resolve the file name within the CWD.

  • autoload: The autoload option runs the loadDatabase method which creates the persistent file the first time DnDB is running in your project, this is optional, but if the loadDatabase method is not executed, the instance will not work until the persistent file exists.

Notice: The configuration content is currently in alpha, more options will be available soon.

๐Ÿ–‹๏ธ Inserting documents

All data types are allowed, but field names starting with โ€˜$โ€™ are reserved for data querying.

If the document does not contain an _id field, DnDB will automatically generate one for you (a RFC4122 UUID alphanumerical string). The _id of a document, once set, shouldnโ€™t be modified.

let obj = {
  name: 'denyn',
  lastName: 'crawford'
}

let insertion = await db.insert(obj)

// OR

db.insert(obj, (insertion) => {
  // ...foo(insetion)
})

To insert documents DnDB exposes the method:

  • insert:

  • returns: Array/object with the inserted documents.

The insert method receives two arguments:

  • data: Json data to insert
  • callback(optional): The callback function to get the data inserted.

You can also insert several documents at the same time by wrapping them in an array.

let foo = "foo"

db.insert([ { name: 'denyn' }, { name: foo } ], (insertion) => {
  // ...foo(insetion)
})

๐Ÿ” Finding documents

To find documents DnDB exposes the methods:

  • find: Finds all the documents that match the query.

    • returns: array of matching documents
  • findOne: Finds the first document that matches the query.

    • returns: exact first matching object

You can select documents based on field equality or use comparison operators ($lt, $lte, $gt, $gte, $in, $nin, $neq). You can also use logical operators $or, $and, $not and $eq. See below for the syntax.

You can use regular expressions in two ways: in basic querying in place of a string, or with the $regex operator.

Example of document querying:

Notice: See all rules and operators list here

db.find({name:"Denyn"}, {}, (docs) => {
  console.log(docs);
});

// or

let docs = await db.find({name:"Denyn"})

// Finding unique document

let docs = await db.findOne({username:"denyncrawford"})

// Deep querying syntax:

let docs = await db.find( { fullName: { lastName: "Crawford" } })

You can also use dot notation to find documents by deep querying.

let docs = await db.find( { "fullName.lastName": "Crawford" })

// Using dot notation to find inside arrays:

let docs = await db.find( { "list.games.0": "Doom" })

If you want to know how to advance query documents please read this

Projections

You can give find and findOne an optional second argument, projections. The syntax is the same as MongoDB: { a: 1, b: 1 } to return only the a and b fields, { a: 0, b: 0 } to omit these two fields. You cannot use both modes at the time, except for _id which is by default always returned and which you can choose to omit. You can project on nested documents.

Notice: See all rules and operators list here

db.find({ planet: 'Mars' }, { planet: 1, system: 1 }, function (docs) {
  // docs is [{ planet: 'Mars', system: 'solar', _id: 'id1' }]
});

๐Ÿ–Œ๏ธ Updating documents

To update documents DnDB exposes the method:

  • update

    • returns: array with the new updated collection
  • updateOne

    • returns: object with the new updated document.

The update method follows the same query rules as in find and findOne at first argument to get the update target document and as a second agument it receives the aggregation operators that modifies the matching fileds values โ€‹โ€‹by following the aggregation rules.

Notice: See all rules and operators list here

db.update({ name: 'denyn' }, { $set: { pet: 'Boots' } }, (update) => {
  // ...foo(update)
});

// OR 

let update = await db.update({ name: 'denyn' }, { $set: { pet: 'Boots' } })

โŒ Removing documents

To remove documents DnDB exposes the method:

  • remove

    • returns: array with the new removed collection
  • removeOne

    • returns: object with the new removed document

The remove method follows the same query rules as in find and findOne at first argument, it will remove all the documents that matches the query.

db.remove({ _id: 'id2' }, function (newDoc) {
  // ...foo(newDoc)
});

//OR

const { remove } = db 

await remove({ _id: 'id2' })

Notice: If you want to unset a value from the document you must use update with the $unset operator. See all rules and operators list here

๐Ÿ“ TO DO

  • Event hooks on all the api usage
  • Count method
  • Improve documentation
  • Prevent updating immutable data
  • Error handlers.
  • SORT, SKIP, and LIMIT modifier methods support.

๐Ÿ“Œ This module is right now on Beta, but the main API is pretty usable for production.

Since it is a standard, the API will not be subject to drastic changes, but its internal working will.

๐Ÿ‘Š Support this project by donating on:

  • Paypal.
  • BTC address: 39ik7oyYvmiMeTXTscY3bb9rUFMHdjf5pd

Download Details:

Author: denyncrawford

Demo: https://dndb.crawford.ml/

Source Code: https://github.com/denyncrawford/dndb

#deno #nodejs #node #javascript

What is GEEK

Buddha Community

A Deno  Persistent, Embeddable and Optimized NoSQL Database for JS & TS

NBB: Ad-hoc CLJS Scripting on Node.js

Nbb

Not babashka. Node.js babashka!?

Ad-hoc CLJS scripting on Node.js.

Status

Experimental. Please report issues here.

Goals and features

Nbb's main goal is to make it easy to get started with ad hoc CLJS scripting on Node.js.

Additional goals and features are:

  • Fast startup without relying on a custom version of Node.js.
  • Small artifact (current size is around 1.2MB).
  • First class macros.
  • Support building small TUI apps using Reagent.
  • Complement babashka with libraries from the Node.js ecosystem.

Requirements

Nbb requires Node.js v12 or newer.

How does this tool work?

CLJS code is evaluated through SCI, the same interpreter that powers babashka. Because SCI works with advanced compilation, the bundle size, especially when combined with other dependencies, is smaller than what you get with self-hosted CLJS. That makes startup faster. The trade-off is that execution is less performant and that only a subset of CLJS is available (e.g. no deftype, yet).

Usage

Install nbb from NPM:

$ npm install nbb -g

Omit -g for a local install.

Try out an expression:

$ nbb -e '(+ 1 2 3)'
6

And then install some other NPM libraries to use in the script. E.g.:

$ npm install csv-parse shelljs zx

Create a script which uses the NPM libraries:

(ns script
  (:require ["csv-parse/lib/sync$default" :as csv-parse]
            ["fs" :as fs]
            ["path" :as path]
            ["shelljs$default" :as sh]
            ["term-size$default" :as term-size]
            ["zx$default" :as zx]
            ["zx$fs" :as zxfs]
            [nbb.core :refer [*file*]]))

(prn (path/resolve "."))

(prn (term-size))

(println (count (str (fs/readFileSync *file*))))

(prn (sh/ls "."))

(prn (csv-parse "foo,bar"))

(prn (zxfs/existsSync *file*))

(zx/$ #js ["ls"])

Call the script:

$ nbb script.cljs
"/private/tmp/test-script"
#js {:columns 216, :rows 47}
510
#js ["node_modules" "package-lock.json" "package.json" "script.cljs"]
#js [#js ["foo" "bar"]]
true
$ ls
node_modules
package-lock.json
package.json
script.cljs

Macros

Nbb has first class support for macros: you can define them right inside your .cljs file, like you are used to from JVM Clojure. Consider the plet macro to make working with promises more palatable:

(defmacro plet
  [bindings & body]
  (let [binding-pairs (reverse (partition 2 bindings))
        body (cons 'do body)]
    (reduce (fn [body [sym expr]]
              (let [expr (list '.resolve 'js/Promise expr)]
                (list '.then expr (list 'clojure.core/fn (vector sym)
                                        body))))
            body
            binding-pairs)))

Using this macro we can look async code more like sync code. Consider this puppeteer example:

(-> (.launch puppeteer)
      (.then (fn [browser]
               (-> (.newPage browser)
                   (.then (fn [page]
                            (-> (.goto page "https://clojure.org")
                                (.then #(.screenshot page #js{:path "screenshot.png"}))
                                (.catch #(js/console.log %))
                                (.then #(.close browser)))))))))

Using plet this becomes:

(plet [browser (.launch puppeteer)
       page (.newPage browser)
       _ (.goto page "https://clojure.org")
       _ (-> (.screenshot page #js{:path "screenshot.png"})
             (.catch #(js/console.log %)))]
      (.close browser))

See the puppeteer example for the full code.

Since v0.0.36, nbb includes promesa which is a library to deal with promises. The above plet macro is similar to promesa.core/let.

Startup time

$ time nbb -e '(+ 1 2 3)'
6
nbb -e '(+ 1 2 3)'   0.17s  user 0.02s system 109% cpu 0.168 total

The baseline startup time for a script is about 170ms seconds on my laptop. When invoked via npx this adds another 300ms or so, so for faster startup, either use a globally installed nbb or use $(npm bin)/nbb script.cljs to bypass npx.

Dependencies

NPM dependencies

Nbb does not depend on any NPM dependencies. All NPM libraries loaded by a script are resolved relative to that script. When using the Reagent module, React is resolved in the same way as any other NPM library.

Classpath

To load .cljs files from local paths or dependencies, you can use the --classpath argument. The current dir is added to the classpath automatically. So if there is a file foo/bar.cljs relative to your current dir, then you can load it via (:require [foo.bar :as fb]). Note that nbb uses the same naming conventions for namespaces and directories as other Clojure tools: foo-bar in the namespace name becomes foo_bar in the directory name.

To load dependencies from the Clojure ecosystem, you can use the Clojure CLI or babashka to download them and produce a classpath:

$ classpath="$(clojure -A:nbb -Spath -Sdeps '{:aliases {:nbb {:replace-deps {com.github.seancorfield/honeysql {:git/tag "v2.0.0-rc5" :git/sha "01c3a55"}}}}}')"

and then feed it to the --classpath argument:

$ nbb --classpath "$classpath" -e "(require '[honey.sql :as sql]) (sql/format {:select :foo :from :bar :where [:= :baz 2]})"
["SELECT foo FROM bar WHERE baz = ?" 2]

Currently nbb only reads from directories, not jar files, so you are encouraged to use git libs. Support for .jar files will be added later.

Current file

The name of the file that is currently being executed is available via nbb.core/*file* or on the metadata of vars:

(ns foo
  (:require [nbb.core :refer [*file*]]))

(prn *file*) ;; "/private/tmp/foo.cljs"

(defn f [])
(prn (:file (meta #'f))) ;; "/private/tmp/foo.cljs"

Reagent

Nbb includes reagent.core which will be lazily loaded when required. You can use this together with ink to create a TUI application:

$ npm install ink

ink-demo.cljs:

(ns ink-demo
  (:require ["ink" :refer [render Text]]
            [reagent.core :as r]))

(defonce state (r/atom 0))

(doseq [n (range 1 11)]
  (js/setTimeout #(swap! state inc) (* n 500)))

(defn hello []
  [:> Text {:color "green"} "Hello, world! " @state])

(render (r/as-element [hello]))

Promesa

Working with callbacks and promises can become tedious. Since nbb v0.0.36 the promesa.core namespace is included with the let and do! macros. An example:

(ns prom
  (:require [promesa.core :as p]))

(defn sleep [ms]
  (js/Promise.
   (fn [resolve _]
     (js/setTimeout resolve ms))))

(defn do-stuff
  []
  (p/do!
   (println "Doing stuff which takes a while")
   (sleep 1000)
   1))

(p/let [a (do-stuff)
        b (inc a)
        c (do-stuff)
        d (+ b c)]
  (prn d))
$ nbb prom.cljs
Doing stuff which takes a while
Doing stuff which takes a while
3

Also see API docs.

Js-interop

Since nbb v0.0.75 applied-science/js-interop is available:

(ns example
  (:require [applied-science.js-interop :as j]))

(def o (j/lit {:a 1 :b 2 :c {:d 1}}))

(prn (j/select-keys o [:a :b])) ;; #js {:a 1, :b 2}
(prn (j/get-in o [:c :d])) ;; 1

Most of this library is supported in nbb, except the following:

  • destructuring using :syms
  • property access using .-x notation. In nbb, you must use keywords.

See the example of what is currently supported.

Examples

See the examples directory for small examples.

Also check out these projects built with nbb:

API

See API documentation.

Migrating to shadow-cljs

See this gist on how to convert an nbb script or project to shadow-cljs.

Build

Prequisites:

  • babashka >= 0.4.0
  • Clojure CLI >= 1.10.3.933
  • Node.js 16.5.0 (lower version may work, but this is the one I used to build)

To build:

  • Clone and cd into this repo
  • bb release

Run bb tasks for more project-related tasks.

Download Details:
Author: borkdude
Download Link: Download The Source Code
Official Website: https://github.com/borkdude/nbb 
License: EPL-1.0

#node #javascript

Kole  Haag

Kole Haag

1602403200

What is NoSQL and How is it Utilized?

Posted on September 25, 2020 by Dean Conally | Updated: October 8, 2020

Category: Tutorials | Tags: CassandraColumnsDatabaseDatabase ManagementDatabase StructureDB2Document StoresDynamic SchemaExtensible Record StoresGraph StoresJSONKey-ValueMSSQLMulti-RowMySQLNodeNode Relationship NodeNon-Relational DatabasesNoSQLNoSQL ModelQueryRowsScalabilitySchema FreeSQLStoresTablesWide-Column

Reading Time: 5 minutes

What is NoSQL?

A NoSQL or a NoSQL Database is a term used when referring to a โ€œnon SQLโ€ or โ€œnot only SQLโ€ database. NoSQL databases store data in a different format than a traditional relational database management systems. This is why NoSQL is often associated with the term โ€œnon-relationalโ€ database. Simply put, NoSQL databases are modern databases with high flexibility, blazing performance, and built for scalability. These databases are used when you require low latency and high extensibility while working with large data structures. The versatility of NoSQL is due to the nature of as being unrestricted in comparison to relational databases models such as MySQL or DB2.

SQL vs. NoSQL Comparison

There are multiple differences between SQL and NoSQL database types. In the table below, we will compare some of the most critical variations.

#tutorials #cassandra #columns #database #database management #database structure #db2 #document stores #dynamic schema #extensible record stores #graph stores #json #key-value #mssql #multi-row #mysql #node #node relationship node #non-relational databases #nosql #nosql model #query #rows #scalability #schema free #sql #stores #tables #wide-column

A Deno Persistent, Embeddable and Optimized NoSQL Database for JS & TS

About DnDB

DnDB is a javascript persistent database written for Deno and available for Typescript.

Inspired by NeDB, DnDB is a robust but minimalist database engine written on JS that uses/emulates the mongo query API to edit and find data, making it 100% exportable to a mongojs environment.

Notice: This project is under development, so it is subject to future improvements and changes.

Why DnDB?

DnDB is an incredibly fast and powerful data store. All methods are streamed and buffered to optimize data reading/writing. DnDB is not only faster, but it consumes less resources (RAM) since only a small portion of the data stream is stored in memory by the buffer, enabling optimal performance on large data collections.

๐Ÿงช Quick test

Run the demo to test DnDB in a server environment.

๐Ÿ“ฆ Importing

deno.land

import Datastore from 'https://deno.land/x/dndb@0.2.4/mod.ts'

nest.land

import Datastore from 'https://x.nest.land/dndb@0.2.4/mod.ts'

๐Ÿ“– Usage

DnDB works by instantiating collections stored in a specific file given by the user in the instance configuration. By default the created instance exposes the necessary methods to work the datastore.

All the api methods are asynchronous by default, so they return promises, but itโ€™s also possible to pass a callback to them to work in a traditional mongo way.

โœ”๏ธ Instantiating the collection

import Datastore from 'https://deno.land/x/dndb@0.2.4/mod.ts'

const db = new Datastore({ filename:"./database.db", autoload: true })

When you instantiate a collection you can pass it a config object with a couple of options:

  • filename: The filename is the absolute path to your target file. If no filename is provided, DnDB will automatically create one in the current working directory, and if a full path is not specified, it will resolve the file name within the CWD.

  • autoload: The autoload option runs the loadDatabase method which creates the persistent file the first time DnDB is running in your project, this is optional, but if the loadDatabase method is not executed, the instance will not work until the persistent file exists.

Notice: The configuration content is currently in alpha, more options will be available soon.

๐Ÿ–‹๏ธ Inserting documents

All data types are allowed, but field names starting with โ€˜$โ€™ are reserved for data querying.

If the document does not contain an _id field, DnDB will automatically generate one for you (a RFC4122 UUID alphanumerical string). The _id of a document, once set, shouldnโ€™t be modified.

let obj = {
  name: 'denyn',
  lastName: 'crawford'
}

let insertion = await db.insert(obj)

// OR

db.insert(obj, (insertion) => {
  // ...foo(insetion)
})

To insert documents DnDB exposes the method:

  • insert:

  • returns: Array/object with the inserted documents.

The insert method receives two arguments:

  • data: Json data to insert
  • callback(optional): The callback function to get the data inserted.

You can also insert several documents at the same time by wrapping them in an array.

let foo = "foo"

db.insert([ { name: 'denyn' }, { name: foo } ], (insertion) => {
  // ...foo(insetion)
})

๐Ÿ” Finding documents

To find documents DnDB exposes the methods:

  • find: Finds all the documents that match the query.

    • returns: array of matching documents
  • findOne: Finds the first document that matches the query.

    • returns: exact first matching object

You can select documents based on field equality or use comparison operators ($lt, $lte, $gt, $gte, $in, $nin, $neq). You can also use logical operators $or, $and, $not and $eq. See below for the syntax.

You can use regular expressions in two ways: in basic querying in place of a string, or with the $regex operator.

Example of document querying:

Notice: See all rules and operators list here

db.find({name:"Denyn"}, {}, (docs) => {
  console.log(docs);
});

// or

let docs = await db.find({name:"Denyn"})

// Finding unique document

let docs = await db.findOne({username:"denyncrawford"})

// Deep querying syntax:

let docs = await db.find( { fullName: { lastName: "Crawford" } })

You can also use dot notation to find documents by deep querying.

let docs = await db.find( { "fullName.lastName": "Crawford" })

// Using dot notation to find inside arrays:

let docs = await db.find( { "list.games.0": "Doom" })

If you want to know how to advance query documents please read this

Projections

You can give find and findOne an optional second argument, projections. The syntax is the same as MongoDB: { a: 1, b: 1 } to return only the a and b fields, { a: 0, b: 0 } to omit these two fields. You cannot use both modes at the time, except for _id which is by default always returned and which you can choose to omit. You can project on nested documents.

Notice: See all rules and operators list here

db.find({ planet: 'Mars' }, { planet: 1, system: 1 }, function (docs) {
  // docs is [{ planet: 'Mars', system: 'solar', _id: 'id1' }]
});

๐Ÿ–Œ๏ธ Updating documents

To update documents DnDB exposes the method:

  • update

    • returns: array with the new updated collection
  • updateOne

    • returns: object with the new updated document.

The update method follows the same query rules as in find and findOne at first argument to get the update target document and as a second agument it receives the aggregation operators that modifies the matching fileds values โ€‹โ€‹by following the aggregation rules.

Notice: See all rules and operators list here

db.update({ name: 'denyn' }, { $set: { pet: 'Boots' } }, (update) => {
  // ...foo(update)
});

// OR 

let update = await db.update({ name: 'denyn' }, { $set: { pet: 'Boots' } })

โŒ Removing documents

To remove documents DnDB exposes the method:

  • remove

    • returns: array with the new removed collection
  • removeOne

    • returns: object with the new removed document

The remove method follows the same query rules as in find and findOne at first argument, it will remove all the documents that matches the query.

db.remove({ _id: 'id2' }, function (newDoc) {
  // ...foo(newDoc)
});

//OR

const { remove } = db 

await remove({ _id: 'id2' })

Notice: If you want to unset a value from the document you must use update with the $unset operator. See all rules and operators list here

๐Ÿ“ TO DO

  • Event hooks on all the api usage
  • Count method
  • Improve documentation
  • Prevent updating immutable data
  • Error handlers.
  • SORT, SKIP, and LIMIT modifier methods support.

๐Ÿ“Œ This module is right now on Beta, but the main API is pretty usable for production.

Since it is a standard, the API will not be subject to drastic changes, but its internal working will.

๐Ÿ‘Š Support this project by donating on:

  • Paypal.
  • BTC address: 39ik7oyYvmiMeTXTscY3bb9rUFMHdjf5pd

Download Details:

Author: denyncrawford

Demo: https://dndb.crawford.ml/

Source Code: https://github.com/denyncrawford/dndb

#deno #nodejs #node #javascript

Ruth  Nabimanya

Ruth Nabimanya

1620633584

System Databases in SQL Server

Introduction

In SSMS, we many of may noticed System Databases under the Database Folder. But how many of us knows its purpose?. In this article lets discuss about the System Databases in SQL Server.

System Database

Fig. 1 System Databases

There are five system databases, these databases are created while installing SQL Server.

  • Master
  • Model
  • MSDB
  • Tempdb
  • Resource
Master
  • This database contains all the System level Information in SQL Server. The Information in form of Meta data.
  • Because of this master database, we are able to access the SQL Server (On premise SQL Server)
Model
  • This database is used as a template for new databases.
  • Whenever a new database is created, initially a copy of model database is what created as new database.
MSDB
  • This database is where a service called SQL Server Agent stores its data.
  • SQL server Agent is in charge of automation, which includes entities such as jobs, schedules, and alerts.
TempDB
  • The Tempdb is where SQL Server stores temporary data such as work tables, sort space, row versioning information and etc.
  • User can create their own version of temporary tables and those are stored in Tempdb.
  • But this database is destroyed and recreated every time when we restart the instance of SQL Server.
Resource
  • The resource database is a hidden, read only database that holds the definitions of all system objects.
  • When we query system object in a database, they appear to reside in the sys schema of the local database, but in actually their definitions reside in the resource db.

#sql server #master system database #model system database #msdb system database #sql server system databases #ssms #system database #system databases in sql server #tempdb system database

Ruth  Nabimanya

Ruth Nabimanya

1620648300

What is SQL? And Where is it Used?

Define: SQL [pron. โ€œsequelโ€] โ€“ stands for Structured Query Language (SQL), used by databases to model and manage tabular/relational datasets; a set of standardized Data Definition Language (DDL) functions to create tables, views, and define relational schema models, and Data Manipulation Language (DML) to query, insert, and modify data in the tables.

*Read-only select queries are technically part of its Data Query Language (DQL) group. Still, operationally, many refer to it as DML because it can do more than read-only queries.

Super Brief History of SQL

Who Uses SQL?

What Can a SQL Database Do?

What is a SQL Query?

Different SQL Database Platforms

SQL for JSON

Application Developers

SQL for Big Data

Further Reading

#sql #database #relational-database #nosql #json #sql-database #database-administration #databases-best-practices