Skip to content

Next.jsでのyarn fireschema rules #38

@npg418

Description

@npg418

Next.jsのプロジェクトにてyarn fireschema rules path/to/schema.tsをするとSyntaxErrorが出ます。
esmやらtypescriptやらあたりなのかわからないがimportをmodule外で使うなと怒られるました(そこら辺の知識がない)。
tsconfig.jsonで何か変えたほうが良いのでしょうか。
直接node_modules/fireschema内のbinスクリプトを実行しても、結果が同じでした。
バージョンはNext.jsでのttypescriptの導入法がわからなかったので5.0.0-36です。

package.json(バージョン関係)
{
    // 略
    "engines": {
        "node": "16.x"
    },
    "dependencies": {
        // 略
        "firebase": "^9.11.0",
        "firebase-admin": "^11.1.0",
        "fireschema": "^5.0.0-36",
        "next": "12.2.5",
        "zod": "^3.19.1",
        // 略
    },
    "devDependencies": {
        // 略
        "ts-node": "^10.9.1",
        "typescript": "^4.8.3"
    }
}
tsconfig.json
{
    "compilerOptions": {
        "target": "es5",
        "lib": ["dom", "dom.iterable", "esnext"],
        "allowJs": true,
        "skipLibCheck": true,
        "strict": true,
        "strictNullChecks": true,
        "forceConsistentCasingInFileNames": true,
        "noEmit": true,
        "esModuleInterop": true,
        "module": "esnext",
        "moduleResolution": "node",
        "resolveJsonModule": true,
        "isolatedModules": true,
        "jsx": "preserve",
        "incremental": true,
        "baseUrl": "./"
    },
    "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
    "exclude": ["node_modules"]
}
schema.ts
import { DataModel, FirestoreModel, rules, timestampType } from 'fireschema';
import { z } from 'zod';

import { documentRefType } from './ZodDocumentRef';

import type { Property } from 'csstype';
import type { Merge } from 'type-fest';

export const ArticleType = z.object({
    id: z.string(),
    title: z.string().optional(),
    body: z.string().optional(),
    tags: documentRefType().array(),
    slug: z.string(),
    publish: z.boolean(),
    release: timestampType(),
});

type Article = z.infer<typeof ArticleType>;

type ArticleDecoded = Merge<Article, { release: Date }>;

const ArticleModel = new DataModel({
    schema: ArticleType,
    decoder: (data: Article): ArticleDecoded => ({
        ...data,
        release: data.release.toDate(),
    }),
    selectors: q => ({
        getRecent: (limit?: number) => [
            q.where('publish', '==', true),
            q.where('release', '<=', new Date()),
            q.orderBy('release', 'desc'),
            q.limit(limit || 10),
        ],
    }),
});

export const TagType = z.object({
    name: z.string(),
    color: z.string(),
    dev_only: z.boolean(),
});

type Tag = z.infer<typeof TagType>;

type TagDecoded = Merge<Tag, { color: Property.Color }>;

const TagModel = new DataModel({
    schema: TagType,
    decoder: (data: Tag): TagDecoded => ({
        ...data,
        color: data.color as Property.Color,
    }),
});

const firestoreModel = new FirestoreModel({
    'function isAdmin()': `
        return exists(${rules.basePath}/administrator/$(request.auth.uid));
    `,
    collectionGroups: {},
    '/article/{articleId}': {
        model: ArticleModel,
        allow: {
            read: rules.and(
                'request.time >= resource.data.release',
                'resource.data.publish == true'
            ),
        },
    },
    '/tag/{tagId}': {
        model: TagModel,
        allow: {
            read: true,
        },
    },
    '/{document=**}': {
        allow: {
            read: rules.or('isAdmin()', 'resource == null'),
        },
    },
});

export default firestoreModel;
スタックトレース
import { DataModel, FirestoreModel, rules, timestampType } from 'fireschema';
^^^^^^
SyntaxError: Cannot use import statement outside a module
  at Object.compileFunction (node:vm:360:18)
  at wrapSafe (node:internal/modules/cjs/loader:1055:15)
  at Module._compile (node:internal/modules/cjs/loader:1090:27)
  at Module.m._compile (/home/npg418/npg418-homepage/node_modules/ts-node/src/index.ts:1618:23)
  at Module.m._compile (/home/npg418/npg418-homepage/node_modules/ts-node/src/index.ts:1618:23)
  at Module._extensions..js (node:internal/modules/cjs/loader:1180:10)
  at require.extensions.<computed> (/home/npg418/npg418-homepage/node_modules/ts-node/src/index.ts:1621:12)
  at Object.require.extensions.<computed> [as .ts] (/home/npg418/npg418-homepage/node_modules/ts-node/src/index.ts:1621:12)
  at Module.load (node:internal/modules/cjs/loader:1004:32)
  at Function.Module._load (node:internal/modules/cjs/loader:839:12)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions