als-store
Version:
Library for streamlined file management and advanced data caching, featuring intelligent file searching, dynamic cache control, and flexible file operations
62 lines (45 loc) • 1.44 kB
Markdown
Example for merging docs:
```js
const {Store} = require('als-store')
const root = new Store({dirPath:__dirname});
(async function() {
// get all files with values from docs folder
const { results } = await root.dir('docs').values().get()
// Merge the content
const content = results.map(({value}) => value).join('\n');
// Create new file readme with merged connent and save it
await root.create('readme.md',content).save()
})()
```
Example for user management:
```js
const { string, email, lowercase, id } = require('als-schema')
const {Store} = require('./index')
const { join } = require('path')
// Create user schema
const schema = {
id,
name: [lowercase, string(1, 25)],
email: [lowercase, email],
}
// Create user's store
const dirPath = join(__dirname, 'users')
const userStore = new Store({ dirPath, schema })
// Save user
const userData = {
id:undefined,
name: 'Alex',
email: 'alex@mail.com'
}
const userBio = 'some bio'
const user = userStore.create(userData,userBio)
console.log(user.name) // Alex.alex@mail[.]com
console.log(user.$name) // alex.alex@mail[.]com.60d215t2817459018
console.log(user.params) // { name: 'alex', email: 'alex@mail.com', id: '60d215t2817459018' }
const {id} = user.params
await user.save()
// find user
await userStore.filter(({params}) => params.id === id).first()
```