The DAO is everything and nothing, it comes from emptiness yet fills the universe.
npm install nothingness```
npm install --save nothingness
`javascript
// thinger-dao.js
// using Babel's module loader for Node
import DAO from 'nothingness'
import { v4 as uuid } from 'node-uuid'
export default class ThingerDAO extends DAO {
generateID (pojo) {
// the #yolo uniqueness constraint
const id = uuid()
pojo[DAO.idSymbol] = id
return id
}
}
`
Use it to persist and load an object:
`javascript
// main.js
import ThingerDAO from './thinger-dao.js'
import assert from 'assert'
import Adaptor from '@nothingness/level'
const dao = new ThingerDAO(new Adaptor('./thinger-db'))
const thingy = { type: 'band' }
// uses Bluebird's .nodeify(), so callback or promise chain are fine
dao.save(thingy)
.then(() => dao.findAll())
.then(results => assert.deepEqual(
results,
[{ type: 'band' }],
'should only have one item, of type "band"'
))
.then(() => console.log('round trip succeeded!'))
.catch(err => console.error(err.stack))
.finally(() => dao.closeDB())
``