A simple lightweight package to manage data
Read the Wiki »
Explore the typedoc »
Quickstart
·
Report Bug
·
Request Feature
- js-yaml@^3.14.0 - the package has an integrated adaptor for yaml using js-yaml (install if you want to use yaml)
- xml-js@^1.6.11 - the package has an integrated adaptor for xml using xml-js (install if you want to use xml)
To get the package up and running follow these simple steps.
You should have already installed npm to install this package
- npm
npm install npm@latest -g
- Install the package
npm i nscdb
Import using Typescript
import { createDatabase } from 'nscdb';
import { JsonFileAdapter } from 'nscdb/json_adapter';
Import using Javascript
const { createDatabase } = require('nscdb');
const { JsonFileAdapter } = require('nscdb/json_adapter');
A small system using the api
let database = await createDatabase(new JsonFileAdapter("./database.json"));
// Set defaults
database.setDefaults({
users: []
});
// Push a value into the Database
let users = database.get("users");
users.push({
id: database.generateId("users"),
name: 'Harleen Dolan',
password: 'a password'
});
users.push({
id: database.generateId("users"),
name: 'Lisa Bradley',
password: 'another password'
});
// print output data from the database
console.log(database.data);
// Console Output:
// >> {
// >> users: [
// >> { id: 0, name: 'Harleen Dolan', password: 'a password' },
// >> { id: 1, name: 'Lisa Bradley', password: 'another password' }
// >> ],
// >> id_counters: { users: 1 }
// >> }
// Save the Database
await database.saveData();
If you want to use yaml instead, replace the json adapter with a yaml adapter and install js-yaml
api
install js-yaml
using npm
npm i js-yaml
Import using Typescript
import { YamlFileAdapter } from 'nscdb/yaml_adapter';
Import using Javascript
const { YamlFileAdapter } = require('nscdb/yaml_adapter');
Database creation
let database = await createDatabase(new JsonFileAdapter("./database.json"));
Warning: Using synchronous versions of some adapters may slow down your application.
First of all use the synchronous adapters of the adapter
Typescript
import { createDatabase } from 'nscdb';
import { SyncJsonFileAdapter } from 'nscdb/json_adapter';
Javascript
const { createDatabase } = require('nscdb');
const { SyncJsonFileAdapter } = require('nscdb/json_adapter');
Now you can use the normal quickstart again, but no await instruction is needed anymore
let database = createDatabase(new JsonFileAdapter("./database.json"));
// Set defaults
database.setDefaults({
users: []
});
// Push a value into the Database
let users = database.get("users");
users.push({
id: database.generateId("users"),
name: 'Harleen Dolan',
password: 'a password'
});
users.push({
id: database.generateId("users"),
name: 'Lisa Bradley',
password: 'another password'
});
// print output data from the database
console.log(database.data);
// Console Output:
// >> {
// >> users: [
// >> { id: 0, name: 'Harleen Dolan', password: 'a password' },
// >> { id: 1, name: 'Lisa Bradley', password: 'another password' }
// >> ],
// >> id_counters: { users: 1 }
// >> }
// Save the Database
database.saveData();
For more examples, please refer to the Documentation
See the open issues for a list of proposed features (and known issues).
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
- Fork the Project
- Create your own Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'Add some AmazingFeature'
) - Push to the Branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
Distributed under the BSD2-Clause License. See LICENSE
for more information.
Project Link: https://github.com/nsc-de/js-database
js-database by Nicolas Schmidt | License BSD-2-Clause | read the docs | GitHub | NPM