0.55.8 • Published 1 year ago

@startupjs/sharedb-access v0.55.8

Weekly downloads
546
License
MIT
Repository
-
Last release
1 year ago

@startupjs/sharedb-access

Installation

  • Install npm: npm install @startupjs/sharedb-access
  • Install yarn: yarn add @startupjs/sharedb-access

Usage

const shareDbAccess = require('sharedb-access')
new shareDbAccess(backend[, options])

Parameters

  • backend - your ShareDB backend instance
  • options(optional) - object with options:
    • options.dontUseOldDocs: false - if true don't save unupdated docs for update action
    • options.opCreatorUserIdPath - path to 'userId' for op's meta

Using sharedb-access you can control create, read, update, and delete database operation for every collection. You can use two types of rules: allow and deny. By default all the operations are denied. So, you should add some rules to allow them. If at least one allow-rule allows the write, and no deny-rules deny the write, then the write is allowed to proceed.

You can call allow and deny-rules as many times as you like. The functions should return true if they think the operation should be allowed for allow rules and denied for deny-rules. Otherwise they should return false, or nothing at all (undefined).

Create

// Allow create-operation for collection 'items'

// docId - id of your doc for access-control
// doc   - document object
// session - your connect session

backend.allowCreate('items', async (docId, doc, session) => {
  return true
})

// Deny creation if user is not admin
backend.denyCreate('items', async (docId, doc, session) => {
  return !session.isAdmin
})

// So, finally, only admins can create docs in 'items' collection
// the same results is if you just write:

backend.allowCreate('items', async (docId, doc, session) => {
  return session.isAdmin
})

Read

Interface is like create-operation

backend.allowRead('items', async (docId, doc, session) => {
  // Allow all operations
  return true
})

backend.denyRead('items', async (docId, doc, session) => {
  // But only if the reader is owner of the doc
  return doc.ownerId !== session.userId
})

Delete

Interface is like create-operation

backend.allowDelete('items', async (docId, doc, session) => {
  // Only owners can delete docs
  return doc.ownerId === session.userId
})

backend.denyDelete('items', async (docId, doc, session) => {
  // But deny deletion if it's a special type of docs
  return doc.type === 'liveForever'
})

Update

// docId - id of your doc for access-control
// oldDoc  - document object (before update)
// session - your connect session
// ops    - array of OT operations
// newDoc  - document object (after update)

const allowUpdateAll = async (docId, oldDoc, session, ops, newDoc) => {
  return true
}

backend.allowUpdate('items', allowUpdateAll);

MIT License 2020

0.57.0-canary.0

1 year ago

0.56.0-alpha.0

1 year ago

0.55.7

1 year ago

0.55.8

1 year ago

0.55.0-alpha.1

2 years ago

0.55.0

2 years ago

0.53.0

2 years ago

0.55.0-alpha.12

2 years ago

0.54.1

2 years ago

0.54.2

2 years ago

0.54.0

2 years ago

0.51.0

2 years ago

0.52.1

2 years ago

0.52.0

2 years ago

0.50.13

2 years ago

0.50.12

2 years ago

0.50.5

2 years ago

0.48.0

3 years ago

0.49.0

3 years ago

0.46.4

3 years ago

0.47.0

3 years ago

0.46.0

3 years ago

0.46.1

3 years ago

0.45.0

3 years ago

0.44.23

3 years ago

0.44.24

3 years ago

0.44.0

3 years ago

0.43.0

3 years ago

0.41.2

3 years ago

0.41.0

3 years ago

0.42.6

3 years ago

0.42.0

3 years ago

0.43.1

3 years ago

0.39.11

4 years ago

0.40.0

4 years ago

0.39.0

4 years ago

0.38.0

4 years ago

0.37.3

4 years ago

0.37.0

4 years ago

0.36.0

4 years ago

0.35.10

4 years ago

0.35.2

4 years ago

0.35.0

4 years ago

0.34.0

4 years ago

0.33.7

4 years ago

0.33.0

4 years ago

0.33.0-alpha.4

4 years ago

0.33.0-alpha.0

4 years ago

0.32.1

4 years ago

0.32.0

4 years ago

0.31.28

4 years ago

0.31.29

4 years ago

0.31.27

4 years ago

0.31.26

4 years ago

0.31.25

4 years ago

0.31.22

4 years ago

0.31.21

4 years ago

0.31.18

4 years ago

0.31.19

4 years ago

0.31.0

4 years ago

0.30.9

4 years ago

0.30.7

4 years ago

0.30.6

4 years ago

0.30.5

4 years ago

0.30.4

4 years ago

0.30.2

4 years ago

0.30.3

4 years ago

0.30.1

4 years ago

0.30.0

4 years ago

0.29.11

5 years ago

0.29.4

5 years ago

0.29.2

5 years ago

0.29.0

5 years ago

0.28.0

5 years ago

0.27.0

5 years ago

0.26.0

5 years ago

0.25.0

5 years ago

0.24.0

5 years ago

0.23.51

5 years ago

0.23.45

5 years ago