Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Factory WIP #96

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@ let page = require('./src/page')
let count = require('./src/count')
let incr = require('./src/incr')
let decr = require('./src/decr')
let Table = require('./src/table')

/**
* instantiate many tables
*
* example
*
* let [cats, dogs] = factory('cats', 'dogs')
*/
function factory (...args) {
let result = args.map(name => new Table(name))
return result.length === 1 ? result[0] : result
}

module.exports = {
get,
Expand All @@ -14,4 +27,6 @@ module.exports = {
count,
incr,
decr,
Table,
factory
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"main": "index.js",
"scripts": {
"lint": "eslint --fix .",
"test": "npm run lint && tape test/*-test.js | tap-spec",
"test": "npm run lint && tape test/factory-test.js | tap-spec",
"_test": "npm run lint && tape test/*-test.js | tap-spec",
"rc": "npm version prerelease --preid RC"
},
"license": "Apache-2.0",
Expand Down
96 changes: 96 additions & 0 deletions src/table.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
let _get = require('./get')
let _set = require('./set')
let _destroy = require('./destroy')
let _page = require('./page')
let _count = require('./count')
let _incr = require('./incr')
let _decr = require('./decr')

/**
* instantiate a begin/data client scoped to a table
* example
*
* const cats = new Table('cats')
*/
module.exports = class Table {

/**
* @param {string} name
*/
constructor (name) {
this.name = name
}

async get (args) {
let params = addTable(args, this.name)
let result = await _get.call({}, params)
return removeTable(result)
}

async set (args) {
let params = addTable(args, this.name)
let result = await _set.call({}, params)
return removeTable(result)
}

async destroy (args) {
let params = addTable(args, this.name)
let result = await _destroy.call({}, params)
return removeTable(result)
}

async page (args) {
let params = addTable(args, this.name)
let result = await _page.call({}, params)
return removeTable(result)
}

async count (args) {
let params = addTable(args, this.name)
let result = await _count.call({}, params)
return removeTable(result)
}

async incr (args) {
let params = addTable(args, this.name)
let result = await _incr.call({}, params)
return removeTable(result)
}

async decr (args) {
let params = addTable(args, this.name)
let result = await _decr.call({}, params)
return removeTable(result)
}
}

function addTable (args, name) {
let params
if (Array.isArray(args)) {
params = args.slice(0).map(function (param) {
params.table = name
return param
})
}
else {
params = { ...args }
params.table = name
}
return params
}

function removeTable (result) {
if (Array.isArray(result)) {
let returns = result.slice(0).map(function (r) {
delete r.table
return r
})
if (result.cursor)
returns.cursor = result.cursor
return returns
}
else {
delete result.table
return result
}
}
54 changes: 54 additions & 0 deletions test/factory-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
let sandbox = require('@architect/sandbox')
let { Table, factory } = require('../')
let test = require('tape')

test('start sandbox', async t => {
t.plan(1)
await sandbox.start({ cwd: __dirname, quiet: true })
t.pass('started')
})

test('classy style', async t => {
t.plan(4)
let cats = new Table('cats')
let sutr0 = await cats.set({ name: 'sutr0' })
let again = await cats.get(sutr0)
t.ok(!!sutr0.key, 'has key')
t.ok(!!sutr0.table === false, 'but no table')
t.ok(again.key === sutr0.key, 'found sutr0')
t.ok(!!again.table === false, 'but no table')
console.log(sutr0, again)
})

test('factory style', async t => {
t.plan(2)
let [ cats, dogs ] = factory('cats', 'dogs')
let tuxedo = await cats.set({ name: 'tuxedo' })
let yoda = await dogs.set({ name: 'yoda' })
t.ok(!!tuxedo.key, 'tuxedo has key')
t.ok(!!yoda.key, 'yoda has key')
console.log(tuxedo, yoda)
})

test('page', async t => {
t.plan(1)
let cats = factory('cats')
let pages = await cats.page({ limit: 1 })
let count = 0
for await (let c of pages)
count += c.length
t.ok(count === 2, 'found two cats')
})

test('count', async t => {
t.plan(1)
let cats = factory('cats')
let len = await cats.count()
t.ok(len === 2, 'found two cats')
})

test('end sandbox', async t => {
t.plan(1)
await sandbox.end()
t.pass('ended')
})