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

feat: add deleted to table Doc #18

Merged
merged 5 commits into from
Sep 26, 2023
Merged
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
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import assert from 'assert'
* @property {string} versionId
* @property {string[]} links
* @property {string} updatedAt
* @property {Boolean} deleted
*/

/** @typedef {{ type: string, pk: 1 | 0, cid: number, notnull: 1 | 0, dflt_value: any, name: string }} ColumnInfo */
Expand All @@ -28,6 +29,7 @@ const docSchema = {
links: { type: 'TEXT', notnull: 1, dflt_value: null, pk: 0 },
forks: { type: 'TEXT', notnull: 1, dflt_value: null, pk: 0 },
updatedAt: { type: 'TEXT', notnull: 1, pk: 0 },
deleted: { type: 'INTEGER', notnull: 1, pk: 0 },
}

/** @type {ColumnSchema} */
Expand Down Expand Up @@ -70,7 +72,7 @@ export class DbApi {
)
const docColumns = tableInfo.map(({ name }) => name)
this.#getDocSql = db.prepare(
`SELECT docId, versionId, links, forks, updatedAt
`SELECT docId, versionId, links, forks, updatedAt, deleted
FROM ${docTableName}
WHERE docId = ?`
)
Expand Down
15 changes: 14 additions & 1 deletion test/complex-docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ test('booleans, arrays and objects are transformed', async (t) => {
boolean: true,
array: [],
object: {},
deleted: false,
},
{
docId: 'B',
Expand All @@ -22,8 +23,17 @@ test('booleans, arrays and objects are transformed', async (t) => {
boolean: false,
array: ['foo'],
object: { foo: 'bar' },
deleted: false,
},
{
docId: 'C',
versionId: '1',
links: [],
updatedAt,
array: [],
object: {},
deleted: false,
},
{ docId: 'C', versionId: '1', links: [], updatedAt, array: [], object: {} },
]

const extraColumns = `
Expand All @@ -45,6 +55,7 @@ object TEXT NOT NULL`
boolean: 1,
array: '[]',
object: '{}',
deleted: 0,
},
{
docId: 'B',
Expand All @@ -55,6 +66,7 @@ object TEXT NOT NULL`
boolean: 0,
array: '["foo"]',
object: '{"foo":"bar"}',
deleted: 0,
},
{
docId: 'C',
Expand All @@ -65,6 +77,7 @@ object TEXT NOT NULL`
boolean: 0,
array: '[]',
object: '{}',
deleted: 0,
},
]

Expand Down
72 changes: 64 additions & 8 deletions test/index-callback.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,70 @@ import test from 'tape'
import { create } from './utils.js'

const docs = [
{ docId: 'A', seq: 1, versionId: '1', links: [], updatedAt: '' },
{ docId: 'A', seq: 2, versionId: '2', links: ['1'], updatedAt: '' },
{ docId: 'A', seq: 3, versionId: '3', links: ['1'], updatedAt: '' },
{ docId: 'A', seq: 4, versionId: '4', links: ['2', '3'], updatedAt: '' },
{ docId: 'A', seq: 5, versionId: '5', links: ['4'], updatedAt: '' },
{ docId: 'A', seq: 6, versionId: '6', links: ['4'], updatedAt: '' },
{ docId: 'A', seq: 7, versionId: '7', links: ['4'], updatedAt: '' },
{ docId: 'A', seq: 8, versionId: '8', links: ['5', '6'], updatedAt: '' },
{
docId: 'A',
seq: 1,
versionId: '1',
links: [],
updatedAt: '',
deleted: false,
},
{
docId: 'A',
seq: 2,
versionId: '2',
links: ['1'],
updatedAt: '',
deleted: false,
},
{
docId: 'A',
seq: 3,
versionId: '3',
links: ['1'],
updatedAt: '',
deleted: false,
},
{
docId: 'A',
seq: 4,
versionId: '4',
links: ['2', '3'],
updatedAt: '',
deleted: false,
},
{
docId: 'A',
seq: 5,
versionId: '5',
links: ['4'],
updatedAt: '',
deleted: false,
},
{
docId: 'A',
seq: 6,
versionId: '6',
links: ['4'],
updatedAt: '',
deleted: false,
},
{
docId: 'A',
seq: 7,
versionId: '7',
links: ['4'],
updatedAt: '',
deleted: false,
},
{
docId: 'A',
seq: 8,
versionId: '8',
links: ['5', '6'],
updatedAt: '',
deleted: false,
},
]

test('onceWriteDoc called for each doc', async (t) => {
Expand Down
35 changes: 27 additions & 8 deletions test/scenarios.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,26 @@ import test from 'tape'
import { create, permute } from './utils.js'

const docs = [
{ docId: 'A', versionId: '1', links: [], updatedAt: '' },
{ docId: 'A', versionId: '2', links: ['1'], updatedAt: '' },
{ docId: 'A', versionId: '3', links: ['1'], updatedAt: '' },
{ docId: 'A', versionId: '4', links: ['2', '3'], updatedAt: '' },
{ docId: 'A', versionId: '5', links: ['4'], updatedAt: '' },
{ docId: 'A', versionId: '6', links: ['4'], updatedAt: '' },
{ docId: 'A', versionId: '7', links: ['4'], updatedAt: '' },
{ docId: 'A', versionId: '8', links: ['5', '6'], updatedAt: '' },
{ docId: 'A', versionId: '1', links: [], updatedAt: '', deleted: false },
{ docId: 'A', versionId: '2', links: ['1'], updatedAt: '', deleted: false },
{ docId: 'A', versionId: '3', links: ['1'], updatedAt: '', deleted: false },
{
docId: 'A',
versionId: '4',
links: ['2', '3'],
updatedAt: '',
deleted: false,
},
{ docId: 'A', versionId: '5', links: ['4'], updatedAt: '', deleted: false },
{ docId: 'A', versionId: '6', links: ['4'], updatedAt: '', deleted: false },
{ docId: 'A', versionId: '7', links: ['4'], updatedAt: '', deleted: true },
{
docId: 'A',
versionId: '8',
links: ['5', '6'],
updatedAt: '',
deleted: true,
},
]

const scenarios = [
Expand All @@ -22,6 +34,7 @@ const scenarios = [
links: ['1'],
forks: [],
updatedAt: '',
deleted: 0,
},
},
{
Expand All @@ -32,6 +45,7 @@ const scenarios = [
links: ['1'],
forks: ['2'],
updatedAt: '',
deleted: 0,
},
},
{
Expand All @@ -42,6 +56,7 @@ const scenarios = [
links: ['2', '3'],
forks: [],
updatedAt: '',
deleted: 0,
},
},
{
Expand All @@ -52,6 +67,7 @@ const scenarios = [
links: ['4'],
forks: [],
updatedAt: '',
deleted: 0,
},
},
{
Expand All @@ -62,6 +78,7 @@ const scenarios = [
links: ['4'],
forks: ['5'],
updatedAt: '',
deleted: 0,
},
},
{
Expand All @@ -72,6 +89,7 @@ const scenarios = [
links: ['4'],
forks: ['5', '6'],
updatedAt: '',
deleted: 1,
},
},
{
Expand All @@ -82,6 +100,7 @@ const scenarios = [
links: ['5', '6'],
forks: ['7'],
updatedAt: '',
deleted: 1,
},
},
]
Expand Down
57 changes: 48 additions & 9 deletions test/timestamps.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,48 @@ test('If doc has timestamp, it is used to select winner', async (t) => {
const updated2 = new Date(1999, 0, 2).toISOString()
const updated3 = new Date(1999, 0, 3).toISOString()
const docs = [
{ docId: 'A', versionId: '1', links: [], updatedAt: updated3 },
{ docId: 'A', versionId: '2', links: ['1'], updatedAt: updated2 },
{ docId: 'A', versionId: '3', links: ['1'], updatedAt: updated1 },
{ docId: 'B', versionId: '1', links: [], updatedAt: updated3 },
{ docId: 'B', versionId: '2', links: ['1'], updatedAt: updated1 },
{ docId: 'B', versionId: '3', links: ['1'], updatedAt: updated2 },
{
docId: 'A',
versionId: '1',
links: [],
updatedAt: updated3,
deleted: false,
},
{
docId: 'A',
versionId: '2',
links: ['1'],
updatedAt: updated2,
deleted: false,
},
{
docId: 'A',
versionId: '3',
links: ['1'],
updatedAt: updated1,
deleted: false,
},
{
docId: 'B',
versionId: '1',
links: [],
updatedAt: updated3,
deleted: false,
},
{
docId: 'B',
versionId: '2',
links: ['1'],
updatedAt: updated1,
deleted: false,
},
{
docId: 'B',
versionId: '3',
links: ['1'],
updatedAt: updated2,
deleted: false,
},
]

const { indexer, api, cleanup } = create({ extraColumns: 'timestamp NUMBER' })
Expand All @@ -25,6 +61,7 @@ test('If doc has timestamp, it is used to select winner', async (t) => {
versionId: '2',
links: ['1'],
forks: ['3'],
deleted: 0,
}

const head = api.getDoc(expected.docId)
Expand All @@ -40,6 +77,7 @@ test('If doc has timestamp, it is used to select winner', async (t) => {
versionId: '3',
links: ['1'],
forks: ['2'],
deleted: 0,
}

const head = api.getDoc(expected.docId)
Expand All @@ -55,9 +93,9 @@ test('If doc has timestamp, it is used to select winner', async (t) => {
test('If doc has no timestamp, version is used to select a deterministic winner', async (t) => {
const updatedAt = new Date().toISOString()
const docs = [
{ docId: 'A', versionId: '1', links: [], updatedAt },
{ docId: 'A', versionId: '2', links: ['1'], updatedAt },
{ docId: 'A', versionId: '3', links: ['1'], updatedAt },
{ docId: 'A', versionId: '1', links: [], updatedAt, deleted: false },
{ docId: 'A', versionId: '2', links: ['1'], updatedAt, deleted: false },
{ docId: 'A', versionId: '3', links: ['1'], updatedAt, deleted: false },
]

const { indexer, api, cleanup } = create()
Expand All @@ -67,6 +105,7 @@ test('If doc has no timestamp, version is used to select a deterministic winner'
versionId: '3',
links: ['1'],
forks: ['2'],
deleted: 0,
}

indexer.batch(docs)
Expand Down
3 changes: 2 additions & 1 deletion test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ export function create({ extraColumns = '' } = {}) {
versionId TEXT NOT NULL,
links TEXT NOT NULL,
forks TEXT NOT NULL,
updatedAt TEXT NOT NULL
updatedAt TEXT NOT NULL,
deleted INTEGER NOT NULL
${extraColumns ? ', ' + extraColumns : ''}
)
WITHOUT ROWID`
Expand Down
Loading