Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Babel plugin changes #4938

Merged
merged 63 commits into from
Oct 18, 2022
Merged
Show file tree
Hide file tree
Changes from 62 commits
Commits
Show all changes
63 commits
Select commit Hold shift + click to select a range
3f85e81
Data and LinkingObjects types
Sep 9, 2022
54d29d2
Add support for static properties
Sep 13, 2022
88a933a
Decorators support, needs tsc fixing
Sep 13, 2022
d19ae3a
Fix types for decorators
Sep 13, 2022
ca09443
Updated template app and added decorators
Sep 14, 2022
c53deb7
Add decorators to full schema test
Sep 15, 2022
4d71b49
Add test for type precedence
Sep 16, 2022
530b316
Disable test
Sep 16, 2022
8eed123
Add babel-plugin-decorators check
Sep 20, 2022
63f3678
Add checking for decorator import
Sep 20, 2022
c63b955
WIP on template app and rollup config
Sep 20, 2022
bfcc625
Add Realm.Types ctors
Sep 21, 2022
c407354
Add missing ref
Sep 21, 2022
428f06e
Handle non-TS source files with a warning
Sep 21, 2022
e6f564a
WIP on PR fixes
Sep 21, 2022
363c99e
Fix Realm.Types
Sep 21, 2022
e888bb2
PR changes
Sep 21, 2022
16fdf05
Move index.test.ts back
Sep 21, 2022
8e16be8
PR fixes
Sep 21, 2022
8f7acab
Allow override for JS template model
Sep 21, 2022
728e873
WIP
Sep 21, 2022
20fe632
Update template sync script to allow overrides, and update templates …
Sep 21, 2022
823cfec
Type tweak
Sep 22, 2022
fa339d6
Add publish workflow for babel plugin
Sep 22, 2022
270327a
Add README
Sep 22, 2022
ad1190e
Add babel-plugin deps properly
Sep 22, 2022
214a261
Update readme
Sep 22, 2022
7f8d194
Add missing primary key
Sep 22, 2022
b8022c8
Add types for plugin statics
Sep 22, 2022
fc6c1f4
Add table
Sep 22, 2022
2295820
WIP on README
Sep 26, 2022
467c1c6
Throw error if schema already defined
Sep 26, 2022
bb92553
WIP on moving to class-based models syntax
Sep 26, 2022
f4f34aa
WIP with example app etc
Sep 26, 2022
5c2b7e1
Add support and test for function default properties
Sep 27, 2022
e27517b
WIP
Sep 27, 2022
b2f6d59
Delete generate
Sep 27, 2022
7bb1e10
Add type for RequiredFields on Realm.Object
Sep 27, 2022
5b1c627
Add docblock and fix types
Sep 27, 2022
cbb07ac
Update example JS
Sep 29, 2022
cea7529
WIP on template app
Sep 29, 2022
e6b47a6
Update template RN version
Sep 29, 2022
7a0d6e2
Revert templates and example
Sep 29, 2022
360007f
Readme
Sep 29, 2022
0210b20
Readme update
Sep 30, 2022
e319fa0
Ignore generated types dir
Sep 30, 2022
0030310
PR tidy
Sep 30, 2022
0a789a8
More PR tidy
Sep 30, 2022
cefb8bd
Fix lint
Sep 30, 2022
facf2a9
Add workflow for babel plugin
Sep 30, 2022
e2a70a9
Readme fix
Oct 3, 2022
c5357d8
Update errors to use buildCodeFrameError
Oct 3, 2022
ae5d3c7
Revert "Fix lint"
Oct 11, 2022
c6627d8
Revert "Add docblock and fix types"
Oct 11, 2022
904b374
Revert "Add type for RequiredFields on Realm.Object"
Oct 11, 2022
f2b951f
Revert "Add support and test for function default properties"
Oct 11, 2022
bedf559
PR changes
Oct 11, 2022
e131633
Remove decorators from output
Oct 11, 2022
ec86219
Move decorators into Realm package and throw if used at runtime
Oct 11, 2022
358466c
Remove decorators from plugin and remove rollup in favour of tsc
Oct 11, 2022
77286a3
Handle decorators imported from Realm
Oct 11, 2022
5190b15
Changelog
Oct 11, 2022
036fa7b
Update pr-babel-plugin.yml
kraenhansen Oct 18, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions .github/workflows/pr-babel-plugin.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
name: Testing Babel Plugin (Pull Request)

env:
REALM_DISABLE_ANALYTICS: 1

on:
pull_request:
paths:
- "*"
- "lib/**"
- "src/**"
- "types/**"
- "react-native/**"
- "vendor/**"
# Integration tests
- "integration-tests/**"
- "packages/babel-plugin/**"
- ".github/workflows/pr-babel-plugin.yml"
# No need to run when updating documentation
- "!**.md"
jobs:
job:
name: Build & test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
submodules: "recursive"

- uses: actions/setup-node@v1
with:
node-version: 16
registry-url: https://registry.npmjs.org/

- name: Install npm v7
run: npm install -g npm@8

# Caches
- uses: actions/cache@v2
name: cached node_modules
id: cache-node-modules
with:
path: node_modules
key: node-modules-${{ github.job }}-${{ hashFiles('package-lock.json') }}

- name: ccache
uses: hendrikmuhs/ccache-action@v1
with:
key: babel-plugin

- name: Prepend ccache executables to the PATH
run: echo PATH="/usr/lib/ccache:/usr/local/opt/ccache/libexec:$PATH" >> $GITHUB_ENV

- name: Install realm node dependencies
run: npm ci

- name: Bootstrap @realm/babel-plugin
run: npx lerna bootstrap --scope @realm/babel-plugin --include-dependencies
env:
npm_config_realm_local_prebuilds: ${{ github.workspace }}/prebuilds
- name: Build realm react
working-directory: packages/babel-plugin
run: npm run build

- name: Run tests
working-directory: packages/babel-plugin
run: npm run test
33 changes: 33 additions & 0 deletions .github/workflows/publish-babel-plugin.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Publish Babel Plugin

on:
push:
tags:
- babel-plugin-v*
env:
REALM_DISABLE_ANALYTICS: 1

jobs:
test-build-and-publish:
name: Test, build & publish
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 16
registry-url: https://registry.npmjs.org/
- name: Install npm v7
run: npm install -g npm@8
# Install the root package (--ignore-scripts to avoid downloading or building the native module)
- run: npm ci --ignore-scripts
# Bootstrap lerna sub-packages
- run: npx lerna bootstrap
# Test the package
- run: npm test
working-directory: packages/babel-plugin
# Publish the package
- run: npm publish
working-directory: packages/babel-plugin
env:
NODE_AUTH_TOKEN: ${{secrets.npm_token}}
11 changes: 11 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,17 @@
"--testNamePattern=${input:testNamePattern}"
]
},
{
"type": "node",
"name": "Node Babel Plugin Tests",
"request": "launch",
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"disableOptimisticBPs": true,
"cwd": "${workspaceFolder}/packages/babel-plugin",
"runtimeExecutable": "npm",
"args": ["run", "test", "--", "--runInBand", "--watchAll=false", "--testNamePattern=${input:testNamePattern}"]
},
{
"type": "node",
"request": "launch",
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Based on Realm JS v10.21.1: See changelog below for details on enhancements and
```
### Enhancements
* Small improvement to performance by caching JSI property String object [#4863](https://github.com/realm/realm-js/pull/4863)
* Added new package `@realm/babel-plugin` to enable definining your Realm models using standard Typescript syntax [#4938](https://github.com/realm/realm-js/pull/4938)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The initial announcement of the plugin can be done here. For the future I suggest that plugin's package has its own changelog.


### Fixed
* None
Expand Down
1 change: 1 addition & 0 deletions integration-tests/tests/src/tests/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ import "./sync/flexible";
import "./sync/asymmetric";
import "./sync/sync-as-local";
import "./transaction";
import "./types";
48 changes: 48 additions & 0 deletions integration-tests/tests/src/tests/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
////////////////////////////////////////////////////////////////////////////
//
// Copyright 2022 Realm Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////

import { expect } from "chai";
import Realm from "realm";

describe("Realm.Types namespace", () => {
// We specify explicit types on the instance so TS will error if the type def is wrong
it("Realm.Types.Decimal128 can be constructed", () => {
const instance: Realm.Types.Decimal128 = new Realm.Types.Decimal128();
expect(instance).to.be.instanceOf(Realm.BSON.Decimal128);
});

it("Realm.Types.ObjectId can be constructed", () => {
const instance: Realm.Types.ObjectId = new Realm.Types.ObjectId();
expect(instance).to.be.instanceOf(Realm.BSON.ObjectId);
});

it("Realm.Types.UUID can be constructed", () => {
const instance: Realm.Types.UUID = new Realm.Types.UUID();
expect(instance).to.be.instanceOf(Realm.BSON.UUID);
});

it("Realm.Types.Date can be constructed", () => {
const instance: Realm.Types.Date = new Realm.Types.Date();
expect(instance).to.be.instanceOf(Date);
});

it("Realm.Types.Data can be constructed", () => {
const instance: Realm.Types.Data = new Realm.Types.Data();
expect(instance).to.be.instanceOf(ArrayBuffer);
});
});
55 changes: 26 additions & 29 deletions lib/extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -500,35 +500,32 @@ module.exports = function (realmConstructor) {
};
}

// TODO: Remove this now useless object.
var types = Object.freeze({
BOOL: "bool",
INT: "int",
FLOAT: "float",
DOUBLE: "double",
STRING: "string",
DATE: "date",
DATA: "data",
OBJECT: "object",
LIST: "list",
});
Object.defineProperty(realmConstructor, "Types", {
get: function () {
if (typeof console != "undefined") {
/* eslint-disable no-console */
var stack = new Error().stack.split("\n").slice(2).join("\n");
var msg = "`Realm.Types` is deprecated! Please specify the type name as lowercase string instead!\n" + stack;
if (console.warn != undefined) {
console.warn(msg);
} else {
console.log(msg);
}
/* eslint-enable no-console */
}
return types;
},
configurable: true,
});
realmConstructor.Types = {
Decimal128: realmConstructor.BSON.Decimal128,
ObjectId: realmConstructor.BSON.ObjectId,
UUID: realmConstructor.BSON.UUID,
Date: Date,
Data: ArrayBuffer,
// These types cannot be constructed, but are re-exported so they can be used for `instanceOf`
List: realmConstructor.List,
Set: realmConstructor.Set,
Dictionary: realmConstructor.Dictionary,
};

// Decorators are not intended to be used at runtime and are removed from the source
// by @realm/babel-plugin. Therefore, if a decorator is called, this means it is being
// used outside of @realm/babel-plugin (or the plugin is incorrectly configured), so
// we should throw
function handleDecoratorCall(decoratorName) {
return function () {
throw new Error(
`The @${decoratorName} decorator cannot be used without the \`@realm/babel-plugin\` Babel plugin. Please check that you have installed and configured the Babel plugin.`,
);
};
}

realmConstructor.index = handleDecoratorCall("index");
realmConstructor.mapTo = handleDecoratorCall("mapTo");

if (!realmConstructor.JsonSerializationReplacer) {
Object.defineProperty(realmConstructor, "JsonSerializationReplacer", {
Expand Down
2 changes: 2 additions & 0 deletions packages/babel-plugin/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dist
types/generated/
Empty file.
Loading