denoでNCMB(ニフクラ mobile backend)を使うためのSDKです。非公式ライブラリになります。Node.js × TypeScript環境でも動作します。
mBaaSでサーバー開発不要! | ニフクラ mobile backend
denoをインストールします。
オブジェクトを保存するコードです。 test.ts
として保存してください。
// 最新版
import NCMB, { NCMBObject, NCMBQuery, NCMBInstallation, NCMBUser } from 'https://raw.githubusercontent.com/NCMBMania/ncmb_deno/master/deno/ncmb.ts'
// バージョン指定
import NCMB, { NCMBObject, NCMBQuery, NCMBInstallation, NCMBUser } from 'https://raw.githubusercontent.com/NCMBMania/ncmb_deno/0.1.8/deno/ncmb.ts'
// 初期化
new NCMB('YOUR_APPLICATION_KEY', 'YOUR_CLIENT_KEY')
// オブジェクト作成
const hello = new NCMBObject('HelloDeno')
// 値を設定して保存
await hello
- .set('message', 'Hello world')
- .set('number', 100)
- .save()
// 保存できていればオブジェクトIDが出力されます
console.log(hello.get('objectId'))
// ACLの使い方
const acl = new NCMBAcl()
acl
- .setPublicReadAccess(true)
- .setPublicWriteAccess(false)
const hello2 = new NCMBObject('HelloDeno')
await hello2
- .set('message', 'Hello world')
- .set('number', 100)
- .set('acl', acl)
- .save()
ACLは次のメソッドがあります。
- setPublicReadAccess(bol: boolean): NCMBAcl
- setPublicWriteAccess(bol: boolean): NCMBAcl
- setUserReadAccess(user: NCMBUser, bol: boolean): NCMBAcl
- setUserWriteAccess(user: NCMBUser, bol: boolean): NCMBAcl
- setRoleReadAccess(role: string, bol: boolean): NCMBAcl
- setRoleWriteAccess(role: string, bol: boolean): NCMBAcl
Denoの場合
--allow-net
は必須です。 --allow-read
は設定ファイルを読み込む際に必須です。
deno run --allow-net --allow-read test.ts
npmなどで行います。
npm install ncmb_ts -S
基本的に使い方は変わりませんが、ルートでのasync/awaitはサポートされていないので注意してください。
import { NCMB, NCMBObject, NCMBQuery, NCMBInstallation, NCMBUser } from 'ncmb_ts'
const ncmb = new NCMB('YOUR_APPLICATION_KEY', 'YOUR_CLIENT_KEY')
const hello = new NCMBObject('HelloDeno');
(async () => {
await hello
.set('message', 'Hello world')
.set('number', 100)
.save()
console.log(hello.get('objectId'))
await hello
.set('number', 200)
.save()
console.log(hello.get('number'))
const query = ncmb.Query('HelloDeno')
query.equalTo('objectId', 'ypk03ZHeJxjSnSM1')
query.limit(1)
const results = await query.fetchAll()
console.log(results)
})();
VS Codeなどで入力補完が使えます。
const query = new NCMBQuery('QueryTest');
const results = await query.in('array', ['c']).fetchAll()
console.log(results.length)
console.log(results[0].get('objectId'))
利用できるオペランド
- equalTo(key: string, value:any): NCMBQuery
- setOperand(key: string, value:any, ope: string|null): NCMBQuery
- notEqualTo(key: string, value:any): NCMBQuery
- lessThan(key: string, value: number | Date): NCMBQuery
- lessThanOrEqualTo(key: string, value: number | Date): NCMBQuery
- greaterThan(key: string, value: number | Date): NCMBQuery
- greaterThanOrEqualTo(key: string, value: number | Date): NCMBQuery
- in(key: string, values: any[]): NCMBQuery
- notIn(key: string, values: any[]): NCMBQuery
- exists(key: string, exist: boolean | null): NCMBQuery
- regularExpressionTo(key: string, regex: string): NCMBQuery
- inArray(key: string, values: any): NCMBQuery
- notInArray(key: string, values: any): NCMBQuery
- allInArray(key: string, values: any): NCMBQuery
- near(key: string, location: NCMBGeoPoint): NCMBQuery
- withinKilometers(key: string, location: NCMBGeoPoint, maxDistance: number): NCMBQuery
- withinMiles(key: string, location: NCMBGeoPoint, maxDistance: number): NCMBQuery
- withinRadians(key: string, location: NCMBGeoPoint, maxDistance: number): NCMBQuery
- withinSquare(key: string, southWestVertex: NCMBGeoPoint, northEastVertex: NCMBGeoPoint): NCMBQuery
const d = new NCMBObject('HelloDeno')
await d
.set('objectId', 'DPnmQfMGTMuSS44Q')
.fetch()
hello.delete()
const user = await NCMBUser.singUp('tester', 'tester')
会員登録メール要求を呼び出します。
await NCMBUser.requestSignUpEmail('tester@moongift.jp')
const user = await NCMBUser.login('tester', 'tester')
const emailLogin = await NCMBUser.loginWithEmail(mailAddress, password)
const anony = await NCMBUser.loginAsAnonymous()
await user
.set('displayName', 'Test user')
.save();
await NCMBUser.logout()
await NCMBUser.requestPasswordReset(mailAddress)
await user.delete()
const installation = new NCMBInstallation
await installation
- .set('deviceToken', 'aaaa')
- .set('deviceType', 'android')
- .save();
await installation
- .set('deviceToken', 'bbbb')
- .save();
await installation.delete();
NCMBGeoPointを使います。
const geo = new NCMBGeoPoint(35.0, 100.0);
const hello = new NCMBObject('HelloDeno')
await hello
.set('message', 'Hello world')
.set('number', 100)
.set('acl', acl)
.set('hello1', hello)
.set('geo', geo)
.save()
MIT.