Skip to content

Commit

Permalink
Updating app files
Browse files Browse the repository at this point in the history
  • Loading branch information
tarsislimadev committed May 11, 2024
1 parent c6cd2cf commit 93fbcd8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 62 deletions.
28 changes: 3 additions & 25 deletions src/database.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
const { v4: uuid } = require('@brtmvdl/uuid')

const fs = require('./libs/fs/index.js')
const path = require('path')

const { DatabaseObject } = require('./object.js')

class Database {
type = 'fs'
config = process.env.DATA_PATH
timestamp = null

constructor({
type = this.type,
config = this.config,
timestamp = this.timestamp,
} = {}) {
this.type = type
this.config = config
this.timestamp = timestamp
}

setType() {
Expand All @@ -38,36 +33,19 @@ class Database {
return this.config
}

setTimestamp(timestamp = null) {
this.timestamp = timestamp
return this
}

getTimestamp() {
if (this.timestamp != null) {
return this.timestamp
}

return Date.now()
}

in(dir = '') {
const { config, type } = this
return new Database({
config: path.resolve(config, dir),
timestamp: this.getTimestamp(),
type,
})
return new Database({ config: path.resolve(config, dir), type, })
}

new(id = uuid()) {
return new DatabaseObject(this, id, this.getTimestamp())
return new DatabaseObject(this, id)
}

list() {
return fs
.readdirSync(this.config)
.map((path) => new DatabaseObject(this, path, this.getTimestamp()))
.map((path) => new DatabaseObject(this, path))
}

find(search = {}) {
Expand Down
45 changes: 8 additions & 37 deletions src/object.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
const { Database } = require('./database')
const { v4: uuid } = require('@brtmvdl/uuid')

const fs = require('./libs/fs/index.js')
const path = require('path')

class DatabaseObject {
db = null
id = null
timestamp = null

constructor(database = new Database(), id = uuid(), timestamp = null) {
constructor(database = new Database(), id = uuid()) {
this.db = database
this.id = id
this.setTimestamp(timestamp)
}

setId(id = uuid()) {
Expand All @@ -24,17 +21,6 @@ class DatabaseObject {
return this.id
}

setTimestamp(timestamp = null) {
this.timestamp = timestamp
return this
}

getTimestamp() {
return this.timestamp != null
? this.timestamp.toString()
: Date.now()
}

getID() {
return this.id
}
Expand All @@ -48,11 +34,8 @@ class DatabaseObject {
}

write(key, value = '') {
const pathname = this.getPath()
const filename = key + '.' + this.getTimestamp()
const filepathname = path.resolve(pathname, filename)

fs.writeFileSync(filepathname, value)
const filename = path.resolve(this.getPath(), key)
fs.writeFileSync(filename, value)
return this
}

Expand All @@ -61,31 +44,19 @@ class DatabaseObject {
.map((key) => this.write(key, obj[key]))
}

getPropName(prop, timestamp = this.getTimestamp()) {
return this.getProps()
.map((filename) => filename.split('.', 2))
.filter(([propName,]) => propName === prop)
.filter(([, propTimestamp]) => +propTimestamp <= +timestamp)
.sort(([, t1], [, t2]) => +t2 - +t1)
.find(() => true)
?.join('.')
}

read(prop, timestamp = this.getTimestamp()) {
read(prop) {
const filepath = this.getPath()
const propName = this.getPropName(prop, timestamp)
if (propName === undefined) return null
const filename = path.resolve(filepath, propName)
const filename = path.resolve(filepath, prop)
return fs.readFileSync(filename, {})
}

readMany(props = []) {
return props.map((key) => this.read(key))
}

readString(prop, timestamp = this.getTimestamp()) {
const blob = this.read(prop, timestamp)
if (blob == undefined) return null
readString(prop) {
const blob = this.read(prop)
if (blob == undefined) return ''
return blob.toString()
}

Expand Down

0 comments on commit 93fbcd8

Please sign in to comment.