xndatabase
v1.0.4
Published
A simple module for creating, saving and loading custom database files with a .xn extension.
Downloads
24
Readme
xndatabase
A simple module for creating, saving and loading custom database files with a .xn extension.
Installation
npm install xndatabase
Usage
const XNDatabase = require('xndatabase');
// Create a new database
const myDB = new XNDatabase('My Database');
// Add tables to the database
myDB.addTable('users', {
name: 'string',
age: 'number',
email: 'string'
});
myDB.addTable('posts', {
title: 'string',
content: 'string',
author: 'string'
});
// Save the database to a file
myDB.saveToFile('./my-database.xn');
// Load the database from a file
const loadedDB = XNDatabase.loadFromFile('./my-database.xn');
// Access the tables and fields in the loaded database
const usersTable = loadedDB.tables.users;
const postsTable = loadedDB.tables.posts;
const userName = usersTable.fields.name;
const postTitle = postsTable.fields.title;
API
new XNDatabase(dbName, [version])
Creates a new database with the given name and version.
dbName
- The name of the databaseversion
- The version of the database (optional)
addTable(tableName, fields)
Adds a new table to the database.
tableName
- The name of the tablefields
- An object containing the field names and types for the table
saveToFile(filePath)
Saves the database to a file.
filePath
- The path to the file to save the database to
static loadFromFile(filePath)
Loads a database from a file.
filePath
- The path to the file to load the database from
static getFileExtension()
Gets the file extension for the custom database format (.xn).
static isDatabaseFile(filePath)
Checks if a file has the custom database format (.xn).
filePath
- The path to the file to check
Example of use in real world scenario:
const XNDatabase = require('xndatabase');
// Create a new database
const db = new XNDatabase('example');
// Add two tables to the database
db.addTable('users', {
'id': 'integer',
'name': 'text',
'email': 'text'
});
db.addTable('orders', {
'id': 'integer',
'user_id': 'integer',
'product': 'text',
'quantity': 'integer'
});
// Save the database to a file
db.saveToFile('example_db.xn');
Example of use to load Database:
const XNDatabase = require('xndatabase');
// Load the database from a file
const db = XNDatabase.loadFromFile('example_db.xn');
// Access the 'users' table and print its fields
const usersTable = db.tables['users'];
console.log('Fields in the users table:');
for (const fieldName in usersTable.fields) {
console.log(fieldName + ': ' + usersTable.fields[fieldName]);
}
// Access the 'orders' table and print its fields
const ordersTable = db.tables['orders'];
console.log('Fields in the orders table:');
for (const fieldName in ordersTable.fields) {
console.log(fieldName + ': ' + ordersTable.fields[fieldName]);
}