realm在node/electron中使用

2 篇文章 0 订阅
0 篇文章 0 订阅

realm数据库

  • 存储文件为 .realm文件
  • 用起来比较类似mongodb
  • realm的写操作需要在 .write() 函数中调用
  • .create() 函数用于获取数据表的对象
  • realm目前最高只支持到electron4.x

引入模块

const Realm = require('realm')
const path = require('path')
const { app } = require('electron')

定义数据表模型

const Test1 = {
  name: 'Test1',
  primaryKey: 'id',
  properties: {
    id: 'string', // String
    content: 'data?', // ArrayBuffer
    createdDate: 'date', // Date
    type: {
      type: 'string',
      default: 'abc
    },
    list: { // List 关联Test2表
      type: 'list',
      objectType: 'Test2'
    },
    marked: 'bool', // Boolean
  }
}
const Test2 = {
  name: 'Test2',
  primaryKey: 'id',
  properties: {
    id: 'string',
    name: 'string'
  }
}

realm 实例化

let realm = new Realm({
  path: 存储路径/文件名.realm,  // path.join(app.getPath('documents'), demo.realm)
   schema: [Test1, Test2], // 数据表
   schemaVersion: 1, // 数据库版本号
   migration: (oldRealm, newRealm) => {} // 用于判断数据库版本号进行数据迁移、管理
})

写入

realm.write(() => {
  // 新建Test1
  let createObj= realm.create('Test1', {
    id: 'sad13123',
    content: 'new Buffer.from('test')',
    createdDate: new Date(),
    type: 'sdasdasda',
    marked: false,
  })
  // 关联Test1写入Test2
  createObj.list.push({
    id: 'j12j3ij',
    name: 'sss'
  })
})

更新

realm.write(() => {
  // 更新Test1的数据
  let updateObj = realm.create('Test1', {
    id: 'sad13123', // 更新的对象的主键 用于指明更新的对象
    marked: true
  })
  // 更新关联的数据
  updateObj.list.forEach(item => {
	item.name = '新内容'
  })
})

删除

// 获取要删除的对象
let delObj = realm.objectForPrimaryKey('Test1', 'sad13123')
realm.write(() => {
  // 删除关联的内容
  realm.delete(delObj.list)
  // 删除本身
  realm.delete(delObj)
})

查询

// 使用主键查询
let findObj = realm.objectForPrimaryKey('Test1', 'sad13123')
// 全查 (如需操作如筛选可以使用filter等数组函数进行操作)
let selectAll = realm.objects('Test1')

关闭数据库

取消对 .realm文件的占用,使用频率较高感觉可以不关闭数据库,关闭应用时也会关闭

realm.close()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在 Spring Boot 使用 Shiro 配置过滤器有以下几个步骤: 1. 引入 Shiro 和 Spring Boot 的相关依赖: ``` <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring-boot-starter</artifactId> <version>1.7.1</version> </dependency> ``` 2. 配置 Shiro: 在 Spring Boot ,可以在 application.yml 或 application.properties 文件配置 Shiro。以下是一个 application.yml 的例子: ``` shiro: filter-chain-definition-map: /login: anon /logout: anon /**: authc ``` 这里配置了三个过滤器: - /login:匿名访问,不需要认证和授权。 - /logout:匿名访问,不需要认证和授权。 - /**:需要认证和授权。 3. 配置 ShiroFilterFactoryBean: 在 Spring Boot ,可以通过 ShiroFilterFactoryBean 来配置 Shiro 的过滤器。以下是一个示例: ``` @Configuration public class ShiroConfig { @Bean public ShiroFilterFactoryBean shiroFilterFactoryBean(DefaultWebSecurityManager securityManager) { ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean(); shiroFilterFactoryBean.setSecurityManager(securityManager); shiroFilterFactoryBean.setLoginUrl("/login"); shiroFilterFactoryBean.setUnauthorizedUrl("/unauthorized"); // 设置过滤器链 Map<String, String> filterChainDefinitionMap = new LinkedHashMap<>(); filterChainDefinitionMap.put("/logout", "logout"); filterChainDefinitionMap.put("/login", "anon"); filterChainDefinitionMap.put("/**", "authc"); shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap); return shiroFilterFactoryBean; } @Bean public DefaultWebSecurityManager securityManager(Realm realm) { DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager(); securityManager.setRealm(realm); return securityManager; } @Bean public Realm realm() { return new MyRealm(); } } ``` 这里配置了三个过滤器: - /logout:logout 过滤器用于处理退出登录,不需要认证和授权。 - /login:anon 过滤器用于匿名访问,不需要认证和授权。 - /**:authc 过滤器用于认证和授权。 4. 配置 Realm: 在 Spring Boot ,可以通过实现 Realm 接口来自定义认证和授权逻辑。以下是一个示例: ``` public class MyRealm extends AuthorizingRealm { @Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) { SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo(); // 添加授权信息 authorizationInfo.addRole("admin"); authorizationInfo.addStringPermission("user:list"); return authorizationInfo; } @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { UsernamePasswordToken upToken = (UsernamePasswordToken) token; String username = upToken.getUsername(); String password = new String(upToken.getPassword()); // 校验用户名和密码 if ("admin".equals(username) && "123456".equals(password)) { return new SimpleAuthenticationInfo(username, password, getName()); } else { throw new AuthenticationException("用户名或密码错误"); } } } ``` 这里模拟了一个简单的认证和授权逻辑。当用户名和密码为 admin 和 123456 时,认证通过,授权拥有 admin 角色和 user:list 权限。 以上是在 Spring Boot 使用 Shiro 配置过滤器的基本步骤。需要注意的是,Shiro 的过滤器和拦截器是有顺序的,如果顺序不正确,可能会导致一些问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值