Verified Commit 18e13854 authored by Aral Balkan's avatar Aral Balkan
Browse files

Make direct instantiation attempt on JSDB throw; update basic example

parent e61835fb
......@@ -52,22 +52,26 @@ const JSDB = require('@small-tech/jsdb')
// Create your database in the test folder.
// (This is where your JSON files – “tables” – will be saved.)
const db = new JSDB('db')
// Create db/people.js table with some initial data.
db.people = [
{name: 'Aral', age: 43},
{name: 'Laura', age: 34}
]
// Correct Laura’s age. (This will automatically update db/people.js)
db.people[1].age = 33
// Add Oskar to the family. (This will automatically update db/people.js)
db.people.push({name: 'Oskar', age: 8})
// Update Oskar’s name to use his nickname. (This will automatically update db/people.js)
db.people[2].name = 'Osky'
//
const db = JSDB.open('db')
// Create db/people.js table with some initial data if it
// doesn’t already exist.
if (!db.people) {
db.people = [
{name: 'Aral', age: 43},
{name: 'Laura', age: 34}
]
// Correct Laura’s age. (This will automatically update db/people.js)
db.people[1].age = 33
// Add Oskar to the family. (This will automatically update db/people.js)
db.people.push({name: 'Oskar', age: 8})
// Update Oskar’s name to use his nickname. (This will automatically update db/people.js)
db.people[2].name = 'Osky'
}
```
After running the above script, take a look at the resulting database table in the `./db/people.js` file.
......
......@@ -2,19 +2,21 @@ const JSDB = require('../..')
// Create your database in the test folder.
// (This is where your JSON files – “tables” – will be saved.)
const db = new JSDB('db')
const db = JSDB.open('db')
// Create test/people.json with some data.
db.people = [
{name: 'Aral', age: 43},
{name: 'Laura', age: 34}
]
if (!db.people) {
db.people = [
{name: 'Aral', age: 43},
{name: 'Laura', age: 34}
]
// Correct Laura’s age. (This will automatically update db/people.js)
db.people[1].age = 33
// Correct Laura’s age. (This will automatically update db/people.js)
db.people[1].age = 33
// Add Oskar to the family. (This will automatically update db/people.js)
db.people.push({name: 'Oskar', age: 8})
// Add Oskar to the family. (This will automatically update db/people.js)
db.people.push({name: 'Oskar', age: 8})
// Update Oskar’s name to use his nickname. (This will automatically update db/people.js)
db.people[2].name = 'Osky'
// Update Oskar’s name to use his nickname. (This will automatically update db/people.js)
db.people[2].name = 'Osky'
}
const JSDB = require('../..')
// This will load test database with the people table we created earlier.
const db = new JSDB('db')
const db = JSDB.open('db')
// Let’s make sure Oskar’s in there… ;)
console.log(db.people[2])
......@@ -30,21 +30,24 @@ class JSDB {
// Class.
//
static openDatabases = {}
static #isBeingInstantiatedByFactoryMethod = false
static #openDatabases = {}
// Returns a reference to the JSDB at the given basepath. If it’s already open,
// returns the reference.
static open (basePath, options) {
basePath = path.resolve(basePath)
if (this.openDatabases[basePath] == undefined) {
this.openDatabases[basePath] = new this(basePath, options)
if (this.#openDatabases[basePath] == undefined) {
this.#isBeingInstantiatedByFactoryMethod = true
this.#openDatabases[basePath] = new this(basePath, options)
this.#isBeingInstantiatedByFactoryMethod = false
}
return this.openDatabases[basePath]
return this.#openDatabases[basePath]
}
static close (basePath) {
this.openDatabases[basePath] = null
this.#openDatabases[basePath] = null
}
//
......@@ -57,6 +60,13 @@ class JSDB {
constructor (basePath, options = {
deleteIfExists: false
}) {
// This class can only be instantiated via the open() factory method.
// This is to ensure that multiple instances of the same database cannot be opened at the
// same time, thereby leading to data corruption.
if (!JSDB.#isBeingInstantiatedByFactoryMethod) {
throw new Error('The JSDB class cannot be directly instantiated. Please use the JSDB.open() factory method instead.')
}
this.basePath = basePath
this.options = options
......
......@@ -668,5 +668,8 @@ test('JSDB', t => {
t.strictEquals(JSON.stringify(loadTable('db', 'arrayTable')), JSON.stringify(db.arrayTable), 'persisted array table matches in-memory data')
t.strictEquals(JSON.stringify(loadTable('db', 'objectTable')), JSON.stringify(db.objectTable), 'persisted object table matched in-memory data')
// Attempting to instantiate the JSDB class directly throws.
t.throws(() => { new JSDB('someBasePath') }, 'Attempting to instantiate the JSDB class directly throws.')
t.end()
})
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment